Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
actionattachonselection.c
Go to the documentation of this file.
2 {
4  {
5  m_Text = "#attach";
6  }
7 
8  override void CreateConditionComponents()
9  {
12  }
13 
14  override ActionData CreateActionData()
15  {
16  AttachActionData action_data = new AttachActionData();
17  return action_data;
18  }
19 
20  int FindSlotIdToAttachOrCombine(PlayerBase player, ActionTarget target, ItemBase item)
21  {
22  EntityAI targetEntity = EntityAI.Cast(target.GetObject());
23  if (targetEntity && item)
24  {
25  if (!targetEntity.GetInventory())
26  return InventorySlots.INVALID;
27 
28  int slotsCount = item.GetInventory().GetSlotIdCount();
29  array<string> selections = new array<string>();
30  targetEntity.GetActionComponentNameList(target.GetComponentIndex(), selections);
31 
32  foreach (string selection : selections)
33  {
34  int slotId = -1;
35  if (!targetEntity.TranslateSlotFromSelection(selection, slotId))
36  slotId = InventorySlots.GetSlotIdFromString(selection);
37 
38  if (slotId == -1)
39  continue;
40 
41  for (int i=0; i < slotsCount; ++i)
42  {
43  int itemSlotId = item.GetInventory().GetSlotId(i);
44  if (slotId == itemSlotId)
45  {
46  ItemBase currentAttachment = ItemBase.Cast(targetEntity.GetInventory().FindAttachment(slotId));
47  if (currentAttachment)
48  {
49  if (currentAttachment.CanBeCombined(item))
50  return itemSlotId;
51  }
52  else
53  {
54  if (targetEntity.GetInventory() && targetEntity.GetInventory().CanAddAttachment(item))
55  return itemSlotId;
56  }
57  }
58  }
59  }
60  }
61 
62  return InventorySlots.INVALID;
63  }
64 
65 
66  override bool SetupAction(PlayerBase player, ActionTarget target, ItemBase item, out ActionData action_data, Param extra_data = null)
67  {
68  int attSlotId = InventorySlots.INVALID;
69  if (!GetGame().IsDedicatedServer() )
70  {
71  attSlotId = FindSlotIdToAttachOrCombine(player, target, item);
72  }
73 
74  if (super.SetupAction( player, target, item, action_data, extra_data))
75  {
76  if (!GetGame().IsDedicatedServer())
77  {
78  if (attSlotId != InventorySlots.INVALID)
79  {
80  AttachActionData action_data_a = AttachActionData.Cast(action_data);
81  action_data_a.m_AttSlot = attSlotId;
82 
83  return true;
84  }
85 
86  return false;
87  }
88 
89  return true;
90  }
91 
92  return false;
93  }
94 
95  override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item)
96  {
97  if (GetGame().IsMultiplayer() && GetGame().IsServer())
98  return true;
99 
100  if (target.GetObject() && target.GetObject().CanUseConstruction())
101  return false;
102 
103  return FindSlotIdToAttachOrCombine(player, target, item) != InventorySlots.INVALID;
104  }
105 
106  override void OnExecuteServer( ActionData action_data )
107  {
108  if (GetGame().IsMultiplayer())
109  return;
110 
111  ClearInventoryReservationEx(action_data);
112 
113  AttachActionData action_data_a = AttachActionData.Cast(action_data);
114  EntityAI entity;
115 
116  if (action_data.m_Target.IsProxy())
117  {
118  entity = EntityAI.Cast(action_data.m_Target.GetParent());
119  }
120  else
121  {
122  entity = EntityAI.Cast(action_data.m_Target.GetObject());
123  }
124 
125  if (entity && action_data.m_MainItem)
126  {
127  action_data.m_Player.PredictiveTakeEntityToTargetAttachmentEx(entity, action_data_a.m_MainItem, action_data_a.m_AttSlot);
128  }
129  }
130 
131  override void OnExecuteClient(ActionData action_data)
132  {
133  ClearInventoryReservationEx(action_data);
134  EntityAI targetEntity = EntityAI.Cast(action_data.m_Target.GetObject());
135  EntityAI itemEntity = action_data.m_MainItem;
136 
137  AttachActionData action_data_a = AttachActionData.Cast(action_data);
138 
139  ItemBase attachment = ItemBase.Cast(targetEntity.GetInventory().FindAttachment(action_data_a.m_AttSlot));
140  if (attachment)
141  {
142  attachment.CombineItemsClient(itemEntity);
143  }
144  else
145  {
146  ItemBase item_base = ItemBase.Cast( itemEntity );
147  float stackable = item_base.GetTargetQuantityMax( action_data_a.m_AttSlot );
148 
149  if (stackable == 0 || stackable >= item_base.GetQuantity())
150  {
151  action_data.m_Player.PredictiveTakeEntityToTargetAttachmentEx(targetEntity, itemEntity, action_data_a.m_AttSlot);
152  }
153  else if (stackable != 0 && stackable < item_base.GetQuantity())
154  {
155  item_base.SplitIntoStackMaxClient(targetEntity, action_data_a.m_AttSlot);
156  }
157  }
158  }
159 }
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
Param
Base Param Class with no parameters. Used as general purpose parameter overloaded with Param1 to Para...
Definition: param.c:11
InventorySlots
provides access to slot configuration
Definition: inventoryslots.c:5
ActionAttachOnSelection
Definition: actionattachonselection.c:1
AttachActionData
Definition: actionattach.c:1
ClearInventoryReservationEx
void ClearInventoryReservationEx(ActionData action_data)
Definition: actionbase.c:862
PlayerBase
Definition: playerbaseclient.c:1
ActionTarget
class ActionTargets ActionTarget
ActionData
Definition: actionbase.c:20
CCTCursor
Definition: cctcursor.c:1
array< string >
m_Text
protected string m_Text
Definition: actionbase.c:49
ActionSingleUseBase
Definition: actionsingleusebase.c:41
m_ConditionItem
ref CCIBase m_ConditionItem
Definition: actionbase.c:55
CCINonRuined
Definition: ccinonruined.c:1
m_ConditionTarget
ref CCTBase m_ConditionTarget
Definition: actionbase.c:56
EntityAI
Definition: building.c:5