batterycharger.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. class BatteryCharger extends ItemBase
  2. {
  3. // Model selections
  4. static protected const string SEL_CLIPS_CAR = "clips_car_battery";
  5. static protected const string SEL_CLIPS_TRUCK = "clips_truck_battery";
  6. static protected const string SEL_CLIPS_DETACHED = "clips_detached";
  7. static protected const string SEL_CLIPS_FOLDED = "clips_folded";
  8. static protected const string SEL_SWITCH_ON = "switch_on";
  9. static protected const string SEL_SWITCH_OFF = "switch_off";
  10. static protected const string SEL_CORD_PLUGGED = "cord_plugged";
  11. static protected const string SEL_CORD_FOLDED = "cord_folded";
  12. static protected const string SEL_LIGHT_STATE_1 = "light_stand_by";
  13. static protected const string SEL_LIGHT_STATE_2 = "light_charging";
  14. static protected const string SEL_LIGHT_STATE_3 = "light_charged";
  15. // glow materials
  16. static protected const string RED_LIGHT_GLOW = "dz\\gear\\camping\\data\\battery_charger_light_r.rvmat";
  17. static protected const string GREEN_LIGHT_GLOW = "dz\\gear\\camping\\data\\battery_charger_light_g.rvmat";
  18. static protected const string YELLOW_LIGHT_GLOW = "dz\\gear\\camping\\data\\battery_charger_light_y.rvmat";
  19. static protected const string SWITCH_LIGHT_GLOW = "dz\\gear\\camping\\data\\battery_charger_light_switch_on.rvmat";
  20. static protected const string DEFAULT_MATERIAL = "dz\\gear\\camping\\data\\battery_charger.rvmat";
  21. protected const string ATTACHED_CLIPS_STATES[] = {SEL_CLIPS_CAR, SEL_CLIPS_TRUCK}; // TO DO: If it's required by design, add helicopter battery here and register its selection names.
  22. protected const int ATTACHED_CLIPS_STATES_COUNT = 2; // Reffers to this ^ array
  23. int m_BatteryEnergy0To100;
  24. protected float m_ChargeEnergyPerSecond;
  25. static protected float m_BlinkingStatusLightInterval = 0.4; // How often the lights blink
  26. ref Timer m_UpdateStatusLightsTimer;
  27. protected bool m_BlinkingStatusLightIsOn = false; // Status of one blinking light
  28. void BatteryCharger()
  29. {
  30. m_ChargeEnergyPerSecond = GetGame().ConfigGetFloat ("CfgVehicles " + GetType() + " ChargeEnergyPerSecond");
  31. m_UpdateStatusLightsTimer = new Timer( CALL_CATEGORY_SYSTEM );
  32. SwitchLightOff();
  33. RegisterNetSyncVariableInt("m_BatteryEnergy0To100");
  34. }
  35. override bool IsElectricAppliance()
  36. {
  37. return true;
  38. }
  39. override void OnWork( float consumed_energy )
  40. {
  41. // Charging functionality
  42. ItemBase battery = ItemBase.Cast( GetCompEM().GetPluggedDevice() );
  43. if ( battery )
  44. {
  45. if ( GetGame().IsServer() )
  46. {
  47. float battery_capacity = battery.GetCompEM().GetEnergyMax();
  48. if ( battery.GetCompEM().GetEnergy() < battery_capacity )
  49. {
  50. float charger_health = GetHealth("", "");
  51. float energy_add = m_ChargeEnergyPerSecond * ( consumed_energy / GetCompEM().GetEnergyUsage() );
  52. #ifdef DIAG_DEVELOPER
  53. if (FeatureTimeAccel.GetFeatureTimeAccelEnabled(ETimeAccelCategories.ENERGY_RECHARGE))
  54. {
  55. float timeAccel = FeatureTimeAccel.GetFeatureTimeAccelValue();
  56. energy_add *= timeAccel;
  57. }
  58. #endif
  59. if ( GetCompEM().ConsumeEnergy(energy_add) ) // consumes energy from the power source
  60. {
  61. // There is enough of energy to use
  62. energy_add = energy_add * ( 0.5 + charger_health*0.005 ); // Damaged charger works less efficiently - 50% damage causes 75% efficiency
  63. }
  64. else
  65. {
  66. // There is NOT enough of energy to use
  67. energy_add = 0;
  68. }
  69. battery.GetCompEM().AddEnergy( energy_add );
  70. }
  71. else
  72. {
  73. battery.GetCompEM().SetEnergy( battery_capacity );
  74. }
  75. m_BatteryEnergy0To100 = battery.GetCompEM().GetEnergy0To100();
  76. SetSynchDirty();
  77. }
  78. }
  79. }
  80. override void OnWorkStart()
  81. {
  82. if ( GetGame().IsClient() || !GetGame().IsMultiplayer() )
  83. {
  84. UpdateStatusLights();
  85. m_UpdateStatusLightsTimer.Run( m_BlinkingStatusLightInterval/2 , this, "UpdateStatusLights", NULL, true);
  86. }
  87. }
  88. override void OnWorkStop()
  89. {
  90. if ( GetGame().IsClient() || !GetGame().IsMultiplayer() )
  91. {
  92. UpdateStatusLights();
  93. m_UpdateStatusLightsTimer.Stop();
  94. }
  95. }
  96. void UpdateStatusLights()
  97. {
  98. if ( GetGame().IsClient() || !GetGame().IsMultiplayer() )
  99. {
  100. if (GetCompEM().IsWorking())
  101. {
  102. SwitchLightOn();
  103. ItemBase battery = ItemBase.Cast( GetCompEM().GetPluggedDevice() );
  104. if (battery)
  105. {
  106. RedLightOff();
  107. if (m_BatteryEnergy0To100 <= 33)
  108. {
  109. // Less than 1/3 charged, yellow status light must repeatedly blink
  110. if (m_BlinkingStatusLightIsOn)
  111. YellowLightOn();
  112. else
  113. YellowLightOff();
  114. m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
  115. }
  116. else if (m_BatteryEnergy0To100 > 33 && m_BatteryEnergy0To100 <= 66)
  117. {
  118. // Less than 2/3 charged, yellow status light must glow
  119. YellowLightOn();
  120. }
  121. else if (m_BatteryEnergy0To100 > 66 && m_BatteryEnergy0To100 < 100)
  122. {
  123. // Less than 3/3 charged, yellow status light must glow, green light must blink
  124. YellowLightOn();
  125. if (m_BlinkingStatusLightIsOn)
  126. GreenLightOn();
  127. else
  128. GreenLightOff();
  129. m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
  130. }
  131. else if (m_BatteryEnergy0To100 >= 100)
  132. {
  133. // Fully charged, green light must glow
  134. YellowLightOff();
  135. GreenLightOn();
  136. }
  137. }
  138. else
  139. {
  140. if (m_BlinkingStatusLightIsOn)
  141. RedLightOn();
  142. else
  143. RedLightOff();
  144. m_BlinkingStatusLightIsOn = !m_BlinkingStatusLightIsOn;
  145. GreenLightOff();
  146. YellowLightOff();
  147. }
  148. }
  149. else
  150. {
  151. SwitchLightOff();
  152. GreenLightOff();
  153. RedLightOff();
  154. YellowLightOff();
  155. }
  156. }
  157. }
  158. override bool CanPutInCargo( EntityAI parent )
  159. {
  160. if( !super.CanPutInCargo(parent) ) {return false;}
  161. // No "Take" action if the item is connected
  162. if ( !GetCompEM().IsPlugged() && !GetCompEM().GetPluggedDevice() )
  163. {
  164. return true;
  165. }
  166. return false;
  167. }
  168. override bool CanPutIntoHands( EntityAI parent )
  169. {
  170. if( !super.CanPutIntoHands( parent ) )
  171. {
  172. return false;
  173. }
  174. // No "Take into hands" action if the item is connected
  175. if ( !GetCompEM().IsPlugged() && !GetCompEM().GetPluggedDevice() )
  176. {
  177. return true;
  178. }
  179. return false;
  180. }
  181. override void OnOwnSocketTaken( EntityAI device )
  182. {
  183. string att_type = device.GetType();
  184. if ( att_type == "CarBattery" )
  185. {
  186. HideAttachedClipsStates();
  187. ShowSelection(SEL_CLIPS_CAR);
  188. }
  189. if ( att_type == "TruckBattery" )
  190. {
  191. HideAttachedClipsStates();
  192. ShowSelection(SEL_CLIPS_TRUCK);
  193. }
  194. HideSelection(SEL_CLIPS_DETACHED);
  195. HideSelection(SEL_CLIPS_FOLDED);
  196. }
  197. override void OnOwnSocketReleased( EntityAI device )
  198. {
  199. HideAttachedClipsStates();
  200. ShowSelection(SEL_CLIPS_DETACHED);
  201. }
  202. override bool CanReceiveAttachment( EntityAI attachment, int slotId )
  203. {
  204. if ( !super.CanReceiveAttachment(attachment, slotId) )
  205. return false;
  206. ItemBase ibase;
  207. Class.CastTo(ibase, attachment);
  208. // No attaching if the charger is in inventory!
  209. PlayerBase charger_owner = PlayerBase.Cast( GetHierarchyRootPlayer() );
  210. if ( charger_owner )
  211. return false;
  212. // Only one attachment allowed
  213. if ( GetCompEM().GetPluggedDevice() )
  214. return false;
  215. if ( ibase.HasEnergyManager() && ibase.GetCompEM().GetPluggedDevicesCount() >= 1 ) // Make sure nothing is plugged into the battery
  216. return false;
  217. return true;
  218. }
  219. override bool CanLoadAttachment( EntityAI attachment)
  220. {
  221. if ( !super.CanLoadAttachment(attachment) )
  222. return false;
  223. ItemBase ibase;
  224. Class.CastTo(ibase, attachment);
  225. // Only one attachment allowed
  226. if ( GetCompEM().GetPluggedDevice() )
  227. return false;
  228. if ( ibase.HasEnergyManager() && ibase.GetCompEM().GetPluggedDevicesCount() >= 1 ) // Make sure nothing is plugged into the battery
  229. return false;
  230. return true;
  231. }
  232. void HideAttachedClipsStates()
  233. {
  234. for ( int i = 0; i < ATTACHED_CLIPS_STATES_COUNT; i++ )
  235. {
  236. string selection = ATTACHED_CLIPS_STATES[i];
  237. HideSelection(selection);
  238. }
  239. }
  240. // Control of status lights
  241. // ON
  242. void RedLightOn()
  243. {
  244. SetObjectMaterial( 0, RED_LIGHT_GLOW );
  245. }
  246. void GreenLightOn()
  247. {
  248. SetObjectMaterial( 2, GREEN_LIGHT_GLOW );
  249. }
  250. void YellowLightOn()
  251. {
  252. SetObjectMaterial( 1, YELLOW_LIGHT_GLOW );
  253. }
  254. void SwitchLightOn()
  255. {
  256. SetObjectMaterial( 3, SWITCH_LIGHT_GLOW );
  257. }
  258. // OFF
  259. void RedLightOff()
  260. {
  261. SetObjectMaterial( 0, DEFAULT_MATERIAL );
  262. }
  263. void GreenLightOff()
  264. {
  265. SetObjectMaterial( 2, DEFAULT_MATERIAL );
  266. }
  267. void YellowLightOff()
  268. {
  269. SetObjectMaterial( 1, DEFAULT_MATERIAL );
  270. }
  271. void SwitchLightOff()
  272. {
  273. SetObjectMaterial( 3, DEFAULT_MATERIAL );
  274. }
  275. override void OnSwitchOn()
  276. {
  277. HideSelection(SEL_SWITCH_OFF);
  278. ShowSelection(SEL_SWITCH_ON);
  279. }
  280. override void OnSwitchOff()
  281. {
  282. HideSelection(SEL_SWITCH_ON);
  283. ShowSelection(SEL_SWITCH_OFF);
  284. }
  285. // Inventory manipulation
  286. override void OnInventoryExit(Man player)
  287. {
  288. super.OnInventoryExit(player);
  289. HideAttachedClipsStates();
  290. HideSelection(SEL_CLIPS_FOLDED);
  291. ShowSelection(SEL_CLIPS_DETACHED);
  292. }
  293. override void OnInventoryEnter(Man player)
  294. {
  295. super.OnInventoryEnter(player);
  296. HideAttachedClipsStates();
  297. HideSelection(SEL_CLIPS_DETACHED);
  298. ShowSelection(SEL_CLIPS_FOLDED);
  299. }
  300. override void RefreshPhysics()
  301. {
  302. super.RefreshPhysics();
  303. if ( GetAttachmentByType(CarBattery) )
  304. {
  305. RemoveProxyPhysics( "battery" );
  306. AddProxyPhysics( "battery" );
  307. }
  308. else
  309. RemoveProxyPhysics( "battery" );
  310. }
  311. //================================================================
  312. // ADVANCED PLACEMENT
  313. //================================================================
  314. override void OnPlacementStarted(Man player)
  315. {
  316. super.OnPlacementStarted(player);
  317. SetAnimationPhase(SEL_CLIPS_DETACHED, 0);
  318. SetAnimationPhase(SEL_CLIPS_FOLDED, 1);
  319. SetAnimationPhase(SEL_SWITCH_ON, 1);
  320. SetAnimationPhase(SEL_SWITCH_OFF, 1);
  321. SetAnimationPhase(SEL_LIGHT_STATE_1, 1);
  322. SetAnimationPhase(SEL_LIGHT_STATE_2, 1);
  323. SetAnimationPhase(SEL_LIGHT_STATE_3, 1);
  324. array<string> selections = {
  325. SEL_CORD_PLUGGED,
  326. SEL_CORD_FOLDED,
  327. SEL_CLIPS_DETACHED,
  328. SEL_CLIPS_FOLDED
  329. };
  330. PlayerBase playerPB = PlayerBase.Cast(player);
  331. foreach (string selection : selections)
  332. {
  333. if (GetGame().IsMultiplayer() && GetGame().IsServer())
  334. playerPB.GetHologramServer().SetSelectionToRefresh(selection);
  335. else
  336. playerPB.GetHologramLocal().SetSelectionToRefresh(selection);
  337. }
  338. }
  339. override bool IsDeployable()
  340. {
  341. return true;
  342. }
  343. override string GetDeploySoundset()
  344. {
  345. return "placeBatteryCharger_SoundSet";
  346. }
  347. override void SetActions()
  348. {
  349. super.SetActions();
  350. AddAction(ActionPlugIn);
  351. AddAction(ActionTogglePlaceObject);
  352. AddAction(ActionUnplugThisByCord);
  353. AddAction(ActionTurnOnWhileOnGround);
  354. AddAction(ActionTurnOffWhileOnGround);
  355. AddAction(ActionPlaceObject);
  356. }
  357. }