hand_events.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. ///@{ events
  2. /**@enum HandEventID
  3. * @brief identifier for events mainly for rpc purposes
  4. **/
  5. enum HandEventID
  6. {
  7. UNKNOWN,
  8. TAKE,
  9. MOVETO,
  10. DROP,
  11. THROW,
  12. SWAP,
  13. FORCESWAP,
  14. DESTROY,
  15. CREATED,
  16. DESTROYED,
  17. REPLACE,
  18. REPLACE2,
  19. REPLACE3,
  20. REPLACED,
  21. HUMANCOMMAND_ACTION_FINISHED,
  22. HUMANCOMMAND_ACTION_ABORTED,
  23. ANIMEVENT_CHANGE_HIDE
  24. };
  25. enum JunctureRequestResult
  26. {
  27. JUNCTURE_NOT_REQUIRED,
  28. JUNCTURE_ACQUIRED,
  29. JUNCTURE_DENIED,
  30. ERROR,
  31. }
  32. /**@class HandEventBase
  33. * @brief represents event that triggers transition from state to state
  34. **/
  35. class HandEventBase
  36. {
  37. int m_EventID = 0;
  38. int m_AnimationID = -1;
  39. DayZPlayer m_Player;
  40. ref InventoryLocation m_Src;
  41. //! For action guards. Although we can guarantee the item will exist for remotes and juncture,
  42. //! we can't guarantee the conditions will be the same as when the action was first performed.
  43. //! So therefore we only perform maximal checks on the Client and then Server but then do minimal
  44. //! checks on Juncture-Client and Juncture-Server and no checks on Remote-Client.
  45. bool m_IsJuncture;
  46. bool m_IsRemote;
  47. void HandEventBase (Man p = null, InventoryLocation src = null) { Class.CastTo(m_Player, p); m_Src = src; }
  48. HandEventID GetEventID () { return m_EventID; }
  49. bool IsAuthoritative()
  50. {
  51. return m_Player && (m_Player.GetInstanceType() != DayZPlayerInstanceType.INSTANCETYPE_CLIENT && m_Player.GetInstanceType() != DayZPlayerInstanceType.INSTANCETYPE_REMOTE);
  52. }
  53. bool IsOwner()
  54. {
  55. return m_Player && (m_Player.GetInstanceType() == DayZPlayerInstanceType.INSTANCETYPE_CLIENT || m_Player.GetInstanceType() == DayZPlayerInstanceType.INSTANCETYPE_AI_SERVER || m_Player.GetInstanceType() == DayZPlayerInstanceType.INSTANCETYPE_AI_SINGLEPLAYER);
  56. }
  57. bool IsProxy()
  58. {
  59. return m_Player && (m_Player.GetInstanceType() == DayZPlayerInstanceType.INSTANCETYPE_REMOTE || m_Player.GetInstanceType() == DayZPlayerInstanceType.INSTANCETYPE_AI_REMOTE);
  60. }
  61. void ReadFromContext (ParamsReadContext ctx) { } // actual read is in CreateHandEventFromContext
  62. void WriteToContext (ParamsWriteContext ctx)
  63. {
  64. ctx.Write(m_EventID);
  65. ctx.Write(m_Player);
  66. OptionalLocationWriteToContext(m_Src, ctx);
  67. ctx.Write(m_AnimationID);
  68. }
  69. InventoryLocation GetSrc () { return m_Src; }
  70. EntityAI GetSrcEntity ()
  71. {
  72. if (m_Src)
  73. return m_Src.GetItem();
  74. return null;
  75. }
  76. EntityAI GetSecondSrcEntity () { return null; }
  77. InventoryLocation GetDst () { return null; }
  78. int GetAnimationID () { return m_AnimationID; }
  79. bool AcquireInventoryJunctureFromServer (notnull Man player) { return false; }
  80. bool CheckRequest () { return true; }
  81. bool CheckRequestEx (InventoryValidation validation)
  82. {
  83. //! Do not check for action validity on remotes or when performing through juncture.
  84. //! Juncture locks guarentee the item is safe to interact with and the server has validated the command at this point.
  85. //! Checking at this point is both wasteful and can result in a failure which leads to desync
  86. if (validation.m_IsRemote || validation.m_IsJuncture)
  87. return true;
  88. return CheckRequest();
  89. }
  90. bool CanPerformEvent () { return true; }
  91. bool CanPerformEventEx (InventoryValidation validation) { return CanPerformEvent(); }
  92. bool CheckRequestSrc () { return true; }
  93. bool IsServerSideOnly () { return false; }
  94. static HandEventBase HandEventFactory (HandEventID id, Man p = null, InventoryLocation src = null)
  95. {
  96. switch (id)
  97. {
  98. case HandEventID.UNKNOWN: return null;
  99. case HandEventID.TAKE: return new HandEventTake(p, src);
  100. case HandEventID.MOVETO: return new HandEventMoveTo(p, src);
  101. case HandEventID.DROP: return new HandEventDrop(p, src);
  102. case HandEventID.THROW: return new HandEventThrow(p, src);
  103. case HandEventID.SWAP: return new HandEventSwap(p, src);
  104. case HandEventID.FORCESWAP: return new HandEventForceSwap(p, src);
  105. case HandEventID.DESTROY: return new HandEventDestroy(p, src);
  106. case HandEventID.CREATED: return new HandEventCreated(p, src);
  107. case HandEventID.DESTROYED: return new HandEventDestroyed(p, src);
  108. case HandEventID.REPLACE: return new HandEventDestroyAndReplaceWithNew(p, src);
  109. case HandEventID.REPLACE2: return new HandEventDestroyAndReplaceWithNewElsewhere(p, src);
  110. case HandEventID.REPLACE3: return new HandEventDestroyElsewhereAndReplaceWithNewInHands(p, src);
  111. case HandEventID.REPLACED: return new HandEventReplaced(p, src);
  112. case HandEventID.ANIMEVENT_CHANGE_HIDE: return HandAnimEventChanged(p, src);
  113. case HandEventID.HUMANCOMMAND_ACTION_FINISHED : return HandEventHumanCommandActionFinished(p, src);
  114. case HandEventID.HUMANCOMMAND_ACTION_ABORTED : return HandEventHumanCommandActionAborted(p, src);
  115. }
  116. Error("[hndfsm] HandEventFactory - unregistered hand event with id=" + id);
  117. return null;
  118. }
  119. static HandEventBase CreateHandEventFromContext (ParamsReadContext ctx)
  120. {
  121. int eventID = -1;
  122. if (!ctx.Read(eventID))
  123. {
  124. Error("[hndfsm] CreateHandEventFromContext - cannot read eventID");
  125. return null;
  126. }
  127. Man player;
  128. if (!ctx.Read(player))
  129. {
  130. Error("[hndfsm] CreateHandEventFromContext - cannot read player");
  131. return null;
  132. }
  133. InventoryLocation src;
  134. OptionalLocationReadFromContext(src, ctx);
  135. int animID = -1;
  136. if (!ctx.Read(animID))
  137. {
  138. Error("[hndfsm] CreateHandEventFromContext - cannot read animID");
  139. return null;
  140. }
  141. HandEventBase b = HandEventFactory(eventID, player, src);
  142. if (b)
  143. {
  144. b.m_AnimationID = animID;
  145. b.ReadFromContext(ctx);
  146. }
  147. return b;
  148. }
  149. string DumpToString ()
  150. {
  151. string res = "{ HandEv id=" + typename.EnumToString(HandEventID, GetEventID());
  152. res = res + " pl=" + Object.GetDebugName(m_Player);
  153. res = res + " src=" + InventoryLocation.DumpToStringNullSafe(m_Src);
  154. res = res + " }";
  155. return res;
  156. }
  157. bool ReserveInventory()
  158. {
  159. InventoryLocation il = GetDst();
  160. if (!il)
  161. {
  162. il = GetSrc();
  163. }
  164. if (il && !m_Player.GetHumanInventory().HasInventoryReservation(il.GetItem(),il))
  165. {
  166. if (m_Player.GetHumanInventory().AddInventoryReservationEx(il.GetItem(), il, GameInventory.c_InventoryReservationTimeoutShortMS))
  167. {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. void ClearInventoryReservation()
  174. {
  175. InventoryLocation il = GetDst();
  176. if (il)
  177. {
  178. il = GetSrc();
  179. }
  180. if (il)
  181. {
  182. m_Player.GetHumanInventory().ClearInventoryReservationEx(il.GetItem(), il);
  183. }
  184. }
  185. };
  186. class HandEventTake extends HandEventBase
  187. {
  188. void HandEventTake (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.TAKE; }
  189. override InventoryLocation GetDst ()
  190. {
  191. InventoryLocation dst = new InventoryLocation;
  192. dst.SetHands(m_Player, GetSrcEntity());
  193. return dst;
  194. }
  195. override bool CheckRequestSrc ()
  196. {
  197. if (!GameInventory.CheckRequestSrc(m_Player, GetSrc(), GameInventory.c_MaxItemDistanceRadius))
  198. {
  199. Debug.InventoryHFSMLog("CANNOT perform", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CheckRequestSrc", m_Player.ToString() );
  200. if (LogManager.IsSyncLogEnable()) syncDebugPrint("[cheat] HandleInputData man=" + Object.GetDebugName(m_Player) + " failed src1 check with cmd=" + typename.EnumToString(HandEventID, GetEventID()) + " src1=" + InventoryLocation.DumpToStringNullSafe(GetSrc()));
  201. return false; // stale packet
  202. }
  203. return true;
  204. }
  205. override bool CheckRequest ()
  206. {
  207. return GameInventory.CheckMoveToDstRequest(m_Player, GetSrc(), GetDst(), GameInventory.c_MaxItemDistanceRadius);
  208. }
  209. override bool CanPerformEventEx(InventoryValidation validation)
  210. {
  211. if (validation.m_IsJuncture)
  212. {
  213. return true;
  214. }
  215. if (!GameInventory.LocationCanMoveEntity(GetSrc(), GetDst()))
  216. {
  217. #ifdef ENABLE_LOGGING
  218. if ( LogManager.IsInventoryHFSMLogEnable() )
  219. {
  220. Debug.InventoryHFSMLog("CANNOT perform", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  221. }
  222. #endif
  223. //if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[desync] HandleInputData man=" + Object.GetDebugName(m_Player) + " CANNOT perform ev=" + DumpToString());
  224. return false;
  225. }
  226. return super.CanPerformEventEx(validation);
  227. }
  228. override bool AcquireInventoryJunctureFromServer (notnull Man player)
  229. {
  230. InventoryLocation src = GetSrc();
  231. InventoryLocation dst = GetDst();
  232. if (src && dst)
  233. {
  234. return TryAcquireInventoryJunctureFromServer(player, src, dst);
  235. }
  236. Error("[hndfsm] HandEventTake. AcquireInventoryJunctureFromServer: no src or dst for ev=" + DumpToString());
  237. return JunctureRequestResult.ERROR;
  238. }
  239. };
  240. class HandEventMoveTo extends HandEventBase
  241. {
  242. ref InventoryLocation m_Dst; /// destination for item in hands
  243. void HandEventMoveTo (Man p = null, InventoryLocation src = null, InventoryLocation dst = null) { m_EventID = HandEventID.MOVETO; m_Dst = dst; }
  244. override void ReadFromContext (ParamsReadContext ctx)
  245. {
  246. m_Dst = new InventoryLocation;
  247. super.ReadFromContext(ctx);
  248. m_Dst.ReadFromContext(ctx);
  249. }
  250. override void WriteToContext (ParamsWriteContext ctx)
  251. {
  252. super.WriteToContext(ctx);
  253. m_Dst.WriteToContext(ctx);
  254. }
  255. override InventoryLocation GetDst () { return m_Dst; }
  256. override bool CheckRequestSrc ()
  257. {
  258. if (!GameInventory.CheckRequestSrc(m_Player, GetSrc(), GameInventory.c_MaxItemDistanceRadius))
  259. {
  260. if (LogManager.IsSyncLogEnable()) syncDebugPrint("[cheat] HandleInputData man=" + Object.GetDebugName(m_Player) + " failed src1 check with cmd=" + typename.EnumToString(HandEventID, GetEventID()) + " src1=" + InventoryLocation.DumpToStringNullSafe(GetSrc()));
  261. return false; // stale packet
  262. }
  263. return true;
  264. }
  265. override bool CheckRequest ()
  266. {
  267. return GameInventory.CheckMoveToDstRequest(m_Player, GetSrc(), GetDst(), GameInventory.c_MaxItemDistanceRadius);
  268. }
  269. override bool CanPerformEvent ()
  270. {
  271. if (!GameInventory.LocationCanMoveEntity(GetSrc(), GetDst()))
  272. {
  273. #ifdef ENABLE_LOGGING
  274. if ( LogManager.IsInventoryHFSMLogEnable() )
  275. {
  276. Debug.InventoryHFSMLog("CANNOT perform", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  277. }
  278. #endif
  279. return false;
  280. }
  281. return true;
  282. }
  283. override bool AcquireInventoryJunctureFromServer (notnull Man player)
  284. {
  285. InventoryLocation src = GetSrc();
  286. InventoryLocation dst = GetDst();
  287. if (src && dst)
  288. {
  289. return TryAcquireInventoryJunctureFromServer(player, src, dst);
  290. }
  291. Error("[hndfsm] HandEventMoveTo. AcquireInventoryJunctureFromServer: no src or dst for ev=" + DumpToString());
  292. return JunctureRequestResult.ERROR;
  293. }
  294. override string DumpToString ()
  295. {
  296. string res = "{ HandEventMoveTo id=" + typename.EnumToString(HandEventID, GetEventID()) + " pl=" + Object.GetDebugName(m_Player) + " src=" + InventoryLocation.DumpToStringNullSafe(m_Src) + " dst=" + InventoryLocation.DumpToStringNullSafe(m_Dst) + " }";
  297. return res;
  298. }
  299. };
  300. //! Abstracted event, not to be used, only inherited
  301. class HandEventRemove extends HandEventBase
  302. {
  303. override InventoryLocation GetDst ()
  304. {
  305. return m_Dst;
  306. }
  307. override bool CheckRequestSrc ()
  308. {
  309. if (!GameInventory.CheckRequestSrc(m_Player, GetSrc(), GameInventory.c_MaxItemDistanceRadius))
  310. {
  311. #ifdef ENABLE_LOGGING
  312. if ( LogManager.IsInventoryHFSMLogEnable() )
  313. {
  314. Debug.InventoryHFSMLog("Check src - failed, src = " + InventoryLocation.DumpToStringNullSafe(GetSrc()), typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CheckRequestSrc", m_Player.ToString() );
  315. }
  316. #endif
  317. return false; // stale packet
  318. }
  319. return true;
  320. }
  321. override bool CheckRequest ()
  322. {
  323. return GameInventory.CheckMoveToDstRequest(m_Player, GetSrc(), GetDst(), GameInventory.c_MaxItemDistanceRadius);
  324. }
  325. override bool CanPerformEvent ()
  326. {
  327. InventoryLocation src = GetSrc();
  328. InventoryLocation dst = GetDst();
  329. if (!dst)
  330. {
  331. #ifdef ENABLE_LOGGING
  332. if ( LogManager.IsInventoryHFSMLogEnable() )
  333. {
  334. Debug.InventoryHFSMLog("CANNOT perform. Dst location is NULL!", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  335. }
  336. #endif
  337. return false;
  338. }
  339. if (dst.GetType() == InventoryLocationType.UNKNOWN)
  340. {
  341. #ifdef ENABLE_LOGGING
  342. if ( LogManager.IsInventoryHFSMLogEnable() )
  343. {
  344. Debug.InventoryHFSMLog("CANNOT perform. Dst location type is UNKNOWN!", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  345. }
  346. #endif
  347. return false;
  348. }
  349. if (!GameInventory.LocationCanMoveEntity(src, dst))
  350. {
  351. #ifdef ENABLE_LOGGING
  352. if ( LogManager.IsInventoryHFSMLogEnable() )
  353. {
  354. Debug.InventoryHFSMLog("CANNOT perform", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  355. }
  356. #endif
  357. return false;
  358. }
  359. return true;
  360. }
  361. override bool AcquireInventoryJunctureFromServer (notnull Man player)
  362. {
  363. InventoryLocation src = GetSrc();
  364. InventoryLocation dst = GetDst();
  365. if (src && dst)
  366. {
  367. return TryAcquireInventoryJunctureFromServer(player, src, dst);
  368. }
  369. Error("[hndfsm] HandEventThrow. AcquireInventoryJunctureFromServer: no src or dst for ev=" + DumpToString());
  370. return JunctureRequestResult.ERROR;
  371. }
  372. ref InventoryLocation m_Dst;
  373. };
  374. class HandEventDrop extends HandEventRemove
  375. {
  376. void HandEventDrop (Man p = null, InventoryLocation src = null)
  377. {
  378. m_EventID = HandEventID.DROP;
  379. m_CanPerformDrop = true;
  380. }
  381. override void ReadFromContext(ParamsReadContext ctx)
  382. {
  383. super.ReadFromContext(ctx);
  384. if (!m_Dst)
  385. {
  386. m_Dst = new InventoryLocation();
  387. }
  388. ctx.Read(m_CanPerformDrop);
  389. OptionalLocationReadFromContext(m_Dst, ctx);
  390. }
  391. override void WriteToContext(ParamsWriteContext ctx)
  392. {
  393. super.WriteToContext(ctx);
  394. ctx.Write(m_CanPerformDrop);
  395. OptionalLocationWriteToContext(m_Dst, ctx);
  396. }
  397. override bool CheckRequestEx(InventoryValidation validation)
  398. {
  399. //! Check to see if this is the initial call from the server (but the event originated from a client)
  400. if (!validation.m_IsJuncture && IsAuthoritative())
  401. {
  402. m_CanPerformDrop = GameInventory.SetGroundPosByOwner(m_Player, GetSrcEntity(), m_Dst);
  403. }
  404. if (!m_CanPerformDrop)
  405. {
  406. validation.m_Reason = InventoryValidationReason.DROP_PREVENTED;
  407. return false;
  408. }
  409. return super.CheckRequestEx(validation);
  410. }
  411. override bool CanPerformEventEx(InventoryValidation validation)
  412. {
  413. if (!m_CanPerformDrop)
  414. {
  415. return false;
  416. }
  417. //! On multiplayer client, if this is the initial call then we are waiting for the server to setup this event still
  418. if (!validation.m_IsJuncture && !validation.m_IsRemote && !GetDst() && (GetGame().IsMultiplayer() && GetGame().IsClient()))
  419. {
  420. return true;
  421. }
  422. //! Singleplayer or server was initial caller
  423. if (!validation.m_IsRemote && !GetDst())
  424. {
  425. m_Dst = new InventoryLocation();
  426. m_CanPerformDrop = GameInventory.SetGroundPosByOwner(m_Player, GetSrcEntity(), m_Dst);
  427. if (!m_CanPerformDrop)
  428. {
  429. validation.m_Reason = InventoryValidationReason.DROP_PREVENTED;
  430. return false;
  431. }
  432. }
  433. return super.CanPerformEventEx(validation);
  434. }
  435. bool m_CanPerformDrop;
  436. };
  437. class HandEventThrow extends HandEventRemove
  438. {
  439. void HandEventThrow (Man p = null, InventoryLocation src = null)
  440. {
  441. m_EventID = HandEventID.THROW;
  442. if( src )
  443. {
  444. vector mat[4];
  445. EntityAI entity = GetSrcEntity();
  446. if (entity)
  447. {
  448. m_Dst = new InventoryLocation;
  449. entity.GetTransform(mat);
  450. m_Dst.SetGround(entity, mat);
  451. }
  452. else
  453. {
  454. Error("[hndfsm] HandEventThrow src entity null!");
  455. }
  456. }
  457. }
  458. override void ReadFromContext (ParamsReadContext ctx)
  459. {
  460. super.ReadFromContext(ctx);
  461. m_Dst = new InventoryLocation;
  462. m_Dst.ReadFromContext(ctx);
  463. float x, y, z;
  464. ctx.Read(x);
  465. ctx.Read(y);
  466. ctx.Read(z);
  467. m_Force[0] = x;
  468. m_Force[1] = y;
  469. m_Force[2] = z;
  470. }
  471. override void WriteToContext (ParamsWriteContext ctx)
  472. {
  473. super.WriteToContext(ctx);
  474. m_Dst.WriteToContext(ctx);
  475. ctx.Write(m_Force[0]);
  476. ctx.Write(m_Force[1]);
  477. ctx.Write(m_Force[2]);
  478. }
  479. void SetForce(vector force) { m_Force = force; }
  480. vector GetForce() { return m_Force; }
  481. vector m_Force;
  482. };
  483. class HandEventSwap extends HandEventBase
  484. {
  485. // super.m_Src == src location of new item (that will be taken)
  486. ref InventoryLocation m_Dst; /// destination for new item (i.e. hands)
  487. ref InventoryLocation m_Src2; /// src of old item in hands
  488. ref InventoryLocation m_Dst2; /// destination for old item that was in hands
  489. int m_Animation2ID = -1;
  490. void HandEventSwap (Man p = null, InventoryLocation src = null, InventoryLocation src2 = null, InventoryLocation dst = null, InventoryLocation dst2 = null)
  491. {
  492. m_EventID = HandEventID.SWAP;
  493. m_Dst = dst;
  494. m_Src2 = src2;
  495. m_Dst2 = dst2;
  496. }
  497. override void ReadFromContext (ParamsReadContext ctx)
  498. {
  499. super.ReadFromContext(ctx);
  500. m_Src2 = new InventoryLocation;
  501. m_Dst = new InventoryLocation;
  502. m_Dst2 = new InventoryLocation;
  503. m_Src2.ReadFromContext(ctx);
  504. m_Dst.ReadFromContext(ctx);
  505. m_Dst2.ReadFromContext(ctx);
  506. ctx.Read(m_Animation2ID);
  507. }
  508. override void WriteToContext (ParamsWriteContext ctx)
  509. {
  510. super.WriteToContext(ctx);
  511. m_Src2.WriteToContext(ctx);
  512. m_Dst.WriteToContext(ctx);
  513. m_Dst2.WriteToContext(ctx);
  514. ctx.Write(m_Animation2ID);
  515. }
  516. override InventoryLocation GetDst ()
  517. {
  518. return m_Dst;
  519. }
  520. override EntityAI GetSecondSrcEntity()
  521. {
  522. return m_Src2.GetItem();
  523. }
  524. override bool CheckRequestSrc ()
  525. {
  526. //return false;
  527. if (!GameInventory.CheckRequestSrc(m_Player, GetSrc(), GameInventory.c_MaxItemDistanceRadius))
  528. {
  529. if (LogManager.IsSyncLogEnable()) syncDebugPrint("[cheat] HandleInputData man=" + Object.GetDebugName(m_Player) + " failed src1 check with cmd=" + typename.EnumToString(HandEventID, GetEventID()) + " src1=" + InventoryLocation.DumpToStringNullSafe(GetSrc()));
  530. return false; // stale packet
  531. }
  532. if (!GameInventory.CheckRequestSrc(m_Player, m_Src2, GameInventory.c_MaxItemDistanceRadius))
  533. {
  534. if (LogManager.IsSyncLogEnable()) syncDebugPrint("[cheat] HandleInputData man=" + Object.GetDebugName(m_Player) + " failed src2 check with cmd=" + typename.EnumToString(HandEventID, GetEventID()) + " src2=" + InventoryLocation.DumpToStringNullSafe(m_Src2));
  535. return false; // stale packet
  536. }
  537. return true;
  538. }
  539. override bool CheckRequest ()
  540. {
  541. if (!GameInventory.CheckSwapItemsRequest(m_Player, m_Src, m_Src2, m_Dst, m_Dst2, GameInventory.c_MaxItemDistanceRadius))
  542. {
  543. #ifdef ENABLE_LOGGING
  544. if ( LogManager.IsInventoryHFSMLogEnable() )
  545. {
  546. Debug.InventoryHFSMLog("CheckSwapItemsRequest - failed", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CheckRequest", m_Player.ToString() );
  547. }
  548. #endif
  549. }
  550. else if (!m_Player.GetHumanInventory().CanAddSwappedEntity(m_Src, m_Src2, m_Dst, m_Dst2))
  551. {
  552. #ifdef ENABLE_LOGGING
  553. if ( LogManager.IsInventoryHFSMLogEnable() )
  554. {
  555. Debug.InventoryHFSMLog("CanAddSwappedEntity - failed", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CheckRequest", m_Player.ToString() );
  556. }
  557. #endif
  558. }
  559. else
  560. return true;
  561. return false;
  562. }
  563. override bool CanPerformEvent ()
  564. {
  565. if (GameInventory.CanForceSwapEntitiesEx(GetSrc().GetItem(), m_Dst, m_Src2.GetItem(), m_Dst2))
  566. return true;
  567. #ifdef ENABLE_LOGGING
  568. if ( LogManager.IsInventoryHFSMLogEnable() )
  569. {
  570. Debug.InventoryHFSMLog("CANNOT perform", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  571. }
  572. #endif
  573. return false;
  574. }
  575. override bool AcquireInventoryJunctureFromServer (notnull Man player)
  576. {
  577. return TryAcquireTwoInventoryJuncturesFromServer(player, m_Src, m_Src2, m_Dst, m_Dst2);
  578. }
  579. override string DumpToString ()
  580. {
  581. return "{ HandEventSwap id=" + typename.EnumToString(HandEventID, GetEventID()) + " pl=" + Object.GetDebugName(m_Player) + " src1=" + InventoryLocation.DumpToStringNullSafe(m_Src) + " src2=" + InventoryLocation.DumpToStringNullSafe(m_Src2) + " dst1=" + InventoryLocation.DumpToStringNullSafe(m_Dst) + " dst2=" + InventoryLocation.DumpToStringNullSafe(m_Dst2) + " }";
  582. }
  583. void CheckAndExecuteForceStandUp()
  584. {
  585. DayZPlayer player = DayZPlayer.Cast(m_Player);
  586. player.ForceStandUpForHeavyItemsSwap( m_Src.GetItem(), m_Src2.GetItem() );
  587. }
  588. override bool ReserveInventory()
  589. {
  590. if (!m_Player.GetHumanInventory().HasInventoryReservation(m_Dst.GetItem(), m_Dst) && !m_Player.GetHumanInventory().HasInventoryReservation(m_Dst2.GetItem(), m_Dst2))
  591. {
  592. if (m_Player.GetHumanInventory().AddInventoryReservationEx(m_Dst.GetItem(), m_Dst, GameInventory.c_InventoryReservationTimeoutShortMS))
  593. {
  594. if (m_Player.GetHumanInventory().AddInventoryReservationEx(m_Dst2.GetItem(), m_Dst2, GameInventory.c_InventoryReservationTimeoutShortMS))
  595. {
  596. return true;
  597. }
  598. else
  599. {
  600. m_Player.GetHumanInventory().ClearInventoryReservationEx(m_Dst.GetItem(), m_Dst);
  601. }
  602. }
  603. }
  604. return false;
  605. }
  606. override void ClearInventoryReservation()
  607. {
  608. m_Player.GetHumanInventory().ClearInventoryReservationEx(m_Dst.GetItem(), m_Dst);
  609. m_Player.GetHumanInventory().ClearInventoryReservationEx(m_Dst2.GetItem(), m_Dst2);
  610. }
  611. };
  612. class HandEventForceSwap extends HandEventSwap
  613. {
  614. void HandEventForceSwap (Man p = null, InventoryLocation src = null, InventoryLocation src2 = null, InventoryLocation dst = null, InventoryLocation dst2 = null) { m_EventID = HandEventID.FORCESWAP; }
  615. override bool CheckRequest ()
  616. {
  617. bool test1 = false;
  618. EntityAI inHands = m_Player.GetHumanInventory().GetEntityInHands();
  619. if (GetSrcEntity() && inHands && m_Dst && m_Dst.IsValid())
  620. {
  621. test1 = GameInventory.CheckSwapItemsRequest(m_Player, m_Src, m_Src2, m_Dst, m_Dst2, GameInventory.c_MaxItemDistanceRadius);
  622. #ifdef ENABLE_LOGGING
  623. if ( LogManager.IsInventoryHFSMLogEnable() )
  624. {
  625. if (!test1)
  626. Debug.InventoryHFSMLog("CheckSwapItemsRequest failed", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CheckRequest", m_Player.ToString() );
  627. }
  628. #endif
  629. }
  630. return test1;
  631. }
  632. override bool CanPerformEvent ()
  633. {
  634. bool test2 = GameInventory.CanForceSwapEntitiesEx(m_Src.GetItem(), m_Dst, m_Src2.GetItem(), m_Dst2); // null here means 'do not search for dst2' (already have valid one from constructor)
  635. #ifdef ENABLE_LOGGING
  636. if ( LogManager.IsInventoryHFSMLogEnable() )
  637. {
  638. if (!test2)
  639. Debug.InventoryHFSMLog("CanForceSwapEntitiesEx failed", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  640. }
  641. #endif
  642. return test2;
  643. }
  644. };
  645. class HandEventDestroy extends HandEventBase
  646. {
  647. void HandEventDestroy (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.DESTROY; }
  648. override bool IsServerSideOnly () { return true; }
  649. };
  650. class HandEventCreated extends HandEventBase
  651. {
  652. void HandEventCreated (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.CREATED; }
  653. };
  654. class HandEventDestroyed extends HandEventBase
  655. {
  656. void HandEventDestroyed (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.DESTROYED; }
  657. };
  658. class HandEventReplaceWithNewBase extends HandEventBase
  659. {
  660. string m_Type;
  661. ref ReplaceItemWithNewLambdaBase m_Lambda;
  662. void HandEvengReplaceWithNewBase (Man p = null, InventoryLocation src = null, ReplaceItemWithNewLambdaBase lambda = NULL) { m_EventID = HandEventID.REPLACE; m_Lambda = lambda; }
  663. override void ReadFromContext (ParamsReadContext ctx)
  664. {
  665. super.ReadFromContext(ctx);
  666. ctx.Read(m_Type);
  667. Error("[hndfsm] HandEventDestroyAndReplaceWithNew - Cannot serialize lambda (read)");
  668. }
  669. override void WriteToContext (ParamsWriteContext ctx)
  670. {
  671. super.WriteToContext(ctx);
  672. ctx.Write(m_Type);
  673. Error("[hndfsm] HandEventDestroyAndReplaceWithNew - Cannot serialize lambda (write)");
  674. }
  675. override bool IsServerSideOnly () { return true; }
  676. override InventoryLocation GetDst ()
  677. {
  678. InventoryLocation dst = new InventoryLocation;
  679. dst.SetHands(m_Player, GetSrcEntity());
  680. return dst;
  681. }
  682. override string DumpToString ()
  683. {
  684. string res = "{ HandEvenReplaceWithNewBase id=" + typename.EnumToString(HandEventID, GetEventID()) + " pl=" + Object.GetDebugName(m_Player) + " src=" + InventoryLocation.DumpToStringNullSafe(GetSrc()) + " lambda=" + m_Lambda + " dst=" + InventoryLocation.DumpToStringNullSafe(GetDst()) + " }";
  685. return res;
  686. }
  687. };
  688. class HandEventDestroyAndReplaceWithNew extends HandEventReplaceWithNewBase
  689. {
  690. void HandEventDestroyAndReplaceWithNew (Man p = null, InventoryLocation src = null, ReplaceItemWithNewLambdaBase lambda = NULL) { m_EventID = HandEventID.REPLACE; m_Lambda = lambda; }
  691. };
  692. class HandEventDestroyAndReplaceWithNewElsewhere extends HandEventDestroyAndReplaceWithNew
  693. {
  694. void HandEventDestroyAndReplaceWithNewElsewhere (Man p = null, InventoryLocation src = null, ReplaceItemWithNewLambdaBase lambda = NULL) { m_EventID = HandEventID.REPLACE3; m_Lambda = lambda; }
  695. };
  696. class HandEventDestroyElsewhereAndReplaceWithNewInHands extends HandEventReplaceWithNewBase
  697. {
  698. void HandEventDestroyElsewhereAndReplaceWithNewInHands (Man p = null, InventoryLocation src = null, ReplaceItemWithNewLambdaBase lambda = NULL) { m_EventID = HandEventID.REPLACE3; m_Lambda = lambda; }
  699. };
  700. class HandEventReplaced extends HandEventBase
  701. {
  702. void HandEventReplaced (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.REPLACED; }
  703. };
  704. // anim events
  705. class HandAnimEventChanged extends HandEventBase
  706. {
  707. void HandAnimEventChanged (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.ANIMEVENT_CHANGE_HIDE; }
  708. };
  709. HandEventBase HandAnimEventFactory (WeaponEvents type, Man p = null, InventoryLocation src = null)
  710. {
  711. switch (type)
  712. {
  713. case WeaponEvents.CHANGE_HIDE: return new HandAnimEventChanged(p, src);
  714. case WeaponEvents.CHANGE_SHOW: return new HandAnimEventChanged(p, src);
  715. }
  716. return null;
  717. }
  718. /**@brief triggered when animation action finishes
  719. **/
  720. class HandEventHumanCommandActionFinished extends HandEventBase
  721. {
  722. void HandEventHumanCommandActionFinished (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.HUMANCOMMAND_ACTION_FINISHED; }
  723. };
  724. /**@brief triggered when animation action aborts
  725. **/
  726. class HandEventHumanCommandActionAborted extends HandEventBase
  727. {
  728. void HandEventHumanCommandActionAborted (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.HUMANCOMMAND_ACTION_ABORTED; }
  729. };
  730. ///@} events