LoginLogin

item data?

Root / General / [.]

zagg2000Created:
thoughts on inventory managementwhat I would do is give each item a unique ID and then store the player inventory as a list of item IDs.
DATA "ID","Name","TYPE","Property1","Property2",...
and then copy them all into an information array and use VAL() when appropriate. Strings are really convenient for dynamic sizing, and in the case of a string I would maybe just write CHR$(QTY)+CHR$(ID) for every item. With clever ID assignment and a maximum quantity, you could even use INSTR to search for items by ID this way. But while convenient, pretty much every operation on this is O(n). Another option is to have contiguous item IDs, from 0 to LAST_ITEM, and statically allocate two arrays
DIM INVENTORY_QTY[LAST_ITEM+1]
DIM INVENTORY_LST[LAST_ITEM+1]
VAR INV_HEAD = 0

DEF INV_ADD ID
  QTY = INVENTORY_QTY[ID]
  IF !QTY THEN
    INVENTORY_LST[INV_HEAD] = ID
    INC INV_HEAD
    INC INVENTORY_QTY[ID]
  ELSE
    INC INVENTORY_QTY[ID]
  ENDIF
END
DEF INV_SUB(ID)
  QTY = INVENTORY_QTY[ID]
  IF QTY <= 0 THEN
    RETURN FALSE
  ELSEIF QTY == 1 THEN
    DEC INVENTORY_QTY[ID]
    VAR I% = INV_HEAD - 1
    WHILE INVENTORY_LST[I%] != ID && I% > 0
      DEC I%
    WEND
    INVENTORY_LST[I%] = INVENTORY_LST[INV_HEAD-1]
    DEC INV_HEAD
  ELSE
    DEC INVENTORY_QTY[ID]
  ENDIF
  RETURN TRUE
END
Then displaying inventory just involves enumerating INVENTORY_LST to INV_HEAD, remove is worst case O(n), but much lighter everything else is like O(1) or something. Completely untested.
How do you find the efficiency of algorithms?
Think about them

thoughts on inventory managementwhat I would do is give each item a unique ID and then store the player inventory as a list of item IDs.
DATA "ID","Name","TYPE","Property1","Property2",...
and then copy them all into an information array and use VAL() when appropriate. Strings are really convenient for dynamic sizing, and in the case of a string I would maybe just write CHR$(QTY)+CHR$(ID) for every item. With clever ID assignment and a maximum quantity, you could even use INSTR to search for items by ID this way. But while convenient, pretty much every operation on this is O(n). Another option is to have contiguous item IDs, from 0 to LAST_ITEM, and statically allocate two arrays
DIM INVENTORY_QTY[LAST_ITEM+1]
DIM INVENTORY_LST[LAST_ITEM+1]
VAR INV_HEAD = 0

DEF INV_ADD ID
  QTY = INVENTORY_QTY[ID]
  IF !QTY THEN
    INVENTORY_LST[INV_HEAD] = ID
    INC INV_HEAD
    INC INVENTORY_QTY[ID]
  ELSE
    INC INVENTORY_QTY[ID]
  ENDIF
END
DEF INV_SUB(ID)
  QTY = INVENTORY_QTY[ID]
  IF QTY <= 0 THEN
    RETURN FALSE
  ELSEIF QTY == 1 THEN
    DEC INVENTORY_QTY[ID]
    VAR I% = INV_HEAD - 1
    WHILE INVENTORY_LST[I%] != ID && I% > 0
      DEC I%
    WEND
    INVENTORY_LST[I%] = INVENTORY_LST[INV_HEAD-1]
    DEC INV_HEAD
  ELSE
    DEC INVENTORY_QTY[ID]
  ENDIF
  RETURN TRUE
END
Then displaying inventory just involves enumerating INVENTORY_LST to INV_HEAD, remove is worst case O(n), but much lighter everything else is like O(1) or something. Completely untested.
How do you find the efficiency of algorithms?
Grab them and brutally ask them what's their efficiency until they spit it out crying for their life