File size: 9,199 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
class Liquid
{
	static ref map<int, ref NutritionalProfile> m_AllLiquidsByType = new map<int, ref NutritionalProfile>;
	static ref map<string, ref NutritionalProfile> m_AllLiquidsByName = new map<string, ref NutritionalProfile>;
	
	static bool m_Init = InitAllLiquids();
	
	static string GetLiquidClassname(int liquid_type)
	{
		NutritionalProfile liquid = m_AllLiquidsByType.Get(liquid_type);
		if (liquid)
		{
			return liquid.GetLiquidClassname();
		}
		
		return "";
	}
	
	static bool InitAllLiquids()
	{
		string cfg_classname = "cfgLiquidDefinitions";
		string property_value = "NULL_PROPERTY";
		int cfg_item_count = g_Game.ConfigGetChildrenCount(cfg_classname);

		for (int i = 0; i < cfg_item_count; i++)
		{
			string liquid_class_name;
			GetGame().ConfigGetChildName(cfg_classname, i, liquid_class_name);
			string liquid_full_path = string.Format("%1 %2",cfg_classname, liquid_class_name);
			int config_liquid_type = GetGame().ConfigGetInt(string.Format("%1 type", liquid_full_path));
			m_AllLiquidsByType.Insert(config_liquid_type, SetUpNutritionalProfile(config_liquid_type, liquid_class_name));
			m_AllLiquidsByName.Insert(liquid_class_name, SetUpNutritionalProfile(config_liquid_type, liquid_class_name));
			
		}
		return true;
	}
	
	//---------------------------------------------------------------------------------------------------------
	static void Transfer(ItemBase source_ent, ItemBase target_ent, float quantity = -1)
	{
		if (!Liquid.CanTransfer(source_ent, target_ent))
			return;
		
		ItemBase source = source_ent;
		ItemBase target = target_ent;
		
//		Debug.Log("Transfer, source:"+source.ToString()+" target: "+target.ToString(), "LiquidTransfer");
		
		float source_quantity 	= source.GetQuantity();
		float target_quantity 	= target.GetQuantity();
		int source_liquid_type 	= source.GetLiquidType();
		int target_liquid_type 	= target.GetLiquidType();
		int target_mask 		= target_ent.ConfigGetFloat("liquidContainerType");
		int source_mask 		= source_ent.ConfigGetFloat("liquidContainerType");
		
		float available_capacity = target.GetQuantityMax() - target_quantity;
		
//		Debug.Log("target_mask ="+target_mask.ToString(), "LiquidTransfer");
//		Debug.Log("source_mask ="+source_mask.ToString(), "LiquidTransfer");
//		Debug.Log("source_liquid_type ="+source_liquid_type.ToString(), "LiquidTransfer");
//		Debug.Log("target_liquid_type ="+target_liquid_type.ToString(), "LiquidTransfer");
		
		//target.GetBloodType(source_liquid_type);//override target liquidType
		float quantity_to_transfer;
		//transfers all
		if (quantity == -1)
		{
			quantity_to_transfer = Math.Clamp(source_quantity,0,available_capacity);
		}
		//transfers exact ammount
		else
		{
			quantity_to_transfer = Math.Clamp(Math.Min(source_quantity,quantity),0,available_capacity);
		}
		
		//target.AddQuantity(quantity_to_transfer);
		PluginTransmissionAgents m_mta = PluginTransmissionAgents.Cast(GetPlugin(PluginTransmissionAgents));
		m_mta.TransmitAgents(source_ent, target_ent, AGT_TRANSFER_COPY);
		
		source.AddQuantity(-quantity_to_transfer);
		
		Liquid.FillContainer(target_ent,source_liquid_type,quantity_to_transfer);
	}
	
	static bool CanTransfer(ItemBase source_ent, ItemBase target_ent)
	{
		if (!source_ent || !target_ent)
			return false;
		
		if (!source_ent.IsItemBase() || !target_ent.IsItemBase())
		{
			//Debug.Log("One of the Items is not of ItemBase type", "LiquidTransfer");
			return false;
		}

		Barrel_ColorBase barrelTarget = Barrel_ColorBase.Cast(target_ent);
		Barrel_ColorBase barrelSource = Barrel_ColorBase.Cast(source_ent);
		if ((barrelTarget && !barrelTarget.IsOpen()) || (barrelSource && !barrelSource.IsOpen()))
		{
			return false;
		}
		
		
		
		float source_quantity = source_ent.GetQuantity();
		if (source_quantity <= 0)
		{
			//Debug.Log("source has no quantity", "LiquidTransfer");
			return false;//if there is nothing to transfer
		}
		
		int source_liquid_type 	= source_ent.GetLiquidType();	
		if (source_liquid_type < 1) 
		{
			//Debug.Log("source has some quantity, but does not have a valid liquidType set, liquidType = "+ToString(source_liquid_type), "LiquidTransfer");
			return false;//if source is not a container
		}
		
		if (!CanFillContainer(target_ent,source_liquid_type))
		{
			return false;
		}
		
		
		return true;
	}
	static void FillContainer(ItemBase container, int liquid_type, float amount)
	{
		if (!CanFillContainer(container,liquid_type))
		{
			return;
		}
		//filling
		container.SetLiquidType(liquid_type);
		container.AddQuantity(amount); 
		
	}

	static void FillContainerEnviro(ItemBase container, int liquid_type, float amount, bool inject_agents = false)
	{
		FillContainer(container,liquid_type,amount);
		if (inject_agents)
		{
			PluginTransmissionAgents plugin = PluginTransmissionAgents.Cast(GetPlugin(PluginTransmissionAgents));
			plugin.TransmitAgents(NULL, container, AGT_WATER_POND, amount);
		}
	}
	
	static bool CanFillContainer(ItemBase container, int liquid_type, bool ignore_fullness_check = false)
	{
		if (!container) 
			return false;
		
		bool is_container_full = container.IsFullQuantity();
		
		if (is_container_full && !ignore_fullness_check)
		{
			//Debug.Log("container is full", "LiquidTransfer");
			return false;
			
		}
		int container_mask = container.ConfigGetFloat("liquidContainerType");
		
		if (container_mask == 0)
		{
			//Debug.Log("target is not a container", "LiquidTransfer");
			return false;//if the target liquidContainerType is set to 0
		}
		
		if ((liquid_type & container_mask) == 0)
		{
			//Debug.Log("target liquidContainerType does not support this liquid type", "LiquidTransfer");
			return false;
		}
		
		float container_quantity 	= container.GetQuantity();
		
		int container_liquid_type 	= container.GetLiquidType();
		
		if (container_quantity > 0 && container_liquid_type != liquid_type) 
		{
			//Debug.Log("target is not empty AND is of different liquid type than liquid_type added in", "LiquidTransfer");
			return false;
		}
		return true;
	}
	
	private static string GetLiquidConfigProperty(int liquid_type, string property_name, bool is_nutrition_property = false)
	{
		string cfg_classname = "cfgLiquidDefinitions";
		string property_value = "NULL_PROPERTY";
		int cfg_item_count = g_Game.ConfigGetChildrenCount(cfg_classname);

		for (int i = 0; i < cfg_item_count; i++)
		{
			string liquid_class_name;
			GetGame().ConfigGetChildName(cfg_classname, i, liquid_class_name);
			string liquid_full_path = string.Format("%1 %2", cfg_classname, liquid_class_name);
			int config_liquid_type = GetGame().ConfigGetInt(string.Format("%1 type", liquid_full_path));
			
			if (config_liquid_type == liquid_type)// found the specific class, now lets extract the values
			{
				if (!is_nutrition_property) 
				{
					GetGame().ConfigGetText(string.Format("%1 %2", liquid_full_path, property_name), property_value);
					return property_value;
				}
				else
				{
					GetGame().ConfigGetText(string.Format("%1 Nutrition %2", liquid_full_path, property_name), property_value);
					return property_value;
				}
			}
		}
		return property_value;
	}
	
	static NutritionalProfile GetNutritionalProfileByType(int liquid_type)
	{
		return m_AllLiquidsByType.Get(liquid_type);
	}
	
	static NutritionalProfile GetNutritionalProfileByName(string class_name)
	{
		return m_AllLiquidsByName.Get(class_name);
	}
	
	static NutritionalProfile SetUpNutritionalProfile(int liquid_type, string liquid_class_name)
	{
		float energy = GetEnergy(liquid_type);
		float nutritional_index = GetNutritionalIndex(liquid_type);
		float volume = GetFullness(liquid_type);
		float water_content = GetWaterContent(liquid_type);
		float toxicity = GetToxicity(liquid_type);
		int agents = GetAgents(liquid_type);
		float digest = GetDigestibility(liquid_type);
		NutritionalProfile profile = new NutritionalProfile(energy, water_content, nutritional_index, volume, toxicity, agents, digest);
		profile.MarkAsLiquid(liquid_type, liquid_class_name);
		return profile;
	}
	
	static int GetAgents(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "agents", true).ToInt();
	}
	
	static float GetToxicity(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "toxicity", true).ToFloat();
	}
	
	static float GetWaterContent(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "water", true).ToFloat();
	}
	
	static float GetEnergy(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "energy", true).ToFloat();
	}
	
	static float GetNutritionalIndex(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "nutritionalIndex", true).ToFloat();
	}
	
	static string GetName(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "name");
	}
	
	static float GetFlammability(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "flammability").ToFloat();
	}
	
	static float GetFullness(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "fullnessIndex", true).ToFloat();
	}
	
	static float GetDigestibility(int liquid_type)
	{
		return Liquid.GetLiquidConfigProperty(liquid_type, "digestibility", true).ToFloat();
	}
	
};