fix loading games with units in containers that do not have a type yet

This commit is contained in:
Tim Felgentreff 2022-07-26 19:44:12 +02:00
parent d5c916b724
commit d8ff81f90c
2 changed files with 18 additions and 3 deletions

View file

@ -331,6 +331,7 @@ static int CclUnit(lua_State *l)
}
CUnit *unit = &UnitManager->GetSlotUnit(slot);
bool hadType = unit->Type != NULL;
CUnitType *type = NULL;
CUnitType *seentype = NULL;
CPlayer *player = NULL;
@ -645,6 +646,15 @@ static int CclUnit(lua_State *l)
MapMarkUnitSight(*unit);
}
if (!hadType && unit->Container) {
// this unit was assigned to a container before it had a type, so we
// need to actually add it now, since only with a type do we know the
// BoardSize it takes up in the container
CUnit *host = unit->Container;
unit->Container = NULL;
unit->AddInContainer(*host);
}
return 0;
}

View file

@ -1093,16 +1093,21 @@ void CUnit::AddInContainer(CUnit &host)
{
Assert(Container == NULL);
Container = &host;
if (!Type) {
// if we're loading a game, the Type may not have been initialized
// yet. so we ignore this and the unit gets added later when it is
// loaded via CclUnit
return;
}
if (host.InsideCount == 0) {
NextContained = PrevContained = this;
host.UnitInside = this;
} else {
// keep sorted by size.
// FIXME: if we're loading a game, the Type may not have been initialized yet!!
int mySize = Type ? Type->BoardSize : 1;
int mySize = Type->BoardSize;
NextContained = host.UnitInside;
bool becomeFirst = true;
while (NextContained->Type ? NextContained->Type->BoardSize : 1 > mySize) {
while (NextContained->Type->BoardSize > mySize) {
becomeFirst = false;
NextContained = NextContained->NextContained;
if (NextContained == host.UnitInside) {