Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 82 83 [84] 85 86 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 94243 times)

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: Programming Help Thread (For Dummies)
« Reply #1245 on: April 20, 2014, 07:33:29 am »

My girlfriend (who is more familiar with Lua than I am) was telling me that it should be capitalized. Then again it could have just been sleep deprived ramblings at 3 AM.

I just did a quick test, it is case=sensitive and the actual keyword is "and"

Antsan

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1246 on: April 20, 2014, 04:40:56 pm »

I am attempting to do a game and for that game I thought I'd use collaborative diffusion. I've got the idea from this paper:
http://www.cs.colorado.edu/~ralex/papers/PDF/OOPSLA06antiobjects.pdf

This paper offers the following diffusion equation:
u(0,t+1)=u(0,t)+d*sum[across neighbours i](u(i,t) - u(0,t))
with u(0,t) being the diffusion value of the center,
u(i,t) being the diffusion values of it's neighbours and
d being the diffusion coefficient determining how fast diffusion happens. d is in the range [0,0.5]

This formula is very likely not correct, as for 2 neighbours, d=0.5 and u(i,t)=0 for all neighbours i it returns u(0,t+1)=0, which just doesn't seem right - I don't think that's how diffusion is supposed to behave.
Even worse: With more neighbours the value for u(0,t+1) can become negative and this is definitely not right, as the magnitude of the negatvie value becomes even bigger when the diffusion value at the center is more positive beforehand.
Does anyone know the correct diffusion equation for discrete grids?

I tried to do some corrections myself, such as not multiplying with d but instead with d/(number of neighbours) or using the sum
sum[across neighbours i](u(i,t) - u(0,t)/(number of neighbours))
instead, but the results of those seemed strange.
Logged
Taste my Paci-Fist

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1247 on: April 20, 2014, 06:31:54 pm »

If all neighbours have a value of 0, why should the value of the centre change at all?
Logged

Antsan

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1248 on: April 21, 2014, 02:48:21 am »

Because what is in the center dissipates into the neighbours. It diffuses.
The diffusion equation is calculated for any position on the map once per time step. In the end the diffusion equation is the rule of a continuously valued cellular automaton.
Logged
Taste my Paci-Fist

AlleeCat

  • Bay Watcher
  • Black, the beast, descends from shadows...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1249 on: April 21, 2014, 03:54:08 am »

Now we have another problem. I'm having a syntax error at the end of an if loop for some reason.
Code: [Select]
if gamestate == "credits" then
love.graphics.setColor(255,0,0)
love.graphics.setFont("buttonFont")
love.graphics.setColor(0,0,0)
love,graphics.print("Back",20,652)
end
I cut out some extraneous bits in the middle. It's a long loop, but I'f got an even longer loop in the same file that works just fine. The error is telling me the syntax error is on line 52, and the only code on that line is "end".

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #1250 on: April 21, 2014, 04:08:44 am »

You typed a comma instead of a period on the 2nd to last line. After love.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

AlleeCat

  • Bay Watcher
  • Black, the beast, descends from shadows...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1251 on: April 21, 2014, 05:02:14 pm »

And this is why coding in the middle of the night is a bad idea.

EDIT:
I Also noticed another typo in that "Back" should be printed at 20,562 instead of 20,652. It was being drawn offscreen.
« Last Edit: April 21, 2014, 05:32:55 pm by AlleeCat »
Logged

My Name is Immaterial

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1252 on: May 06, 2014, 01:00:55 am »

Hey, does anyone know how to make data selection start where the last one left off in Google Drive's Spreadsheets? I've got about 3500 datapoints starting in January of '09 going to April of '14, so I can't do it manually and stay sane.
Here's the code I have:
Code: [Select]
=average(Daily!D2:D32)
=average(Daily!D33:D60)
=average(Daily!D61:D91)
=average(Daily!D92:D121)
=average(Daily!D122:D152)
=average(Daily!D153:D182)
=average(Daily!D183:D213)
=average(Daily!D214:D244)
=average(Daily!D245:D274)
=average(Daily!D275:D305)
=average(Daily!D306:D335)
=average(Daily!D336:D366)

and I want that to continue to be and so on:
Code: [Select]
=average(Daily!D367:D397)
=average(Daily!D398:D425)
=average(Daily!D426:D456)

Should I just write a script that will set up this data field for me?

My Name is Immaterial

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1253 on: May 06, 2014, 11:26:16 am »

Or should I just do it in Excel?

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1254 on: May 06, 2014, 12:23:39 pm »

Excel's probably easier when it comes to formulas, afaik.
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1255 on: May 27, 2014, 02:52:50 am »

I'm having an issue in SDL2 with creating a window inside a class constructor, and I haven't any real idea about the cause.

Code: (C++) [Select]
class Window {
    public:
        Window(const char* title,
            int x=SDL_WINDOWPOS_UNDEFINED,
            int y=SDL_WINDOWPOS_UNDEFINED,
            int w=640, int h=480,
            Uint32 flags=SDL_WINDOW_RESIZABLE);
        virtual ~Window();
        SDL_Window* handle;
};

Window::Window(const char* title, int x, int y, int w, int h, Uint32 flags) {
    handle = SDL_CreateWindow(title, x, y, w, h, flags);
    // Check that the window was successfully made
    if (handle == NULL) {
        printf("Could not create window: %s\n", SDL_GetError());
    }
    SDL_Delay(5000);
    std::cout << "Window created." << std::endl;
}

Window::~Window() {
    SDL_DestroyWindow(handle);
    std::cout << "Window destroyed." << std::endl;
}

If I create the window in the main function, it works as expected:

Code: [Select]
int main(int argc, char **argv) {
    Application app;
    app.initialize();
    Window* window = new Window("This is a Test!");

    app.sleep(5000);

    return 0;
}

The problem occurs if I move the window creation to the Application initialization and store the pointer as an Application member:

Code: [Select]
int Application::initialize() {
    try {
        SDL sdl(SDL_INIT_VIDEO|SDL_INIT_TIMER);
        window = new Window("This is a Test!");
        return 0;
    } catch (const InitError& err) {
        std::cerr
        << "Error initializing SDL:  "
        << err.what()
        << std::endl;
    }
    return 1;
}

/*-----*/

int main(int argc, char **argv) {
    bool quit = false;
    SDL_Event event;

    Application app;
    app.initialize();

    app.sleep(5000);

    return 0;


This causes the window to pop up at first as it is apparently created successfully - then the window closes right away after the call to app.initialize() is finished, long before the Window destructor is called. The program still exits successfully, and at no point does SDL throw an error, the window just disappears.

For now, I'm just going to construct the Window in the main function so I can move on, but I'd really appreciate it if someone could explain the exact issue here. Constructing the new object in the Application constructor and storing the pointer as a member are clearly problematic, I just don't know why.

If it's just a quirk of SDL2 then that's one thing, but I suspect there is some aspect of the way C++ handles data that I'm missing.
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1256 on: May 27, 2014, 01:39:47 pm »

It might be from this line:
Code: [Select]
SDL sdl(SDL_INIT_VIDEO|SDL_INIT_TIMER);
I'm not sure what your SDL class does, but in this case sdl is being initialized then the destructor is called when the function returns. If you're quitting SDL on destruction then that might close the window immediately.
Logged

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1257 on: May 27, 2014, 02:13:13 pm »

/facepalm

Yes, that was it. Thank you.
Logged

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1258 on: June 08, 2014, 02:57:37 pm »

The (1st) problem was that I forgot to reset the least costly square so far...

Having some trouble with a* in c++:
Code: [Select]

struct Node
{
int parent;

int x;
int y;
int z;

int g; /// Cost to this point
int h; /// Est. cost to target
int f; /// Total Est. Cost

MOV_DIR direction;

Node(int,int,int,int,int,int, MOV_DIR);

};

Node::Node(int parentS, int xS, int yS, int zS, int gS, int hS, MOV_DIR dir)
{
parent = parentS;
x = xS;
y = yS;
z = zS;
g = gS;
h = hS;
f = g + h;
direction = dir;

}

bool AgentKing::aStarVer1(ObjLoc ori, ObjLoc tar, MovementPath& path)
{
    bool debug = true;

const int NSEW_COST = 10;
bool done = false;
bool fail = false;



int nodeCount = 0;
vector<Node> node;

int nodesThisRound = 0;

vector<int> open;
vector<int> closed;

node.push_back(Node(-1, ori.x, ori.y, ori.z, 0, 0, MD_DWN));
closed.push_back(nodeCount); nodeCount++;

int nodeToCheck = 0;
int n2cP = ori.p;
int n2cX;
int n2cY;
int n2cZ;


int leastEstCost = 999999999;
bool nodeClosed;



while(done == false && fail == false)
{
///Open all valid adjacent nodes
///First get the current location

if(debug)printf("\n\nNODE BEING CHECKED = %i", nodeToCheck);
            if(debug)printf("\nNODE X =%i", node[nodeToCheck].x);
            if(debug)printf("\nNODE Y =%i", node[nodeToCheck].y);
            if(debug)printf("\nNODE Z =%i", node[nodeToCheck].z);
n2cX = node[nodeToCheck].x;
n2cY = node[nodeToCheck].y;
n2cZ = node[nodeToCheck].z;
if (manhattanCostEst(n2cZ, n2cY, n2cX, tar) == 0)
{
done = true;
if(debug)printf("\nPATHING TARGET REACHED");
}
else
{
///Then check all 6-degrees
if (n2cY > 0 && locationPassable(n2cP, n2cZ, n2cY, n2cX, MD_N)) ///Northern Check
{
node.push_back(Node(nodeToCheck, n2cX, n2cY+1, n2cZ,
node[nodeToCheck].g + NSEW_COST, manhattanCostEst(n2cZ, n2cY+1, n2cX, tar), MD_N));
open.push_back(nodeCount); nodeCount++;
nodesThisRound++;
}
if (n2cY < planetYdimension - 1 && locationPassable(n2cP, n2cZ, n2cY, n2cX, MD_S)) ///Southern Check
{
node.push_back(Node(nodeToCheck, n2cX, n2cY-1, n2cZ,
node[nodeToCheck].g + NSEW_COST, manhattanCostEst(n2cZ, n2cY-1, n2cX, tar), MD_S));
open.push_back(nodeCount); nodeCount++;
nodesThisRound++;
}
                if (n2cX > 0 && locationPassable(n2cP, n2cZ, n2cY, n2cX, MD_E)) ///Eastern Check
{
node.push_back(Node(nodeToCheck, n2cX+1, n2cY, n2cZ,
node[nodeToCheck].g + NSEW_COST, manhattanCostEst(n2cZ, n2cY, n2cX+1, tar), MD_E));
open.push_back(nodeCount); nodeCount++;
nodesThisRound++;
}
                if (n2cX < planetXdimension - 1 && locationPassable(n2cP, n2cZ, n2cY, n2cX, MD_W)) ///Western Check
{
node.push_back(Node(nodeToCheck, n2cX-1, n2cY, n2cZ,
node[nodeToCheck].g + NSEW_COST, manhattanCostEst(n2cZ, n2cY, n2cX-1, tar), MD_W));
open.push_back(nodeCount); nodeCount++;
nodesThisRound++;
}
                if (n2cZ < planetZdimension - 1 && locationPassable(n2cP, n2cZ, n2cY, n2cX, MD_UP)) ///Upper Check
{
node.push_back(Node(nodeToCheck, n2cX, n2cY, n2cZ+1,
node[nodeToCheck].g + NSEW_COST, manhattanCostEst(n2cZ+1, n2cY, n2cX, tar), MD_UP));
open.push_back(nodeCount); nodeCount++;
nodesThisRound++;
}
                if (n2cZ > 0 && locationPassable(n2cP, n2cZ, n2cY, n2cX, MD_DWN)) ///Lower Check
{
node.push_back(Node(nodeToCheck, n2cX, n2cY, n2cZ-1,
node[nodeToCheck].g + NSEW_COST, manhattanCostEst(n2cZ-1, n2cY, n2cX, tar), MD_DWN));
open.push_back(nodeCount); nodeCount++;
nodesThisRound++;
}

while(nodesThisRound > 0) /// Find the lowest est cost of the <= 6 nodes opened and register node to check
{
    //if(debug)printf("\nNODES THIS ROUND = %i", nodesThisRound);
if (node[nodeCount - nodesThisRound].f < leastEstCost)
{
leastEstCost = node[nodeCount - nodesThisRound].f;
nodeToCheck = nodeCount - nodesThisRound;
}




nodesThisRound--;
}

///find if an opened node picked, if not, pick last new node created
nodeClosed = false;
for(int i = 0; i < closed.size(); i++)
{
if (closed[i] == nodeToCheck)
nodeClosed = true;
}
if (nodeClosed == true)
{
nodeToCheck = open[open.size()-1];
}
///Finally close the open node to be checked
for(int i = 0; i < open.size(); i++)
{
if(open[i] == nodeToCheck)
{
    ///if(debug)printf("\nNODES OPEN = %i", node.size());
open.erase(open.begin()+i);
closed.push_back(nodeToCheck);
fail = false;
break;
}
else
                    {
                        fail = true;
                    }
}

}

        if (done == true && fail == false)
        {
            if(debug)printf("DONE WITH PATHING, CAPTURING PATH");
            while (nodeToCheck != 0 && path.pathLength < 255)
            {
                nodeCount = 0;
                path.direction[nodeCount] = node[nodeToCheck].direction;
                path.pathLength++;
                nodeToCheck = node[nodeToCheck].parent;
                nodeCount++;
            }
        }
}



    return done;

}



I realize this is a massive block of code. For some reason it just keep recycling through a few points very close to origin without venturing outwards(the node checker I mean, it's an infinite loop). It's probably something obvious that I'm just not seeing...
« Last Edit: June 08, 2014, 03:24:19 pm by thobal »
Logged
Signature goes here.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1259 on: June 08, 2014, 03:19:10 pm »

So, I need to find a way to get the size (in pixels) of some text,  the only problem is it isn't monospaced,
I know I could find the size of each letter, and have a function that counts how many of what letters are in the text and add together the sizes, but I was wondering if there was an easier way?

I'm using SDL, is there any way to get the size of a surface? 

I haven't learned SDL2 yet, when I do I need to convert all of my program,  I was going to wait until I finish this first part of it first.

anyways, any ideas?

after a tiny bit of research I think I already found the answer I was looking for,

is there a way to delete a post?

« Last Edit: June 08, 2014, 03:24:19 pm by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.
Pages: 1 ... 82 83 [84] 85 86 ... 91