# How to handle “weapon slots”?

I'm working on implementing an RPG engine, and I'm trying to figure out the best way to implement equipment. The really tricky part is the hand slots.

It would be easy to give a character one hand slot for a weapon, and one off-hand slot for a shield. But defining it that way would make it impossible to dual-wield, or to wield two-handed weapons. Giving the character two generic "hand slots" would allow for dual wielding, two-handed weapons is where things get tricky.

The obvious way to equip a two-handed weapon is by filling both slots with the same object. But then I need special-case code for calculating stat bonuses (you can't simply sum all bonuses from all slots, because the weapon's values will get added twice) and for removal (you can't just clear the item from one slot). If I only put the object in one slot, then calculating bonuses becomes easy again, but there needs to be special-case code to check whether the "empty" offhand slot is really empty or not.

Obviously I'm not the first person to face something like this. Two-handed weapons have been around in RPGs for decades. Is there any elegant way to handle the logic involved?

This is actually rather easy to do. Just get every item some bitmask to define how/where it can be equipped.

Some random example:

enum Slot {
SLOT_NONE  = 0x0000,
SLOT_LEFT  = 0x0001,
SLOT_RIGHT = 0x0002,
SLOT_TWOH  = 0x0004,
};


When trying to equip a weapon into a specific slot, you can check/compare flags to do different things:

• If itemSupportedSlots & slotType is not equal 0, the item can be equipped in this slot.
• If itemSupportedSlots & SLOT_TWOH is not equal 0, the item is dual wield and equipping it automatically removes the item in the left hand.
• If the item goes into the left hand and there's a two-handed item in the right hand, the item in the right hand is removed.

Doing the slots with such flags makes it rather easy to define new slots, create special items that can fit either slot (e.g. multiple ring slots or the ability to wear a ring around the neck, etc.).

In Everquest (a 14 year old MMO), each item has a bitmask called "slots" and this allows for items to be designed to be fit in the primary weapon slot, off-hand, both, back slot, and so on. So for example, it allows for a weapon that can be used only in your right hand, a weapon that can only be used in your left hand, a shield that can only be used by your left hand, or a shield that can be used as a shield and as back armor (switch between the "details" and "raw" to see what the different views of the same item produce).

The advantage of this is that you don't need separate enumerations for clothing and weapons - they all share the same enum:

enum Slots {                // Lots left out
Face =         0x00010,
Back =         0x00100,
Wrist_Left =   0x00200, // Perhaps you want watches only on the left wrist?
Wrist_Right =  0x00400,
Range =        0x00800, // This is usually for ranged weapons, like bows
Primary =      0x02000, // Your right hand
Secondary =    0x04000, // Your left hand
Feet =         0x80000,
Waist =       0x100000
}

• How would you then solve the problem with an item which takes multiple slots at the same time, like a two-handed weapon? – Philipp Jan 25 '13 at 8:19