hand_events.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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 (false == 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 (false == 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 (false == 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 (false == 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. if (false == GameInventory.LocationCanMoveEntity(GetSrc(), GetDst()))
  328. {
  329. #ifdef ENABLE_LOGGING
  330. if ( LogManager.IsInventoryHFSMLogEnable() )
  331. {
  332. Debug.InventoryHFSMLog("CANNOT perform", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  333. }
  334. #endif
  335. return false;
  336. }
  337. return true;
  338. }
  339. override bool AcquireInventoryJunctureFromServer (notnull Man player)
  340. {
  341. InventoryLocation src = GetSrc();
  342. InventoryLocation dst = GetDst();
  343. if (src && dst)
  344. {
  345. return TryAcquireInventoryJunctureFromServer(player, src, dst);
  346. }
  347. Error("[hndfsm] HandEventThrow. AcquireInventoryJunctureFromServer: no src or dst for ev=" + DumpToString());
  348. return JunctureRequestResult.ERROR;
  349. }
  350. ref InventoryLocation m_Dst;
  351. };
  352. class HandEventDrop extends HandEventRemove
  353. {
  354. void HandEventDrop (Man p = null, InventoryLocation src = null)
  355. {
  356. m_EventID = HandEventID.DROP;
  357. m_CanPerformDrop = true;
  358. }
  359. override void ReadFromContext(ParamsReadContext ctx)
  360. {
  361. super.ReadFromContext(ctx);
  362. ctx.Read(m_CanPerformDrop);
  363. OptionalLocationReadFromContext(m_Dst, ctx);
  364. if (!m_Dst)
  365. {
  366. m_Dst = new InventoryLocation();
  367. }
  368. }
  369. override void WriteToContext(ParamsWriteContext ctx)
  370. {
  371. super.WriteToContext(ctx);
  372. ctx.Write(m_CanPerformDrop);
  373. OptionalLocationWriteToContext(m_Dst, ctx);
  374. }
  375. override bool CheckRequestEx(InventoryValidation validation)
  376. {
  377. //! Check to see if this is the initial call from the server (but the event originated from a client)
  378. if (!validation.m_IsJuncture && IsAuthoritative())
  379. {
  380. m_CanPerformDrop = GameInventory.SetGroundPosByOwner(m_Player, GetSrcEntity(), m_Dst);
  381. }
  382. if (!m_CanPerformDrop)
  383. {
  384. validation.m_Reason = InventoryValidationReason.DROP_PREVENTED;
  385. return false;
  386. }
  387. return super.CheckRequestEx(validation);
  388. }
  389. override bool CanPerformEventEx(InventoryValidation validation)
  390. {
  391. if (!m_CanPerformDrop)
  392. {
  393. return false;
  394. }
  395. //! On multiplayer client, if this is the initial call then we are waiting for the server to setup this event still
  396. if (!validation.m_IsJuncture && !validation.m_IsRemote && !GetDst() && (GetGame().IsMultiplayer() && GetGame().IsClient()))
  397. {
  398. return true;
  399. }
  400. //! Singleplayer or server was initial caller
  401. if (!validation.m_IsRemote && !GetDst())
  402. {
  403. m_Dst = new InventoryLocation();
  404. m_CanPerformDrop = GameInventory.SetGroundPosByOwner(m_Player, GetSrcEntity(), m_Dst);
  405. if (!m_CanPerformDrop)
  406. {
  407. validation.m_Reason = InventoryValidationReason.DROP_PREVENTED;
  408. return false;
  409. }
  410. }
  411. return super.CanPerformEventEx(validation);
  412. }
  413. bool m_CanPerformDrop;
  414. };
  415. class HandEventThrow extends HandEventRemove
  416. {
  417. void HandEventThrow (Man p = null, InventoryLocation src = null)
  418. {
  419. m_EventID = HandEventID.THROW;
  420. if( src )
  421. {
  422. vector mat[4];
  423. EntityAI entity = GetSrcEntity();
  424. if (entity)
  425. {
  426. m_Dst = new InventoryLocation;
  427. entity.GetTransform(mat);
  428. m_Dst.SetGround(entity, mat);
  429. }
  430. else
  431. {
  432. Error("[hndfsm] HandEventThrow src entity null!");
  433. }
  434. }
  435. }
  436. override void ReadFromContext (ParamsReadContext ctx)
  437. {
  438. super.ReadFromContext(ctx);
  439. m_Dst = new InventoryLocation;
  440. m_Dst.ReadFromContext(ctx);
  441. float x, y, z;
  442. ctx.Read(x);
  443. ctx.Read(y);
  444. ctx.Read(z);
  445. m_Force[0] = x;
  446. m_Force[1] = y;
  447. m_Force[2] = z;
  448. }
  449. override void WriteToContext (ParamsWriteContext ctx)
  450. {
  451. super.WriteToContext(ctx);
  452. m_Dst.WriteToContext(ctx);
  453. ctx.Write(m_Force[0]);
  454. ctx.Write(m_Force[1]);
  455. ctx.Write(m_Force[2]);
  456. }
  457. void SetForce(vector force) { m_Force = force; }
  458. vector GetForce() { return m_Force; }
  459. vector m_Force;
  460. };
  461. class HandEventSwap extends HandEventBase
  462. {
  463. // super.m_Src == src location of new item (that will be taken)
  464. ref InventoryLocation m_Dst; /// destination for new item (i.e. hands)
  465. ref InventoryLocation m_Src2; /// src of old item in hands
  466. ref InventoryLocation m_Dst2; /// destination for old item that was in hands
  467. int m_Animation2ID = -1;
  468. void HandEventSwap (Man p = null, InventoryLocation src = null, InventoryLocation src2 = null, InventoryLocation dst = null, InventoryLocation dst2 = null)
  469. {
  470. m_EventID = HandEventID.SWAP;
  471. m_Dst = dst;
  472. m_Src2 = src2;
  473. m_Dst2 = dst2;
  474. }
  475. override void ReadFromContext (ParamsReadContext ctx)
  476. {
  477. super.ReadFromContext(ctx);
  478. m_Src2 = new InventoryLocation;
  479. m_Dst = new InventoryLocation;
  480. m_Dst2 = new InventoryLocation;
  481. m_Src2.ReadFromContext(ctx);
  482. m_Dst.ReadFromContext(ctx);
  483. m_Dst2.ReadFromContext(ctx);
  484. ctx.Read(m_Animation2ID);
  485. }
  486. override void WriteToContext (ParamsWriteContext ctx)
  487. {
  488. super.WriteToContext(ctx);
  489. m_Src2.WriteToContext(ctx);
  490. m_Dst.WriteToContext(ctx);
  491. m_Dst2.WriteToContext(ctx);
  492. ctx.Write(m_Animation2ID);
  493. }
  494. override InventoryLocation GetDst ()
  495. {
  496. return m_Dst;
  497. }
  498. override EntityAI GetSecondSrcEntity()
  499. {
  500. return m_Src2.GetItem();
  501. }
  502. override bool CheckRequestSrc ()
  503. {
  504. //return false;
  505. if (false == GameInventory.CheckRequestSrc(m_Player, GetSrc(), GameInventory.c_MaxItemDistanceRadius))
  506. {
  507. if (LogManager.IsSyncLogEnable()) syncDebugPrint("[cheat] HandleInputData man=" + Object.GetDebugName(m_Player) + " failed src1 check with cmd=" + typename.EnumToString(HandEventID, GetEventID()) + " src1=" + InventoryLocation.DumpToStringNullSafe(GetSrc()));
  508. return false; // stale packet
  509. }
  510. if (false == GameInventory.CheckRequestSrc(m_Player, m_Src2, GameInventory.c_MaxItemDistanceRadius))
  511. {
  512. 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));
  513. return false; // stale packet
  514. }
  515. return true;
  516. }
  517. override bool CheckRequest ()
  518. {
  519. if (!GameInventory.CheckSwapItemsRequest(m_Player, m_Src, m_Src2, m_Dst, m_Dst2, GameInventory.c_MaxItemDistanceRadius))
  520. {
  521. #ifdef ENABLE_LOGGING
  522. if ( LogManager.IsInventoryHFSMLogEnable() )
  523. {
  524. Debug.InventoryHFSMLog("CheckSwapItemsRequest - failed", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CheckRequest", m_Player.ToString() );
  525. }
  526. #endif
  527. }
  528. else if (!m_Player.GetHumanInventory().CanAddSwappedEntity(m_Src, m_Src2, m_Dst, m_Dst2))
  529. {
  530. #ifdef ENABLE_LOGGING
  531. if ( LogManager.IsInventoryHFSMLogEnable() )
  532. {
  533. Debug.InventoryHFSMLog("CanAddSwappedEntity - failed", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CheckRequest", m_Player.ToString() );
  534. }
  535. #endif
  536. }
  537. else
  538. return true;
  539. return false;
  540. }
  541. override bool CanPerformEvent ()
  542. {
  543. if (GameInventory.CanForceSwapEntitiesEx(GetSrc().GetItem(), m_Dst, m_Src2.GetItem(), m_Dst2))
  544. return true;
  545. #ifdef ENABLE_LOGGING
  546. if ( LogManager.IsInventoryHFSMLogEnable() )
  547. {
  548. Debug.InventoryHFSMLog("CANNOT perform", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  549. }
  550. #endif
  551. return false;
  552. }
  553. override bool AcquireInventoryJunctureFromServer (notnull Man player)
  554. {
  555. return TryAcquireTwoInventoryJuncturesFromServer(player, m_Src, m_Src2, m_Dst, m_Dst2);
  556. }
  557. override string DumpToString ()
  558. {
  559. 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) + " }";
  560. }
  561. void CheckAndExecuteForceStandUp()
  562. {
  563. DayZPlayer player = DayZPlayer.Cast(m_Player);
  564. player.ForceStandUpForHeavyItemsSwap( m_Src.GetItem(), m_Src2.GetItem() );
  565. }
  566. override bool ReserveInventory()
  567. {
  568. if (!m_Player.GetHumanInventory().HasInventoryReservation(m_Dst.GetItem(), m_Dst) && !m_Player.GetHumanInventory().HasInventoryReservation(m_Dst2.GetItem(), m_Dst2))
  569. {
  570. if (m_Player.GetHumanInventory().AddInventoryReservationEx(m_Dst.GetItem(), m_Dst, GameInventory.c_InventoryReservationTimeoutShortMS))
  571. {
  572. if (m_Player.GetHumanInventory().AddInventoryReservationEx(m_Dst2.GetItem(), m_Dst2, GameInventory.c_InventoryReservationTimeoutShortMS))
  573. {
  574. return true;
  575. }
  576. else
  577. {
  578. m_Player.GetHumanInventory().ClearInventoryReservationEx(m_Dst.GetItem(), m_Dst);
  579. }
  580. }
  581. }
  582. return false;
  583. }
  584. override void ClearInventoryReservation()
  585. {
  586. m_Player.GetHumanInventory().ClearInventoryReservationEx(m_Dst.GetItem(), m_Dst);
  587. m_Player.GetHumanInventory().ClearInventoryReservationEx(m_Dst2.GetItem(), m_Dst2);
  588. }
  589. };
  590. class HandEventForceSwap extends HandEventSwap
  591. {
  592. void HandEventForceSwap (Man p = null, InventoryLocation src = null, InventoryLocation src2 = null, InventoryLocation dst = null, InventoryLocation dst2 = null) { m_EventID = HandEventID.FORCESWAP; }
  593. override bool CheckRequest ()
  594. {
  595. bool test1 = false;
  596. EntityAI inHands = m_Player.GetHumanInventory().GetEntityInHands();
  597. if (GetSrcEntity() && inHands && m_Dst && m_Dst.IsValid())
  598. {
  599. test1 = GameInventory.CheckSwapItemsRequest(m_Player, m_Src, m_Src2, m_Dst, m_Dst2, GameInventory.c_MaxItemDistanceRadius);
  600. #ifdef ENABLE_LOGGING
  601. if ( LogManager.IsInventoryHFSMLogEnable() )
  602. {
  603. if (!test1)
  604. Debug.InventoryHFSMLog("CheckSwapItemsRequest failed", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CheckRequest", m_Player.ToString() );
  605. }
  606. #endif
  607. }
  608. return test1;
  609. }
  610. override bool CanPerformEvent ()
  611. {
  612. 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)
  613. #ifdef ENABLE_LOGGING
  614. if ( LogManager.IsInventoryHFSMLogEnable() )
  615. {
  616. if (!test2)
  617. Debug.InventoryHFSMLog("CanForceSwapEntitiesEx failed", typename.EnumToString(HandEventID, GetEventID()) , "n/a", "CanPerformEvent", m_Player.ToString() );
  618. }
  619. #endif
  620. return test2;
  621. }
  622. };
  623. class HandEventDestroy extends HandEventBase
  624. {
  625. void HandEventDestroy (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.DESTROY; }
  626. override bool IsServerSideOnly () { return true; }
  627. };
  628. class HandEventCreated extends HandEventBase
  629. {
  630. void HandEventCreated (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.CREATED; }
  631. };
  632. class HandEventDestroyed extends HandEventBase
  633. {
  634. void HandEventDestroyed (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.DESTROYED; }
  635. };
  636. class HandEventReplaceWithNewBase extends HandEventBase
  637. {
  638. string m_Type;
  639. ref ReplaceItemWithNewLambdaBase m_Lambda;
  640. void HandEvengReplaceWithNewBase (Man p = null, InventoryLocation src = null, ReplaceItemWithNewLambdaBase lambda = NULL) { m_EventID = HandEventID.REPLACE; m_Lambda = lambda; }
  641. override void ReadFromContext (ParamsReadContext ctx)
  642. {
  643. super.ReadFromContext(ctx);
  644. ctx.Read(m_Type);
  645. Error("[hndfsm] HandEventDestroyAndReplaceWithNew - Cannot serialize lambda (read)");
  646. }
  647. override void WriteToContext (ParamsWriteContext ctx)
  648. {
  649. super.WriteToContext(ctx);
  650. ctx.Write(m_Type);
  651. Error("[hndfsm] HandEventDestroyAndReplaceWithNew - Cannot serialize lambda (write)");
  652. }
  653. override bool IsServerSideOnly () { return true; }
  654. override InventoryLocation GetDst ()
  655. {
  656. InventoryLocation dst = new InventoryLocation;
  657. dst.SetHands(m_Player, GetSrcEntity());
  658. return dst;
  659. }
  660. override string DumpToString ()
  661. {
  662. string res = "{ HandEvenReplaceWithNewBase id=" + typename.EnumToString(HandEventID, GetEventID()) + " pl=" + Object.GetDebugName(m_Player) + " src=" + InventoryLocation.DumpToStringNullSafe(GetSrc()) + " lambda=" + m_Lambda + " dst=" + InventoryLocation.DumpToStringNullSafe(GetDst()) + " }";
  663. return res;
  664. }
  665. };
  666. class HandEventDestroyAndReplaceWithNew extends HandEventReplaceWithNewBase
  667. {
  668. void HandEventDestroyAndReplaceWithNew (Man p = null, InventoryLocation src = null, ReplaceItemWithNewLambdaBase lambda = NULL) { m_EventID = HandEventID.REPLACE; m_Lambda = lambda; }
  669. };
  670. class HandEventDestroyAndReplaceWithNewElsewhere extends HandEventDestroyAndReplaceWithNew
  671. {
  672. void HandEventDestroyAndReplaceWithNewElsewhere (Man p = null, InventoryLocation src = null, ReplaceItemWithNewLambdaBase lambda = NULL) { m_EventID = HandEventID.REPLACE3; m_Lambda = lambda; }
  673. };
  674. class HandEventDestroyElsewhereAndReplaceWithNewInHands extends HandEventReplaceWithNewBase
  675. {
  676. void HandEventDestroyElsewhereAndReplaceWithNewInHands (Man p = null, InventoryLocation src = null, ReplaceItemWithNewLambdaBase lambda = NULL) { m_EventID = HandEventID.REPLACE3; m_Lambda = lambda; }
  677. };
  678. class HandEventReplaced extends HandEventBase
  679. {
  680. void HandEventReplaced (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.REPLACED; }
  681. };
  682. // anim events
  683. class HandAnimEventChanged extends HandEventBase
  684. {
  685. void HandAnimEventChanged (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.ANIMEVENT_CHANGE_HIDE; }
  686. };
  687. HandEventBase HandAnimEventFactory (WeaponEvents type, Man p = null, InventoryLocation src = null)
  688. {
  689. switch (type)
  690. {
  691. case WeaponEvents.CHANGE_HIDE: return new HandAnimEventChanged(p, src);
  692. case WeaponEvents.CHANGE_SHOW: return new HandAnimEventChanged(p, src);
  693. }
  694. return null;
  695. }
  696. /**@brief triggered when animation action finishes
  697. **/
  698. class HandEventHumanCommandActionFinished extends HandEventBase
  699. {
  700. void HandEventHumanCommandActionFinished (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.HUMANCOMMAND_ACTION_FINISHED; }
  701. };
  702. /**@brief triggered when animation action aborts
  703. **/
  704. class HandEventHumanCommandActionAborted extends HandEventBase
  705. {
  706. void HandEventHumanCommandActionAborted (Man p = null, InventoryLocation src = null) { m_EventID = HandEventID.HUMANCOMMAND_ACTION_ABORTED; }
  707. };
  708. ///@} events