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