Advertisement
Dr_Davenstein

RPG Inventory System Example

Jan 9th, 2021
1,696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "crt.bi"
  2.  
  3. 'base item enum
  4. 'used to determine exactly what type of item to process
  5. enum ITEM_ENUM
  6.  
  7.     FOOD = 1
  8.     POTION
  9.    
  10. end enum
  11.  
  12. 'food item enum
  13. 'used to determine exactly what type of food to process
  14. enum FOOD_ENUM
  15.  
  16.     BREAD = 1
  17.     CHEESE
  18.     APPLE
  19.  
  20. end enum
  21.  
  22. 'potion item enum
  23. enum POTION_ENUM
  24.  
  25.     WATER = 1
  26.     BEER
  27.  
  28. end enum
  29.  
  30.  
  31. 'the base type
  32. 'all items will extend from this base type
  33. type base_item
  34.  
  35.     as item_enum        iType
  36.  
  37.     as string*24        name   
  38.    
  39.     as String*128       description
  40.    
  41.     'as vec3f           position
  42.    
  43.     'as fb.image ptr    sprite
  44.    
  45. end type
  46.  
  47.  
  48. 'the food type...
  49. 'since it extends base_item, it inherits iType, name, etc...
  50. type food_item extends base_item
  51.    
  52.     as FOOD_ENUM    fType
  53.    
  54.     as integer      hp
  55.  
  56. end type
  57.  
  58.  
  59. 'the potion type...
  60. 'since it extends base_item, it inherits the same things as food_item,
  61. 'but notice that it has totally different variables...
  62. 'we'll still be able to store them both in the same container
  63. type potion_item extends base_item
  64.    
  65.     as POTION_ENUM pType
  66.    
  67.     as integer      hpEffect
  68.    
  69.     as integer      strEffect
  70.  
  71. end type
  72.  
  73.  
  74. 'the bag_struct
  75. 'using an "any ptr" allows us to allocate any size memory blocks we wish
  76. 'we just have to get a little creative with the casting
  77. type bag_struct
  78.  
  79.     as any ptr          items(any)
  80.    
  81.     'as fb.image ptr    sprite
  82.    
  83. end type
  84.  
  85.  
  86. 'the main inventory_struct, which houses the bags,
  87. 'and facilitates adding/removing items from them...
  88. type inventory_struct
  89.  
  90.     as bag_struct   bags(any)
  91.  
  92.     declare function add_item( byref item as any ptr ) as integer
  93.    
  94.     declare function remove_item( byref bagId as integer, byref itemID as integer ) as integer
  95.    
  96. end type
  97.  
  98.  
  99. 'the player struct...
  100. type player_struct
  101.    
  102.     'as vec3f                   position
  103.    
  104.     as inventory_struct inventory
  105.    
  106. end type
  107.  
  108.  
  109.  
  110. 'create a player
  111. dim as player_struct player
  112.  
  113. 'give him one bag
  114. redim player.inventory.bags(0)
  115.  
  116. 'let that bag hold 1000 items
  117. with player.inventory.bags(0)
  118.     redim .items(999)
  119. end with
  120.  
  121.  
  122. 'create a bread food item and add it to the inventory
  123. dim as food_item food
  124. food.iType = ITEM_ENUM.FOOD
  125. food.fType = FOOD_ENUM.BREAD
  126. food.name = "Bread"
  127. food.description = "Moldy ass bread... yum."
  128. food.hp = 10
  129. player.inventory.add_item( @food )
  130.  
  131.  
  132. 'create a water potion item and add it to the inventory
  133. dim as potion_item potion
  134. potion.iType = ITEM_ENUM.POTION
  135. potion.pType = POTION_ENUM.WATER
  136. potion.name = "Water"
  137. potion.hpEffect = 1
  138. potion.description = "Smells like orc piss... kinda makes me horny?"
  139. player.inventory.add_item( @potion )
  140.  
  141.  
  142. 'create a beer potion item and add it to the inventory
  143. dim as potion_item potion2
  144. potion2.iType = ITEM_ENUM.POTION
  145. potion2.pType = POTION_ENUM.BEER
  146. potion2.name = "Beer"
  147. potion2.hpEffect = -5
  148. potion2.strEffect = 7
  149. potion2.description = "Smells of university?"
  150. player.inventory.add_item( @potion2 )
  151.  
  152.  
  153. 'now that those items have been added,
  154. 'we'll just print out some stuff to make sure they're actually in there
  155. print ""
  156. print "Checking the player's inventory..."
  157. print ""
  158.  
  159. for i as integer = 0 to ubound(player.inventory.bags(0).items)
  160.    
  161.    
  162.     'if the ptr is valid, then there is an item here...
  163.     if player.inventory.bags(0).items(i)<>0 then
  164.        
  165.         'cast the ptr to the base type to get the base information
  166.         dim as base_item ptr item = cast(base_item ptr, player.inventory.bags(0).items(i) )
  167.        
  168.         'now we can figure out which type of item this actually is...
  169.         select case as const item->iType
  170.            
  171.            
  172.             case ITEM_ENUM.FOOD
  173.                
  174.                 'since we know this is a food item,
  175.                 'we can safely cast the original ptr to the food_item type
  176.                 'doing this allows us to extract the data exclusive to this particular type
  177.                 dim as food_item ptr food = cast(food_item ptr, player.inventory.bags(0).items(i))
  178.                
  179.                 print "This item is... " + food->name
  180.                 print rtrim(food->description)
  181.                 print "It yields " & food->hp & " hit points"
  182.                
  183.                 'now we can refine the processing a little more,
  184.                 'to figure out exactly what type of food this is
  185.                 'perhaps you want to play a special sound fx or something...
  186.                 select case as const food->fType
  187.                    
  188.                     case FOOD_ENUM.BREAD
  189.                        
  190.                         print "Hard as a rock!"
  191.                        
  192.                     case FOOD_ENUM.CHEESE
  193.                        
  194.                         print "nyuk, nyuk, nyuk..."
  195.                    
  196.                 end select
  197.                
  198.                
  199.             case ITEM_ENUM.POTION
  200.                
  201.                 'process potions the same way...
  202.                
  203.                 dim as potion_item ptr potion = cast(potion_item ptr, player.inventory.bags(0).items(i))
  204.                
  205.                 print "This item is... " + potion->name
  206.                 print rtrim(potion->description)
  207.                 print "It yields " & potion->hpEffect & " hit points"
  208.                 print "It yields " & potion->strEffect & " str points"
  209.                  
  210.                 'and check the refined potion type...
  211.                 select case as const potion->pType
  212.                    
  213.                     case POTION_ENUM.WATER
  214.                        
  215.                         print "Sip, sip... "
  216.                        
  217.                     case POTION_ENUM.BEER
  218.                        
  219.                         print "GLUG, GLUG, GLUG!!!"
  220.                    
  221.                 end select
  222.                
  223.                
  224.         end select
  225.    
  226.         print " "
  227.    
  228.     end if 
  229.    
  230. next
  231.  
  232.  
  233.  
  234. sleep
  235.  
  236.  
  237. 'this function adds an item to the inventory
  238. 'it fills the first slot in the first bag...
  239. 'returns true on success
  240. 'returns false if no more space available
  241. 'the casting stuff works exactly the same as above...
  242. function inventory_struct.add_item( byref item as any ptr ) as integer
  243.  
  244.     if item = 0 then return false
  245.  
  246.     for b as integer = 0 to ubound(bags)
  247.  
  248.         for i as integer = 0 to ubound(bags(b).items)
  249.  
  250.             if bags(b).items(i) = 0 then
  251.  
  252.                 dim as base_item ptr aPtr = cast( base_item ptr, item )
  253.  
  254.  
  255.                 select case as const aPtr->iType
  256.  
  257.  
  258.                     case ITEM_ENUM.FOOD
  259.  
  260.                         dim as food_item ptr pPtr = cast( food_item ptr, item )
  261.  
  262.                         if pPtr = 0 then
  263.  
  264.                             return false
  265.  
  266.                         end if
  267.  
  268.                         print pPtr->name + " food added"
  269.  
  270.                         bags(b).items(i) = callocate( 1, sizeof(food_item) )
  271.  
  272.                         memcpy( bags(b).items(i), item, sizeof(food_item) )
  273.  
  274.                         return true
  275.  
  276.  
  277.  
  278.                     case ITEM_ENUM.POTION
  279.  
  280.                         dim as potion_item ptr pPtr = cast( potion_item ptr, item )
  281.  
  282.                         if pPtr = 0 then
  283.  
  284.                             return false
  285.  
  286.                         end if
  287.  
  288.                         print pPtr->name + " potion added"
  289.  
  290.                         bags(b).items(i) = callocate( 1, sizeof(potion_item) )
  291.  
  292.                         memcpy( bags(b).items(i), item, sizeof(potion_item) )
  293.  
  294.                         return true
  295.  
  296.                 end select
  297.  
  298.             end if
  299.  
  300.         next
  301.  
  302.     next
  303.  
  304.     return false
  305.  
  306. end function
  307.  
  308.  
  309. function inventory_struct.remove_item( byref bagId as integer, byref itemID as integer ) as integer
  310.  
  311.     if bagId>ubound(bags) then return false
  312.  
  313.     if itemId<lbound(bags(bagId).items) then return false
  314.     if itemId>ubound(bags(bagId).items) then return false
  315.    
  316.     'if there is an item here, get rid of it...
  317.     if bags(bagId).items(itemId)<>0 then
  318.  
  319.         deallocate( bags(bagId).items(itemId) )
  320.  
  321.         bags(bagId).items(itemId) = 0
  322.  
  323.         return true
  324.  
  325.     end if
  326.  
  327.     return false
  328.  
  329. end function
  330.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement