Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ItemSetStack(vector<PhysObject>& from, uint64 id_item, int to) {
- int item_loc = ItemGetById(from, id_item);
- if(to == 0)
- from.erase(from.begin() + item_loc, from.begin() + item_loc + 1);
- else
- {
- float weight_each = str2float(PropGetDeep(from[item_loc], "ItemWield", "WeightEach"));
- float weight_final = weight_each * to;
- PropSetDeep(from[item_loc], "ItemWield", "Stack", to);
- PropSetDeep(from[item_loc], "ItemWield", "Weight", weight_final);
- }
- }
- void ItemSetStack(PhysObject& item, int to) {
- float weight_each = str2float(PropGetDeep(item, "ItemWield", "WeightEach"));
- float weight_final = weight_each * to;
- PropSetDeep(item, "ItemWield", "Stack", to);
- PropSetDeep(item, "ItemWield", "Weight", weight_final);
- }
- void ItemConsume(vector<PhysObject>& within, PhysObject& item) {
- ItemSetStack(within, item.id, str2int(PropGetDeep(item, "ItemWield", "Stack")) - 1);
- }
- void ItemSplit(vector<PhysObject>& from, uint64 id_item, int to) {
- PhysObject* item = &from[ItemGetById(from, id_item)];
- int stack = str2int(PropGetDeep(*item, "ItemWield", "Stack"));
- from.push_back(*item);
- GiveID(from[from.size() - 1].id);
- ItemSetStack(from, id_item, stack - to);
- ItemSetStack(from, from[from.size() - 1].id, to);
- }
- //////////////////////////////////////////////////////////////////////////
- // just move an item with proper stack count stuff, all weight/addon/compatibility checks have taken place BEFORE THIS
- //////////////////////////////////////////////////////////////////////////
- //void ItemMoveToInv(
- string ItemMove(vector<PhysObject>& from, vector<PhysObject>& to, uint64 id_item, int how_much_allowed) {
- if(how_much_allowed > 0)
- {
- int item_loc = ItemGetById(from, id_item);
- string name = from[item_loc].name;
- string stack = PropGetDeep(from[item_loc], "ItemWield", "Stack");
- string nostack = PropGetDeep(from[item_loc], "ItemWield", "NoStack");
- //
- int found = vecontains(to, from[item_loc].name); // is an item of this type already there and is our item stackable?
- if(found != -1 && nostack == "false")
- {
- string stack2 = PropGetDeep(to[found], "ItemWield", "Stack");
- stringmath(stack2, how_much_allowed, '+');
- ItemSetStack(to[found], str2int(stack2));
- stringmath(stack, how_much_allowed, '-');
- }
- // guess not, creating new item
- else
- {
- to.push_back(from[item_loc]);
- GiveID(to[to.size()-1].id);
- ItemSetStack(to[to.size()-1], how_much_allowed);
- stringmath(stack, how_much_allowed, '-');
- }
- if(str2int(stack) > 0) ItemSetStack(from[item_loc], str2int(stack));
- else
- if(!from.empty())
- from.erase(from.begin() + item_loc, from.begin() + item_loc + 1); // deleting the item cause its stack is 0
- cout << " ** moved: " << name << " x" << how_much_allowed << "\n";
- return "";
- }
- }
- void ItemMoveWeightCheck(PhysObject& from, PhysObject& to, uint64 id_item) {
- int item_loc = ItemGetById(from.inventory, id_item);
- int stack = str2int(PropGetDeep(from.inventory[item_loc], "ItemWield", "Stack"));
- float weighteach = str2float(PropGetDeep(from.inventory[item_loc], "ItemWield", "WeightEach"));
- float capacity = str2float(PropGetDeep(to, "Container", "Capacity"));
- float used_capacity = InventoryGetWeight(to.inventory);
- int max_moveable = (capacity - used_capacity) / weighteach;
- clamp(max_moveable, stack);
- if(max_moveable > 0)
- ItemMove(from.inventory, to.inventory, id_item, max_moveable);
- else
- cout<<"\nno space for more items\n";
- }
- void ItemMoveWeightCheck(vector<PhysObject>& from, PhysObject& to, uint64 id_item) {
- int item_loc = ItemGetById(from, id_item);
- int stack = str2int(PropGetDeep(from[item_loc], "ItemWield", "Stack"));
- float weighteach = str2float(PropGetDeep(from[item_loc], "ItemWield", "WeightEach"));
- float capacity = str2float(PropGetDeep(to, "Container", "Capacity"));
- float used_capacity = InventoryGetWeight(to.inventory);
- int max_moveable = (capacity - used_capacity) / weighteach;
- clamp(max_moveable, stack);
- if(max_moveable > 0)
- ItemMove(from, to.inventory, id_item, max_moveable);
- else
- cout<<"\nno space for more items\n";
- }
- bool AddonTest(vector<PhysObject>& items, uint64 id_item, uint64 id_addon) {
- int addon_loc = ItemGetById(items, id_addon); // where the addon is located in the inventory vector
- int item_loc = ItemGetById(items, id_item); // where the item we are attaching the addon to is --
- Item prodtemplate = ItemGetTemplate(items[item_loc]); // the item's original template
- Item prodtemplate2 = ItemGetTemplate(items[addon_loc]); // the addon's original template
- for(int i=0; i<prodtemplate.addons.size(); i++)
- if(prodtemplate.addons[i].product_type == prodtemplate2.product_type)
- if(prodtemplate.addons[i].product_standard == prodtemplate2.product_standard)
- return true;
- return false;
- }
- bool AddonTestWorld(vector<PhysObject>& items, vector<PhysObject>& worlditems, uint64 id_item, uint64 id_addon) {
- int addon_loc = ItemGetById(items, id_addon); // where the addon is located in the inventory vector
- int item_loc = ItemGetById(worlditems, id_item); // where the item we are attaching the addon to is --
- Item prodtemplate = ItemGetTemplate(worlditems[item_loc]); // the item's original template
- Item prodtemplate2 = ItemGetTemplate(items[addon_loc]); // the addon's original template
- for(int i=0; i<prodtemplate.addons.size(); i++)
- if(prodtemplate.addons[i].product_type == prodtemplate2.product_type)
- if(prodtemplate.addons[i].product_standard == prodtemplate2.product_standard)
- return true;
- return false;
- }
- string AttachAddon(vector<PhysObject>& items, uint64 id_item, uint64 id_addon, int clamper) {
- int addon_loc = ItemGetById(items, id_addon); // where the addon is located in the inventory vector
- int item_loc = ItemGetById(items, id_item); // where the item we are attaching the addon to is --
- Item prodtemplate = ItemGetTemplate(items[item_loc]); // the item's original template
- Item prodtemplate2 = ItemGetTemplate(items[addon_loc]); // the addon's original template
- 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
- string stack = PropGetDeep(items[addon_loc], "ItemWield", "Stack");
- for(int i=0; i<prodtemplate.addons.size(); i++)
- if(prodtemplate.addons[i].product_type == prodtemplate2.product_type)
- if(prodtemplate.addons[i].product_standard == prodtemplate2.product_standard)
- {
- // we can attach this addon
- if(prodtemplate.addons[i].count > addon_num) // but only if there is room for more addons according to the template
- {
- int howmuch = prodtemplate.addons[i].count - addon_num;
- clamp(howmuch, str2int(stack));
- if(clamper > 0)
- clamp(howmuch, clamper);
- //
- ItemMove(items, items[item_loc].inventory, id_addon, howmuch);
- //
- return "\n\n -- Addon attached successfully: " + prodtemplate2.name + " -> " + prodtemplate.name;
- }
- else return "\n\n -- Cannot attach more addons of type";
- }
- else return "\n\n -- Addon incompatible with item";
- }
- string DetachAddon(vector<PhysObject>& items, uint64 id_item, uint64 id_addon) {
- int item_loc = ItemGetById(items, id_item); // where the item we are detaching from is located in the inventory vector
- int addon_loc = ItemGetById(items[item_loc].inventory, id_addon); // where the addon is located in the item's addons vector
- string stack = PropGetDeep(items[item_loc].inventory[addon_loc], "ItemWield", "Stack");
- ItemMove(items[item_loc].inventory, items, id_addon, str2int(stack));
- return "\n\n -- Addon detached";
- }
- string AttachAddonWorld(vector<PhysObject>& items, vector<PhysObject>& worlditems, uint64 id_item, uint64 id_addon, int clamper) {
- int addon_loc = ItemGetById(items, id_addon); // where the addon is located in the inventory vector
- int item_loc = ItemGetById(worlditems, id_item); // where the item we are attaching the addon to is --
- Item prodtemplate = ItemGetTemplate(worlditems[item_loc]); // the item's original template
- Item prodtemplate2 = ItemGetTemplate(items[addon_loc]); // the addon's original template
- 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
- string stack = PropGetDeep(items[addon_loc], "ItemWield", "Stack");
- for(int i=0; i<prodtemplate.addons.size(); i++)
- if(prodtemplate.addons[i].product_type == prodtemplate2.product_type)
- if(prodtemplate.addons[i].product_standard == prodtemplate2.product_standard)
- {
- // we can attach this addon
- if(prodtemplate.addons[i].count > addon_num) // but only if there is room for more addons according to the template
- {
- int howmuch = prodtemplate.addons[i].count - addon_num;
- clamp(howmuch, str2int(stack));
- if(clamper > 0)
- clamp(howmuch, clamper);
- //
- ItemMove(items, worlditems[item_loc].inventory, id_addon, howmuch);
- //
- return "\n\n -- WORLD Addon attached successfully: " + prodtemplate2.name + " -> " + prodtemplate.name;
- }
- else return "\n\n -- WORLD Cannot attach more addons of type";
- }
- else return "\n\n -- WORLD Addon incompatible with item";
- }
- string DetachAddonWorld(vector<PhysObject>& items, vector<PhysObject>& worlditems, uint64 id_item, uint64 id_addon) {
- int item_loc = ItemGetById(worlditems, id_item); // where the item we are detaching from is located in the inventory vector
- int addon_loc = ItemGetById(worlditems[item_loc].inventory, id_addon); // where the addon is located in the item's addons vector
- string stack = PropGetDeep(items[item_loc].inventory[addon_loc], "ItemWield", "Stack");
- ItemMove(worlditems[item_loc].inventory, items, id_addon, str2int(stack));
- return "\n\n -- WORLD Addon detached";
- }
- ------------------------------
- void ItemUse(uint64 id_item) {
- int item_loc = ItemGetById(gd->player.inventory, id_item);
- //
- if(item_loc == -1) // the item used must be an addon, or an addon's addon...
- {
- foreach(PhysObject obj, gd->player.inventory)
- {
- item_loc = ItemGetById(obj.inventory, id_item);
- if(item_loc != -1)
- break;
- }
- }
- //
- PhysObject* item = &gd->player.inventory[item_loc];
- string usetype = PropGetDeep(*item, "ItemWield", "UseType");
- if(usetype == "useconsume" || usetype == "use" || usetype == "usewield")
- for(int i=0; i<item->BaseModules.size(); i++)
- {
- if(item->BaseModules[i].functional)
- {
- int poss = vecontains(item->BaseModules[i].flags, "scriptuse");
- if(poss != -1)
- if(usetype != "usewield" || (usetype == "usewield" && PropGetDeep(*item, "ItemWield", "Location") != "inventory"))
- Scriptron(*item, item->BaseModules[i].flags[poss+1], WordVectorBuilder("module_index", int2str(i)));
- else
- cout<<"\nmust wield item before use";
- // possible to add here an objpointer id to the PhysObject that fired the script mebbe
- }
- }
- if(usetype == "usewith" || usetype == "usewithconsume")
- {
- //
- int loc = InventoryPick();
- //
- for(int i=0; i<item->BaseModules.size(); i++)
- {
- int poss = vecontains(item->BaseModules[i].flags, "scriptuseitem");
- if(poss != -1)
- Scriptron(*item, item->BaseModules[i].flags[poss+1], WordVectorBuilder("inv_loc", int2str(loc))); // fire the 'scriptuseitem' script with the item loc as param
- }
- }
- if(usetype == "useconsume" || usetype == "usewithconsume")
- ItemSetStack(gd->player.inventory, item->id, str2int(PropGetDeep(*item, "ItemWield", "Stack")) - 1);
- if(usetype == "modules")
- ToggleModules(*item);
- }
- void ItemUse(uint64 id_item1, uint64 id_item2) {
- int item_loc1 = ItemGetById(gd->player.inventory, id_item1);
- int item_loc2 = ItemGetById(gd->player.inventory, id_item2);
- PhysObject* item1 = &gd->player.inventory[item_loc1];
- PhysObject* item2 = &gd->player.inventory[item_loc2];
- string usetype = PropGetDeep(*item1, "ItemWield", "UseType");
- string usetype2 = PropGetDeep(*item2, "ItemWield", "UseType");
- if(AddonTest(playainv, id_item2, id_item1))
- {
- int clmp;
- cout<<"attach how many: "; cin>>clmp; // should only trigger if receiver can accept more than 1? maybe holding down shift or something
- AttachAddon(playainv, id_item2, id_item1, clmp);
- }
- else
- if(usetype == "usewith")
- for(int i=0; i<item1->BaseModules.size(); i++)
- {
- int poss = vecontains(item1->BaseModules[i].flags, "scriptuseitem");
- if(poss != -1)
- Scriptron(*item1, item1->BaseModules[i].flags[poss+1], WordVectorBuilder("inv_loc", int2str(item_loc2)));
- }
- else
- if(usetype2 == "container")
- ItemMoveWeightCheck(gd->player, *item2, item1->id);
- else cout<<"\nno result\n";
- }
- void WorldUse(uint64 id) {
- int item_loc = ItemGetById(gd->objects, id);
- PhysObject* item = &gd->objects[item_loc];
- string usetype = PropGetDeep(*item, "ItemWield", "UseType");
- string used = PropGetDeep(*item, "ItemWield", "Used");
- if(used != "world" && used != "null")
- ItemMoveWeightCheck(gd->objects, gd->player, id); //ItemPickup(playa, gd->objects, id, -1);
- else
- if(usetype == "use" && (used == "world" || used =="null"))
- for(int i=0; i<item->BaseModules.size(); i++)
- {
- int poss = vecontains(item->BaseModules[i].flags, "scriptuse");
- if(poss != -1)
- Scriptron(*item, item->BaseModules[i].flags[poss+1], WordVectorBuilder("module_index", int2str(i)));
- }
- else
- if(usetype == "usewith")
- {
- //
- int loc = InventoryPick();
- //
- if(AddonTestWorld(playainv, gd->objects, id, playainv[loc].id))
- {
- int clmp;
- cout<<"attach how many: "; cin>>clmp; // will be optional, maybe holding down shift or something
- cout<<AttachAddonWorld(playainv, gd->objects, id, playainv[loc].id, clmp);
- }
- for(int i=0; i<item->BaseModules.size(); i++)
- {
- int poss = vecontains(item->BaseModules[i].flags, "scriptuseitem");
- if(poss != -1)
- Scriptron(*item, item->BaseModules[i].flags[poss+1], WordVectorBuilder("inv_loc", int2str(loc))); // fire the 'scriptuseitem' script with the item loc as param
- }
- }
- else
- if(usetype == "container")
- {
- // do an inventorypick analog for the contents of the world container, clicking on any item tries to move it to inventory
- // until you close the world inventory thing
- // OR
- // better what if it opened two inventory picks? no drag-dropping complication, just clickin on objects stalker-style to move
- // them from one screen to the other, own
- // the stuff i did before still applies for when you just want to dragdrop an item into a bag in your inventory etc own
- // -- screen should have option to pick the whole thing up if it's pickupable, i.e not a giant crate
- }
- }
- int InventoryPick() {
- int pick;
- cout<<"opening inventory to pick: \n";
- for(int i=0; i<gd->player.inventory.size(); i++)
- {
- Item base = ItemGetTemplate(gd->player.inventory[i]);
- cout<<i<<" - "<<gd->player.inventory[i].name<<" - "<<base.product_type<<" - "<<base.product_standard<<
- " x"<<PropGetDeep(gd->player.inventory[i], "ItemWield", "Stack")<<"\n";
- }
- cout<<"\npick: "; cin>>pick;
- return pick;
- }
- int InventoryGetCount(const vector<PhysObject>& inventory) {
- int ret = 0;
- foreach(PhysObject obj, inventory)
- ret += str2int(PropGetDeep(obj, "ItemWield", "Stack"));
- return ret;
- }
- float InventoryGetWeight(const vector<PhysObject>& inventory) {
- float weight = 0;
- foreach(PhysObject i, inventory)
- {
- weight += str2float(PropGetDeep(i, "ItemWield", "Weight"));
- if(i.inventory.size() != 0)
- weight += InventoryGetWeight(i.inventory);
- }
- return weight;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement