Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: WIP: Use type-safe ids #1707

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/externalized/ContentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ class ContentManager : public ItemSystem, public MercSystem
virtual ST::string* loadDialogQuoteFromFile(const ST::string& filename, int quote_number) = 0;

/** Get weapons with the give index. */
virtual const WeaponModel* getWeapon(uint16_t index) = 0;
virtual const WeaponModel* getWeapon(ItemId index) = 0;
virtual const WeaponModel* getWeaponByName(const ST::string &internalName) = 0;

virtual const MagazineModel* getMagazineByName(const ST::string &internalName) = 0;
virtual const MagazineModel* getMagazineByItemIndex(uint16_t itemIndex) = 0;
virtual const MagazineModel* getMagazineByItemIndex(ItemId itemIndex) = 0;
virtual const std::vector<const MagazineModel*>& getMagazines() const = 0;

virtual const CalibreModel* getCalibre(uint8_t index) = 0;
Expand Down
24 changes: 12 additions & 12 deletions src/externalized/DefaultContentManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void DefaultContentManager::loadAllDialogQuotes(STRING_ENC_TYPE encType, const S
}

/** Get weapons with the give index. */
const WeaponModel* DefaultContentManager::getWeapon(uint16_t itemIndex)
const WeaponModel* DefaultContentManager::getWeapon(ItemId itemIndex)
{
return getItem(itemIndex)->asWeapon();
}
Expand All @@ -483,7 +483,7 @@ const MagazineModel* DefaultContentManager::getMagazineByName(const ST::string &
return m_magazineMap[internalName];
}

const MagazineModel* DefaultContentManager::getMagazineByItemIndex(uint16_t itemIndex)
const MagazineModel* DefaultContentManager::getMagazineByItemIndex(ItemId itemIndex)
{
return getItem(itemIndex)->asAmmo();
}
Expand Down Expand Up @@ -525,13 +525,13 @@ bool DefaultContentManager::loadWeapons(const VanillaItemStrings& vanillaItemStr
WeaponModel *w = WeaponModel::deserialize(obj, m_calibreMap, vanillaItemStrings);
SLOGD("Loaded weapon {} {}", w->getItemIndex(), w->getInternalName());

if (w->getItemIndex() > MAX_WEAPONS)
if (w->getItemIndex().inner() > MAX_WEAPONS)
{
SLOGE("Weapon index must be in the interval 0 - {}", MAX_WEAPONS);
return false;
}

m_items[w->getItemIndex()] = w;
m_items[w->getItemIndex().inner()] = w;
m_weaponMap.insert(std::make_pair(w->getInternalName(), w));
}
}
Expand All @@ -546,13 +546,13 @@ bool DefaultContentManager::loadItems(const VanillaItemStrings& vanillaItemStrin
{
JsonObjectReader obj(el);
auto* item = ItemModel::deserialize(obj, vanillaItemStrings);
if (item->getItemIndex() <= MAX_WEAPONS || item->getItemIndex() > MAXITEMS)
if (item->getItemIndex().inner() <= MAX_WEAPONS || item->getItemIndex().inner() > MAXITEMS.inner())
{
ST::string err = ST::format("Item index must be in the interval {} - {}", MAX_WEAPONS+1, MAXITEMS);
throw DataError(err);
}

m_items[item->getItemIndex()] = item;
m_items[item->getItemIndex().inner()] = item;
}

return true;
Expand All @@ -571,7 +571,7 @@ bool DefaultContentManager::loadMagazines(const VanillaItemStrings& vanillaItemS
SLOGD("Loaded magazine {} {}", mag->getItemIndex(), mag->getInternalName());

m_magazines.push_back(mag);
m_items[mag->getItemIndex()] = mag;
m_items[mag->getItemIndex().inner()] = mag;
m_magazineMap.insert(std::make_pair(mag->getInternalName(), mag));
}
}
Expand Down Expand Up @@ -813,7 +813,7 @@ bool DefaultContentManager::loadGameData()
SLOGE("Could not read vanilla item strings from {}: {}", VanillaItemStrings::filename(), err.what());
}

m_items.resize(MAXITEMS);
m_items.resize(MAXITEMS.inner());
bool result = loadItems(vanillaItemStrings)
&& loadCalibres()
&& loadAmmoTypes()
Expand Down Expand Up @@ -1059,13 +1059,13 @@ bool DefaultContentManager::loadAllDealersAndInventory()
return true;
}

const ItemModel* DefaultContentManager::getItem(uint16_t itemIndex) const
const ItemModel* DefaultContentManager::getItem(ItemId itemIndex) const
{
if(itemIndex >= m_items.size())
if(itemIndex.inner() >= m_items.size())
{
return nullptr;
}
return m_items[itemIndex];
return m_items[itemIndex.inner()];
}

const ItemModel* DefaultContentManager::getItemByName(const ST::string &internalName) const
Expand Down Expand Up @@ -1104,7 +1104,7 @@ std::vector<ST::string> DefaultContentManager::getAllSmallInventoryGraphicPaths(
return v;
}

const std::map<uint16_t, uint16_t> DefaultContentManager::getMapItemReplacements() const
const std::map<ItemId, ItemId> DefaultContentManager::getMapItemReplacements() const
{
return m_mapItemReplacements;
}
Expand Down
10 changes: 5 additions & 5 deletions src/externalized/DefaultContentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class DefaultContentManager : public ContentManager, public IGameDataLoader
void loadAllDialogQuotes(STRING_ENC_TYPE encType, const ST::string& filename, std::vector<ST::string*> &quotes) const;

/** Get weapons with the give index. */
virtual const WeaponModel* getWeapon(uint16_t index) override;
virtual const WeaponModel* getWeapon(ItemId index) override;
virtual const WeaponModel* getWeaponByName(const ST::string &internalName) override;

virtual const MagazineModel* getMagazineByName(const ST::string &internalName) override;
virtual const MagazineModel* getMagazineByItemIndex(uint16_t itemIndex) override;
virtual const MagazineModel* getMagazineByItemIndex(ItemId itemIndex) override;
virtual const std::vector<const MagazineModel*>& getMagazines() const override;

virtual const CalibreModel* getCalibre(uint8_t index) override;
Expand All @@ -85,11 +85,11 @@ class DefaultContentManager : public ContentManager, public IGameDataLoader

virtual const AmmoTypeModel* getAmmoType(uint8_t index) override;

virtual const ItemModel* getItem(uint16_t index) const override;
virtual const ItemModel* getItem(ItemId index) const override;
virtual const ItemModel* getItemByName(const ST::string &internalName) const override;
virtual const ItemModel* getKeyItemForKeyId(uint16_t usKeyItem) const override;
virtual std::vector<ST::string> getAllSmallInventoryGraphicPaths() const override;
virtual const std::map<uint16_t, uint16_t> getMapItemReplacements() const override;
virtual const std::map<ItemId, ItemId> getMapItemReplacements() const override;

virtual const std::vector<std::vector<const WeaponModel*> > & getNormalGunChoice() const override;
virtual const std::vector<std::vector<const WeaponModel*> > & getExtendedGunChoice() const override;
Expand Down Expand Up @@ -191,7 +191,7 @@ class DefaultContentManager : public ContentManager, public IGameDataLoader
std::map<ST::string, const MagazineModel*> m_magazineMap;
std::map<ST::string, const WeaponModel*> m_weaponMap;
std::map<ST::string, const ItemModel*> m_itemMap;
std::map<uint16_t, uint16_t> m_mapItemReplacements;
std::map<ItemId, ItemId> m_mapItemReplacements;
std::multimap<MusicMode, const ST::string> m_musicMap;

std::vector<std::vector<const WeaponModel*> > mNormalGunChoice;
Expand Down
12 changes: 6 additions & 6 deletions src/externalized/ItemModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "WeaponModels.h"


ItemModel::ItemModel(uint16_t itemIndex,
ItemModel::ItemModel(ItemId itemIndex,
ST::string internalName,
uint32_t usItemClass,
uint8_t classIndex,
Expand All @@ -29,7 +29,7 @@ ItemModel::ItemModel(uint16_t itemIndex,
this->fFlags = 0;
}

ItemModel::ItemModel(uint16_t itemIndex,
ItemModel::ItemModel(ItemId itemIndex,
ST::string internalName,
ST::string shortName,
ST::string name,
Expand Down Expand Up @@ -72,7 +72,7 @@ const ST::string& ItemModel::getShortName() const { return shortName; }
const ST::string& ItemModel::getName() const { return name; }
const ST::string& ItemModel::getDescription() const { return description; }

uint16_t ItemModel::getItemIndex() const { return itemIndex; }
ItemId ItemModel::getItemIndex() const { return itemIndex; }
uint32_t ItemModel::getItemClass() const { return usItemClass; }
uint8_t ItemModel::getClassIndex() const { return ubClassIndex; }
ItemCursor ItemModel::getCursor() const { return ubCursor; }
Expand Down Expand Up @@ -153,14 +153,14 @@ uint32_t ItemModel::deserializeFlags(JsonObjectReader &obj) const
}

/** Check if the given attachment can be attached to the item. */
bool ItemModel::canBeAttached(uint16_t attachment) const
bool ItemModel::canBeAttached(ItemId attachment) const
{
return false;
}

void ItemModel::serializeTo(JsonObject &obj) const
{
obj.AddMember("itemIndex", itemIndex);
obj.AddMember("itemIndex", itemIndex.inner());
obj.AddMember("internalName", internalName);
obj.AddMember("usItemClass", (uint32_t)getItemClass());
obj.AddMember("ubClassIndex", getClassIndex());
Expand Down Expand Up @@ -206,7 +206,7 @@ ST::string ItemModel::deserializeDescription(JsonObjectReader &obj, const Vanill

const ItemModel* ItemModel::deserialize(JsonObjectReader &obj, const VanillaItemStrings& vanillaItemStrings)
{
uint16_t itemIndex = obj.GetUInt("itemIndex");
auto itemIndex = ItemId(obj.GetUInt("itemIndex"));
ST::string internalName = obj.GetString("internalName");
const rapidjson::Value& igSource = obj.GetValue("inventoryGraphics");
JsonObjectReader igGreader(igSource);
Expand Down
10 changes: 5 additions & 5 deletions src/externalized/ItemModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ struct WeaponModel;
struct ItemModel
{
ItemModel(
uint16_t itemIndex,
ItemId itemIndex,
ST::string internalName,
uint32_t usItemClass,
uint8_t classIndex=0,
ItemCursor cursor=INVALIDCURS);

ItemModel(
uint16_t itemIndex,
ItemId itemIndex,
ST::string internalName,
ST::string shortName,
ST::string name,
Expand All @@ -46,7 +46,7 @@ struct ItemModel
const virtual ST::string& getName() const;
const virtual ST::string& getDescription() const;

virtual uint16_t getItemIndex() const;
virtual ItemId getItemIndex() const;
virtual uint32_t getItemClass() const;
virtual uint8_t getClassIndex() const;
virtual ItemCursor getCursor() const;
Expand Down Expand Up @@ -88,7 +88,7 @@ struct ItemModel
virtual const MagazineModel* asAmmo() const { return NULL; }

/** Check if the given attachment can be attached to the item. */
virtual bool canBeAttached(uint16_t attachment) const;
virtual bool canBeAttached(ItemId attachment) const;

virtual void serializeTo(JsonObject &obj) const;

Expand All @@ -97,7 +97,7 @@ struct ItemModel
static ST::string deserializeDescription(JsonObjectReader &obj, const VanillaItemStrings& vanillaItemStrings);
static const ItemModel* deserialize(JsonObjectReader &obj, const VanillaItemStrings& vanillaItemStrings);
protected:
uint16_t itemIndex;
ItemId itemIndex;
ST::string internalName;
ST::string shortName;
ST::string name;
Expand Down
4 changes: 2 additions & 2 deletions src/externalized/ItemSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class ItemSystem
virtual const ItemModel* getItemByName(const ST::string &internalName) const = 0;

// Returns an item by numeric id
virtual const ItemModel* getItem(uint16_t itemIndex) const = 0;
virtual const ItemModel* getItem(ItemId itemIndex) const = 0;

// Returns all paths to small inventory images
virtual std::vector<ST::string> getAllSmallInventoryGraphicPaths() const = 0;

// Returns item replacements for maps
virtual const std::map<uint16_t, uint16_t> getMapItemReplacements() const = 0;
virtual const std::map<ItemId, ItemId> getMapItemReplacements() const = 0;

// Returns a key for
virtual const ItemModel* getKeyItemForKeyId(uint16_t usKeyItem) const = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/externalized/MagazineModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "JsonObject.h"
#include <utility>

MagazineModel::MagazineModel(uint16_t itemIndex_,
MagazineModel::MagazineModel(ItemId itemIndex_,
ST::string internalName_,
ST::string shortName_,
ST::string name_,
Expand All @@ -31,7 +31,7 @@ MagazineModel::MagazineModel(uint16_t itemIndex_,

void MagazineModel::serializeTo(JsonObject &obj) const
{
obj.AddMember("itemIndex", itemIndex);
obj.AddMember("itemIndex", itemIndex.inner());
obj.AddMember("internalName", internalName.c_str());
obj.AddMember("calibre", calibre->internalName);
obj.AddMember("capacity", capacity);
Expand Down Expand Up @@ -63,7 +63,7 @@ MagazineModel* MagazineModel::deserialize(
const std::map<ST::string, const AmmoTypeModel*> &ammoTypeMap,
const VanillaItemStrings& vanillaItemStrings)
{
int itemIndex = obj.GetInt("itemIndex");
auto itemIndex = ItemId(obj.GetInt("itemIndex"));
ST::string internalName = obj.GetString("internalName");
const CalibreModel *calibre = getCalibre(obj.GetString("calibre"), calibreMap);
uint32_t itemClass = (calibre->index != NOAMMO) ? IC_AMMO : IC_NONE;
Expand Down
2 changes: 1 addition & 1 deletion src/externalized/MagazineModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct CalibreModel;

struct MagazineModel : ItemModel
{
MagazineModel(uint16_t itemIndex,
MagazineModel(ItemId itemIndex,
ST::string internalName,
ST::string shortName,
ST::string name,
Expand Down
20 changes: 10 additions & 10 deletions src/externalized/Soldier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,18 @@ static bool isHeadPosition(int8_t pos)
return (pos == HEAD1POS) || (pos == HEAD2POS);
}

static void showGearEquipMessage(const SOLDIERTYPE* s, uint16_t usItem)
static void showGearEquipMessage(const SOLDIERTYPE* s, ItemId usItem)
{
ScreenMsg(FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, st_format_printf(*GCM->getNewString(NS_SOLDIER_EQUIPS_ITEM), s->name, GCM->getItem(usItem)->getName()));
}

static void showGearRemoveMessage(const SOLDIERTYPE* s, uint16_t usItem)
static void showGearRemoveMessage(const SOLDIERTYPE* s, ItemId usItem)
{
ScreenMsg(FONT_MCOLOR_LTYELLOW, MSG_INTERFACE, st_format_printf(*GCM->getNewString(NS_SOLDIER_REMOVES_ITEM), s->name, GCM->getItem(usItem)->getName()));
}

const std::vector<ITEMDEFINE> HEAD_GEARS_DAY { SUNGOGGLES };
const std::vector<ITEMDEFINE> HEAD_GEARS_NIGHT { UVGOGGLES, NIGHTGOGGLES };
const std::vector<ItemId> HEAD_GEARS_DAY { SUNGOGGLES };
const std::vector<ItemId> HEAD_GEARS_NIGHT { UVGOGGLES, NIGHTGOGGLES };

#define SWITCH_TO_NIGHT_GEAR 0
#define SWITCH_TO_DAY_GEAR 1
Expand All @@ -299,14 +299,14 @@ void Soldier::switchHeadGear(int switchDirection)
return;
}

const std::vector<ITEMDEFINE>& fromGears = (switchDirection == SWITCH_TO_NIGHT_GEAR) ? HEAD_GEARS_DAY : HEAD_GEARS_NIGHT;
const std::vector<ITEMDEFINE>& toGears = (switchDirection == SWITCH_TO_NIGHT_GEAR) ? HEAD_GEARS_NIGHT : HEAD_GEARS_DAY;
const std::vector<ItemId>& fromGears = (switchDirection == SWITCH_TO_NIGHT_GEAR) ? HEAD_GEARS_DAY : HEAD_GEARS_NIGHT;
const std::vector<ItemId>& toGears = (switchDirection == SWITCH_TO_NIGHT_GEAR) ? HEAD_GEARS_NIGHT : HEAD_GEARS_DAY;
OBJECTTYPE* helmet = &mSoldier->inv[HELMETPOS];
OBJECTTYPE detachedGear{};

// find the gear we want to wear
OBJECTTYPE* optimalEyeGear = NULL;
for (ITEMDEFINE ic : fromGears)
for (ItemId ic : fromGears)
{
INT8 slot = FindObj(mSoldier, ic);
if (slot != NO_SLOT)
Expand All @@ -326,7 +326,7 @@ void Soldier::switchHeadGear(int switchDirection)

// find the eye gear we are currently wearing
OBJECTTYPE* currentEyeGear = NULL;
for (ITEMDEFINE ic : toGears)
for (ItemId ic : toGears)
{
if (mSoldier->inv[HEAD1POS].usItem == ic) currentEyeGear = &mSoldier->inv[HEAD1POS];
if (mSoldier->inv[HEAD2POS].usItem == ic) currentEyeGear = &mSoldier->inv[HEAD2POS];
Expand Down Expand Up @@ -354,7 +354,7 @@ void Soldier::switchHeadGear(int switchDirection)

if (detachedGear.usItem != NONE)
{
// the gear we want had been detached from helmet and put on;
// the gear we want had been detached from helmet and put on;
// now after the object swap, we attach the gear we just put off
AttachObject(mSoldier, helmet, &detachedGear);
}
Expand All @@ -373,7 +373,7 @@ void Soldier::switchHeadGear(int switchDirection)
else if (canAttachToHelmet)
{
// attach to helmet
UINT8 currentItem = currentEyeGear->usItem;
ItemId currentItem = currentEyeGear->usItem;
BOOLEAN fSuccess = AttachObject(mSoldier, helmet, currentEyeGear);
if (fSuccess) showGearRemoveMessage(mSoldier, currentItem);
}
Expand Down
Loading