glob2-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [glob2-devel] Cleanups...


From: simon schuler
Subject: Re: [glob2-devel] Cleanups...
Date: Mon, 24 Jan 2005 21:39:45 +0000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041229

Nuage wrote:

Thank you for pointing out possible possible improvement of the code.

Here is some explaination of the code:

in Map.h:
protected:
    //Used for scheduling computation time.
    bool gradientUpdated[32][MAX_NB_RESSOURCES][2];

As written, this is used to schedule the computation of the ressource
gradients. Ressources gradient is currently the one of the most time
consuming computation. In order to make the game feeling smooth, the
idea is to make an idential amount of ressource gradient computation
on each tick. Currently we make exactly one ressource gradient
computation on each tick. (History: when they where slower, we did used
to make one gradient on 2 ticks).

That's exactly what my patch also does, simply without the for loops


This is done in Map.cpp
void Map::syncStep(Uint32 stepCounter)
currenlty at line 1204

Once a ressource gradient is computed, which can be uniquely identified
with "t", "r", "s", then the gradientUpdated[t][r][s] is set to "true".
Once they are all updated, they are all set back to "false". They are
all set to "false" when an external event occurs, like at the game
loading or creation.

Therfor, it can't by simplified with only one "bool resetgradients" as
you suggested. Your latest code suggestion (correction.diff) would
only compute one gradient ressouce: for the team[0], the ressource[0]
(#defined WOOD) ("wheat"), for the swimming[0] (non-swimming) units.

no it doesn't, the
   static unsigned int t=0,  r=0, s=0;
variables are only initialized with 0 when the function is executed the first time, from the on the variables keep their values. (static definition) I've attached a small prog that demonstrates the function. Just compile and run it and you will see that it does the same like the old code. For better readabilty the =0's could also be removed, but then you would have to make sure, that the resetgradients is false before you execute the function first. The benefit of this patch is not allways have to execute these loops, (the test if (!gradientUpdated[t][r][s]) was executed MAX_RESSOURCES*numberOfTeam times on average, and allways the counting variables of the loop had to be incremented and tested) and not have to use this array anymore (saving some memory)


If you tested it shortly, you probably didn't noticed any bad behaviour.

Played some games like this, I didn't notice strange behaviour.

It's not obvious why. To ensure fair and smooth start for each team, all
the ressources gradients are computed at the beggining (init/loading)
of a new game. And as far as the gradient are quite "flexible", you
won't notice any bad baheviour util a ressource is seriously consumed,
or the ressouce is removed from a place. But then the unit would go
to a place where there is no more ressource.

Just ask if something is not clear.

Thank you!
Nuage


simon schuler wrote:

I was a bit browsing through the code today, triing to understand the whole code. Sometimes the code isn't easy to understand, but that's normal for such a project... I found a lot of loops, which are (in my opinion) useless. I have attached a patch to remove some useless loops, and replace them with equivalent code.

Summary of the patch: I removed the bool gradientUpdated[32][MAX_NB_RESSOURCES][2] array and replaced it with equivalent faster code.

simon



------------------------------------------------------------------------

_______________________________________________
glob2-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/glob2-devel



_______________________________________________
glob2-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/glob2-devel


Simon
#include <stdio.h>
#define MAX_RESSOURCES 8

bool resetgradients=false;

void syncStep(void)
{
        //growRessources();
        //for (int i=0; i<sizeSector; i++)
        //      sectors[i].step();
        
        // We only update one gradient per step:
        static unsigned int t=0,  r=0, s=0;
        int numberOfTeam=2;//=game->session.numberOfTeam;
        if(resetgradients || t>=numberOfTeam)
        {
                t=r=s=0;
                resetgradients=false;
        }
        printf("%u %u %u\n", t, r, s);
        //updateRessourcesGradient(t, r, (bool)s);
        s++;
        if(s>=2)
        {
                s=0;
                r++;
                if(r>=MAX_RESSOURCES)
                {
                        r=0;
                        t++;
                }
        }
}

int main()
{
        for(int i=0;i<50; i++)
        {
                syncStep();
        }
        printf("resetgradients\n");
        resetgradients=true;
        for(int i=0;i<10; i++)
        {
                syncStep();
        }
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]