Skip to content

Commit

Permalink
NWN2: Include check function for a valid item property
Browse files Browse the repository at this point in the history
  • Loading branch information
rjshae authored and DrMcCoy committed Mar 15, 2019
1 parent f2a7f74 commit fd3f17b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/engines/nwn2/itemproperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,38 @@ uint8 ItemProperty::getItemPropertyCostTableValue() const {
return _costValue;
}

bool ItemProperty::getIsItemPropertyValid() const {
// Load the item properties row
const Aurora::TwoDAFile &twoDA = TwoDAReg.get2DA("itempropdef");
const size_t count = twoDA.getRowCount();
if (_type >= count)
return false;

// Check the item type
const Aurora::TwoDARow &row = twoDA.getRow(_type);
Common::UString name = row.getString("Name");
if (name.empty() || name.equalsIgnoreCase("padding"))
return false;

// Check the subtype
Common::UString subTypeResRef = row.getString("SubTypeResRef");
if (!subTypeResRef.empty()) {
const Aurora::TwoDAFile &twoDAsubType = TwoDAReg.get2DA(subTypeResRef);
const size_t subTypeCount = twoDAsubType.getRowCount();
if (_subtype >= subTypeCount)
return false;

// "Name" column seems common to these tables
const Aurora::TwoDARow &rowSubType = twoDA.getRow(_subtype);
Common::UString nameSubTyle = rowSubType.getString("Name");
if (name.empty())
return false;
}

// TODO: Check the param1 data and price tables
return true;
}

void ItemProperty::load(const Aurora::GFF3Struct &gff) {

_type = (ItemPropertyType) gff.getUint("PropertyName");
Expand Down
2 changes: 2 additions & 0 deletions src/engines/nwn2/itemproperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class ItemProperty : public Aurora::NWScript::EngineType {
uint8 getItemPropertyCostTable() const;
/** Return the cost value. */
uint8 getItemPropertyCostTableValue() const;
/** Return true if this item property is valid. */
bool getIsItemPropertyValid() const;

private:
ItemPropertyType _type; ///< Index into 'itempropdef.2da'.
Expand Down
2 changes: 1 addition & 1 deletion src/engines/nwn2/script/function_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 608, "SetItemCharges" , &Functions::setItemCharges },
{ 609, "AddItemProperty" , 0 },
{ 610, "RemoveItemProperty" , 0 },
{ 611, "GetIsItemPropertyValid" , 0 },
{ 611, "GetIsItemPropertyValid" , &Functions::getIsItemPropertyValid },
{ 612, "GetFirstItemProperty" , &Functions::getFirstItemProperty },
{ 613, "GetNextItemProperty" , &Functions::getNextItemProperty },
{ 614, "GetItemPropertyType" , &Functions::getItemPropertyType },
Expand Down
1 change: 1 addition & 0 deletions src/engines/nwn2/script/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ class Functions {
void getItemPropertyParam1Value (Aurora::NWScript::FunctionContext &ctx);
void getItemPropertyCostTable (Aurora::NWScript::FunctionContext &ctx);
void getItemPropertyCostTableValue(Aurora::NWScript::FunctionContext &ctx);
void getIsItemPropertyValid (Aurora::NWScript::FunctionContext &ctx);
// '---

// .--- Creatures, functions_creature.cpp
Expand Down
6 changes: 6 additions & 0 deletions src/engines/nwn2/script/functions_itemproperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ void Functions::getItemPropertyCostTableValue(Aurora::NWScript::FunctionContext
ctx.getReturn() = prop->getItemPropertyCostTableValue();
}

void Functions::getIsItemPropertyValid(Aurora::NWScript::FunctionContext &ctx) {
ItemProperty *prop = NWN2::ObjectContainer::toItemProperty(ctx.getParams()[0].getEngineType());
if (prop)
ctx.getReturn() = prop->getIsItemPropertyValid();
}

} // End of namespace NWN2

} // End of namespace Engines

0 comments on commit fd3f17b

Please sign in to comment.