portablegasstove.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. if (item.IsCookware())
  74. {
  75. SetCookingEquipment(ItemBase.Cast(item));
  76. RefreshFlameVisual(m_EM.IsSwitchedOn(), true);
  77. }
  78. }
  79. override void EEItemDetached(EntityAI item, string slot_name)
  80. {
  81. super.EEItemDetached(item, slot_name);
  82. //cookware
  83. if (item.IsCookware())
  84. {
  85. //remove cooking equipment reference
  86. ClearCookingEquipment(ItemBase.Cast(item));
  87. RefreshFlameVisual(m_EM.IsSwitchedOn(), false);
  88. }
  89. //stop effects
  90. RemoveCookingAudioVisuals();
  91. }
  92. //--- POWER EVENTS
  93. override void OnSwitchOn()
  94. {
  95. super.OnSwitchOn();
  96. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  97. {
  98. m_UTSource.SetDefferedActive(true, 3.0);
  99. }
  100. //sound (client only)
  101. SoundTurnOn();
  102. }
  103. override void OnSwitchOff()
  104. {
  105. super.OnSwitchOff();
  106. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  107. {
  108. m_UTSource.SetDefferedActive(false, 5.0);
  109. }
  110. //sound (client only)
  111. SoundTurnOff();
  112. if (m_CookingProcess && GetCookingEquipment())
  113. {
  114. m_CookingProcess.TerminateCookingSounds(GetCookingEquipment());
  115. }
  116. }
  117. override void OnWorkStart()
  118. {
  119. super.OnWorkStart();
  120. #ifndef SERVER
  121. m_Light = StoveLight.Cast(ScriptedLightBase.CreateLight(StoveLight, "0 0 0"));
  122. m_Light.AttachOnMemoryPoint(this, "light");
  123. #endif
  124. //refresh visual
  125. RefreshFlameVisual(true, GetCookingEquipment() != null);
  126. //sound (client only)
  127. SoundBurningStart();
  128. }
  129. override void OnWorkStop()
  130. {
  131. #ifndef SERVER
  132. if (m_Light)
  133. {
  134. m_Light.FadeOut();
  135. }
  136. #endif
  137. //refresh visual
  138. RefreshFlameVisual(false, false);
  139. //stop effects
  140. RemoveCookingAudioVisuals();
  141. //sound (client only)
  142. SoundBurningStop();
  143. }
  144. //on update
  145. override void OnWork(float consumed_energy)
  146. {
  147. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  148. {
  149. m_UTSource.Update(m_UTSSettings, m_UTSLConst);
  150. }
  151. //manage cooking equipment
  152. ItemBase item = ItemBase.Cast(GetCookingEquipment());
  153. if (item && item.CanHaveTemperature())
  154. {
  155. if (GetGame() && GetGame().IsServer())
  156. {
  157. float cook_equip_temp = item.GetTemperature();
  158. float target; //makes sense here, since gas stove does not heat itself, and we need some target
  159. if (!GetCookingTargetTemperature(target))
  160. target = PARAM_COOKING_TARGET_TEMP; //fallback value
  161. float diff = target - cook_equip_temp;
  162. float heatPermCoef = item.GetHeatPermeabilityCoef();
  163. //heat only, no self-coolig like FireplaceBase
  164. if (diff > 0)
  165. item.SetTemperatureEx(new TemperatureDataInterpolated(target,ETemperatureAccessTypes.ACCESS_FIREPLACE,GetCompEM().GetUpdateInterval(),GameConstants.TEMP_COEF_GAS_STOVE,heatPermCoef));
  166. m_TimeFactor = consumed_energy;
  167. CookWithEquipment(); //heat children
  168. //! damaging of cookware
  169. GetCookingEquipment().DecreaseHealth(GameConstants.FIRE_ATTACHMENT_DAMAGE_PER_SECOND * GetCompEM().GetUpdateInterval(), false);
  170. }
  171. }
  172. }
  173. void CookWithEquipment()
  174. {
  175. if (m_CookingProcess == null)
  176. m_CookingProcess = new Cooking();
  177. m_CookingProcess.CookWithEquipment(GetCookingEquipment(), PARAM_COOKING_TIME_INC_COEF * m_TimeFactor);
  178. }
  179. override bool GetCookingTargetTemperature(out float temperature)
  180. {
  181. temperature = PARAM_COOKING_TARGET_TEMP;
  182. return true;
  183. }
  184. protected void RefreshFlameVisual(bool working = false, bool hasAttachment = false)
  185. {
  186. if (!working)
  187. {
  188. SetObjectTexture(0, FLAME_BUTANE_OFF);
  189. SetObjectTexture(1, FLAME_BUTANE_OFF);
  190. return;
  191. }
  192. if (!hasAttachment)
  193. {
  194. //! full size flame selection
  195. SetObjectTexture(0, FLAME_BUTANE_ON);
  196. SetObjectTexture(1, FLAME_BUTANE_OFF);
  197. }
  198. else
  199. {
  200. //! shrinked flame selection
  201. SetObjectTexture(0, FLAME_BUTANE_OFF);
  202. SetObjectTexture(1, FLAME_BUTANE_ON);
  203. }
  204. }
  205. //================================================================
  206. // PARTICLES
  207. //================================================================
  208. //cooking equipment steam
  209. protected void RemoveCookingAudioVisuals()
  210. {
  211. ItemBase cookEquipment;
  212. if (Class.CastTo(cookEquipment,GetCookingEquipment()) && (cookEquipment.IsCookware() || cookEquipment.IsLiquidContainer())) //also stops boiling effect on bottles
  213. cookEquipment.RemoveAudioVisualsOnClient();
  214. }
  215. //================================================================
  216. // SOUNDS
  217. //================================================================
  218. protected void SoundBurningStart()
  219. {
  220. PlaySoundSetLoop(m_SoundBurningLoop, SOUND_BURNING, 0.1, 0.3);
  221. }
  222. protected void SoundBurningStop()
  223. {
  224. StopSoundSet(m_SoundBurningLoop);
  225. }
  226. protected void SoundTurnOn()
  227. {
  228. PlaySoundSet(m_SoundTurnOn, SOUND_TURN_ON, 0.1, 0.1);
  229. }
  230. protected void SoundTurnOff()
  231. {
  232. PlaySoundSet(m_SoundTurnOff, SOUND_TURN_OFF, 0.1, 0.1);
  233. }
  234. //================================================================
  235. // CONDITIONS
  236. //================================================================
  237. override bool IsSelfAdjustingTemperature() //prevents CE temperature updates in vicinity, does not really have its own temperature
  238. {
  239. return GetCompEM().IsWorking();
  240. }
  241. //this into/outo parent.Cargo
  242. override bool CanPutInCargo(EntityAI parent)
  243. {
  244. if (!super.CanPutInCargo(parent))
  245. return false;
  246. if (GetCompEM().IsSwitchedOn())
  247. return false;
  248. //can 'parent' be attached to 'this' (->assumed smaller size than 'this')?
  249. if (parent)
  250. {
  251. int slotId;
  252. for (int i = 0; i < GetInventory().GetAttachmentSlotsCount(); i++)
  253. {
  254. slotId = GetInventory().GetAttachmentSlotId(i);
  255. if (parent.GetInventory().HasInventorySlot(slotId))
  256. {
  257. //Print("CanPutInCargo | parent " + parent + " matches in slot name: " + InventorySlots.GetSlotName(slotId) + " of " + this);
  258. return false;
  259. }
  260. }
  261. }
  262. return true;
  263. }
  264. override bool CanRemoveFromCargo(EntityAI parent)
  265. {
  266. return true;
  267. }
  268. override bool CanReceiveAttachment(EntityAI attachment, int slotId)
  269. {
  270. InventoryLocation loc = new InventoryLocation();
  271. EntityAI ent = this;
  272. EntityAI parent;
  273. while (ent)
  274. {
  275. if (ent.GetInventory().GetCurrentInventoryLocation(loc) && loc.IsValid())
  276. {
  277. if (loc.GetType() == InventoryLocationType.CARGO)
  278. {
  279. parent = ent.GetHierarchyParent();
  280. if (parent && parent.GetInventory().HasInventorySlot(slotId))
  281. {
  282. //Print("CanReceiveAttachment | parent " + parent + " matches in slot name: " + InventorySlots.GetSlotName(slotId) + " of " + this);
  283. return false;
  284. }
  285. }
  286. }
  287. ent = ent.GetHierarchyParent();
  288. }
  289. return super.CanReceiveAttachment(attachment, slotId);
  290. }
  291. override bool CanLoadAttachment(EntityAI attachment)
  292. {
  293. int slotId;
  294. for (int i = 0; i < attachment.GetInventory().GetSlotIdCount(); i++)
  295. {
  296. slotId = attachment.GetInventory().GetSlotId(i);
  297. if (GetInventory().HasAttachmentSlot(slotId))
  298. {
  299. InventoryLocation loc = new InventoryLocation();
  300. EntityAI ent = this;
  301. EntityAI parent;
  302. while (ent)
  303. {
  304. if (ent.GetInventory().GetCurrentInventoryLocation(loc) && loc.IsValid())
  305. {
  306. if (loc.GetType() == InventoryLocationType.CARGO)
  307. {
  308. parent = ent.GetHierarchyParent();
  309. if (parent.GetInventory().HasInventorySlot(slotId))
  310. {
  311. //Print("CanLoadAttachment | parent " + parent + " matches in slot name: " + InventorySlots.GetSlotName(slotId) + " of " + this);
  312. return false;
  313. }
  314. }
  315. }
  316. ent = ent.GetHierarchyParent();
  317. }
  318. }
  319. }
  320. return super.CanLoadAttachment(attachment);
  321. }
  322. //hands
  323. override bool CanPutIntoHands(EntityAI parent)
  324. {
  325. if (!super.CanPutIntoHands(parent))
  326. {
  327. return false;
  328. }
  329. return !GetCompEM().IsSwitchedOn();
  330. }
  331. //================================================================
  332. // ITEM-TO-ITEM FIRE DISTRIBUTION
  333. //================================================================
  334. override bool IsIgnited()
  335. {
  336. return GetCompEM().IsWorking();
  337. }
  338. override bool CanIgniteItem(EntityAI ignite_target = NULL)
  339. {
  340. return GetCompEM().IsWorking();
  341. }
  342. override void SetActions()
  343. {
  344. super.SetActions();
  345. AddAction(ActionLightItemOnFire);
  346. AddAction(ActionTurnOnWhileOnGround);
  347. AddAction(ActionTurnOffWhileOnGround);
  348. }
  349. //Debug menu Spawn Ground Special
  350. override void OnDebugSpawn()
  351. {
  352. EntityAI entity;
  353. if ( Class.CastTo(entity, this) )
  354. {
  355. GetInventory().CreateInInventory("LargeGasCanister");
  356. GetInventory().CreateInInventory("Pot");
  357. SpawnEntityOnGroundPos("WaterBottle", entity.GetPosition() + Vector(0.2, 0, 0));
  358. }
  359. }
  360. ///////////////////////////
  361. //DEPRECATED STUFF BELOW//
  362. /////////////////////////
  363. protected const float PARAM_COOKING_TEMP_THRESHOLD = 100; //!DEPRECATED
  364. protected const float PARAM_COOKING_EQUIP_MAX_TEMP = 400; //!DEPRECATED
  365. protected const float PARAM_COOKING_EQUIP_TEMP_INCREASE = 10; //!DEPRECATED
  366. /////////////////////////
  367. }