Why won't this work.

J

JoeC

I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
m.atIn(rteam[lp]);} //reads in the attracking units to map
panager
for(int lp = 0; lp != yteam.size(); lp++){
m.dtIn(yteam[lp]);} //reads defender
} else { //same but for the other turn
for(int lp = 0; lp != rteam.size(); lp++){
m.atIn(yteam[lp]);}
for(int lp = 0; lp != yteam.size(); lp++){
m.dtIn(rteam[lp]);}
}
m.fight(); //directs units in the same space to fingt.
//rteam[1].tomark(); <-this works units are killed here
//yteam[1].disbersed(); <-this also works units are dispered
correctly
kill(rteam); //kills any red units
kill(yteam); //kills any purple units
m.clear(); //clears the map manager.
changeTurn(red, ppl); //changes red, purple turns

All my stuff works here in the main loop but once I send units rteam,
yteam to an object a refrences they don't get the combat affects:

Here is some sample code:

void mapmgt::atIn(unit& u){
int x = u.getXloc();
int y = u.getYloc();
h[y][x].at.push_back(u);

struct hold{
vector<unit>at; //holds the attackers
vector<unit>dt; //holds the defenders
float getAt(); //adds attacking factors
float getDt(); //addus up defending factors
void killA(); //attacker eliminated
void dispA(); //attacker disbersed
void dispD(); //defenders same as attackers
void killD();
};

void mapmgt::fight(){
float atk = 0;
float def = 0;
float odds = 0;
for(int lp1 = 0; lp1 != ylen; lp1++){
for(int lp2 = 0; lp2 != xlen; lp2++){
if(h[lp2][lp1].at.size() > 0 && h[lp2][lp1].dt.size() > 0){
atk = h[lp2][lp1].getAt();
def = h[lp2][lp1].getDt();
odds = atk/def;
int roll = (rand()%6)+1;
//int mod = t[b->GetSpace(lp2,lp1)].getDef(); <-this dosn't
work, seperat issue
//roll+=mod;
int res = tbl.result(roll, odds);
if(res == 0){h[lp2][lp1].killA();} //atk elim <-these don't
seem to be working
if(res == 1){h[lp2][lp1].dispA();} //atk disp imposing the
combat results
if(res == 2){h[lp2][lp1].dispD();} //def disp on the units
if(res == 3){h[lp2][lp1].killD();} //def elim

MessageBox(NULL, "There is fighting!", "simulation!", MB_OK);
}
}
}

}

Example of combat effects, works in the main loop but not here and this
is my main problem:

void hold::killA(){
MessageBox(NULL, "Atk elim!", "simulation!", MB_OK);
for(int lp = 0; lp != at.size(); lp++){
at[lp].tomark();
}
}
 
M

mlimber

JoeC said:
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
[snip]

Problem #1: your index is going past the end of the vector. Use <
rteam.size(), not != rteam.size().

If there are more problems, post a complete but minimal sample that
demonstrates the problem (i.e., one that we can cut and paste unchanged
into our editors to see the problem). Also see these guidelines for
posting code that doesn't work:

http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Cheers! --M
 
J

JoeC

mlimber said:
JoeC said:
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
[snip]

Problem #1: your index is going past the end of the vector. Use <
rteam.size(), not != rteam.size().

If there are more problems, post a complete but minimal sample that
demonstrates the problem (i.e., one that we can cut and paste unchanged
into our editors to see the problem). Also see these guidelines for
posting code that doesn't work:

OK that is an easy start.
 
J

JoeC

mlimber said:
JoeC said:
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
[snip]

Problem #1: your index is going past the end of the vector. Use <
rteam.size(), not != rteam.size().

If there are more problems, post a complete but minimal sample that
demonstrates the problem (i.e., one that we can cut and paste unchanged
into our editors to see the problem). Also see these guidelines for
posting code that doesn't work:

OK that is an easy start but didn't solve my problem.
 
M

mlimber

JoeC said:
mlimber said:
JoeC said:
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
[snip]

Problem #1: your index is going past the end of the vector. Use <
rteam.size(), not != rteam.size().

If there are more problems, post a complete but minimal sample that
demonstrates the problem (i.e., one that we can cut and paste unchanged
into our editors to see the problem). Also see these guidelines for
posting code that doesn't work:

OK that is an easy start but didn't solve my problem.

Then follow the instructions second part of my previous response.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top