Page 1 of 1

Problems with for-loops

PostPosted: Thu Jul 18, 2013 7:15 pm
by Jukeri
Hello GE community, I am a new guy here, but not completely inexperienced in game making. (I'm still an amateur lol)

I'm not sure if this is the right place to post this, but here goes.

I have 5 armies, (both side1/side2) and 5 fronts as done in arrays, and a for-loop to handle them all. However, testing it makes it come out with very odd results... I seem to get the values only for the last 3 armies/fronts that I want to update, but not for the first two. Some of them are not updating correctly too. There is a value z used in the for loop for going through all the armies and fronts.

Example:
Code: Select all
/check who is attacking
//used to check if the damage is taken to attacker's zeal
//or defend HP
void checkattacking() {
   for ( z = 0; z <= 3; z++ ) {
      if (turned == 1 & side1[z].isattacking == 1) {
         turned = 0;
         side1[z].isattacking = 0;
         side2[z].isattacking = 1;
         side1[z].zeal = 0;
         side2[z].defhp = 0;
         side2[z].zeal = 30;
         side1[z].defhp = 30;
   PlaySound2("data/scchh.wav", 1.000000, 1, 0.000000);
      }
      else if (turned == 1 & side2[z].isattacking == 1) {
         turned = 0;
         side1[z].isattacking = 1;
         side2[z].isattacking = 0;
         side1[z].zeal = 30;
         side2[z].zeal = 0;
         side2[z].defhp = 30;
         side1[z].defhp = 0;
   PlaySound2("data/scchh.wav", 1.000000, 1, 0.000000);
      }
      //check damage against defhp or zeal
      //depending on attacker
  for ( z = 0; z <= 3; z++ ) {
      if (side1[z].isattacking == 1 & side2[z].isattacking == 0) {
         if (fronts[z].differ < 0) {
            side1[z].zeal = side1[z].zeal + differ;
    PlaySound2("data/tuneuiw.wav", 1.000000, 1, 0.000000);
                             }
         if (fronts[z].differ > 0) {
            side2[z].defhp = side2[z].defhp - differ;
    PlaySound2("data/scchh.wav", 1.000000, 1, 0.000000);
                             }
      }
      if (side2[z].isattacking == 1 & side1[0].isattacking == 0) {
         if (fronts[z].differ > 0) {
            side1[z].defhp = side1[z].defhp - differ;
    PlaySound2("data/tuneuiw.wav", 1.000000, 1, 0.000000);
                             }
         if (fronts[z].differ < 0) {
            side2[z].zeal = side2[z].zeal + differ;
    PlaySound2("data/scchh.wav", 1.000000, 1, 0.000000);
                             }
      }
 }
}
};


What could be the problem, or is there a better way to update a group of 5 armies/5 fronts (the battlegrounds where the armies fight) or 5 anything that are made with structs?

Also, minuscule question: Is there a way to make GE's script editor format stuff better? :D Formatting stuff in Notepad++ and importing them back in results in a jumbled mess.

Re: Problems with for-loops

PostPosted: Thu Jul 18, 2013 8:52 pm
by skydereign
Jukeri wrote:I have 5 armies, (both side1/side2) and 5 fronts as done in arrays, and a for-loop to handle them all. However, testing it makes it come out with very odd results... I seem to get the values only for the last 3 armies/fronts that I want to update, but not for the first two. Some of them are not updating correctly too. There is a value z used in the for loop for going through all the armies and fronts.

Five armies? What is an army, and which variables represent them. Your function starts with a for loop to go through 4 values in the arrays, so side1/side2/fronts are all arrays of size 4, so I'm not sure where the 5 armies come from. Now, to point out a rather serious problem with that code, you use a for loop with z within a for loop already using z. This will mess things up, and that first for loop will only run once. If you want to nest for loops, you need to use a different variable.

Jukeri wrote:Also, minuscule question: Is there a way to make GE's script editor format stuff better? :D Formatting stuff in Notepad++ and importing them back in results in a jumbled mess.

This is probably fixable, but there isn't anything you can do for now (except change how you format your code).

Re: Problems with for-loops

PostPosted: Fri Jul 19, 2013 10:03 am
by Jukeri
Actually I did not even notice there being 2 for loops in the same function lol.

Code: Select all
struct army {
    int hastanks;
    int hascannons;
    int hasasltguns;
    int zeal;
    int defhp;
    int side;
    int isattacking;
    int agression; //agression 0 = Lazy-mode, 1 = normal, 2 = push
    int stamina;
    int turned;
    int hasmoved;
    int tempbonuses;
    int bonuses;
    int roll;
    int queuelen; //reinforcement queue values, basically
    int queueslot; //current queue slot to handle
    int queue[15]; //moving stuff to the right until it hits the end (queuelen) of the array
    int senttanks; //tanks moving in queue
    int sentaslt; //assault guns in queue
    int sentcannons; //cannons in queue
};


This is what currently represents an army. The Side1/Side2 are arrays made from struct army. The idea in the game is that basically 5 armies on 5 fronts fight trying to push to the enemy's base. When this is done, that front is won for the side who pushed all the way through (and other fronts possibly gain bonuses because of this). The goal is to push all or more lines than the opponent into the enemy's base to win.

The trouble that I'm getting is that the for loop seems erratic/odd and only checks some of the side1/side2 armies.

I guess I should paste the full global code I'm currently using
Code: Select all
//some global stuff
//such as reinforcement pools
int s1tanks;
int s1aslt;
int s1cannons;
int s1airstrikes;
int s2tanks;
int s2aslt;
int s2cannons;
int s2airstrikes;




int z; //amount of armies; used for handling all the armies
z = 0;


struct army {
    int hastanks;
    int hascannons;
    int hasasltguns;
    int zeal;
    int defhp;
    int side;
    int isattacking;
    int agression; //agression 0 = Lazy-mode, 1 = normal, 2 = push
    int stamina;
    int turned;
    int hasmoved;
    int tempbonuses;
    int bonuses;
    int roll;
    int queuelen; //reinforcement queue values, basically
    int queueslot; //current queue slot to handle
    int queue[15]; //moving stuff to the right until it hits the end (queuelen) of the array
    int senttanks; //tanks moving in queue
    int sentaslt; //assault guns in queue
    int sentcannons; //cannons in queue
};




//roll for initiative
//if <5 player 1
//else player 2
int initiative;
 
 
//let's create our armies
//let's build a list to store up to 5 fronts
//NOTE: Think of our sides as an posX
//to the left (-) is our side 1; the better negative results are made
//the more damage left side (side 1) does
//on the right (+) is side 2; they deal damage out of positive results of difference


struct army side1[5];
struct army side2[5];


//the virtual "battlegrounds"
//you can use this to tune the size of maps
int size_of_maps = 10;
int genslot = 0;

//map properties (10 = base size)
//tracks the maps and position in them
struct map {
    int type[10]; //base type
    int modifier1[10]; //modifiers
    int modifier2[10]; //there can be up to 2 modifiers in one tile
    int x; // pos in front, if x = 0 side 1 is victorious, if x = 10 side 2
    int victor; //1 = side 1, 2 = side 2
    int differ; //difference of rolls is tracked HERE
};
//arrays map our battlegrounds
//0 = plains, 1 = forest, 2 = swamp, 3 = hills, 4 = settled area
//modifiers:
//0 = nothing, 1 = extreme cold, 2 = radio silence, 3 = fortified
struct map fronts[5];



//this function will generate our maps
void genmaps() {
     for ( size_of_maps; genslot < size_of_maps; genslot++ ) {
           fronts[0].type[genslot] = rand(4);
           fronts[1].type[genslot] = rand(4);
           fronts[2].type[genslot] = rand(4);
           fronts[3].type[genslot] = rand(4);
           fronts[4].type[genslot] = rand(4);
           fronts[0].modifier1[genslot] = rand(3);
           fronts[1].modifier1[genslot] = rand(3);
           fronts[2].modifier1[genslot] = rand(3);
           fronts[3].modifier1[genslot] = rand(3);
           fronts[4].modifier1[genslot] = rand(3);
     }
};


//randomly roll the first offensive
void attackfirst() {
 
   initiative = rand(10);
   for ( z = 0; z < 3; z++) {
   if (initiative <= 5) {
      //PlaySound2("data/tunewiu.wav", 1.000000, 1, 0.000000);
            side1[z].isattacking = 1;
            side2[z].isattacking = 0;
            side1[z].zeal = 15;
            side2[z].defhp = 30;

          }
   
     else {
      //PlaySound2("data/tuneuiw.wav", 1.000000, 1, 0.000000);
            side2[z].isattacking = 1;
            side1[z].isattacking = 0;
            side2[z].zeal = 15;
            side1[z].defhp = 30;
   }
                            }
};


//check if either side had ordered reinforcements and where
//START POINT AT ONE LESS THAN THE LAST SLOT IN QUEUE
//it is checked separately
void checkreinf() {
    //first check if the queueslot is out of range
   for ( z = 0; z <= 3; z++) {
      if (side1[z].queueslot >= 4)
         side1[z].queueslot = 0;
 
    //then check the just arrived reinforcements
      if (side1[z].queue[4] == 1) {
         side1[z].hastanks = 1;
         side1[z].queue[4] = 0;
      }
    //never send stuff if the first slot is full
      if (side1[z].senttanks == 1 & side1[0].queue[0] < 1) {
         side1[z].queue[0] = 1;
         side1[z].senttanks = 0;
      }


      if (side1[z].queue[side1[z].queueslot] > 0) {
         side1[z].queue[side1[z].queueslot+1] = side1[0].queue[side1[0].queueslot];
         side1[z].queue[side1[z].queueslot] = 0;
         side1[z].queueslot = side1[z].queueslot + 1;
      }
   }
};


//check who is attacking
//used to check if the damage is taken to attacker's zeal
//or defend HP
void checkattacking() {
   for ( z = 0; z <= 3; z++ ) {
      if (turned == 1 & side1[z].isattacking == 1) {
         turned = 0;
         side1[z].isattacking = 0;
         side2[z].isattacking = 1;
         side1[z].zeal = 0;
         side2[z].defhp = 0;
         side2[z].zeal = 30;
         side1[z].defhp = 30;
   PlaySound2("data/scchh.wav", 1.000000, 1, 0.000000);
      }
      else if (turned == 1 & side2[z].isattacking == 1) {
         turned = 0;
         side1[z].isattacking = 1;
         side2[z].isattacking = 0;
         side1[z].zeal = 30;
         side2[z].zeal = 0;
         side2[z].defhp = 30;
         side1[z].defhp = 0;
   PlaySound2("data/scchh.wav", 1.000000, 1, 0.000000);
      }
      //check damage against defhp or zeal
      //depending on attacker
      if (side1[z].isattacking == 1 & side2[z].isattacking == 0) {
         if (fronts[z].differ < 0) {
            side1[z].zeal = side1[z].zeal + differ;
    PlaySound2("data/tuneuiw.wav", 1.000000, 1, 0.000000);
                             }
         if (fronts[z].differ > 0) {
            side2[z].defhp = side2[z].defhp - differ;
    PlaySound2("data/scchh.wav", 1.000000, 1, 0.000000);
                             }
      }
      if (side2[z].isattacking == 1 & side1[0].isattacking == 0) {
         if (fronts[z].differ > 0) {
            side1[z].defhp = side1[z].defhp - differ;
    PlaySound2("data/tuneuiw.wav", 1.000000, 1, 0.000000);
                             }
         if (fronts[z].differ < 0) {
            side2[z].zeal = side2[z].zeal + differ;
    PlaySound2("data/scchh.wav", 1.000000, 1, 0.000000);
                             }
      }
 }
};


//check if someone counterattacks or advances
void checkturning() {
   for ( z = 0; z <= 3; z++) {
      if (side1[z].isattacking == 1 & side1[z].zeal <= 0)
         turned = 1;
      if (side2[z].isattacking == 1 & side2[z].zeal <= 0)
         turned = 1;
   }
};
//check if the attacking side advances
//also check if this equals the victory condition
void checkadvance() {
    for ( z = 0; z < 3; z++) {
      if (side1[z].isattacking == 1 & side2[z].defhp <= 0) {
         frontlinex = frontlinex + 1;
         side2[z].defhp = 15;
      }
      if (side2[z].isattacking == 1 & side1[z].defhp <= 0) {
         frontlinex = frontlinex - 1;
         side1[z].defhp = 30;
      }
      if (frontlinex == -5) {
 
         }
   }
};
//check modifiers, basically just code both sides with their rock-paper-scissors logic
//tempbonuses stores the values that aren't finalized yet
//remember: The better in negative are values (gaining more negative difference)
//the stronger was the attack of side 1 (-)
void checkmodifiers() {


    for ( z = 0; z <= 3; z++) {
      if (side1[z].hasasltguns == 1 & side2[z].hastanks == 1)
         side1[z].tempbonuses = side1[z].tempbonuses + 5;
 
      if (side1[z].hastanks == 1)
         side1[z].tempbonuses = side1[z].tempbonuses + 4;
 
      if (side1[z].hascannons == 1)
         side1[z].tempbonuses = side1[z].tempbonuses + 5;
 
      if (side2[z].hasasltguns == 1 & side1[0].hastanks == 1)
         side2[z].tempbonuses = side2[0].tempbonuses + 5;


      if (side2[z].hastanks == 1)
         side2[z].tempbonuses = side2[0].tempbonuses + 4;
 
      if (side2[z].hascannons == 1)
         side2[z].tempbonuses = side2[0].tempbonuses + 5;


 
      side1[z].bonuses = side1[z].tempbonuses;
      side2[z].bonuses = side2[z].tempbonuses;
 
      side1[z].tempbonuses = 0;
      side2[z].tempbonuses = 0;
 
 }
 
};

Re: Problems with for-loops

PostPosted: Sat Jul 20, 2013 4:25 am
by skydereign
Jukeri wrote:The trouble that I'm getting is that the for loop seems erratic/odd and only checks some of the side1/side2 armies.

Your for loops only loop through values for 0, 1, 2, and 3. These will therefore always skip side1[4] and side2[4]. Another thing to note, are you deliberating using the bitwise and instead of logical and? Normally you should be using && instead of a single &. You have a lot of conditions determining if and how each army attacks, so have you checked to make sure they are all properly set?