glob2-devel
[Top][All Lists]
Advanced

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

[glob2-devel] I'm confused by the code in NewNicowar::control_attacks


From: Joe Wells
Subject: [glob2-devel] I'm confused by the code in NewNicowar::control_attacks
Date: Fri, 10 Aug 2007 18:26:54 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)

Okay, I'm lucky this time I happened to be running under the debugger
when I got a segmentation fault.  (It should *not* be necessary to run
under the debugger.  Programs that crash should leave core dump
files.)

This time, the guilty code is in NewNicowar::control_attacks.  (I have
no idea if this is related to the segmentation violation I reported
earlier.)  For reference, I've appended the code of this function
below.

This code is doing several things that I don't understand.

The part most directly at fault is that it is assuming the command b =
echo.get_building_register().get_building(i) stores a valid building
in b.  This time, it does not, and dereferencing the building with
b->locked[true] causes a crash.  So it should definitely check that b
is valid before using it.

But anyway, how could this call possibly work?  The number i is an
index of an attack flag.  (Right?)  How could this possibly be
connected to swimming pools?  And why is code for controlling attack
flags concerned with swimming pools at all?

Maybe this is code that was copied from elsewhere and insufficiently
adapted afterward?

I'm confused.

-- 
Joe

======================================================================
void NewNicowar::control_attacks(Echo& echo)
{
        choose_enemy_target(echo);

        if(target!=-1)
        {
                int number_attacks=0;
                if(war)
                {
                        number_attacks=2;
                }
                
        
                if(attack_flags.size() < number_attacks)
                {
                        attack_building(echo);
                }
        }

        BuildingSearch bs_pool(echo);
        bs_pool.add_condition(new 
SpecificBuildingType(IntBuildingType::SWIMSPEED_BUILDING));
        int num_pool=bs_pool.count_buildings();
        
        for(int i=0; i<attack_flags.size(); ++i)
        {
                Building* b = echo.get_building_register().get_building(i);
                if((num_pool && b->locked[true]) || (num_pool==0 && 
b->locked[false]))
                {
                        ManagementOrder* mo_destroy=new DestroyBuilding(i);
                        echo.add_management_order(mo_destroy);
                }
        }
}




reply via email to

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