/** * Recount all Units, ignoring the cache array. Also set the unitCount * of all houses to zero. */ void Unit_Recount() { uint16 index; PoolFindStruct find = {-1, -1, -1}; House* h = House_Find(&find); while (h != NULL) { h->unitCount = 0; h = House_Find(&find); } g_unitFindCount = 0; for (index = 0; index < UNIT_INDEX_MAX; index++) { Unit* u = Unit_Get_ByIndex(index); if (!u->o.flags.s.used) continue; h = House_Get_ByIndex(u->o.houseID); h->unitCount++; g_unitFindArray[g_unitFindCount++] = u; } }
static void Scenario_Load_Houses() { House *h; uint8 houseID; for (houseID = 0; houseID < HOUSE_MAX; houseID++) { Scenario_Load_House(houseID); } h = g_playerHouse; /* In case there was no unitCountMax in the scenario, calculate * it based on values used for the AI controlled houses. */ if (h->unitCountMax == 0) { PoolFindStruct find; uint8 max; House *h; find.houseID = HOUSE_INVALID; find.index = 0xFFFF; find.type = 0xFFFF; max = 80; while ((h = House_Find(&find)) != NULL) { /* Skip the human controlled house */ if (h->flags.human) continue; max -= h->unitCountMax; } h->unitCountMax = max; } }
/** * Recount all Structures, ignoring the cache array. Also set the structureCount * of all houses to zero. */ void Structure_Recount(void) { uint16 index; PoolFindStruct find = { -1, -1, -1 }; House *h = House_Find(&find); while (h != NULL) { h->unitCount = 0; h = House_Find(&find); } g_structureFindCount = 0; for (index = 0; index < STRUCTURE_INDEX_MAX_SOFT; index++) { Structure *s = Structure_Get_ByIndex(index); if (s->o.flags.s.used) g_structureFindArray[g_structureFindCount++] = s; } }
/** * Loop over all houses, preforming various of tasks. */ void GameLoop_House(void) { PoolFindStruct find; House *h = NULL; bool tickHouse = false; bool tickPowerMaintenance = false; bool tickStarport = false; bool tickReinforcement = false; bool tickMissileCountdown = false; bool tickStarportAvailability = false; if (g_debugScenario) return; if (s_tickHouseHouse <= g_timerGame) { tickHouse = true; s_tickHouseHouse = g_timerGame + 900; } if (g_tickHousePowerMaintenance <= g_timerGame) { tickPowerMaintenance = true; g_tickHousePowerMaintenance = g_timerGame + 10800; } if (s_tickHouseStarport <= g_timerGame) { tickStarport = true; s_tickHouseStarport = g_timerGame + 180; } if (s_tickHouseReinforcement <= g_timerGame) { tickReinforcement = true; s_tickHouseReinforcement = g_timerGame + (g_debugGame ? 60 : 600); } if (s_tickHouseMissileCountdown <= g_timerGame) { tickMissileCountdown = true; s_tickHouseMissileCountdown = g_timerGame + 60; } if (s_tickHouseStarportAvailability <= g_timerGame) { tickStarportAvailability = true; s_tickHouseStarportAvailability = g_timerGame + 1800; } if (tickMissileCountdown && g_houseMissileCountdown != 0) { g_houseMissileCountdown--; Sound_Output_Feedback(g_houseMissileCountdown + 41); if (g_houseMissileCountdown == 0) Unit_LaunchHouseMissile(Map_FindLocationTile(4, g_playerHouseID)); } if (tickStarportAvailability) { uint16 type; /* Pick a random unit to increase starport availability */ type = Tools_RandomLCG_Range(0, UNIT_MAX - 1); /* Increase how many of this unit is available via starport by one */ if (g_starportAvailable[type] != 0 && g_starportAvailable[type] < 10) { if (g_starportAvailable[type] == -1) { g_starportAvailable[type] = 1; } else { g_starportAvailable[type]++; } } } if (tickReinforcement) { Unit *nu = NULL; int i; for (i = 0; i < 16; i++) { uint16 locationID; bool deployed; Unit *u; if (g_scenario.reinforcement[i].unitID == UNIT_INDEX_INVALID) continue; if (g_scenario.reinforcement[i].timeLeft == 0) continue; if (--g_scenario.reinforcement[i].timeLeft != 0) continue; u = Unit_Get_ByIndex(g_scenario.reinforcement[i].unitID); locationID = g_scenario.reinforcement[i].locationID; deployed = false; if (locationID >= 4) { if (nu == NULL) { nu = Unit_Create(UNIT_INDEX_INVALID, UNIT_CARRYALL, u->o.houseID, Tile_UnpackTile(Map_FindLocationTile(Tools_Random_256() & 3, u->o.houseID)), 100); if (nu != NULL) { nu->o.flags.s.byScenario = true; Unit_SetDestination(nu, Tools_Index_Encode(Map_FindLocationTile(locationID, u->o.houseID), IT_TILE)); } } if (nu != NULL) { u->o.linkedID = nu->o.linkedID; nu->o.linkedID = (uint8)u->o.index; nu->o.flags.s.inTransport = true; g_scenario.reinforcement[i].unitID = UNIT_INDEX_INVALID; deployed = true; } else { /* Failed to create carry-all, try again in a short moment */ g_scenario.reinforcement[i].timeLeft = 1; } } else { deployed = Unit_SetPosition(u, Tile_UnpackTile(Map_FindLocationTile(locationID, u->o.houseID))); } if (deployed && g_scenario.reinforcement[i].repeat != 0) { tile32 tile; tile.x = 0xFFFF; tile.y = 0xFFFF; g_validateStrictIfZero++; u = Unit_Create(UNIT_INDEX_INVALID, u->o.type, u->o.houseID, tile, 0); g_validateStrictIfZero--; if (u != NULL) { g_scenario.reinforcement[i].unitID = u->o.index; g_scenario.reinforcement[i].timeLeft = g_scenario.reinforcement[i].timeBetween; } } } } find.houseID = HOUSE_INVALID; find.index = 0xFFFF; find.type = 0xFFFF; while (true) { h = House_Find(&find); if (h == NULL) break; if (tickHouse) { /* ENHANCEMENT -- Originally this code was outside the house loop, which seems very odd. * This problem is considered to be so bad, that the original code has been removed. */ if (h->index != g_playerHouseID) { if (h->creditsStorage < h->credits) { h->credits = h->creditsStorage; } } else { uint16 maxCredits = max(h->creditsStorage, g_playerCreditsNoSilo); if (h->credits > maxCredits) { h->credits = maxCredits; GUI_DisplayText(String_Get_ByIndex(STR_INSUFFICIENT_SPICE_STORAGE_AVAILABLE_SPICE_IS_LOST), 1); } } if (h->index == g_playerHouseID) { if (h->creditsStorage > g_playerCreditsNoSilo) { g_playerCreditsNoSilo = 0; } if (g_playerCreditsNoSilo == 0 && g_campaignID > 1 && h->credits != 0) { if (h->creditsStorage != 0 && ((h->credits * 256 / h->creditsStorage) > 200)) { GUI_DisplayText(String_Get_ByIndex(STR_SPICE_STORAGE_CAPACITY_LOW_BUILD_SILOS), 0); } } if (h->credits < 100 && g_playerCreditsNoSilo != 0) { GUI_DisplayText(String_Get_ByIndex(STR_CREDITS_ARE_LOW_HARVEST_SPICE_FOR_MORE_CREDITS), 0); } } } if (tickHouse) House_EnsureHarvesterAvailable((uint8)h->index); if (tickStarport && h->starportLinkedID != UNIT_INDEX_INVALID) { Unit *u = NULL; h->starportTimeLeft--; if ((int16)h->starportTimeLeft < 0) h->starportTimeLeft = 0; if (h->starportTimeLeft == 0) { Structure *s; s = Structure_Get_ByIndex(g_structureIndex); if (s->o.type == STRUCTURE_STARPORT && s->o.houseID == h->index) { u = Unit_CreateWrapper((uint8)h->index, UNIT_FRIGATE, Tools_Index_Encode(s->o.index, IT_STRUCTURE)); } else { PoolFindStruct find2; find2.houseID = h->index; find2.index = 0xFFFF; find2.type = STRUCTURE_STARPORT; while (true) { s = Structure_Find(&find2); if (s == NULL) break; if (s->o.linkedID != 0xFF) continue; u = Unit_CreateWrapper((uint8)h->index, UNIT_FRIGATE, Tools_Index_Encode(s->o.index, IT_STRUCTURE)); break; } } if (u != NULL) { u->o.linkedID = (uint8)h->starportLinkedID; h->starportLinkedID = UNIT_INDEX_INVALID; u->o.flags.s.inTransport = true; Sound_Output_Feedback(38); } h->starportTimeLeft = (u != NULL) ? g_table_houseInfo[h->index].starportDeliveryTime : 1; } } if (tickHouse) { House_CalculatePowerAndCredit(h); Structure_CalculateHitpointsMax(h); if (h->timerUnitAttack != 0) h->timerUnitAttack--; if (h->timerSandwormAttack != 0) h->timerSandwormAttack--; if (h->timerStructureAttack != 0) h->timerStructureAttack--; if (h->harvestersIncoming > 0 && Unit_CreateWrapper((uint8)h->index, UNIT_HARVESTER, 0) != NULL) h->harvestersIncoming--; } if (tickPowerMaintenance) { uint16 powerMaintenanceCost = (h->powerUsage / 32) + 1; h->credits -= min(h->credits, powerMaintenanceCost); } } }
/** * Prepare the map (after loading scenario or savegame). Does some basic * sanity-check and corrects stuff all over the place. */ void Game_Prepare(void) { PoolFindStruct find; uint16 oldSelectionType; Tile *t; int i; g_validateStrictIfZero++; oldSelectionType = g_selectionType; g_selectionType = SELECTIONTYPE_MENTAT; Structure_Recount(); Unit_Recount(); Team_Recount(); t = &g_map[0]; for (i = 0; i < 64 * 64; i++, t++) { Structure *s; Unit *u; u = Unit_Get_ByPackedTile(i); s = Structure_Get_ByPackedTile(i); if (u == NULL || !u->o.flags.s.used) t->hasUnit = false; if (s == NULL || !s->o.flags.s.used) t->hasStructure = false; if (t->isUnveiled) Map_UnveilTile(i, g_playerHouseID); } find.houseID = HOUSE_INVALID; find.index = 0xFFFF; find.type = 0xFFFF; while (true) { Unit *u; u = Unit_Find(&find); if (u == NULL) break; if (u->o.flags.s.isNotOnMap) continue; Unit_RemoveFog(u); Unit_UpdateMap(1, u); } find.houseID = HOUSE_INVALID; find.index = 0xFFFF; find.type = 0xFFFF; while (true) { Structure *s; s = Structure_Find(&find); if (s == NULL) break; if (s->o.type == STRUCTURE_SLAB_1x1 || s->o.type == STRUCTURE_SLAB_2x2 || s->o.type == STRUCTURE_WALL) continue; if (s->o.flags.s.isNotOnMap) continue; Structure_RemoveFog(s); if (s->o.type == STRUCTURE_STARPORT && s->o.linkedID != 0xFF) { Unit *u = Unit_Get_ByIndex(s->o.linkedID); if (!u->o.flags.s.used || !u->o.flags.s.isNotOnMap) { s->o.linkedID = 0xFF; s->countDown = 0; } else { Structure_SetState(s, STRUCTURE_STATE_READY); } } Script_Load(&s->o.script, s->o.type); if (s->o.type == STRUCTURE_PALACE) { House_Get_ByIndex(s->o.houseID)->palacePosition = s->o.position; } if ((House_Get_ByIndex(s->o.houseID)->palacePosition.x != 0) || (House_Get_ByIndex(s->o.houseID)->palacePosition.y != 0)) continue; House_Get_ByIndex(s->o.houseID)->palacePosition = s->o.position; } find.houseID = HOUSE_INVALID; find.index = 0xFFFF; find.type = 0xFFFF; while (true) { House *h; h = House_Find(&find); if (h == NULL) break; h->structuresBuilt = Structure_GetStructuresBuilt(h); House_UpdateCreditsStorage((uint8)h->index); House_CalculatePowerAndCredit(h); } GUI_Palette_CreateRemap(g_playerHouseID); Map_SetSelection(g_selectionPosition); if (g_structureActiveType != 0xFFFF) { Map_SetSelectionSize(g_table_structureInfo[g_structureActiveType].layout); } else { Structure *s = Structure_Get_ByPackedTile(g_selectionPosition); if (s != NULL) Map_SetSelectionSize(g_table_structureInfo[s->o.type].layout); } Voice_LoadVoices(g_playerHouseID); g_tickHousePowerMaintenance = max(g_timerGame + 70, g_tickHousePowerMaintenance); g_viewport_forceRedraw = true; g_playerCredits = 0xFFFF; g_selectionType = oldSelectionType; g_validateStrictIfZero--; }