inventorylocation.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. //@NOTE: DO NOT EDIT! enum values are overwritten from c++
  2. /// types of Inventory Location
  3. enum InventoryLocationType
  4. {
  5. UNKNOWN, ///< unknown, usually freshly created object
  6. GROUND, /// < ground
  7. ATTACHMENT, ///< attachment of another entity
  8. CARGO, ///< cargo of another entity
  9. HANDS, ///< hands of another entity
  10. PROXYCARGO, ///< cargo of a large object (building,...)
  11. VEHICLE,
  12. };
  13. //@NOTE: DO NOT EDIT! enum values are overwritten from c++
  14. /// flags for searching locations in inventory
  15. enum FindInventoryLocationType
  16. {
  17. ATTACHMENT, ///< ATT
  18. CARGO, ///< CGO
  19. HANDS, ///< HND
  20. PROXYCARGO, ///< PXY
  21. ANY_CARGO, ///< CGO | PXY
  22. ANY, ///< ATT | CGO | PXY | HND
  23. NO_SLOT_AUTO_ASSIGN ///< skips auto-assign test
  24. };
  25. //! InventoryLocation
  26. class InventoryLocation
  27. {
  28. /**
  29. * @fn IsValid
  30. * @brief verify current set inventory location
  31. * @return true if valid, false otherwise
  32. **/
  33. proto native bool IsValid();
  34. /**
  35. * @fn GetType
  36. * @brief returns type of InventoryLocation
  37. *
  38. * @see InventoryLocationType for known types of inventory locations
  39. **/
  40. proto native int GetType();
  41. /**
  42. * @fn GetParent
  43. * @brief returns parent of current inventory location
  44. *
  45. * Parent entity can be null if item is on ground.
  46. *
  47. * @return parent entity, null otherwise
  48. **/
  49. proto native EntityAI GetParent();
  50. /**
  51. * @fn GetItem
  52. * @brief returns item of current inventory location
  53. *
  54. * Item can be null if and only if this is a query for location
  55. * of item that is about to be created (new spawn).
  56. *
  57. * @return item
  58. **/
  59. proto native EntityAI GetItem();
  60. /**
  61. * @fn GetSlot
  62. * @brief returns slot id if current type is Attachment
  63. *
  64. * @NOTE: slot id is related to order of entries in DZ/data/config.cpp secton CfgSlots
  65. *
  66. * @return slot id if attachment, -1 otherwise
  67. **/
  68. proto native int GetSlot();
  69. /**
  70. * @fn GetIdx
  71. * @brief returns index of cargo if current type is Cargo / ProxyCargo
  72. * @return index or -1
  73. **/
  74. proto native int GetIdx();
  75. /**
  76. * @fn GetRow
  77. * @brief returns row of cargo if current type is Cargo / ProxyCargo
  78. * @return row or -1
  79. **/
  80. proto native int GetRow();
  81. /**
  82. * @fn GetCol
  83. * @brief returns column of cargo if current type is Cargo / ProxyCargo
  84. * @return index or -1
  85. **/
  86. proto native int GetCol();
  87. /**
  88. * @fn GetFlip
  89. * @brief returns flip status of cargo
  90. * @return true/false
  91. **/
  92. proto native bool GetFlip();
  93. /**
  94. * @fn GetPos
  95. * @brief returns position of item in world if type is Ground
  96. * @return position
  97. **/
  98. proto native vector GetPos();
  99. /**
  100. * @fn GetDir
  101. * @brief returns direction of item in world if type is Ground
  102. * @return position
  103. **/
  104. proto native void GetDir(out float dir[4]);
  105. /**
  106. * @fn SetGround
  107. * @brief sets current inventory location type to Ground with transformation mat
  108. * @param[in] e entity with this inventory location (null for new spawns)
  109. * @param[in] mat transformation matrix
  110. **/
  111. proto native void SetGround(EntityAI e, vector mat[4]);
  112. /**
  113. * @fn SetGroundEx
  114. * @brief sets current inventory location type to Ground with transformation mat
  115. * @param[in] e entity with this inventory location (null for new spawns)
  116. * @param[in] pos position vector
  117. * @param[in] dir direction quat
  118. **/
  119. proto native void SetGroundEx(EntityAI e, vector pos, float dir[4]);
  120. /**
  121. * @fn SetAttachment
  122. * @brief sets current inventory location type to Attachment with slot id set to <slotId>
  123. * @param[in] parent parent entity the item <e> will be attached to
  124. * @param[in] e entity with this inventory location (null for new spawns)
  125. * @param[in] slotId slot id where the item will be attached
  126. **/
  127. proto native void SetAttachment(notnull EntityAI parent, EntityAI e, int slotId);
  128. /**
  129. * @fn SetCargoAuto
  130. * @brief based on Cargo.IsProxyCargo uses SetProxyCargo or SetCargo respectively
  131. * @param[in] cargo cargo that will receive item <e> into cargo
  132. * @param[in] e entity with this inventory location (null for new spawns)
  133. * @param[in] row row of the cargo
  134. * @param[in] col column of the cargo
  135. **/
  136. proto native void SetCargoAuto(notnull CargoBase cargo, EntityAI e, int row, int col, bool flip);
  137. /**
  138. * @fn SetCargo
  139. * @brief sets current inventory location type to Cargo with coordinates (idx, row, col)
  140. * @param[in] parent parent entity that will receive item <e> into cargo
  141. * @param[in] e entity with this inventory location (null for new spawns)
  142. * @param[in] idx index of cargo
  143. * @param[in] row row of the cargo
  144. * @param[in] col column of the cargo
  145. **/
  146. proto native void SetCargo(notnull EntityAI parent, EntityAI e, int idx, int row, int col, bool flip);
  147. /**
  148. * @fn SetProxyCargo
  149. * @brief sets current inventory location type to ProxyCargo with coordinates (idx, row, col)
  150. * @NOTE: typical usage is in building-like dummy structures, where furniture with cargo is part
  151. * of the model, and is not a physical entity (like person or bag).
  152. * @param[in] parent parent entity that will receive item <e> into proxy cargo
  153. * @param[in] e entity with this inventory location (null for new spawns)
  154. * @param[in] idx index of cargo
  155. * @param[in] row row of the cargo
  156. * @param[in] col column of the cargo
  157. *
  158. **/
  159. proto native void SetProxyCargo(notnull EntityAI parent, EntityAI e, int idx, int row, int col, bool flip);
  160. /**
  161. * @fn SetHands
  162. * @brief sets current inventory location type to Hands
  163. * @param[in] parent parent entity the item <e> will be attached to. this'd better be inherited from Person
  164. * @param[in] e entity with this inventory location (null for new spawns)
  165. **/
  166. proto native void SetHands(notnull EntityAI parent, EntityAI e);
  167. /**
  168. * @fn SetVehicle
  169. * brief sets current inventory location's GetParent
  170. * @param[in] idx index of seat
  171. **/
  172. proto native void SetVehicle(notnull EntityAI parent, EntityAI e, int idx);
  173. /**
  174. * @fn SetParent
  175. * brief sets current inventory location's GetParent
  176. **/
  177. proto native void SetParent(notnull EntityAI parent);
  178. /**
  179. * @fn SetItem
  180. * brief sets current inventory location's item
  181. **/
  182. proto native void SetItem(notnull EntityAI item);
  183. // direct set methods
  184. proto native void SetSlot(int slotId);
  185. proto native void SetIndex(int idx);
  186. proto native void SetRow(int row);
  187. proto native void SetCol(int col);
  188. proto native void SetFlip(bool flip);
  189. /**
  190. * @fn Reset
  191. **/
  192. proto native void Reset();
  193. proto native bool CompareLocationOnly(notnull InventoryLocation other);
  194. /**
  195. * @fn CollidesWith
  196. * @brief checks if inventory locations collides each with other
  197. **/
  198. proto native bool CollidesWith(notnull InventoryLocation rhs);
  199. /**
  200. * @fn Copy
  201. * @brief copies location data to another location
  202. *
  203. * @NOTE it copies all data (except enforce internal data)
  204. **/
  205. proto native InventoryLocation Copy(notnull InventoryLocation rhs);
  206. /**
  207. * @fn CopyLocationFrom
  208. * @brief copies location to another location without m_item member
  209. *
  210. * @NOTE it does NOT do a copy of m_item member!
  211. **/
  212. proto native InventoryLocation CopyLocationFrom(notnull InventoryLocation rhs, bool copyFlip);
  213. static string DumpToStringNullSafe(InventoryLocation loc)
  214. {
  215. if (loc)
  216. return loc.DumpToString();
  217. return "{ null }";
  218. }
  219. string DumpToString()
  220. {
  221. string res = "{ type=" + typename.EnumToString(InventoryLocationType, GetType());
  222. switch (GetType())
  223. {
  224. case InventoryLocationType.UNKNOWN:
  225. {
  226. break;
  227. }
  228. case InventoryLocationType.GROUND:
  229. {
  230. res = res + " item=" + Object.GetDebugName(GetItem());
  231. vector pos = GetPos();
  232. float dir[4];
  233. GetDir(dir);
  234. res = res + " pos=(" + pos[0] + ", " + pos[1] + ", " + pos[2] + ")";
  235. res = res + " dir=(" + dir[0] + ", " + dir[1] + ", " + dir[2] + ", " + dir[3] + ")";
  236. break;
  237. }
  238. case InventoryLocationType.ATTACHMENT:
  239. {
  240. res = res + " item=" + Object.GetDebugName(GetItem());
  241. res = res + " parent=" + Object.GetDebugName(GetParent());
  242. res = res + " slot=" + GetSlot();
  243. break;
  244. }
  245. case InventoryLocationType.CARGO:
  246. {
  247. res = res + " item=" + Object.GetDebugName(GetItem());
  248. res = res + " parent=" + Object.GetDebugName(GetParent());
  249. res = res + " idx=" + GetIdx() + " row=" + GetRow() + " col=" + GetCol() + " f=" + GetFlip();
  250. break;
  251. }
  252. case InventoryLocationType.HANDS:
  253. {
  254. res = res + " item=" + Object.GetDebugName(GetItem());
  255. res = res + " parent=" + Object.GetDebugName(GetParent());
  256. break;
  257. }
  258. case InventoryLocationType.PROXYCARGO:
  259. {
  260. res = res + " item=" + Object.GetDebugName(GetItem());
  261. res = res + " parent=" + Object.GetDebugName(GetParent());
  262. res = res + " idx=" + GetIdx() + " row=" + GetRow() + " col=" + GetCol() + " f=" + GetFlip();
  263. break;
  264. }
  265. case InventoryLocationType.VEHICLE:
  266. {
  267. res = res + " item=" + Object.GetDebugName(GetItem());
  268. res = res + " parent=" + Object.GetDebugName(GetParent());
  269. res = res + " idx=" + GetIdx();
  270. break;
  271. }
  272. default:
  273. {
  274. res = res + "??";
  275. break;
  276. }
  277. }
  278. res = res + " }";
  279. return res;
  280. }
  281. bool ReadFromContext(ParamsReadContext ctx)
  282. {
  283. EntityAI parent;
  284. EntityAI item;
  285. int type = 0;
  286. int idx = -1;
  287. int row = -1;
  288. int col = -1;
  289. bool flp = false;
  290. if (!ctx.Read(type))
  291. return false;
  292. switch (type)
  293. {
  294. case InventoryLocationType.UNKNOWN:
  295. {
  296. break;
  297. }
  298. case InventoryLocationType.GROUND:
  299. {
  300. if (!ctx.Read(item))
  301. return false;
  302. vector pos;
  303. if (!ctx.Read(pos))
  304. return false;
  305. float dir[4];
  306. if (!ctx.Read(dir))
  307. return false;
  308. if (!item)
  309. {
  310. #ifdef ENABLE_LOGGING
  311. #ifdef SERVER
  312. Debug.Log(string.Format("Item=%1 does not exist on server!", Object.GetDebugName(item)), "GROUND" , "n/a", "ReadFromContext", this.ToString() );
  313. #endif
  314. #endif
  315. break; // parent or item is possibly not spawned in bubble yet
  316. }
  317. SetGroundEx(item, pos, dir);
  318. break;
  319. }
  320. case InventoryLocationType.ATTACHMENT:
  321. {
  322. if (!ctx.Read(parent))
  323. return false;
  324. if (!ctx.Read(item))
  325. return false;
  326. int slot;
  327. if (!ctx.Read(slot))
  328. return false;
  329. if (!parent || !item)
  330. {
  331. #ifdef ENABLE_LOGGING
  332. #ifdef SERVER
  333. Debug.Log(string.Format("Parent=%1 or Item=%2 does not exist on server!", Object.GetDebugName(parent), Object.GetDebugName(item)), "ATTACHMENT" , "n/a", "ReadFromContext", this.ToString() );
  334. #endif
  335. #endif
  336. break; // parent or item is possibly not spawned in bubble yet
  337. }
  338. SetAttachment(parent, item, slot);
  339. break;
  340. }
  341. case InventoryLocationType.CARGO:
  342. {
  343. if (!ctx.Read(parent))
  344. return false;
  345. if (!ctx.Read(item))
  346. return false;
  347. if (!ctx.Read(idx))
  348. return false;
  349. if (!ctx.Read(row))
  350. return false;
  351. if (!ctx.Read(col))
  352. return false;
  353. if (!ctx.Read(flp))
  354. return false;
  355. if (!parent || !item)
  356. {
  357. #ifdef ENABLE_LOGGING
  358. #ifdef SERVER
  359. Debug.Log(string.Format("Parent=%1 or Item=%2 does not exist on server!", Object.GetDebugName(parent), Object.GetDebugName(item)), "CARGO" , "n/a", "ReadFromContext", this.ToString() );
  360. #endif
  361. #endif
  362. break; // parent or item is possibly not spawned in bubble yet
  363. }
  364. SetCargo(parent, item, idx, row, col, flp);
  365. break;
  366. }
  367. case InventoryLocationType.HANDS:
  368. {
  369. if (!ctx.Read(parent))
  370. return false;
  371. if (!ctx.Read(item))
  372. return false;
  373. if (!parent || !item)
  374. {
  375. #ifdef ENABLE_LOGGING
  376. #ifdef SERVER
  377. Debug.Log(string.Format("Parent=%1 or Item=%2 does not exist on server!", Object.GetDebugName(parent), Object.GetDebugName(item)), "HANDS" , "n/a", "ReadFromContext", this.ToString() );
  378. #endif
  379. #endif
  380. break; // parent or item is possibly not spawned in bubble yet
  381. }
  382. SetHands(parent, item);
  383. break;
  384. }
  385. case InventoryLocationType.PROXYCARGO:
  386. {
  387. if (!ctx.Read(parent))
  388. return false;
  389. if (!ctx.Read(item))
  390. return false;
  391. if (!ctx.Read(idx))
  392. return false;
  393. if (!ctx.Read(row))
  394. return false;
  395. if (!ctx.Read(col))
  396. return false;
  397. if (!ctx.Read(flp))
  398. return false;
  399. if (!parent || !item)
  400. {
  401. #ifdef ENABLE_LOGGING
  402. #ifdef SERVER
  403. Debug.Log(string.Format("Parent=%1 or Item=%2 does not exist on server!", Object.GetDebugName(parent), Object.GetDebugName(item)), "PROXYCARGO" , "n/a", "ReadFromContext", this.ToString() );
  404. #endif
  405. #endif
  406. break; // parent or item is possibly not spawned in bubble yet
  407. }
  408. SetProxyCargo(parent, item, idx, row, col, flp);
  409. break;
  410. }
  411. case InventoryLocationType.VEHICLE:
  412. {
  413. if (!ctx.Read(parent))
  414. return false;
  415. if (!ctx.Read(item))
  416. return false;
  417. if (!ctx.Read(idx))
  418. return false;
  419. if (!parent || !item)
  420. {
  421. #ifdef ENABLE_LOGGING
  422. #ifdef SERVER
  423. Debug.Log(string.Format("Parent=%1 or Item=%2 does not exist on server!", Object.GetDebugName(parent), Object.GetDebugName(item)), "VEHICLE" , "n/a", "ReadFromContext", this.ToString() );
  424. #endif
  425. #endif
  426. break; // parent or item is possibly not spawned in bubble yet
  427. }
  428. SetVehicle(parent, item, idx);
  429. break;
  430. }
  431. default:
  432. {
  433. ErrorEx("ReadFromContext - really unknown location type, this should not happen, type=" + type);
  434. return false;
  435. }
  436. }
  437. return true;
  438. }
  439. bool WriteToContext(ParamsWriteContext ctx)
  440. {
  441. if (!ctx.Write(GetType()))
  442. {
  443. Error("InventoryLocation::WriteToContext - cannot write to context! failed to write type");
  444. return false;
  445. }
  446. switch (GetType())
  447. {
  448. case InventoryLocationType.UNKNOWN:
  449. {
  450. break;
  451. }
  452. case InventoryLocationType.GROUND:
  453. {
  454. if (!ctx.Write(GetItem()))
  455. {
  456. Error("InventoryLocation::WriteToContext - cannot write to context! failed GND, arg=item");
  457. return false;
  458. }
  459. vector pos = GetPos();
  460. if (!ctx.Write(pos))
  461. {
  462. Error("InventoryLocation::WriteToContext - cannot write to context! failed GND, arg=pos");
  463. return false;
  464. }
  465. float dir[4];
  466. GetDir(dir);
  467. if (!ctx.Write(dir))
  468. {
  469. Error("InventoryLocation::WriteToContext - cannot write to context! failed GND, arg=dir");
  470. return false;
  471. }
  472. break;
  473. }
  474. case InventoryLocationType.ATTACHMENT:
  475. {
  476. if (!ctx.Write(GetParent()))
  477. {
  478. Error("InventoryLocation::WriteToContext - cannot write to context! failed ATT, arg=parent");
  479. return false;
  480. }
  481. if (!ctx.Write(GetItem()))
  482. {
  483. Error("InventoryLocation::WriteToContext - cannot write to context! failed ATT, arg=item");
  484. return false;
  485. }
  486. if (!ctx.Write(GetSlot()))
  487. {
  488. Error("InventoryLocation::WriteToContext - cannot write to context! failed ATT, arg=slot");
  489. return false;
  490. }
  491. break;
  492. }
  493. case InventoryLocationType.CARGO:
  494. {
  495. if (!ctx.Write(GetParent()))
  496. {
  497. Error("InventoryLocation::WriteToContext - cannot write to context! failed CGO, arg=parent");
  498. return false;
  499. }
  500. if (!ctx.Write(GetItem()))
  501. {
  502. Error("InventoryLocation::WriteToContext - cannot write to context! failed CGO, arg=item");
  503. return false;
  504. }
  505. if (!ctx.Write(GetIdx()))
  506. {
  507. Error("InventoryLocation::WriteToContext - cannot write to context! failed CGO, arg=idx");
  508. return false;
  509. }
  510. if (!ctx.Write(GetRow()))
  511. {
  512. Error("InventoryLocation::WriteToContext - cannot write to context! failed CGO, arg=row");
  513. return false;
  514. }
  515. if (!ctx.Write(GetCol()))
  516. {
  517. Error("InventoryLocation::WriteToContext - cannot write to context! failed CGO, arg=col");
  518. return false;
  519. }
  520. if (!ctx.Write(GetFlip()))
  521. {
  522. Error("InventoryLocation::WriteToContext - cannot write to context! failed CGO, arg=flp");
  523. return false;
  524. }
  525. break;
  526. }
  527. case InventoryLocationType.HANDS:
  528. {
  529. if (!ctx.Write(GetParent()))
  530. {
  531. Error("InventoryLocation::WriteToContext - cannot write to context! failed HND, arg=parent");
  532. return false;
  533. }
  534. if (!ctx.Write(GetItem()))
  535. {
  536. Error("InventoryLocation::WriteToContext - cannot write to context! failed HND, arg=item");
  537. return false;
  538. }
  539. break;
  540. }
  541. case InventoryLocationType.PROXYCARGO:
  542. {
  543. if (!ctx.Write(GetParent()))
  544. {
  545. Error("InventoryLocation::WriteToContext - cannot write to context! failed PXY, arg=parent");
  546. return false;
  547. }
  548. if (!ctx.Write(GetItem()))
  549. {
  550. Error("InventoryLocation::WriteToContext - cannot write to context! failed PXY, arg=item");
  551. return false;
  552. }
  553. if (!ctx.Write(GetIdx()))
  554. {
  555. Error("InventoryLocation::WriteToContext - cannot write to context! failed PXY, arg=idx");
  556. return false;
  557. }
  558. if (!ctx.Write(GetRow()))
  559. {
  560. Error("InventoryLocation::WriteToContext - cannot write to context! failed PXY, arg=row");
  561. return false;
  562. }
  563. if (!ctx.Write(GetCol()))
  564. {
  565. Error("InventoryLocation::WriteToContext - cannot write to context! failed PXY, arg=col");
  566. return false;
  567. }
  568. if (!ctx.Write(GetFlip()))
  569. {
  570. Error("InventoryLocation::WriteToContext - cannot write to context! failed PXY, arg=flp");
  571. return false;
  572. }
  573. break;
  574. }
  575. case InventoryLocationType.VEHICLE:
  576. {
  577. if (!ctx.Write(GetParent()))
  578. {
  579. Error("InventoryLocation::WriteToContext - cannot write to context! failed VHC, arg=parent");
  580. return false;
  581. }
  582. if (!ctx.Write(GetItem()))
  583. {
  584. Error("InventoryLocation::WriteToContext - cannot write to context! failed VHC, arg=item");
  585. return false;
  586. }
  587. if (!ctx.Write(GetIdx()))
  588. {
  589. Error("InventoryLocation::WriteToContext - cannot write to context! failed VHC, arg=idx");
  590. return false;
  591. }
  592. break;
  593. }
  594. default:
  595. {
  596. Error("WriteToContext - really unknown location type, this should not happen, type=" + GetType());
  597. return false;
  598. }
  599. }
  600. return true;
  601. }
  602. };
  603. bool OptionalLocationWriteToContext(InventoryLocation loc, notnull ParamsWriteContext ctx)
  604. {
  605. if (loc)
  606. {
  607. if (!ctx.Write(true))
  608. {
  609. Error("OptionalLocationWriteToContext - cannot write 1 to context!");
  610. return false;
  611. }
  612. return loc.WriteToContext(ctx);
  613. }
  614. else
  615. {
  616. if (!ctx.Write(false))
  617. {
  618. Error("OptionalLocationWriteToContext - cannot write 0 to context!");
  619. return false;
  620. }
  621. }
  622. return true;
  623. }
  624. bool OptionalLocationReadFromContext(out InventoryLocation loc, notnull ParamsReadContext ctx)
  625. {
  626. bool present = false;
  627. if (!ctx.Read(present))
  628. {
  629. Error("OptionalLocationReadFromContext - cannot read bool from context!");
  630. return false;
  631. }
  632. if (!present)
  633. return true;
  634. loc = new InventoryLocation();
  635. if (!loc.ReadFromContext(ctx))
  636. {
  637. Error("OptionalLocationReadFromContext - cannot read (present) inventorylocation from context!");
  638. return false;
  639. }
  640. return true;
  641. }