Code archives/Miscellaneous/Inventory example (Bag in bag system)

This code has been declared by its author to be Public Domain code.

Download source code

Inventory example (Bag in bag system) by Wings
(Posted 21 years ago)
Have Swords,Helments,Flower,Bags and putt em all in a bag.
Stack the gold and split em.
Right klick a bag and it will empty itself.
;Get whole source with data file here.
;http://server.voidrpg.com/2007/inventory_demo.zip

;this demo demostrates a simple inventory handling with mouse system.
Global countID=0
Global selectmode
Global selected_stuff.item

Graphics 800,600,32,0
SetBuffer BackBuffer()

Type ITEM
	Field child.ITEM[30]
	Field mother.item
	Field MaxSlots
	Field Name$
	Field id
	Field obj
	Field xpos
	Field ypos
	Field Nr ;Nr of items.
	Field Stack ;Nr of stack of same items.. good for stacking gold coins..
End Type


Dim ICON(100)
Global mark,mark2,empty,lmsg$
mark=LoadImage("mark.bmp")
mark2=LoadImage("markb.bmp")
empty=LoadImage("empty.bmp")
ICON(1)=LoadImage("sword.bmp")
ICON(2)=LoadImage("bag.bmp")
ICON(3)=LoadImage("healment.bmp")
ICON(4)=LoadImage("sheld.bmp")
ICON(5)=LoadImage("ring.bmp")
ICON(6)=LoadImage("flower.bmp")
ICON(7)=LoadAnimImage("coins.bmp",32,32,0,10)
;g=LoadAnimImage("coins.bmp",32,32,0,9)
;Stop

For i=1 To 12
	countID=countID+1
	x=Rnd(700)+50
	y=Rnd(500)+50
	stuff.item = New item
	r=Rnd(6)+1

	Select r
	Case 1;Create Sword
		stuff\Name$="Sword"
		stuff\MaxSLots=0
		stuff\id=countID
		stuff\obj=1 ;Ie sword image :)
	Case 2;Create bag
		stuff\Name$="Bag"
		stuff\maxslots=9
		stuff\id=countID
		stuff\obj=2 ;Bag image..
	Case 3;Create healment
		stuff\name$="Healment"
		stuff\MaxSlots=0
		stuff\id=countID
		stuff\obj=3 ;healment image..
	Case 4;Create sheld
		stuff\name$="Sheld"
		stuff\MaxSlots=0
		stuff\id=countID
		stuff\obj=4;Sheld image
	Case 5;Create ring
		stuff\name$="Ring"
		stuff\MaxSlots=0
		stuff\id=countID
		stuff\obj=5;Ring imgae..
	Case 6;Create a flower
		stuff\name$="Flower"
		stuff\MaxSlots=0
		stuff\id=countID
		stuff\obj=6;Flower image.
	Case 7;Create Coins.
		stuff\name$="Gold"
		stuff\MaxSlots=0
		stuff\id=countID
		stuff\obj=7
		stuff\nr=Rnd(9)+1
		stuff\stack=10
	End Select
	
	stuff\xpos=x
	stuff\ypos=y
Next









While Not KeyHit(1)

	Cls
	
	handle_input()
	drawall()
	Flip()
	
Wend










;Handles all inputs
Function handle_input()

	x=MouseX()
	y=MouseY()

	Select selectmode
	
	Case 0
		If MouseDown(1)
			For stuff.item = Each item
				If RectsOverlap(x,y,1,1,stuff\xpos,stuff\ypos,32,32) And stuff\mother=Null 
					selected_stuff=stuff
					selectmode=1
					Exit
				End If
			Next
		End If
	Case 1
		selected_stuff\xpos=x-16
		selected_stuff\ypos=y-16
		lmsg$="Picked "+selected_stuff\nr+" of "+selected_stuff\name$

		If MouseDown(1)=0 Then selectmode=2
	Case 2
		For stuff.item = Each item
			If RectsOverlap(x,y,1,1,stuff\xpos,stuff\ypos,32,32)
				If stuff\MaxSlots>0 And stuff<>selected_stuff
					If stuff\mother=Null
						For i=1 To stuff\MaxSlots
							If stuff\CHILD[i]=Null
							
								stuff\CHILD[i]=selected_stuff
								selected_stuff\mother=stuff
								Exit
						
							End If
						Next
					End If
				Else If stuff\name$="Gold" And selected_stuff\name$="Gold" And stuff<>selected_stuff And stuff\mother=Null

					stuff\nr=stuff\nr+selected_stuff\nr
					Delete selected_stuff
					lmsg$="Stacked "+stuff\nr+" of "+stuff\name$
				End If
			End If
		Next
		
		selectmode=0
	End Select


	;Splatt out all things on screen for fun :)
	If MouseDown(2)
		For stuff.item = Each item
			If RectsOverlap(x,y,1,1,stuff\xpos,stuff\ypos,32,32) And stuff\mother=Null 
				
				If stuff\name$="Gold"
					nr=Input ("Enter nr to splitt")
					If nr>stuff\nr Then nr=stuff\nr
					stuff\nr=stuff\nr-nr
					x=stuff\xpos+16
					y=stuff\ypos+16
					If stuff\nr<1 Then Delete stuff
					countID=countID+1
					stuff.item = New item
					stuff\name$="Gold"
					stuff\MaxSlots=0
					stuff\id=countID
					stuff\obj=7
					stuff\nr=nr
					stuff\stack=10
					stuff\xpos=x
					stuff\ypos=y
					

					Exit
				End If
				
				If stuff\MaxSlots>0 
					For i=1 To stuff\MaxSlots
						If stuff\CHILD[i]<>Null
							x=stuff\xpos+Rnd(50)-25
							y=stuff\ypos+Rnd(50)-25
							stuff\CHILD[i]\xpos=x
							stuff\CHILD[i]\ypos=y
							stuff\CHILD[i]\mother=Null
							stuff\CHILD[i]=Null
						End If

					Next
					Exit
				End If
			End If
		Next
	End If


End Function













Function drawall()

	For stuff.item=Each item
		If stuff\mother=Null ; If item got a mother it will not be drawn on screen. :)
			
			
			
			If stuff\name$="Gold"
				If stuff\nr<10
					DrawImage icon(stuff\obj),stuff\xpos,stuff\ypos,stuff\nr-1
				Else
					DrawImage icon(stuff\obj),stuff\xpos,stuff\ypos,9
				End If
			Else
				DrawImage icon(stuff\obj),stuff\xpos,stuff\ypos
			
				If stuff\MaxSlots>0
					x1=stuff\xpos
					y1=stuff\ypos-20
					x2=0:y2=0
					For i=1 To stuff\MaxSlots
					
						If stuff\CHILD[i]<>Null
							If stuff\CHILD[i]\MaxSlots>0
								DrawImage mark2,x1+x2,y1+y2
							Else
								DrawImage mark,x1+x2,y1+y2
							End If
							x2=x2+5	
							If x2>25 Then x2=0:y2=y2+5	
						
						End If
					Next
			
				End If
			End If
		End If
	
	Next


If selected_stuff<>Null And selectmode=1
	stuff.item = selected_stuff
	
	If selected_stuff\name$="Gold"
		If stuff\nr<10
			DrawImage icon(selected_stuff\obj),selected_stuff\xpos,selected_stuff\ypos,selected_stuff\nr-1

		Else
			DrawImage icon(selected_stuff\obj),selected_stuff\xpos,selected_stuff\ypos	,9
		End If
	Else
		DrawImage icon(selected_stuff\obj),selected_stuff\xpos,selected_stuff\ypos
	End If
End If



Text 0,0,lmsg$

End Function

Comments

big10p
(Posted 21 years ago)
Wings, I wrote some code for someone on these forums to do something similar. It has no limit on containers-in-containers or the amount of objects that can be placed inside. Here's the code, but if you don't want it posted here, I'll remove it. :)

	Graphics 640,480,0,2

	Type characterT
		Field name$							; Descriptive name of character.
		Field inventory.itemT		; Points to first item in character's inventory list.
	End Type
	
	Type itemT
		Field name$							; Descriptive name of item.
		Field container.itemT		; Points to the container this item is held within.
		Field owner.characterT	; Points to the character who currently owns this item.
		Field contents.itemT		; Points to first item contained within this item.
		Field before_item.itemT	; Points to item before this one in linked list.
		Field after_item.itemT	; Points to item after this one in linked list.
	End Type
	
	; Create some characters.
	wizard.characterT = New characterT
	wizard\name$ = "Murthlok the Wizard"
	elf.characterT = New characterT
	elf\name$ = "Giffin the Elf"
	
	; Create some items.
	Const NUM_ITEMS%			= 7
	Const ITEM_FOOD%			= 0
	Const ITEM_STAFF%			= 1
	Const ITEM_BAG%				= 2
	Const ITEM_MAP%				= 3
	Const ITEM_SWORD%			= 4
	Const ITEM_BOX%				= 5
	Const ITEM_TALISMAN%	= 6
	Dim game_item.itemT(NUM_ITEMS-1)
	Restore item_data
	For n = 0 To NUM_ITEMS-1
		game_item(n) = New itemT
		Read game_item(n)\name$
	Next
	
	; Give everything to the wizard.
	add_item_to_inventory(game_item(ITEM_BAG), wizard)
	add_item_to_inventory(game_item(ITEM_STAFF), wizard)
	add_item_to_inventory(game_item(ITEM_FOOD), wizard)
	add_item_to_inventory(game_item(ITEM_MAP), wizard)
	add_item_to_inventory(game_item(ITEM_SWORD), wizard)
	add_item_to_inventory(game_item(ITEM_BOX), wizard)
	add_item_to_inventory(game_item(ITEM_TALISMAN), wizard)
	print_inventories()
	Color 255,0,0
	Print "Press a key to put the TALISMAN into the BOX."
	Print
	Color 255,255,255
	WaitKey()

	put_item_in_container(game_item(ITEM_TALISMAN), game_item(ITEM_BOX))
	print_inventories()
	Color 255,0,0
	Print "Press a key to put the BOX, FOOD and MAP into the BAG."
	Print
	Color 255,255,255
	WaitKey()

	put_item_in_container(game_item(ITEM_BOX), game_item(ITEM_BAG))
	put_item_in_container(game_item(ITEM_FOOD), game_item(ITEM_BAG))
	put_item_in_container(game_item(ITEM_MAP), game_item(ITEM_BAG))
	print_inventories()
	Color 255,0,0
	Print "Press a key to give the BAG and SWORD to the Elf"
	Print
	Color 255,255,255
	WaitKey()

	add_item_to_inventory(game_item(ITEM_BAG), elf)
	add_item_to_inventory(game_item(ITEM_SWORD), elf)
	print_inventories()
	Color 255,0,0
	Print "The Elf bogs off on an amazing adventure..."
	Color 255,255,255
	WaitKey()
	End


;
; Adds an item to a character's inventory linked list.
;
Function add_item_to_inventory(item.itemT, character.characterT)	

	release_item(item)

	If character\inventory <> Null Then character\inventory\before_item = item
	
	item\after_item = character\inventory
	character\inventory = item
	
	set_item_owner(item, character)
	
End Function


;
; Adds an item to a container item's contents linked list.
;
Function put_item_in_container(item.itemT, container.itemT)	

	release_item(item)

	If container\contents <> Null Then container\contents\before_item = item
	
	item\after_item = container\contents
	container\contents = item
	item\container = container
	
	set_item_owner(item, container\owner)
	
End Function


;
; Cleanly removes a given item from any linked list it may be part of.
;
Function release_item(item.itemT)

	If item\container <> Null ; Item is inside a container...

		If item\container\contents = item	; Item is first in containers contents list...
			item\container\contents = item\after_item
		EndIf

	ElseIf item\owner <> Null ; Item is in owner's direct inventory...
	
		If item\owner\inventory = item ; Item is first in owner's inventory list...
			item\owner\inventory = item\after_item
		EndIf
		
	Else
	
		Return
	
	EndIf

	If item\after_item <> Null Then item\after_item\before_item = item\before_item
	If item\before_item <> Null Then item\before_item\after_item = item\after_item

	item\container = Null
	item\before_item = Null
	item\after_item = Null

	set_item_owner(item, Null)

End Function


;
; Sets the owner of a given item.
; Note: this function is recursive so any items contained in the
; given item are also set to have the same owner.
;
Function set_item_owner(item.itemT, owner.characterT)

	While item <> Null

		item\owner = owner
		
		If item\contents <> Null Then set_item_owner(item\contents, owner)
	
		item = item\after_item

	Wend
	
End Function


;
; Prints all characters' inventories.
;
Function print_inventories()

	For character.characterT = Each characterT

		Print character\name$ + " is carrying:"
		print_items(character\inventory)
		Print

	Next

End Function


;
; Prints a given item's name
; Note: this function is recursive so any items contained in the
; given item are also printed.
;
Function print_items(item.itemT, tabs% = 2)

	If item = Null
		Print String$(" ",tabs) + "Nothing!"
		Return
	EndIf
	
	While item <> Null

		If item\contents <> Null
			Print String$(" ",tabs) + item\name$ + ", inside is:"
			print_items(item\contents, tabs + 2)
		Else
			Print String$(" ",tabs) + item\name$
		EndIf
	
		item = item\after_item

	Wend
		
End Function

	
.item_data
Data "Some food"
Data "An old staff"
Data "A leather bag"
Data "A map"
Data "A short sword"
Data "A small box"
Data "A ruby talisman"


Wings
(Posted 21 years ago)
there is no limit in mine system how mant things u can put in bag. 30 means i have direct access to em. it is not a limit !

octothorpe
(Posted 21 years ago)
big10p: your code might be a little simpler if each character's \inventory was secretly a container instead of the first item in a list with no container. Then you can call put_item_in_container(item, character\inventory) instead of add_item_to_inventory(), which would no longer be required. Of course, when you create a character, you'd need to create the secret "inventory" item.

My turn? If you insist! :D

include "listC.bb" ;http://blitzbasic.com/codearcs/codearcs.php?code=1438

;---

type item
	field name$
	field contents.listC                  ; a list of items
	field container_node.listC_node       ; so we can remove ourselves from our parent's list quickly
end type

function item_new.item(name$)
	i.item = new item
	i\name = name$
	i\contents = list_new()
	return i
end function

;---

type character
	field name$
	field inventory.listC                 ; a list of items
end type

function character_new.character(name$)
	c.character = new character
	c\name = name$
	c\inventory = list_new()
	return c
end function

;---

fred.character = character_new("fred")

box.item       = item_new("box")
coin.item      = item_new("coin")
sandwich.item  = item_new("sandwich")

put_item_in_container(box, fred\inventory)
put_item_in_container(coin, fred\inventory)
put_item_in_container(sandwich, box\contents)

print_all_character_inventories()

;---

function put_item_in_container(item.item, item_list.listC)
	if item\container_node <> null then list_remove_node(item\container_node)
	item\container_node = list_push(item_list, handle(item))
end function

function print_all_character_inventories()
	for c.character = each character
		print c\name
		print_item_list_recursively(c\inventory, "  ")
	next
end function

function print_item_list_recursively(item_list.listC, indent_string$)

	; for i.item = each item_list
	it.listC_iter = list_iterator_begin(item_list)
	while list_iterator_next(it)
		i.item = object.item(list_iterator_get(it))

		print indent_string$ + i\name
		
		print_item_list_recursively(i\contents, indent_string$ + "  ")

	wend
end function


Wings
(Posted 21 years ago)
;box.item = item_new("box")
;coin.item = item_new("coin")
;sandwich.item = item_new("sandwich")

;put_item_in_container(box, fred\inventory)
;put_item_in_container(coin, fred\inventory)
;put_item_in_container(sandwich, box\contents)


looks interesting... Can this system putt a Box in a Box in a box and so on ?

octothorpe
(Posted 21 years ago)
Yes, there's no limit.

Wings
(Posted 21 years ago)
Yes i see that now.. iv been thinking real har and decided you are right :)

Techlord
(Posted 19 years ago)
Just adding my Inventory System Code To this thread since its already in progress. The purpose for the Inventory System is to evaluate a player possessions To perform a specific action (ie: unlock a door). This checking is at the core of every action a player can perform.

Everything For the player is stored in a single inventory list To include: attributes, skills, weapons, items, etc. This is the GIVE List.
Every Game Control (Trigger/ Player Control / Gui Button) has a list of required item(s) needed To activate. This is the TAKE List. Items in both Lists are stored as label. The GIVE List item contains name & value. The TAKE List item contains name & value, And a logic condition {>, <, <>, <=, =>, =}.

When a player attempts to activate a Control, items in their GIVE List are compared to the items in the Control's TAKE List. First the items are checked to see If they exist, than they are compared using the logic condition. Each check results in a True or False. If all items checks = True, the action is executed. If any item check = False, the action is aborted.

;============================
;INVENTORYTABLEITEM CLASS
;Generated with TypeWriter V
;============================

;============================
;CONSTANTS
;============================
Const INVENTORYTABLEITEM_MAX%=1024

;============================
;GLOBALS
;============================
Global inventoryTableItem_ID.inventoryTableItem[INVENTORYTABLEITEM_MAX%];Primary Key Object Pointer
Global inventoryTableItem_Index%[INVENTORYTABLEITEM_MAX%], inventoryTableItem_IndexPointer%;Primary Key ID Management Stack

;============================
;OBJECT
;============================
Type inventoryTableItem
	;Purpose: Loolup Table by Name for all possible items 
	;Properties:
	Field id%;
	Field name$;
End Type

;============================
;METHODS
;============================
Function inventoryTableItemStart()
	;Purpose: Initialize Class
	;Parameters: TBD
	;Return: None
	For inventoryTableItemloop=INVENTORYTABLEITEM_MAX To 1 Step -1; Initialize Primary Key ID Management Stack
		inventoryTableItemIndexPush(inventoryTableItemloop)
	Next
End Function

Function inventoryTableItemStop()
	;Purpose: Shutdown Class
	;Parameters: TBD
	;Return: None
	For this.inventoryTableItem=Each inventoryTableItem
		inventoryTableItemDelete(this)
	Next
End Function

Function inventoryTableItemNew.inventoryTableItem()
	;Purpose: CONSTRUCTOR - Creates inventoryTableItem Instance; Sets Default Property Values
	;Parameters: TBD
	;Return: inventoryTableItem Instance
	this.inventoryTableItem=New inventoryTableItem
	this\id%=0
	this\name$=""
	this\id%=inventoryTableItemIndexPop()
	inventoryTableItem_ID[this\id%]=this
	Return this
End Function

Function inventoryTableItemDelete(this.inventoryTableItem)
	;Purpose: DESTRUCTOR - Removes inventoryTableItem Instance
	;Parameters: inventoryTableItem Instance
	;Return: None
	inventoryTableItem_ID[this\id]=Null
	inventoryTableItemIndexPush(this\id%)
	this\name$=""
	Delete this
End Function

Function inventoryTableItemIndexPush(inventoryTableItemid%)
	;Purpose: Pushes ID into Primary Key ID Management Stack
	;Parameters: inventoryTableItemid=Object's ID
	;Return: None
	inventoryTableItem_Index[inventoryTableItem_IndexPointer]=inventoryTableItemid%
	inventoryTableItem_IndexPointer=inventoryTableItem_IndexPointer+1
End Function

Function inventoryTableItemIndexPop()
	;Purpose: Pops ID out of Primary Key ID Management Stack
	;Parameters: None
	;Return: Object ID%
	inventoryTableItem_IndexPointer=inventoryTableItem_IndexPointer-1
	Return inventoryTableItem_Index[inventoryTableItem_IndexPointer]
End Function

Function inventoryTableItemAdd%(inventoryTableItemname$)
	;Purpose: Creates an Object and returns it ID
	;Parameters: Object's Properties
	;Return: Object's ID%
	this.inventoryTableItem=inventoryTableItemNew()
	this\name$=inventoryTableItemname$
	Return this\id
End Function

Function inventoryTableItemIndexGet%(inventoryTableItemname$)
	;Purpose: Returns Index for inventorytableitem,  a unique index is used for each entry 
	;	If the name is not found it will add it to the table, returning the  new index
	;Parameters: TBD
	;Return: None
	For this.inventoryTableItem = Each inventoryTableItem
		If this\name = inventoryTableItemname$ 
			Return this\ID 
		EndIf	
	Next
	Return inventoryTableItemAdd(inventoryTableItemname$)
End Function


;============================
;INVENTORY CLASS
;Generated with TypeWriter V
;============================

;============================
;CONSTANTS
;============================
Const INVENTORY_MAX%=16
Const INVENTORY_TYPE_GIVE%=0
Const INVENTORY_TYPE_TAKE%=1

;============================
;OBJECT
;============================
Type inventory
	;Purpose: 
	;Properties:
	Field typeid%;
	Field inventoryitem.inventoryitem[INVENTORYITEM_MAX%];
	Field inventoryitems;
End Type

;============================
;METHODS
;============================
Function inventoryStart()
	;Purpose: Initialize Class
	;Parameters: TBD
	;Return: None
End Function

Function inventoryStop()
	;Purpose: Shutdown Class
	;Parameters: TBD
	;Return: None
	For this.inventory=Each inventory
		inventoryDelete(this)
	Next
End Function

Function inventoryNew.inventory(inventorytypeid=INVENTORY_TYPE_GIVE)
	;Purpose: CONSTRUCTOR - Creates inventory Instance; Sets Default Property Values
	;Parameters: TBD
	;Return: inventory Instance
	this.inventory=New inventory
	this\typeid%=inventorytypeid
	Return this
End Function

Function inventoryDelete(this.inventory)
	;Purpose: DESTRUCTOR - Removes inventory Instance
	;Parameters: inventory Instance
	;Return: None
	Delete this
End Function

Function InventoryCheck(take.inventory,give.inventory)
	;Purpose: Checks all items in the take list against item players give inventory.
	;Parameters:
	;	take inventory list object
	;	give inventory list object
	;Return: FALSE if a failure exists, TRUE Otherwise
	For takeinventoryitems = 1 To take\inventoryitems
		;DebugLog take\inventoryitem[takeinventoryitems]\inventoryID
		If  give\inventoryitem[take\inventoryitem[takeinventoryitems]\inventorytableid] <> Null 
			If Not InventoryItemCompare(take\inventoryitem[takeinventoryitems], give\inventoryitem[take\inventoryitem[takeinventoryitems]\inventorytableid]) Return False
			InventoryItemMatch = True
		EndIf
		If InventoryItemMatch = False  And take\inventoryitem[takeinventoryitems]\requirement = INVENTORYITEM_REQUIREMENT_MANDATORY Return False
		InventoryItemMatch = False
	Next
	Return True
End Function


Function InventoryAddItem(this.inventory, inventoryitemname$="", inventoryitemvalue%=0, inventoryitemcondition%=0, inventoryitemrequirement%=INVENTORYITEM_REQUIREMENT_MANDATORY)
	;Purpose: Adss item to inventory list. the last item in the list is move into the open position.
	;Parameters:
	;	this give inventory list object
	;	inventoryitem name - name of item
	;	inventoryitemvalue - value of inventory
	;	inventoryitemcondition -	INVENTORYITEM_CONDITION_NQ:1,INVENTORYITEM_CONDITION_EQ:2,INVENTORYITEM_CONDITION_GT:3
	;								INVENTORYITEM_CONDITION_GE:4,INVENTORYITEM_CONDITION_LT:5,INVENTORYITEM_CONDITION_LE:6
	;Return: none
	Select this\typeid
		Case INVENTORY_TYPE_TAKE
			this\inventoryitems = this\inventoryitems + 1
			this\inventoryitem[this\inventoryitems]	= inventoryitemNew(InventoryTableItemIndexGet%(inventoryitemname$), inventoryitemvalue, inventoryitemcondition%, inventoryitemrequirement%)
		Case INVENTORY_TYPE_GIVE
			this\inventoryitems = this\inventoryitems + 1
			this\inventoryitem[inventoryTableItemIndexGet%(inventoryitemname$)] = inventoryitemNew(inventoryTableItemIndexGet%(inventoryitemname$), inventoryitemvalue%, inventoryitemcondition%, inventoryitemrequirement%)
	End Select 	
End Function

Function InventoryRemoveItem(this.inventory,inventoryitemtableid)
	;Purpose: Removes item from inventory list
	;Parameters:
	;	take inventory list object
	;Return: none
	inventoryItemDelete(this\inventoryitem[inventoryitemtableid])
	this\inventoryitems = this\inventoryitems - 1
End Function


;============================
;INVENTORYITEM CLASS
;Generated with TypeWriter V
;============================
;============================
;CONSTANTS
;============================
Const INVENTORYITEM_MAX%=1024
Const INVENTORYITEM_CONDITION_NQ	= 	0
Const INVENTORYITEM_CONDITION_EQ	= 	1
Const INVENTORYITEM_CONDITION_GT	= 	2
Const INVENTORYITEM_CONDITION_GE	= 	3
Const INVENTORYITEM_CONDITION_LT	= 	4
Const INVENTORYITEM_CONDITION_LE	= 	5
Const INVENTORYITEM_REQUIREMENT_OPTIONAL = 0
Const INVENTORYITEM_REQUIREMENT_MANDATORY = 1

;============================
;OBJECT
;============================
Type inventoryItem
	;Purpose: 
	;Properties:
	Field inventorytableid%;
	Field value%;
	Field condition%;
	Field requirement%;
End Type

;============================
;METHODS
;============================
Function inventoryItemStart()
	;Purpose: Initialize Class
	;Parameters: TBD
	;Return: None
End Function

Function inventoryItemStop()
	;Purpose: Shutdown Class
	;Parameters: TBD
	;Return: None
	For this.inventoryItem=Each inventoryItem
		inventoryItemDelete(this)
	Next
End Function

Function inventoryItemNew.inventoryItem(inventoryitemtableid%=0, inventoryitemvalue%=0, inventoryitemcondition%=0, inventoryitemrequirement%=INVENTORYITEM_REQUIREMENT_MANDATORY)
	;Purpose: CONSTRUCTOR - Creates inventoryItem Instance; Sets Default Property Values
	;Parameters: 
	;	inventoryitemtableid% Lookup TableID 
	;	inventoryitemvalue% Value
	;	inventoryitemcondition% Logic Condition
	;	inventoryitemrequirement%=INVENTORYITEM_REQUIREMENT_MANDATORY,_OPTIONAL,_RANDOM
	;Return: inventoryItem Instance
	this.inventoryItem=New inventoryItem
	this\inventorytableid%=inventoryitemtableid%
	this\value%=inventoryitemvalue
	this\condition%=inventoryitemcondition
	this\requirement% = inventoryitemrequirement
	Return this
End Function

Function inventoryItemDelete(this.inventoryItem)
	;Purpose: DESTRUCTOR - Removes inventoryItem Instance
	;Parameters: inventoryItem Instance
	;Return: None
	Delete this
End Function

Function InventoryItemCompare(takeinventoryitem.inventoryitem,giveinventoryitem.inventoryitem)
	;Purpose: compares take inventory item value against give inventory item value using the condition in the take item 
	;Parameters: takeinventoryitem , giveinventoryitem
	;Return: TRUE if check is sucessful, FALSE otherwise
	Select takeinventoryitem\condition
		Case 0:	If giveinventoryitem\value <> takeinventoryitem\value Return True
		Case 1: If giveinventoryitem\value =  takeinventoryitem\value Return True
		Case 2:	If giveinventoryitem\value >  takeinventoryitem\value Return True
		Case 3:	If giveinventoryitem\value => takeinventoryitem\value Return True
		Case 4:	If giveinventoryitem\value <  takeinventoryitem\value Return True
		Case 5:	If giveinventoryitem\value <= takeinventoryitem\value Return True
	End Select	
	Return False
End Function

;================================================

	;item lookup table
	inventoryTableItemStart()
	inventoryTableItemAdd("HP")
	inventoryTableItemAdd("ST")
	inventoryTableItemAdd("DX")
	inventoryTableItemAdd("IQ")
	inventoryTableItemAdd("MP")
	inventoryTableItemAdd("Level")
	inventoryTableItemAdd("LockPick")
	inventoryTableItemAdd("Dazed")

	;door
	take.inventory = inventorynew(INVENTORY_TYPE_TAKE)
	InventoryAddItem(take,"Level",5,INVENTORYITEM_CONDITION_GE)
	InventoryAddItem(take,"LockPick",2,INVENTORYITEM_CONDITION_GE)
	InventoryAddItem(take,"Dazed",False,INVENTORYITEM_CONDITION_EQ,INVENTORYITEM_REQUIREMENT_OPTIONAL)
		
	;player
	give.inventory = inventorynew(INVENTORY_TYPE_GIVE)
	InventoryAddItem(give ,"Level",5)
	InventoryAddItem(give ,"LockPick",2)
	InventoryAddItem(give, "Dazed",False)
	
	;inventoryitemmatch 
	Select InventoryCheck(take,give)
		Case True	Print "Player has sufficent skills to open the door."
		Case False	Print "Player is too weak to open the door."
	End Select	
	
	inventoryitemstop()
	inventorystop()
	inventoryitemstop


Wings
(Posted 18 years ago)
Updated url so now you can download icons for inventory.
http://server.voidrpg.com/2007/inventory_demo.zip

Code Archives Forum