dayzplayerutils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. enum QueryFlags
  2. {
  3. NONE,
  4. //! Static objects are included in the query
  5. STATIC,
  6. //! Dynamic objects are included in the query
  7. DYNAMIC,
  8. //! Check only distance to object origins, not BB
  9. ORIGIN_DISTANCE,
  10. //! Only roadways are included in the query
  11. ONLY_ROADWAYS,
  12. }
  13. // *************************************************************************************
  14. // ! DayZPlayerUtils - some utils used for dayz player - static functions
  15. // *************************************************************************************
  16. class DayZPlayerUtils
  17. {
  18. //--------------------------------------------------
  19. // Debug Draw
  20. //! enables debug draw in functions
  21. static proto native void EnableDebugDraw(bool pEnable);
  22. static proto native void DrawDebugText(string text, vector pos, float size);
  23. //! clear info - new draw starts
  24. static proto native void DrawStartFrame();
  25. //! draws debug box (color=red)
  26. static proto native void DrawDebugBox(vector pos, float size, int color);
  27. //--------------------------------------------------
  28. // animation override
  29. //! overrides total animation translation
  30. static proto native bool DebugOverrideAnimationTranslation(string pAnimName, vector pTranslation);
  31. //! overrides total animation rotation
  32. static proto native bool DebugOverrideAnimationRotation(string pAnimName, vector pRotation);
  33. //! overrides total animation speed
  34. static proto native bool DebugOverrideAnimationSpeed(string pAnimName, float pSpeed);
  35. //--------------------------------------------------
  36. //
  37. //! pLimits in form Xvalue, Ymin, Ymax, Xvalue1, Ymin1, Ymax1, ....
  38. //! sample 0, 0, 5, 1, 2, 3, 2, 0, 1
  39. //! for X <= 0 it clamps Y to Rangle (0,5)
  40. //! for X == 0.5 it clamps Y to Rangle (1,4)
  41. //! for X == 1 it clamps Y to Rangle (2,3)
  42. //! for X == 1.5 it clamps Y to Rangle (1,2)
  43. //! for X >= 2 it clamps Y to Rangle (0,1)
  44. static proto float LinearRangeClamp(float pValueX, float pValueY, float pLimits[]);
  45. //--------------------------------------------------
  46. // From Physics World
  47. //! returns entities overlapping/touching in AABB box -
  48. static proto native void PhysicsGetEntitiesInBox(vector min, vector max, notnull out array<EntityAI> entList);
  49. //--------------------------------------------------
  50. // From Scene
  51. //! returns entities overlapping/touching in AABB box -
  52. static proto native void SceneGetEntitiesInBox(vector min, vector max, notnull out array<EntityAI> entList, int flags = QueryFlags.DYNAMIC);
  53. //--------------------------------------------------
  54. // Custom player functions
  55. /**@fn GetEntitiesInCone
  56. * @brief returns entities in height-extended 2D cone
  57. * @param[in] angle \p angle in degrees
  58. * @param[in] minHeigh \p cone 3D-extension Y min
  59. * @param[in] maxHeight \p cone 3D-extension Y max
  60. * @note the detection hits points/faces from ANY non-edit model geometry, including resolution LODs, regardless of animation hiding (scaling). That is why certain animated objects, like flags in folded state, get detected from significant distance away!
  61. **/
  62. static proto native void GetEntitiesInCone(vector pos, vector dir, float angle, float dist, float minHeigh, float maxHeight, out array<Object> entList);
  63. //! returns fight target
  64. static Object GetMeleeTarget(vector pos, vector dir, float angle, float dist, float minHeight, float maxHeight, EntityAI pToIgnore, array<typename> targetableObjects, out array<Object> allTargets = NULL)
  65. {
  66. // Print("In: GetFightTarget");
  67. InitCachedEntList();
  68. GetEntitiesInCone(pos, dir, angle, dist, minHeight, maxHeight, m_CachedEntList);
  69. Object ret = NULL;
  70. Object obj = NULL;
  71. float retVal = dist * 2; // max value
  72. float tgAngle = Math.Tan(Math.DEG2RAD * angle);
  73. // DrawStartFrame();
  74. foreach(auto ent: m_CachedEntList)
  75. {
  76. if (ent == pToIgnore)
  77. {
  78. continue;
  79. }
  80. Class.CastTo(obj, ent);
  81. if(obj)
  82. {
  83. // check for targetable objects
  84. if( !obj.IsAnyInherited(targetableObjects) )
  85. { continue; }
  86. if( !obj.IsAlive() )
  87. { continue; }
  88. }
  89. vector entpos = ent.GetPosition();
  90. vector diff = entpos - pos;
  91. float cDistSq = diff.LengthSq();
  92. if (cDistSq > dist*dist) // out of distance
  93. {
  94. //Print("out of distance");
  95. continue;
  96. }
  97. float frontDist = diff[0]*dir[0] + diff[2]*dir[2];
  98. if (frontDist < 0.1) // behind the pos/dist half plane or further than dist
  99. {
  100. //Print("behind the pos/dist half plane or further than dist");
  101. continue;
  102. }
  103. vector project = pos + dir * frontDist;
  104. vector posdiff = Vector(project[0] - entpos[0], 0, project[2] - entpos[2]);
  105. float sideDist = posdiff.LengthSq();
  106. if (sideDist > tgAngle) // out of cone
  107. {
  108. //Print("out of cone");
  109. continue;
  110. }
  111. float sum = frontDist + sideDist;
  112. if (sum < retVal)
  113. {
  114. ret = ent;
  115. retVal = sum;
  116. }
  117. else
  118. {
  119. //Print("sum !< retVal");
  120. }
  121. // string txt = project.ToString() + ": " + sum.ToString();
  122. // DrawDebugBox(project,0.01,0xffff0000);
  123. // DrawDebugBox(entpos,0.01,0xffff0000);
  124. // DrawDebugText(txt, project, 1.0);
  125. allTargets.Insert(ent);
  126. }
  127. return ret;
  128. }
  129. static proto native bool PlayerCanChangeStance(DayZPlayer pPlayer, int pTargetStance, bool forceCheck = false);
  130. //! inventory DayZPlayer utils
  131. /**@fn FindMagazinesForAmmo
  132. * @brief searches inventory for all magazines that can hold specified ammo type
  133. * @param[in] player \p DayZPlayer
  134. * @param[in] ammoTypeName \p type name of the cartridge
  135. * @param[out] mags \p array filled with suitable magazines
  136. * @returns true if found at least one
  137. **/
  138. static proto native bool FindMagazinesForAmmo(DayZPlayer player, string ammoTypeName, out array<Magazine> mags);
  139. /*static bool HandleEjectMagazine (DayZPlayer player, Weapon weapon, int muzzleIndex)
  140. {
  141. int slotId = weapon.GetSlotFromMuzzleIndex(muzzleIndex);
  142. EntityAI att = weapon.FindAttachment(slotId);
  143. if (att)
  144. {
  145. InventoryLocation loc = new InventoryLocation;
  146. if (player.FindFreeLocationFor(att, loc))
  147. {
  148. return weapon.EjectMagazine(muzzleIndex, loc);
  149. }
  150. }
  151. return false;
  152. }*/
  153. static Magazine SelectStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, Magazine exclude_mag, float damage, string magTypeName)
  154. {
  155. if (damage < 1.0)
  156. {
  157. // find suitable heap / mag (but not the excluded one)
  158. ref array<Magazine> mags = new array<Magazine>;
  159. if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, mags))
  160. {
  161. int sz = mags.Count();
  162. for (int i = 0; i < sz; ++i)
  163. {
  164. Magazine mag_i = mags.Get(i);
  165. if (mag_i != exclude_mag && mag_i.CanAddCartridges(1))
  166. {
  167. return mag_i;
  168. }
  169. }
  170. }
  171. // create a new one in inventory
  172. InventoryLocation inv_loc = new InventoryLocation;
  173. if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inv_loc))
  174. {
  175. EntityAI eai_inv = SpawnEntity(magTypeName, inv_loc, ECE_IN_INVENTORY, RF_DEFAULT);
  176. if (eai_inv && eai_inv.IsInherited(Magazine))
  177. {
  178. Magazine mag_inv;
  179. if (Class.CastTo(mag_inv, eai_inv))
  180. {
  181. mag_inv.ServerSetAmmoCount(0);
  182. return mag_inv;
  183. }
  184. }
  185. }
  186. }
  187. vector pos = player.GetPosition();
  188. EntityAI eai_gnd = player.SpawnEntityOnGroundPos(magTypeName, pos);
  189. if (eai_gnd && eai_gnd.IsInherited(Magazine))
  190. {
  191. Magazine mag_gnd;
  192. if (Class.CastTo(mag_gnd, eai_gnd))
  193. {
  194. mag_gnd.ServerSetAmmoCount(0);
  195. return mag_gnd;
  196. }
  197. }
  198. return NULL;
  199. }
  200. static bool HandleDropMagazine(DayZPlayer player, Magazine mag)
  201. {
  202. vector m4[4];
  203. Math3D.MatrixIdentity4(m4);
  204. //! We don't care if a valid transform couldn't be found, we just want to preferably use it instead of placing on the player
  205. GameInventory.PrepareDropEntityPos(player, mag, m4, false, -1);
  206. InventoryLocation il_mag_next = new InventoryLocation;
  207. il_mag_next.SetGround(mag, m4);
  208. InventoryLocation il_mag_curr = new InventoryLocation;
  209. if (mag.GetInventory().GetCurrentInventoryLocation(il_mag_curr))
  210. {
  211. return GameInventory.LocationSyncMoveEntity(il_mag_curr, il_mag_next);
  212. }
  213. else
  214. {
  215. Error("DayZPlayerUtils::HandleDropMagazine - cannot get current inv location of mag=" + mag);
  216. return false;
  217. }
  218. }
  219. static bool HandleDropCartridge(DayZPlayer player, float damage, string cartTypeName, string magTypeName)
  220. {
  221. vector pos = player.GetPosition();
  222. EntityAI entityGround = player.SpawnEntityOnGroundPos(magTypeName, pos);
  223. if (entityGround && entityGround.IsInherited(Magazine))
  224. {
  225. Magazine magazineGround;
  226. if (Class.CastTo(magazineGround, entityGround))
  227. {
  228. magazineGround.ServerSetAmmoCount(0);
  229. magazineGround.SetHealth("", "", (1 - damage) * magazineGround.GetMaxHealth());
  230. if (magazineGround.ServerStoreCartridge(damage, cartTypeName))
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. static bool HandleStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, float damage, string cartTypeName, string magTypeName, bool CanDrop = true)
  237. {
  238. if (damage < 1.0)
  239. {
  240. //! find suitable heap / mag
  241. array<Magazine> magazines = new array<Magazine>();
  242. if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, magazines))
  243. {
  244. int healthLevel = -1;
  245. float testHeatlh = 1 - damage;
  246. foreach (Magazine magazine : magazines)
  247. {
  248. if (healthLevel == -1)
  249. {
  250. if (magazine.CanAddCartridges(1))
  251. {
  252. int numberOfHealthLevels = magazine.GetNumberOfHealthLevels();
  253. for (int i = 1; i < numberOfHealthLevels; i++)
  254. {
  255. if (magazine.GetHealthLevelValue(i) < testHeatlh)
  256. {
  257. healthLevel = i - 1;
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. if (magazine.GetHealthLevel() == healthLevel)
  264. {
  265. if (magazine.ServerStoreCartridge(damage, cartTypeName))
  266. return true;
  267. }
  268. }
  269. }
  270. //! create a new one in inventory
  271. InventoryLocation inventoryLocation = new InventoryLocation();
  272. if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inventoryLocation))
  273. {
  274. EntityAI entityInventory = SpawnEntity(magTypeName, inventoryLocation, ECE_IN_INVENTORY, RF_DEFAULT);
  275. if (entityInventory && entityInventory.IsInherited(Magazine))
  276. {
  277. Magazine magazineInventory;
  278. if (Class.CastTo(magazineInventory, entityInventory))
  279. {
  280. magazineInventory.ServerSetAmmoCount(0);
  281. magazineInventory.SetHealth("", "", (1 - damage) * magazineInventory.GetMaxHealth());
  282. if (magazineInventory.ServerStoreCartridge(damage, cartTypeName))
  283. return true;
  284. }
  285. }
  286. }
  287. //! drop on ground
  288. if (CanDrop)
  289. return HandleDropCartridge(player, damage, cartTypeName, magTypeName);
  290. }
  291. return false;
  292. }
  293. /**@fn InitComponentCollisions
  294. * @brief initializes table of component collisions
  295. * @param[in] player \p DayZPlayer
  296. * @param[in] boxes \p array filled with box @see ComponentCollisionBox
  297. * @param[in] capsules \p array filled with capsules @see ComponentCollisionCapsule
  298. * @returns true if success
  299. **/
  300. static proto native bool InitComponentCollisions(Human player, array<ref ComponentCollisionBox> boxes, array<ref ComponentCollisionCapsule> capsules);
  301. /**@fn IsComponentCollisionInitialized
  302. * @returns true if already initialized
  303. **/
  304. static proto native bool IsComponentCollisionInitialized();
  305. /**@fn ClearComponentCollisions
  306. **/
  307. static proto native void ClearComponentCollisions();
  308. static proto native vector GetMemoryPointPositionBoneRelative(DayZPlayer pPlayer, int pBoneIndex, int pPointIndex);
  309. static void InitPlayerComponentCollisions(Human player)
  310. {
  311. if (IsComponentCollisionInitialized())
  312. Error("DayZPlayerUtils.InitComponentCollisions: already initialized!");
  313. array<ref ComponentCollisionBox> b = new array<ref ComponentCollisionBox>;
  314. b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Pelvis", "Spine2"));
  315. b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Spine3", "Neck"));
  316. array<ref ComponentCollisionCapsule> c = new array<ref ComponentCollisionCapsule>;
  317. c.Insert(new ComponentCollisionCapsule(0.11, "Neck1", "Head"));
  318. c.Insert(new ComponentCollisionCapsule(0.09, "LeftArm", "LeftArmRoll"));
  319. c.Insert(new ComponentCollisionCapsule(0.08, "LeftForeArm", "LeftHand"));
  320. c.Insert(new ComponentCollisionCapsule(0.09, "RightArm", "RightArmRoll"));
  321. c.Insert(new ComponentCollisionCapsule(0.08, "RightForeArm", "RightHand"));
  322. c.Insert(new ComponentCollisionCapsule(0.11, "LeftUpLeg", "LeftUpLegRoll"));
  323. c.Insert(new ComponentCollisionCapsule(0.10, "LeftLeg", "LeftFoot"));
  324. c.Insert(new ComponentCollisionCapsule(0.11, "RightUpLeg", "RightUpLegRoll"));
  325. c.Insert(new ComponentCollisionCapsule(0.10, "RightLeg", "RightFoot"));
  326. DayZPlayerUtils.InitComponentCollisions(player, b, c);
  327. }
  328. static int ConvertStanceMaskToStanceIdx(int stanceMask)
  329. {
  330. switch (stanceMask)
  331. {
  332. case DayZPlayerConstants.STANCEMASK_PRONE:
  333. return DayZPlayerConstants.STANCEIDX_PRONE;
  334. case DayZPlayerConstants.STANCEMASK_CROUCH:
  335. return DayZPlayerConstants.STANCEIDX_CROUCH;
  336. case DayZPlayerConstants.STANCEMASK_ERECT:
  337. return DayZPlayerConstants.STANCEIDX_ERECT;
  338. case DayZPlayerConstants.STANCEMASK_RAISEDPRONE:
  339. return DayZPlayerConstants.STANCEIDX_RAISEDPRONE;
  340. case DayZPlayerConstants.STANCEMASK_RAISEDCROUCH:
  341. return DayZPlayerConstants.STANCEIDX_RAISEDCROUCH;
  342. case DayZPlayerConstants.STANCEMASK_RAISEDERECT:
  343. return DayZPlayerConstants.STANCEIDX_RAISEDERECT;
  344. }
  345. return -1;
  346. }
  347. static EWaterLevels CheckWaterLevel(DayZPlayer pPlayer, out vector waterLevel)
  348. {
  349. SHumanCommandSwimSettings swimData = pPlayer.GetDayZPlayerType().CommandSwimSettingsW();
  350. vector pp = pPlayer.PhysicsGetPositionWS();
  351. waterLevel = HumanCommandSwim.WaterLevelCheck(pPlayer, pp);
  352. if (waterLevel[1] < swimData.m_fToCrouchLevel)
  353. {
  354. return EWaterLevels.LEVEL_LOW;
  355. }
  356. else if (waterLevel[1] >= swimData.m_fToCrouchLevel && waterLevel[1] < swimData.m_fToErectLevel)
  357. {
  358. return EWaterLevels.LEVEL_CROUCH;
  359. }
  360. else// if (waterLevel[1] >= swimData.m_fToErectLevel)
  361. {
  362. float waterLevelToSwim = swimData.m_fWaterLevelIn;
  363. if (PlayerBase.Cast(pPlayer).IsClimbingLadder())
  364. waterLevelToSwim += 0.2;
  365. //! if total water depth >= 1.5m && character is 1.5m in water
  366. if (waterLevel[0] >= swimData.m_fWaterLevelIn && waterLevel[1] >= waterLevelToSwim)
  367. {
  368. return EWaterLevels.LEVEL_SWIM_START;
  369. }
  370. else
  371. {
  372. return EWaterLevels.LEVEL_ERECT;
  373. }
  374. }
  375. }
  376. //------------------------------------------------
  377. // private data
  378. private static ref array<Object> m_CachedEntList;
  379. //! cannot be instantiated
  380. private void DayZPlayerUtils() {};
  381. //!
  382. private static void InitCachedEntList()
  383. {
  384. if (m_CachedEntList == NULL)
  385. {
  386. m_CachedEntList = new array<Object>;
  387. }
  388. m_CachedEntList.Clear();
  389. }
  390. }
  391. class ComponentCollisionBox
  392. {
  393. vector m_Offset;
  394. string m_BoneName0;
  395. string m_BoneName1;
  396. void ComponentCollisionBox(float x, float y, float z, string b0, string b1)
  397. {
  398. m_Offset[0] = x;
  399. m_Offset[1] = y;
  400. m_Offset[2] = z;
  401. m_BoneName0 = b0;
  402. m_BoneName1 = b1;
  403. }
  404. };
  405. class ComponentCollisionCapsule
  406. {
  407. float m_Radius;
  408. string m_BoneName0;
  409. string m_BoneName1;
  410. void ComponentCollisionCapsule(float r, string b0, string b1)
  411. {
  412. m_Radius = r;
  413. m_BoneName0 = b0;
  414. m_BoneName1 = b1;
  415. }
  416. };