/** * Check if the given tile is a valid destination. In case of for example * a carry-all it checks if the unit carrying can be placed on destination. * In case of structures, it checks if you can walk into it. * * Stack: 1 - An encoded tile, indicating the destination. * * @param script The script engine to operate on. * @return ??. */ uint16 Script_Unit_IsValidDestination(ScriptEngine *script) { Unit *u; Unit *u2; uint16 encoded; uint16 index; u = g_scriptCurrentUnit; encoded = STACK_PEEK(1); index = Tools_Index_Decode(encoded); switch (Tools_Index_GetType(encoded)) { case IT_TILE: if (!Map_IsValidPosition(index)) return 1; if (u->o.linkedID == 0xFF) return 1; u2 = Unit_Get_ByIndex(u->o.linkedID); u2->o.position = Tools_Index_GetTile(encoded); if (!Unit_IsTileOccupied(u2)) return 0; u2->o.position.tile = 0xFFFFFFFF; return 1; case IT_STRUCTURE: { Structure *s; s = Structure_Get_ByIndex(index); if (s->o.houseID == Unit_GetHouseID(u)) return 0; if (u->o.linkedID == 0xFF) return 1; u2 = Unit_Get_ByIndex(u->o.linkedID); return Unit_IsValidMovementIntoStructure(u2, s) != 0 ? 1 : 0; } default: return 1; } }
/** * Allocate a Unit. * * @param index The index to use, or UNIT_INDEX_INVALID to find an unused index. * @param typeID The type of the new Unit. * @param houseID The House of the new Unit. * @return The Unit allocated, or NULL on failure. */ Unit *Unit_Allocate(uint16 index, uint8 type, uint8 houseID) { House *h; Unit *u = NULL; if (type == 0xFF || houseID == 0xFF) return NULL; h = House_Get_ByIndex(houseID); if (h->unitCount >= h->unitCountMax) { if (g_table_unitInfo[type].movementType != MOVEMENT_WINGER && g_table_unitInfo[type].movementType != MOVEMENT_SLITHER) { if (g_var_38BC == 0x00) return NULL; } } if (index == 0 || index == UNIT_INDEX_INVALID) { uint16 indexStart = g_table_unitInfo[type].indexStart; uint16 indexEnd = g_table_unitInfo[type].indexEnd; for (index = indexStart; index <= indexEnd; index++) { u = Unit_Get_ByIndex(index); if (!u->o.flags.s.used) break; } if (index > indexEnd) return NULL; } else { u = Unit_Get_ByIndex(index); if (u->o.flags.s.used) return NULL; } assert(u != NULL); h->unitCount++; /* Initialize the Unit */ memset(u, 0, sizeof(Unit)); u->o.index = index; u->o.type = type; u->o.houseID = houseID; u->o.linkedID = 0xFF; u->o.flags.s.used = true; u->o.flags.s.allocated = true; u->o.flags.s.isUnit = true; u->o.script.delay = 0; u->route[0] = 0xFF; if (type == UNIT_SANDWORM) u->amount = 3; g_unitFindArray[g_unitFindCount++] = u; return u; }
/** * Find a UnitType and make it go to the current structure. In general, type * should be a Carry-All for this to make any sense. * * Stack: 1 - An unit type. * * @param script The script engine to operate on. * @return unknown. */ uint16 Script_Structure_FindUnitByType(ScriptEngine *script) { Structure *s; Unit *u; Unit *carryall; uint16 type; uint16 position; uint16 carryallIndex; s = g_scriptCurrentStructure; if (s->state != STRUCTURE_STATE_READY) return IT_NONE; if (s->o.linkedID == 0xFF) return IT_NONE; type = STACK_PEEK(1); position = Structure_FindFreePosition(s, false); u = Unit_Get_ByIndex(s->o.linkedID); if (g_playerHouseID == s->o.houseID && u->o.type == UNIT_HARVESTER && u->targetLast.tile == 0 && position != 0) { return IT_NONE; } carryall = Unit_CallUnitByType(type, s->o.houseID, Tools_Index_Encode(s->o.index, IT_STRUCTURE), position == 0); if (carryall == NULL) return IT_NONE; carryallIndex = Tools_Index_Encode(carryall->o.index, IT_UNIT); Object_Script_Variable4_Set(&s->o, carryallIndex); return carryallIndex; }
/** * 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; } }
/** * @brief f__167E_00F3_001E_8CB3. * @details Simplified logic for IT_TILE. */ uint16 Tools_Index_Encode(uint16 index, IndexType type) { switch (type) { case IT_TILE: { const uint16 x = (Tile_GetPackedX(index) << 1) | 0x01; const uint16 y = (Tile_GetPackedY(index) << 8) | 0x80; return (0xC000 | y | x); } case IT_UNIT: { const Unit* u = Unit_Get_ByIndex(index); return (u->o.flags.s.allocated) ? (0x4000 | index) : 0; } case IT_STRUCTURE: return (0x8000 | index); case IT_NONE: default: break; } return 0; }
/** * @brief f__167E_0005_0013_AF0C. * @details Removed guard for IT_UNIT. */ bool Tools_Index_IsValid(uint16 encoded) { if (encoded == 0) return false; uint16 index = Tools_Index_Decode(encoded); switch (Tools_Index_GetType(encoded)) { case IT_UNIT: { const Unit* u = Unit_Get_ByIndex(index); return u->o.flags.s.used && u->o.flags.s.allocated; } case IT_STRUCTURE: { const Structure* s = Structure_Get_ByIndex(index); return s->o.flags.s.used; } case IT_TILE: case IT_NONE: default: return true; } return false; }
/** * Gives a harvester to the given house if it has a refinery and no harvesters. * * @param houseID The index of the house to give a harvester to. */ void House_EnsureHarvesterAvailable(uint8 houseID) { PoolFindStruct find; Structure *s; find.houseID = houseID; find.type = 0xFFFF; find.index = 0xFFFF; while (true) { s = Structure_Find(&find); if (s == NULL) break; /* ENHANCEMENT -- Dune2 checked the wrong type to skip. LinkedID is a structure for a Construction Yard */ if (!g_dune2_enhanced && s->o.type == STRUCTURE_HEAVY_VEHICLE) continue; if (g_dune2_enhanced && s->o.type == STRUCTURE_CONSTRUCTION_YARD) continue; if (s->o.linkedID == UNIT_INVALID) continue; if (Unit_Get_ByIndex(s->o.linkedID)->o.type == UNIT_HARVESTER) return; } find.houseID = houseID; find.type = UNIT_CARRYALL; find.index = 0xFFFF; while (true) { Unit *u; u = Unit_Find(&find); if (u == NULL) break; if (u->o.linkedID == UNIT_INVALID) continue; if (Unit_Get_ByIndex(u->o.linkedID)->o.type == UNIT_HARVESTER) return; } if (Unit_IsTypeOnMap(houseID, UNIT_HARVESTER)) return; find.houseID = houseID; find.type = STRUCTURE_REFINERY; find.index = 0xFFFF; s = Structure_Find(&find); if (s == NULL) return; if (Unit_CreateWrapper(houseID, UNIT_HARVESTER, Tools_Index_Encode(s->o.index, IT_STRUCTURE)) == NULL) return; if (houseID != g_playerHouseID) return; GUI_DisplayText(String_Get_ByIndex(STR_HARVESTER_IS_HEADING_TO_REFINERY), 0); }
/** * Unknown function 0C5A. * * Stack: *none* * * @param script The script engine to operate on. * @return unknown. */ uint16 Script_Structure_Unknown0C5A(ScriptEngine *script) { tile32 tile; Structure *s; Unit *u; uint16 position; VARIABLE_NOT_USED(script); s = g_scriptCurrentStructure; if (s->o.linkedID == 0xFF) return 0; u = Unit_Get_ByIndex(s->o.linkedID); if (g_table_unitInfo[u->o.type].movementType == MOVEMENT_WINGER && Unit_SetPosition(u, s->o.position)) { s->o.linkedID = u->o.linkedID; u->o.linkedID = 0xFF; if (s->o.linkedID == 0xFF) Structure_SetState(s, STRUCTURE_STATE_IDLE); Object_Script_Variable4_Clear(&s->o); if (s->o.houseID == g_playerHouseID) Sound_Output_Feedback(g_playerHouseID + 49); return 1; } position = Structure_FindFreePosition(s, u->o.type == UNIT_HARVESTER); if (position == 0) return 0; u->o.seenByHouses |= s->o.seenByHouses; tile = Tile_Center(Tile_UnpackTile(position)); if (!Unit_SetPosition(u, tile)) return 0; s->o.linkedID = u->o.linkedID; u->o.linkedID = 0xFF; Unit_SetOrientation(u, Tile_GetDirection(s->o.position, u->o.position) & 0xE0, true, 0); Unit_SetOrientation(u, u->orientation[0].current, true, 1); if (u->o.houseID == g_playerHouseID && u->o.type == UNIT_HARVESTER) { GUI_DisplayHint(STR_SEARCH_FOR_SPICE_FIELDS_TO_HARVEST, 0x6A); } if (s->o.linkedID == 0xFF) Structure_SetState(s, STRUCTURE_STATE_IDLE); Object_Script_Variable4_Clear(&s->o); if (s->o.houseID != g_playerHouseID) return 1; if (s->o.type == STRUCTURE_REPAIR) return 1; Sound_Output_Feedback(g_playerHouseID + ((u->o.type == UNIT_HARVESTER) ? 68 : 30)); return 1; }
/** * Gets the amount of the unit linked to current unit, or current unit if not linked. * * Stack: *none*. * * @param script The script engine to operate on. * @return The amount. */ uint16 Script_Unit_GetAmount(ScriptEngine *script) { Unit *u; VARIABLE_NOT_USED(script); u = g_scriptCurrentUnit; if (u->o.linkedID == 0xFF) return u->amount; return Unit_Get_ByIndex(u->o.linkedID)->amount; }
/** * Get the object on the given packed tile. * @param packed The packed tile to get the object from. * @return The object. */ Object* Object_GetByPackedTile(uint16 packed) { Tile* t; if (Tile_IsOutOfMap(packed)) return NULL; t = &g_map[packed]; if (t->hasUnit) return &Unit_Get_ByIndex(t->index - 1)->o; if (t->hasStructure) return &Structure_Get_ByIndex(t->index - 1)->o; return NULL; }
/** * Move the unit to the first available structure it can find of the required * type. * * Stack: 1 - Type of structure. * * @param script The script engine to operate on. * @return An encoded structure index. */ uint16 Script_Unit_MoveToStructure(ScriptEngine *script) { Unit *u; PoolFindStruct find; u = g_scriptCurrentUnit; if (u->o.linkedID != 0xFF) { Structure *s; s = Tools_Index_GetStructure(Unit_Get_ByIndex(u->o.linkedID)->originEncoded); if (s != NULL && s->state == STRUCTURE_STATE_IDLE && s->o.script.variables[4] == 0) { uint16 encoded; encoded = Tools_Index_Encode(s->o.index, IT_STRUCTURE); Object_Script_Variable4_Link(Tools_Index_Encode(u->o.index, IT_UNIT), encoded); u->targetMove = u->o.script.variables[4]; return encoded; } } find.houseID = Unit_GetHouseID(u); find.index = 0xFFFF; find.type = STACK_PEEK(1); while (true) { Structure *s; uint16 encoded; s = Structure_Find(&find); if (s == NULL) break; if (s->state != STRUCTURE_STATE_IDLE) continue; if (s->o.script.variables[4] != 0) continue; encoded = Tools_Index_Encode(s->o.index, IT_STRUCTURE); Object_Script_Variable4_Link(Tools_Index_Encode(u->o.index, IT_UNIT), encoded); u->targetMove = encoded; return encoded; } return 0; }
/** * Refine spice in the current structure. * * Stack: *none* * * @param script The script engine to operate on. * @return 0 if there is no spice to refine, otherwise 1. */ uint16 Script_Structure_RefineSpice(ScriptEngine *script) { const StructureInfo *si; Structure *s; Unit *u; House *h; uint16 harvesterStep, creditsStep; VARIABLE_NOT_USED(script); s = g_scriptCurrentStructure; if (s->o.linkedID == 0xFF) { Structure_SetState(s, STRUCTURE_STATE_IDLE); return 0; } u = Unit_Get_ByIndex(s->o.linkedID); si = &g_table_structureInfo[s->o.type]; harvesterStep = (s->o.hitpoints * 256 / si->o.hitpoints) * 3 / 256; if (u->amount < harvesterStep) harvesterStep = u->amount; if (u->amount != 0 && harvesterStep < 1) harvesterStep = 1; if (harvesterStep == 0) return 0; creditsStep = 7; if (u->o.houseID != g_playerHouseID) { creditsStep += (Tools_Random_256() % 4) - 1; } creditsStep *= harvesterStep; if (House_AreAllied(g_playerHouseID, s->o.houseID)) { g_scenario.harvestedAllied += creditsStep; if (g_scenario.harvestedAllied > 65000) g_scenario.harvestedAllied = 65000; } else { g_scenario.harvestedEnemy += creditsStep; if (g_scenario.harvestedEnemy > 65000) g_scenario.harvestedEnemy = 65000; } h = House_Get_ByIndex(s->o.houseID); h->credits += creditsStep; u->amount -= harvesterStep; if (u->amount == 0) u->o.flags.s.inTransport = false; s->o.script.delay = 6; return 1; }
static uint32 SaveLoad_UnitActive(void *object, uint32 value, bool loading) { VARIABLE_NOT_USED(object); if (loading) { if ((uint16)value != 0xFFFF && value < UNIT_INDEX_MAX) { g_unitActive = Unit_Get_ByIndex((uint16)value); } else { g_unitActive = NULL; } return 0; } if (g_unitActive != NULL) { return g_unitActive->o.index; } else { return 0xFFFF; } }
/** * 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--; }
/** * Delivery of transport, either to structure or to a tile. * * Stack: *none*. * * @param script The script engine to operate on. * @return One if delivered, zero otherwise.. */ uint16 Script_Unit_TransportDeliver(ScriptEngine *script) { Unit *u; Unit *u2; VARIABLE_NOT_USED(script); u = g_scriptCurrentUnit; if (u->o.linkedID == 0xFF) return 0; if (Tools_Index_GetType(u->targetMove) == IT_UNIT) return 0; if (Tools_Index_GetType(u->targetMove) == IT_STRUCTURE) { const StructureInfo *si; Structure *s; s = Tools_Index_GetStructure(u->targetMove); si = &g_table_structureInfo[s->o.type]; if (s->o.type == STRUCTURE_STARPORT) { uint16 ret = 0; if (s->state == STRUCTURE_STATE_BUSY) { s->o.linkedID = u->o.linkedID; u->o.linkedID = 0xFF; u->o.flags.s.inTransport = false; u->amount = 0; Unit_UpdateMap(2, u); Voice_PlayAtTile(24, u->o.position); Structure_SetState(s, STRUCTURE_STATE_READY); ret = 1; } Object_Script_Variable4_Clear(&u->o); u->targetMove = 0; return ret; } if ((s->state == STRUCTURE_STATE_IDLE || (si->o.flags.busyStateIsIncoming && s->state == STRUCTURE_STATE_BUSY)) && s->o.linkedID == 0xFF) { Voice_PlayAtTile(24, u->o.position); Unit_EnterStructure(Unit_Get_ByIndex(u->o.linkedID), s); Object_Script_Variable4_Clear(&u->o); u->targetMove = 0; u->o.linkedID = 0xFF; u->o.flags.s.inTransport = false; u->amount = 0; Unit_UpdateMap(2, u); return 1; } Object_Script_Variable4_Clear(&u->o); u->targetMove = 0; return 0; } if (!Map_IsValidPosition(Tile_PackTile(Tile_Center(u->o.position)))) return 0; u2 = Unit_Get_ByIndex(u->o.linkedID); if (!Unit_SetPosition(u2, Tile_Center(u->o.position))) return 0; if (u2->o.houseID == g_playerHouseID) { Voice_PlayAtTile(24, u->o.position); } Unit_SetOrientation(u2, u->orientation[0].current, true, 0); Unit_SetOrientation(u2, u->orientation[0].current, true, 1); Unit_SetSpeed(u2, 0); u->o.linkedID = u2->o.linkedID; u2->o.linkedID = 0xFF; if (u->o.linkedID != 0xFF) return 1; u->o.flags.s.inTransport = false; Object_Script_Variable4_Clear(&u->o); u->targetMove = 0; return 1; }
/** * 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); } } }
/** * Pickup a unit (either from structure or on the map). The unit that does the * picking up returns the unit to his last position. * * Stack: *none*. * * @param script The script engine to operate on. * @return The value 0. Always. */ uint16 Script_Unit_Pickup(ScriptEngine *script) { Unit *u; VARIABLE_NOT_USED(script); u = g_scriptCurrentUnit; if (u->o.linkedID != 0xFF) return 0; switch (Tools_Index_GetType(u->targetMove)) { case IT_STRUCTURE: { Structure *s; Unit *u2; s = Tools_Index_GetStructure(u->targetMove); /* There was nothing to pickup here */ if (s->state != STRUCTURE_STATE_READY) { Object_Script_Variable4_Clear(&u->o); u->targetMove = 0; return 0; } u->o.flags.s.inTransport = true; Object_Script_Variable4_Clear(&u->o); u->targetMove = 0; u2 = Unit_Get_ByIndex(s->o.linkedID); /* Pickup the unit */ u->o.linkedID = u2->o.index & 0xFF; s->o.linkedID = u2->o.linkedID; u2->o.linkedID = 0xFF; if (s->o.linkedID == 0xFF) Structure_SetState(s, STRUCTURE_STATE_IDLE); /* Check if the unit has a return-to position or try to find spice in case of a harvester */ if (u2->targetLast.tile != 0) { u->targetMove = Tools_Index_Encode(Tile_PackTile(u2->targetLast), IT_TILE); } else if (u2->o.type == UNIT_HARVESTER && Unit_GetHouseID(u2) != g_playerHouseID) { u->targetMove = Tools_Index_Encode(Map_SearchSpice(Tile_PackTile(u->o.position), 20), IT_TILE); } Unit_UpdateMap(2, u); return 1; } case IT_UNIT: { Unit *u2; Structure *s = NULL; PoolFindStruct find; int16 minDistance = 0; u2 = Tools_Index_GetUnit(u->targetMove); if (!u2->o.flags.s.allocated) return 0; find.houseID = Unit_GetHouseID(u); find.index = 0xFFFF; find.type = 0xFFFF; /* Find closest refinery / repair station */ while (true) { Structure *s2; int16 distance; s2 = Structure_Find(&find); if (s2 == NULL) break; distance = Tile_GetDistanceRoundedUp(s2->o.position, u->o.position); if (u2->o.type == UNIT_HARVESTER) { if (s2->o.type != STRUCTURE_REFINERY || s2->state != STRUCTURE_STATE_IDLE || s2->o.script.variables[4] != 0) continue; if (minDistance != 0 && distance >= minDistance) break; minDistance = distance; s = s2; break; } if (s2->o.type != STRUCTURE_REPAIR || s2->state != STRUCTURE_STATE_IDLE || s2->o.script.variables[4] != 0) continue; if (minDistance != 0 && distance >= minDistance) continue; minDistance = distance; s = s2; } if (s == NULL) return 0; /* Deselect the unit as it is about to be picked up */ if (u2 == g_unitSelected) Unit_Select(NULL); /* Pickup the unit */ u->o.linkedID = u2->o.index & 0xFF; u->o.flags.s.inTransport = true; Unit_UpdateMap(0, u2); Unit_Hide(u2); /* Set where we are going to */ Object_Script_Variable4_Link(Tools_Index_Encode(u->o.index, IT_UNIT), Tools_Index_Encode(s->o.index, IT_STRUCTURE)); u->targetMove = u->o.script.variables[4]; Unit_UpdateMap(2, u); if (u2->o.type != UNIT_HARVESTER) return 0; /* Check if we want to return to this spice field later */ if (Map_SearchSpice(Tile_PackTile(u2->o.position), 2) == 0) { u2->targetPreLast.tile = 0; u2->targetLast.tile = 0; } return 0; } default: return 0; } }