Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
cacontinuousquantityliquidtransfer.c
Go to the documentation of this file.
2 {
3  protected int m_TendencyDrain; //1 == drain, 0 == pour, -1 == cancel!
4  protected float m_ItemQuantity;
5  protected float m_SpentQuantity;
6  protected float m_SpentQuantity_total;
7  protected float m_ItemMaxQuantity; //basically free capacity
8  protected float m_TimeElpased;
9  protected float m_QuantityUsedPerSecond;
10  protected float m_AdjustedQuantityUsedPerSecond;
11  protected float m_DefaultTimeStep;
12  protected ref Param1<float> m_SpentUnits;
13 
14  void CAContinuousQuantityLiquidTransfer(float quantity_used_per_second, float time_to_progress, int drain = -1) //-1 used as a safeguard; if no valid value arrives, callback is canceled
15  {
16  m_QuantityUsedPerSecond = quantity_used_per_second;
17  m_DefaultTimeStep = time_to_progress;
18  m_TendencyDrain = drain;
19  }
20 
21  override void Setup(ActionData action_data)
22  {
23  ItemBase target_item = ItemBase.Cast(action_data.m_Target.GetObject());
24 
25  m_TimeElpased = 0;
26  m_SpentQuantity = 0;
27 
28  if (!m_SpentUnits)
29  {
30  m_SpentUnits = new Param1<float>(0);
31  }
32  else
33  {
34  m_SpentUnits.param1 = 0;
35  }
36 
37  m_QuantityUsedPerSecond *= Math.Min(action_data.m_MainItem.GetLiquidThroughputCoef(),target_item.GetLiquidThroughputCoef());
38 
39  if (m_TendencyDrain == 1)
40  {
41  if (target_item.GetQuantity() > (action_data.m_MainItem.GetQuantityMax() - action_data.m_MainItem.GetQuantity()))
42  {
43  m_ItemMaxQuantity = action_data.m_MainItem.GetQuantityMax() - action_data.m_MainItem.GetQuantity();
44  }
45  else
46  {
47  m_ItemMaxQuantity = target_item.GetQuantity();
48  }
49 
50  m_ItemQuantity = m_ItemMaxQuantity;
51  }
52  else if (m_TendencyDrain == 0)
53  {
54  if (action_data.m_MainItem.GetQuantity() > (target_item.GetQuantityMax() - target_item.GetQuantity()))
55  {
56  m_ItemMaxQuantity = target_item.GetQuantityMax() - target_item.GetQuantity();
57  }
58  else
59  {
60  m_ItemMaxQuantity = action_data.m_MainItem.GetQuantity();
61  }
62 
63  m_ItemQuantity = m_ItemMaxQuantity;
64  }
65  }
66 
67 
68  override int Execute(ActionData action_data )
69  {
70  ItemBase target_item = ItemBase.Cast(action_data.m_Target.GetObject());
71 
72  if (!action_data.m_Player || m_TendencyDrain == -1 || !action_data.m_Target)
73  {
74  return UA_ERROR;
75  }
76 
77  if (m_ItemQuantity <= 0)
78  {
79  return UA_FINISHED;
80  }
81  else
82  {
83  if (m_SpentQuantity_total < m_ItemQuantity)
84  {
85  m_AdjustedQuantityUsedPerSecond = action_data.m_Player.GetSoftSkillsManager().AddSpecialtyBonus(m_QuantityUsedPerSecond, m_Action.GetSpecialtyWeight(), true);
86  m_SpentQuantity += m_AdjustedQuantityUsedPerSecond * action_data.m_Player.GetDeltaT();
87  m_TimeElpased += action_data.m_Player.GetDeltaT();
88 
89  if (m_TimeElpased >= m_DefaultTimeStep)
90  {
91  CalcAndSetQuantity(action_data);
92  m_TimeElpased = 0;
93  }
94 
95  return UA_PROCESSING;
96  }
97  else
98  {
99  CalcAndSetQuantity(action_data);
100  OnCompletePogress(action_data);
101  return UA_FINISHED;
102  }
103  }
104  }
105 
106  override int Cancel(ActionData action_data)
107  {
108  if (!action_data.m_Player || m_TendencyDrain == -1 || !action_data.m_Target || !action_data.m_Target.GetObject())
109  {
110  return UA_ERROR;
111  }
112 
113  CalcAndSetQuantity(action_data);
114  return UA_INTERRUPT;
115  }
116 
117  override float GetProgress()
118  {
119  if (m_TendencyDrain)
120  {
121  return m_SpentQuantity_total / m_ItemMaxQuantity;
122  }
123  else
124  {
125  return -(m_SpentQuantity_total / m_ItemMaxQuantity);
126  }
127  }
128 
129  //---------------------------------------------------------------------------
130 
131  void CalcAndSetQuantity(ActionData action_data)
132  {
133  ItemBase target_item = ItemBase.Cast(action_data.m_Target.GetObject());
134  m_SpentQuantity_total += m_SpentQuantity;
135 
136  if (GetGame().IsServer())
137  {
138  if (m_SpentUnits)
139  {
140  m_SpentUnits.param1 = m_SpentQuantity;
141  SetACData(m_SpentUnits);
142  }
143 
144  //could move following stuff to action itself, if needed
145  if (m_TendencyDrain)
146  {
147  Liquid.Transfer(target_item, action_data.m_MainItem, m_SpentQuantity);
148  }
149  else
150  {
151  Liquid.Transfer(action_data.m_MainItem, target_item, m_SpentQuantity);
152  }
153  }
154  m_SpentQuantity = 0;
155  }
156 }
ItemBase
Definition: inventoryitem.c:730
UA_INTERRUPT
const int UA_INTERRUPT
Definition: constants.c:438
GetGame
proto native CGame GetGame()
CAContinuousBase
Definition: cacontinuousbase.c:1
UA_ERROR
const int UA_ERROR
Definition: constants.c:455
CAContinuousQuantityLiquidTransfer
Definition: cacontinuousquantityliquidtransfer.c:1
UA_FINISHED
const int UA_FINISHED
Definition: constants.c:436
ActionData
Definition: actionbase.c:20
UA_PROCESSING
const int UA_PROCESSING
Definition: constants.c:434
Liquid
Definition: liquid.c:1
Math
Definition: enmath.c:6
m_Action
enum ActionInputType m_Action