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 Wings2005
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://www.tiberion.eu/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

big10p2005
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. :)




Wings2005
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 !


octothorpe2005
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




Wings2005
;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 ?


octothorpe2005
Yes, there's no limit.


Wings2005
Yes i see that now.. iv been thinking real har and decided you are right :)


Techlord2007
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.




Wings2009
Updated url so now you can download icons for inventory.
http://www.tiberion.eu/2007/inventory_demo.zip


Code Archives Forum