diff --git a/src/graphics/Renderer.cpp b/src/graphics/Renderer.cpp index f282929ee1..b66eef9612 100644 --- a/src/graphics/Renderer.cpp +++ b/src/graphics/Renderer.cpp @@ -1377,7 +1377,7 @@ void Renderer::render_parts() cplayer = &sim->player; else if(t==PT_STKM2) cplayer = &sim->player2; - else if(t==PT_FIGH) + else if (t==PT_FIGH && sim->parts[i].tmp >= 0 && sim->parts[i].tmp < MAX_FIGHTERS) cplayer = &sim->fighters[(unsigned char)sim->parts[i].tmp]; else continue; diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 3d4b922974..923aef3e88 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -148,8 +148,7 @@ int Simulation::Load(int fullX, int fullY, GameSave * save) } else if (parts[i].type == PT_FIGH) { - //TODO: 100 should be replaced with a macro - for(int fcount = 0; fcount < 100; fcount++) + for (int fcount = 0; fcount < MAX_FIGHTERS; fcount++) { if(!fighters[fcount].spwn) { @@ -318,7 +317,7 @@ Snapshot * Simulation::CreateSnapshot() snap->FanVelocityY.insert(snap->FanVelocityY.begin(), &fvy[0][0], &fvy[0][0]+((XRES/CELL)*(YRES/CELL))); snap->stickmen.push_back(player2); snap->stickmen.push_back(player); - snap->stickmen.insert(snap->stickmen.begin(), &fighters[0], &fighters[255]); + snap->stickmen.insert(snap->stickmen.begin(), &fighters[0], &fighters[MAX_FIGHTERS]); snap->signs = signs; return snap; } @@ -2686,6 +2685,16 @@ void Simulation::part_change_type(int i, int x, int y, int t)//changes the type kill_part(i); return; } + else if ((t == PT_STKM || t == PT_STKM2 || t == PT_SPAWN || t == PT_SPAWN2) && elementCount[t]) + { + kill_part(i); + return; + } + else if ((t == PT_STKM && player.spwn) || (t == PT_STKM2 && player2.spwn)) + { + kill_part(i); + return; + } if (parts[i].type == PT_STKM) player.spwn = 0; @@ -2709,17 +2718,6 @@ void Simulation::part_change_type(int i, int x, int y, int t)//changes the type else if (parts[i].type == PT_SOAP) Element_SOAP::detach(this, i); - if ((t == PT_STKM || t == PT_STKM2 || t == PT_SPAWN || t == PT_SPAWN2) && elementCount[t]) - { - kill_part(i); - return; - } - else if ((t == PT_STKM && player.spwn) || (t == PT_STKM2 && player2.spwn)) - { - kill_part(i); - return; - } - if (parts[i].type > 0 && parts[i].type < PT_NUM && elementCount[parts[i].type]) elementCount[parts[i].type]--; elementCount[t]++; @@ -2734,8 +2732,8 @@ void Simulation::part_change_type(int i, int x, int y, int t)//changes the type Element_STKM::STKM_init_legs(this, &player2, i); else if (t == PT_FIGH) { - if (parts[i].tmp2 >= 0 && parts[i].tmp2 < 100) - Element_STKM::STKM_init_legs(this, &fighters[parts[i].tmp2], i); + if (parts[i].tmp >= 0 && parts[i].tmp < MAX_FIGHTERS) + Element_STKM::STKM_init_legs(this, &fighters[parts[i].tmp], i); } parts[i].type = t; @@ -3097,8 +3095,8 @@ int Simulation::create_part(int p, int x, int y, int tv) case PT_FIGH: { unsigned char fcount = 0; - while (fcount < 100 && fcount < (fighcount+1) && fighters[fcount].spwn==1) fcount++; - if (fcount < 100 && fighters[fcount].spwn==0) + while (fcount < MAX_FIGHTERS && fcount < (fighcount+1) && fighters[fcount].spwn==1) fcount++; + if (fcount < MAX_FIGHTERS && fighters[fcount].spwn==0) { parts[i].life = 100; parts[i].tmp = fcount; @@ -4407,7 +4405,7 @@ void Simulation::update_particles_i(int start, int inc) stickman = &player; else if (t == PT_STKM2) stickman = &player2; - else if (t == PT_FIGH && parts[i].tmp >= 0 && parts[i].tmp < 256) + else if (t == PT_FIGH && parts[i].tmp >= 0 && parts[i].tmp < MAX_FIGHTERS) stickman = &fighters[parts[i].tmp]; if (stickman) diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index 93d92a63b7..76919fa3d4 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -64,7 +64,7 @@ class Simulation //Stickman playerst player; playerst player2; - playerst fighters[256]; //255 is the maximum number of fighters + playerst fighters[MAX_FIGHTERS]; //Defined in Stickman.h unsigned char fighcount; //Contains the number of fighters bool gravWallChanged; //Portals and Wifi diff --git a/src/simulation/Stickman.h b/src/simulation/Stickman.h index 7ea88629b2..fda3cafa37 100644 --- a/src/simulation/Stickman.h +++ b/src/simulation/Stickman.h @@ -1,6 +1,7 @@ #ifndef STICKMAN_H_ #define STICKMAN_H_ +#define MAX_FIGHTERS 100 struct playerst { char comm; //command cell diff --git a/src/simulation/elements/FIGH.cpp b/src/simulation/elements/FIGH.cpp index f476671c1e..7fa98df68e 100644 --- a/src/simulation/elements/FIGH.cpp +++ b/src/simulation/elements/FIGH.cpp @@ -49,6 +49,11 @@ Element_FIGH::Element_FIGH() //#TPT-Directive ElementHeader Element_FIGH static int update(UPDATE_FUNC_ARGS) int Element_FIGH::update(UPDATE_FUNC_ARGS) { + if (parts[i].tmp < 0 || parts[i].tmp >= MAX_FIGHTERS) + { + sim->kill_part(i); + return 1; + } playerst* figh = &sim->fighters[(unsigned char)parts[i].tmp]; unsigned int tarx, tary;