Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
liquid.c
Go to the documentation of this file.
1 class Liquid
2 {
3  static ref map<int, ref NutritionalProfile> m_AllLiquidsByType = new map<int, ref NutritionalProfile>;
5 
6  static bool m_Init = InitAllLiquids();
7 
8  static string GetLiquidClassname(int liquid_type)
9  {
10  NutritionalProfile liquid = m_AllLiquidsByType.Get(liquid_type);
11  if (liquid)
12  {
13  return liquid.GetLiquidClassname();
14  }
15 
16  return "";
17  }
18 
19  static bool InitAllLiquids()
20  {
21  string cfg_classname = "cfgLiquidDefinitions";
22  string property_value = "NULL_PROPERTY";
23  int cfg_item_count = g_Game.ConfigGetChildrenCount(cfg_classname);
24 
25  for (int i = 0; i < cfg_item_count; i++)
26  {
27  string liquid_class_name;
28  GetGame().ConfigGetChildName(cfg_classname, i, liquid_class_name);
29  string liquid_full_path = string.Format("%1 %2",cfg_classname, liquid_class_name);
30  int config_liquid_type = GetGame().ConfigGetInt(string.Format("%1 type", liquid_full_path));
31  m_AllLiquidsByType.Insert(config_liquid_type, SetUpNutritionalProfile(config_liquid_type, liquid_class_name));
32  m_AllLiquidsByName.Insert(liquid_class_name, SetUpNutritionalProfile(config_liquid_type, liquid_class_name));
33 
34  }
35  return true;
36  }
37 
38  //---------------------------------------------------------------------------------------------------------
39  static void Transfer(ItemBase source_ent, ItemBase target_ent, float quantity = -1)
40  {
41  if (!Liquid.CanTransfer(source_ent, target_ent))
42  return;
43 
44  ItemBase source = source_ent;
45  ItemBase target = target_ent;
46 
47 // Debug.Log("Transfer, source:"+source.ToString()+" target: "+target.ToString(), "LiquidTransfer");
48 
49  float source_quantity = source.GetQuantity();
50  float target_quantity = target.GetQuantity();
51  int source_liquid_type = source.GetLiquidType();
52  int target_liquid_type = target.GetLiquidType();
53  int target_mask = target_ent.ConfigGetFloat("liquidContainerType");
54  int source_mask = source_ent.ConfigGetFloat("liquidContainerType");
55 
56  float available_capacity = target.GetQuantityMax() - target_quantity;
57 
58 // Debug.Log("target_mask ="+target_mask.ToString(), "LiquidTransfer");
59 // Debug.Log("source_mask ="+source_mask.ToString(), "LiquidTransfer");
60 // Debug.Log("source_liquid_type ="+source_liquid_type.ToString(), "LiquidTransfer");
61 // Debug.Log("target_liquid_type ="+target_liquid_type.ToString(), "LiquidTransfer");
62 
63  //target.GetBloodType(source_liquid_type);//override target liquidType
64  float quantity_to_transfer;
65  //transfers all
66  if (quantity == -1)
67  {
68  quantity_to_transfer = Math.Clamp(source_quantity,0,available_capacity);
69  }
70  //transfers exact ammount
71  else
72  {
73  quantity_to_transfer = Math.Clamp(Math.Min(source_quantity,quantity),0,available_capacity);
74  }
75 
76  //target.AddQuantity(quantity_to_transfer);
77  PluginTransmissionAgents m_mta = PluginTransmissionAgents.Cast(GetPlugin(PluginTransmissionAgents));
78  m_mta.TransmitAgents(source_ent, target_ent, AGT_TRANSFER_COPY);
79 
80  source.AddQuantity(-quantity_to_transfer);
81 
82  Liquid.FillContainer(target_ent,source_liquid_type,quantity_to_transfer);
83  }
84 
85  static bool CanTransfer(ItemBase source_ent, ItemBase target_ent)
86  {
87  if (!source_ent || !target_ent)
88  return false;
89 
90  if (!source_ent.IsItemBase() || !target_ent.IsItemBase())
91  {
92  //Debug.Log("One of the Items is not of ItemBase type", "LiquidTransfer");
93  return false;
94  }
95 
96  Barrel_ColorBase barrelTarget = Barrel_ColorBase.Cast(target_ent);
97  Barrel_ColorBase barrelSource = Barrel_ColorBase.Cast(source_ent);
98  if ((barrelTarget && !barrelTarget.IsOpen()) || (barrelSource && !barrelSource.IsOpen()))
99  {
100  return false;
101  }
102 
103 
104 
105  float source_quantity = source_ent.GetQuantity();
106  if (source_quantity <= 0)
107  {
108  //Debug.Log("source has no quantity", "LiquidTransfer");
109  return false;//if there is nothing to transfer
110  }
111 
112  int source_liquid_type = source_ent.GetLiquidType();
113  if (source_liquid_type < 1)
114  {
115  //Debug.Log("source has some quantity, but does not have a valid liquidType set, liquidType = "+ToString(source_liquid_type), "LiquidTransfer");
116  return false;//if source is not a container
117  }
118 
119  if (!CanFillContainer(target_ent,source_liquid_type))
120  {
121  return false;
122  }
123 
124 
125  return true;
126  }
127  static void FillContainer(ItemBase container, int liquid_type, float amount)
128  {
129  if (!CanFillContainer(container,liquid_type))
130  {
131  return;
132  }
133  //filling
134  container.SetLiquidType(liquid_type);
135  container.AddQuantity(amount);
136 
137  }
138 
139  static void FillContainerEnviro(ItemBase container, int liquid_type, float amount, bool inject_agents = false)
140  {
141  FillContainer(container,liquid_type,amount);
142  if (inject_agents)
143  {
144  PluginTransmissionAgents plugin = PluginTransmissionAgents.Cast(GetPlugin(PluginTransmissionAgents));
145  plugin.TransmitAgents(NULL, container, AGT_WATER_POND, amount);
146  }
147  }
148 
149  static bool CanFillContainer(ItemBase container, int liquid_type, bool ignore_fullness_check = false)
150  {
151  if (!container)
152  return false;
153 
154  bool is_container_full = container.IsFullQuantity();
155 
156  if (is_container_full && !ignore_fullness_check)
157  {
158  //Debug.Log("container is full", "LiquidTransfer");
159  return false;
160 
161  }
162  int container_mask = container.ConfigGetFloat("liquidContainerType");
163 
164  if (container_mask == 0)
165  {
166  //Debug.Log("target is not a container", "LiquidTransfer");
167  return false;//if the target liquidContainerType is set to 0
168  }
169 
170  if ((liquid_type & container_mask) == 0)
171  {
172  //Debug.Log("target liquidContainerType does not support this liquid type", "LiquidTransfer");
173  return false;
174  }
175 
176  float container_quantity = container.GetQuantity();
177 
178  int container_liquid_type = container.GetLiquidType();
179 
180  if (container_quantity > 0 && container_liquid_type != liquid_type)
181  {
182  //Debug.Log("target is not empty AND is of different liquid type than liquid_type added in", "LiquidTransfer");
183  return false;
184  }
185  return true;
186  }
187 
188  private static string GetLiquidConfigProperty(int liquid_type, string property_name, bool is_nutrition_property = false)
189  {
190  string cfg_classname = "cfgLiquidDefinitions";
191  string property_value = "NULL_PROPERTY";
192  int cfg_item_count = g_Game.ConfigGetChildrenCount(cfg_classname);
193 
194  for (int i = 0; i < cfg_item_count; i++)
195  {
196  string liquid_class_name;
197  GetGame().ConfigGetChildName(cfg_classname, i, liquid_class_name);
198  string liquid_full_path = string.Format("%1 %2", cfg_classname, liquid_class_name);
199  int config_liquid_type = GetGame().ConfigGetInt(string.Format("%1 type", liquid_full_path));
200 
201  if (config_liquid_type == liquid_type)// found the specific class, now lets extract the values
202  {
203  if (!is_nutrition_property)
204  {
205  GetGame().ConfigGetText(string.Format("%1 %2", liquid_full_path, property_name), property_value);
206  return property_value;
207  }
208  else
209  {
210  GetGame().ConfigGetText(string.Format("%1 Nutrition %2", liquid_full_path, property_name), property_value);
211  return property_value;
212  }
213  }
214  }
215  return property_value;
216  }
217 
218  static NutritionalProfile GetNutritionalProfileByType(int liquid_type)
219  {
220  return m_AllLiquidsByType.Get(liquid_type);
221  }
222 
223  static NutritionalProfile GetNutritionalProfileByName(string class_name)
224  {
225  return m_AllLiquidsByName.Get(class_name);
226  }
227 
228  static NutritionalProfile SetUpNutritionalProfile(int liquid_type, string liquid_class_name)
229  {
230  float energy = GetEnergy(liquid_type);
231  float nutritional_index = GetNutritionalIndex(liquid_type);
232  float volume = GetFullness(liquid_type);
233  float water_content = GetWaterContent(liquid_type);
234  float toxicity = GetToxicity(liquid_type);
235  int agents = GetAgents(liquid_type);
236  float digest = GetDigestibility(liquid_type);
237  NutritionalProfile profile = new NutritionalProfile(energy, water_content, nutritional_index, volume, toxicity, agents, digest);
238  profile.MarkAsLiquid(liquid_type, liquid_class_name);
239  return profile;
240  }
241 
242  static int GetAgents(int liquid_type)
243  {
244  return Liquid.GetLiquidConfigProperty(liquid_type, "agents", true).ToInt();
245  }
246 
247  static float GetToxicity(int liquid_type)
248  {
249  return Liquid.GetLiquidConfigProperty(liquid_type, "toxicity", true).ToFloat();
250  }
251 
252  static float GetWaterContent(int liquid_type)
253  {
254  return Liquid.GetLiquidConfigProperty(liquid_type, "water", true).ToFloat();
255  }
256 
257  static float GetEnergy(int liquid_type)
258  {
259  return Liquid.GetLiquidConfigProperty(liquid_type, "energy", true).ToFloat();
260  }
261 
262  static float GetNutritionalIndex(int liquid_type)
263  {
264  return Liquid.GetLiquidConfigProperty(liquid_type, "nutritionalIndex", true).ToFloat();
265  }
266 
267  static string GetName(int liquid_type)
268  {
269  return Liquid.GetLiquidConfigProperty(liquid_type, "name");
270  }
271 
272  static float GetFlammability(int liquid_type)
273  {
274  return Liquid.GetLiquidConfigProperty(liquid_type, "flammability").ToFloat();
275  }
276 
277  static float GetFullness(int liquid_type)
278  {
279  return Liquid.GetLiquidConfigProperty(liquid_type, "fullnessIndex", true).ToFloat();
280  }
281 
282  static float GetDigestibility(int liquid_type)
283  {
284  return Liquid.GetLiquidConfigProperty(liquid_type, "digestibility", true).ToFloat();
285  }
286 
287 };
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
AGT_TRANSFER_COPY
const int AGT_TRANSFER_COPY
Definition: constants.c:469
GetPlugin
PluginBase GetPlugin(typename plugin_type)
Definition: pluginmanager.c:316
map
map
Definition: controlsxboxnew.c:3
g_Game
DayZGame g_Game
Definition: dayzgame.c:3727
class_name
class OptionSelectorMultistate extends OptionSelector class_name
NutritionalProfile
Definition: nutritionalprofile.c:1
Barrel_ColorBase
Definition: barrel_colorbase.c:1
Liquid
Definition: liquid.c:1
Math
Definition: enmath.c:6
AGT_WATER_POND
const int AGT_WATER_POND
Definition: constants.c:471