Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
liquid.c
Go to the documentation of this file.
1//extendable!
3{
4 string m_LiquidClassName;
5 string m_LiquidDisplayName;
6 int m_LiquidType;
7 float m_TemperatureLiquidFreezeThreshold = float.LOWEST;
8 float m_TemperatureLiquidThawThreshold = float.LOWEST;
9 float m_TemperatureLiquidBoilThreshold = Cooking.LIQUID_BOILING_POINT;
10 float m_Flammability;
11
12 ref NutritionalProfile m_NutriProfile;
13
14 void LiquidInfo(string className, NutritionalProfile profile)
15 {
16 m_NutriProfile = profile;
17 Init(className);
18 }
19
20 protected void Init(string className)
21 {
22 string path = "cfgLiquidDefinitions " + className;
23 m_LiquidClassName = className;
24 GetGame().ConfigGetTextRaw(string.Format("%1 displayName", path), m_LiquidDisplayName);
25 GetGame().FormatRawConfigStringKeys(m_LiquidDisplayName);
26 m_LiquidType = GetGame().ConfigGetInt(string.Format("%1 type", path));
27 if (GetGame().ConfigIsExisting(string.Format("%1 liquidFreezeThreshold", path)))
28 m_TemperatureLiquidFreezeThreshold = GetGame().ConfigGetFloat(string.Format("%1 liquidFreezeThreshold", path));
29 if (GetGame().ConfigIsExisting(string.Format("%1 liquidThawThreshold", path)))
30 m_TemperatureLiquidThawThreshold = GetGame().ConfigGetFloat(string.Format("%1 liquidThawThreshold", path));
31 if (GetGame().ConfigIsExisting(string.Format("%1 liquidBoilingThreshold", path)))
32 m_TemperatureLiquidBoilThreshold = GetGame().ConfigGetFloat(string.Format("%1 liquidBoilingThreshold", path));
33 m_Flammability = GetGame().ConfigGetFloat(string.Format("%1 flammability", path));
34 }
35}
36
37class Liquid
38{
40 static ref map<string, ref NutritionalProfile> m_AllLiquidsByName = new map<string, ref NutritionalProfile>; //DEPRECATED!
41 static ref map<int, ref LiquidInfo> m_LiquidInfosByType = new map<int, ref LiquidInfo>;
42 static ref map<string, ref LiquidInfo> m_LiquidInfosByName = new map<string, ref LiquidInfo>;
43 static bool m_Init = InitAllLiquids();
44
45 static string GetLiquidClassname(int liquid_type)
46 {
47 LiquidInfo info = m_LiquidInfosByType.Get(liquid_type);
48 if (info)
49 {
50 return info.m_LiquidClassName;
51 }
52
53 return "";
54 }
55
56 static bool InitAllLiquids()
57 {
58 if (!g_Game)
59 return false;
60
61 string cfg_classname = "cfgLiquidDefinitions";
62 string property_value = "NULL_PROPERTY";
63 int cfg_item_count = g_Game.ConfigGetChildrenCount(cfg_classname);
64
65 for (int i = 0; i < cfg_item_count; i++)
66 {
67 string liquid_class_name;
68 GetGame().ConfigGetChildName(cfg_classname, i, liquid_class_name);
69 string liquid_full_path = string.Format("%1 %2",cfg_classname, liquid_class_name);
70 int config_liquid_type = GetGame().ConfigGetInt(string.Format("%1 type", liquid_full_path));
71
72 NutritionalProfile profile = SetUpNutritionalProfile(config_liquid_type, liquid_class_name);
73 LiquidInfo info = new LiquidInfo(liquid_class_name, profile);
74 m_LiquidInfosByType.Insert(config_liquid_type, info);
75 m_LiquidInfosByName.Insert(liquid_class_name, info);
76
77 //legacy stuff
78 m_AllLiquidsByType.Insert(config_liquid_type, profile);
79 m_AllLiquidsByName.Insert(liquid_class_name, profile);
80 }
81 return true;
82 }
83
84 //---------------------------------------------------------------------------------------------------------
85 static void Transfer(ItemBase source_ent, ItemBase target_ent, float quantity = -1)
86 {
87 if (!Liquid.CanTransfer(source_ent, target_ent))
88 return;
89
90 float source_quantity = source_ent.GetQuantity();
91 float target_quantity = target_ent.GetQuantity();
92 float targetCfgWeight = target_ent.m_ConfigWeight;
93 int source_liquid_type = source_ent.GetLiquidType();
94
95 float available_capacity = target_ent.GetQuantityMax() - target_quantity;
96 float quantity_to_transfer;
97 //transfers all
98 if (quantity == -1)
99 {
100 quantity_to_transfer = Math.Clamp(source_quantity,0,available_capacity);
101 }
102 //transfers exact ammount
103 else
104 {
105 quantity_to_transfer = Math.Clamp(Math.Min(source_quantity,quantity),0,available_capacity);
106 }
107
108 PluginTransmissionAgents m_mta = PluginTransmissionAgents.Cast(GetPlugin(PluginTransmissionAgents));
109 m_mta.TransmitAgents(source_ent, target_ent, AGT_TRANSFER_COPY);
110
111 source_ent.AddQuantity(-quantity_to_transfer);
112
113 float retultTemp = (source_ent.GetTemperature() * quantity_to_transfer + target_ent.GetTemperature() * (targetCfgWeight + target_quantity)) / (targetCfgWeight + target_quantity + quantity_to_transfer);
114 target_ent.SetTemperature(retultTemp);
115
116 AffectContainerOnTransfer(target_ent,source_liquid_type,quantity_to_transfer,source_ent.GetTemperature());
117
118 Liquid.FillContainer(target_ent,source_liquid_type,quantity_to_transfer);
119 }
120
121 static bool CanTransfer(ItemBase source_ent, ItemBase target_ent)
122 {
123 if (!source_ent || !target_ent)
124 return false;
125
126 Barrel_ColorBase barrelTarget = Barrel_ColorBase.Cast(target_ent);
127 Barrel_ColorBase barrelSource = Barrel_ColorBase.Cast(source_ent);
128 if ((barrelTarget && !barrelTarget.IsOpen()) || (barrelSource && !barrelSource.IsOpen()))
129 {
130 return false;
131 }
132
133 if (source_ent.GetIsFrozen())
134 {
135 return false;
136 }
137
138 float source_quantity = source_ent.GetQuantity();
139 if (source_quantity <= 0)
140 {
141 //Debug.Log("source has no quantity", "LiquidTransfer");
142 return false;//if there is nothing to transfer
143 }
144
145 int source_liquid_type = source_ent.GetLiquidType();
146 if (source_liquid_type < 1)
147 {
148 //Debug.Log("source has some quantity, but does not have a valid liquidType set, liquidType = "+ToString(source_liquid_type), "LiquidTransfer");
149 return false;//if source is not a container
150 }
151
152 if (!CanFillContainer(target_ent,source_liquid_type))
153 {
154 return false;
155 }
156
157 return true;
158 }
159
160 static void FillContainer(ItemBase container, int liquid_type, float amount)
161 {
162 if (!CanFillContainer(container,liquid_type))
163 {
164 return;
165 }
166 //filling
167 container.SetLiquidType(liquid_type);
168 container.AddQuantity(amount);
169 }
170
172 static void FillContainerEnviro(ItemBase container, int liquid_type, float amount, bool inject_agents = true)
173 {
174 float containerCfgWeight = container.m_ConfigWeight;
175 float retultTemp = (GetLiquidTypeEnviroTemperature(liquid_type) * amount + container.GetTemperature() * (containerCfgWeight + container.GetQuantity())) / (container.GetQuantity() + containerCfgWeight + amount);
176 container.SetTemperature(retultTemp);
177 AffectContainerOnFill(container,liquid_type,amount);
178
179 FillContainer(container, TranslateLiquidType(liquid_type), amount);
180
181 if (inject_agents)
182 {
183 PluginTransmissionAgents plugin = PluginTransmissionAgents.Cast(GetPlugin(PluginTransmissionAgents));
184 int agtSource;
185 switch (liquid_type)
186 {
190 agtSource = AGT_WATER_POND;
191 break;
192 case LIQUID_SNOW:
193 agtSource = AGT_SNOW;
194 break;
195 case LIQUID_HOTWATER:
196 agtSource = AGT_WATER_HOT_SPRING;
197 break;
198 default:
199 agtSource = AGT_NONE;
200 break;
201 }
202
203 plugin.TransmitAgents(NULL, container, agtSource, amount);
204 }
205 }
206
208 static void AffectContainerOnFill(ItemBase container, int liquid_type, float amount)
209 {
210 container.AffectLiquidContainerOnFill(liquid_type,amount);
211 }
212
213 static void AffectContainerOnTransfer(ItemBase container, int liquidType, float amount, float sourceLiquidTransfer)
214 {
215 container.AffectLiquidContainerOnTransfer(liquidType,amount,sourceLiquidTransfer);
216 }
217
218 static bool IsLiquidDrinkWater(int liquidType)
219 {
220 if (liquidType & (LIQUID_GROUP_DRINKWATER))
221 return true;
222 return false;
223 }
224
226 static int TranslateLiquidType(int liquidType)
227 {
228 if (IsLiquidDrinkWater(liquidType))
229 return LIQUID_WATER;
230 else
231 return liquidType;
232 }
233
234 static bool CanFillContainer(ItemBase container, int liquid_type, bool ignore_fullness_check = false)
235 {
236 if (!container)
237 return false;
238
239 bool isContainerFull = container.IsFullQuantity();
240 if (isContainerFull && !ignore_fullness_check)
241 {
242 //Debug.Log("container is full", "LiquidTransfer");
243 return false;
244
245 }
246
247 int containerMask = container.GetLiquidContainerMask();
248 if (containerMask == 0)
249 {
250 //Debug.Log("target is not a container", "LiquidTransfer");
251 return false;//if the target liquidContainerType is set to 0
252 }
253
254 if ((liquid_type & containerMask) == 0)
255 {
256 //Debug.Log("target liquidContainerType does not support this liquid type", "LiquidTransfer");
257 return false;
258 }
259
260 float containerQuantity = container.GetQuantity();
261
262 int containerLiquidType = container.GetLiquidType();
263
264 if (containerQuantity > 0 && containerLiquidType != TranslateLiquidType(liquid_type))
265 {
266 //Debug.Log("target is not empty AND is of different liquid type than liquid_type added in", "LiquidTransfer");
267 return false;
268 }
269 return true;
270 }
271
273
276 static float GetLiquidTypeEnviroTemperature(int liquidType)
277 {
278 float ret = GetGame().GetMission().GetWorldData().GetLiquidTypeEnviroTemperature(liquidType);
279 //ret = Math.Max(ret,GetLiquidFreezeThreshold(liquidType));
280
281 return ret;
282 }
283
284 private static string GetLiquidConfigProperty(int liquid_type, string property_name, bool is_nutrition_property = false)
285 {
286 string cfg_classname = "cfgLiquidDefinitions";
287 string property_value = "NULL_PROPERTY";
288 if (!g_Game)
289 return property_value;
290
291 int cfg_item_count = g_Game.ConfigGetChildrenCount(cfg_classname);
292
293 for (int i = 0; i < cfg_item_count; i++)
294 {
295 string liquid_class_name;
296 GetGame().ConfigGetChildName(cfg_classname, i, liquid_class_name);
297 string liquid_full_path = string.Format("%1 %2", cfg_classname, liquid_class_name);
298 int config_liquid_type = GetGame().ConfigGetInt(string.Format("%1 type", liquid_full_path));
299
300 if (config_liquid_type == liquid_type)// found the specific class, now lets extract the values
301 {
302 if (!is_nutrition_property)
303 {
304 GetGame().ConfigGetText(string.Format("%1 %2", liquid_full_path, property_name), property_value);
305 return property_value;
306 }
307 else
308 {
309 GetGame().ConfigGetText(string.Format("%1 Nutrition %2", liquid_full_path, property_name), property_value);
310 return property_value;
311 }
312 }
313 }
314 return property_value;
315 }
316
317 static NutritionalProfile GetNutritionalProfileByType(int liquid_type)
318 {
319 LiquidInfo info = m_LiquidInfosByType.Get(liquid_type);
320 if (info && info.m_NutriProfile)
321 return info.m_NutriProfile;
322
323 return null;
324 }
325
326 static NutritionalProfile GetNutritionalProfileByName(string class_name)
327 {
328 LiquidInfo info = m_LiquidInfosByName.Get(class_name);
329 if (info && info.m_NutriProfile)
330 return info.m_NutriProfile;
331
332 return null;
333 }
334
335 static NutritionalProfile SetUpNutritionalProfile(int liquid_type, string liquid_class_name)
336 {
338 profile.m_Energy = Liquid.GetLiquidConfigProperty(liquid_type, "energy", true).ToFloat();
339 profile.m_NutritionalIndex = Liquid.GetLiquidConfigProperty(liquid_type, "nutritionalIndex", true).ToFloat();
340 profile.m_FullnessIndex = Liquid.GetLiquidConfigProperty(liquid_type, "fullnessIndex", true).ToFloat();
341 profile.m_WaterContent = Liquid.GetLiquidConfigProperty(liquid_type, "water", true).ToFloat();
342 profile.m_Toxicity = Liquid.GetLiquidConfigProperty(liquid_type, "toxicity", true).ToFloat();
343 profile.m_Agents = Liquid.GetLiquidConfigProperty(liquid_type, "agents", true).ToInt();
344 profile.m_Digestibility = Liquid.GetLiquidConfigProperty(liquid_type, "digestibility", true).ToFloat();
345 profile.m_AgentsPerDigest = Liquid.GetLiquidConfigProperty(liquid_type, "agentsPerDigest", true).ToFloat();
346
347 profile.MarkAsLiquid(liquid_type, liquid_class_name);
348 return profile;
349 }
350
351 static int GetAgents(int liquid_type)
352 {
353 return m_LiquidInfosByType.Get(liquid_type).m_NutriProfile.GetAgents();
354 }
355
356 static int GetAgentsPerDigest(int liquidType)
357 {
358 return m_LiquidInfosByType.Get(liquidType).m_NutriProfile.m_AgentsPerDigest;
359 }
360
361 static float GetToxicity(int liquid_type)
362 {
363 return m_LiquidInfosByType.Get(liquid_type).m_NutriProfile.GetToxicity();
364 }
365
366 static float GetWaterContent(int liquid_type)
367 {
368 return m_LiquidInfosByType.Get(liquid_type).m_NutriProfile.GetWaterContent();
369 }
370
371 static float GetEnergy(int liquid_type)
372 {
373 return m_LiquidInfosByType.Get(liquid_type).m_NutriProfile.GetEnergy();
374 }
375
376 static float GetNutritionalIndex(int liquid_type)
377 {
378 return m_LiquidInfosByType.Get(liquid_type).m_NutriProfile.GetNutritionalIndex();
379 }
380
381 static string GetDisplayName(int liquid_type)
382 {
383 return m_LiquidInfosByType.Get(liquid_type).m_LiquidDisplayName;
384 }
385
386 static float GetFlammability(int liquid_type)
387 {
388 return m_LiquidInfosByType.Get(liquid_type).m_Flammability;
389 }
390
391 static float GetFullness(int liquid_type)
392 {
393 return m_LiquidInfosByType.Get(liquid_type).m_NutriProfile.GetFullnessIndex();
394 }
395
396 static float GetDigestibility(int liquid_type)
397 {
398 return m_LiquidInfosByType.Get(liquid_type).m_NutriProfile.GetDigestibility();
399 }
400
401 static float GetFreezeThreshold(int liquid_type)
402 {
403 return m_LiquidInfosByType.Get(liquid_type).m_TemperatureLiquidFreezeThreshold;
404 }
405
406 static float GetThawThreshold(int liquid_type)
407 {
408 return m_LiquidInfosByType.Get(liquid_type).m_TemperatureLiquidThawThreshold;
409 }
410
411 static float GetBoilThreshold(int liquid_type)
412 {
413 return m_LiquidInfosByType.Get(liquid_type).m_TemperatureLiquidBoilThreshold;
414 }
415
417//deprecated methods below
419 static string GetName(int liquid_type)
420 {
421 return Liquid.GetLiquidConfigProperty(liquid_type, "name");
422 }
423};
#define LIQUID_RIVERWATER
#define LIQUID_FRESHWATER
#define LIQUID_SNOW
#define LIQUID_WATER
#define LIQUID_HOTWATER
#define LIQUID_STILLWATER
override bool IsOpen()
void Init(string className)
Definition liquid.c:20
Definition enmath.c:7
float GetLiquidTypeEnviroTemperature(int liquidType)
Definition worlddata.c:228
DayZGame g_Game
Definition dayzgame.c:3868
override Widget Init()
Definition dayzgame.c:127
int m_LiquidType
Definition environment.c:61
proto native CGame GetGame()
const int AGT_NONE
Definition constants.c:496
const int AGT_TRANSFER_COPY
Definition constants.c:500
const int AGT_WATER_HOT_SPRING
Definition constants.c:510
const int AGT_WATER_POND
Definition constants.c:502
const int AGT_SNOW
Definition constants.c:508
const int LIQUID_GROUP_DRINKWATER
Definition constants.c:556
float GetEnergy()
Definition itembase.c:8420
override int GetAgents()
Definition itembase.c:8810
class LiquidInfo m_AllLiquidsByType
class OptionSelectorMultistate extends OptionSelector class_name
PluginBase GetPlugin(typename plugin_type)