actionmanagerclient.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. typedef map<typename,ref array<ActionBase_Basic>> TInputActionMap;
  2. typedef map<typename,ref ActionInput> TTypeNameActionInputMap;
  3. class ActionManagerClient: ActionManagerBase
  4. {
  5. //Last send AcknowledgmentID (client can send more requests before recive ackfor first action)
  6. protected int m_LastAcknowledgmentID;
  7. protected bool m_ActionPossible;
  8. protected ref array<ref InventoryLocation> m_ReservedInventoryLocations; // obsolete
  9. protected ref InventoryActionHandler m_InventoryActionHandler;
  10. protected ref InventoryLocation m_HandInventoryLocationTest;
  11. protected ref TTypeNameActionInputMap m_RegistredInputsMap;
  12. protected ref array<ActionInput> m_OrederedAllActionInput;
  13. protected ref array<ActionInput> m_OrderedStandartActionInputs;
  14. protected ref array<ActionInput> m_DefaultOrderOfActionInputs;
  15. protected int m_SelectedActionInputToSrollIndex;
  16. protected bool m_IgnoreAutoInputEnd;
  17. protected ref ActionData m_PendingActionData;
  18. protected bool m_ActionWantEndRequest_Send; //Request to server was sended
  19. protected bool m_ActionInputWantEnd_Send;
  20. void ActionManagerClient(PlayerBase player)
  21. {
  22. m_HandInventoryLocationTest = new InventoryLocation;
  23. m_HandInventoryLocationTest.SetHands(player,null);
  24. m_LastAcknowledgmentID = 1;
  25. m_Targets = new ActionTargets(player);
  26. //m_ReservedInventoryLocations = new array<ref InventoryLocation>;
  27. m_InventoryActionHandler = new InventoryActionHandler(player);
  28. m_ActionWantEndRequest_Send = false;
  29. m_ActionInputWantEnd_Send = false;
  30. RegisterInputs(player);
  31. m_SelectedActionInputToSrollIndex = 0;
  32. }
  33. //pCurrentCommandID is command ID at time of call command handler, some called methods can change actual true value (need call m_Player.GetCurrentCommandID() for actual command ID)
  34. override void Update(int pCurrentCommandID)
  35. {
  36. m_InventoryActionHandler.OnUpdate();
  37. super.Update(pCurrentCommandID);
  38. int currentCommandID = m_Player.GetCurrentCommandID();
  39. m_ActionPossible = ActionPossibilityCheck(currentCommandID);
  40. if (m_PendingActionData) //SP only
  41. {
  42. m_CurrentActionData = m_PendingActionData;
  43. m_CurrentActionData.m_Action.Start(m_CurrentActionData);
  44. if (m_CurrentActionData.m_Action.IsInstant())
  45. OnActionEnd();
  46. m_PendingActionData = null;
  47. }
  48. if (m_CurrentActionData)
  49. {
  50. if (m_CurrentActionData.m_State != UA_AM_PENDING && m_CurrentActionData.m_State != UA_AM_REJECTED && m_CurrentActionData.m_State != UA_AM_ACCEPTED)
  51. m_CurrentActionData.m_Action.OnUpdateClient(m_CurrentActionData);
  52. switch (m_CurrentActionData.m_State)
  53. {
  54. case UA_AM_PENDING:
  55. break;
  56. case UA_AM_ACCEPTED:
  57. int condition_mask = ActionBase.ComputeConditionMask(m_Player, m_CurrentActionData.m_Target, m_CurrentActionData.m_MainItem);
  58. m_CurrentActionData.m_Action.ClearInventoryReservationEx(m_CurrentActionData);
  59. bool can_be_action_done = ((condition_mask & m_CurrentActionData.m_Action.m_ConditionMask) == condition_mask);
  60. // check currentCommandID before start or reject
  61. if (m_ActionPossible && can_be_action_done)
  62. {
  63. m_CurrentActionData.m_Action.InventoryReservation(m_CurrentActionData);
  64. m_CurrentActionData.m_State = UA_START;
  65. m_CurrentActionData.m_Action.Start(m_CurrentActionData);
  66. if (m_CurrentActionData.m_Action.IsInstant())
  67. OnActionEnd();
  68. }
  69. else
  70. {
  71. RequestInterruptAction();
  72. OnActionEnd();
  73. }
  74. m_PendingActionAcknowledgmentID = -1;
  75. break;
  76. case UA_AM_REJECTED:
  77. OnActionEnd();
  78. m_PendingActionAcknowledgmentID = -1;
  79. break;
  80. default:
  81. ProcessActionRequestEnd();
  82. ProcessActionInputEnd();
  83. break;
  84. }
  85. }
  86. #ifdef DEVELOPER
  87. if (DeveloperFreeCamera.IsFreeCameraEnabled())
  88. {
  89. m_ActionPossible = false;
  90. ResetInputsActions();
  91. }
  92. else
  93. {
  94. #endif
  95. if (!m_CurrentActionData)
  96. {
  97. bool isMenuOpen = false;
  98. #ifndef NO_GUI
  99. isMenuOpen = GetGame().GetUIManager().IsMenuOpen(MENU_INVENTORY);
  100. #endif
  101. if (m_Player.IsRaised() || isMenuOpen)
  102. {
  103. m_Targets.Clear();
  104. }
  105. else
  106. {
  107. m_Targets.Update();
  108. }
  109. FindContextualUserActions(currentCommandID);
  110. }
  111. InputsUpdate();
  112. #ifdef DEVELOPER
  113. }
  114. #endif
  115. }
  116. void RegisterInputs(PlayerBase player)
  117. {
  118. if (!m_RegistredInputsMap)
  119. {
  120. m_RegistredInputsMap = new TTypeNameActionInputMap;
  121. for (int i = 0; i < m_ActionsArray.Count(); i++)
  122. {
  123. ActionBase action = m_ActionsArray.Get(i);
  124. typename input_type_name = action.GetInputType();
  125. ref ActionInput ai;
  126. ai = ActionInput.Cast(m_RegistredInputsMap.Get(input_type_name));
  127. if (!ai)
  128. {
  129. ai = ActionInput.Cast(input_type_name.Spawn());
  130. m_RegistredInputsMap.Insert(input_type_name, ai);
  131. }
  132. action.SetInput(ai);
  133. }
  134. for (int j = 0; j < m_RegistredInputsMap.Count(); j++)
  135. {
  136. m_RegistredInputsMap.GetElement(j).Init(player, this);
  137. }
  138. SetActioninputOrder();
  139. }
  140. }
  141. //Set order of inputs base of priority (output -> m_OrederedAllActionInput)
  142. void SetActioninputOrder()
  143. {
  144. int i, j;
  145. int priority;
  146. ActionInput input;
  147. m_OrederedAllActionInput = new array<ActionInput>;
  148. map<int, ref array<ActionInput>> temp_map_for_sort = new map<int, ref array<ActionInput>>;
  149. array<int> array_of_priorities_to_sort = new array<int>;
  150. ref array<ActionInput> same_priority_input_array;
  151. for (i = 0; i < m_RegistredInputsMap.Count(); i++)
  152. {
  153. input = m_RegistredInputsMap.GetElement(i);
  154. priority = input.GetPriority();
  155. same_priority_input_array = temp_map_for_sort.Get(priority);
  156. if (same_priority_input_array)
  157. {
  158. same_priority_input_array.Insert(input);
  159. continue;
  160. }
  161. same_priority_input_array = new array<ActionInput>;
  162. same_priority_input_array.Insert(input);
  163. temp_map_for_sort.Insert(priority,same_priority_input_array);
  164. array_of_priorities_to_sort.Insert(priority);
  165. }
  166. array_of_priorities_to_sort.Sort();
  167. for (i = 0; i < array_of_priorities_to_sort.Count(); i++)
  168. {
  169. priority = array_of_priorities_to_sort[i];
  170. same_priority_input_array = temp_map_for_sort.Get(priority);
  171. for (j = 0; j < same_priority_input_array.Count(); j++)
  172. {
  173. input = same_priority_input_array.Get(j);
  174. m_OrederedAllActionInput.Insert(input);
  175. }
  176. }
  177. SetDefaultInputsOrder();
  178. }
  179. //Order for user action inputs - will be dynamically change base on context (more complex handling viz set m_OrderedStandartActionInputs in UpdateActionCategoryPriority())
  180. void SetDefaultInputsOrder()
  181. {
  182. int i, j;
  183. m_DefaultOrderOfActionInputs = new array<ActionInput>;
  184. m_OrderedStandartActionInputs = new array<ActionInput>;
  185. ActionInput input = m_RegistredInputsMap.Get(ContinuousDefaultActionInput);
  186. if (input)
  187. {
  188. m_OrderedStandartActionInputs.Insert(input);
  189. }
  190. input = m_RegistredInputsMap.Get(DefaultActionInput);
  191. if (input)
  192. {
  193. m_OrderedStandartActionInputs.Insert(input);
  194. }
  195. input = m_RegistredInputsMap.Get(ContinuousInteractActionInput);
  196. if (input)
  197. {
  198. m_OrderedStandartActionInputs.Insert(input);
  199. }
  200. input = m_RegistredInputsMap.Get(InteractActionInput);
  201. if (input)
  202. {
  203. m_OrderedStandartActionInputs.Insert(input);
  204. }
  205. for (i = 0; i < m_OrederedAllActionInput.Count(); i++)
  206. {
  207. for (j = 0; j < m_OrderedStandartActionInputs.Count(); j++)
  208. {
  209. if (m_OrederedAllActionInput[i] == m_OrderedStandartActionInputs[j])
  210. {
  211. m_DefaultOrderOfActionInputs.Insert(m_OrederedAllActionInput[i]);
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. static ActionVariantManager GetVariantManager(typename actionName)
  218. {
  219. ActionBase action = GetAction(actionName);
  220. if (action)
  221. {
  222. return action.GetVariantManager();
  223. }
  224. //VME ACTION not exist typo most likely
  225. return null;
  226. }
  227. override void RequestEndAction()
  228. {
  229. if (!m_ActionWantEndRequest_Send)
  230. {
  231. m_ActionWantEndRequest = true;
  232. }
  233. }
  234. override void EndActionInput()
  235. {
  236. if (!m_ActionInputWantEnd_Send)
  237. {
  238. m_ActionInputWantEnd = true;
  239. }
  240. }
  241. void InputsUpdate()
  242. {
  243. ActionInput ain;
  244. if (m_CurrentActionData)
  245. {
  246. if (!m_ActionInputWantEnd)
  247. {
  248. ActionInput ai = m_CurrentActionData.m_Action.GetInput();
  249. ai.Update();
  250. if (m_Player.IsQBControl())
  251. {
  252. if (ai.JustActivate())
  253. m_Player.SetActionEndInput(m_CurrentActionData.m_Action);
  254. }
  255. else
  256. {
  257. if (ai.WasEnded() && (ai.GetInputType() == ActionInputType.AIT_CONTINUOUS || ai.GetInputType() == ActionInputType.AIT_CLICKCONTINUOUS) && !m_IgnoreAutoInputEnd)
  258. {
  259. EndActionInput();
  260. }
  261. }
  262. }
  263. }
  264. else
  265. {
  266. if (m_ActionsAvaibale)
  267. {
  268. for (int i = 0; i < m_OrederedAllActionInput.Count();i++)
  269. {
  270. ain = m_OrederedAllActionInput[i];
  271. ain.Update();
  272. if (ain.JustActivate())
  273. {
  274. ActionBase action = ain.GetAction();
  275. if (action)
  276. {
  277. ActionStart(action, ain.GetUsedActionTarget(), ain.GetUsedMainItem());
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. void ProcessActionRequestEnd()
  286. {
  287. if (m_ActionWantEndRequest)
  288. {
  289. if (GetGame().IsMultiplayer() && !m_CurrentActionData.m_Action.IsLocal())
  290. {
  291. if (!m_ActionWantEndRequest_Send && ScriptInputUserData.CanStoreInputUserData())
  292. {
  293. if (LogManager.IsActionLogEnable())
  294. {
  295. Debug.ActionLog("Time stamp: " + m_Player.GetSimulationTimeStamp(), m_CurrentActionData.m_Action.ToString() , "n/a", "EndRequest", m_CurrentActionData.m_Player.ToString());
  296. }
  297. ScriptInputUserData ctx = new ScriptInputUserData;
  298. ctx.Write(INPUT_UDT_STANDARD_ACTION_END_REQUEST);
  299. ctx.Send();
  300. m_ActionWantEndRequest_Send = true;
  301. m_ActionWantEndRequest = false;
  302. m_CurrentActionData.m_Action.EndRequest(m_CurrentActionData);
  303. }
  304. }
  305. else
  306. {
  307. m_ActionWantEndRequest = false;
  308. m_CurrentActionData.m_Action.EndRequest(m_CurrentActionData);
  309. }
  310. }
  311. }
  312. void ProcessActionInputEnd()
  313. {
  314. if (m_ActionInputWantEnd)
  315. {
  316. if (GetGame().IsMultiplayer() && !m_CurrentActionData.m_Action.IsLocal())
  317. {
  318. if (!m_ActionInputWantEnd_Send && ScriptInputUserData.CanStoreInputUserData())
  319. {
  320. if (LogManager.IsActionLogEnable())
  321. {
  322. Debug.ActionLog("Time stamp: " + m_Player.GetSimulationTimeStamp(), m_CurrentActionData.m_Action.ToString() , "n/a", "EndInput", m_CurrentActionData.m_Player.ToString());
  323. }
  324. ScriptInputUserData ctxi = new ScriptInputUserData;
  325. ctxi.Write(INPUT_UDT_STANDARD_ACTION_INPUT_END);
  326. ctxi.Send();
  327. m_ActionInputWantEnd_Send = true;
  328. m_ActionInputWantEnd = false;
  329. m_CurrentActionData.m_Action.EndInput(m_CurrentActionData);
  330. }
  331. }
  332. else
  333. {
  334. if (!m_ActionInputWantEnd_Send)
  335. {
  336. m_ActionInputWantEnd_Send = true;
  337. m_ActionInputWantEnd = false;
  338. m_CurrentActionData.m_Action.EndInput(m_CurrentActionData);
  339. }
  340. }
  341. }
  342. }
  343. ActionBase GetPossibleAction(typename inputType)
  344. {
  345. ActionInput action_input = m_RegistredInputsMap.Get(inputType);
  346. if (action_input)
  347. {
  348. return action_input.GetAction();
  349. }
  350. return NULL;
  351. }
  352. array<ActionBase> GetPossibleActions(typename inputType)
  353. {
  354. ActionInput action_input = m_RegistredInputsMap.Get(inputType);
  355. if (action_input)
  356. {
  357. return action_input.GetPossibleActions();
  358. }
  359. return NULL;
  360. }
  361. int GetPossibleActionIndex(typename inputType)
  362. {
  363. ActionInput action_input = m_RegistredInputsMap.Get(inputType);
  364. if (action_input)
  365. {
  366. return action_input.GetPossibleActionIndex();
  367. }
  368. return -1;
  369. }
  370. int GetPossibleActionCount(typename inputType)
  371. {
  372. ActionInput action_input = m_RegistredInputsMap.Get(inputType);
  373. if (action_input)
  374. {
  375. return action_input.GetPossibleActionsCount();
  376. }
  377. return 0;
  378. }
  379. //--------------------------------------------------------
  380. // Alows to set different action to current contextual one
  381. //--------------------------------------------------------
  382. void InjectAction(ActionBase action, ActionTarget target, ItemBase item)
  383. {
  384. ActionInput ai = action.GetInput();
  385. ai.ForceAction(action, target, item);
  386. }
  387. void InjectAction(typename actionType, ActionTarget target, ItemBase item)
  388. {
  389. ActionBase action = GetAction(actionType);
  390. InjectAction(action, target, item);
  391. }
  392. void EjectAction(ActionBase action)
  393. {
  394. ActionInput ai = action.GetInput();
  395. ai.ClearForcedAction();
  396. }
  397. void EjectAction(typename actionType)
  398. {
  399. ActionBase action = GetAction(actionType);
  400. EjectAction(action);
  401. }
  402. void ForceTarget(Object targetObject)
  403. {
  404. Object parent = null;
  405. EntityAI targetEntity = EntityAI.Cast(targetObject);
  406. if (targetEntity)
  407. parent = targetEntity.GetHierarchyParent();
  408. m_ForceTarget = new ActionTarget(targetObject, parent, -1, vector.Zero, -1);
  409. }
  410. void ClearForceTarget()
  411. {
  412. m_ForceTarget = NULL;
  413. }
  414. //-------------------------------------------------------------------------
  415. override ActionTarget FindActionTarget()
  416. {
  417. if (m_ForceTarget)
  418. return m_ForceTarget;
  419. ActionTarget action_target;
  420. action_target = NULL;
  421. int targetsCount = m_Targets.GetTargetsCount();
  422. if (targetsCount)
  423. {
  424. for (int i = 0; i < targetsCount; ++i)
  425. {
  426. action_target = m_Targets.GetTarget(i);
  427. Object targetObject = action_target.GetObject();
  428. Object targetParent = action_target.GetParent();
  429. if (targetParent)
  430. {
  431. break;
  432. }
  433. if (targetObject)
  434. {
  435. break;
  436. }
  437. }
  438. }
  439. else
  440. {
  441. action_target = new ActionTarget(null, null, -1, vector.Zero, -1);
  442. }
  443. return action_target;
  444. }
  445. ItemBase FindActionItem()
  446. {
  447. ItemBase item;
  448. if (m_Player && m_Player.GetItemInHands() && m_Player.GetItemInHands().IsItemBase())
  449. {
  450. item = m_Player.GetItemInHands();
  451. }
  452. return item;
  453. }
  454. protected bool HasHandInventoryReservation()
  455. {
  456. m_HandInventoryLocationTest.SetHands(m_Player,m_Player.GetItemInHands());
  457. if (m_Player.GetHumanInventory().HasInventoryReservation(m_Player.GetItemInHands(),m_HandInventoryLocationTest))
  458. return true;
  459. return false;
  460. }
  461. protected void FindContextualUserActions(int pCurrentCommandID)
  462. {
  463. // TODO: NEEDS OPTIMIZATION (focus on UpdatePossibleActions > CraftingManager::OnUpdate)
  464. m_ActionsAvaibale = false;
  465. if (!m_ActionPossible || HasHandInventoryReservation() || GetGame().IsInventoryOpen())
  466. {
  467. ResetInputsActions();
  468. return;
  469. }
  470. if (!GetRunningAction())
  471. {
  472. ActionBase action;
  473. ActionTarget target;
  474. ItemBase item;
  475. // Gathering current inputs
  476. m_ActionsAvaibale = true;
  477. item = FindActionItem();
  478. target = FindActionTarget();
  479. int actionConditionMask = ActionBase.ComputeConditionMask(m_Player,target,item);
  480. for (int i = 0; i < m_OrederedAllActionInput.Count();i++)
  481. {
  482. ActionInput ain = m_OrederedAllActionInput[i];
  483. ain.UpdatePossibleActions(m_Player,target,item, actionConditionMask);
  484. }
  485. UpdateActionCategoryPriority();
  486. SetActionContext(target,item);
  487. }
  488. }
  489. //TOOD MW In progress
  490. protected bool LockInventory(ActionData action_data)
  491. {
  492. bool success = false;
  493. if (action_data.m_Action.IsInstant())
  494. {
  495. if (LogManager.IsActionLogEnable())
  496. {
  497. Debug.ActionLog("(-) Inventory lock - Not Used", action_data.m_Action.ToString() , "n/a", "LockInventory", action_data.m_Player.ToString());
  498. }
  499. success = true;
  500. }
  501. else
  502. {
  503. if (LogManager.IsActionLogEnable())
  504. {
  505. Debug.ActionLog("(X) Inventory lock", action_data.m_Action.ToString() , "n/a", "LockInventory", action_data.m_Player.ToString());
  506. }
  507. if (action_data.m_Action)
  508. {
  509. success = action_data.m_Action.InventoryReservation(action_data);
  510. }
  511. }
  512. return success;
  513. }
  514. void UnlockInventory(ActionData action_data)
  515. {
  516. if (LogManager.IsActionLogEnable())
  517. {
  518. Debug.ActionLog("(O) Inventory unlock", action_data.m_Action.ToString() , "n/a", "UnlockInventory", action_data.m_Player.ToString());
  519. }
  520. if (action_data.m_Action)
  521. action_data.m_Action.ClearInventoryReservationEx(action_data);
  522. }
  523. protected void ActionStart(ActionBase action, ActionTarget target, ItemBase item, Param extra_data = NULL)
  524. {
  525. if (!m_CurrentActionData && action && ActionPossibilityCheck(m_Player.GetCurrentCommandID()))
  526. {
  527. m_ActionWantEndRequest_Send = false;
  528. m_ActionInputWantEnd_Send = false;
  529. m_ActionWantEndRequest = false;
  530. m_ActionInputWantEnd = false;
  531. if (action.CanBePerformedFromQuickbar())
  532. m_Player.SetActionEndInput(action);
  533. HandleInputsOnActionStart(action);
  534. if (LogManager.IsActionLogEnable())
  535. {
  536. if ( target )
  537. {
  538. Debug.ActionLog("Item = " + item + ", " + target.DumpToString(), action.ToString() , "n/a", "ActionStart", m_Player.ToString());
  539. }
  540. else
  541. {
  542. Debug.ActionLog("Item = " + item + ", no target", action.ToString() , "n/a", "ActionStart", m_Player.ToString());
  543. }
  544. }
  545. m_Interrupted = false;
  546. if (GetGame().IsMultiplayer() && !action.IsLocal())
  547. {
  548. if (!ScriptInputUserData.CanStoreInputUserData())
  549. {
  550. DPrint("ScriptInputUserData already posted - ActionManagerClient");
  551. if (LogManager.IsActionLogEnable())
  552. {
  553. Debug.ActionLog("Cannot start because ScriptInputUserData is already used", action.ToString() , "n/a", "ActionStart", m_Player.ToString());
  554. }
  555. return;
  556. }
  557. }
  558. if (!action.SetupAction(m_Player, target, item, m_CurrentActionData, extra_data))
  559. {
  560. DPrint("Can not inicialize action" + action + " - ActionManagerClient");
  561. m_CurrentActionData = NULL;
  562. return;
  563. }
  564. if (LogManager.IsActionLogEnable())
  565. {
  566. Debug.ActionLog("Action data created wait to start", action.ToString() , "n/a", "ActionStart", m_Player.ToString());
  567. }
  568. if (GetGame().IsMultiplayer() && !action.IsLocal())
  569. {
  570. ScriptInputUserData ctx = new ScriptInputUserData;
  571. ctx.Write(INPUT_UDT_STANDARD_ACTION_START);
  572. ctx.Write(action.GetID());
  573. action.WriteToContext(ctx, m_CurrentActionData);
  574. if (action.UseAcknowledgment())
  575. {
  576. m_CurrentActionData.m_State = UA_AM_PENDING;
  577. m_PendingActionAcknowledgmentID = ++m_LastAcknowledgmentID;
  578. ctx.Write(m_PendingActionAcknowledgmentID);
  579. }
  580. ctx.Send();
  581. if (!action.UseAcknowledgment())
  582. {
  583. action.Start(m_CurrentActionData);
  584. if (action.IsInstant())
  585. OnActionEnd();
  586. }
  587. }
  588. else
  589. {
  590. action.Start(m_CurrentActionData);
  591. if (action.IsInstant())
  592. OnActionEnd();
  593. }
  594. }
  595. }
  596. void HandleInputsOnActionStart(ActionBase action)
  597. {
  598. for (int i = 0; i < m_OrederedAllActionInput.Count();i++)
  599. {
  600. ActionInput ain = m_OrederedAllActionInput[i];
  601. if (action.GetInput() == ain)
  602. {
  603. ain.OnActionStart();
  604. }
  605. else
  606. {
  607. ain.Reset();
  608. }
  609. }
  610. }
  611. void HandleInputsOnActionEnd()
  612. {
  613. ResetInputsState();
  614. }
  615. void ResetInputsState()
  616. {
  617. for (int i = 0; i < m_OrederedAllActionInput.Count();i++)
  618. {
  619. ActionInput ain = m_OrederedAllActionInput[i];
  620. ain.Reset();
  621. }
  622. }
  623. void ResetInputsActions()
  624. {
  625. for (int i = 0; i < m_OrederedAllActionInput.Count();i++)
  626. {
  627. ActionInput ain = m_OrederedAllActionInput[i];
  628. ain.ActionsSelectReset();
  629. }
  630. }
  631. override void OnJumpStart()
  632. {
  633. EndOrInterruptCurrentAction();
  634. }
  635. override void EndOrInterruptCurrentAction()
  636. {
  637. if (m_CurrentActionData)
  638. {
  639. if (m_CurrentActionData.m_State == UA_AM_PENDING || m_CurrentActionData.m_State == UA_AM_REJECTED || m_CurrentActionData.m_State == UA_AM_ACCEPTED)
  640. {
  641. OnActionEnd();
  642. m_PendingActionAcknowledgmentID = -1;
  643. }
  644. else
  645. {
  646. m_CurrentActionData.m_Action.Interrupt(m_CurrentActionData);
  647. }
  648. }
  649. }
  650. //Instant Action (Debug Actions) ---------------------------------
  651. override void OnInstantAction(typename user_action_type, Param data = NULL)
  652. {
  653. ActionBase action = GetAction(user_action_type);
  654. if (action)
  655. {
  656. ActionStart(action,NULL,NULL, data);
  657. }
  658. }
  659. #ifdef BOT
  660. /// used for bots
  661. void PerformAction(int user_action_id, ActionTarget target, ItemBase item, Param extraData = NULL)
  662. {
  663. ActionStart(GetAction(user_action_id), target, item, extraData);
  664. }
  665. #endif
  666. void PerformActionStart(ActionBase action, ActionTarget target, ItemBase item, Param extra_data = NULL)
  667. {
  668. if (!GetGame().IsMultiplayer())
  669. {
  670. m_PendingActionData = new ActionData;
  671. if (!action.SetupAction(m_Player,target,item,m_PendingActionData,extra_data))
  672. m_PendingActionData = null;
  673. }
  674. else
  675. ActionStart(action, target, item, extra_data);
  676. }
  677. override void OnActionEnd()
  678. {
  679. if (m_CurrentActionData)
  680. {
  681. UnlockInventory(m_CurrentActionData);
  682. if (m_CurrentActionData.m_Action.RemoveForceTargetAfterUse())
  683. m_InventoryActionHandler.DeactiveAction();
  684. super.OnActionEnd();
  685. HandleInputsOnActionEnd();
  686. }
  687. }
  688. void SetInventoryAction(ActionBase action_name, ItemBase target_item, ItemBase main_item)
  689. {
  690. m_InventoryActionHandler.SetAction(action_name, target_item, main_item);
  691. }
  692. void SetInventoryAction(ActionBase action_name, ActionTarget target, ItemBase main_item)
  693. {
  694. m_InventoryActionHandler.SetAction(action_name, target, main_item);
  695. }
  696. void UnsetInventoryAction()
  697. {
  698. m_InventoryActionHandler.DeactiveAction();
  699. }
  700. override typename GetSelectedActionCategory()
  701. {
  702. return m_OrderedStandartActionInputs[m_SelectedActionInputToSrollIndex].Type();
  703. }
  704. void UpdateActionCategoryPriority()
  705. {
  706. int i;
  707. int last_target_index = 0;
  708. for (i = 0; i < m_DefaultOrderOfActionInputs.Count(); i++)
  709. {
  710. if (m_DefaultOrderOfActionInputs[i].HasTarget())
  711. {
  712. m_OrderedStandartActionInputs[last_target_index] = m_DefaultOrderOfActionInputs[i];
  713. last_target_index++;
  714. }
  715. }
  716. for (i = 0; i < m_DefaultOrderOfActionInputs.Count(); i++)
  717. {
  718. if (!m_DefaultOrderOfActionInputs[i].HasTarget())
  719. {
  720. m_OrderedStandartActionInputs[last_target_index] = m_DefaultOrderOfActionInputs[i];
  721. last_target_index++;
  722. }
  723. }
  724. }
  725. override void SelectFirstActionCategory()
  726. {
  727. UpdateActionCategoryPriority();
  728. m_SelectedActionInputToSrollIndex = 0;
  729. for (int index = 0; index < m_OrderedStandartActionInputs.Count(); index++)
  730. {
  731. if (m_OrderedStandartActionInputs[index].GetPossibleActionsCount() > 1)
  732. {
  733. m_SelectedActionInputToSrollIndex = index;
  734. break;
  735. }
  736. }
  737. }
  738. override void SelectNextActionCategory()
  739. {
  740. int index;
  741. for (index = m_SelectedActionInputToSrollIndex + 1; index != m_SelectedActionInputToSrollIndex;;)
  742. {
  743. if (++index >= m_OrderedStandartActionInputs.Count())
  744. {
  745. index = 0;
  746. }
  747. if (m_OrderedStandartActionInputs[index].GetPossibleActionsCount() > 1)
  748. {
  749. m_SelectedActionInputToSrollIndex = index;
  750. break;
  751. }
  752. }
  753. }
  754. override void SelectPrevActionCategory()
  755. {
  756. int index;
  757. for (index = m_SelectedActionInputToSrollIndex; index != m_SelectedActionInputToSrollIndex;;)
  758. {
  759. if (--index < 0)
  760. {
  761. index = m_OrderedStandartActionInputs.Count() - 1;
  762. }
  763. if (m_OrderedStandartActionInputs[index].GetPossibleActionsCount() > 1)
  764. {
  765. m_SelectedActionInputToSrollIndex = index;
  766. break;
  767. }
  768. }
  769. }
  770. override void SelectNextAction()
  771. {
  772. ActionInput ai;
  773. if (m_SelectedActionInputToSrollIndex < m_OrderedStandartActionInputs.Count())
  774. {
  775. ai = m_OrderedStandartActionInputs[m_SelectedActionInputToSrollIndex];
  776. if (ai && ai.GetPossibleActionsCount() > 1)
  777. {
  778. ai.SelectNextAction();
  779. }
  780. }
  781. }
  782. override void SelectPrevAction()
  783. {
  784. ActionInput ai;
  785. if (m_SelectedActionInputToSrollIndex < m_OrderedStandartActionInputs.Count())
  786. {
  787. ai = m_OrderedStandartActionInputs[m_SelectedActionInputToSrollIndex];
  788. if (ai && ai.GetPossibleActionsCount() > 1)
  789. {
  790. ai.SelectPrevAction();
  791. }
  792. }
  793. }
  794. bool CanPerformActionFromQuickbar(ItemBase mainItem, ItemBase targetItem)
  795. {
  796. ItemBase itemInHand = m_Player.GetItemInHands();
  797. ActionTarget target;
  798. target = new ActionTarget(targetItem, null, -1, vector.Zero, -1);
  799. bool hasTarget = targetItem != NULL;
  800. if (mainItem)
  801. {
  802. array<ActionBase_Basic> actions;
  803. ActionBase picked_action;
  804. int i;
  805. mainItem.GetActions(QuickaBarActionInput, actions);
  806. if (actions)
  807. {
  808. for (i = 0; i < actions.Count(); i++)
  809. {
  810. picked_action = ActionBase.Cast(actions[i]);
  811. if (picked_action && picked_action.Can(m_Player,target, itemInHand))
  812. {
  813. if (hasTarget == picked_action.HasTarget())
  814. return true;
  815. }
  816. }
  817. }
  818. //First check continuous actions
  819. mainItem.GetActions(ContinuousDefaultActionInput, actions);
  820. if (actions)
  821. {
  822. for (i = 0; i < actions.Count(); i++)
  823. {
  824. picked_action = ActionBase.Cast(actions[i]);
  825. if (picked_action && picked_action.Can(m_Player,target, itemInHand) && picked_action.CanBePerformedFromQuickbar())
  826. {
  827. if (hasTarget == picked_action.HasTarget())
  828. return true;
  829. }
  830. }
  831. }
  832. //second single use actions
  833. mainItem.GetActions(DefaultActionInput, actions);
  834. if (actions)
  835. {
  836. for (i = 0; i < actions.Count(); i++)
  837. {
  838. picked_action = ActionBase.Cast(actions[i]);
  839. if (picked_action && picked_action.HasTarget() && picked_action.Can(m_Player,target, itemInHand) && picked_action.CanBePerformedFromQuickbar())
  840. {
  841. if (hasTarget == picked_action.HasTarget())
  842. return true;
  843. }
  844. }
  845. }
  846. }
  847. return false;
  848. }
  849. void PerformActionFromQuickbar(ItemBase mainItem, ItemBase targetItem)
  850. {
  851. ItemBase itemInHand = m_Player.GetItemInHands();
  852. ActionTarget target;
  853. ItemBase parent = null;
  854. if (targetItem)
  855. parent = ItemBase.Cast(targetItem.GetHierarchyParent());
  856. target = new ActionTarget(targetItem, parent, -1, vector.Zero, -1);
  857. bool hasTarget = targetItem != NULL;
  858. if (mainItem)
  859. {
  860. ActionBase picked_action;
  861. array<ActionBase_Basic> actions;
  862. int i;
  863. mainItem.GetActions(QuickaBarActionInput, actions);
  864. if (actions)
  865. {
  866. for (i = 0; i < actions.Count(); i++)
  867. {
  868. picked_action = ActionBase.Cast(actions[i]);
  869. if (picked_action && picked_action.HasTarget() && picked_action.Can(m_Player,target, itemInHand))
  870. {
  871. if (hasTarget == picked_action.HasTarget())
  872. {
  873. ActionStart(picked_action, target, mainItem);
  874. return;
  875. }
  876. }
  877. }
  878. }
  879. //First continuous actions
  880. mainItem.GetActions(ContinuousDefaultActionInput, actions);
  881. if (actions)
  882. {
  883. for (i = 0; i < actions.Count(); i++)
  884. {
  885. picked_action = ActionBase.Cast(actions[i]);
  886. if (picked_action && picked_action.HasTarget() && picked_action.Can(m_Player,target, itemInHand) && picked_action.CanBePerformedFromQuickbar())
  887. {
  888. if (hasTarget == picked_action.HasTarget())
  889. {
  890. ActionStart(picked_action, target, mainItem);
  891. return;
  892. }
  893. }
  894. }
  895. }
  896. //Second check single use actions
  897. mainItem.GetActions(DefaultActionInput, actions);
  898. if (actions)
  899. {
  900. for (i = 0; i < actions.Count(); i++)
  901. {
  902. picked_action = ActionBase.Cast(actions[i]);
  903. if (picked_action && picked_action.Can(m_Player,target, itemInHand) && picked_action.CanBePerformedFromQuickbar())
  904. {
  905. if (hasTarget == picked_action.HasTarget())
  906. {
  907. ActionStart(picked_action, target, mainItem);
  908. return;
  909. }
  910. }
  911. }
  912. }
  913. }
  914. }
  915. //TODO Variants support ???
  916. bool CanPerformActionFromInventory(ItemBase mainItem, ItemBase targetItem)
  917. {
  918. ItemBase itemInHand = m_Player.GetItemInHands();
  919. ActionTarget target;
  920. target = new ActionTarget(targetItem, null, -1, vector.Zero, -1);
  921. bool hasTarget = targetItem != NULL;
  922. if (mainItem)
  923. {
  924. array<ActionBase_Basic> actions;
  925. ActionBase picked_action;
  926. int i;
  927. //First check single use actions
  928. mainItem.GetActions(DefaultActionInput, actions);
  929. if (actions)
  930. {
  931. for (i = 0; i < actions.Count(); i++)
  932. {
  933. picked_action = ActionBase.Cast(actions[i]);
  934. if (picked_action && picked_action.Can(m_Player,target, itemInHand) && picked_action.CanBePerformedFromInventory())
  935. {
  936. if (hasTarget == picked_action.HasTarget())
  937. return true;
  938. }
  939. }
  940. }
  941. //Inventory specific actions
  942. mainItem.GetActions(InventoryOnlyActionInput, actions);
  943. if (actions)
  944. {
  945. for (i = 0; i < actions.Count(); i++)
  946. {
  947. picked_action = ActionBase.Cast(actions[i]);
  948. if (picked_action && picked_action.Can(m_Player,target, itemInHand))
  949. {
  950. if (hasTarget == picked_action.HasTarget())
  951. return true;
  952. }
  953. }
  954. }
  955. }
  956. return false;
  957. }
  958. //TODO Variants support ???
  959. void PerformActionFromInventory(ItemBase mainItem, ItemBase targetItem)
  960. {
  961. ItemBase itemInHand = m_Player.GetItemInHands();
  962. ActionTarget target;
  963. target = new ActionTarget(targetItem, null, -1, vector.Zero, -1);
  964. bool hasTarget = targetItem != NULL;
  965. if (mainItem)
  966. {
  967. ActionBase picked_action;
  968. array<ActionBase_Basic> actions;
  969. int i;
  970. //First check single use actions
  971. mainItem.GetActions(DefaultActionInput, actions);
  972. if (actions)
  973. {
  974. for (i = 0; i < actions.Count(); i++)
  975. {
  976. picked_action = ActionBase.Cast(actions[i]);
  977. if (picked_action && picked_action.Can(m_Player,target, itemInHand) && picked_action.CanBePerformedFromInventory())
  978. {
  979. if (hasTarget == picked_action.HasTarget())
  980. {
  981. ActionStart(picked_action, target, mainItem);
  982. return;
  983. }
  984. }
  985. }
  986. }
  987. //Inventory specific actions
  988. mainItem.GetActions(InventoryOnlyActionInput, actions);
  989. if (actions)
  990. {
  991. for (i = 0; i < actions.Count(); i++)
  992. {
  993. picked_action = ActionBase.Cast(actions[i]);
  994. if (picked_action && picked_action.Can(m_Player,target, itemInHand))
  995. {
  996. if (hasTarget == picked_action.HasTarget())
  997. {
  998. ActionStart(picked_action, target, mainItem);
  999. return;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. }
  1005. }
  1006. bool CanSetActionFromInventory(ItemBase mainItem, ItemBase targetItem)
  1007. {
  1008. ItemBase itemInHand = m_Player.GetItemInHands();
  1009. ActionTarget target;
  1010. EntityAI parent = null;
  1011. if (targetItem)
  1012. {
  1013. parent = targetItem.GetHierarchyParent();
  1014. }
  1015. target = new ActionTarget(targetItem, parent, -1, vector.Zero, -1);
  1016. bool hasTarget = targetItem != NULL;
  1017. if (mainItem)
  1018. {
  1019. array<ActionBase_Basic> actions;
  1020. array<ref ActionBase> variant_actions;
  1021. ActionBase picked_action;
  1022. int variants_count,v;
  1023. //First check single use actions
  1024. mainItem.GetActions(DefaultActionInput, actions);
  1025. if (actions)
  1026. {
  1027. for (int i = 0; i < actions.Count(); i++)
  1028. {
  1029. picked_action = ActionBase.Cast(actions[i]);
  1030. if (picked_action.HasVariants())
  1031. {
  1032. picked_action.UpdateVariants(itemInHand, targetItem, -1);
  1033. picked_action.GetVariants(variant_actions);
  1034. for (v = 0; v < variant_actions.Count(); v ++)
  1035. {
  1036. picked_action = variant_actions[v];
  1037. if (picked_action && picked_action.CanBeSetFromInventory() && picked_action.Can(m_Player,target, itemInHand))
  1038. {
  1039. if (hasTarget == picked_action.HasTarget())
  1040. return true;
  1041. }
  1042. }
  1043. }
  1044. else
  1045. {
  1046. if (picked_action && picked_action.CanBeSetFromInventory() && picked_action.Can(m_Player,target, itemInHand))
  1047. {
  1048. if (hasTarget == picked_action.HasTarget())
  1049. return true;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. //second continuous actions
  1055. mainItem.GetActions(ContinuousDefaultActionInput, actions);
  1056. if (actions)
  1057. {
  1058. for (int j = 0; j < actions.Count(); j++)
  1059. {
  1060. picked_action = ActionBase.Cast(actions[j]);
  1061. if (picked_action.HasVariants())
  1062. {
  1063. picked_action.UpdateVariants(itemInHand, targetItem, -1);
  1064. picked_action.GetVariants(variant_actions);
  1065. for (v = 0; v < variant_actions.Count(); v ++)
  1066. {
  1067. picked_action = variant_actions[v];
  1068. if (picked_action && picked_action.HasTarget() && picked_action.CanBeSetFromInventory() && picked_action.Can(m_Player,target, itemInHand))
  1069. {
  1070. if (hasTarget == picked_action.HasTarget())
  1071. return true;
  1072. }
  1073. }
  1074. }
  1075. else
  1076. {
  1077. if (picked_action && picked_action.HasTarget() && picked_action.CanBeSetFromInventory() && picked_action.Can(m_Player,target, itemInHand))
  1078. {
  1079. if (hasTarget == picked_action.HasTarget())
  1080. return true;
  1081. }
  1082. }
  1083. }
  1084. }
  1085. }
  1086. return false;
  1087. }
  1088. //TODO fix for variants
  1089. void SetActionFromInventory(ItemBase mainItem, ItemBase targetItem)
  1090. {
  1091. ItemBase itemInHand = m_Player.GetItemInHands();
  1092. ActionTarget target;
  1093. target = new ActionTarget(targetItem, null, -1, vector.Zero, -1);
  1094. bool hasTarget = targetItem != NULL;
  1095. if (mainItem)
  1096. {
  1097. array<ActionBase_Basic> actions;
  1098. ActionBase picked_action;
  1099. //First check single use actions
  1100. mainItem.GetActions(DefaultActionInput, actions);
  1101. if (actions)
  1102. {
  1103. for (int i = 0; i < actions.Count(); i++)
  1104. {
  1105. picked_action = ActionBase.Cast(actions[i]);
  1106. if (picked_action && picked_action.Can(m_Player,target, itemInHand) && picked_action.CanBeSetFromInventory())
  1107. {
  1108. if (hasTarget == picked_action.HasTarget())
  1109. {
  1110. SetInventoryAction(picked_action, target, itemInHand);
  1111. return;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. //second continuous actions
  1117. mainItem.GetActions(ContinuousDefaultActionInput, actions);
  1118. if (actions)
  1119. {
  1120. for (int j = 0; j < actions.Count(); j++)
  1121. {
  1122. picked_action = ActionBase.Cast(actions[j]);
  1123. if (picked_action && picked_action.HasTarget() && picked_action.Can(m_Player,target, itemInHand) && picked_action.CanBeSetFromInventory())
  1124. {
  1125. if (hasTarget == picked_action.HasTarget())
  1126. {
  1127. SetInventoryAction(picked_action, target, itemInHand);
  1128. return;
  1129. }
  1130. }
  1131. }
  1132. }
  1133. }
  1134. }
  1135. // disables automatic end of continuous action when input ended
  1136. void SetIgnoreAutomaticInputEnd(bool state)
  1137. {
  1138. m_IgnoreAutoInputEnd = state;
  1139. }
  1140. //! client requests action interrupt
  1141. override void RequestInterruptAction()
  1142. {
  1143. if (ScriptInputUserData.CanStoreInputUserData())
  1144. {
  1145. ScriptInputUserData ctx = new ScriptInputUserData;
  1146. ctx.Write(INPUT_UDT_STANDARD_ACTION_END_REQUEST);
  1147. ctx.Write(DayZPlayerConstants.CMD_ACTIONINT_INTERRUPT);
  1148. ctx.Send();
  1149. }
  1150. }
  1151. private ref ActionTarget m_ForceTarget;
  1152. private ref ActionTargets m_Targets;
  1153. };