chemlight.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. class Chemlight_ColorBase : ItemBase
  2. {
  3. string m_DefaultMaterial;
  4. string m_GlowMaterial;
  5. ChemlightLight m_Light;
  6. private int m_Efficiency0To10; // Synchronized variable
  7. static private float m_EfficiencyDecayStart = 0.05; // At this % of maximum energy the output of the light starts to weaken.
  8. //! Returns efficiency. The value is synchronized from server to all clients and is accurate down to 0.1 units.
  9. float GetEfficiency0To1()
  10. {
  11. return m_Efficiency0To10 / 10;
  12. }
  13. //! Returns efficiency. The value is synchronized from server to all clients and is accurate down to 0.1 units.
  14. float GetEfficiencyDecayStart()
  15. {
  16. return m_EfficiencyDecayStart;
  17. }
  18. override void OnEnergyConsumed()
  19. {
  20. super.OnEnergyConsumed();
  21. if ( GetGame().IsServer() )
  22. {
  23. float energy_coef = GetCompEM().GetEnergy0To1();
  24. if ( energy_coef < m_EfficiencyDecayStart && m_EfficiencyDecayStart > 0 )
  25. {
  26. m_Efficiency0To10 = Math.Round( (energy_coef / m_EfficiencyDecayStart) * 10 );
  27. SetSynchDirty();
  28. }
  29. }
  30. }
  31. override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
  32. {
  33. super.EEHealthLevelChanged(oldLevel,newLevel,zone);
  34. SetObjectMaterial( 0, GetMaterialForDamageState(GetCompEM().IsWorking(),newLevel) );
  35. }
  36. void Chemlight_ColorBase()
  37. {
  38. //materials
  39. array<string> config_materials = GetHiddenSelectionsMaterials();
  40. if (config_materials.Count() == 2)
  41. {
  42. m_DefaultMaterial = config_materials[0];
  43. m_GlowMaterial = config_materials[1];
  44. }
  45. else
  46. {
  47. string error = "Error! Item " + GetType() + " must have 2 entries in config array hiddenSelectionsMaterials[]. One for the default state, the other one for the glowing state. Currently it has " + config_materials.Count() + ".";
  48. Error(error);
  49. }
  50. m_Efficiency0To10 = 10;
  51. RegisterNetSyncVariableInt("m_Efficiency0To10");
  52. }
  53. void CreateLight()
  54. {
  55. SetObjectMaterial( 0, GetMaterialForDamageState(true) ); // must be server side!
  56. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // client side
  57. {
  58. m_Light = ChemlightLight.Cast( ScriptedLightBase.CreateLight( ChemlightLight, "0 0 0") );
  59. m_Light.AttachOnMemoryPoint(this, "light");
  60. string type = GetType();
  61. switch( type )
  62. {
  63. case "Chemlight_White":
  64. m_Light.SetColorToWhite();
  65. break;
  66. case "Chemlight_Red":
  67. m_Light.SetColorToRed();
  68. break;
  69. case "Chemlight_Green":
  70. m_Light.SetColorToGreen();
  71. break;
  72. case "Chemlight_Blue":
  73. m_Light.SetColorToBlue();
  74. break;
  75. case "Chemlight_Yellow":
  76. m_Light.SetColorToYellow();
  77. break;
  78. default: { m_Light.SetColorToWhite(); };
  79. }
  80. }
  81. }
  82. override void OnWorkStart()
  83. {
  84. }
  85. // Inventory manipulation
  86. override void OnInventoryExit(Man player)
  87. {
  88. super.OnInventoryExit(player);
  89. StandUp();
  90. }
  91. void StandUp()
  92. {
  93. if ( GetGame().IsServer() && GetCompEM().IsWorking() )
  94. {
  95. vector ori_rotate = "0 0 0";
  96. SetOrientation(ori_rotate);
  97. }
  98. }
  99. override void OnWorkStop()
  100. {
  101. SetObjectMaterial( 0, GetMaterialForDamageState(false) );
  102. if (m_Light)
  103. {
  104. m_Light.FadeOut();
  105. }
  106. if ( GetGame().IsServer() )
  107. {
  108. //Safeguard if item is turned off by another event than running out of energy
  109. if (GetCompEM().GetEnergy() > 0)
  110. return;
  111. SetHealth(0);
  112. }
  113. }
  114. override void OnWork (float consumed_energy)
  115. {
  116. if (!m_Light)
  117. CreateLight();
  118. // Handle fade out of chemlight
  119. if (m_Light)
  120. {
  121. float efficiency = GetEfficiency0To1();
  122. if ( efficiency < 1 )
  123. {
  124. m_Light.SetIntensity( efficiency, GetCompEM().GetUpdateInterval() );
  125. }
  126. }
  127. }
  128. override void SetActions()
  129. {
  130. super.SetActions();
  131. AddAction(ActionTurnOnWhileInHands);
  132. }
  133. string GetMaterialForDamageState(bool glowing,int healthLevel = -1)
  134. {
  135. int currentHealthLevel;
  136. int suffixIndex;
  137. string base;
  138. if (healthLevel == -1)
  139. currentHealthLevel = GetHealthLevel();
  140. else
  141. currentHealthLevel = healthLevel;
  142. if (glowing)
  143. base = m_GlowMaterial;
  144. else
  145. base = m_DefaultMaterial;
  146. suffixIndex = base.IndexOf(".rvmat");
  147. if (suffixIndex == -1)
  148. {
  149. Error("Error - no valid rvmat found for chemlight");
  150. return "";
  151. }
  152. base = base.Substring(0,suffixIndex);
  153. if (currentHealthLevel == GameConstants.STATE_BADLY_DAMAGED || currentHealthLevel == GameConstants.STATE_DAMAGED)
  154. {
  155. base = base + "_damage";
  156. }
  157. else if (currentHealthLevel == GameConstants.STATE_RUINED)
  158. {
  159. base = base + "_destruct";
  160. }
  161. return base + ".rvmat";
  162. }
  163. };
  164. class Chemlight_White: Chemlight_ColorBase {};
  165. class Chemlight_Red: Chemlight_ColorBase {};
  166. class Chemlight_Green: Chemlight_ColorBase {};
  167. class Chemlight_Blue: Chemlight_ColorBase {};
  168. class Chemlight_Yellow: Chemlight_ColorBase {};