diff --git a/Source/gendung.cpp b/Source/gendung.cpp index 0b3a226c5..b2f5c01e5 100644 --- a/Source/gendung.cpp +++ b/Source/gendung.cpp @@ -55,7 +55,7 @@ DungeonFlag dFlags[MAXDUNX][MAXDUNY]; int8_t dPlayer[MAXDUNX][MAXDUNY]; int16_t dMonster[MAXDUNX][MAXDUNY]; int8_t dCorpse[MAXDUNX][MAXDUNY]; -char dObject[MAXDUNX][MAXDUNY]; +int8_t dObject[MAXDUNX][MAXDUNY]; int8_t dItem[MAXDUNX][MAXDUNY]; char dSpecial[MAXDUNX][MAXDUNY]; int themeCount; diff --git a/Source/gendung.h b/Source/gendung.h index 14b67f283..836f5ca2a 100644 --- a/Source/gendung.h +++ b/Source/gendung.h @@ -223,7 +223,7 @@ extern int16_t dMonster[MAXDUNX][MAXDUNY]; */ extern DVL_API_FOR_TEST int8_t dCorpse[MAXDUNX][MAXDUNY]; /** Contains the object numbers (objects array indices) of the map. */ -extern DVL_API_FOR_TEST char dObject[MAXDUNX][MAXDUNY]; +extern DVL_API_FOR_TEST int8_t dObject[MAXDUNX][MAXDUNY]; /** Contains the item numbers (items array indices) of the map. */ extern int8_t dItem[MAXDUNX][MAXDUNY]; /** diff --git a/Source/objects.cpp b/Source/objects.cpp index f3188e8de..7e297aef9 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -682,7 +682,7 @@ void LoadMapObjects(const char *path, Point start, Rectangle mapRange, int lever if (objectId != 0) { Point mapPos = start + Displacement { i, j }; AddObject(ObjTypeConv[objectId], mapPos); - ObjectAtPosition(mapPos).InitializeLoadedObject(mapRange, leveridx); + ObjectAtPosition(mapPos)->InitializeLoadedObject(mapRange, leveridx); } } } @@ -4350,6 +4350,16 @@ bool Object::IsDisabled() const return IsAnyOf(static_cast(_oVar1), shrine_type::ShrineFascinating, shrine_type::ShrineOrnate, shrine_type::ShrineSacred); } +Object *ObjectAtPosition(Point position) +{ + if (InDungeonBounds(position) && dObject[position.x][position.y] != 0) { + return &Objects[abs(dObject[position.x][position.y]) - 1]; + } + + // nothing at this position, return a nullptr + return nullptr; +} + void InitObjectGFX() { bool fileload[56] = {}; diff --git a/Source/objects.h b/Source/objects.h index 7bb65db8f..4e784f90e 100644 --- a/Source/objects.h +++ b/Source/objects.h @@ -238,6 +238,14 @@ extern int ActiveObjectCount; extern bool ApplyObjectLighting; extern bool LoadingMapObjects; +/** + * @brief Find an object given a point in map coordinates + * + * @param position The map coordinate to test + * @return A pointer to the object or nullptr if no object exists at this location + */ +Object *ObjectAtPosition(Point position); + void InitObjectGFX(); void FreeObjectGFX(); void AddL1Objs(int x1, int y1, int x2, int y2); diff --git a/Source/setmaps.cpp b/Source/setmaps.cpp index 4d3107889..ff5e1e0b5 100644 --- a/Source/setmaps.cpp +++ b/Source/setmaps.cpp @@ -87,29 +87,29 @@ BYTE SkelChamTrans3[] = { void AddSKingObjs() { constexpr Rectangle SmallSecretRoom { { 20, 7 }, { 3, 3 } }; - ObjectAtPosition({ 64, 34 }).InitializeLoadedObject(SmallSecretRoom, 1); + ObjectAtPosition({ 64, 34 })->InitializeLoadedObject(SmallSecretRoom, 1); constexpr Rectangle Gate { { 20, 14 }, { 1, 2 } }; - ObjectAtPosition({ 64, 59 }).InitializeLoadedObject(Gate, 2); + ObjectAtPosition({ 64, 59 })->InitializeLoadedObject(Gate, 2); constexpr Rectangle LargeSecretRoom { { 8, 1 }, { 7, 10 } }; - ObjectAtPosition({ 27, 37 }).InitializeLoadedObject(LargeSecretRoom, 3); - ObjectAtPosition({ 46, 35 }).InitializeLoadedObject(LargeSecretRoom, 3); - ObjectAtPosition({ 49, 53 }).InitializeLoadedObject(LargeSecretRoom, 3); - ObjectAtPosition({ 27, 53 }).InitializeLoadedObject(LargeSecretRoom, 3); + ObjectAtPosition({ 27, 37 })->InitializeLoadedObject(LargeSecretRoom, 3); + ObjectAtPosition({ 46, 35 })->InitializeLoadedObject(LargeSecretRoom, 3); + ObjectAtPosition({ 49, 53 })->InitializeLoadedObject(LargeSecretRoom, 3); + ObjectAtPosition({ 27, 53 })->InitializeLoadedObject(LargeSecretRoom, 3); } void AddSChamObjs() { - ObjectAtPosition({ 37, 30 }).InitializeLoadedObject({ { 17, 0 }, { 4, 5 } }, 1); - ObjectAtPosition({ 37, 46 }).InitializeLoadedObject({ { 13, 0 }, { 3, 5 } }, 2); + ObjectAtPosition({ 37, 30 })->InitializeLoadedObject({ { 17, 0 }, { 4, 5 } }, 1); + ObjectAtPosition({ 37, 46 })->InitializeLoadedObject({ { 13, 0 }, { 3, 5 } }, 2); } void AddVileObjs() { - ObjectAtPosition({ 26, 45 }).InitializeLoadedObject({ { 1, 1 }, { 8, 9 } }, 1); - ObjectAtPosition({ 45, 46 }).InitializeLoadedObject({ { 11, 1 }, { 9, 9 } }, 2); - ObjectAtPosition({ 35, 36 }).InitializeLoadedObject({ { 7, 11 }, { 6, 7 } }, 3); + ObjectAtPosition({ 26, 45 })->InitializeLoadedObject({ { 1, 1 }, { 8, 9 } }, 1); + ObjectAtPosition({ 45, 46 })->InitializeLoadedObject({ { 11, 1 }, { 9, 9 } }, 2); + ObjectAtPosition({ 35, 36 })->InitializeLoadedObject({ { 7, 11 }, { 6, 7 } }, 3); } void SetmapTransparancy(const char *path) @@ -137,16 +137,6 @@ void SetmapTransparancy(const char *path) } // namespace -Object &ObjectAtPosition(Point position) -{ - for (int i = 0; i < ActiveObjectCount; i++) { - int oi = ActiveObjects[i]; - if (Objects[oi].position == position) - return Objects[oi]; - } - app_fatal("ObjectAtPosition: Active object not found at (%i,%i)", position.x, position.y); -} - void LoadSetMap() { switch (setlvlnum) { diff --git a/Source/setmaps.h b/Source/setmaps.h index 9e1dedd9f..2ca0da0b5 100644 --- a/Source/setmaps.h +++ b/Source/setmaps.h @@ -11,14 +11,6 @@ namespace devilution { -/** - * @brief Find an object given a point in map coordinates - * - * @param position The map coordinate to test - * @return A reference to the object - */ -Object &ObjectAtPosition(Point position); - /** * @brief Load a quest map, the given map is specified via the global setlvlnum */