powergeneratorstatic.c 3.8 KB

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