|
|
|
|
|
|
|
class ReplaceItemWithNewLambdaBase |
|
{ |
|
EntityAI m_OldItem; |
|
string m_NewItemType; |
|
protected ref InventoryLocation m_OldLocation; |
|
protected ref InventoryLocation m_NewLocation; |
|
protected bool m_RemoveFromLocationPassed = false; |
|
private bool m_RemoveNetworkObjectInfoPassed = false; |
|
|
|
void ReplaceItemWithNewLambdaBase(EntityAI old_item, string new_item_type) |
|
{ |
|
m_OldItem = old_item; |
|
m_NewItemType = new_item_type; |
|
} |
|
|
|
void OverrideNewLocation(InventoryLocation newLocation) |
|
{ |
|
m_NewLocation = newLocation; |
|
} |
|
|
|
void VerifyItemTypeBySlotType() {} |
|
|
|
protected bool WantCreateNewEntity() |
|
{ |
|
return m_NewLocation && m_NewItemType != string.Empty; |
|
} |
|
|
|
protected bool CanExecuteLambda() |
|
{ |
|
if (m_OldItem) |
|
if (GameInventory.LocationCanRemoveEntity(m_OldLocation)) |
|
|
|
return true; |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
protected bool PrepareLocations() |
|
{ |
|
if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[inv] ReplaceItemWithNewLambdaBase Step A) Prepare inventory locations, old_item=" + m_OldItem); |
|
m_OldLocation = new InventoryLocation; |
|
if (m_OldItem.GetInventory().GetCurrentInventoryLocation(m_OldLocation)) |
|
{ |
|
if (m_NewLocation == null) |
|
{ |
|
m_NewLocation = new InventoryLocation; |
|
m_NewLocation.CopyLocationFrom(m_OldLocation, true); |
|
|
|
|
|
if (!m_NewLocation.GetParent()) |
|
{ |
|
vector m4[4]; |
|
Math3D.MatrixIdentity4(m4); |
|
m4[3] = m_NewLocation.GetPos(); |
|
string path = "" + CFG_VEHICLESPATH + " " + m_NewItemType + " inherit_rotation"; |
|
bool keep_rotation = GetGame().ConfigIsExisting(path) && GetGame().ConfigGetInt(path) > 0; |
|
|
|
if (m_OldLocation.GetType() == InventoryLocationType.GROUND && keep_rotation) |
|
{ |
|
float dir[4]; |
|
m_OldLocation.GetDir(dir); |
|
m_NewLocation.SetGroundEx(null,m_NewLocation.GetPos(),dir); |
|
} |
|
else |
|
{ |
|
m_NewLocation.SetGround(null,m4); |
|
} |
|
} |
|
} |
|
return true; |
|
} |
|
else |
|
Error("[inv] ReplaceItemWithNewLambdaBase Step A.1) failed to get old_item inventory location"); |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void RemoveOldItemFromLocation() |
|
{ |
|
if (!GameInventory.LocationRemoveEntity(m_OldLocation)) |
|
{ |
|
Error("[inv] ReplaceItemWithNewLambdaBase Step B) failed to remove old_item rom current inventory location"); |
|
m_RemoveFromLocationPassed = false; |
|
} |
|
Print("[inv] ReplaceItemWithNewLambdaBase Step B) remove OK, loc=" + InventoryLocation.DumpToStringNullSafe(m_OldLocation)); |
|
m_RemoveFromLocationPassed = true; |
|
} |
|
protected void UndoRemoveOldItemFromLocation() |
|
{ |
|
if (!GameInventory.LocationAddEntity(m_OldLocation)) |
|
Error("[inv] ReplaceItemWithNewLambdaBase Step B) failed to undo remove"); |
|
Print("[inv] ReplaceItemWithNewLambdaBase Step B) undo remove OK, loc=" + InventoryLocation.DumpToStringNullSafe(m_OldLocation)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void RemoveNetworkObjectInfo() |
|
{ |
|
GetGame().RemoteObjectTreeDelete(m_OldItem); |
|
|
|
m_RemoveNetworkObjectInfoPassed = true; |
|
} |
|
protected void UndoRemoveNetworkObjectInfo() |
|
{ |
|
GetGame().RemoteObjectTreeCreate(m_OldItem); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected EntityAI CreateNewEntity() |
|
{ |
|
if (WantCreateNewEntity()) |
|
{ |
|
VerifyItemTypeBySlotType(); |
|
EntityAI new_item; |
|
|
|
switch (m_NewLocation.GetType()) |
|
{ |
|
case InventoryLocationType.GROUND: |
|
new_item = EntityAI.Cast(GetGame().CreateObjectEx(m_NewItemType,m_NewLocation.GetPos(),ECE_PLACE_ON_SURFACE|ECE_LOCAL)); |
|
string path = "" + CFG_VEHICLESPATH + " " + m_NewItemType + " inherit_rotation"; |
|
bool keep_rotation = GetGame().ConfigIsExisting(path) && GetGame().ConfigGetInt(path) > 0; |
|
if (keep_rotation) |
|
{ |
|
new_item.SetOrientation(m_OldItem.GetOrientation()); |
|
} |
|
break; |
|
case InventoryLocationType.ATTACHMENT: |
|
|
|
m_NewLocation.SetAttachment(m_NewLocation.GetParent(), null, m_NewLocation.GetSlot()); |
|
new_item = GameInventory.LocationCreateEntity(m_NewLocation, m_NewItemType, ECE_OBJECT_SWAP, RF_NONE); |
|
break; |
|
default: |
|
new_item = GameInventory.LocationCreateLocalEntity(m_NewLocation, m_NewItemType, ECE_OBJECT_SWAP, RF_NONE); |
|
break; |
|
} |
|
|
|
if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[inv] ReplaceItemWithNewLambdaBase Step D) Created new new_item=" + new_item); |
|
if (new_item) |
|
{ |
|
return new_item; |
|
} |
|
else |
|
{ |
|
return null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
} |
|
|
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
void CopyOldPropertiesToNew(notnull EntityAI old_item, EntityAI new_item) |
|
{ |
|
if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[inv] ReplaceItemWithNewLambdaBase Step E) Copying props " + old_item + " --> " + new_item); |
|
} |
|
|
|
|
|
|
|
|
|
protected void DeleteOldEntity() |
|
{ |
|
if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[inv] ReplaceItemWithNewLambdaBase Step F) delete old item=" + m_OldItem); |
|
m_OldItem.DeleteSafe(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void CreateNetworkObjectInfo(EntityAI new_item) |
|
{ |
|
if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[inv] ReplaceItemWithNewLambdaBase Step G) CreateNetworkObjectInfo =" + new_item); |
|
if (new_item) |
|
GetGame().RemoteObjectTreeCreate(new_item); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void OnSuccess(EntityAI new_item) |
|
{ |
|
if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[inv] ReplaceItemWithNewLambdaBase Step H) OnSuccess=" + new_item); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void OnAbort() |
|
{ |
|
Print("Error [inv] ReplaceItemWithNewLambdaBase OnAbort"); |
|
} |
|
|
|
void Execute(HumanInventoryWithFSM fsm_to_notify = null) |
|
{ |
|
int t = GetGame().GetTime(); |
|
if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[syncinv] t=" + t + " lambda.Execute start "); |
|
|
|
|
|
bool prepared = PrepareLocations(); |
|
|
|
if (prepared && CanExecuteLambda()) |
|
{ |
|
|
|
RemoveOldItemFromLocation(); |
|
if (!m_RemoveFromLocationPassed) |
|
{ |
|
Error("[inv] ReplaceItemWithNewLambdaBase Step B) ABORT - failed while rm old item from loc=" + InventoryLocation.DumpToStringNullSafe(m_OldLocation)); |
|
|
|
if (fsm_to_notify) |
|
fsm_to_notify.ProcessHandAbortEvent(new HandEventHumanCommandActionAborted(fsm_to_notify.GetManOwner())); |
|
OnAbort(); |
|
return; |
|
} |
|
|
|
|
|
RemoveNetworkObjectInfo(); |
|
|
|
|
|
EntityAI new_item = CreateNewEntity(); |
|
if (WantCreateNewEntity() && new_item == null) |
|
{ |
|
|
|
Print("Error [inv] ReplaceItemWithNewLambdaBase Step D) ABORT - wanted to create=" + m_NewItemType + " at loc=" + InventoryLocation.DumpToStringNullSafe(m_NewLocation) + ", but failed"); |
|
if (m_RemoveFromLocationPassed) |
|
UndoRemoveOldItemFromLocation(); |
|
if (m_RemoveNetworkObjectInfoPassed) |
|
UndoRemoveNetworkObjectInfo(); |
|
|
|
OnAbort(); |
|
if (fsm_to_notify) |
|
fsm_to_notify.ProcessHandAbortEvent(new HandEventHumanCommandActionAborted(fsm_to_notify.GetManOwner())); |
|
return; |
|
} |
|
|
|
|
|
CopyOldPropertiesToNew(m_OldItem, new_item); |
|
|
|
|
|
DeleteOldEntity(); |
|
|
|
|
|
CreateNetworkObjectInfo(new_item); |
|
|
|
|
|
OnSuccess(new_item); |
|
} |
|
else |
|
{ |
|
Print("[syncinv] warning, lambda cannot be executed, skipping!"); |
|
if (fsm_to_notify) |
|
fsm_to_notify.ProcessHandAbortEvent(new HandEventHumanCommandActionAborted(fsm_to_notify.GetManOwner())); |
|
OnAbort(); |
|
return; |
|
} |
|
int te = GetGame().GetTime(); |
|
int dt = te - t; |
|
if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[syncinv] te=" + te + " lambda.Execute end, exec time=" + dt); |
|
} |
|
|
|
string DumpToString() |
|
{ |
|
string s = "{ old=" + m_OldItem + " newType=" + m_NewItemType + "}"; |
|
return s; |
|
} |
|
}; |
|
|
|
|
|
class ItemInventory : GameInventory |
|
{ |
|
}; |
|
|