Skip to content

Commit

Permalink
Fixed container attack range game load issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrettin committed Sep 13, 2015
1 parent 9038ff4 commit 1a50aac
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/action/action_upgradeto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ static int TransformUnitIntoType(CUnit &unit, const CUnitType &newtype)
RestoreSelection();
} else {
MapMarkUnitSight(unit);
//Wyrmgus start
//if unit has a container, update the container's attack range, as the unit's range may have been changed with the upgrade
container->UpdateContainerAttackRange();
//Wyrmgus end
}
//
// Update possible changed buttons.
Expand Down
5 changes: 5 additions & 0 deletions src/animation/animation_setvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@
goal->Variable[index].Value = goal->Variable[index].Max * value / 100;
}
clamp(&goal->Variable[index].Value, 0, goal->Variable[index].Max);
//Wyrmgus start
if (index == ATTACKRANGE_INDEX && goal->Container) {
goal->Container->UpdateContainerAttackRange();
}
//Wyrmgus end
}

/*
Expand Down
7 changes: 7 additions & 0 deletions src/game/loadgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ static void PlaceUnits()
unit.Removed = 1;
unit.Place(unit.tilePos);
}

//Wyrmgus start
//calculate attack range for containers now, as when loading a game it couldn't be done when the container was initially loaded
if (unit.BoardCount > 0 && unit.InsideCount > 0) {
unit.UpdateContainerAttackRange();
}
//Wyrmgus end
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/game/savegame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ int SaveGame(const std::string &filename)
//
file.printf("SavedGameInfo({\n");
file.printf("--- \"comment\", \"Generated by Stratagus Version " VERSION "\",\n");
file.printf("--- \"comment\", \"Visit https://launchpad.net/stratagus for more informations\",\n");
//Wyrmgus start
// file.printf("--- \"comment\", \"Visit https://launchpad.net/stratagus for more informations\",\n");
file.printf("--- \"comment\", \"Visit https://launchpad.net/stratagus for more information\",\n");
//Wyrmgus end
file.printf("--- \"type\", \"%s\",\n", "single-player");
file.printf("--- \"date\", \"%s\",\n", dateStr);
file.printf("--- \"map\", \"%s\",\n", Map.Info.Description.c_str());
Expand Down
3 changes: 3 additions & 0 deletions src/include/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ class CUnit
void MoveToXY(const Vec2i &pos);
/// Add a unit inside a container. Only deal with list stuff.
void AddInContainer(CUnit &host);
//Wyrmgus start
void UpdateContainerAttackRange();
//Wyrmgus end
/// Change owner of unit
void ChangeOwner(CPlayer &newplayer);

Expand Down
6 changes: 6 additions & 0 deletions src/spell/spell_adjustvariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@
unit->Variable[i].Value += this->Var[i].IncreaseTime * unit->Variable[i].Increase;

clamp(&unit->Variable[i].Value, 0, unit->Variable[i].Max);

//Wyrmgus start
if (i == ATTACKRANGE_INDEX && unit->Container) {
unit->Container->UpdateContainerAttackRange();
}
//Wyrmgus end
}
return 1;
}
Expand Down
6 changes: 6 additions & 0 deletions src/stratagus/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,11 @@ std::string TransliterateText(std::string text) //convert special characters int
text = FindAndReplaceString(text, "ø", "ö"); //Source: Henry Adams Bellows (transl.), "The Poetic Edda", p. xxviii.
text = FindAndReplaceString(text, "Ǫ", "O"); //Source: Henry Adams Bellows (transl.), "The Poetic Edda", p. xxviii.
text = FindAndReplaceString(text, "ǫ", "o"); //Source: Henry Adams Bellows (transl.), "The Poetic Edda", p. xxviii.
text = FindAndReplaceString(text, "Ș", "S");
text = FindAndReplaceString(text, "ș", "s");
text = FindAndReplaceString(text, "ß", "ss"); //Source: Henry Adams Bellows (transl.), "The Poetic Edda", p. xxviii.
text = FindAndReplaceString(text, "Ț", "T");
text = FindAndReplaceString(text, "ț", "t");
text = FindAndReplaceString(text, "Þ", "Th"); //Source: Henry Adams Bellows (transl.), "The Poetic Edda", p. xxviii.
text = FindAndReplaceString(text, "þ", "th"); //Source: Henry Adams Bellows (transl.), "The Poetic Edda", p. xxviii.
text = FindAndReplaceString(text, "Ū", "U");
Expand Down Expand Up @@ -656,6 +660,8 @@ std::string CapitalizeString(std::string text)
text = FindAndReplaceStringBeginning(text, "ô", "Ô");
text = FindAndReplaceStringBeginning(text, "ø", "Ø");
text = FindAndReplaceStringBeginning(text, "ǫ", "Ǫ");
text = FindAndReplaceStringBeginning(text, "ș", "Ș");
text = FindAndReplaceStringBeginning(text, "ț", "Ț");
text = FindAndReplaceStringBeginning(text, "þ", "Þ");
text = FindAndReplaceStringBeginning(text, "ū", "Ū");
text = FindAndReplaceStringBeginning(text, "û", "Û");
Expand Down
9 changes: 9 additions & 0 deletions src/unit/script_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ static int CclUnit(lua_State *l)
lua_rawgeti(l, -1, k + 1);
CUnit *u = CclGetUnitFromRef(l);
lua_pop(l, 1);
//Wyrmgus start
Assert(u != NULL);
Assert(unit != NULL);
//Wyrmgus end
u->AddInContainer(*unit);
}
lua_pop(l, 1);
Expand Down Expand Up @@ -1515,6 +1519,11 @@ static int CclSetUnitVariable(lua_State *l)
} else {
LuaError(l, "Bad variable type '%s'\n" _C_ type);
}
//Wyrmgus start
if (index == ATTACKRANGE_INDEX && unit->Container) {
unit->Container->UpdateContainerAttackRange();
}
//Wyrmgus end
}
}
lua_pushnumber(l, value);
Expand Down
40 changes: 26 additions & 14 deletions src/unit/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1378,9 +1378,8 @@ void CUnit::AddInContainer(CUnit &host)
host.UnitInside = this;
host.InsideCount++;
//Wyrmgus start
if (host.Stats->Variables[ATTACKRANGE_INDEX].Max == 0 && this->Variable[ATTACKRANGE_INDEX].Value > host.Variable[ATTACKRANGE_INDEX].Value && host.Type->CanAttack && host.Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value && this->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value) { //if host has no range by itself, but the unit has range, and the unit can attack from a transporter, change the host's range to the unit's
host.Variable[ATTACKRANGE_INDEX].Max = this->Variable[ATTACKRANGE_INDEX].Max;
host.Variable[ATTACKRANGE_INDEX].Value = this->Variable[ATTACKRANGE_INDEX].Value;
if (!SaveGameLoading) { //if host has no range by itself, but the unit has range, and the unit can attack from a transporter, change the host's range to the unit's; but don't do this while loading, as it causes a crash (since one unit needs to be loaded before the other, and when this function is processed both won't already have their variables set)
host.UpdateContainerAttackRange();
}
//Wyrmgus end
}
Expand Down Expand Up @@ -1409,22 +1408,29 @@ static void RemoveUnitFromContainer(CUnit &unit)
unit.Container = NULL;
//Wyrmgus start
//reset host attack range
if (host->Stats->Variables[ATTACKRANGE_INDEX].Max == 0 && host->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value && host->Type->CanAttack && unit.Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value) {
host->Variable[ATTACKRANGE_INDEX].Max = 0;
host->Variable[ATTACKRANGE_INDEX].Value = 0;
if (host->BoardCount > 0) {
CUnit *boarded_unit = host->UnitInside;
for (int i = 0; i < host->InsideCount; ++i, boarded_unit = boarded_unit->NextContained) {
if (boarded_unit->Variable[ATTACKRANGE_INDEX].Value > host->Variable[ATTACKRANGE_INDEX].Value && boarded_unit->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value) { //if host has no range by itself, but the unit has range, and the unit can attack from a transporter, change the host's range to the unit's
host->Variable[ATTACKRANGE_INDEX].Max = boarded_unit->Variable[ATTACKRANGE_INDEX].Max;
host->Variable[ATTACKRANGE_INDEX].Value = boarded_unit->Variable[ATTACKRANGE_INDEX].Value;
host->UpdateContainerAttackRange();
//Wyrmgus end
}

//Wyrmgus start
void CUnit::UpdateContainerAttackRange()
{
//reset attack range, if this unit is a container from which units can attack, but which doesn't have a native attack range
if (this->Stats->Variables[ATTACKRANGE_INDEX].Max == 0 && this->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value && this->Type->CanAttack) {
this->Variable[ATTACKRANGE_INDEX].Max = 0;
this->Variable[ATTACKRANGE_INDEX].Value = 0;
if (this->BoardCount > 0) {
CUnit *boarded_unit = this->UnitInside;
for (int i = 0; i < this->InsideCount; ++i, boarded_unit = boarded_unit->NextContained) {
if (boarded_unit->Variable[ATTACKRANGE_INDEX].Value > this->Variable[ATTACKRANGE_INDEX].Value && boarded_unit->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value) { //if container has no range by itself, but the unit has range, and the unit can attack from a transporter, change the container's range to the unit's
this->Variable[ATTACKRANGE_INDEX].Max = boarded_unit->Variable[ATTACKRANGE_INDEX].Max;
this->Variable[ATTACKRANGE_INDEX].Value = boarded_unit->Variable[ATTACKRANGE_INDEX].Value;
}
}
}
}
//Wyrmgus end
}

//Wyrmgus end

/**
** Affect Tile coord of a unit (with units inside) to tile (x, y).
Expand Down Expand Up @@ -3281,6 +3287,12 @@ static void HitUnit_ChangeVariable(CUnit &target, const Missile &missile)
target.Variable[var].Value = target.Variable[var].Max;
}
}

//Wyrmgus start
if (var == ATTACKRANGE_INDEX && target.Container) {
target.Container->UpdateContainerAttackRange();
}
//Wyrmgus end
}


Expand Down
22 changes: 20 additions & 2 deletions src/unit/upgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,11 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um)
if (unit.Variable[j].Max > 0) {
clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max);
}
//Wyrmgus start
if (j == ATTACKRANGE_INDEX && unit.Container) {
unit.Container->UpdateContainerAttackRange();
}
//Wyrmgus end
}
//Wyrmgus start
//change variation if current one becomes forbidden
Expand Down Expand Up @@ -1125,6 +1130,11 @@ static void RemoveUpgradeModifier(CPlayer &player, const CUpgradeModifier *um)
unit.Variable[j].Max = std::max(unit.Variable[j].Max, 0);

clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max);
//Wyrmgus start
if (j == ATTACKRANGE_INDEX && unit.Container) {
unit.Container->UpdateContainerAttackRange();
}
//Wyrmgus end
}
//Wyrmgus start
//change variation if current one becomes forbidden
Expand Down Expand Up @@ -1225,7 +1235,11 @@ void ApplyIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier *um)
if (unit.Variable[j].Max > 0) {
clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max);
}

//Wyrmgus start
if (j == ATTACKRANGE_INDEX && unit.Container) {
unit.Container->UpdateContainerAttackRange();
}
//Wyrmgus end
}

//change variation if current one becomes forbidden
Expand Down Expand Up @@ -1312,7 +1326,11 @@ static void RemoveIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier
if (unit.Variable[j].Max > 0) {
clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max);
}

//Wyrmgus start
if (j == ATTACKRANGE_INDEX && unit.Container) {
unit.Container->UpdateContainerAttackRange();
}
//Wyrmgus end
}

//change variation if current one becomes forbidden
Expand Down

0 comments on commit 1a50aac

Please sign in to comment.