Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TINV_SubInventory {
- var Id = "NOT_DEFINED";
- var Width, Height;
- var Items = { };
- var Slots = { }; // Slots point at items
- var Player; // Serverside only, set by inventory constructor
- /*
- Constructor
- */
- function TINV_SubInventory(id, width, height) {
- this.Id = id;
- this.Width, this.Height = width, height;
- for(var x = 1; x <= width; x++) {
- this.Slots[x] = { };
- }
- }
- /*
- Updates slots. Should be called after every modification.
- */
- function UpdateSlots() {
- for(var x = 1; x <= this.Width; x++)
- this.Slots[x] = { };
- foreach(var item in this.Items) {
- for(var itemX = item.SlotX; itemX <= item.SlotX + item.Width - 1; itemX++)
- for(var itemY = item.SlotY; itemY <= item.SlotY + item.Height - 1; itemY++)
- this.Slots[itemX][itemY] = item;
- }
- }
- /*
- Determines, if the player can access/modify the inventory at the moment.
- */
- function CanAccess(ply) {
- return true;
- }
- /*
- Item functions
- */
- /*
- Removes specified item object from inventory
- */
- function RemoveItem(item) {
- table.RemoveByValue(this.Items, item);
- this:UpdateSlots();
- }
- /*
- Returns true, if there's a place for an item with specific size on specific position
- */
- function HasPlaceOn(x, y, width, height) {
- var ok = true;
- for(var itemX = x; itemX <= x + width - 1; itemX++) {
- if(!ok) break;
- for(var itemY = y; itemY <= y + height - 1; itemY++)
- if(itemX < 1 || itemY < 1 || itemX > this.Width || itemY > this.Height || this.Slots[itemX][itemY]) { // We found an item on this slot, no place on this position
- return false;
- }
- }
- return true;
- }
- /*
- Returns true, slotX, slotY, if there's a place for an item with specific size, otherwise, returns false.
- */
- function HasPlace(width, height) {
- for(var y = 1; y <= this.Height - height + 1; y++)
- for(var x = 1; x <= this.Width - width + 1; x++) {
- var ok = true;
- for(var itemX = x; itemX <= x + width - 1; itemX++) {
- if(!ok) break;
- for(var itemY = y; itemY <= y + height - 1; itemY++)
- if(itemX < 1 || itemY < 1 || itemX > this.Width || itemY > this.Height || this.Slots[itemX][itemY]) { // We found an item on this slot, no place on this position
- ok = false;
- break;
- }
- }
- if(ok)
- return true, x, y;
- }
- return false;
- }
- /*
- Adds item to inventory - first free slot.
- */
- function AddItem(item) {
- if(!item)
- error("TINV Error: Trying to add a null item!");
- var ok, x, y = this:HasPlace(item.Width, item.Height);
- if(ok)
- print(string.format("There's a place for item on X: %s, Y: %s", x, y));
- else {
- print("No place for your item :(");
- return false;
- }
- this.Items[#this.Items+1] = item;
- item:UpdateSubInventory(this, x, y);
- this:UpdateSlots();
- if(SERVER)
- TINV.UpdatePlayerSubInventory(this.Player, this);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement