portablegasstove.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. class PortableGasStove extends ItemBase
  2. {
  3. StoveLight m_Light;
  4. protected const string FLAME_BUTANE_ON = "dz\\gear\\cooking\\data\\flame_butane_ca.paa";
  5. protected const string FLAME_BUTANE_OFF = "";
  6. typename ATTACHMENT_COOKING_POT = Pot;
  7. typename ATTACHMENT_FRYING_PAN = FryingPan;
  8. typename ATTACHMENT_CAULDRON = Cauldron;
  9. //cooking
  10. protected const float PARAM_COOKING_TIME_INC_COEF = 0.5; //cooking time increase coeficient, can be used when balancing how fast a food can be cooked
  11. protected const float PARAM_COOKING_TARGET_TEMP = 400; //target temperature for general item heating
  12. private float m_TimeFactor;
  13. //
  14. ref Cooking m_CookingProcess;
  15. ItemBase m_CookingEquipment;
  16. //sound
  17. const string SOUND_BURNING = "portablegasstove_burn_SoundSet";
  18. const string SOUND_TURN_ON = "portablegasstove_turn_on_SoundSet";
  19. const string SOUND_TURN_OFF = "portablegasstove_turn_off_SoundSet";
  20. protected EffectSound m_SoundBurningLoop;
  21. protected EffectSound m_SoundTurnOn;
  22. protected EffectSound m_SoundTurnOff;
  23. protected ref UniversalTemperatureSource m_UTSource;
  24. protected ref UniversalTemperatureSourceSettings m_UTSSettings;
  25. protected ref UniversalTemperatureSourceLambdaConstant m_UTSLConst;
  26. //cooking equipment
  27. ItemBase GetCookingEquipment()
  28. {
  29. return m_CookingEquipment;
  30. }
  31. void SetCookingEquipment(ItemBase equipment)
  32. {
  33. m_CookingEquipment = equipment;
  34. }
  35. void ClearCookingEquipment(ItemBase pItem)
  36. {
  37. if (m_CookingProcess)
  38. {
  39. m_CookingProcess.TerminateCookingSounds(pItem);
  40. }
  41. SetCookingEquipment(null);
  42. }
  43. //Destroy
  44. void DestroyFireplace()
  45. {
  46. //delete object
  47. GetGame().ObjectDelete(this);
  48. }
  49. override void EEInit()
  50. {
  51. super.EEInit();
  52. if (m_CookingProcess == null)
  53. m_CookingProcess = new Cooking();
  54. m_CookingProcess.SetCookingUpdateTime(GetCompEM().GetUpdateInterval());
  55. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  56. {
  57. m_UTSSettings = new UniversalTemperatureSourceSettings();
  58. m_UTSSettings.m_ManualUpdate = true;
  59. m_UTSSettings.m_TemperatureItemCap = GameConstants.ITEM_TEMPERATURE_NEUTRAL_ZONE_MIDDLE;
  60. m_UTSSettings.m_TemperatureCap = 0;
  61. m_UTSSettings.m_RangeFull = 0;
  62. m_UTSSettings.m_RangeMax = 0;
  63. m_UTSSettings.m_IsWorldOverriden = false;
  64. m_UTSLConst = new UniversalTemperatureSourceLambdaConstant();
  65. m_UTSource = new UniversalTemperatureSource(this, m_UTSSettings, m_UTSLConst);
  66. }
  67. }
  68. //--- ATTACHMENTS
  69. override void EEItemAttached(EntityAI item, string slot_name)
  70. {
  71. super.EEItemAttached(item, slot_name);
  72. //cookware
  73. switch (item.Type())
  74. {
  75. case ATTACHMENT_CAULDRON:
  76. case ATTACHMENT_COOKING_POT:
  77. case ATTACHMENT_FRYING_PAN:
  78. SetCookingEquipment(ItemBase.Cast(item));
  79. RefreshFlameVisual(m_EM.IsSwitchedOn(), true);
  80. break;
  81. }
  82. }
  83. override void EEItemDetached(EntityAI item, string slot_name)
  84. {
  85. super.EEItemDetached(item, slot_name);
  86. //cookware
  87. switch (item.Type())
  88. {
  89. case ATTACHMENT_CAULDRON:
  90. case ATTACHMENT_COOKING_POT:
  91. case ATTACHMENT_FRYING_PAN:
  92. RemoveCookingAudioVisuals();
  93. //remove cooking equipment reference
  94. ClearCookingEquipment(ItemBase.Cast(item));
  95. RefreshFlameVisual(m_EM.IsSwitchedOn(), false);
  96. break;
  97. }
  98. }
  99. //--- POWER EVENTS
  100. override void OnSwitchOn()
  101. {
  102. super.OnSwitchOn();
  103. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  104. {
  105. m_UTSource.SetDefferedActive(true, 3.0);
  106. }
  107. //sound (client only)
  108. SoundTurnOn();
  109. }
  110. override void OnSwitchOff()
  111. {
  112. super.OnSwitchOff();
  113. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  114. {
  115. m_UTSource.SetDefferedActive(false, 5.0);
  116. }
  117. //sound (client only)
  118. SoundTurnOff();
  119. if (m_CookingProcess && GetCookingEquipment())
  120. {
  121. m_CookingProcess.TerminateCookingSounds(GetCookingEquipment());
  122. }
  123. }
  124. override void OnWorkStart()
  125. {
  126. super.OnWorkStart();
  127. #ifndef SERVER
  128. m_Light = StoveLight.Cast(ScriptedLightBase.CreateLight(StoveLight, "0 0 0"));
  129. m_Light.AttachOnMemoryPoint(this, "light");
  130. #endif
  131. //refresh visual
  132. RefreshFlameVisual(true, GetCookingEquipment() != null);
  133. //sound (client only)
  134. SoundBurningStart();
  135. }
  136. override void OnWorkStop()
  137. {
  138. #ifndef SERVER
  139. if (m_Light)
  140. {
  141. m_Light.FadeOut();
  142. }
  143. #endif
  144. //refresh visual
  145. RefreshFlameVisual(false, false);
  146. //stop steam particle
  147. RemoveCookingAudioVisuals();
  148. //sound (client only)
  149. SoundBurningStop();
  150. }
  151. //on update
  152. override void OnWork(float consumed_energy)
  153. {
  154. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  155. {
  156. m_UTSource.Update(m_UTSSettings, m_UTSLConst);
  157. }
  158. //manage cooking equipment
  159. ItemBase item = ItemBase.Cast(GetCookingEquipment());
  160. if (item && item.CanHaveTemperature())
  161. {
  162. if (GetGame() && GetGame().IsServer())
  163. {
  164. float cook_equip_temp = item.GetTemperature();
  165. float target; //makes sense here, since gas stove does not heat itself, and we need some target
  166. if (!GetCookingTargetTemperature(target))
  167. target = PARAM_COOKING_TARGET_TEMP; //fallback value
  168. float diff = target - cook_equip_temp;
  169. float heatPermCoef = item.GetHeatPermeabilityCoef();
  170. //heat only, no self-coolig like FireplaceBase
  171. if (diff > 0)
  172. item.SetTemperatureEx(new TemperatureDataInterpolated(target,ETemperatureAccessTypes.ACCESS_FIREPLACE,GetCompEM().GetUpdateInterval(),GameConstants.TEMP_COEF_GAS_STOVE,heatPermCoef));
  173. m_TimeFactor = consumed_energy;
  174. CookWithEquipment(); //heat children
  175. //! damaging of cookware
  176. GetCookingEquipment().DecreaseHealth(GameConstants.FIRE_ATTACHMENT_DAMAGE_PER_SECOND * GetCompEM().GetUpdateInterval(), false);
  177. }
  178. }
  179. }
  180. void CookWithEquipment()
  181. {
  182. if (m_CookingProcess == null)
  183. m_CookingProcess = new Cooking();
  184. m_CookingProcess.CookWithEquipment(GetCookingEquipment(), PARAM_COOKING_TIME_INC_COEF * m_TimeFactor);
  185. }
  186. override bool GetCookingTargetTemperature(out float temperature)
  187. {
  188. temperature = PARAM_COOKING_TARGET_TEMP;
  189. return true;
  190. }
  191. protected void RefreshFlameVisual(bool working = false, bool hasAttachment = false)
  192. {
  193. if (!working)
  194. {
  195. SetObjectTexture(0, FLAME_BUTANE_OFF);
  196. SetObjectTexture(1, FLAME_BUTANE_OFF);
  197. return;
  198. }
  199. if (!hasAttachment)
  200. {
  201. //! full size flame selection
  202. SetObjectTexture(0, FLAME_BUTANE_ON);
  203. SetObjectTexture(1, FLAME_BUTANE_OFF);
  204. }
  205. else
  206. {
  207. //! shrinked flame selection
  208. SetObjectTexture(0, FLAME_BUTANE_OFF);
  209. SetObjectTexture(1, FLAME_BUTANE_ON);
  210. }
  211. }
  212. //================================================================
  213. // PARTICLES
  214. //================================================================
  215. //cooking equipment steam
  216. protected void RemoveCookingAudioVisuals()
  217. {
  218. ItemBase cookEquipment = GetCookingEquipment();
  219. if (cookEquipment)
  220. {
  221. switch (cookEquipment.Type())
  222. {
  223. case ATTACHMENT_CAULDRON:
  224. case ATTACHMENT_COOKING_POT:
  225. Bottle_Base cookingPot = Bottle_Base.Cast(cookEquipment);
  226. cookingPot.RemoveAudioVisualsOnClient();
  227. break;
  228. case ATTACHMENT_FRYING_PAN:
  229. FryingPan fryingPan = FryingPan.Cast(cookEquipment);
  230. fryingPan.RemoveAudioVisualsOnClient();
  231. break;
  232. }
  233. }
  234. }
  235. //================================================================
  236. // SOUNDS
  237. //================================================================
  238. protected void SoundBurningStart()
  239. {
  240. PlaySoundSetLoop(m_SoundBurningLoop, SOUND_BURNING, 0.1, 0.3);
  241. }
  242. protected void SoundBurningStop()
  243. {
  244. StopSoundSet(m_SoundBurningLoop);
  245. }
  246. protected void SoundTurnOn()
  247. {
  248. PlaySoundSet(m_SoundTurnOn, SOUND_TURN_ON, 0.1, 0.1);
  249. }
  250. protected void SoundTurnOff()
  251. {
  252. PlaySoundSet(m_SoundTurnOff, SOUND_TURN_OFF, 0.1, 0.1);
  253. }
  254. //================================================================
  255. // CONDITIONS
  256. //================================================================
  257. override bool IsSelfAdjustingTemperature() //prevents CE temperature updates in vicinity, does not really have its own temperature
  258. {
  259. return GetCompEM().IsWorking();
  260. }
  261. //this into/outo parent.Cargo
  262. override bool CanPutInCargo(EntityAI parent)
  263. {
  264. if (!super.CanPutInCargo(parent))
  265. return false;
  266. if (GetCompEM().IsSwitchedOn())
  267. return false;
  268. //can 'parent' be attached to 'this' (->assumed smaller size than 'this')?
  269. if (parent)
  270. {
  271. int slotId;
  272. for (int i = 0; i < GetInventory().GetAttachmentSlotsCount(); i++)
  273. {
  274. slotId = GetInventory().GetAttachmentSlotId(i);
  275. if (parent.GetInventory().HasInventorySlot(slotId))
  276. {
  277. //Print("CanPutInCargo | parent " + parent + " matches in slot name: " + InventorySlots.GetSlotName(slotId) + " of " + this);
  278. return false;
  279. }
  280. }
  281. }
  282. return true;
  283. }
  284. override bool CanRemoveFromCargo(EntityAI parent)
  285. {
  286. return true;
  287. }
  288. override bool CanReceiveAttachment(EntityAI attachment, int slotId)
  289. {
  290. InventoryLocation loc = new InventoryLocation();
  291. EntityAI ent = this;
  292. EntityAI parent;
  293. while (ent)
  294. {
  295. if (ent.GetInventory().GetCurrentInventoryLocation(loc) && loc.IsValid())
  296. {
  297. if (loc.GetType() == InventoryLocationType.CARGO)
  298. {
  299. parent = ent.GetHierarchyParent();
  300. if (parent && parent.GetInventory().HasInventorySlot(slotId))
  301. {
  302. //Print("CanReceiveAttachment | parent " + parent + " matches in slot name: " + InventorySlots.GetSlotName(slotId) + " of " + this);
  303. return false;
  304. }
  305. }
  306. }
  307. ent = ent.GetHierarchyParent();
  308. }
  309. return super.CanReceiveAttachment(attachment, slotId);
  310. }
  311. override bool CanLoadAttachment(EntityAI attachment)
  312. {
  313. int slotId;
  314. for (int i = 0; i < attachment.GetInventory().GetSlotIdCount(); i++)
  315. {
  316. slotId = attachment.GetInventory().GetSlotId(i);
  317. if (GetInventory().HasAttachmentSlot(slotId))
  318. {
  319. InventoryLocation loc = new InventoryLocation();
  320. EntityAI ent = this;
  321. EntityAI parent;
  322. while (ent)
  323. {
  324. if (ent.GetInventory().GetCurrentInventoryLocation(loc) && loc.IsValid())
  325. {
  326. if (loc.GetType() == InventoryLocationType.CARGO)
  327. {
  328. parent = ent.GetHierarchyParent();
  329. if (parent.GetInventory().HasInventorySlot(slotId))
  330. {
  331. //Print("CanLoadAttachment | parent " + parent + " matches in slot name: " + InventorySlots.GetSlotName(slotId) + " of " + this);
  332. return false;
  333. }
  334. }
  335. }
  336. ent = ent.GetHierarchyParent();
  337. }
  338. }
  339. }
  340. return super.CanLoadAttachment(attachment);
  341. }
  342. //hands
  343. override bool CanPutIntoHands(EntityAI parent)
  344. {
  345. if (!super.CanPutIntoHands(parent))
  346. {
  347. return false;
  348. }
  349. return !GetCompEM().IsSwitchedOn();
  350. }
  351. //================================================================
  352. // ITEM-TO-ITEM FIRE DISTRIBUTION
  353. //================================================================
  354. override bool IsIgnited()
  355. {
  356. return GetCompEM().IsWorking();
  357. }
  358. override bool CanIgniteItem(EntityAI ignite_target = NULL)
  359. {
  360. return GetCompEM().IsWorking();
  361. }
  362. override void SetActions()
  363. {
  364. super.SetActions();
  365. AddAction(ActionLightItemOnFire);
  366. AddAction(ActionTurnOnWhileOnGround);
  367. AddAction(ActionTurnOffWhileOnGround);
  368. }
  369. //Debug menu Spawn Ground Special
  370. override void OnDebugSpawn()
  371. {
  372. EntityAI entity;
  373. if ( Class.CastTo(entity, this) )
  374. {
  375. GetInventory().CreateInInventory("LargeGasCanister");
  376. GetInventory().CreateInInventory("Pot");
  377. SpawnEntityOnGroundPos("WaterBottle", entity.GetPosition() + Vector(0.2, 0, 0));
  378. }
  379. }
  380. ///////////////////////////
  381. //DEPRECATED STUFF BELOW//
  382. /////////////////////////
  383. protected const float PARAM_COOKING_TEMP_THRESHOLD = 100; //!DEPRECATED
  384. protected const float PARAM_COOKING_EQUIP_MAX_TEMP = 400; //!DEPRECATED
  385. protected const float PARAM_COOKING_EQUIP_TEMP_INCREASE = 10; //!DEPRECATED
  386. /////////////////////////
  387. }