Skip to content

Commit

Permalink
Support updating name/description and searching for items to update b…
Browse files Browse the repository at this point in the history
…y resref
  • Loading branch information
Logg-y committed Nov 26, 2024
1 parent 2101e6f commit 83aa532
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
21 changes: 20 additions & 1 deletion src/nss/inc_itemupdate.nss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct ItemPropertyUpdateInfo
const int ITEM_UPDATE_ITEMPROPERTIES = 1;
const int ITEM_UPDATE_ADDITIONALGOLDCOST = 2;
const int ITEM_UPDATE_TAG = 4;
const int ITEM_UPDATE_DESCRIPTION = 8;
const int ITEM_UPDATE_NAME = 16;

// Update oItem to its "newer" form.
// Returns a Json object saying what was updated.
Expand Down Expand Up @@ -315,11 +317,28 @@ int GetIdentifiedGoldCost(object oItem)
struct ItemPropertyUpdateInfo UpdateItemProperties(object oItem)
{
struct ItemPropertyUpdateInfo sRet;
object oTreasureStorage = GetTFNEquipmentByName(oItem);
object oTreasureStorage = GetTFNStagedEquipmentForItem(oItem);
if (!GetIsObjectValid(oTreasureStorage))
{
return sRet;
}
if (GetDescription(oItem) != GetDescription(oTreasureStorage))
{
SetDescription(oItem, GetDescription(oTreasureStorage));
sRet.nUpdateFlags |= ITEM_UPDATE_DESCRIPTION;
}
if (GetIdentified(oItem))
{
int bOldIdentified = GetIdentified(oTreasureStorage);
SetIdentified(oTreasureStorage, 1);
string sNewName = GetName(oTreasureStorage);
SetIdentified(oTreasureStorage, bOldIdentified);
if (GetName(oItem) != sNewName)
{
SetName(oItem, sNewName);
sRet.nUpdateFlags |= ITEM_UPDATE_NAME;
}
}

int nThisHash = GetItemPropertiesHash(oItem);
int nTreasureHash = GetItemPropertiesHash(oTreasureStorage);
Expand Down
2 changes: 1 addition & 1 deletion src/nss/inc_loot.nss
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ json _AddToDroppableLootArray(json jItems, object oLootOrigin, object oItem, int
{
json jBlacklist = GetLocalJson(oLootOrigin, "OwnDroppableLootBlacklist");

object oTFN = GetTFNEquipmentByName(oItem);
object oTFN = GetTFNStagedEquipmentForItem(oItem);
if (GetLocalInt(oItem, "creature_drop_only"))
{
oTFN = oItem;
Expand Down
91 changes: 85 additions & 6 deletions src/nss/inc_treasure.nss
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,26 @@ location GetTreasureStagingLocation() {return Location(GetObjectByTag("_TREASURE
// Also, some creatures add itemprops to them on spawn, which isn't ideal
// To get around that we can map the generic names to the TFN object
// ... which this DB can do for us happily
void BuildItemNamesToObjectsDB()
void BuildTreasureStagingToObjectsDB()
{
object oMod = GetModule();
sqlquery sql = SqlPrepareQueryObject(GetModule(),
"CREATE TABLE IF NOT EXISTS item_name_lookup (" +
"itemname TEXT PRIMARY KEY ON CONFLICT FAIL, " +
"oid TEXT);");
SqlStep(sql);
sql = SqlPrepareQueryObject(GetModule(),
"CREATE TABLE IF NOT EXISTS item_resref_lookup (" +
"resref TEXT PRIMARY KEY ON CONFLICT FAIL, " +
"oid TEXT);");
SqlStep(sql);
int nTier;
int nItemType;
int nUniqueness;
int nRarity;
string sRarity;
string sItemType;
json jResrefMappingsWithConflicts = JsonArray();
for (nTier=1; nTier<=5; nTier++)
{
// Armor, Melee, Range, Apparel
Expand Down Expand Up @@ -358,8 +364,9 @@ void BuildItemNamesToObjectsDB()
SetIdentified(oTest, 1);
// As of right now there are about four items who conflict
// including base item name resolves them all
// If that ever changes, this will throw full blown sqlite errors
string sName = GetName(oTest) + IntToString(GetBaseItemType(oTest));
SetIdentified(oTest, bIdentified);

sql = SqlPrepareQueryObject(GetModule(),
"INSERT INTO item_name_lookup " +
"(itemname, oid) VALUES (@itemname, @oid);");// +
Expand All @@ -373,13 +380,79 @@ void BuildItemNamesToObjectsDB()
{
WriteTimestampedLogEntry("Error while writing item name: " + sName);
}

// Resref mapping: we can expect to have conflicts here
// It is nice not to spam the server log with them.

sName = GetResRef(oTest) + "_baseitem_" + IntToString(GetBaseItemType(oTest));
sql = SqlPrepareQueryObject(GetModule(),
"SELECT EXISTS(SELECT 1 FROM item_resref_lookup WHERE resref = @resref);");
SqlBindString(sql, "@resref", sName);
SqlStep(sql);
if (SqlGetInt(sql, 0))
{
WriteTimestampedLogEntry("Resref lookup for " + GetName(oTest) + " has conflicts with something that came before it using key " + sName);
if (JsonFind(jResrefMappingsWithConflicts, JsonString(sName)) == JsonNull())
{
JsonArrayInsertInplace(jResrefMappingsWithConflicts, JsonString(sName));
}
}
else
{
sql = SqlPrepareQueryObject(GetModule(),
"INSERT INTO item_resref_lookup " +
"(resref, oid) VALUES (@resref, @oid);");
SqlBindString(sql, "@resref", sName);
SqlBindString(sql, "@oid", ObjectToString(oTest));
SqlStep(sql);
sError = SqlGetError(sql);
if (sError != "")
{
WriteTimestampedLogEntry("Error while writing item resref mapping: " + sName);
}
}
SetIdentified(oTest, bIdentified);
oTest = GetNextItemInInventory(oChest);
}
}
}
}
}
}
// Drop the records that had conflicts
int i;
for (i=0; i<JsonGetLength(jResrefMappingsWithConflicts); i++)
{
sql = SqlPrepareQueryObject(GetModule(),
"DELETE FROM item_resref_lookup " +
"where resref = @resref RETURNING oid;");
string sKey = JsonGetString(JsonArrayGet(jResrefMappingsWithConflicts, i));
SqlBindString(sql, "@resref", sKey);
SqlStep(sql);
object oConflicter = StringToObject(SqlGetString(sql, 0));
int bIdentified = GetIdentified(oConflicter);
SetIdentified(oConflicter, 1);
WriteTimestampedLogEntry("Conflict for key " + sKey + " was with " + GetName(oConflicter));
SetIdentified(oConflicter, bIdentified);
}
}

object GetTFNEquipmentByResref(string sResRef, int nBaseItemType)
{
string sName = sResRef + "_baseitem_" + IntToString(nBaseItemType);
sqlquery sql = SqlPrepareQueryObject(GetModule(),
"SELECT oid FROM item_resref_lookup " +
"WHERE resref = @resref;");
SqlBindString(sql, "@resref", sName);
if (SqlStep(sql))
{
object oRet = StringToObject(SqlGetString(sql, 0));
//WriteTimestampedLogEntry("GetTFNEquipmentByResref: " + sName + " -> " + GetName(oRet));
return oRet;
}
//WriteTimestampedLogEntry("GetTFNEquipmentByResref: " + sName + " -> invalid");

return OBJECT_INVALID;
}

object GetTFNEquipmentFromName(string sItemName, int nBaseItemType)
Expand All @@ -392,15 +465,15 @@ object GetTFNEquipmentFromName(string sItemName, int nBaseItemType)
if (SqlStep(sql))
{
object oRet = StringToObject(SqlGetString(sql, 0));
//WriteTimestampedLogEntry("GetTFNEquipmentByName: " + GetName(oItem) + " -> " + GetName(oRet));
//WriteTimestampedLogEntry("GetTFNEquipmentFromName: " + sName+ " -> " + GetName(oRet));
return oRet;
}
//WriteTimestampedLogEntry("GetTFNEquipmentByName: " + GetName(oItem) + " -> invalid");
//WriteTimestampedLogEntry("GetTFNEquipmentFromName: " + sName + " -> invalid");

return OBJECT_INVALID;
}

object GetTFNEquipmentByName(object oItem)
object GetTFNStagedEquipmentForItem(object oItem)
{
if (GetIsObjectValid(oItem))
{
Expand All @@ -413,7 +486,13 @@ object GetTFNEquipmentByName(object oItem)
}

SetIdentified(oItem, bIdentified);
return GetTFNEquipmentFromName(sName, GetBaseItemType(oItem));
object oRet = GetTFNEquipmentFromName(sName, GetBaseItemType(oItem));
if (GetIsObjectValid(oRet))
{
return oRet;
}

return GetTFNEquipmentByResref(GetResRef(oItem), GetBaseItemType(oItem));
}
return OBJECT_INVALID;
}
Expand Down
2 changes: 1 addition & 1 deletion src/nss/on_mod_load.nss
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ void main()
}
}

BuildItemNamesToObjectsDB();
BuildTreasureStagingToObjectsDB();
SetLocalInt(GetModule(), "treasure_ready", 1);
CalculatePlaceableLootValues();

Expand Down

0 comments on commit 83aa532

Please sign in to comment.