Advertisement
RustyDios

OnLoadItemReplacement

Nov 1st, 2020 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. static function ReplaceOutdatedItem(name ItemTemplateName, name ReplacementName)
  2. {
  3.     local XComGameStateHistory              History;
  4.     local XComGameState_HeadquartersXCom    XComHQ;
  5.     local X2ItemTemplateManager             ItemMgr;
  6.  
  7.     local X2ItemTemplate                    ItemTemplate, ReplacementItemTemplate;
  8.  
  9.     local XComGameState                     NewGameState;
  10.  
  11.     local array<StateObjectReference>       InventoryItemRefs;
  12.     local array<XComGameState_Item>         InventoryItems;
  13.     local XComGameState_Item                ItemState, ReplacementItemState;
  14.  
  15.     local array<XComGameState_Unit>         Soldiers;
  16.     local XComGameState_Unit                SoldierState;
  17.     local EInventorySlot                    InventorySlot;
  18.     local int iSoldier;
  19.  
  20.     //get history and Hq
  21.     History = `XCOMHISTORY;
  22.     XComHQ = `XCOMHQ;
  23.  
  24.     //karen !!! call the item manager
  25.     ItemMgr = class'X2ItemTemplateManager'.static.GetItemTemplateManager();
  26.  
  27.     //get the templates we want
  28.     ItemTemplate = ItemMgr.FindItemTemplate(ItemTemplateName);
  29.     ReplacementItemTemplate = ItemMgr.FindItemTemplate(ReplacementName);
  30.  
  31.     //state for swapping the item
  32.     NewGameState = class'XComGameStateContext_ChangeContainer'.static.CreateChangeState("Replacing Heavy Items");
  33.  
  34.     //  We will be modifying HQ's Inventory, so we need to add it to the NewGameState.
  35.     XComHQ = XComGameState_HeadquartersXCom(NewGameState.ModifyStateObject(class'XComGameState_HeadquartersXCom', XComHQ.ObjectID));
  36.  
  37.     //  If there is an item like that in the HQ Inventory already
  38.     if (ItemTemplate != none && XComHQ.HasItem(ItemTemplate))
  39.     {
  40.         //get the state of the item
  41.         ItemState = XComHQ.GetItemByName(ItemTemplate.DataName);
  42.        
  43.         //create a replacement state, with the same quanity
  44.         ReplacementItemState = ReplacementItemTemplate.CreateInstanceFromTemplate(NewGameState);
  45.         ReplacementItemState.Quantity = ItemState.Quantity;
  46.  
  47.         //remove the old item
  48.         XComHQ.RemoveItemFromInventory(NewGameState, ItemState.GetReference(), ItemState.Quantity);
  49.         NewGameState.RemoveStateObject(ItemState.GetReference().ObjectID);
  50.        
  51.         //give the new item
  52.         XComHQ.PutItemInInventory(NewGameState, ReplacementItemState);
  53.     }
  54.  
  55.     //then check every soldier's inventory and replace the old item with a new one
  56.     Soldiers = XComHQ.GetSoldiers();
  57.     for (iSoldier = 0; iSoldier < Soldiers.Length; iSoldier++)
  58.     {
  59.         InventoryItems = Soldiers[iSoldier].GetAllInventoryItems(NewGameState, false);
  60.         foreach InventoryItems(ItemState)
  61.         {
  62.             if(ItemState.GetMyTemplateName() == ItemTemplate.DataName)
  63.             {
  64.                 //create a replacement state, with the same quanity
  65.                 ReplacementItemState = ReplacementItemTemplate.CreateInstanceFromTemplate(NewGameState);
  66.                 ReplacementItemState.Quantity = ItemState.Quantity;
  67.                 InventorySlot = ItemState.InventorySlot; // save the slot location for the new item !
  68.                
  69.                 // Remove the old item from the soldier
  70.                 SoldierState = XComGameState_Unit(NewGameState.ModifyStateObject(class'XComGameState_Unit', Soldiers[iSoldier].ObjectID));
  71.                 SoldierState.RemoveItemFromInventory(ItemState, NewGameState);
  72.  
  73.                 // Delete the old item
  74.                 NewGameState.RemoveStateObject(ItemState.GetReference().ObjectID);
  75.  
  76.                 // Then add the new item to the soldier in the same slot
  77.                 SoldierState.AddItemToInventory(ReplacementItemState, InventorySlot, NewGameState);
  78.             }
  79.         }
  80.     }
  81.  
  82.     //  Submit the New Game State if we have added any State Objects to it, other than XCOM HQ itself.
  83.     if (NewGameState.GetNumGameStateObjects() > 1)
  84.     {
  85.         History.AddGameStateToHistory(NewGameState);
  86.     }
  87.     else
  88.     {
  89.         //  Otherwise, clean it up.
  90.         History.CleanupPendingGameState(NewGameState);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement