roadflare.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. Author: Boris Vacula
  3. Description: The flare has 3 burning states during which it uses different particle effects while illumnatingthe environment. After it burns up, it still releases smoke for a while.
  4. When the flare is dropped while its burning, it is stood up on its stands. This makes the shadows, illumination and particles stand out better.
  5. */
  6. enum RoadflareBurningState
  7. {
  8. NOT_BURNING,
  9. INITIAL_BURN,
  10. MAIN_BURN,
  11. FINAL_BURN,
  12. SMOKE_ONLY
  13. };
  14. enum RoadflareModelStates
  15. {
  16. DEFAULT,
  17. UNCAPPED_UNIGNITED,
  18. UNCAPPED_IGNITED,
  19. UNCAPPED_BURNED_OUT
  20. };
  21. class Roadflare : ItemBase
  22. {
  23. // Burning
  24. static protected const int INITIAL_BURNING_STATE_TIME = 5;
  25. static protected const int FINAL_BURNING_STATE_TIME = 60;
  26. static protected vector m_FlameLocalPos = "0 0.285 0";
  27. private int m_BurningState = RoadflareBurningState.NOT_BURNING;
  28. // Light
  29. RoadflareLight m_Light;
  30. // Particles
  31. protected Particle m_ParInitialFire;
  32. protected Particle m_ParMainFire;
  33. protected Particle m_ParFinalFire;
  34. protected Particle m_ParJustSmoke;
  35. static protected int PARTICLE_INIT_FIRE = ParticleList.ROADFLARE_BURNING_INIT;
  36. static protected int PARTICLE_MAIN_FIRE = ParticleList.ROADFLARE_BURNING_MAIN;
  37. static protected int PARTICLE_FINAL_FIRE = ParticleList.ROADFLARE_BURNING_ENDING;
  38. static protected int PARTICLE_FINAL_SMOKE = ParticleList.ROADFLARE_BURNING_SMOKE;
  39. // Sounds
  40. protected EffectSound m_BurningSound;
  41. protected EffectSound m_IgniteSound;
  42. static protected const string BURNING_SOUND = "roadflareLoop_SoundSet";
  43. static protected const string IGNITE_SOUND = "roadflareTurnOn_SoundSet";
  44. static protected const int BURNING_NOISE_RANGE = 30;
  45. // Timers
  46. ref Timer m_FinalSmokeTimer;
  47. // Ignition states
  48. private int m_ModelState = RoadflareModelStates.DEFAULT;
  49. // Selections
  50. static const string STANDS_FOLDED = "Sticks_Flare_Folded";
  51. static const string STANDS_UNFOLDED = "Sticks_Flare_Unfolded";
  52. static const string FLARE_CAP = "Flare_cap";
  53. static const string UNIGNITED_TIP = "Pristine";
  54. static const string IGNITED_TIP = "Burning";
  55. static const string EXTINGUISHED_TIP = "Burned_out";
  56. static const int SELECTION_Burning = 0;
  57. static const int SELECTION_Burned_out = 1;
  58. static const int SELECTION_Pristine = 2;
  59. static const int SELECTION_All = 3;
  60. static const string DEFAULT_TEXTURE = "dz\\gear\\consumables\\data\\road_flare_co.paa";
  61. static const string BURNING_TEXTURE = "dz\\gear\\consumables\\data\\road_flare_e_co.paa";
  62. static const string DEFAULT_MATERIAL = "dz\\gear\\consumables\\data\\road_flare.rvmat";
  63. static const string BURNING_MATERIAL = "dz\\gear\\consumables\\data\\road_flare_on.rvmat";
  64. // Noise
  65. ref NoiseParams m_NoisePar;
  66. private float m_NoiseTimer;
  67. void Roadflare()
  68. {
  69. RegisterNetSyncVariableInt("m_BurningState");
  70. }
  71. // Use RoadflareModelStates enum
  72. void SetModelState(int enum_state)
  73. {
  74. m_ModelState = enum_state;
  75. UpdateModelSelections();
  76. }
  77. override void EEDelete(EntityAI parent)
  78. {
  79. super.EEDelete(parent);
  80. if ( GetGame() )
  81. {
  82. SEffectManager.DestroyEffect( m_BurningSound );
  83. SEffectManager.DestroyEffect( m_IgniteSound );
  84. delete m_FinalSmokeTimer;
  85. }
  86. DestroyAllParticles();
  87. if (m_Light)
  88. m_Light.FadeOut(0);
  89. }
  90. override void OnStoreSave(ParamsWriteContext ctx)
  91. {
  92. super.OnStoreSave(ctx);
  93. ctx.Write( m_ModelState );
  94. }
  95. override bool OnStoreLoad(ParamsReadContext ctx, int version)
  96. {
  97. if ( !super.OnStoreLoad(ctx, version) )
  98. return false;
  99. int state;
  100. if ( !ctx.Read(state) )
  101. state = RoadflareModelStates.DEFAULT;
  102. SetModelState(state);
  103. return true;
  104. }
  105. void UpdateModelSelections()
  106. {
  107. switch (m_ModelState)
  108. {
  109. case RoadflareModelStates.DEFAULT:
  110. ShowSelection(FLARE_CAP);
  111. ShowSelection(UNIGNITED_TIP);
  112. HideSelection(IGNITED_TIP);
  113. HideSelection(EXTINGUISHED_TIP);
  114. SetObjectTexture(SELECTION_Pristine, DEFAULT_TEXTURE);
  115. SetObjectMaterial(SELECTION_Pristine, DEFAULT_MATERIAL);
  116. break;
  117. case RoadflareModelStates.UNCAPPED_UNIGNITED:
  118. ShowSelection(UNIGNITED_TIP);
  119. HideSelection(FLARE_CAP);
  120. HideSelection(IGNITED_TIP);
  121. HideSelection(EXTINGUISHED_TIP);
  122. SetObjectTexture(SELECTION_Pristine, DEFAULT_TEXTURE);
  123. SetObjectMaterial(SELECTION_Pristine, DEFAULT_MATERIAL);
  124. break;
  125. case RoadflareModelStates.UNCAPPED_IGNITED:
  126. ShowSelection(IGNITED_TIP);
  127. HideSelection(UNIGNITED_TIP);
  128. HideSelection(FLARE_CAP);
  129. HideSelection(EXTINGUISHED_TIP);
  130. // No texture/material change here because the model already contains them
  131. break;
  132. case RoadflareModelStates.UNCAPPED_BURNED_OUT:
  133. ShowSelection(EXTINGUISHED_TIP);
  134. HideSelection(UNIGNITED_TIP);
  135. HideSelection(FLARE_CAP);
  136. HideSelection(IGNITED_TIP);
  137. SetObjectTexture(SELECTION_Burned_out, DEFAULT_TEXTURE);
  138. SetObjectMaterial(SELECTION_Burned_out, DEFAULT_MATERIAL);
  139. break;
  140. }
  141. }
  142. // When the flare starts burning
  143. override void OnWorkStart()
  144. {
  145. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() )
  146. {
  147. PlaySoundSetLoop( m_BurningSound, BURNING_SOUND, 0.5, 0 );
  148. PlaySoundSet( m_IgniteSound, IGNITE_SOUND, 0, 0 );
  149. m_Light = RoadflareLight.Cast( ScriptedLightBase.CreateLight( RoadflareLight, Vector(0,0,0) ) );
  150. m_Light.AttachOnMemoryPoint( this, m_Light.m_MemoryPoint );
  151. }
  152. if ( GetGame().IsServer() )
  153. {
  154. /*m_NoisePar = new NoiseParams();
  155. m_NoisePar.LoadFromPath("cfgVehicles Roadflare NoiseRoadFlare");
  156. if ( GetGame().GetWorld().IsNight() )
  157. {
  158. NoiseSystem noise = GetGame().GetNoiseSystem();
  159. if ( noise )
  160. {
  161. noise.AddNoisePos( this, GetPosition(), m_NoisePar, NoiseAIEvaluate.GetNoiseReduction(GetGame().GetWeather()) );
  162. }
  163. }*/
  164. }
  165. SetBurningState(RoadflareBurningState.INITIAL_BURN);
  166. SetModelState( RoadflareModelStates.UNCAPPED_IGNITED );
  167. }
  168. // Insert RoadflareBurningState enum index as parameter
  169. void SetBurningState(int state_number)
  170. {
  171. m_BurningState = state_number;
  172. }
  173. // Insert RoadflareBurningState enum index as parameter. Activates server -> client synchronization
  174. void SetBurningStateSynchronized(int state_number)
  175. {
  176. if ( GetGame().IsServer() )
  177. {
  178. m_BurningState = state_number;
  179. SetSynchDirty();
  180. }
  181. }
  182. // Every second of the flare burning
  183. override void OnWork(float consumed_energy)
  184. {
  185. if ( GetGame().IsServer() )
  186. {
  187. float burning_time = GetCompEM().GetEnergyMax() - GetCompEM().GetEnergy();
  188. // Update burning states
  189. if (m_BurningState == RoadflareBurningState.INITIAL_BURN)
  190. {
  191. if (burning_time >= INITIAL_BURNING_STATE_TIME)
  192. {
  193. SetBurningStateSynchronized(RoadflareBurningState.MAIN_BURN);
  194. }
  195. }
  196. else if ( m_BurningState == RoadflareBurningState.MAIN_BURN )
  197. {
  198. /*if ( GetGame().GetWorld().IsNight() )
  199. {
  200. NoiseSystem noise = GetGame().GetNoiseSystem();
  201. if ( noise )
  202. {
  203. noise.AddNoisePos( this, GetPosition(), m_NoisePar, NoiseAIEvaluate.GetNoiseReduction(GetGame().GetWeather()));
  204. }
  205. }*/
  206. if ( burning_time >= GetCompEM().GetEnergyMax() - FINAL_BURNING_STATE_TIME )
  207. {
  208. SetBurningStateSynchronized(RoadflareBurningState.FINAL_BURN);
  209. }
  210. }
  211. // Burn containers of this roadflare. This might be removed if lit roadflare can't be put into inventory.
  212. /*EntityAI container_EAI = GetHierarchyParent();
  213. if ( container_EAI && container_EAI.IsInherited(ItemBase) && !container_EAI.IsInherited(TripwireTrap) )
  214. {
  215. ItemBase container_IB = ItemBase.Cast( container_EAI );
  216. int c_size = container_IB.GetItemSize();
  217. if (c_size == 0)
  218. c_size = 1;
  219. container_IB.AddHealth("","",-10/c_size);
  220. }*/
  221. }
  222. UpdateActiveParticles();
  223. }
  224. // When the flare stops burning
  225. override void OnWorkStop()
  226. {
  227. if ( GetGame().IsMissionMainMenu() ) // This is singleplayer main menu so no synchronization here!
  228. {
  229. SetBurningState(RoadflareBurningState.NOT_BURNING);
  230. UpdateActiveParticles();
  231. }
  232. else
  233. {
  234. if ( GetGame().IsServer() )
  235. {
  236. //Safeguard if item is turned off by another event than running out of energy
  237. if (GetCompEM().GetEnergy() > 0)
  238. {
  239. if (m_Light)
  240. {
  241. m_Light.FadeOut();
  242. }
  243. SetBurningState(RoadflareBurningState.NOT_BURNING);
  244. return;
  245. }
  246. SetBurningStateSynchronized(RoadflareBurningState.SMOKE_ONLY);
  247. SetHealth("","",0);
  248. }
  249. UpdateActiveParticles();
  250. m_FinalSmokeTimer = new Timer( CALL_CATEGORY_SYSTEM );
  251. m_FinalSmokeTimer.Run(60, this, "StopSmoking", NULL, false);
  252. }
  253. if ( m_BurningSound )
  254. StopSoundSet( m_BurningSound );
  255. if (m_Light)
  256. m_Light.FadeOut();
  257. SetModelState( RoadflareModelStates.UNCAPPED_BURNED_OUT );
  258. }
  259. // Updates all (in)active particles
  260. void UpdateActiveParticles()
  261. {
  262. if ( GetGame().IsDedicatedServer() )
  263. return;
  264. switch (m_BurningState)
  265. {
  266. case RoadflareBurningState.NOT_BURNING:
  267. DestroyAllParticles();
  268. break;
  269. case RoadflareBurningState.INITIAL_BURN:
  270. if (!m_ParInitialFire)
  271. {
  272. DestroyAllParticles();
  273. m_ParInitialFire = ParticleManager.GetInstance().PlayOnObject( PARTICLE_INIT_FIRE, this, m_FlameLocalPos);
  274. m_ParInitialFire.SetWiggle( 10, 0.3 );
  275. }
  276. break;
  277. case RoadflareBurningState.MAIN_BURN:
  278. if (!m_ParMainFire)
  279. {
  280. m_ParMainFire = ParticleManager.GetInstance().PlayOnObject( PARTICLE_MAIN_FIRE, this, m_FlameLocalPos);
  281. m_ParMainFire.SetWiggle( 7, 0.3 );
  282. }
  283. DestroyParticleEx(m_ParInitialFire);
  284. break;
  285. case RoadflareBurningState.FINAL_BURN:
  286. if (!m_ParFinalFire)
  287. {
  288. DestroyAllParticles();
  289. m_ParFinalFire = ParticleManager.GetInstance().PlayOnObject( PARTICLE_FINAL_FIRE, this, m_FlameLocalPos);
  290. m_ParFinalFire.SetWiggle( 4, 0.3 );
  291. }
  292. break;
  293. case RoadflareBurningState.SMOKE_ONLY:
  294. if (!m_ParJustSmoke)
  295. {
  296. DestroyAllParticles();
  297. m_ParJustSmoke = ParticleManager.GetInstance().PlayOnObject( PARTICLE_FINAL_SMOKE, this, m_FlameLocalPos);
  298. m_ParJustSmoke.SetWiggle( 2, 0.3 );
  299. }
  300. break;
  301. }
  302. }
  303. // Destroys the given particle
  304. void DestroyParticle( Particle p )
  305. {
  306. if (p)
  307. {
  308. p.SetWiggle(0,0);
  309. p.Stop();
  310. }
  311. }
  312. void DestroyParticleEx( out Particle p )
  313. {
  314. DestroyParticle(p);
  315. p = null;
  316. }
  317. // Destroys all particles
  318. void DestroyAllParticles()
  319. {
  320. DestroyParticleEx(m_ParInitialFire);
  321. DestroyParticleEx(m_ParMainFire);
  322. DestroyParticleEx(m_ParFinalFire);
  323. DestroyParticleEx(m_ParJustSmoke);
  324. }
  325. // Stop releasing final smoke
  326. void StopSmoking()
  327. {
  328. SetBurningStateSynchronized(RoadflareBurningState.NOT_BURNING);
  329. UpdateActiveParticles();
  330. }
  331. // Inventory manipulation
  332. override void OnInventoryExit(Man player)
  333. {
  334. super.OnInventoryExit(player);
  335. if (m_BurningState != RoadflareBurningState.NOT_BURNING)
  336. {
  337. HideSelection(STANDS_FOLDED);
  338. ShowSelection(STANDS_UNFOLDED);
  339. if (player)
  340. {
  341. vector ori_rotate = player.GetOrientation();
  342. ori_rotate = ori_rotate + Vector(180, 32, 0);
  343. SetOrientation(ori_rotate);
  344. }
  345. }
  346. }
  347. override bool CanPutInCargo( EntityAI parent )
  348. {
  349. if ( !super.CanPutInCargo(parent) )
  350. return false;
  351. if ( parent && parent.IsInherited( FireplaceBase ) )
  352. return true;
  353. if ( m_BurningState != RoadflareBurningState.NOT_BURNING )
  354. return false;
  355. return true;
  356. }
  357. override void OnInventoryEnter(Man player)
  358. {
  359. super.OnInventoryEnter(player);
  360. HideSelection(STANDS_UNFOLDED);
  361. ShowSelection(STANDS_FOLDED);
  362. }
  363. override void OnActivatedByItem(notnull ItemBase item)
  364. {
  365. if (item.IsInherited(TripwireTrap))
  366. {
  367. GetCompEM().SwitchOn();
  368. }
  369. }
  370. override bool CanIgniteItem(EntityAI ignite_target = NULL)
  371. {
  372. return GetCompEM().IsWorking();
  373. }
  374. override void OnVariablesSynchronized()
  375. {
  376. super.OnVariablesSynchronized();
  377. UpdateActiveParticles();
  378. }
  379. override void SetActions()
  380. {
  381. super.SetActions();
  382. AddAction(ActionAttach);
  383. AddAction(ActionDetach);
  384. AddAction(ActionLightItemOnFire);
  385. AddAction(ActionTurnOnWhileInHands);
  386. }
  387. };