Browse Source

Move ObjectAtPosition to objects.cpp and make it return a pointer

All set maps initialise objects as expected so dObject has the correct ID already, no need to iterate over all active objects. Also by returning a pointer we can use this function to test if an object exists at the target position.

While on msvc int8_t is typedefed to signed char we might as well match the expected data type for the dObject array
pull/3859/head
ephphatha 4 years ago committed by Anders Jenbo
parent
commit
379d6e87b3
  1. 2
      Source/gendung.cpp
  2. 2
      Source/gendung.h
  3. 12
      Source/objects.cpp
  4. 8
      Source/objects.h
  5. 32
      Source/setmaps.cpp
  6. 8
      Source/setmaps.h

2
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;

2
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];
/**

12
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<shrine_type>(_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] = {};

8
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);

32
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) {

8
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
*/

Loading…
Cancel
Save