Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
cooking.c
Go to the documentation of this file.
2 {
3  NONE = 0, //no cooking method available
4  BAKING = 1,
5  BOILING = 2,
6  DRYING = 3,
7  TIME = 4,
8 
10 }
11 
12 class Cooking
13 {
14  static const float TIME_WITH_SUPPORT_MATERIAL_COEF = 1.0;
15  static const float TIME_WITHOUT_SUPPORT_MATERIAL_COEF = 2.0;
16 
17  static const float COOKING_FOOD_TIME_INC_VALUE = 2;
18  static const float COOKING_LARD_DECREASE_COEF = 25;
19  static const float COOKING_FOOD_QUANTITY_DECREASE_AMOUNT_NONE = 25;
20  static const float COOKING_FOOD_QUANTITY_DECREASE_AMOUNT_LARD = 0;
21 
22  static const float DEFAULT_COOKING_TEMPERATURE = 150; //default temperature for cooking (e.g. cooking on stick)
23  static const float FOOD_MAX_COOKING_TEMPERATURE = 150; //
24  static const float PARAM_BURN_DAMAGE_COEF = 0.05; //value for calculating damage on items located in fireplace CargoGrid
25 
26  static const float LIQUID_BOILING_POINT = 150; //boiling point for liquids
27  static const float LIQUID_VAPOR_QUANTITY = 2; //vapor quantity
28 
29  typename COOKING_EQUIPMENT_POT = Pot;
30  typename COOKING_EQUIPMENT_FRYINGPAN = FryingPan;
31  typename COOKING_EQUIPMENT_CAULDRON = Cauldron;
32  typename COOKING_INGREDIENT_LARD = Lard;
33 
34  void ProcessItemToCook(notnull ItemBase pItem, ItemBase cookingEquip, Param2<CookingMethodType, float> pCookingMethod, out Param2<bool, bool> pStateFlags)
35  {
36  Edible_Base item_to_cook = Edible_Base.Cast(pItem);
37 
39  pStateFlags = new Param2<bool, bool>(false, false);
40 
41  if (item_to_cook && item_to_cook.CanBeCooked())
42  {
44  item_to_cook.MakeSoundsOnClient(true, pCookingMethod.param1);
45 
47  UpdateCookingState(item_to_cook, pCookingMethod.param1, cookingEquip, pCookingMethod.param2);
48 
49  //check for done state for boiling and drying
50  if (item_to_cook.IsFoodBoiled() || item_to_cook.IsFoodDried())
51  {
52  pStateFlags.param1 = true;
53  }
55  else if (item_to_cook.IsFoodBaked() && item_to_cook.Type() != Lard)
56  {
57  pStateFlags.param1 = true;
58  }
60  else if (item_to_cook.IsFoodBurned())
61  {
62  pStateFlags.param2 = true;
63  }
64  }
65  else
66  {
67  //damage item
68  pItem.DecreaseHealth("", "", PARAM_BURN_DAMAGE_COEF * 100);
69 
70  //add temperature to item
71  AddTemperatureToItem(pItem, null, 0);
72  }
73  }
74 
75  //COOKING PROCESS
76  //--- Cooking with equipment (pot)
77  //Returns 1 if the item changed its cooking stage, 0 if not
78  int CookWithEquipment(ItemBase cooking_equipment, float cooking_time_coef = 1)
79  {
80  bool is_empty;
81 
82  //check cooking conditions
83  if (cooking_equipment == null)
84  {
85  return 0;
86  }
87 
88  if (cooking_equipment.IsRuined())
89  {
90  return 0;
91  }
92 
93  //manage items in cooking equipment
94  Param2<bool, bool> stateFlags = new Param2<bool, bool>(false, false); // 1st - done; 2nd - burned
95  Param2<CookingMethodType, float> cookingMethodWithTime = GetCookingMethodWithTimeOverride(cooking_equipment);
96 
98  if (cooking_time_coef != 1)
99  {
100  cookingMethodWithTime.param2 = cooking_time_coef;
101  }
102 
103  CargoBase cargo = cooking_equipment.GetInventory().GetCargo();
104  if (cargo)
105  {
106  is_empty = cargo.GetItemCount() == 0;
107 
108  //process items
109  for (int i = 0; i < cargo.GetItemCount(); i++)
110  {
111  ProcessItemToCook(ItemBase.Cast(cargo.GetItem(i)), cooking_equipment, cookingMethodWithTime, stateFlags);
112  }
113  }
114  else
115  {
116  ProcessItemToCook(cooking_equipment, cooking_equipment, cookingMethodWithTime, stateFlags);
117  }
118 
119  //manage cooking equipment
120  Bottle_Base bottle_base = Bottle_Base.Cast(cooking_equipment);
121  if (bottle_base)
122  {
123  float cookingEquipmentTemp = cooking_equipment.GetTemperature();
124 
125  //handle water boiling
126  if (cookingEquipmentTemp >= LIQUID_BOILING_POINT)
127  {
128  //remove agents
129  cooking_equipment.RemoveAllAgents();
130 
131  if (cooking_equipment.GetQuantity() > 0)
132  {
133  //vaporize liquid
134  cooking_equipment.AddQuantity(-LIQUID_VAPOR_QUANTITY);
135  }
136  }
137 
138  //handle audio visuals
139  bottle_base.RefreshAudioVisualsOnClient(cookingMethodWithTime.param1, stateFlags.param1, is_empty, stateFlags.param2);
140  }
141 
142  FryingPan frying_pan = FryingPan.Cast(cooking_equipment);
143  if (frying_pan && !bottle_base)
144  {
145  //handle audio visuals
146  frying_pan.RefreshAudioVisualsOnClient(cookingMethodWithTime.param1, stateFlags.param1, is_empty, stateFlags.param2);
147  }
148 
149  return 1;
150  }
151 
152  //Returns 1 if the item changed its cooking stage, 0 if not
153  int CookOnStick( Edible_Base item_to_cook, float cook_time_inc )
154  {
155  if ( item_to_cook && item_to_cook.CanBeCookedOnStick() )
156  {
157  //update food
158  return UpdateCookingStateOnStick( item_to_cook, cook_time_inc );
159  }
160 
161  return 0;
162  }
163 
164  //Returns 1 if the item changed its cooking stage, 0 if not
165  protected int UpdateCookingState(Edible_Base item_to_cook, CookingMethodType cooking_method, ItemBase cooking_equipment, float cooking_time_coef)
166  {
167  //food properties
168  float food_temperature = item_to_cook.GetTemperature();
169 
170  //{min_temperature, time_to_cook, max_temperature (optional)}
171  //get next stage name - if next stage is not defined, next stage name will be empty "" and no cooking properties (food_min_temp, food_time_to_cook, food_max_temp) will be set
172  FoodStageType new_stage_type = item_to_cook.GetNextFoodStageType(cooking_method);
173 
174  float food_min_temp = 0;
175  float food_time_to_cook = 0;
176  float food_max_temp = -1;
177 
178  //Set next stage cooking properties if next stage possible
179  if (item_to_cook.CanChangeToNewStage(cooking_method)) // new_stage_type != NONE
180  {
181  array<float> next_stage_cooking_properties = new array<float>();
182  next_stage_cooking_properties = FoodStage.GetAllCookingPropertiesForStage(new_stage_type, null, item_to_cook.GetType());
183 
184  food_min_temp = next_stage_cooking_properties.Get(eCookingPropertyIndices.MIN_TEMP);
185  food_time_to_cook = next_stage_cooking_properties.Get(eCookingPropertyIndices.COOK_TIME);
186  // The last element is optional and might not exist
187  if (next_stage_cooking_properties.Count() > 2)
188  {
189  food_max_temp = next_stage_cooking_properties.Get(eCookingPropertyIndices.MAX_TEMP);
190  }
191  }
192 
193  //add temperature
194  AddTemperatureToItem(item_to_cook, cooking_equipment, food_min_temp);
195 
196  //add cooking time if the food can be cooked by this method
197  if (food_min_temp > 0 && food_temperature >= food_min_temp)
198  {
199  float new_cooking_time = item_to_cook.GetCookingTime() + COOKING_FOOD_TIME_INC_VALUE * cooking_time_coef;
200  item_to_cook.SetCookingTime(new_cooking_time);
201 
202  //progress to next stage
203  if (item_to_cook.GetCookingTime() >= food_time_to_cook)
204  {
205  //if max temp is defined check next food stage
206  if (food_max_temp >= 0)
207  {
208  if (food_temperature > food_max_temp && item_to_cook.GetFoodStageType() != FoodStageType.BURNED)
209  {
210  new_stage_type = FoodStageType.BURNED;
211  }
212  }
213 
215  item_to_cook.ChangeFoodStage(new_stage_type);
217  item_to_cook.RemoveAllAgentsExcept(eAgents.BRAIN);
218 
219  if (cooking_equipment)
220  {
221  if (cooking_method == CookingMethodType.BAKING)
222  {
223  ItemBase lard = GetItemTypeFromCargo(COOKING_INGREDIENT_LARD, cooking_equipment);
224  if (lard)
225  {
226  //decrease lard quantity
227  float lardQuantity = lard.GetQuantity() - COOKING_LARD_DECREASE_COEF;
228  lardQuantity = Math.Clamp(lardQuantity, 0, lard.GetQuantityMax());
229  lard.SetQuantity(lardQuantity);
230  }
231  else
232  {
234  DecreaseCookedItemQuantity(item_to_cook, COOKING_FOOD_QUANTITY_DECREASE_AMOUNT_NONE);
235  }
236  }
237  }
238  else
239  {
241  DecreaseCookedItemQuantity(item_to_cook, COOKING_FOOD_QUANTITY_DECREASE_AMOUNT_NONE);
242  }
243 
244  //reset cooking time
245  item_to_cook.SetCookingTime(0);
246 
247  return 1;
248  }
249  }
250 
251  return 0;
252  }
253 
254  //Returns 1 if the item changed its cooking stage, 0 if not
255  protected int UpdateCookingStateOnStick( Edible_Base item_to_cook, float cook_time_inc )
256  {
257  //food properties
258  float food_temperature = item_to_cook.GetTemperature();
259 
260  //{min_temperature, time_to_cook, max_temperature (optional)}
261  //get next stage name - if next stage is not defined, next stage name will be empty "" and no cooking properties (food_min_temp, food_time_to_cook, food_max_temp) will be set
262  FoodStageType new_stage_type = item_to_cook.GetNextFoodStageType( CookingMethodType.BAKING );
263  float food_min_temp = 0;
264  float food_time_to_cook = 0;
265  float food_max_temp = -1;
266  bool is_done = false; // baked
267  bool is_burned = false; // burned
268 
269  //Set next stage cooking properties if next stage possible
270  if ( item_to_cook.CanChangeToNewStage( CookingMethodType.BAKING ) )
271  {
272  array<float> next_stage_cooking_properties = new array<float>;
273  next_stage_cooking_properties = FoodStage.GetAllCookingPropertiesForStage( new_stage_type, null, item_to_cook.GetType() );
274 
275  food_min_temp = next_stage_cooking_properties.Get( eCookingPropertyIndices.MIN_TEMP );
276  food_time_to_cook = next_stage_cooking_properties.Get( eCookingPropertyIndices.COOK_TIME );
277  // The last element is optional and might not exist
278  if ( next_stage_cooking_properties.Count() > 2 )
279  food_max_temp = next_stage_cooking_properties.Get( eCookingPropertyIndices.MAX_TEMP );
280  }
281 
282 
283  // refresh audio
284  if (item_to_cook.GetInventory().IsAttachment())
285  {
286  item_to_cook.MakeSoundsOnClient(true, CookingMethodType.BAKING);
287  //add temperature
288  AddTemperatureToItem(item_to_cook, null, food_min_temp);
289  }
290 
291  //add cooking time if the food can be cooked by this method
292  if ( food_min_temp > 0 && food_temperature >= food_min_temp )
293  {
294  float new_cooking_time = item_to_cook.GetCookingTime() + cook_time_inc;
295  item_to_cook.SetCookingTime( new_cooking_time );
296 
297  //progress to next stage
298  if ( item_to_cook.GetCookingTime() >= food_time_to_cook )
299  {
300  //if max temp is defined check next food stage
301  if ( food_max_temp >= 0 )
302  {
303  if ( food_temperature > food_max_temp && item_to_cook.GetFoodStageType() != FoodStageType.BURNED )
304  {
305  new_stage_type = FoodStageType.BURNED;
306  }
307  }
308 
309  //change food stage
310  item_to_cook.ChangeFoodStage( new_stage_type );
311  item_to_cook.RemoveAllAgentsExcept(eAgents.BRAIN);
312 
313  DecreaseCookedItemQuantity(item_to_cook, COOKING_FOOD_QUANTITY_DECREASE_AMOUNT_NONE);
314 
315  //reset cooking time
316  item_to_cook.SetCookingTime( 0 );
317  return 1;
318  }
319  }
320 
321  return 0;
322  }
323 
324  void SmokeItem(Edible_Base item_to_cook, float cook_time_inc)
325  {
326  if (item_to_cook)
327  {
328  float new_cook_time = item_to_cook.GetCookingTime() + cook_time_inc;
329  float drying_cook_time = FoodStage.GetCookingPropertyFromIndex(eCookingPropertyIndices.COOK_TIME, FoodStageType.DRIED, null, item_to_cook.GetType());
330 
331  switch (item_to_cook.GetFoodStageType())
332  {
333  case FoodStageType.RAW:
334  item_to_cook.SetCookingTime(new_cook_time);
335 
336  if (item_to_cook.GetCookingTime() >= drying_cook_time)
337  {
338  item_to_cook.ChangeFoodStage(FoodStageType.DRIED);
339  item_to_cook.RemoveAllAgentsExcept(eAgents.BRAIN);
340  item_to_cook.SetCookingTime(0);
341  }
342  break;
343  default:
344  item_to_cook.SetCookingTime(new_cook_time);
345 
346  if (item_to_cook.GetCookingTime() >= drying_cook_time)
347  {
348  item_to_cook.ChangeFoodStage(FoodStageType.BURNED);
349  item_to_cook.RemoveAllAgents();
350  item_to_cook.SetCookingTime(0);
351  }
352  break;
353  }
354  }
355  }
356 
358  {
359  Edible_Base edible;
360  if (pItem)
361  {
362  if (pItem.GetInventory()) // cookware
363  {
364  CargoBase cargo = pItem.GetInventory().GetCargo();
365  if (cargo)
366  {
367  for (int i = 0; i < cargo.GetItemCount(); i++)
368  {
369  edible = Edible_Base.Cast(cargo.GetItem(i));
370  if (edible)
371  {
372  edible.MakeSoundsOnClient(false);
373  }
374  }
375  }
376  }
377  else
378  {
379  edible = Edible_Base.Cast(pItem);
380  if (edible)
381  {
382  edible.MakeSoundsOnClient(false);
383  }
384  }
385  }
386  }
387 
389  protected ItemBase GetItemTypeFromCargo( typename item_type, ItemBase cooking_equipment )
390  {
391  CargoBase cargo = cooking_equipment.GetInventory().GetCargo();
392  if (cargo)
393  {
394  for (int i = 0; i < cargo.GetItemCount(); i++)
395  {
396  EntityAI entity = cargo.GetItem(i);
397  if (entity.Type() == item_type)
398  {
399  ItemBase item = ItemBase.Cast(entity);
400 
401  return item;
402  }
403  }
404  }
405 
406  return null;
407  }
408 
410  protected CookingMethodType GetCookingMethod(ItemBase cooking_equipment)
411  {
412  if (cooking_equipment.Type() == COOKING_EQUIPMENT_POT || cooking_equipment.Type() == COOKING_EQUIPMENT_CAULDRON)
413  {
414  //has water, but not petrol dammit X)
415  if (cooking_equipment.GetQuantity() > 0 && cooking_equipment.GetLiquidType() != LIQUID_GASOLINE)
416  {
417  return CookingMethodType.BOILING;
418  }
419 
420  //has lard in cargo
421  if (GetItemTypeFromCargo(COOKING_INGREDIENT_LARD, cooking_equipment))
422  {
423  return CookingMethodType.BAKING;
424  }
425  return CookingMethodType.DRYING;
426  }
427 
428  if (cooking_equipment.Type() == COOKING_EQUIPMENT_FRYINGPAN)
429  {
430  if (GetItemTypeFromCargo(COOKING_INGREDIENT_LARD, cooking_equipment))
431  {
432  return CookingMethodType.BAKING;
433  }
434  return CookingMethodType.DRYING;
435  }
436 
437  return CookingMethodType.NONE;
438  }
439 
441  {
443 
444  switch (cooking_equipment.Type())
445  {
449  if (cooking_equipment.GetQuantity() > 0)
450  {
451  if (cooking_equipment.GetLiquidType() == LIQUID_GASOLINE)
452  {
454  val = new Param2<CookingMethodType, float>(CookingMethodType.DRYING, TIME_WITHOUT_SUPPORT_MATERIAL_COEF);
455  break;
456  }
457 
459  break;
460  }
461 
462  if (GetItemTypeFromCargo(COOKING_INGREDIENT_LARD, cooking_equipment))
463  {
464  //has lard in cargo, slower process
466  break;
467  }
468 
469  if (cooking_equipment.GetInventory().GetCargo().GetItemCount() > 0)
470  {
471  val = new Param2<CookingMethodType, float>(CookingMethodType.BAKING, TIME_WITHOUT_SUPPORT_MATERIAL_COEF);
472  break;
473  }
474 
475  val = new Param2<CookingMethodType, float>(CookingMethodType.NONE, TIME_WITHOUT_SUPPORT_MATERIAL_COEF);
476  break;
477 
478  default:
479  val = new Param2<CookingMethodType, float>(CookingMethodType.BAKING, TIME_WITHOUT_SUPPORT_MATERIAL_COEF);
480  break;
481  }
482 
483  return val;
484  }
485 
487  {
488  Edible_Base food_on_stick = Edible_Base.Cast( stick_item.GetAttachmentByType( Edible_Base ) );
489 
490  return food_on_stick;
491  }
492 
493  float GetTimeToCook( Edible_Base item_to_cook, CookingMethodType cooking_method )
494  {
495  FoodStageType food_stage_type = item_to_cook.GetNextFoodStageType( cooking_method );
496  return FoodStage.GetCookingPropertyFromIndex( eCookingPropertyIndices.COOK_TIME, food_stage_type, null, item_to_cook.GetType());
497  }
498 
499  float GetMinTempToCook( Edible_Base item_to_cook, CookingMethodType cooking_method )
500  {
501  FoodStageType food_stage_type = item_to_cook.GetNextFoodStageType( cooking_method );
502  return FoodStage.GetCookingPropertyFromIndex( eCookingPropertyIndices.MIN_TEMP, food_stage_type, null, item_to_cook.GetType());
503  }
504 
505  //add temperature to item
506  protected void AddTemperatureToItem( ItemBase cooked_item, ItemBase cooking_equipment, float min_temperature )
507  {
508  if ( cooked_item.GetTemperatureMax() >= FireplaceBase.PARAM_ITEM_HEAT_MIN_TEMP )
509  {
510  float item_temperature = cooked_item.GetTemperature();
511 
512  //set actual cooking temperature
513  float actual_cooking_temp = DEFAULT_COOKING_TEMPERATURE; //default
514  if ( cooking_equipment )
515  {
516  actual_cooking_temp = cooking_equipment.GetTemperature();
517  }
518 
519  //add temperature
520  if ( actual_cooking_temp > item_temperature )
521  {
522  item_temperature = actual_cooking_temp * 0.5;
523  item_temperature = Math.Clamp( item_temperature, min_temperature, FOOD_MAX_COOKING_TEMPERATURE );
524 
525  //set new temperature
526  if ( GetGame() && GetGame().IsServer() )
527  {
528  cooked_item.SetTemperature( item_temperature );
529  }
530  }
531  }
532  }
533 
534  protected void DecreaseCookedItemQuantity(notnull Edible_Base pItem, float pAmount = 0.0)
535  {
536  if (GetGame().IsServer())
537  {
538  float quantity = pItem.GetQuantity() - pAmount;
539  quantity = Math.Clamp(quantity, 0, pItem.GetQuantityMax());
540  pItem.SetQuantity(quantity);
541  }
542  }
543 }
ItemBase
Definition: inventoryitem.c:730
Param2
Definition: ppeconstants.c:66
GetTimeToCook
float GetTimeToCook(Edible_Base item_to_cook, CookingMethodType cooking_method)
Definition: cooking.c:493
GetGame
proto native CGame GetGame()
GetCookingMethod
protected CookingMethodType GetCookingMethod(ItemBase cooking_equipment)
DEPREACTED.
Definition: cooking.c:410
COOKING_INGREDIENT_LARD
COOKING_INGREDIENT_LARD
Definition: cooking.c:32
UpdateCookingStateOnStick
protected int UpdateCookingStateOnStick(Edible_Base item_to_cook, float cook_time_inc)
Definition: cooking.c:255
CookOnStick
int CookOnStick(Edible_Base item_to_cook, float cook_time_inc)
Definition: cooking.c:153
AddTemperatureToItem
protected void AddTemperatureToItem(ItemBase cooked_item, ItemBase cooking_equipment, float min_temperature)
Definition: cooking.c:506
DRYING
@ DRYING
Definition: cooking.c:6
BAKING
@ BAKING
Definition: cooking.c:4
TIME_WITH_SUPPORT_MATERIAL_COEF
enum CookingMethodType TIME_WITH_SUPPORT_MATERIAL_COEF
Bottle_Base
Definition: canistergasoline.c:1
FireplaceBase
Definition: barrelholes_colorbase.c:1
LIQUID_GASOLINE
const int LIQUID_GASOLINE
Definition: constants.c:509
UpdateCookingState
protected int UpdateCookingState(Edible_Base item_to_cook, CookingMethodType cooking_method, ItemBase cooking_equipment, float cooking_time_coef)
Definition: cooking.c:165
GetMinTempToCook
float GetMinTempToCook(Edible_Base item_to_cook, CookingMethodType cooking_method)
Definition: cooking.c:499
COOKING_EQUIPMENT_FRYINGPAN
COOKING_EQUIPMENT_FRYINGPAN
Definition: cooking.c:30
eAgents
eAgents
Definition: eagents.c:2
TIME
@ TIME
Definition: cooking.c:7
CargoBase
represents base for cargo storage for entities
Definition: cargo.c:6
NONE
@ NONE
Definition: cooking.c:3
GetItemTypeFromCargo
protected ItemBase GetItemTypeFromCargo(typename item_type, ItemBase cooking_equipment)
Cooking data.
Definition: cooking.c:389
FoodStageType
FoodStageType
Definition: foodstage.c:1
COOKING_EQUIPMENT_CAULDRON
COOKING_EQUIPMENT_CAULDRON
Definition: cooking.c:31
array< float >
TerminateCookingSounds
void TerminateCookingSounds(ItemBase pItem)
Definition: cooking.c:357
DecreaseCookedItemQuantity
protected void DecreaseCookedItemQuantity(notnull Edible_Base pItem, float pAmount=0.0)
Definition: cooking.c:534
COOKING_EQUIPMENT_POT
COOKING_EQUIPMENT_POT
Definition: cooking.c:29
COUNT
@ COUNT
Definition: cooking.c:9
CookingMethodType
CookingMethodType
Definition: cooking.c:1
SmokeItem
void SmokeItem(Edible_Base item_to_cook, float cook_time_inc)
Definition: cooking.c:324
Math
Definition: enmath.c:6
ProcessItemToCook
void ProcessItemToCook(notnull ItemBase pItem, ItemBase cookingEquip, Param2< CookingMethodType, float > pCookingMethod, out Param2< bool, bool > pStateFlags)
Definition: cooking.c:34
PARAM_BURN_DAMAGE_COEF
const float PARAM_BURN_DAMAGE_COEF
base value of fire consumption rate (how many base energy will be spent on each update)
Definition: fireplacebase.c:56
GetCookingMethodWithTimeOverride
protected Param2< CookingMethodType, float > GetCookingMethodWithTimeOverride(ItemBase cooking_equipment)
Definition: cooking.c:440
EntityAI
Definition: building.c:5
GetFoodOnStick
Edible_Base GetFoodOnStick(ItemBase stick_item)
Definition: cooking.c:486
Edible_Base
Definition: bearsteakmeat.c:1
CookWithEquipment
int CookWithEquipment(ItemBase cooking_equipment, float cooking_time_coef=1)
Definition: cooking.c:78
BOILING
@ BOILING
Definition: cooking.c:5