powergeneratorstatic.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. class PowerGeneratorStatic : PowerGeneratorBase
  2. {
  3. const int SWITCH_COUNT = 6;
  4. const int LOW_ENERGY_PERCENT = 5;
  5. protected static ref set<PowerGeneratorStatic> m_PowerGenerators = new set<PowerGeneratorStatic>();
  6. protected Land_WarheadStorage_PowerStation m_Parent;
  7. protected int m_ParentID1;
  8. protected int m_ParentID2;
  9. protected int m_ParentID3;
  10. protected int m_ParentID4;
  11. protected int m_LeverStatesBits;
  12. void PowerGeneratorStatic()
  13. {
  14. RegisterPersistentObject(this);
  15. }
  16. void ~PowerGeneratorStatic()
  17. {
  18. UnregisterPersistentObject(this);
  19. }
  20. static void RegisterPersistentObject(PowerGeneratorStatic obj)
  21. {
  22. m_PowerGenerators.Insert(obj);
  23. }
  24. static void UnregisterPersistentObject(PowerGeneratorStatic obj)
  25. {
  26. if (!m_PowerGenerators)
  27. return;
  28. int index = m_PowerGenerators.Find(obj);
  29. if (index != -1)
  30. {
  31. m_PowerGenerators.Remove(index);
  32. }
  33. }
  34. static PowerGeneratorStatic GetClosestGenerator(vector position, float tolerance)
  35. {
  36. float toleranceSq = tolerance * tolerance;
  37. float smallestDist = float.MAX;
  38. PowerGeneratorStatic closest;
  39. foreach (PowerGeneratorStatic obj:m_PowerGenerators)
  40. {
  41. float distSq = vector.DistanceSq(position,obj.GetPosition());
  42. if (distSq < toleranceSq)
  43. {
  44. return obj;
  45. }
  46. }
  47. return null;
  48. }
  49. void SetParent(Land_WarheadStorage_PowerStation parent)
  50. {
  51. m_Parent = parent;
  52. }
  53. // States saved thorugh the generator entity since it doesnt work when done building side
  54. void StoreLeverStates(int leverStateBits)
  55. {
  56. m_LeverStatesBits = leverStateBits;
  57. }
  58. int GetStoredLeverBits()
  59. {
  60. return m_LeverStatesBits;
  61. }
  62. // Generator is working
  63. override void OnWorkStart()
  64. {
  65. super.OnWorkStart();
  66. if (m_Parent)
  67. {
  68. m_Parent.OnGeneratorStart();
  69. }
  70. if (!GetGame().IsServer())
  71. return;
  72. for (int i = 1; i <= SWITCH_COUNT; i++)
  73. {
  74. SetAnimationPhase("Switch" + i.ToString(), 1);
  75. }
  76. }
  77. // Turn off when this runs out of fuel
  78. override void OnWorkStop()
  79. {
  80. super.OnWorkStop();
  81. if (m_Parent)
  82. {
  83. m_Parent.OnGeneratorStop();
  84. }
  85. if (!GetGame().IsServer())
  86. return;
  87. for (int i = 1; i <= SWITCH_COUNT; i++)
  88. {
  89. SetAnimationPhase("Switch" + i.ToString(), 0);
  90. }
  91. }
  92. override void SetLowEnergyState(bool state)
  93. {
  94. super.SetLowEnergyState(state);
  95. if (GetGame().IsServer())
  96. {
  97. Land_WarheadStorage_Main bunker = m_Parent.GetClosestBunker();
  98. if (bunker)
  99. bunker.SetLowEnergyStateServer(state);
  100. }
  101. }
  102. override vector GetSmokeParticlePosition()
  103. {
  104. return "1.1 1.1 -1.1";
  105. }
  106. override vector GetSmokeParticleOrientation()
  107. {
  108. return "90 0 23";
  109. }
  110. // Checks sparkplug
  111. override bool HasSparkplug()
  112. {
  113. int slot = InventorySlots.GetSlotIdFromString("GlowPlug");
  114. EntityAI ent = GetInventory().FindAttachment(slot);
  115. return ent && !ent.IsRuined();
  116. }
  117. // Taking item into inventory
  118. override bool CanPutInCargo( EntityAI parent )
  119. {
  120. return false;
  121. }
  122. // Taking item into inventory
  123. override bool CanPutIntoHands(EntityAI parent)
  124. {
  125. return false;
  126. }
  127. override void OnStoreSave(ParamsWriteContext ctx)
  128. {
  129. super.OnStoreSave(ctx);
  130. if (m_Parent)
  131. m_Parent.GetPersistentID(m_ParentID1,m_ParentID2, m_ParentID3, m_ParentID4);
  132. ctx.Write(m_ParentID1);
  133. ctx.Write(m_ParentID2);
  134. ctx.Write(m_ParentID3);
  135. ctx.Write(m_ParentID4);
  136. ctx.Write(m_LeverStatesBits);
  137. }
  138. override bool OnStoreLoad( ParamsReadContext ctx, int version )
  139. {
  140. if ( !super.OnStoreLoad( ctx, version ) )
  141. return false;
  142. if ( !ctx.Read(m_ParentID1))
  143. {
  144. return false;
  145. }
  146. if ( !ctx.Read(m_ParentID2))
  147. {
  148. return false;
  149. }
  150. if ( !ctx.Read(m_ParentID3))
  151. {
  152. return false;
  153. }
  154. if ( !ctx.Read( m_ParentID4))
  155. {
  156. return false;
  157. }
  158. if (version >= 141 && !ctx.Read(m_LeverStatesBits) )
  159. return false;
  160. return true;
  161. }
  162. override void EEOnAfterLoad()
  163. {
  164. // Does this part actually do anything?
  165. Land_WarheadStorage_PowerStation powerStation = Land_WarheadStorage_PowerStation.Cast(GetGame().GetEntityByPersitentID(m_ParentID1, m_ParentID2, m_ParentID3, m_ParentID4));
  166. if (powerStation)
  167. {
  168. PowerGeneratorStatic otherGenerator = powerStation.GetPowerGenerator();
  169. if (otherGenerator)
  170. {
  171. otherGenerator.SetFuel(GetFuel());
  172. Delete();
  173. }
  174. }
  175. }
  176. override bool CanReleaseAttachment(EntityAI attachment)
  177. {
  178. if (!super.CanReleaseAttachment(attachment))
  179. return false;
  180. return !GetCompEM().IsWorking();
  181. }
  182. override bool IsTakeable()
  183. {
  184. return false;
  185. }
  186. override bool IsActionTargetVisible()
  187. {
  188. return true;
  189. }
  190. override bool DisableVicinityIcon()
  191. {
  192. return true;
  193. }
  194. // DEPRECATED
  195. void SetLowEnergyStateServer(bool state);
  196. }