totem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. //! Client-side, saves positions of active lifetime refreshers to MissionGameplay
  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. //update on refresher activation / last update on refresher deactivation
  170. if (GetCEApi())
  171. GetCEApi().RadiusLifetimeReset(GetPosition(),GameConstants.REFRESHER_RADIUS);
  172. }
  173. }
  174. void InsertRefresherPosition()
  175. {
  176. Mission mission = GetGame().GetMission();
  177. mission.m_ActiveRefresherLocations.Insert(GetPosition());
  178. }
  179. void RemoveRefresherPosition(int idx = -2)
  180. {
  181. if (!GetGame() || (GetGame().IsMultiplayer() && GetGame().IsServer()) )
  182. return;
  183. Mission mission = GetGame().GetMission();
  184. if (!mission || !mission.m_ActiveRefresherLocations)
  185. return;
  186. if (idx == -2)
  187. {
  188. idx = mission.m_ActiveRefresherLocations.Find(GetPosition());
  189. }
  190. if (idx > -1)
  191. {
  192. mission.m_ActiveRefresherLocations.Remove(idx);
  193. }
  194. }
  195. override void OnVariablesSynchronized()
  196. {
  197. super.OnVariablesSynchronized();
  198. if ( m_RefresherActive != m_RefresherActiveLocal )
  199. {
  200. HandleRefreshers();
  201. m_RefresherActiveLocal = m_RefresherActive;
  202. }
  203. }
  204. //--- BUILD EVENTS
  205. //CONSTRUCTION EVENTS
  206. override void OnPartBuiltServer( notnull Man player, string part_name, int action_id )
  207. {
  208. //ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
  209. super.OnPartBuiltServer( player, part_name, action_id );
  210. //update visuals (server)
  211. UpdateVisuals();
  212. }
  213. override void OnPartDismantledServer( notnull Man player, string part_name, int action_id )
  214. {
  215. ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
  216. super.OnPartDismantledServer( player, part_name, action_id );
  217. //update visuals (server)
  218. UpdateVisuals();
  219. }
  220. override void OnPartDestroyedServer( Man player, string part_name, int action_id, bool destroyed_by_connected_part = false )
  221. {
  222. super.OnPartDestroyedServer( player, part_name, action_id );
  223. //update visuals (server)
  224. UpdateVisuals();
  225. }
  226. //--- ATTACHMENT & CONDITIONS
  227. override void EEItemDetached( EntityAI item, string slot_name )
  228. {
  229. super.EEItemDetached( item, slot_name );
  230. if (GetGame().IsServer()) // reset totem state is flag is decrafted while raised
  231. {
  232. float state = GetAnimationPhase("flag_mast");
  233. if (state < 1)
  234. {
  235. AnimateFlag(1);
  236. SetRefreshTimer01(0);
  237. }
  238. }
  239. }
  240. override bool CanReceiveAttachment( EntityAI attachment, int slotId ) //TODO
  241. {
  242. if ( !super.CanReceiveAttachment(attachment, slotId) )
  243. return false;
  244. //manage action initiator (AT_ATTACH_TO_CONSTRUCTION)
  245. if ( !GetGame().IsDedicatedServer() )
  246. {
  247. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  248. if ( player )
  249. {
  250. ConstructionActionData construction_action_data = player.GetConstructionActionData();
  251. //reset action initiator
  252. construction_action_data.SetActionInitiator( NULL );
  253. }
  254. }
  255. //conditions
  256. string slot_name = InventorySlots.GetSlotName(slotId);
  257. //flag item attachment
  258. if ( slot_name == "Material_FPole_Flag" )
  259. {
  260. if ( GetConstruction().IsPartConstructed("pole") )
  261. return true;
  262. else
  263. return false;
  264. }
  265. //materials
  266. ConstructionPart part;
  267. ConstructionPart part_lowest;
  268. int part_id = -1;
  269. for (int i = 0; i < GetConstruction().GetConstructionParts().Count(); i++)
  270. {
  271. part = GetConstruction().GetConstructionParts().GetElement(i);
  272. if (!part.IsBuilt())
  273. {
  274. if (part_id == -1 || part_id > part.GetId())
  275. {
  276. part_lowest = part;
  277. part_id = part.GetId();
  278. }
  279. }
  280. }
  281. if (part_lowest /*&& !part_lowest.IsBuilt()*/)
  282. {
  283. string cfg_path = "cfgVehicles " + GetType() + " Construction " + part_lowest.GetMainPartName() + " " + part_lowest.GetPartName() + " Materials";
  284. string material_path;
  285. if ( GetGame().ConfigIsExisting(cfg_path) )
  286. {
  287. string child_name;
  288. string child_slot_name;
  289. for ( i = 0; i < GetGame().ConfigGetChildrenCount( cfg_path ); i++ )
  290. {
  291. GetGame().ConfigGetChildName( cfg_path, i, child_name );
  292. material_path = "" + cfg_path + " " + child_name + " slot_name";
  293. if ( GetGame().ConfigGetText(material_path,child_slot_name) && child_slot_name == slot_name )
  294. {
  295. return true;
  296. }
  297. }
  298. }
  299. }
  300. return false;
  301. }
  302. //hands
  303. override bool CanPutIntoHands( EntityAI parent )
  304. {
  305. return false;
  306. }
  307. override bool CanBeRepairedToPristine()
  308. {
  309. return true;
  310. }
  311. //--- ACTION CONDITIONS
  312. override bool IsPlayerInside( PlayerBase player, string selection )
  313. {
  314. return true; //TODO
  315. vector player_pos = player.GetPosition();
  316. vector fence_pos = GetPosition();
  317. vector ref_dir = GetDirection();
  318. ref_dir[1] = 0;
  319. ref_dir.Normalize();
  320. vector x[2];
  321. vector b1,b2;
  322. GetCollisionBox(x);
  323. b1 = x[0];
  324. b2 = x[1];
  325. vector dir_to_fence = fence_pos - player_pos;
  326. dir_to_fence[1] = 0;
  327. float len = dir_to_fence.Length();
  328. dir_to_fence.Normalize();
  329. vector ref_dir_angle = ref_dir.VectorToAngles();
  330. vector dir_to_fence_angle = dir_to_fence.VectorToAngles();
  331. vector test_angles = dir_to_fence_angle - ref_dir_angle;
  332. vector test_position = test_angles.AnglesToVector() * len;
  333. if(test_position[0] < b1[0] || test_position[0] > b2[0] || test_position[2] < 0.2 || test_position[2] > 2.2 )
  334. {
  335. return false;
  336. }
  337. else
  338. {
  339. return true;
  340. }
  341. }
  342. override bool IsFacingPlayer( PlayerBase player, string selection )
  343. {
  344. return true; //TODO
  345. vector fence_pos = GetPosition();
  346. vector player_pos = player.GetPosition();
  347. vector ref_dir = GetDirection();
  348. //vector fence_player_dir = player_pos - fence_pos;
  349. vector fence_player_dir = player.GetDirection();
  350. fence_player_dir.Normalize();
  351. fence_player_dir[1] = 0; //ignore height
  352. ref_dir.Normalize();
  353. ref_dir[1] = 0; //ignore height
  354. if ( ref_dir.Length() != 0 )
  355. {
  356. float angle = Math.Acos( fence_player_dir * ref_dir );
  357. if ( angle >= MAX_ACTION_DETECTION_ANGLE_RAD )
  358. {
  359. return true;
  360. }
  361. }
  362. return false;
  363. }
  364. override bool IsFacingCamera( string selection )
  365. {
  366. return false; //TODO
  367. vector ref_dir = GetDirection();
  368. vector cam_dir = GetGame().GetCurrentCameraDirection();
  369. //ref_dir = GetGame().GetCurrentCameraPosition() - GetPosition();
  370. ref_dir.Normalize();
  371. ref_dir[1] = 0; //ignore height
  372. cam_dir.Normalize();
  373. cam_dir[1] = 0; //ignore height
  374. if ( ref_dir.Length() != 0 )
  375. {
  376. float angle = Math.Acos( cam_dir * ref_dir );
  377. if ( angle >= MAX_ACTION_DETECTION_ANGLE_RAD )
  378. {
  379. return true;
  380. }
  381. }
  382. return false;
  383. }
  384. override bool HasProperDistance( string selection, PlayerBase player )
  385. {
  386. //TODO
  387. if ( MemoryPointExists( selection ) )
  388. {
  389. vector selection_pos = ModelToWorld( GetMemoryPointPos( selection ) );
  390. float distance = vector.Distance( selection_pos, player.GetPosition() );
  391. if ( distance >= MAX_ACTION_DETECTION_DISTANCE )
  392. {
  393. return false;
  394. }
  395. }
  396. return true;
  397. }
  398. //================================================================
  399. // SOUNDS
  400. //================================================================
  401. //TODO
  402. override void SetActions()
  403. {
  404. super.SetActions();
  405. AddAction(ActionRaiseFlag);
  406. AddAction(ActionLowerFlag);
  407. AddAction(ActionFoldBaseBuildingObject);
  408. }
  409. /*override void UpdateVisuals()
  410. {
  411. super.UpdateVisuals();
  412. }*/
  413. //================================================================
  414. // FLAG MANIPULATION + REFRESHER
  415. //================================================================
  416. void AnimateFlagEx(float delta, PlayerBase player = null)
  417. {
  418. float temp = Math.Clamp( delta,0,1 );
  419. float phase_new;
  420. if (temp < 0.01 || temp > 0.99)
  421. {
  422. phase_new = Math.Round(temp);
  423. }
  424. else
  425. phase_new = temp;
  426. SetAnimationPhase("flag_mast",phase_new);
  427. if (player)
  428. LogAnimateFlag(phase_new, player);
  429. GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString("Material_FPole_Flag"),phase_new != 1);
  430. }
  431. void AnimateFlag(float delta)
  432. {
  433. AnimateFlagEx(delta);
  434. }
  435. protected void LogAnimateFlag(float newPhase, notnull PlayerBase player)
  436. {
  437. PluginAdminLog logs = PluginAdminLog.Cast(GetPlugin(PluginAdminLog));
  438. if (newPhase == 0)
  439. {
  440. // at the top(it's inverted)
  441. logs.TotemFlagChange(true, player, this);
  442. }
  443. else if (newPhase == 1)
  444. {
  445. // at the bottom(it's inverted)
  446. logs.TotemFlagChange(false, player, this);
  447. }
  448. }
  449. void SetRefreshTimer01(float fraction)
  450. {
  451. float temp = Math.Clamp(m_FlagRefresherMaxDuration * fraction, 0, m_FlagRefresherMaxDuration);
  452. m_RefresherTimeRemaining = Math.Round(temp);
  453. }
  454. /*void AddRefresherTimeFlat(float seconds) //seconds?
  455. {
  456. float temp = Math.Clamp(m_RefresherTimeRemaining + seconds, 0, m_FlagRefresherMaxDuration);
  457. m_RefresherTimeRemaining = Math.Round(temp);
  458. SetRefresherActive(m_RefresherTimeRemaining > 0);
  459. }*/
  460. void AddRefresherTime01(float fraction)
  461. {
  462. float temp = Math.Clamp(m_RefresherTimeRemaining + (fraction * m_FlagRefresherMaxDuration), 0, m_FlagRefresherMaxDuration);
  463. m_RefresherTimeRemaining = Math.Round(temp);
  464. SetRefresherActive(m_RefresherTimeRemaining > 0);
  465. //Print(m_RefresherTimeRemaining);
  466. }
  467. float GetRefresherTime01()
  468. {
  469. return m_RefresherTimeRemaining / m_FlagRefresherMaxDuration;
  470. }
  471. void CheckLoadedVariables(int max_duration)
  472. {
  473. if (max_duration != m_FlagRefresherMaxDuration)
  474. {
  475. m_RefresherTimeRemaining = m_FlagRefresherMaxDuration * m_RefresherTimeRemaining / max_duration;
  476. }
  477. }
  478. //================================================================
  479. // DEBUG
  480. //================================================================
  481. /*
  482. override void DebugCustomState()
  483. {
  484. //debug
  485. m_SyncParts01 = 881; //full fence with gate
  486. m_HasHinges = true;
  487. m_HasBase = true;
  488. OnVariablesSynchronized();
  489. }
  490. */
  491. //Debug menu Spawn Ground Special
  492. override void OnDebugSpawn()
  493. {
  494. super.OnDebugSpawn();
  495. GetInventory().CreateInInventory("Flag_DayZ");
  496. AnimateFlag(0);
  497. AddRefresherTime01(1);
  498. }
  499. }