Skip to content

Commit

Permalink
share item sanitation code between item loading and creation
Browse files Browse the repository at this point in the history
Also use it for shops.
  • Loading branch information
fizzet committed May 5, 2013
1 parent 5986ffd commit 9ca67d1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 61 deletions.
1 change: 1 addition & 0 deletions gemrb/core/GameScript/GSUtils.cpp
Expand Up @@ -1276,6 +1276,7 @@ bool CreateItemCore(CREItem *item, const char *resref, int a, int b, int c)
} }
item->Flags=0; item->Flags=0;
item->Expired=0; item->Expired=0;
core->SanitizeItem(item);
return true; return true;
} }


Expand Down
53 changes: 48 additions & 5 deletions gemrb/core/Interface.cpp
Expand Up @@ -4785,17 +4785,60 @@ CREItem *Interface::ReadItem(DataStream *str, CREItem *itm)
str->ReadWord( &itm->Usages[1] ); str->ReadWord( &itm->Usages[1] );
str->ReadWord( &itm->Usages[2] ); str->ReadWord( &itm->Usages[2] );
str->ReadDword( &itm->Flags ); str->ReadDword( &itm->Flags );
if (ResolveRandomItem(itm)) {
SanitizeItem(itm);
return itm;
}
return NULL;
}

//Make sure the item attributes are valid
//we don't update all flags here because some need to be set later (like
//unmovable items in containers (e.g. the bg2 portal key) so that they
//can actually be picked up)
void Interface::SanitizeItem(CREItem *item) const
{
//the stacked flag will be set by the engine if the item is indeed stacked //the stacked flag will be set by the engine if the item is indeed stacked
//this is to fix buggy saves so TakeItemNum works //this is to fix buggy saves so TakeItemNum works
//the equipped bit is also reset //the equipped bit is also reset
itm->Flags&=~(IE_INV_ITEM_STACKED|IE_INV_ITEM_EQUIPPED); item->Flags &= ~(IE_INV_ITEM_STACKED|IE_INV_ITEM_EQUIPPED);
if (GF_NO_UNDROPPABLE) { if (GF_NO_UNDROPPABLE) {
itm->Flags&=~IE_INV_ITEM_UNDROPPABLE; item->Flags &= ~IE_INV_ITEM_UNDROPPABLE;
} }
if (ResolveRandomItem(itm) ) {
return itm; Item *itm = gamedata->GetItem(item->ItemResRef, true);
if (itm) {
//This hack sets the charge counters for non-rechargeable items,
//if their charge is zero
for (int i = 0; i < CHARGE_COUNTERS; i++) {
if (item->Usages[i]) {
continue;
}
ITMExtHeader *h = itm->GetExtHeader(i);
if (h && !(h->RechargeFlags&IE_ITEM_RECHARGE)) {
//HACK: the original (bg2) allows for 0 charged gems
if (h->Charges) {
item->Usages[i] = h->Charges;
} else {
item->Usages[i] = 1;
}
}
}

//auto identify basic items
if (!itm->LoreToID) {
item->Flags |= IE_INV_ITEM_IDENTIFIED;
}

//if item is stacked mark it as so
if (itm->MaxStackAmount) {
item->Flags |= IE_INV_ITEM_STACKED;
}

item->MaxStackAmount = itm->MaxStackAmount;

gamedata->FreeItem(itm, item->ItemResRef, false);
} }
return NULL;
} }


#define MAX_LOOP 10 #define MAX_LOOP 10
Expand Down
1 change: 1 addition & 0 deletions gemrb/core/Interface.h
Expand Up @@ -616,6 +616,7 @@ class GEM_EXPORT Interface
void SetDraggedPortrait(int dp, int cursor=-1); void SetDraggedPortrait(int dp, int cursor=-1);
CREItem *ReadItem(DataStream *str); CREItem *ReadItem(DataStream *str);
CREItem *ReadItem(DataStream *str, CREItem *itm); CREItem *ReadItem(DataStream *str, CREItem *itm);
void SanitizeItem(CREItem *item) const;
bool ResolveRandomItem(CREItem *itm); bool ResolveRandomItem(CREItem *itm);
ieStrRef GetRumour(const ieResRef resname); ieStrRef GetRumour(const ieResRef resname);
Container *GetCurrentContainer(); Container *GetCurrentContainer();
Expand Down
44 changes: 0 additions & 44 deletions gemrb/core/Inventory.cpp
Expand Up @@ -158,52 +158,10 @@ CREItem *Inventory::GetItem(unsigned int slot)
return item; return item;
} }


//Make sure the item attributes are valid
//we don't update all flags here because some need to be set later (like
//unmovable items in containers (e.g. the bg2 protal key) so that they
//can actually be picked up)
void Inventory::SanitizeItem(CREItem *item)
{
Item *itm = gamedata->GetItem(item->ItemResRef, true);
if (itm) {
//This hack sets the charge counters for non-rechargeable items,
//if their charge is zero
for (int i=0;i<3;i++) {
if (item->Usages[i]) {
continue;
}
ITMExtHeader *h = itm->GetExtHeader(i);
if (h && !(h->RechargeFlags&IE_ITEM_RECHARGE)) {
//HACK: the original (bg2) allows for 0 charged gems
if (h->Charges) {
item->Usages[i] = h->Charges;
} else {
item->Usages[i] = 1;
}
}
}

//auto identify basic items
if (!itm->LoreToID) {
item->Flags |= IE_INV_ITEM_IDENTIFIED;
}

//if item is stacked mark it as so
if (itm->MaxStackAmount) {
item->Flags |= IE_INV_ITEM_STACKED;
}

item->MaxStackAmount = itm->MaxStackAmount;

gamedata->FreeItem( itm, item->ItemResRef, false );
}
}

void Inventory::AddItem(CREItem *item) void Inventory::AddItem(CREItem *item)
{ {
if (!item) return; //invalid items get no slot if (!item) return; //invalid items get no slot
Slots.push_back(item); Slots.push_back(item);
SanitizeItem(item);
} }


void Inventory::CalculateWeight() const void Inventory::CalculateWeight() const
Expand Down Expand Up @@ -619,8 +577,6 @@ void Inventory::SetSlotItem(CREItem* item, unsigned int slot)
delete Slots[slot]; delete Slots[slot];
} }


SanitizeItem(item);

Slots[slot] = item; Slots[slot] = item;


//update the action bar next time //update the action bar next time
Expand Down
1 change: 0 additions & 1 deletion gemrb/core/Inventory.h
Expand Up @@ -363,7 +363,6 @@ class GEM_EXPORT Inventory {
inline Item *GetItemPointer(ieDword slot, CREItem *&Slot) const; inline Item *GetItemPointer(ieDword slot, CREItem *&Slot) const;
void UpdateWeaponAnimation(); void UpdateWeaponAnimation();
void UpdateShieldAnimation(Item *it); void UpdateShieldAnimation(Item *it);
void SanitizeItem(CREItem *item);
}; };


} }
Expand Down
23 changes: 12 additions & 11 deletions gemrb/plugins/STOImporter/STOImporter.cpp
Expand Up @@ -174,19 +174,20 @@ void STOImporter::GetItem(STOItem *it, Store *s)
} }
// make sure the inventory knows that it needs to update flags+weight // make sure the inventory knows that it needs to update flags+weight
it->Weight = -1; it->Weight = -1;
Item *item = gamedata->GetItem( it->ItemResRef ); if (!(it->Flags & IE_INV_ITEM_IDENTIFIED) && !s->IsBag()) {
if (item) { Item *item = gamedata->GetItem( it->ItemResRef );
it->MaxStackAmount = item->MaxStackAmount; if (item) {
//another hack-fix //another hack-fix
if (item->LoreToID <= s->Lore && !s->IsBag()) { if (item->LoreToID <= s->Lore) {
it->Flags |= IE_INV_ITEM_IDENTIFIED; it->Flags |= IE_INV_ITEM_IDENTIFIED;
} }
gamedata->FreeItem( item, it->ItemResRef, false ); gamedata->FreeItem( item, it->ItemResRef, false );
//make sure it has at least one charge for proper pricing purposes
if (!it->Usages[0]) {
it->Usages[0] = 1;
} }
} }
//make sure it has at least one charge for proper pricing purposes
if (!it->Usages[0]) {
it->Usages[0] = 1;
}
str->ReadDword( (ieDword *) &it->InfiniteSupply ); str->ReadDword( (ieDword *) &it->InfiniteSupply );
ieDwordSigned tmp; ieDwordSigned tmp;


Expand Down

0 comments on commit 9ca67d1

Please sign in to comment.