DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
ActionAttachOnSelection.c
Go to the documentation of this file.
2{
4 {
5 m_Text = "#attach";
6 }
7
8 override void CreateConditionComponents()
9 {
12 }
13
15 {
16 AttachActionData action_data = new AttachActionData();
17 return action_data;
18 }
19
21 {
22 EntityAI targetEntity = EntityAI.Cast(target.GetObject());
23 if (targetEntity && item)
24 {
25 if (!targetEntity.GetInventory())
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
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}
ref CCIBase m_ConditionItem
Definition ActionBase.c:55
ref CCTBase m_ConditionTarget
Definition ActionBase.c:56
void ClearInventoryReservationEx(ActionData action_data)
Definition ActionBase.c:861
protected string m_Text
Definition ActionBase.c:49
class ActionTargets ActionTarget
int FindSlotIdToAttachOrCombine(PlayerBase player, ActionTarget target, ItemBase item)
override void OnExecuteServer(ActionData action_data)
override ActionData CreateActionData()
override void CreateConditionComponents()
override bool SetupAction(PlayerBase player, ActionTarget target, ItemBase item, out ActionData action_data, Param extra_data=null)
override void OnExecuteClient(ActionData action_data)
override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item)
ItemBase m_MainItem
Definition ActionBase.c:28
PlayerBase m_Player
Definition ActionBase.c:33
ref ActionTarget m_Target
Definition ActionBase.c:32
provides access to slot configuration
static proto native int GetSlotIdFromString(string slot_name)
converts string to slot_id
const int INVALID
Invalid slot (-1)
override bool CanBeCombined(EntityAI other_item, bool reservation_check=true, bool stack_max_limit=false)
Definition Rag.c:52
Base Param Class with no parameters. Used as general purpose parameter overloaded with Param1 to Para...
Definition param.c:12
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
proto native CGame GetGame()