Custom Inventory Part II: Need help!

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Nash
 
 
Posts: 17505
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Custom Inventory Part II: Need help!

Post by Nash »

I'm going to write a new custom inventory system for my game (the old one was poorly coded)... one of the reasons I wanted to do this rewrite was that I wanted items that can hold other items (!)... if any of you have played Ultima Underworld, you'd know what I mean.

Anyway I'm having some trouble setting up the data structres that would be required to tackle this ambitious endeavour. Here's what I thought up so far:

Image

So each item will have a number of properties... starting with ID, and then things like weaponDamage, armorRating, etc etc. Within that item would then be a "contains" array which will store pointers to other items... (I will probably have to limit the amount recursions at some point to retain sanity).

Another detail to note:

There will be 2 huge data structures:

- Master item data

This is where the "base" items are defined and stored. This list is created at compile and will not change in-game. Its purpose is to store a master database of every single item in the game.

- Created item data

This mirrors the master item database... basically, every single item placed into the map or created in-game will have a unique reference created for them. This is so that every single item can have unique data. When an item is created, it will pull its base stats from the master table, then create an entire copy of itself into this data structure. From then on, its properties can be modified in any way. I will of course have to create some form of clean up routine later down the line to delete removed items from this list to make way for newer items.

Any thoughts? Thinking of using a 3D array but if anyone has any input on how to optimize this entire system further... I'd like to hear about it.

Here's what I have so far but this is only in 2 dimensions and doesn't take into account the extra dimension needed to store contained items because I just can't wrap my head around this concept and translate it into code at the moment...

Code: Select all

//===========================================================================
//
// GLOBAL VARIABLES
//
//===========================================================================

global int 32: item[];            // master item data
global str 33: itemname[];        // master item name
global str 34: itemdesc[];        // master item description text

global int 35: createditem[];     // a list of all items created
global str 36: createditemname[]; // names of created items
global str 37: createditemdesc[]; // descriptions of created items

//===========================================================================
//
// MASTER ITEM DATA
//
//===========================================================================

#define item_max 16384   // maximum number of definable items
#define item_propsmax 16 // maximum number of properties

// item properties
#define item_prop_type         0 // the item type. weapon, armor, food, etc
#define item_prop_value        1 // the item's value for bartering
#define item_prop_weight       2 // the item's weight, in fixed point
#define item_prop_weapondamage 3 // weapon damage
#define item_prop_armorrating  4 // armor rating
#define item_prop_creationtime 5 // time stamp of item's creation
#define item_prop_condition    6 // item condition/durability
#define item_prop_conditionmax 7
#define item_prop_stolen       8 // is item stolen?
#define item_prop_weightmax    9 // if this item is a container, how much can it hold?
#define item_prop_weaponmod   10 // a flag of enabled enchantments on this weapon
#define item_prop_armormod    11 // a flag of enabled enchantments on this armor
#define item_prop_useeffect   12 // if this item is used, what will it do? for potions, food, etc
#define item_prop_13          13
#define item_prop_14          14
#define item_prop_15          15

// item types
#define item_type_none      0
#define item_type_food      1
#define item_type_weapon    2
#define item_type_container 3 // this item can hold other items!

function void SetItem(int x, int y, int val)
{
    if(x >= item_max)
    {
        log(s:"x exceeds item_max");
        return;
    }
    if(y >= item_propsmax)
    {
        log(s:"y exceeds item_propsmax");
        return;
    }
    item[item_max * x + y] = val;
}

function int GetItem(int x, int y)
{
    if(x >= item_max)
    {
        log(s:"x exceeds item_max");
        return 0;
    }
    if(y >= item_propsmax)
    {
        log(s:"y exceeds item_propsmax");
        return 0;
    }
    return item[item_max * x + y];
}

function void InitItems (void)
{
    // 0
    itemname[0] = "";
    itemdesc[0] = "";
    SetItem(0, item_prop_type, 0);
    SetItem(0, item_prop_value, 0);
    SetItem(0, item_prop_weight, 0);
    SetItem(0, item_prop_weapondamage, 0);
    SetItem(0, item_prop_armorrating, 0);
    SetItem(0, item_prop_creationtime, 0);
    SetItem(0, item_prop_condition, 0);
    SetItem(0, item_prop_conditionmax, 0);
    SetItem(0, item_prop_stolen, 0);
    SetItem(0, item_prop_weightmax, 0);
    SetItem(0, item_prop_weaponmod, 0);
    SetItem(0, item_prop_armormod, 0);
    SetItem(0, item_prop_useeffect, 0);
    SetItem(0, item_prop_13, 0);
    SetItem(0, item_prop_14, 0);
    SetItem(0, item_prop_15, 0);
    
    // 1
    itemname[1] = "Roasted Chicken";
    itemdesc[1] = "A juicy roasted chicken that heals for $someval points";
    SetItem(1, item_prop_type, item_type_food);
    SetItem(1, item_prop_value, 10);
    SetItem(1, item_prop_weight, 0.5);
    SetItem(1, item_prop_weapondamage, 0);
    SetItem(1, item_prop_armorrating, 0);
    SetItem(1, item_prop_creationtime, 0);
    SetItem(1, item_prop_condition, 0);
    SetItem(1, item_prop_conditionmax, 0);
    SetItem(1, item_prop_stolen, 0);
    SetItem(1, item_prop_weightmax, 0);
    SetItem(1, item_prop_weaponmod, 0);
    SetItem(1, item_prop_armormod, 0);
    SetItem(1, item_prop_useeffect, 0);
    SetItem(1, item_prop_13, 0);
    SetItem(1, item_prop_14, 0);
    SetItem(1, item_prop_15, 0);
    
    // 2
    itemname[2] = "Iron Sword";
    itemdesc[2] = "A sword made of iron that deals $someval damage";
    SetItem(2, item_prop_type, item_type_weapon);
    SetItem(2, item_prop_value, 200);
    SetItem(2, item_prop_weight, 2.5);
    SetItem(2, item_prop_weapondamage, 25);
    SetItem(2, item_prop_armorrating, 0);
    SetItem(2, item_prop_creationtime, 0);
    SetItem(2, item_prop_condition, 0);
    SetItem(2, item_prop_conditionmax, 0);
    SetItem(2, item_prop_stolen, 0);
    SetItem(2, item_prop_weightmax, 0);
    SetItem(2, item_prop_weaponmod, 0);
    SetItem(2, item_prop_armormod, 0);
    SetItem(2, item_prop_useeffect, 0);
    SetItem(2, item_prop_13, 0);
    SetItem(2, item_prop_14, 0);
    SetItem(2, item_prop_15, 0);
}

//===========================================================================
//
// CREATED ITEMS
// An item created in-game will pull its base states from the master list
//
//===========================================================================

#define createditem_max      2147483647    // total number of items creatable
#define createditem_propsmax item_propsmax // same max properties as master

function void SetCreatedItem(int x, int y, int val)
{
    if(x >= createditem_max)
    {
        log(s:"x exceeds createditem_max");
        return;
    }
    if(y >= createditem_propsmax)
    {
        log(s:"y exceeds item_propsmax");
        return;
    }
    createditem[createditem_max * x + y] = val;
}

function int GetCreatedItem(int x, int y)
{
    if(x >= createditem_max)
    {
        log(s:"x exceeds createditem_max");
        return 0;
    }
    if(y >= createditem_propsmax)
    {
        log(s:"y exceeds item_propsmax");
        return 0;
    }
    return createditem[createditem_max * x + y];
}
User avatar
GooberMan
Posts: 1336
Joined: Fri Aug 08, 2003 12:57 am
Location: Helsinki, Finland

Re: Custom Inventory Part II: Need help!

Post by GooberMan »

Is there any reason why you wouldn't make a base item type in DECORATE with a bunch of user variables and have all items derive from that? It would simplify the ACS significantly (ie to the point where you wouldn't need those gigantic arrays).
User avatar
Nash
 
 
Posts: 17505
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Custom Inventory Part II: Need help!

Post by Nash »

That's a good idea but I don't know how I'm going to generate unique items from the base class in-game.

For example, if I have a base class IronSword... in-game there must be the possibility to generate IronSwords with different damage values, or durabilities, or assigned enchantments etc.

Moreover, sliding over these pickups will just add the base class into the player's inventory, ignoring any of the unique values the derived swords have.

I like the fact that I can have user arrays within an item class so that solves part of the problem (container items) but I still don't see it making things easier for me right now...

In other news, I'm trying to see if I can fake node trees with arrays. That will allow me to implement the system as how I've sketched it. Googling node trees as I type this post...
User avatar
GooberMan
Posts: 1336
Joined: Fri Aug 08, 2003 12:57 am
Location: Helsinki, Finland

Re: Custom Inventory Part II: Need help!

Post by GooberMan »

Surely damage can be implemented as a user variable anyway? A_CustomPunch(user_damageValue) seems reasonable to me. A generation function would create the thing and then do a bunch of SetUserVariable calls on the new object. It also allows you to place it on the map through an editor and then, using UDMF map format, add custom values for it by adding user_damageValue properties to it.

I guess conceptually, what you want to be thinking of is treating a thing like an object exactly like you would in object oriented programming. Making those multidimensional arrays to store properties is half way in between the system anyway. There's no need to derive from CustomInventory either if you want to avoid sticking things in ZDoom's managed inventory.
Locked

Return to “Editing (Archive)”