diff --git a/src/stratagus/missile.cpp b/src/stratagus/missile.cpp index 9535bc638..6b04f2801 100644 --- a/src/stratagus/missile.cpp +++ b/src/stratagus/missile.cpp @@ -106,7 +106,7 @@ void MissileType::LoadMissileSprite() /** ** Load the graphics for all missiles types */ -void LoadMissileSprites(void) +void LoadMissileSprites() { #ifndef DYNAMIC_LOAD for (std::vector<MissileType*>::iterator i = MissileTypes.begin(); i != MissileTypes.end(); ++i) { @@ -135,9 +135,8 @@ MissileType *MissileTypeByIdent(const std::string &ident) */ MissileType *NewMissileTypeSlot(const std::string &ident) { - MissileType *mtype; + MissileType *mtype = new MissileType(ident); - mtype = new MissileType(ident); MissileTypeMap[ident] = mtype; MissileTypes.push_back(mtype); return mtype; @@ -147,7 +146,7 @@ MissileType *NewMissileTypeSlot(const std::string &ident) ** Constructor */ Missile::Missile() : - SourceX(0), SourceY(0), X(0), Y(0), DX(0), DY(0), + SourceX(0), SourceY(0), X(0), Y(0), DX(0), DY(0), Type(NULL), SpriteFrame(0), State(0), AnimWait(0), Wait(0), Delay(0), SourceUnit(NULL), TargetUnit(NULL), Damage(0), TTL(-1), Hidden(0), @@ -242,9 +241,8 @@ Missile *Missile::Init(MissileType *mtype, int sx, int sy, int dx, int dy) */ Missile *MakeMissile(MissileType *mtype, int sx, int sy, int dx, int dy) { - Missile *missile; + Missile *missile = Missile::Init(mtype, sx, sy, dx, dy); - missile = Missile::Init(mtype, sx, sy, dx, dy); GlobalMissiles.push_back(missile); return missile; } @@ -262,9 +260,8 @@ Missile *MakeMissile(MissileType *mtype, int sx, int sy, int dx, int dy) */ Missile *MakeLocalMissile(MissileType *mtype, int sx, int sy, int dx, int dy) { - Missile *missile; + Missile *missile = Missile::Init(mtype, sx, sy, dx, dy); - missile = Missile::Init(mtype, sx, sy, dx, dy); missile->Local = 1; LocalMissiles.push_back(missile); return missile; @@ -312,21 +309,18 @@ static void FreeMissile(std::vector<Missile *> &missiles, std::vector<Missile*>: ** ** @return damage inflicted to goal. */ -static int CalculateDamageStats(const CUnitStats *attacker_stats, - const CUnitStats *goal_stats, int bloodlust) +static int CalculateDamageStats(const CUnitStats &attacker_stats, + const CUnitStats &goal_stats, int bloodlust) { - int damage; - int basic_damage; - int piercing_damage; + int basic_damage = attacker_stats.Variables[BASICDAMAGE_INDEX].Value; + int piercing_damage = attacker_stats.Variables[PIERCINGDAMAGE_INDEX].Value; - basic_damage = attacker_stats->Variables[BASICDAMAGE_INDEX].Value; - piercing_damage = attacker_stats->Variables[PIERCINGDAMAGE_INDEX].Value; if (bloodlust) { basic_damage *= 2; piercing_damage *= 2; } - damage = std::max<int>(basic_damage - goal_stats->Variables[ARMOR_INDEX].Value, 1); + int damage = std::max<int>(basic_damage - goal_stats.Variables[ARMOR_INDEX].Value, 1); damage += piercing_damage; damage -= SyncRand() % ((damage + 2) / 2); Assert(damage >= 0); @@ -342,24 +336,19 @@ static int CalculateDamageStats(const CUnitStats *attacker_stats, ** ** @return damage produces on goal. */ -static int CalculateDamage(const CUnit *attacker, const CUnit *goal) +static int CalculateDamage(const CUnit &attacker, const CUnit &goal) { - int res; - - Assert(attacker); - Assert(goal); - if (!Damage) { // Use old method. - return CalculateDamageStats(attacker->Stats, goal->Stats, - attacker->Variable[BLOODLUST_INDEX].Value); + return CalculateDamageStats(*attacker.Stats, *goal.Stats, + attacker.Variable[BLOODLUST_INDEX].Value); } Assert(Damage); - UpdateUnitVariables((CUnit *)attacker); - UpdateUnitVariables((CUnit *)goal); - TriggerData.Attacker = (CUnit *)attacker; - TriggerData.Defender = (CUnit *)goal; - res = EvalNumber(Damage); + UpdateUnitVariables(const_cast<CUnit *>(&attacker)); + UpdateUnitVariables(const_cast<CUnit *>(&goal)); + TriggerData.Attacker = const_cast<CUnit *>(&attacker); + TriggerData.Defender = const_cast<CUnit *>(&goal); + const int res = EvalNumber(Damage); TriggerData.Attacker = NULL; TriggerData.Defender = NULL; return res; @@ -376,13 +365,11 @@ void FireMissile(CUnit *unit) int y; int dx; int dy; - CUnit *goal; - Missile *missile; + CUnit *goal = unit->CurrentOrder()->GetGoal(); // // Goal dead? // - goal = unit->CurrentOrder()->GetGoal(); if (goal) { // Better let the caller/action handle this. @@ -410,12 +397,12 @@ void FireMissile(CUnit *unit) if (Map.WallOnMap(dx, dy)) { if (Map.HumanWallOnMap(dx, dy)) { Map.HitWall(dx, dy, - CalculateDamageStats(unit->Stats, - UnitTypeHumanWall->Stats, unit->Variable[BLOODLUST_INDEX].Value)); + CalculateDamageStats(*unit->Stats, + *UnitTypeHumanWall->Stats, unit->Variable[BLOODLUST_INDEX].Value)); } else { Map.HitWall(dx, dy, - CalculateDamageStats(unit->Stats, - UnitTypeOrcWall->Stats, unit->Variable[BLOODLUST_INDEX].Value)); + CalculateDamageStats(*unit->Stats, + *UnitTypeOrcWall->Stats, unit->Variable[BLOODLUST_INDEX].Value)); } return; } @@ -423,8 +410,7 @@ void FireMissile(CUnit *unit) DebugPrint("Missile-none hits no unit, shouldn't happen!\n"); return; } - - HitUnit(unit, goal, CalculateDamage(unit, goal)); + HitUnit(unit, goal, CalculateDamage(*unit, *goal)); return; } @@ -465,7 +451,7 @@ void FireMissile(CUnit *unit) // Fire to the tile center of the destination. dx = dx * TileSizeX + TileSizeX / 2; dy = dy * TileSizeY + TileSizeY / 2; - missile = MakeMissile(unit->Type->Missile.Missile, x, y, dx, dy); + Missile *missile = MakeMissile(unit->Type->Missile.Missile, x, y, dx, dy); // // Damage of missile // @@ -517,16 +503,14 @@ static int MissileVisibleInViewport(const CViewport *vp, const Missile *missile) int max_x; int min_y; int max_y; - int x; - int y; GetMissileMapArea(missile, &min_x, &min_y, &max_x, &max_y); if (!vp->AnyMapAreaVisibleInViewport(min_x, min_y, max_x, max_y)) { return 0; } - for (x = min_x; x <= max_x; ++x) { - for (y = min_y; y <= max_y; ++y) { + for (int x = min_x; x <= max_x; ++x) { + for (int y = min_y; y <= max_y; ++y) { if (ReplayRevealMap || Map.IsFieldVisible(ThisPlayer, x, y)) { return 1; } @@ -565,9 +549,8 @@ void MissileType::DrawMissileType(int frame, int x, int y) const } } } else { - int row; + const int row = this->NumDirections / 2 + 1; - row = this->NumDirections / 2 + 1; if (frame < 0) { frame = ((-frame - 1) / row) * this->NumDirections + this->NumDirections - (-frame - 1) % row; } else { @@ -583,8 +566,8 @@ void MissileType::DrawMissileType(int frame, int x, int y) const void MissileDrawProxy::DrawMissile(const CViewport *vp) const { - int x = this->X - vp->MapX * TileSizeX + vp->X - vp->OffsetX; - int y = this->Y - vp->MapY * TileSizeY + vp->Y - vp->OffsetY; + const int x = this->X - vp->MapX * TileSizeX + vp->X - vp->OffsetX; + const int y = this->Y - vp->MapY * TileSizeY + vp->Y - vp->OffsetY; switch (this->Type->Class) { case MissileClassHit: CLabel(GameFont).DrawClip(x,y, this->data.Damage); @@ -612,9 +595,6 @@ void MissileDrawProxy::operator=(const Missile* missile) { */ void Missile::DrawMissile(const CViewport *vp) const { - int x; - int y; - Assert(this->Type); // FIXME: I should copy SourcePlayer for second level missiles. @@ -625,12 +605,11 @@ void Missile::DrawMissile(const CViewport *vp) const } #endif } - - x = this->X - vp->MapX * TileSizeX + vp->X - vp->OffsetX; - y = this->Y - vp->MapY * TileSizeY + vp->Y - vp->OffsetY; + const int x = this->X - vp->MapX * TileSizeX + vp->X - vp->OffsetX; + const int y = this->Y - vp->MapY * TileSizeY + vp->Y - vp->OffsetY; switch (this->Type->Class) { case MissileClassHit: - CLabel(GameFont).DrawClip(x,y, this->Damage); + CLabel(GameFont).DrawClip(x, y, this->Damage); break; default: this->Type->DrawMissileType(this->SpriteFrame, x, y); @@ -660,13 +639,12 @@ static bool MissileDrawLevelCompare(const Missile*const l, int FindAndSortMissiles(const CViewport *vp, Missile *table[], const int tablesize) { - int nmissiles; + int nmissiles = 0; std::vector<Missile *>::const_iterator i; // // Loop through global missiles, then through locals. // - nmissiles = 0; for (i = GlobalMissiles.begin(); i != GlobalMissiles.end() && nmissiles < tablesize; ++i) { Missile *missile = (*i); if (missile->Delay || missile->Hidden) { @@ -695,9 +673,11 @@ int FindAndSortMissiles(const CViewport *vp, int FindAndSortMissiles(const CViewport *vp, MissileDrawProxy table[], const int tablesize) { - Missile *buffer[MAX_MISSILES * 9]; Assert(tablesize <= MAX_MISSILES * 9); - int n = FindAndSortMissiles(vp,buffer, MAX_MISSILES * 9); + + Missile *buffer[MAX_MISSILES * 9]; + int n = FindAndSortMissiles(vp, buffer, MAX_MISSILES * 9); + if (n > 0) { table[0] = buffer[0]; for (int i = 1; i < n; ++i) { @@ -711,38 +691,36 @@ int FindAndSortMissiles(const CViewport *vp, /** ** Change missile heading from x,y. ** -** @param missile Missile pointer. +** @param missile Missile. ** @param dx Delta in x. ** @param dy Delta in y. ** @internal We have : SpriteFrame / (2 * (Numdirection - 1)) == DirectionToHeading / 256. */ -static void MissileNewHeadingFromXY(Missile *missile, int dx, int dy) +static void MissileNewHeadingFromXY(Missile &missile, int dx, int dy) { - int dir; - int nextdir; int neg; - if (missile->Type->NumDirections == 1 || (dx == 0 && dy == 0)) { + if (missile.Type->NumDirections == 1 || (dx == 0 && dy == 0)) { return; } - if (missile->SpriteFrame < 0) { - missile->SpriteFrame = -missile->SpriteFrame - 1; + if (missile.SpriteFrame < 0) { + missile.SpriteFrame = -missile.SpriteFrame - 1; neg = 1; } else { neg = 0; } - missile->SpriteFrame /= missile->Type->NumDirections / 2 + 1; - missile->SpriteFrame *= missile->Type->NumDirections / 2 + 1; + missile.SpriteFrame /= missile.Type->NumDirections / 2 + 1; + missile.SpriteFrame *= missile.Type->NumDirections / 2 + 1; - nextdir = 256 / missile->Type->NumDirections; + const int nextdir = 256 / missile.Type->NumDirections; Assert(nextdir != 0); - dir = ((DirectionToHeading(10 * dx, 10 * dy) + nextdir / 2) & 0xFF) / nextdir; + const int dir = ((DirectionToHeading(10 * dx, 10 * dy) + nextdir / 2) & 0xFF) / nextdir; if (dir <= LookingS / nextdir) { // north->east->south - missile->SpriteFrame += dir; + missile.SpriteFrame += dir; } else { - missile->SpriteFrame += 256 / nextdir - dir; - missile->SpriteFrame = -missile->SpriteFrame - 1; + missile.SpriteFrame += 256 / nextdir - dir; + missile.SpriteFrame = -missile.SpriteFrame - 1; } } @@ -753,30 +731,28 @@ static void MissileNewHeadingFromXY(Missile *missile, int dx, int dy) ** ** @return 1 if goal is reached, 0 else. */ -static int MissileInitMove(Missile *missile) +static int MissileInitMove(Missile &missile) { - int dx; - int dy; + const int dx = missile.DX - missile.X; + const int dy = missile.DY - missile.Y; - dx = missile->DX - missile->X; - dy = missile->DY - missile->Y; MissileNewHeadingFromXY(missile, dx, dy); - if (!(missile->State & 1)) { - missile->CurrentStep = 0; - missile->TotalStep = 0; + if (!(missile.State & 1)) { + missile.CurrentStep = 0; + missile.TotalStep = 0; if (dx == 0 && dy == 0) { return 1; } // initialize - missile->TotalStep = MapDistance(missile->SourceX, missile->SourceY, missile->DX, missile->DY); - missile->State++; + missile.TotalStep = MapDistance(missile.SourceX, missile.SourceY, missile.DX, missile.DY); + missile.State++; return 0; } - Assert(missile->TotalStep != 0); - missile->CurrentStep += missile->Type->Speed; - if (missile->CurrentStep >= missile->TotalStep) { - missile->X = missile->DX; - missile->Y = missile->DY; + Assert(missile.TotalStep != 0); + missile.CurrentStep += missile.Type->Speed; + if (missile.CurrentStep >= missile.TotalStep) { + missile.X = missile.DX; + missile.Y = missile.DY; return 1; } return 0; @@ -789,27 +765,23 @@ static int MissileInitMove(Missile *missile) ** ** @return 1 if goal is reached, 0 else. */ -static int PointToPointMissile(Missile *missile) +static int PointToPointMissile(Missile &missile) { - int xstep; - int ystep; - int x; - int y; - if (MissileInitMove(missile) == 1) { return 1; } - Assert(missile->Type != NULL); - Assert(missile->TotalStep != 0); - xstep = (missile->DX - missile->SourceX) * 1024 / missile->TotalStep; - ystep = (missile->DY - missile->SourceY) * 1024 / missile->TotalStep; - missile->X = missile->SourceX + xstep * missile->CurrentStep / 1024; - missile->Y = missile->SourceY + ystep * missile->CurrentStep / 1024; - if (missile->Type->SmokeMissile && missile->CurrentStep) { - x = missile->X + missile->Type->Width / 2; - y = missile->Y + missile->Type->Height / 2; - MakeMissile(missile->Type->SmokeMissile, x, y, x, y); + Assert(missile.Type != NULL); + Assert(missile.TotalStep != 0); + + const int xstep = (missile.DX - missile.SourceX) * 1024 / missile.TotalStep; + const int ystep = (missile.DY - missile.SourceY) * 1024 / missile.TotalStep; + missile.X = missile.SourceX + xstep * missile.CurrentStep / 1024; + missile.Y = missile.SourceY + ystep * missile.CurrentStep / 1024; + if (missile.Type->SmokeMissile && missile.CurrentStep) { + const int x = missile.X + missile.Type->Width / 2; + const int y = missile.Y + missile.Type->Height / 2; + MakeMissile(missile.Type->SmokeMissile, x, y, x, y); } return 0; } @@ -823,18 +795,14 @@ static int PointToPointMissile(Missile *missile) ** ** @todo Find good values for ZprojToX and Y */ -static int ParabolicMissile(Missile *missile) +static int ParabolicMissile(Missile &missile) { int orig_x; // position before moving. int orig_y; // position before moving. - int xstep; - int ystep; int k; // Coefficient of the parabol. int zprojToX; // Projection of Z axis on axis X. int zprojToY; // Projection of Z axis on axis Y. - int z; // should be missile->Z later. - int x; - int y; + int z; // should be missile.Z later. k = -2048; //-1024; // Should be initialised by an other method (computed with distance...) zprojToX = 4; @@ -842,26 +810,26 @@ static int ParabolicMissile(Missile *missile) if (MissileInitMove(missile) == 1) { return 1; } - Assert(missile->Type != NULL); - orig_x = missile->X; - orig_y = missile->Y; - xstep = missile->DX - missile->SourceX; - ystep = missile->DY - missile->SourceY; - Assert(missile->TotalStep != 0); - xstep = xstep * 1000 / missile->TotalStep; - ystep = ystep * 1000 / missile->TotalStep; - missile->X = missile->SourceX + xstep * missile->CurrentStep / 1000; - missile->Y = missile->SourceY + ystep * missile->CurrentStep / 1000; + Assert(missile.Type != NULL); + orig_x = missile.X; + orig_y = missile.Y; + int xstep = missile.DX - missile.SourceX; + int ystep = missile.DY - missile.SourceY; + Assert(missile.TotalStep != 0); + xstep = xstep * 1000 / missile.TotalStep; + ystep = ystep * 1000 / missile.TotalStep; + missile.X = missile.SourceX + xstep * missile.CurrentStep / 1000; + missile.Y = missile.SourceY + ystep * missile.CurrentStep / 1000; Assert(k != 0); - z = missile->CurrentStep * (missile->TotalStep - missile->CurrentStep) / k; + z = missile.CurrentStep * (missile.TotalStep - missile.CurrentStep) / k; // Until Z is used for drawing, modify X and Y. - missile->X += z * zprojToX / 64; - missile->Y += z * zprojToY / 64; - MissileNewHeadingFromXY(missile, missile->X - orig_x, missile->Y - orig_y); - if (missile->Type->SmokeMissile && missile->CurrentStep) { - x = missile->X + missile->Type->Width / 2; - y = missile->Y + missile->Type->Height / 2; - MakeMissile(missile->Type->SmokeMissile, x, y, x, y); + missile.X += z * zprojToX / 64; + missile.Y += z * zprojToY / 64; + MissileNewHeadingFromXY(missile, missile.X - orig_x, missile.Y - orig_y); + if (missile.Type->SmokeMissile && missile.CurrentStep) { + const int x = missile.X + missile.Type->Width / 2; + const int y = missile.Y + missile.Type->Height / 2; + MakeMissile(missile.Type->SmokeMissile, x, y, x, y); } return 0; } @@ -873,19 +841,19 @@ static int ParabolicMissile(Missile *missile) ** @param goal Goal of the missile. ** @param splash Splash damage divisor. */ -static void MissileHitsGoal(const Missile *missile, CUnit *goal, int splash) +static void MissileHitsGoal(const Missile &missile, CUnit *goal, int splash) { - if (!missile->Type->CanHitOwner && goal == missile->SourceUnit) { + if (!missile.Type->CanHitOwner && goal == missile.SourceUnit) { return; } if (goal->CurrentAction() != UnitActionDie) { - if (missile->Damage) { // direct damage, spells mostly - HitUnit(missile->SourceUnit, goal, missile->Damage / splash); + if (missile.Damage) { // direct damage, spells mostly + HitUnit(missile.SourceUnit, goal, missile.Damage / splash); } else { - Assert(missile->SourceUnit != NULL); - HitUnit(missile->SourceUnit, goal, - CalculateDamage(missile->SourceUnit, goal) / splash); + Assert(missile.SourceUnit != NULL); + HitUnit(missile.SourceUnit, goal, + CalculateDamage(*missile.SourceUnit, *goal) / splash); } } } @@ -900,26 +868,26 @@ static void MissileHitsGoal(const Missile *missile, CUnit *goal, int splash) ** ** @todo FIXME: Support for more races. */ -static void MissileHitsWall(const Missile *missile, int x, int y, int splash) +static void MissileHitsWall(const Missile &missile, int x, int y, int splash) { CUnitStats *stats; // stat of the wall. if (!Map.WallOnMap(x, y)) { return; } - if (missile->Damage) { // direct damage, spells mostly - Map.HitWall(x, y, missile->Damage / splash); + if (missile.Damage) { // direct damage, spells mostly + Map.HitWall(x, y, missile.Damage / splash); return; } - Assert(missile->SourceUnit != NULL); + Assert(missile.SourceUnit != NULL); if (Map.HumanWallOnMap(x, y)) { stats = UnitTypeHumanWall->Stats; } else { Assert(Map.OrcWallOnMap(x, y)); stats = UnitTypeOrcWall->Stats; } - Map.HitWall(x, y, CalculateDamageStats(missile->SourceUnit->Stats, stats, 0) / splash); + Map.HitWall(x, y, CalculateDamageStats(*missile.SourceUnit->Stats, *stats, 0) / splash); } @@ -930,19 +898,12 @@ static void MissileHitsWall(const Missile *missile, int x, int y, int splash) */ void MissileHit(Missile *missile) { - CUnit *goal; - int x; - int y; - int n; - int i; - int splash; - if (missile->Type->ImpactSound.Sound) { PlayMissileSound(missile, missile->Type->ImpactSound.Sound); } - x = missile->X + missile->Type->Width / 2; - y = missile->Y + missile->Type->Height / 2; + int x = missile->X + missile->Type->Width / 2; + int y = missile->Y + missile->Type->Height / 2; // // The impact generates a new missile. @@ -978,42 +939,41 @@ void MissileHit(Missile *missile) // // Missiles without range only hits the goal always. // - goal = missile->TargetUnit; - if (goal->Destroyed) { // Destroyed - goal->RefsDecrease(); + CUnit &goal = *missile->TargetUnit; + if (goal.Destroyed) { // Destroyed + goal.RefsDecrease(); missile->TargetUnit = NoUnitP; return; } - MissileHitsGoal(missile, goal, 1); + MissileHitsGoal(*missile, &goal, 1); return; } - MissileHitsWall(missile, x, y, 1); + MissileHitsWall(*missile, x, y, 1); return; } { - CUnit *table[UnitMax]; - // // Hits all units in range. // - i = missile->Type->Range; - n = Map.Select(x - i + 1, y - i + 1, x + i, y + i, table); + const int range = missile->Type->Range; + CUnit *table[UnitMax]; + const int n = Map.Select(x - range + 1, y - range + 1, x + range, y + range, table); Assert(missile->SourceUnit != NULL); - for (i = 0; i < n; ++i) { - goal = (CUnit*)table[i]; + for (int i = 0; i < n; ++i) { + CUnit &goal = *table[i]; // // Can the unit attack the this unit-type? // NOTE: perhaps this should be come a property of the missile. // - if (CanTarget(missile->SourceUnit->Type, goal->Type)) { - splash = goal->MapDistanceTo(x, y); + if (CanTarget(missile->SourceUnit->Type, goal.Type)) { + int splash = goal.MapDistanceTo(x, y); if (splash) { splash *= missile->Type->SplashFactor; } else { splash = 1; } - MissileHitsGoal(missile, goal, splash); + MissileHitsGoal(*missile, &goal, splash); } } } @@ -1023,15 +983,15 @@ void MissileHit(Missile *missile) // x -= missile->Type->Range; y -= missile->Type->Range; - for (i = missile->Type->Range * 2; --i;) { - for (n = missile->Type->Range * 2; --n;) { - if (x + i >= 0 && x + i < Map.Info.MapWidth && y + n >= 0 && y + n < Map.Info.MapHeight) { - int d = MapDistance(x + missile->Type->Range, y + missile->Type->Range, x + i, y + n); + for (int i = missile->Type->Range * 2; --i;) { + for (int j = missile->Type->Range * 2; --j;) { + if (x + i >= 0 && x + i < Map.Info.MapWidth && y + j >= 0 && y + j < Map.Info.MapHeight) { + int d = MapDistance(x + missile->Type->Range, y + missile->Type->Range, x + i, y + j); d *= missile->Type->SplashFactor; if (d == 0) { d = 1; } - MissileHitsWall(missile, x + i, y + n, d); + MissileHitsWall(*missile, x + i, y + j, d); } } } @@ -1046,7 +1006,7 @@ void MissileHit(Missile *missile) ** ** @return 1 if animation is finished, 0 else. */ -static int NextMissileFrame(Missile *missile, char sign, char longAnimation) +static int NextMissileFrame(Missile &missile, char sign, char longAnimation) { int neg; // True for mirroring sprite. int animationIsFinished; // returned value. @@ -1057,10 +1017,10 @@ static int NextMissileFrame(Missile *missile, char sign, char longAnimation) // neg = 0; animationIsFinished = 0; - numDirections = missile->Type->NumDirections / 2 + 1; - if (missile->SpriteFrame < 0) { + numDirections = missile.Type->NumDirections / 2 + 1; + if (missile.SpriteFrame < 0) { neg = 1; - missile->SpriteFrame = -missile->SpriteFrame - 1; + missile.SpriteFrame = -missile.SpriteFrame - 1; } if (longAnimation) { int totalf; // Total number of frame (for one direction). @@ -1068,29 +1028,29 @@ static int NextMissileFrame(Missile *missile, char sign, char longAnimation) int totalx; // Total distance to cover. int dx; // Covered distance. - totalx = MapDistance(missile->DX, missile->DY, missile->SourceX, missile->SourceY); - dx = MapDistance(missile->X, missile->Y, missile->SourceX, missile->SourceY); - totalf = missile->Type->SpriteFrames / numDirections; - df = missile->SpriteFrame / numDirections; + totalx = MapDistance(missile.DX, missile.DY, missile.SourceX, missile.SourceY); + dx = MapDistance(missile.X, missile.Y, missile.SourceX, missile.SourceY); + totalf = missile.Type->SpriteFrames / numDirections; + df = missile.SpriteFrame / numDirections; if ((sign == 1 && dx * totalf <= df * totalx) || (sign == -1 && dx * totalf > df * totalx)) { return animationIsFinished; } } - missile->SpriteFrame += sign * numDirections; + missile.SpriteFrame += sign * numDirections; if (sign > 0) { - if (missile->SpriteFrame >= missile->Type->SpriteFrames) { - missile->SpriteFrame -= missile->Type->SpriteFrames; + if (missile.SpriteFrame >= missile.Type->SpriteFrames) { + missile.SpriteFrame -= missile.Type->SpriteFrames; animationIsFinished = 1; } } else { - if (missile->SpriteFrame < 0) { - missile->SpriteFrame += missile->Type->SpriteFrames; + if (missile.SpriteFrame < 0) { + missile.SpriteFrame += missile.Type->SpriteFrames; animationIsFinished = 1; } } if (neg) { - missile->SpriteFrame = -missile->SpriteFrame - 1; + missile.SpriteFrame = -missile.SpriteFrame - 1; } return animationIsFinished; @@ -1102,38 +1062,32 @@ static int NextMissileFrame(Missile *missile, char sign, char longAnimation) ** ** @param missile Missile pointer. */ -static void NextMissileFrameCycle(Missile *missile) +static void NextMissileFrameCycle(Missile &missile) { - int neg; - int totalx; - int dx; - int f; - int i; - int j; + int neg = 0; - neg = 0; - if (missile->SpriteFrame < 0) { + if (missile.SpriteFrame < 0) { neg = 1; - missile->SpriteFrame = -missile->SpriteFrame - 1; + missile.SpriteFrame = -missile.SpriteFrame - 1; } - totalx = abs(missile->DX - missile->SourceX); - dx = abs(missile->X - missile->SourceX); - f = missile->Type->SpriteFrames / (missile->Type->NumDirections / 2 + 1); + const int totalx = abs(missile.DX - missile.SourceX); + const int dx = abs(missile.X - missile.SourceX); + int f = missile.Type->SpriteFrames / (missile.Type->NumDirections / 2 + 1); f = 2 * f - 1; - for (i = 1, j = 1; i <= f; ++i) { + for (int i = 1, j = 1; i <= f; ++i) { if (dx * f / i < totalx) { if ((i - 1) * 2 < f) { j = i - 1; } else { j = f - i; } - missile->SpriteFrame = missile->SpriteFrame % (missile->Type->NumDirections / 2 + 1) + - j * (missile->Type->NumDirections / 2 + 1); + missile.SpriteFrame = missile.SpriteFrame % (missile.Type->NumDirections / 2 + 1) + + j * (missile.Type->NumDirections / 2 + 1); break; } } if (neg) { - missile->SpriteFrame = -missile->SpriteFrame - 1; + missile.SpriteFrame = -missile.SpriteFrame - 1; } } @@ -1149,32 +1103,32 @@ static void MissilesActionLoop(std::vector<Missile *> &missiles) // for (std::vector<Missile *>::size_type i = 0; i != missiles.size();) { - Missile *missile = missiles[i]; + Missile &missile = *missiles[i]; - if (missile->Delay) { - missile->Delay--; + if (missile.Delay) { + missile.Delay--; ++i; continue; // delay start of missile } - if (missile->TTL > 0) { - missile->TTL--; // overall time to live if specified + if (missile.TTL > 0) { + missile.TTL--; // overall time to live if specified } - if (!missile->TTL) { + if (!missile.TTL) { FreeMissile(missiles, i); continue; } - Assert(missile->Wait); - if (--missile->Wait) { // wait until time is over + Assert(missile.Wait); + if (--missile.Wait) { // wait until time is over ++i; continue; } - missile->Action(); + missile.Action(); - if (!missile->TTL) { + if (!missile.TTL) { FreeMissile(missiles, i); continue; } @@ -1185,7 +1139,7 @@ static void MissilesActionLoop(std::vector<Missile *> &missiles) /** ** Handle all missile actions. */ -void MissileActions(void) +void MissileActions() { MissilesActionLoop(GlobalMissiles); MissilesActionLoop(LocalMissiles); @@ -1200,8 +1154,8 @@ void MissileActions(void) */ int ViewPointDistanceToMissile(const Missile *missile) { - int x = (missile->X + missile->Type->Width / 2) / TileSizeX; - int y = (missile->Y + missile->Type->Height / 2) / TileSizeY; // pixel -> tile + const int x = (missile->X + missile->Type->Width / 2) / TileSizeX; + const int y = (missile->Y + missile->Type->Height / 2) / TileSizeY; // pixel -> tile return ViewPointDistance(x, y); } @@ -1415,11 +1369,11 @@ void MissileNone::Action() void MissilePointToPoint::Action() { this->Wait = this->Type->Sleep; - if (PointToPointMissile(this)) { + if (PointToPointMissile(*this)) { MissileHit(this); this->TTL = 0; } else { - NextMissileFrame(this, 1, 0); + NextMissileFrame(*this, 1, 0); } } @@ -1430,8 +1384,8 @@ void MissilePointToPoint::Action() void MissilePointToPointWithHit::Action() { this->Wait = this->Type->Sleep; - if (PointToPointMissile(this)) { - if (NextMissileFrame(this, 1, 0)) { + if (PointToPointMissile(*this)) { + if (NextMissileFrame(*this, 1, 0)) { MissileHit(this); this->TTL = 0; } @@ -1444,11 +1398,11 @@ void MissilePointToPointWithHit::Action() void MissilePointToPointCycleOnce::Action() { this->Wait = this->Type->Sleep; - if (PointToPointMissile(this)) { + if (PointToPointMissile(*this)) { MissileHit(this); this->TTL = 0; } else { - NextMissileFrameCycle(this); + NextMissileFrameCycle(*this); } } @@ -1458,7 +1412,7 @@ void MissilePointToPointCycleOnce::Action() void MissileStay::Action() { this->Wait = this->Type->Sleep; - if (NextMissileFrame(this, 1, 0)) { + if (NextMissileFrame(*this, 1, 0)) { MissileHit(this); this->TTL = 0; } @@ -1470,20 +1424,17 @@ void MissileStay::Action() void MissilePointToPointBounce::Action() { this->Wait = this->Type->Sleep; - if (PointToPointMissile(this)) { + if (PointToPointMissile(*this)) { if (this->State < 2 * this->Type->NumBounces - 1 && this->TotalStep) { - int xstep; - int ystep; + const int xstep = (this->DX - this->SourceX) * 1024 / this->TotalStep; + const int ystep = (this->DY - this->SourceY) * 1024 / this->TotalStep; - xstep = (this->DX - this->SourceX) * 1024 / this->TotalStep; - ystep = (this->DY - this->SourceY) * 1024 / this->TotalStep; this->DX += xstep * (TileSizeX + TileSizeY) * 3 / 4 / 1024; this->DY += ystep * (TileSizeX + TileSizeY) * 3 / 4 / 1024; - this->State++; // !(State & 1) to initialise this->SourceX = this->X; this->SourceY = this->Y; - PointToPointMissile(this); + PointToPointMissile(*this); //this->State++; MissileHit(this); // FIXME: hits to left and right @@ -1493,7 +1444,7 @@ void MissilePointToPointBounce::Action() this->TTL = 0; } } else { - NextMissileFrame(this, 1, 0); + NextMissileFrame(*this, 1, 0); } } @@ -1510,12 +1461,12 @@ void MissileCycleOnce::Action() ++this->State; break; case 1: - if (NextMissileFrame(this, 1, 0)) { + if (NextMissileFrame(*this, 1, 0)) { ++this->State; } break; case 3: - if (NextMissileFrame(this, -1, 0)) { + if (NextMissileFrame(*this, -1, 0)) { MissileHit(this); this->TTL = 0; } @@ -1528,24 +1479,21 @@ void MissileCycleOnce::Action() */ void MissileFire::Action() { - CUnit *unit; + CUnit &unit = *this->SourceUnit; - unit = this->SourceUnit; this->Wait = this->Type->Sleep; - if (unit->Destroyed || unit->CurrentAction() == UnitActionDie) { + if (unit.Destroyed || unit.CurrentAction() == UnitActionDie) { this->TTL = 0; return; } - if (NextMissileFrame(this, 1, 0)) { - int f; - MissileType *fire; - + if (NextMissileFrame(*this, 1, 0)) { this->SpriteFrame = 0; - f = (100 * unit->Variable[HP_INDEX].Value) / unit->Variable[HP_INDEX].Max; - fire = MissileBurningBuilding(f); + const int f = (100 * unit.Variable[HP_INDEX].Value) / unit.Variable[HP_INDEX].Max; + MissileType *fire = MissileBurningBuilding(f); + if (!fire) { this->TTL = 0; - unit->Burning = 0; + unit.Burning = 0; } else { if (this->Type != fire) { this->X += this->Type->Width / 2; @@ -1564,7 +1512,7 @@ void MissileFire::Action() void MissileHit::Action() { this->Wait = this->Type->Sleep; - if (PointToPointMissile(this)) { + if (PointToPointMissile(*this)) { ::MissileHit(this); this->TTL = 0; } @@ -1576,11 +1524,11 @@ void MissileHit::Action() void MissileParabolic::Action() { this->Wait = this->Type->Sleep; - if (ParabolicMissile(this)) { + if (ParabolicMissile(*this)) { MissileHit(this); this->TTL = 0; } else { - NextMissileFrameCycle(this); + NextMissileFrameCycle(*this); } } @@ -1595,23 +1543,12 @@ void MissileFlameShield::Action() -30, 5, -31, 0, -32, -5, -31, -10, -30, -16, -27, -20, -24, -24, -20, -27, -15, -30, -10, -31, -5, -32, 0, -31, 5, -30, 10, -27, 16, -24, 20, -20, 24, -15, 27, -10, 30, -5, 31, 0, 32}; - CUnit *unit; - int n; - int i; - int dx; - int dy; - int ux; - int uy; - int ix; - int iy; - int uw; - int uh; this->Wait = this->Type->Sleep; - i = this->TTL % 36; // 36 positions on the circle - dx = fs_dc[i * 2]; - dy = fs_dc[i * 2 + 1]; - unit = this->TargetUnit; + const int index = this->TTL % 36; // 36 positions on the circle + const int dx = fs_dc[index * 2]; + const int dy = fs_dc[index * 2 + 1]; + CUnit *unit = this->TargetUnit; // // Show around the top most unit. // FIXME: conf, do we hide if the unit is contained or not? @@ -1619,16 +1556,16 @@ void MissileFlameShield::Action() while (unit->Container) { unit = unit->Container; } - ux = unit->X; - uy = unit->Y; - ix = unit->IX; - iy = unit->IY; - uw = unit->Type->TileWidth; - uh = unit->Type->TileHeight; + const int ux = unit->X; + const int uy = unit->Y; + const int ix = unit->IX; + const int iy = unit->IY; + const int uw = unit->Type->TileWidth; + const int uh = unit->Type->TileHeight; this->X = ux * TileSizeX + ix + uw * TileSizeX / 2 + dx - 16; this->Y = uy * TileSizeY + iy + uh * TileSizeY / 2 + dy - 32; if (unit->CurrentAction() == UnitActionDie) { - this->TTL = i; + this->TTL = index; } if (unit->Container) { @@ -1644,8 +1581,8 @@ void MissileFlameShield::Action() } CUnit* table[UnitMax]; - n = Map.Select(ux - 1, uy - 1, ux + 1 + 1, uy + 1 + 1, table); - for (i = 0; i < n; ++i) { + const int n = Map.Select(ux - 1, uy - 1, ux + 1 + 1, uy + 1 + 1, table); + for (int i = 0; i < n; ++i) { if (table[i] == unit) { // cannot hit target unit continue; @@ -1682,18 +1619,18 @@ struct LandMineTargetFinder { */ void MissileLandMine::Action() { - int x = this->X / TileSizeX; - int y = this->Y / TileSizeY; + const int x = this->X / TileSizeX; + const int y = this->Y / TileSizeY; if(LandMineTargetFinder(this->SourceUnit, - this->Type->CanHitOwner).FindOnTile(Map.Field(x,y)) != NULL) { + this->Type->CanHitOwner).FindOnTile(Map.Field(x, y)) != NULL) { DebugPrint("Landmine explosion at %d,%d.\n" _C_ x _C_ y); MissileHit(this); this->TTL = 0; return; } if (!this->AnimWait--) { - NextMissileFrame(this, 1, 0); + NextMissileFrame(*this, 1, 0); this->AnimWait = this->Type->Sleep; } this->Wait = 1; @@ -1706,16 +1643,13 @@ void MissileLandMine::Action() */ void MissileWhirlwind::Action() { - int x; - int y; - // // Animate, move. // if (!this->AnimWait--) { - if (NextMissileFrame(this, 1, 0)) { + if (NextMissileFrame(*this, 1, 0)) { this->SpriteFrame = 0; - PointToPointMissile(this); + PointToPointMissile(*this); } this->AnimWait = this->Type->Sleep; } @@ -1723,8 +1657,8 @@ void MissileWhirlwind::Action() // // Center of the tornado // - x = (this->X + TileSizeX / 2 + this->Type->Width / 2) / TileSizeX; - y = (this->Y + TileSizeY + this->Type->Height / 2) / TileSizeY; + const int x = (this->X + TileSizeX / 2 + this->Type->Width / 2) / TileSizeX; + const int y = (this->Y + TileSizeY + this->Type->Height / 2) / TileSizeY; #if 0 CUnit *table[UnitMax]; @@ -1793,15 +1727,12 @@ void MissileWhirlwind::Action() */ void MissileDeathCoil::Action() { - int i; - int n; - CUnit *source; - this->Wait = this->Type->Sleep; - if (PointToPointMissile(this)) { - source = this->SourceUnit; - Assert(source != NULL); - if (source->Destroyed) { + if (PointToPointMissile(*this)) { + Assert(this->SourceUnit != NULL); + CUnit &source = *this->SourceUnit; + + if (source.Destroyed) { return; } // source unit still exists @@ -1810,48 +1741,44 @@ void MissileDeathCoil::Action() // if (this->TargetUnit && !this->TargetUnit->Destroyed && this->TargetUnit->CurrentAction() == UnitActionDie) { - HitUnit(source, this->TargetUnit, this->Damage); - if (source->CurrentAction() != UnitActionDie) { - source->Variable[HP_INDEX].Value += this->Damage; - if (source->Variable[HP_INDEX].Value > source->Variable[HP_INDEX].Max) { - source->Variable[HP_INDEX].Value = source->Variable[HP_INDEX].Max; + HitUnit(&source, this->TargetUnit, this->Damage); + if (source.CurrentAction() != UnitActionDie) { + source.Variable[HP_INDEX].Value += this->Damage; + if (source.Variable[HP_INDEX].Value > source.Variable[HP_INDEX].Max) { + source.Variable[HP_INDEX].Value = source.Variable[HP_INDEX].Max; } } } else { // // No target unit -- try enemies in range 5x5 // Must be parametrable // - int ec; // enemy count - int x; - int y; + int ec = 0; // enemy count CUnit* table[UnitMax]; + const int x = this->DX / TileSizeX; + const int y = this->DY / TileSizeY; + const int n = Map.Select(x - 2, y - 2, x + 2 + 1, y + 2 + 1, table); - ec = 0; - x = this->DX / TileSizeX; - y = this->DY / TileSizeY; - - n = Map.Select(x - 2, y - 2, x + 2 + 1, y + 2 + 1, table); if (n == 0) { return; } // calculate organic enemy count - for (i = 0; i < n; ++i) { - ec += (source->IsEnemy(table[i]) + for (int i = 0; i < n; ++i) { + ec += (source.IsEnemy(table[i]) /*&& table[i]->Type->Organic != 0*/); } if (ec > 0) { // yes organic enemies found - for (i = 0; i < n; ++i) { - if (source->IsEnemy(table[i])/* && table[i]->Type->Organic != 0*/) { + for (int i = 0; i < n; ++i) { + if (source.IsEnemy(table[i])/* && table[i]->Type->Organic != 0*/) { // disperse damage between them // NOTE: 1 is the minimal damage - HitUnit(source, table[i], this->Damage / ec); + HitUnit(&source, table[i], this->Damage / ec); } } - if (source->CurrentAction() != UnitActionDie) { - source->Variable[HP_INDEX].Value += this->Damage; - if (source->Variable[HP_INDEX].Value > source->Variable[HP_INDEX].Max) { - source->Variable[HP_INDEX].Value = source->Variable[HP_INDEX].Max; + if (source.CurrentAction() != UnitActionDie) { + source.Variable[HP_INDEX].Value += this->Damage; + if (source.Variable[HP_INDEX].Value > source.Variable[HP_INDEX].Max) { + source.Variable[HP_INDEX].Value = source.Variable[HP_INDEX].Max; } } }