Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.87 KB | None | 0 0
  1. void ItemSetStack(vector<PhysObject>& from, uint64 id_item, int to) {
  2.  
  3.  
  4. int item_loc = ItemGetById(from, id_item);
  5.  
  6. if(to == 0)
  7. from.erase(from.begin() + item_loc, from.begin() + item_loc + 1);
  8. else
  9. {
  10. float weight_each = str2float(PropGetDeep(from[item_loc], "ItemWield", "WeightEach"));
  11. float weight_final = weight_each * to;
  12.  
  13. PropSetDeep(from[item_loc], "ItemWield", "Stack", to);
  14. PropSetDeep(from[item_loc], "ItemWield", "Weight", weight_final);
  15. }
  16. }
  17.  
  18. void ItemSetStack(PhysObject& item, int to) {
  19.  
  20. float weight_each = str2float(PropGetDeep(item, "ItemWield", "WeightEach"));
  21. float weight_final = weight_each * to;
  22.  
  23. PropSetDeep(item, "ItemWield", "Stack", to);
  24. PropSetDeep(item, "ItemWield", "Weight", weight_final);
  25. }
  26.  
  27.  
  28. void ItemConsume(vector<PhysObject>& within, PhysObject& item) {
  29.  
  30. ItemSetStack(within, item.id, str2int(PropGetDeep(item, "ItemWield", "Stack")) - 1);
  31.  
  32. }
  33.  
  34.  
  35. void ItemSplit(vector<PhysObject>& from, uint64 id_item, int to) {
  36.  
  37.  
  38. PhysObject* item = &from[ItemGetById(from, id_item)];
  39. int stack = str2int(PropGetDeep(*item, "ItemWield", "Stack"));
  40.  
  41. from.push_back(*item);
  42. GiveID(from[from.size() - 1].id);
  43.  
  44. ItemSetStack(from, id_item, stack - to);
  45. ItemSetStack(from, from[from.size() - 1].id, to);
  46. }
  47.  
  48.  
  49.  
  50. //////////////////////////////////////////////////////////////////////////
  51. // just move an item with proper stack count stuff, all weight/addon/compatibility checks have taken place BEFORE THIS
  52. //////////////////////////////////////////////////////////////////////////
  53.  
  54.  
  55.  
  56. //void ItemMoveToInv(
  57.  
  58.  
  59.  
  60. string ItemMove(vector<PhysObject>& from, vector<PhysObject>& to, uint64 id_item, int how_much_allowed) {
  61.  
  62.  
  63.  
  64. if(how_much_allowed > 0)
  65. {
  66. int item_loc = ItemGetById(from, id_item);
  67. string name = from[item_loc].name;
  68.  
  69. string stack = PropGetDeep(from[item_loc], "ItemWield", "Stack");
  70. string nostack = PropGetDeep(from[item_loc], "ItemWield", "NoStack");
  71.  
  72. //
  73. int found = vecontains(to, from[item_loc].name); // is an item of this type already there and is our item stackable?
  74.  
  75. if(found != -1 && nostack == "false")
  76. {
  77. string stack2 = PropGetDeep(to[found], "ItemWield", "Stack");
  78. stringmath(stack2, how_much_allowed, '+');
  79. ItemSetStack(to[found], str2int(stack2));
  80. stringmath(stack, how_much_allowed, '-');
  81. }
  82.  
  83. // guess not, creating new item
  84.  
  85. else
  86. {
  87. to.push_back(from[item_loc]);
  88. GiveID(to[to.size()-1].id);
  89.  
  90. ItemSetStack(to[to.size()-1], how_much_allowed);
  91. stringmath(stack, how_much_allowed, '-');
  92. }
  93. if(str2int(stack) > 0) ItemSetStack(from[item_loc], str2int(stack));
  94. else
  95. if(!from.empty())
  96. from.erase(from.begin() + item_loc, from.begin() + item_loc + 1); // deleting the item cause its stack is 0
  97.  
  98.  
  99.  
  100. cout << " ** moved: " << name << " x" << how_much_allowed << "\n";
  101. return "";
  102. }
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109. void ItemMoveWeightCheck(PhysObject& from, PhysObject& to, uint64 id_item) {
  110.  
  111.  
  112. int item_loc = ItemGetById(from.inventory, id_item);
  113.  
  114. int stack = str2int(PropGetDeep(from.inventory[item_loc], "ItemWield", "Stack"));
  115. float weighteach = str2float(PropGetDeep(from.inventory[item_loc], "ItemWield", "WeightEach"));
  116. float capacity = str2float(PropGetDeep(to, "Container", "Capacity"));
  117. float used_capacity = InventoryGetWeight(to.inventory);
  118.  
  119. int max_moveable = (capacity - used_capacity) / weighteach;
  120.  
  121. clamp(max_moveable, stack);
  122.  
  123. if(max_moveable > 0)
  124. ItemMove(from.inventory, to.inventory, id_item, max_moveable);
  125. else
  126. cout<<"\nno space for more items\n";
  127. }
  128.  
  129.  
  130. void ItemMoveWeightCheck(vector<PhysObject>& from, PhysObject& to, uint64 id_item) {
  131.  
  132.  
  133. int item_loc = ItemGetById(from, id_item);
  134.  
  135. int stack = str2int(PropGetDeep(from[item_loc], "ItemWield", "Stack"));
  136. float weighteach = str2float(PropGetDeep(from[item_loc], "ItemWield", "WeightEach"));
  137. float capacity = str2float(PropGetDeep(to, "Container", "Capacity"));
  138. float used_capacity = InventoryGetWeight(to.inventory);
  139.  
  140. int max_moveable = (capacity - used_capacity) / weighteach;
  141.  
  142. clamp(max_moveable, stack);
  143.  
  144. if(max_moveable > 0)
  145. ItemMove(from, to.inventory, id_item, max_moveable);
  146. else
  147. cout<<"\nno space for more items\n";
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. bool AddonTest(vector<PhysObject>& items, uint64 id_item, uint64 id_addon) {
  156.  
  157.  
  158. int addon_loc = ItemGetById(items, id_addon); // where the addon is located in the inventory vector
  159. int item_loc = ItemGetById(items, id_item); // where the item we are attaching the addon to is --
  160.  
  161. Item prodtemplate = ItemGetTemplate(items[item_loc]); // the item's original template
  162. Item prodtemplate2 = ItemGetTemplate(items[addon_loc]); // the addon's original template
  163.  
  164.  
  165.  
  166. for(int i=0; i<prodtemplate.addons.size(); i++)
  167. if(prodtemplate.addons[i].product_type == prodtemplate2.product_type)
  168. if(prodtemplate.addons[i].product_standard == prodtemplate2.product_standard)
  169. return true;
  170.  
  171. return false;
  172. }
  173.  
  174.  
  175. bool AddonTestWorld(vector<PhysObject>& items, vector<PhysObject>& worlditems, uint64 id_item, uint64 id_addon) {
  176.  
  177.  
  178. int addon_loc = ItemGetById(items, id_addon); // where the addon is located in the inventory vector
  179. int item_loc = ItemGetById(worlditems, id_item); // where the item we are attaching the addon to is --
  180.  
  181. Item prodtemplate = ItemGetTemplate(worlditems[item_loc]); // the item's original template
  182. Item prodtemplate2 = ItemGetTemplate(items[addon_loc]); // the addon's original template
  183.  
  184.  
  185. for(int i=0; i<prodtemplate.addons.size(); i++)
  186. if(prodtemplate.addons[i].product_type == prodtemplate2.product_type)
  187. if(prodtemplate.addons[i].product_standard == prodtemplate2.product_standard)
  188. return true;
  189.  
  190. return false;
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197. string AttachAddon(vector<PhysObject>& items, uint64 id_item, uint64 id_addon, int clamper) {
  198.  
  199.  
  200. int addon_loc = ItemGetById(items, id_addon); // where the addon is located in the inventory vector
  201. int item_loc = ItemGetById(items, id_item); // where the item we are attaching the addon to is --
  202.  
  203. Item prodtemplate = ItemGetTemplate(items[item_loc]); // the item's original template
  204. Item prodtemplate2 = ItemGetTemplate(items[addon_loc]); // the addon's original template
  205.  
  206. int addon_num = ItemGetAddonsNum(items[item_loc], prodtemplate2.product_standard, prodtemplate2.product_type); // how many addons of this type we already have attached to the item
  207. string stack = PropGetDeep(items[addon_loc], "ItemWield", "Stack");
  208.  
  209. for(int i=0; i<prodtemplate.addons.size(); i++)
  210. if(prodtemplate.addons[i].product_type == prodtemplate2.product_type)
  211. if(prodtemplate.addons[i].product_standard == prodtemplate2.product_standard)
  212. {
  213. // we can attach this addon
  214. if(prodtemplate.addons[i].count > addon_num) // but only if there is room for more addons according to the template
  215. {
  216. int howmuch = prodtemplate.addons[i].count - addon_num;
  217. clamp(howmuch, str2int(stack));
  218.  
  219. if(clamper > 0)
  220. clamp(howmuch, clamper);
  221.  
  222. //
  223. ItemMove(items, items[item_loc].inventory, id_addon, howmuch);
  224. //
  225.  
  226. return "\n\n -- Addon attached successfully: " + prodtemplate2.name + " -> " + prodtemplate.name;
  227. }
  228. else return "\n\n -- Cannot attach more addons of type";
  229. }
  230. else return "\n\n -- Addon incompatible with item";
  231.  
  232. }
  233.  
  234.  
  235.  
  236.  
  237. string DetachAddon(vector<PhysObject>& items, uint64 id_item, uint64 id_addon) {
  238.  
  239.  
  240. int item_loc = ItemGetById(items, id_item); // where the item we are detaching from is located in the inventory vector
  241. int addon_loc = ItemGetById(items[item_loc].inventory, id_addon); // where the addon is located in the item's addons vector
  242.  
  243. string stack = PropGetDeep(items[item_loc].inventory[addon_loc], "ItemWield", "Stack");
  244.  
  245. ItemMove(items[item_loc].inventory, items, id_addon, str2int(stack));
  246.  
  247. return "\n\n -- Addon detached";
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254. string AttachAddonWorld(vector<PhysObject>& items, vector<PhysObject>& worlditems, uint64 id_item, uint64 id_addon, int clamper) {
  255.  
  256.  
  257. int addon_loc = ItemGetById(items, id_addon); // where the addon is located in the inventory vector
  258. int item_loc = ItemGetById(worlditems, id_item); // where the item we are attaching the addon to is --
  259.  
  260. Item prodtemplate = ItemGetTemplate(worlditems[item_loc]); // the item's original template
  261. Item prodtemplate2 = ItemGetTemplate(items[addon_loc]); // the addon's original template
  262.  
  263. int addon_num = ItemGetAddonsNum(worlditems[item_loc], prodtemplate2.product_standard, prodtemplate2.product_type); // how many addons of this type we already have attached to the item
  264. string stack = PropGetDeep(items[addon_loc], "ItemWield", "Stack");
  265.  
  266. for(int i=0; i<prodtemplate.addons.size(); i++)
  267. if(prodtemplate.addons[i].product_type == prodtemplate2.product_type)
  268. if(prodtemplate.addons[i].product_standard == prodtemplate2.product_standard)
  269. {
  270. // we can attach this addon
  271. if(prodtemplate.addons[i].count > addon_num) // but only if there is room for more addons according to the template
  272. {
  273. int howmuch = prodtemplate.addons[i].count - addon_num;
  274. clamp(howmuch, str2int(stack));
  275.  
  276. if(clamper > 0)
  277. clamp(howmuch, clamper);
  278.  
  279. //
  280. ItemMove(items, worlditems[item_loc].inventory, id_addon, howmuch);
  281. //
  282.  
  283. return "\n\n -- WORLD Addon attached successfully: " + prodtemplate2.name + " -> " + prodtemplate.name;
  284. }
  285. else return "\n\n -- WORLD Cannot attach more addons of type";
  286.  
  287. }
  288. else return "\n\n -- WORLD Addon incompatible with item";
  289.  
  290. }
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297. string DetachAddonWorld(vector<PhysObject>& items, vector<PhysObject>& worlditems, uint64 id_item, uint64 id_addon) {
  298.  
  299.  
  300. int item_loc = ItemGetById(worlditems, id_item); // where the item we are detaching from is located in the inventory vector
  301. int addon_loc = ItemGetById(worlditems[item_loc].inventory, id_addon); // where the addon is located in the item's addons vector
  302.  
  303. string stack = PropGetDeep(items[item_loc].inventory[addon_loc], "ItemWield", "Stack");
  304.  
  305. ItemMove(worlditems[item_loc].inventory, items, id_addon, str2int(stack));
  306.  
  307. return "\n\n -- WORLD Addon detached";
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316. ------------------------------
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326. void ItemUse(uint64 id_item) {
  327.  
  328.  
  329. int item_loc = ItemGetById(gd->player.inventory, id_item);
  330.  
  331. //
  332.  
  333. if(item_loc == -1) // the item used must be an addon, or an addon's addon...
  334. {
  335. foreach(PhysObject obj, gd->player.inventory)
  336. {
  337. item_loc = ItemGetById(obj.inventory, id_item);
  338. if(item_loc != -1)
  339. break;
  340. }
  341. }
  342.  
  343. //
  344.  
  345. PhysObject* item = &gd->player.inventory[item_loc];
  346.  
  347. string usetype = PropGetDeep(*item, "ItemWield", "UseType");
  348.  
  349.  
  350. if(usetype == "useconsume" || usetype == "use" || usetype == "usewield")
  351. for(int i=0; i<item->BaseModules.size(); i++)
  352. {
  353. if(item->BaseModules[i].functional)
  354. {
  355. int poss = vecontains(item->BaseModules[i].flags, "scriptuse");
  356.  
  357. if(poss != -1)
  358. if(usetype != "usewield" || (usetype == "usewield" && PropGetDeep(*item, "ItemWield", "Location") != "inventory"))
  359. Scriptron(*item, item->BaseModules[i].flags[poss+1], WordVectorBuilder("module_index", int2str(i)));
  360. else
  361. cout<<"\nmust wield item before use";
  362. // possible to add here an objpointer id to the PhysObject that fired the script mebbe
  363. }
  364. }
  365.  
  366. if(usetype == "usewith" || usetype == "usewithconsume")
  367. {
  368. //
  369. int loc = InventoryPick();
  370. //
  371.  
  372. for(int i=0; i<item->BaseModules.size(); i++)
  373. {
  374. int poss = vecontains(item->BaseModules[i].flags, "scriptuseitem");
  375.  
  376. if(poss != -1)
  377. Scriptron(*item, item->BaseModules[i].flags[poss+1], WordVectorBuilder("inv_loc", int2str(loc))); // fire the 'scriptuseitem' script with the item loc as param
  378. }
  379. }
  380.  
  381.  
  382.  
  383. if(usetype == "useconsume" || usetype == "usewithconsume")
  384. ItemSetStack(gd->player.inventory, item->id, str2int(PropGetDeep(*item, "ItemWield", "Stack")) - 1);
  385.  
  386. if(usetype == "modules")
  387. ToggleModules(*item);
  388. }
  389.  
  390.  
  391. void ItemUse(uint64 id_item1, uint64 id_item2) {
  392.  
  393.  
  394. int item_loc1 = ItemGetById(gd->player.inventory, id_item1);
  395. int item_loc2 = ItemGetById(gd->player.inventory, id_item2);
  396. PhysObject* item1 = &gd->player.inventory[item_loc1];
  397. PhysObject* item2 = &gd->player.inventory[item_loc2];
  398.  
  399. string usetype = PropGetDeep(*item1, "ItemWield", "UseType");
  400. string usetype2 = PropGetDeep(*item2, "ItemWield", "UseType");
  401.  
  402. if(AddonTest(playainv, id_item2, id_item1))
  403. {
  404. int clmp;
  405. cout<<"attach how many: "; cin>>clmp; // should only trigger if receiver can accept more than 1? maybe holding down shift or something
  406. AttachAddon(playainv, id_item2, id_item1, clmp);
  407. }
  408.  
  409. else
  410. if(usetype == "usewith")
  411. for(int i=0; i<item1->BaseModules.size(); i++)
  412. {
  413. int poss = vecontains(item1->BaseModules[i].flags, "scriptuseitem");
  414.  
  415. if(poss != -1)
  416. Scriptron(*item1, item1->BaseModules[i].flags[poss+1], WordVectorBuilder("inv_loc", int2str(item_loc2)));
  417. }
  418. else
  419. if(usetype2 == "container")
  420. ItemMoveWeightCheck(gd->player, *item2, item1->id);
  421.  
  422. else cout<<"\nno result\n";
  423.  
  424. }
  425.  
  426.  
  427.  
  428.  
  429. void WorldUse(uint64 id) {
  430.  
  431. int item_loc = ItemGetById(gd->objects, id);
  432. PhysObject* item = &gd->objects[item_loc];
  433.  
  434. string usetype = PropGetDeep(*item, "ItemWield", "UseType");
  435. string used = PropGetDeep(*item, "ItemWield", "Used");
  436.  
  437.  
  438. if(used != "world" && used != "null")
  439. ItemMoveWeightCheck(gd->objects, gd->player, id); //ItemPickup(playa, gd->objects, id, -1);
  440. else
  441. if(usetype == "use" && (used == "world" || used =="null"))
  442. for(int i=0; i<item->BaseModules.size(); i++)
  443. {
  444. int poss = vecontains(item->BaseModules[i].flags, "scriptuse");
  445.  
  446. if(poss != -1)
  447. Scriptron(*item, item->BaseModules[i].flags[poss+1], WordVectorBuilder("module_index", int2str(i)));
  448. }
  449.  
  450. else
  451. if(usetype == "usewith")
  452. {
  453. //
  454. int loc = InventoryPick();
  455. //
  456.  
  457. if(AddonTestWorld(playainv, gd->objects, id, playainv[loc].id))
  458. {
  459. int clmp;
  460. cout<<"attach how many: "; cin>>clmp; // will be optional, maybe holding down shift or something
  461. cout<<AttachAddonWorld(playainv, gd->objects, id, playainv[loc].id, clmp);
  462. }
  463.  
  464. for(int i=0; i<item->BaseModules.size(); i++)
  465. {
  466. int poss = vecontains(item->BaseModules[i].flags, "scriptuseitem");
  467.  
  468. if(poss != -1)
  469. Scriptron(*item, item->BaseModules[i].flags[poss+1], WordVectorBuilder("inv_loc", int2str(loc))); // fire the 'scriptuseitem' script with the item loc as param
  470. }
  471. }
  472. else
  473. if(usetype == "container")
  474. {
  475. // do an inventorypick analog for the contents of the world container, clicking on any item tries to move it to inventory
  476. // until you close the world inventory thing
  477.  
  478. // OR
  479.  
  480. // better what if it opened two inventory picks? no drag-dropping complication, just clickin on objects stalker-style to move
  481. // them from one screen to the other, own
  482.  
  483. // the stuff i did before still applies for when you just want to dragdrop an item into a bag in your inventory etc own
  484.  
  485.  
  486. // -- screen should have option to pick the whole thing up if it's pickupable, i.e not a giant crate
  487. }
  488. }
  489.  
  490.  
  491. int InventoryPick() {
  492.  
  493. int pick;
  494.  
  495. cout<<"opening inventory to pick: \n";
  496.  
  497. for(int i=0; i<gd->player.inventory.size(); i++)
  498. {
  499. Item base = ItemGetTemplate(gd->player.inventory[i]);
  500.  
  501. cout<<i<<" - "<<gd->player.inventory[i].name<<" - "<<base.product_type<<" - "<<base.product_standard<<
  502. " x"<<PropGetDeep(gd->player.inventory[i], "ItemWield", "Stack")<<"\n";
  503. }
  504.  
  505. cout<<"\npick: "; cin>>pick;
  506.  
  507. return pick;
  508.  
  509. }
  510.  
  511.  
  512. int InventoryGetCount(const vector<PhysObject>& inventory) {
  513.  
  514. int ret = 0;
  515.  
  516. foreach(PhysObject obj, inventory)
  517. ret += str2int(PropGetDeep(obj, "ItemWield", "Stack"));
  518.  
  519. return ret;
  520. }
  521.  
  522.  
  523. float InventoryGetWeight(const vector<PhysObject>& inventory) {
  524.  
  525.  
  526. float weight = 0;
  527.  
  528.  
  529. foreach(PhysObject i, inventory)
  530. {
  531. weight += str2float(PropGetDeep(i, "ItemWield", "Weight"));
  532.  
  533. if(i.inventory.size() != 0)
  534. weight += InventoryGetWeight(i.inventory);
  535. }
  536.  
  537. return weight;
  538. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement