/** * Link two variable4 values to eachother, and clean up existing values if * needed. * @param encodedFrom From where the link goes. * @param encodedTo To where the link goes. */ void Object_Script_Variable4_Link(uint16 encodedFrom, uint16 encodedTo) { Object* objectFrom; Object* objectTo; if (!Tools_Index_IsValid(encodedFrom)) return; if (!Tools_Index_IsValid(encodedTo)) return; objectFrom = Tools_Index_GetObject(encodedFrom); objectTo = Tools_Index_GetObject(encodedTo); if (objectFrom == NULL) return; if (objectTo == NULL) return; if (objectFrom->script.variables[4] != objectTo->script.variables[4]) { Object_Script_Variable4_Clear(objectFrom); Object_Script_Variable4_Clear(objectTo); } if (objectFrom->script.variables[4] != 0) return; Object_Script_Variable4_Set(objectFrom, encodedTo); Object_Script_Variable4_Set(objectTo, encodedFrom); return; }
/** * Clear variable4 in a safe (and recursive) way from an object. * @param object The Oject to clear variable4 of. */ void Object_Script_Variable4_Clear(Object* object) { Object* objectVariable; uint16 encoded = object->script.variables[4]; if (encoded == 0) return; objectVariable = Tools_Index_GetObject(encoded); Object_Script_Variable4_Set(object, 0); Object_Script_Variable4_Set(objectVariable, 0); }
/** * 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; }