totem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. class TerritoryFlag extends BaseBuildingBase
  2. {
  3. const float MAX_ACTION_DETECTION_ANGLE_RAD = 1.3; //1.3 RAD = ~75 DEG
  4. const float MAX_ACTION_DETECTION_DISTANCE = 2.0; //meters
  5. bool m_RefresherActive;
  6. bool m_RefresherActiveLocal;
  7. bool m_RefresherInitialized;
  8. int m_RefresherTimeRemaining;
  9. int m_RefreshTimeCounter;
  10. protected int m_FlagRefresherFrequency = GameConstants.REFRESHER_FREQUENCY_DEFAULT; //how often does the flag increase lifetimes
  11. protected int m_FlagRefresherMaxDuration = GameConstants.REFRESHER_MAX_DURATION_DEFAULT; //how long will the refresher run; multiple of m_FlagRefresherFrequency by default
  12. void TerritoryFlag()
  13. {
  14. m_RefresherActive = false;
  15. m_RefresherActiveLocal = false;
  16. m_RefresherInitialized = false;
  17. m_RefresherTimeRemaining = 0;
  18. if ( GetCEApi() )
  19. {
  20. InitRefresherData();
  21. }
  22. RegisterNetSyncVariableBool("m_RefresherActive");
  23. }
  24. void ~TerritoryFlag()
  25. {
  26. RemoveRefresherPosition();
  27. }
  28. void InitRefresherData()
  29. {
  30. int frequency = GetCEApi().GetCEGlobalInt("FlagRefreshFrequency");
  31. int max_duration = GetCEApi().GetCEGlobalInt("FlagRefreshMaxDuration");
  32. if ( frequency > 0)
  33. {
  34. m_FlagRefresherFrequency = frequency;
  35. }
  36. if ( max_duration > 0 )
  37. {
  38. m_FlagRefresherMaxDuration = max_duration;
  39. }
  40. m_RefresherInitialized = true;
  41. }
  42. override string GetConstructionKitType()
  43. {
  44. return "TerritoryFlagKit";
  45. }
  46. override int GetMeleeTargetType()
  47. {
  48. return EMeleeTargetType.NONALIGNABLE;
  49. }
  50. /*override bool NameOverride(out string output)
  51. {
  52. if ( m_GateState != GATE_STATE_NONE )
  53. {
  54. output = "#str_cfgvehicles_construction_part_gate"; //changes object displayed name if in 'gate' state
  55. output.ToUpper();
  56. return true;
  57. }
  58. return false;
  59. }*/
  60. //--- CONSTRUCTION KIT
  61. override vector GetKitSpawnPosition()
  62. {
  63. if ( MemoryPointExists( "kit_spawn_position" ) )
  64. {
  65. vector position;
  66. position = GetMemoryPointPos( "kit_spawn_position" );
  67. return ModelToWorld( position );
  68. }
  69. return GetPosition();
  70. }
  71. override bool CanDisplayAttachmentCategory( string category_name )
  72. {
  73. if ( category_name == "Base" && !HasBase() )
  74. return true;
  75. else if ( category_name == "Support" && HasBase() && !GetConstruction().IsPartConstructed("support") )
  76. return true;
  77. else if ( category_name == "Pole" && GetConstruction().IsPartConstructed("support") && !GetConstruction().IsPartConstructed("pole") )
  78. return true;
  79. else if ( category_name == "Flag" && GetConstruction().IsPartConstructed("pole") )
  80. return true;
  81. else
  82. return false;
  83. }
  84. // ---
  85. // --- EVENTS
  86. override void OnStoreSave( ParamsWriteContext ctx )
  87. {
  88. super.OnStoreSave( ctx );
  89. ctx.Write( m_RefresherTimeRemaining );
  90. ctx.Write( m_RefreshTimeCounter );
  91. ctx.Write( m_FlagRefresherMaxDuration );
  92. ctx.Write( m_RefresherActive );
  93. }
  94. override bool OnStoreLoad( ParamsReadContext ctx, int version )
  95. {
  96. if ( !super.OnStoreLoad( ctx, version ) )
  97. return false;
  98. //int loaded_frequency;
  99. int loaded_max_duration;
  100. if ( !ctx.Read( m_RefresherTimeRemaining ) )
  101. {
  102. return false;
  103. }
  104. if ( !ctx.Read( m_RefreshTimeCounter ) )
  105. {
  106. return false;
  107. }
  108. if ( !ctx.Read( loaded_max_duration ) )
  109. {
  110. return false;
  111. }
  112. if ( version >= 118 && !ctx.Read( m_RefresherActive ) )
  113. {
  114. return false;
  115. }
  116. CheckLoadedVariables(loaded_max_duration);
  117. AnimateFlag(1 - GetRefresherTime01());
  118. return true;
  119. }
  120. override void AfterStoreLoad()
  121. {
  122. super.AfterStoreLoad();
  123. if (!m_RefresherInitialized && GetCEApi())
  124. {
  125. InitRefresherData();
  126. }
  127. SetSynchDirty();
  128. UpdateVisuals();
  129. }
  130. override void OnCEUpdate()
  131. {
  132. super.OnCEUpdate();
  133. int time_elapsed_rounded = Math.Round(m_ElapsedSinceLastUpdate);
  134. if ( m_RefresherTimeRemaining > 0 )
  135. {
  136. m_RefreshTimeCounter += time_elapsed_rounded;
  137. if ( m_RefreshTimeCounter >= m_FlagRefresherFrequency )
  138. {
  139. GetCEApi().RadiusLifetimeReset(GetPosition(),GameConstants.REFRESHER_RADIUS);
  140. m_RefresherTimeRemaining = Math.Clamp(m_RefresherTimeRemaining - m_RefreshTimeCounter,0,m_FlagRefresherMaxDuration);
  141. m_RefreshTimeCounter = 0; //possibly carry over the rest?
  142. AnimateFlag( 1 - GetRefresherTime01() );
  143. }
  144. }
  145. SetRefresherActive(m_RefresherTimeRemaining > 0);
  146. }
  147. //! Saves positions of active lifetime refreshers to MissionGameplay / MissionServer
  148. void HandleRefreshers()
  149. {
  150. Mission mission = GetGame().GetMission();
  151. if (!mission.m_ActiveRefresherLocations)
  152. return;
  153. int idx = mission.m_ActiveRefresherLocations.Find(GetPosition());
  154. if ( m_RefresherActive && idx == -1 )
  155. {
  156. InsertRefresherPosition();
  157. }
  158. else if ( !m_RefresherActive && idx > -1 )
  159. {
  160. RemoveRefresherPosition(idx);
  161. }
  162. }
  163. void SetRefresherActive(bool state)
  164. {
  165. if ( m_RefresherActive != state )
  166. {
  167. m_RefresherActive = state;
  168. SetSynchDirty();
  169. HandleRefreshers();
  170. //update on refresher activation / last update on refresher deactivation
  171. if (GetCEApi())
  172. GetCEApi().RadiusLifetimeReset(GetPosition(),GameConstants.REFRESHER_RADIUS);
  173. }
  174. }
  175. void InsertRefresherPosition()
  176. {
  177. Mission mission = GetGame().GetMission();
  178. mission.m_ActiveRefresherLocations.Insert(GetPosition());
  179. }
  180. void RemoveRefresherPosition(int idx = -2)
  181. {
  182. if (!GetGame())
  183. return;
  184. Mission mission = GetGame().GetMission();
  185. if (!mission || !mission.m_ActiveRefresherLocations)
  186. return;
  187. if (idx == -2)
  188. {
  189. idx = mission.m_ActiveRefresherLocations.Find(GetPosition());
  190. }
  191. if (idx > -1)
  192. {
  193. mission.m_ActiveRefresherLocations.Remove(idx);
  194. }
  195. }
  196. override void OnVariablesSynchronized()
  197. {
  198. super.OnVariablesSynchronized();
  199. if ( m_RefresherActive != m_RefresherActiveLocal )
  200. {
  201. HandleRefreshers();
  202. m_RefresherActiveLocal = m_RefresherActive;
  203. }
  204. }
  205. //--- BUILD EVENTS
  206. //CONSTRUCTION EVENTS
  207. override void OnPartBuiltServer( notnull Man player, string part_name, int action_id )
  208. {
  209. //ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
  210. super.OnPartBuiltServer( player, part_name, action_id );
  211. //update visuals (server)
  212. UpdateVisuals();
  213. }
  214. override void OnPartDismantledServer( notnull Man player, string part_name, int action_id )
  215. {
  216. ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
  217. super.OnPartDismantledServer( player, part_name, action_id );
  218. //update visuals (server)
  219. UpdateVisuals();
  220. }
  221. override void OnPartDestroyedServer( Man player, string part_name, int action_id, bool destroyed_by_connected_part = false )
  222. {
  223. super.OnPartDestroyedServer( player, part_name, action_id );
  224. //update visuals (server)
  225. UpdateVisuals();
  226. }
  227. //--- ATTACHMENT & CONDITIONS
  228. override void EEItemDetached( EntityAI item, string slot_name )
  229. {
  230. super.EEItemDetached( item, slot_name );
  231. if (GetGame().IsServer()) // reset totem state is flag is decrafted while raised
  232. {
  233. float state = GetAnimationPhase("flag_mast");
  234. if (state < 1)
  235. {
  236. AnimateFlag(1);
  237. SetRefreshTimer01(0);
  238. }
  239. }
  240. }
  241. override bool CanReceiveAttachment( EntityAI attachment, int slotId ) //TODO
  242. {
  243. if ( !super.CanReceiveAttachment(attachment, slotId) )
  244. return false;
  245. //manage action initiator (AT_ATTACH_TO_CONSTRUCTION)
  246. if ( !GetGame().IsDedicatedServer() )
  247. {
  248. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  249. if ( player )
  250. {
  251. ConstructionActionData construction_action_data = player.GetConstructionActionData();
  252. //reset action initiator
  253. construction_action_data.SetActionInitiator( NULL );
  254. }
  255. }
  256. //conditions
  257. string slot_name = InventorySlots.GetSlotName(slotId);
  258. //flag item attachment
  259. if ( slot_name == "Material_FPole_Flag" )
  260. {
  261. if ( GetConstruction().IsPartConstructed("pole") )
  262. return true;
  263. else
  264. return false;
  265. }
  266. //materials
  267. ConstructionPart part;
  268. ConstructionPart part_lowest;
  269. int part_id = -1;
  270. for (int i = 0; i < GetConstruction().GetConstructionParts().Count(); i++)
  271. {
  272. part = GetConstruction().GetConstructionParts().GetElement(i);
  273. if (!part.IsBuilt())
  274. {
  275. if (part_id == -1 || part_id > part.GetId())
  276. {
  277. part_lowest = part;
  278. part_id = part.GetId();
  279. }
  280. }
  281. }
  282. if (part_lowest /*&& !part_lowest.IsBuilt()*/)
  283. {
  284. string cfg_path = "cfgVehicles " + GetType() + " Construction " + part_lowest.GetMainPartName() + " " + part_lowest.GetPartName() + " Materials";
  285. string material_path;
  286. if ( GetGame().ConfigIsExisting(cfg_path) )
  287. {
  288. string child_name;
  289. string child_slot_name;
  290. for ( i = 0; i < GetGame().ConfigGetChildrenCount( cfg_path ); i++ )
  291. {
  292. GetGame().ConfigGetChildName( cfg_path, i, child_name );
  293. material_path = "" + cfg_path + " " + child_name + " slot_name";
  294. if ( GetGame().ConfigGetText(material_path,child_slot_name) && child_slot_name == slot_name )
  295. {
  296. return true;
  297. }
  298. }
  299. }
  300. }
  301. return false;
  302. }
  303. //hands
  304. override bool CanPutIntoHands( EntityAI parent )
  305. {
  306. return false;
  307. }
  308. override bool CanBeRepairedToPristine()
  309. {
  310. return true;
  311. }
  312. //--- ACTION CONDITIONS
  313. override bool IsPlayerInside( PlayerBase player, string selection )
  314. {
  315. return true; //TODO
  316. vector player_pos = player.GetPosition();
  317. vector fence_pos = GetPosition();
  318. vector ref_dir = GetDirection();
  319. ref_dir[1] = 0;
  320. ref_dir.Normalize();
  321. vector x[2];
  322. vector b1,b2;
  323. GetCollisionBox(x);
  324. b1 = x[0];
  325. b2 = x[1];
  326. vector dir_to_fence = fence_pos - player_pos;
  327. dir_to_fence[1] = 0;
  328. float len = dir_to_fence.Length();
  329. dir_to_fence.Normalize();
  330. vector ref_dir_angle = ref_dir.VectorToAngles();
  331. vector dir_to_fence_angle = dir_to_fence.VectorToAngles();
  332. vector test_angles = dir_to_fence_angle - ref_dir_angle;
  333. vector test_position = test_angles.AnglesToVector() * len;
  334. if(test_position[0] < b1[0] || test_position[0] > b2[0] || test_position[2] < 0.2 || test_position[2] > 2.2 )
  335. {
  336. return false;
  337. }
  338. else
  339. {
  340. return true;
  341. }
  342. }
  343. override bool IsFacingPlayer( PlayerBase player, string selection )
  344. {
  345. return true; //TODO
  346. vector fence_pos = GetPosition();
  347. vector player_pos = player.GetPosition();
  348. vector ref_dir = GetDirection();
  349. //vector fence_player_dir = player_pos - fence_pos;
  350. vector fence_player_dir = player.GetDirection();
  351. fence_player_dir.Normalize();
  352. fence_player_dir[1] = 0; //ignore height
  353. ref_dir.Normalize();
  354. ref_dir[1] = 0; //ignore height
  355. if ( ref_dir.Length() != 0 )
  356. {
  357. float angle = Math.Acos( fence_player_dir * ref_dir );
  358. if ( angle >= MAX_ACTION_DETECTION_ANGLE_RAD )
  359. {
  360. return true;
  361. }
  362. }
  363. return false;
  364. }
  365. override bool IsFacingCamera( string selection )
  366. {
  367. return false; //TODO
  368. vector ref_dir = GetDirection();
  369. vector cam_dir = GetGame().GetCurrentCameraDirection();
  370. //ref_dir = GetGame().GetCurrentCameraPosition() - GetPosition();
  371. ref_dir.Normalize();
  372. ref_dir[1] = 0; //ignore height
  373. cam_dir.Normalize();
  374. cam_dir[1] = 0; //ignore height
  375. if ( ref_dir.Length() != 0 )
  376. {
  377. float angle = Math.Acos( cam_dir * ref_dir );
  378. if ( angle >= MAX_ACTION_DETECTION_ANGLE_RAD )
  379. {
  380. return true;
  381. }
  382. }
  383. return false;
  384. }
  385. override bool HasProperDistance( string selection, PlayerBase player )
  386. {
  387. //TODO
  388. if ( MemoryPointExists( selection ) )
  389. {
  390. vector selection_pos = ModelToWorld( GetMemoryPointPos( selection ) );
  391. float distance = vector.Distance( selection_pos, player.GetPosition() );
  392. if ( distance >= MAX_ACTION_DETECTION_DISTANCE )
  393. {
  394. return false;
  395. }
  396. }
  397. return true;
  398. }
  399. //================================================================
  400. // SOUNDS
  401. //================================================================
  402. //TODO
  403. override void SetActions()
  404. {
  405. super.SetActions();
  406. AddAction(ActionRaiseFlag);
  407. AddAction(ActionLowerFlag);
  408. AddAction(ActionFoldBaseBuildingObject);
  409. }
  410. /*override void UpdateVisuals()
  411. {
  412. super.UpdateVisuals();
  413. }*/
  414. //================================================================
  415. // FLAG MANIPULATION + REFRESHER
  416. //================================================================
  417. void AnimateFlagEx(float delta, PlayerBase player = null)
  418. {
  419. float temp = Math.Clamp( delta,0,1 );
  420. float phase_new;
  421. if (temp < 0.01 || temp > 0.99)
  422. {
  423. phase_new = Math.Round(temp);
  424. }
  425. else
  426. phase_new = temp;
  427. SetAnimationPhase("flag_mast",phase_new);
  428. if (player)
  429. LogAnimateFlag(phase_new, player);
  430. GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString("Material_FPole_Flag"),phase_new != 1);
  431. }
  432. void AnimateFlag(float delta)
  433. {
  434. AnimateFlagEx(delta);
  435. }
  436. protected void LogAnimateFlag(float newPhase, notnull PlayerBase player)
  437. {
  438. PluginAdminLog logs = PluginAdminLog.Cast(GetPlugin(PluginAdminLog));
  439. if (newPhase == 0)
  440. {
  441. // at the top(it's inverted)
  442. logs.TotemFlagChange(true, player, this);
  443. }
  444. else if (newPhase == 1)
  445. {
  446. // at the bottom(it's inverted)
  447. logs.TotemFlagChange(false, player, this);
  448. }
  449. }
  450. void SetRefreshTimer01(float fraction)
  451. {
  452. float temp = Math.Clamp(m_FlagRefresherMaxDuration * fraction, 0, m_FlagRefresherMaxDuration);
  453. m_RefresherTimeRemaining = Math.Round(temp);
  454. }
  455. /*void AddRefresherTimeFlat(float seconds) //seconds?
  456. {
  457. float temp = Math.Clamp(m_RefresherTimeRemaining + seconds, 0, m_FlagRefresherMaxDuration);
  458. m_RefresherTimeRemaining = Math.Round(temp);
  459. SetRefresherActive(m_RefresherTimeRemaining > 0);
  460. }*/
  461. void AddRefresherTime01(float fraction)
  462. {
  463. float temp = Math.Clamp(m_RefresherTimeRemaining + (fraction * m_FlagRefresherMaxDuration), 0, m_FlagRefresherMaxDuration);
  464. m_RefresherTimeRemaining = Math.Round(temp);
  465. SetRefresherActive(m_RefresherTimeRemaining > 0);
  466. //Print(m_RefresherTimeRemaining);
  467. }
  468. float GetRefresherTime01()
  469. {
  470. return m_RefresherTimeRemaining / m_FlagRefresherMaxDuration;
  471. }
  472. void CheckLoadedVariables(int max_duration)
  473. {
  474. if (max_duration != m_FlagRefresherMaxDuration)
  475. {
  476. m_RefresherTimeRemaining = m_FlagRefresherMaxDuration * m_RefresherTimeRemaining / max_duration;
  477. }
  478. }
  479. //================================================================
  480. // DEBUG
  481. //================================================================
  482. /*
  483. override void DebugCustomState()
  484. {
  485. //debug
  486. m_SyncParts01 = 881; //full fence with gate
  487. m_HasHinges = true;
  488. m_HasBase = true;
  489. OnVariablesSynchronized();
  490. }
  491. */
  492. //Debug menu Spawn Ground Special
  493. override void OnDebugSpawn()
  494. {
  495. super.OnDebugSpawn();
  496. GetInventory().CreateInInventory("Flag_DayZ");
  497. AnimateFlag(0);
  498. AddRefresherTime01(1);
  499. }
  500. }