quickbarbase.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. const int MAX_QUICKBAR_SLOTS_COUNT = 10;
  2. // Script File
  3. class QuickBarItem
  4. {
  5. EntityAI m_entity;
  6. bool m_enabled;
  7. void QuickBarItem()
  8. {
  9. m_entity = NULL;
  10. m_enabled = false;
  11. }
  12. }
  13. class QuickBarBase
  14. {
  15. protected ref QuickBarItem m_aQuickbarEnts[MAX_QUICKBAR_SLOTS_COUNT];
  16. protected PlayerBase _player;
  17. protected int m_slotsCount;
  18. void QuickBarBase(PlayerBase player)
  19. {
  20. for (int i = 0; i < MAX_QUICKBAR_SLOTS_COUNT; i++)
  21. {
  22. m_aQuickbarEnts[i] = new QuickBarItem;
  23. m_aQuickbarEnts[i].m_enabled = false;
  24. m_aQuickbarEnts[i].m_entity = NULL;
  25. }
  26. _player = player;
  27. m_slotsCount = 0;
  28. }
  29. //-------------------------------------------------------------
  30. void SetSize(int newSize)
  31. {
  32. int i = m_slotsCount;
  33. if ( newSize == m_slotsCount )
  34. return;
  35. if (newSize > MAX_QUICKBAR_SLOTS_COUNT)
  36. newSize = MAX_QUICKBAR_SLOTS_COUNT;
  37. if ( newSize > i )
  38. {
  39. for (; i < newSize; i++)
  40. {
  41. EntityAI entity = m_aQuickbarEnts[i].m_entity;
  42. if ( entity != NULL && entity.GetHierarchyRootPlayer() == _player )
  43. {
  44. m_aQuickbarEnts[i].m_enabled = true;
  45. }
  46. }
  47. }
  48. else
  49. {
  50. for (i--; i >= newSize; i--)
  51. m_aQuickbarEnts[i].m_enabled = false;
  52. }
  53. m_slotsCount = newSize;
  54. if (_player.m_Hud)
  55. _player.m_Hud.RefreshQuickbar(true);
  56. }
  57. //-------------------------------------------------------------
  58. int GetNonEmptyCount()
  59. {
  60. int count = 0;
  61. for ( int i = 0; i < m_slotsCount; i++ )
  62. {
  63. if (m_aQuickbarEnts[i].m_enabled)
  64. count++;
  65. }
  66. return count;
  67. }
  68. //-------------------------------------------------------------
  69. EntityAI GetEntity(int index)
  70. {
  71. if (index < 0 || index >= m_slotsCount)
  72. return NULL;
  73. if (m_aQuickbarEnts[index].m_enabled)
  74. return m_aQuickbarEnts[index].m_entity;
  75. return NULL;
  76. }
  77. //-------------------------------------------------------------
  78. int GetSize()
  79. {
  80. return m_slotsCount;
  81. }
  82. //-------------------------------------------------------------
  83. int FindEntityIndex(EntityAI entity)
  84. {
  85. for (int i = 0; i < MAX_QUICKBAR_SLOTS_COUNT; i++)
  86. {
  87. if (m_aQuickbarEnts[i].m_entity == entity)
  88. return i;
  89. }
  90. return -1;
  91. }
  92. //-------------------------------------------------------------
  93. bool OnInputUserDataProcess(int userDataType, ParamsReadContext ctx)
  94. {
  95. if ( userDataType == INPUT_UDT_QUICKABARSHORTCUT)
  96. {
  97. OnSetEntityRequest(ctx);
  98. return true;
  99. }
  100. return false;
  101. }
  102. //-------------------------------------------------------------
  103. void UpdateShotcutVisibility(int index)
  104. {
  105. m_aQuickbarEnts[index].m_enabled = CanAddAsShortcut(m_aQuickbarEnts[index].m_entity);
  106. if (!m_aQuickbarEnts[index].m_enabled)
  107. CancelContinuousUse(index);
  108. if (_player.m_Hud)
  109. _player.m_Hud.RefreshQuickbar(true);
  110. }
  111. //-------------------------------------------------------------
  112. void SetShotcutEnable(int index, bool value)
  113. {
  114. m_aQuickbarEnts[index].m_enabled = value && CanAddAsShortcut(m_aQuickbarEnts[index].m_entity);
  115. if (!m_aQuickbarEnts[index].m_enabled)
  116. CancelContinuousUse(index);
  117. if (_player.m_Hud)
  118. _player.m_Hud.RefreshQuickbar(true);
  119. }
  120. //-------------------------------------------------------------
  121. bool CanAddAsShortcut(EntityAI entity)
  122. {
  123. InventoryLocation loc = new InventoryLocation;
  124. entity.GetInventory().GetCurrentInventoryLocation(loc);
  125. EntityAI parent = loc.GetParent();
  126. return (entity && entity.GetHierarchyRootPlayer() == _player && parent.CanAssignAttachmentsToQuickbar() && entity.CanAssignToQuickbar());
  127. }
  128. //-------------------------------------------------------------
  129. void SetEntityShortcut(EntityAI entity, int index, bool force = false)
  130. {
  131. //Client
  132. if (GetGame().IsClient())
  133. {
  134. if (ScriptInputUserData.CanStoreInputUserData())
  135. {
  136. ScriptInputUserData ctx = new ScriptInputUserData;
  137. ctx.Write(INPUT_UDT_QUICKABARSHORTCUT);
  138. ctx.Write(entity);
  139. ctx.Write(index);
  140. ctx.Write(force);
  141. ctx.Send();
  142. _SetEntityShortcut(entity, index, force);
  143. }
  144. }
  145. //Server
  146. else if (GetGame().IsMultiplayer() && GetGame().IsServer())
  147. {
  148. DayZPlayerSyncJunctures.SendQuickbarSetShortcut(_player, entity, index, force);
  149. }
  150. else
  151. {
  152. // Single player
  153. _SetEntityShortcut(entity,index,force);
  154. }
  155. }
  156. //-------------------------------------------------------------
  157. void OnSetEntityNoSync(EntityAI entity, int index, bool force = false )
  158. {
  159. _SetEntityShortcut(entity, index, force);
  160. }
  161. //-------------------------------------------------------------
  162. //! Reaction on Rpc from server for set inicial state for quickbar
  163. void OnSetEntityRPC(ParamsReadContext ctx)
  164. {
  165. Param2<Entity,int> param;
  166. if (!ctx.Read(param))
  167. return;
  168. EntityAI entity1 = EntityAI.Cast(param.param1);
  169. _SetEntityShortcut(entity1, param.param2, false);
  170. }
  171. //-------------------------------------------------------------
  172. //! Reaction on SetEntityShortcut from client
  173. void OnSetEntityRequest(ParamsReadContext ctx)
  174. {
  175. EntityAI eai = null;
  176. if (!ctx.Read(eai))
  177. return;
  178. int index = -1;
  179. if (!ctx.Read(index))
  180. return;
  181. bool force = false;
  182. if (!ctx.Read(force))
  183. return
  184. _SetEntityShortcut(eai, index, force);
  185. }
  186. //-------------------------------------------------------------
  187. protected void _SetEntityShortcut(EntityAI entity, int index, bool force = false)
  188. {
  189. //TODO Check, if is in inventory
  190. //if(entity.GetLoca)
  191. if ( entity == NULL )
  192. {
  193. _RemoveEntity(index);
  194. return;
  195. }
  196. int i = FindEntityIndex(entity);
  197. if ( i != -1 )
  198. _RemoveEntity(i);
  199. _RemoveEntity(index);
  200. _SetEntity( entity, index, force);
  201. }
  202. //-------------------------------------------------------------
  203. void updateSlotsCount()
  204. {
  205. int slotsCount = _player.GetQuickBarBonus();
  206. int attCount = _player.GetInventory().AttachmentCount();
  207. for ( int i = 0; i < attCount; ++i)
  208. {
  209. slotsCount += _player.GetInventory().GetAttachmentFromIndex(i).GetQuickBarBonus();
  210. }
  211. //max slots is 10
  212. SetSize(Math.Min(slotsCount, 10));
  213. }
  214. //-------------------------------------------------------------
  215. protected void _RemoveEntity(int index)
  216. {
  217. if ( index >= 0 && index < MAX_QUICKBAR_SLOTS_COUNT )
  218. {
  219. CancelContinuousUse(index);
  220. m_aQuickbarEnts[index].m_entity = NULL;
  221. m_aQuickbarEnts[index].m_enabled = false;
  222. if (_player.m_Hud)
  223. _player.m_Hud.RefreshQuickbar(true);
  224. }
  225. }
  226. //-------------------------------------------------------------
  227. protected void _SetEntity( EntityAI entity, int index, bool force = false)
  228. {
  229. if ( index >= 0 && index < MAX_QUICKBAR_SLOTS_COUNT )
  230. {
  231. if ( CanAddAsShortcut(entity) )
  232. {
  233. m_aQuickbarEnts[index].m_entity = entity;
  234. m_aQuickbarEnts[index].m_enabled = true;
  235. }
  236. else
  237. {
  238. CancelContinuousUse(index);
  239. m_aQuickbarEnts[index].m_enabled = false;
  240. }
  241. if (_player.m_Hud)
  242. _player.m_Hud.RefreshQuickbar(true);
  243. }
  244. }
  245. //-------------------------------------------------------------
  246. void ~QuickBarBase()
  247. {
  248. }
  249. //-------------------------------------------------------------
  250. protected void CancelContinuousUse(int index)
  251. {
  252. if (_player.m_QuickBarHold)
  253. {
  254. HumanInputController hic = _player.GetInputController();
  255. if (hic && hic.IsQuickBarSlot() == index + 1)
  256. _player.OnQuickBarContinuousUseEnd(index + 1);
  257. }
  258. }
  259. }