Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static function ReplaceOutdatedItem(name ItemTemplateName, name ReplacementName)
- {
- local XComGameStateHistory History;
- local XComGameState_HeadquartersXCom XComHQ;
- local X2ItemTemplateManager ItemMgr;
- local X2ItemTemplate ItemTemplate, ReplacementItemTemplate;
- local XComGameState NewGameState;
- local array<StateObjectReference> InventoryItemRefs;
- local array<XComGameState_Item> InventoryItems;
- local XComGameState_Item ItemState, ReplacementItemState;
- local array<XComGameState_Unit> Soldiers;
- local XComGameState_Unit SoldierState;
- local EInventorySlot InventorySlot;
- local int iSoldier;
- //get history and Hq
- History = `XCOMHISTORY;
- XComHQ = `XCOMHQ;
- //karen !!! call the item manager
- ItemMgr = class'X2ItemTemplateManager'.static.GetItemTemplateManager();
- //get the templates we want
- ItemTemplate = ItemMgr.FindItemTemplate(ItemTemplateName);
- ReplacementItemTemplate = ItemMgr.FindItemTemplate(ReplacementName);
- //state for swapping the item
- NewGameState = class'XComGameStateContext_ChangeContainer'.static.CreateChangeState("Replacing Heavy Items");
- // We will be modifying HQ's Inventory, so we need to add it to the NewGameState.
- XComHQ = XComGameState_HeadquartersXCom(NewGameState.ModifyStateObject(class'XComGameState_HeadquartersXCom', XComHQ.ObjectID));
- // If there is an item like that in the HQ Inventory already
- if (ItemTemplate != none && XComHQ.HasItem(ItemTemplate))
- {
- //get the state of the item
- ItemState = XComHQ.GetItemByName(ItemTemplate.DataName);
- //create a replacement state, with the same quanity
- ReplacementItemState = ReplacementItemTemplate.CreateInstanceFromTemplate(NewGameState);
- ReplacementItemState.Quantity = ItemState.Quantity;
- //remove the old item
- XComHQ.RemoveItemFromInventory(NewGameState, ItemState.GetReference(), ItemState.Quantity);
- NewGameState.RemoveStateObject(ItemState.GetReference().ObjectID);
- //give the new item
- XComHQ.PutItemInInventory(NewGameState, ReplacementItemState);
- }
- //then check every soldier's inventory and replace the old item with a new one
- Soldiers = XComHQ.GetSoldiers();
- for (iSoldier = 0; iSoldier < Soldiers.Length; iSoldier++)
- {
- InventoryItems = Soldiers[iSoldier].GetAllInventoryItems(NewGameState, false);
- foreach InventoryItems(ItemState)
- {
- if(ItemState.GetMyTemplateName() == ItemTemplate.DataName)
- {
- //create a replacement state, with the same quanity
- ReplacementItemState = ReplacementItemTemplate.CreateInstanceFromTemplate(NewGameState);
- ReplacementItemState.Quantity = ItemState.Quantity;
- InventorySlot = ItemState.InventorySlot; // save the slot location for the new item !
- // Remove the old item from the soldier
- SoldierState = XComGameState_Unit(NewGameState.ModifyStateObject(class'XComGameState_Unit', Soldiers[iSoldier].ObjectID));
- SoldierState.RemoveItemFromInventory(ItemState, NewGameState);
- // Delete the old item
- NewGameState.RemoveStateObject(ItemState.GetReference().ObjectID);
- // Then add the new item to the soldier in the same slot
- SoldierState.AddItemToInventory(ReplacementItemState, InventorySlot, NewGameState);
- }
- }
- }
- // Submit the New Game State if we have added any State Objects to it, other than XCOM HQ itself.
- if (NewGameState.GetNumGameStateObjects() > 1)
- {
- History.AddGameStateToHistory(NewGameState);
- }
- else
- {
- // Otherwise, clean it up.
- History.CleanupPendingGameState(NewGameState);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement