fence.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. class Fence extends BaseBuildingBase
  2. {
  3. const int GATE_STATE_NONE = 0;
  4. const int GATE_STATE_PARTIAL = 1;
  5. const int GATE_STATE_FULL = 2;
  6. const string ATTACHMENT_SLOT_COMBINATION_LOCK = "Att_CombinationLock";
  7. const string SOUND_GATE_OPEN_START = "DoorWoodTowerOpen_SoundSet";
  8. const string SOUND_GATE_CLOSE_START = "DoorWoodTowerClose_start_SoundSet";
  9. const string SOUND_GATE_CLOSE_END = "DoorWoodTowerClose_end_SoundSet";
  10. //gate openining
  11. const float GATE_ROTATION_ANGLE_DEG = 100;
  12. const float GATE_ROTATION_TIME_APPROX = 2000; //ms
  13. const float MAX_ACTION_DETECTION_ANGLE_RAD = 1.3; //1.3 RAD = ~75 DEG
  14. const float MAX_ACTION_DETECTION_DISTANCE = 2.0; //meters
  15. typename ATTACHMENT_WOODEN_LOG = WoodenLog;
  16. typename ATTACHMENT_COMBINATION_LOCK = CombinationLock;
  17. string ATTSLOT_CAMONET = "Wall_Camonet";
  18. string ATTSLOT_BARBEDWIRE_DOWN = "Wall_Barbedwire_1";
  19. string ATTSLOT_BARBEDWIRE_UP = "Wall_Barbedwire_2";
  20. //protected bool m_HasHinges = false;
  21. //protected bool m_GateFullyConstructed = false;
  22. protected bool m_ToDiscard = false; //for legacy OnStoreLoad handling
  23. protected bool m_IsOpened = false;
  24. protected bool m_IsOpenedClient = false;
  25. protected int m_GateState = 0;
  26. protected EffectSound m_SoundGate_Start;
  27. protected EffectSound m_SoundGate_End;
  28. void Fence()
  29. {
  30. //synchronized variables
  31. //RegisterNetSyncVariableBool( "m_HasHinges" );
  32. //RegisterNetSyncVariableBool( "m_GateFullyConstructed" );
  33. RegisterNetSyncVariableBool( "m_IsOpened" );
  34. RegisterNetSyncVariableInt( "m_GateState" );
  35. }
  36. override string GetConstructionKitType()
  37. {
  38. return "FenceKit";
  39. }
  40. override int GetMeleeTargetType()
  41. {
  42. return EMeleeTargetType.NONALIGNABLE;
  43. }
  44. //Gate
  45. bool HasHinges()
  46. {
  47. return m_GateState > GATE_STATE_NONE;
  48. }
  49. bool HasFullyConstructedGate()
  50. {
  51. return m_GateState == GATE_STATE_FULL;
  52. }
  53. void SetGateState( int state )
  54. {
  55. m_GateState = state;
  56. SetSynchDirty();
  57. }
  58. int GetGateState()
  59. {
  60. return m_GateState;
  61. }
  62. int CheckGateState()
  63. {
  64. ConstructionPart gate_part = GetConstruction().GetGateConstructionPart();
  65. int state = GATE_STATE_NONE;
  66. if( gate_part.IsBuilt() )
  67. {
  68. ConstructionPart req_part;
  69. array<string> req_parts = gate_part.GetRequiredParts();
  70. for (int i = 0; i < req_parts.Count(); i++)
  71. {
  72. req_part = GetConstruction().GetConstructionPart(req_parts.Get(i));
  73. if(!req_part.IsBuilt())
  74. break;
  75. }
  76. if( i != req_parts.Count() )
  77. {
  78. state = GATE_STATE_PARTIAL;
  79. }
  80. else
  81. {
  82. state = GATE_STATE_FULL;
  83. }
  84. }
  85. return state;
  86. }
  87. void SetOpenedState( bool state )
  88. {
  89. m_IsOpened = state;
  90. }
  91. override bool IsOpened()
  92. {
  93. return m_IsOpened;
  94. }
  95. bool IsLocked()
  96. {
  97. CombinationLock combination_lock = GetCombinationLock();
  98. if ( combination_lock && combination_lock.IsLocked() )
  99. {
  100. return true;
  101. }
  102. return false;
  103. }
  104. override bool NameOverride(out string output)
  105. {
  106. if ( m_GateState != GATE_STATE_NONE )
  107. {
  108. output = "#str_cfgvehicles_construction_part_gate"; //changes object displayed name if in 'gate' state
  109. output.ToUpper();
  110. return true;
  111. }
  112. return false;
  113. }
  114. //
  115. CombinationLock GetCombinationLock()
  116. {
  117. CombinationLock combination_lock = CombinationLock.Cast( FindAttachmentBySlotName( ATTACHMENT_SLOT_COMBINATION_LOCK ) );
  118. return combination_lock;
  119. }
  120. CamoNet GetCamoNet()
  121. {
  122. CamoNet camonet = CamoNet.Cast( FindAttachmentBySlotName( "Wall_Camonet" ) );
  123. return camonet;
  124. }
  125. BarbedWire GetBarbedWire1()
  126. {
  127. BarbedWire barbedwire = BarbedWire.Cast( FindAttachmentBySlotName( "Wall_Barbedwire_1" ) );
  128. return barbedwire;
  129. }
  130. BarbedWire GetBarbedWire2()
  131. {
  132. BarbedWire barbedwire = BarbedWire.Cast( FindAttachmentBySlotName( "Wall_Barbedwire_2" ) );
  133. return barbedwire;
  134. }
  135. //--- CONSTRUCTION KIT
  136. override vector GetKitSpawnPosition()
  137. {
  138. if ( MemoryPointExists( "kit_spawn_position" ) )
  139. {
  140. vector position;
  141. position = GetMemoryPointPos( "kit_spawn_position" );
  142. return ModelToWorld( position );
  143. }
  144. return GetPosition();
  145. }
  146. // --- INVENTORY
  147. override bool CanDisplayAttachmentSlot( int slot_id )
  148. {
  149. if (!super.CanDisplayAttachmentSlot(slot_id))
  150. return false;
  151. string slot_name = InventorySlots.GetSlotName(slot_id);
  152. if ( slot_name == "Att_CombinationLock" )
  153. {
  154. if ( !HasFullyConstructedGate() )
  155. {
  156. return false;
  157. }
  158. }
  159. if ( !GateAttachmentConditions(InventorySlots.GetSlotIdFromString(slot_name)) )
  160. return false;
  161. return true;
  162. }
  163. override bool CanDisplayAttachmentCategory( string category_name )
  164. {
  165. if ( category_name == "Attachments" || category_name == "Material" )
  166. {
  167. if ( !HasBase() )
  168. {
  169. return false;
  170. }
  171. }
  172. return true;
  173. }
  174. // ---
  175. // --- EVENTS
  176. override void OnStoreSave( ParamsWriteContext ctx )
  177. {
  178. super.OnStoreSave( ctx );
  179. //write
  180. ctx.Write( m_GateState );
  181. ctx.Write( m_IsOpened );
  182. if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] OnStoreSave - build=" + m_GateState + " opened=" + m_IsOpened);
  183. }
  184. override bool OnStoreLoad( ParamsReadContext ctx, int version )
  185. {
  186. if ( !super.OnStoreLoad( ctx, version ) )
  187. return false;
  188. //--- Fence data ---
  189. //has gate
  190. if (version < 110)
  191. {
  192. if ( !ctx.Read( m_ToDiscard ) )
  193. {
  194. m_ToDiscard = false;
  195. return false;
  196. }
  197. m_GateState = GATE_STATE_NONE;
  198. }
  199. else if ( !ctx.Read( m_GateState ) )
  200. {
  201. m_GateState = GATE_STATE_NONE;
  202. return false;
  203. }
  204. //is opened
  205. if ( !ctx.Read( m_IsOpened ) )
  206. {
  207. m_IsOpened = false;
  208. return false;
  209. }
  210. if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] OnStoreLoad - build=" + m_GateState + " opened=" + m_IsOpened);
  211. //---
  212. return true;
  213. }
  214. override void AfterStoreLoad()
  215. {
  216. super.AfterStoreLoad();
  217. //set gate state
  218. ConstructionPart gate_part = GetConstruction().GetGateConstructionPart();
  219. SetGateState( CheckGateState() );
  220. //update gate state visual
  221. if ( IsOpened() )
  222. {
  223. OpenFence();
  224. }
  225. UpdateVisuals();
  226. if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] AfterStoreLoad - build=" + gate_part.IsBuilt() + " opened=" + IsOpened());
  227. }
  228. override void OnVariablesSynchronized()
  229. {
  230. super.OnVariablesSynchronized();
  231. if ( m_IsOpenedClient != m_IsOpened )
  232. {
  233. m_IsOpenedClient = m_IsOpened;
  234. if ( m_IsOpenedClient )
  235. {
  236. OpenFence();
  237. }
  238. else
  239. {
  240. CloseFence();
  241. }
  242. }
  243. }
  244. //--- BUILD EVENTS
  245. //CONSTRUCTION EVENTS
  246. override void OnPartBuiltServer( notnull Man player, string part_name, int action_id )
  247. {
  248. ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
  249. super.OnPartBuiltServer( player, part_name, action_id );
  250. SetGateState(CheckGateState());
  251. //update visuals (server)
  252. UpdateVisuals();
  253. }
  254. override void OnPartDismantledServer( notnull Man player, string part_name, int action_id )
  255. {
  256. ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
  257. //check gate state
  258. if ( constrution_part.IsGate() )
  259. {
  260. if ( IsLocked() )
  261. {
  262. CombinationLock combination_lock = CombinationLock.Cast( FindAttachmentBySlotName( ATTACHMENT_SLOT_COMBINATION_LOCK ) );
  263. combination_lock.UnlockServer( player , this );
  264. }
  265. }
  266. super.OnPartDismantledServer( player, part_name, action_id );
  267. SetGateState( CheckGateState() );
  268. //update visuals (server)
  269. UpdateVisuals();
  270. }
  271. override void OnPartDestroyedServer( Man player, string part_name, int action_id, bool destroyed_by_connected_part = false )
  272. {
  273. super.OnPartDestroyedServer( player, part_name, action_id );
  274. //check gate state
  275. ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
  276. if ( constrution_part.IsGate() && destroyed_by_connected_part ) //avoids attachment dropping on regular hinges destruction
  277. {
  278. //drop regular attachments
  279. HandleDropAttachment(GetBarbedWire1());
  280. HandleDropAttachment(GetBarbedWire2());
  281. HandleDropAttachment(GetCamoNet());
  282. HandleDropAttachment(GetCombinationLock());
  283. //rotate back to place
  284. if ( IsOpened() )
  285. CloseFence();
  286. }
  287. if ( part_name == "wall_base_down" )
  288. {
  289. HandleDropAttachment(GetBarbedWire1());
  290. HandleDropAttachment(GetCombinationLock());
  291. }
  292. if ( part_name == "wall_base_up" )
  293. {
  294. HandleDropAttachment(GetBarbedWire2());
  295. HandleDropAttachment(GetCamoNet());
  296. HandleDropAttachment(GetCombinationLock());
  297. }
  298. SetGateState( CheckGateState() );
  299. //update visuals (server)
  300. UpdateVisuals();
  301. }
  302. //--- ATTACHMENT & CONDITIONS
  303. override bool CanReceiveAttachment( EntityAI attachment, int slotId )
  304. {
  305. if ( !super.CanReceiveAttachment(attachment, slotId) )
  306. return false;
  307. //manage action initiator (AT_ATTACH_TO_CONSTRUCTION)
  308. if ( !GetGame().IsDedicatedServer() )
  309. {
  310. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  311. if ( player )
  312. {
  313. ConstructionActionData construction_action_data = player.GetConstructionActionData();
  314. //reset action initiator
  315. construction_action_data.SetActionInitiator( NULL );
  316. }
  317. }
  318. //conditions
  319. if ( attachment.Type() != ATTACHMENT_WOODEN_LOG )
  320. {
  321. if ( !HasBase() )
  322. {
  323. return false;
  324. }
  325. }
  326. if ( attachment.IsInherited( ATTACHMENT_COMBINATION_LOCK ) )
  327. {
  328. return ( HasFullyConstructedGate() && !IsOpened() );
  329. }
  330. if ( !GateAttachmentConditions(slotId) )
  331. return false;
  332. return true;
  333. }
  334. //hands
  335. override bool CanPutIntoHands( EntityAI parent )
  336. {
  337. if( !super.CanPutIntoHands( parent ) )
  338. {
  339. return false;
  340. }
  341. if ( HasBase() )
  342. {
  343. return false;
  344. }
  345. return true;
  346. }
  347. override bool CanBeRepairedToPristine()
  348. {
  349. return true;
  350. }
  351. //--- OPEN/CLOSE ACTIONS
  352. bool CanOpenFence()
  353. {
  354. if ( HasHinges() && !IsOpened() && !IsLocked() )
  355. {
  356. return true;
  357. }
  358. return false;
  359. }
  360. bool CanCloseFence()
  361. {
  362. if ( HasHinges() && IsOpened() )
  363. {
  364. return true;
  365. }
  366. return false;
  367. }
  368. void OpenFence()
  369. {
  370. //server or single player
  371. if ( GetGame().IsServer() )
  372. {
  373. float value = GATE_ROTATION_ANGLE_DEG;
  374. SetAnimationPhase( "Wall_Interact_Rotate", value );
  375. SetAnimationPhase( "Wall_Barbedwire_1_Mounted_Rotate", value );
  376. SetAnimationPhase( "Wall_Barbedwire_2_Mounted_Rotate", value );
  377. SetAnimationPhase( "Wall_Camonet_Rotate", value );
  378. SetAnimationPhase( "Wall_Gate_Rotate", value );
  379. SetAnimationPhase( "Wall_Base_Down_Rotate", value );
  380. SetAnimationPhase( "Wall_Base_Up_Rotate", value );
  381. SetAnimationPhase( "Wall_Wood_Down_Rotate", value );
  382. SetAnimationPhase( "Wall_Wood_Up_Rotate", value );
  383. SetAnimationPhase( "Wall_Metal_Down_Rotate", value );
  384. SetAnimationPhase( "Wall_Metal_Up_Rotate", value );
  385. SetOpenedState( true );
  386. //regenerate navmesh
  387. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( UpdateNavmesh, GATE_ROTATION_TIME_APPROX, false );
  388. //synchronize
  389. SynchronizeBaseState();
  390. }
  391. //client or single player
  392. if ( !GetGame().IsDedicatedServer() )
  393. {
  394. //play sound
  395. SoundGateOpenStart();
  396. }
  397. //remove BarbedWire AreaDamageTrigger
  398. UpdateBarbedWireAreaDamagePos(0,true);
  399. //add check
  400. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).Remove(CheckFenceClosed);
  401. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( CheckFenceOpened, 0, true );
  402. }
  403. void CloseFence()
  404. {
  405. //server or single player
  406. if ( GetGame().IsServer() )
  407. {
  408. float value = 0;
  409. SetAnimationPhase( "Wall_Interact_Rotate", value );
  410. SetAnimationPhase( "Wall_Barbedwire_1_Mounted_Rotate", value );
  411. SetAnimationPhase( "Wall_Barbedwire_2_Mounted_Rotate", value );
  412. SetAnimationPhase( "Wall_Camonet_Rotate", value );
  413. SetAnimationPhase( "Wall_Gate_Rotate", value );
  414. SetAnimationPhase( "Wall_Base_Down_Rotate", value );
  415. SetAnimationPhase( "Wall_Base_Up_Rotate", value );
  416. SetAnimationPhase( "Wall_Wood_Down_Rotate", value );
  417. SetAnimationPhase( "Wall_Wood_Up_Rotate", value );
  418. SetAnimationPhase( "Wall_Metal_Down_Rotate", value );
  419. SetAnimationPhase( "Wall_Metal_Up_Rotate", value );
  420. SetOpenedState( false );
  421. //regenerate navmesh
  422. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( UpdateNavmesh, GATE_ROTATION_TIME_APPROX, false );
  423. //synchronize
  424. SynchronizeBaseState();
  425. }
  426. //client or single player
  427. if ( !GetGame().IsDedicatedServer() )
  428. {
  429. //play sound
  430. SoundGateCloseStart();
  431. }
  432. //remove BarbedWire AreaDamageTrigger
  433. UpdateBarbedWireAreaDamagePos(0,true);
  434. //add check
  435. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).Remove(CheckFenceOpened);
  436. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( CheckFenceClosed, 0, true );
  437. }
  438. protected void CheckFenceOpened()
  439. {
  440. if ( GetAnimationPhase( "Wall_Gate_Rotate" ) == GATE_ROTATION_ANGLE_DEG ) //animation finished - open
  441. {
  442. UpdateBarbedWireAreaDamagePos(GetAnimationPhase( "Wall_Gate_Rotate" ));
  443. //remove check
  444. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).Remove( CheckFenceOpened );
  445. }
  446. }
  447. protected void CheckFenceClosed()
  448. {
  449. if ( GetAnimationPhase( "Wall_Gate_Rotate" ) == 0 ) //animation finished - closed
  450. {
  451. //client or single player
  452. if ( !GetGame().IsDedicatedServer() )
  453. {
  454. //play sound
  455. if ( this ) SoundGateCloseEnd();
  456. }
  457. UpdateBarbedWireAreaDamagePos(GetAnimationPhase( "Wall_Gate_Rotate" ));
  458. //remove check
  459. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).Remove( CheckFenceClosed );
  460. }
  461. }
  462. //Damage triggers
  463. override void CreateAreaDamage( string slot_name, float rotation_angle = 0 )
  464. {
  465. if ( IsOpened() )
  466. {
  467. rotation_angle = 100;
  468. }
  469. super.CreateAreaDamage( slot_name, rotation_angle );
  470. }
  471. //BarbedWire update
  472. void UpdateBarbedWireAreaDamagePos(float rotation_angle = 0, bool to_delete = false )
  473. {
  474. int slot_id;
  475. string slot_name;
  476. string slot_name_mounted;
  477. if ( GetBarbedWire1() && GetBarbedWire1().IsMounted() )
  478. {
  479. GetBarbedWire1().GetInventory().GetCurrentAttachmentSlotInfo(slot_id,slot_name);
  480. slot_name_mounted = slot_name + "_Mounted";
  481. if (to_delete)
  482. {
  483. DestroyAreaDamage( slot_name_mounted );
  484. }
  485. else
  486. {
  487. super.CreateAreaDamage( slot_name_mounted, rotation_angle );
  488. }
  489. }
  490. if ( GetBarbedWire2() && GetBarbedWire2().IsMounted() )
  491. {
  492. GetBarbedWire2().GetInventory().GetCurrentAttachmentSlotInfo(slot_id,slot_name);
  493. slot_name_mounted = slot_name + "_Mounted";
  494. if (to_delete)
  495. {
  496. DestroyAreaDamage( slot_name_mounted );
  497. }
  498. else
  499. {
  500. super.CreateAreaDamage( slot_name_mounted, rotation_angle );
  501. }
  502. }
  503. }
  504. //Here deal damage to BarbedWire when entity taking damage from it
  505. override void PostAreaDamageActions()
  506. {
  507. /*if (GetBarbedWire1())
  508. {
  509. //DecreaseHealth("Wall_BarbedWire_1", "", 1000); //why no dmg to wire???
  510. //BarbedWire wire = GetBarbedWire1();
  511. //wire.PostAreaDamageActions();
  512. //Print(GetHealth("BarbedWire1", ""));
  513. }*/
  514. }
  515. //--- ACTION CONDITIONS
  516. override bool IsPlayerInside( PlayerBase player, string selection )
  517. {
  518. vector player_pos = player.GetPosition();
  519. vector fence_pos = GetPosition();
  520. vector ref_dir = GetDirection();
  521. ref_dir[1] = 0;
  522. ref_dir.Normalize();
  523. vector x[2];
  524. vector b1,b2;
  525. GetCollisionBox(x);
  526. b1 = x[0];
  527. b2 = x[1];
  528. vector dir_to_fence = fence_pos - player_pos;
  529. dir_to_fence[1] = 0;
  530. float len = dir_to_fence.Length();
  531. dir_to_fence.Normalize();
  532. vector ref_dir_angle = ref_dir.VectorToAngles();
  533. vector dir_to_fence_angle = dir_to_fence.VectorToAngles();
  534. vector test_angles = dir_to_fence_angle - ref_dir_angle;
  535. vector test_position = test_angles.AnglesToVector() * len;
  536. if(test_position[0] < b1[0] || test_position[0] > b2[0] || test_position[2] < 0.2 || test_position[2] > 2.2 )
  537. {
  538. return false;
  539. }
  540. else
  541. {
  542. return true;
  543. }
  544. }
  545. override bool IsFacingPlayer( PlayerBase player, string selection )
  546. {
  547. vector fence_pos = GetPosition();
  548. vector player_pos = player.GetPosition();
  549. vector ref_dir = GetDirection();
  550. //vector fence_player_dir = player_pos - fence_pos;
  551. vector fence_player_dir = player.GetDirection();
  552. fence_player_dir.Normalize();
  553. fence_player_dir[1] = 0; //ignore height
  554. ref_dir.Normalize();
  555. ref_dir[1] = 0; //ignore height
  556. if ( ref_dir.Length() != 0 )
  557. {
  558. float angle = Math.Acos( fence_player_dir * ref_dir );
  559. if ( angle >= MAX_ACTION_DETECTION_ANGLE_RAD )
  560. {
  561. return true;
  562. }
  563. }
  564. return false;
  565. }
  566. override bool IsFacingCamera( string selection )
  567. {
  568. vector ref_dir = GetDirection();
  569. vector cam_dir = GetGame().GetCurrentCameraDirection();
  570. //ref_dir = GetGame().GetCurrentCameraPosition() - GetPosition();
  571. ref_dir.Normalize();
  572. ref_dir[1] = 0; //ignore height
  573. cam_dir.Normalize();
  574. cam_dir[1] = 0; //ignore height
  575. if ( ref_dir.Length() != 0 )
  576. {
  577. float angle = Math.Acos( cam_dir * ref_dir );
  578. if ( angle >= MAX_ACTION_DETECTION_ANGLE_RAD )
  579. {
  580. return true;
  581. }
  582. }
  583. return false;
  584. }
  585. override bool HasProperDistance( string selection, PlayerBase player )
  586. {
  587. if ( MemoryPointExists( selection ) )
  588. {
  589. vector selection_pos = ModelToWorld( GetMemoryPointPos( selection ) );
  590. float distance = vector.Distance( selection_pos, player.GetPosition() );
  591. if ( distance >= MAX_ACTION_DETECTION_DISTANCE )
  592. {
  593. return false;
  594. }
  595. }
  596. return true;
  597. }
  598. override bool CanUseConstructionBuild()
  599. {
  600. return !IsOpened();
  601. }
  602. //================================================================
  603. // SOUNDS
  604. //================================================================
  605. protected void SoundGateOpenStart()
  606. {
  607. //client or single player
  608. if ( !GetGame().IsDedicatedServer() )
  609. {
  610. PlaySoundSet( m_SoundGate_Start, SOUND_GATE_OPEN_START, 0.1, 0.1 );
  611. }
  612. }
  613. protected void SoundGateCloseStart()
  614. {
  615. //client or single player
  616. if ( !GetGame().IsDedicatedServer() )
  617. {
  618. PlaySoundSet( m_SoundGate_Start, SOUND_GATE_CLOSE_START, 0.1, 0.1 );
  619. }
  620. }
  621. protected void SoundGateCloseEnd()
  622. {
  623. //client or single player
  624. if ( !GetGame().IsDedicatedServer() )
  625. {
  626. PlaySoundSet( m_SoundGate_End, SOUND_GATE_CLOSE_END, 0.1, 0.1 );
  627. }
  628. }
  629. void GateAttachmentsSanityCheck()
  630. {
  631. ConstructionPart wall_base_down = GetConstruction().GetConstructionPart("wall_base_down");
  632. ConstructionPart wall_base_up = GetConstruction().GetConstructionPart("wall_base_up");
  633. if(GetBarbedWire1() && !wall_base_down.IsBuilt())
  634. {
  635. HandleDropAttachment(GetBarbedWire1());
  636. HandleDropAttachment(GetCombinationLock());
  637. }
  638. if( ( GetCamoNet() || GetBarbedWire2() ) && !wall_base_up.IsBuilt() )
  639. {
  640. HandleDropAttachment(GetBarbedWire2());
  641. HandleDropAttachment(GetCamoNet());
  642. HandleDropAttachment(GetCombinationLock());
  643. }
  644. }
  645. void HandleDropAttachment(ItemBase item)
  646. {
  647. BarbedWire wire;
  648. CombinationLock lock;
  649. if (Class.CastTo(wire,item)) //special barbed wire beh.
  650. {
  651. wire.SetMountedState( false );
  652. GetInventory().DropEntity(InventoryMode.SERVER, this, wire);
  653. }
  654. else if (Class.CastTo(lock,item))
  655. {
  656. lock.UnlockServer(null,this);
  657. }
  658. else if (item)//generic behaviour
  659. {
  660. GetInventory().DropEntity(InventoryMode.SERVER, this, item);
  661. }
  662. }
  663. bool GateAttachmentConditions(int slotId)
  664. {
  665. if ( GetGateState() == GATE_STATE_PARTIAL )
  666. {
  667. ConstructionPart wall_base_down = GetConstruction().GetConstructionPart("wall_base_down");
  668. ConstructionPart wall_base_up = GetConstruction().GetConstructionPart("wall_base_up");
  669. string slot_name = InventorySlots.GetSlotName(slotId);
  670. if ( !wall_base_up.IsBuilt() )
  671. {
  672. if ( slot_name == ATTSLOT_CAMONET || slot_name == ATTSLOT_BARBEDWIRE_UP )
  673. {
  674. return false;
  675. }
  676. }
  677. if ( !wall_base_down.IsBuilt() )
  678. {
  679. if ( slot_name == ATTSLOT_BARBEDWIRE_DOWN )
  680. {
  681. return false;
  682. }
  683. }
  684. }
  685. return true;
  686. }
  687. //specific selection for camonet attaching (other ones might be animated via rotation!)
  688. override bool TranslateSlotFromSelection(string selection_name, out int slot_id)
  689. {
  690. if ( selection_name == "wall_camonet_attach" )
  691. {
  692. slot_id = InventorySlots.GetSlotIdFromString("Wall_Camonet");
  693. return true;
  694. }
  695. return false;
  696. }
  697. override void SetActions()
  698. {
  699. super.SetActions();
  700. AddAction(ActionTogglePlaceObject);
  701. AddAction(ActionPlaceObject);
  702. AddAction(ActionFoldBaseBuildingObject);
  703. //AddAction(ActionDialCombinationLockOnTarget);
  704. //AddAction(ActionNextCombinationLockDialOnTarget);
  705. AddAction(ActionOpenFence);
  706. AddAction(ActionCloseFence);
  707. }
  708. //================================================================
  709. // DEBUG
  710. //================================================================
  711. /*
  712. override void DebugCustomState()
  713. {
  714. //debug
  715. m_SyncParts01 = 881; //full fence with gate
  716. m_HasHinges = true;
  717. m_HasBase = true;
  718. OnVariablesSynchronized();
  719. }
  720. */
  721. //! Excludes certain parts from being built by OnDebugSpawn, uses Contains to compare
  722. override array<string> OnDebugSpawnBuildExcludes()
  723. {
  724. array<string> excludes = {};
  725. #ifdef DIAG_DEVELOPER
  726. bool bWood = DiagMenu.GetBool(DiagMenuIDs.BASEBUILDING_WOOD);
  727. #else
  728. bool bWood = false;
  729. #endif
  730. if (bWood)
  731. {
  732. excludes.Insert("_metal_");
  733. }
  734. else
  735. {
  736. excludes.Insert("_wood_");
  737. }
  738. #ifdef DIAG_DEVELOPER
  739. bool bGate = DiagMenu.GetBool(DiagMenuIDs.BASEBUILDING_GATE);
  740. #else
  741. bool bGate = false;
  742. #endif
  743. if (bGate)
  744. {
  745. excludes.Insert("platform");
  746. }
  747. else
  748. {
  749. excludes.Insert("gate");
  750. }
  751. return excludes;
  752. }
  753. //Debug menu Spawn Ground Special
  754. override void OnDebugSpawn()
  755. {
  756. super.OnDebugSpawn();
  757. GetInventory().CreateInInventory("CamoNet");
  758. for (int i = 0; i < 2; ++i)
  759. {
  760. BarbedWire wire = BarbedWire.Cast(GetInventory().CreateInInventory("BarbedWire"));
  761. wire.SetMountedState(true);
  762. }
  763. }
  764. }