Neverwinter Nights: Enhanced Edition

Neverwinter Nights: Enhanced Edition

View Stats:
Profile Aug 19, 2018 @ 1:53am
Script help item inventory slots
I haven't found anything, but maybe someone might know a better way.

I was hoping to retrieve the INVENTORY_SLOT type (or, in the case of rings or dual-wieldable weapons for example: either left or right wouldbe acceptable for this purpose) for an equippable item WITHOUT needing to poll the BASE_ITEM type and test these.

Even if there's a simpler way of categorising whether weapons or other equippable too would be a bonus, since there are over 100 BASE_ITEM types, and since they are not in a convenient order (additions from XPs etc. meant that for example weapons appear at both ends of the list and throughout the middle sporadically)

I should mention, the item in question may not necessarily be equipped or even in an invenotry at this time.

Also, it needs to be generalised to account for possible modifications to baseitem.2da and nwscript updates from hak/override accordingly, so I can't even group the literal BASE_ITEM such as "if (nType < BASE_ITEM_ARMOR) { return INVENOTRY_SLOT_LEFT_HAND;} etc.

So far, the only way I know to do this is with a ridiculous 100+ indexed switch - but it's costly and longwinded. Surely there's a more efficient method?

EDIT: here's what I mean - godawful mess

int GetItemSlot(object oItem)
{
if ((GetIsObjectValid(oItem))&(GetObjectType(oItem)==OBJECT_TYPE_ITEM))
{
int nType=GetBaseItemType(oItem)
switch (nType)
{
case BASE_ITEM_SHORTSWORD: return INVENTORY_SLOT_RIGHT_HAND;break;
// etc. etc.
default: return -1;break;
}

}
}











Last edited by Profile; Aug 19, 2018 @ 5:25am
< >
Showing 1-3 of 3 comments
wendigo211 Aug 19, 2018 @ 2:20pm 
You can use a 2da look up, e.g.

int iType=GetBaseItemType(oItem);
string sSlot=Get2DAString("baseitems", "EquipableSlots", iType);

sSlot will be a hex-string representation of a bit field that contains the various item slots the item can be equipped in. For more information see: http://nwn.wikia.com/wiki/Baseitems.2da

This will address the problem of custom content, but 2da lookups are horribly inefficient (going from running conditionals to searching through an ASCII file), and I don't see any functions for converting a hex-string to a integer so you can use bitwise operations. Although it shouldn't be too hard to write your own.

The switch should be faster, even if it is horrible to look at, don't forget you can use fall through to write it a bit quicker eg.

...
case BASE_ITEM_LARGESHIELD:
case BASE_ITEM_SMALLSHIELD:
case BASE_ITEM_TOWERSHIELD:
iSlot=INVENTORY_SLOT_LEFTHAND;
break;
...

Even though it's an old compiler it should convert the switch to a hash table.
Profile Aug 20, 2018 @ 9:22am 
Hi Wendigo, thanks.

I always avoid using Get2daString because disk reads are comparitively slow.

Looks like I'm stuck with a long switch, then.
To my understanding (whihch may not be up to date or accurate) values are not hashed, they are direct binary comparisons.
wendigo211 Aug 20, 2018 @ 2:31pm 
Originally posted by Profile:
Hi Wendigo, thanks.

I always avoid using Get2daString because disk reads are comparitively slow.

Looks like I'm stuck with a long switch, then.
To my understanding (whihch may not be up to date or accurate) values are not hashed, they are direct binary comparisons.

There was a big a debate years ago on the Bioware forums about whether switches were any better than if/else if statements (other than readability), someone actually did a test and it turned out that switches do execute faster, so it appears that there is some hashing or extra optimization is involved.

Anyway it's not like it matters on a modern opimizing compiler. It will handle either implementation the same and use the most efficient implementation. Actually switch has fallen out of vogue and is considered bad coding practice from a "readable code" perspective. I suppose it's because most languages have an if/else statement but don't have a switch statement.
< >
Showing 1-3 of 3 comments
Per page: 1530 50

Date Posted: Aug 19, 2018 @ 1:53am
Posts: 3