Browse Source

use `Point`s instead of `x, y`s in GetRndObjLoc (objects.cpp) and portal-related functions (portal.cpp) (#5897)

Nice cleanup, thanks.
pull/5899/head
Łukasz 3 years ago committed by GitHub
parent
commit
2ec0603896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      Source/msg.cpp
  2. 33
      Source/objects.cpp
  3. 2
      Source/player.cpp
  4. 11
      Source/portal.cpp
  5. 4
      Source/portal.h

5
Source/msg.cpp

@ -2709,13 +2709,12 @@ void DeltaSyncJunk()
{
for (int i = 0; i < MAXPORTAL; i++) {
if (sgJunk.portal[i].x == 0xFF) {
SetPortalStats(i, false, 0, 0, 0, DTYPE_TOWN, false);
SetPortalStats(i, false, { 0, 0 }, 0, DTYPE_TOWN, false);
} else {
SetPortalStats(
i,
true,
sgJunk.portal[i].x,
sgJunk.portal[i].y,
{ sgJunk.portal[i].x, sgJunk.portal[i].y },
sgJunk.portal[i].level,
(dungeon_type)sgJunk.portal[i].ltype,
sgJunk.portal[i].setlvl);

33
Source/objects.cpp

@ -1491,41 +1491,41 @@ void AddTorturedBody(Object &torturedBody)
torturedBody._oPreFlag = true;
}
void GetRndObjLoc(int randarea, int *xx, int *yy)
Point GetRndObjLoc(int randarea)
{
if (randarea == 0)
return;
return { 0, 0 };
int tries = 0;
int x;
int y;
while (true) {
tries++;
if (tries > 1000 && randarea > 1)
randarea--;
*xx = GenerateRnd(MAXDUNX);
*yy = GenerateRnd(MAXDUNY);
x = GenerateRnd(MAXDUNX);
y = GenerateRnd(MAXDUNY);
bool failed = false;
for (int i = 0; i < randarea && !failed; i++) {
for (int j = 0; j < randarea && !failed; j++) {
failed = !RndLocOk(i + *xx, j + *yy);
failed = !RndLocOk(i + x, j + y);
}
}
if (!failed)
break;
}
return { x, y };
}
void AddMushPatch()
{
int y;
int x;
if (ActiveObjectCount < MAXOBJECTS) {
int i = AvailableObjects[0];
GetRndObjLoc(5, &x, &y);
dObject[x + 1][y + 1] = -(i + 1);
dObject[x + 2][y + 1] = -(i + 1);
dObject[x + 1][y + 2] = -(i + 1);
AddObject(OBJ_MUSHPATCH, { x + 2, y + 2 });
const Point loc = GetRndObjLoc(5);
dObject[loc.x + 1][loc.y + 1] = -(i + 1);
dObject[loc.x + 2][loc.y + 1] = -(i + 1);
dObject[loc.x + 1][loc.y + 2] = -(i + 1);
AddObject(OBJ_MUSHPATCH, { loc.x + 2, loc.y + 2 });
}
}
@ -3808,11 +3808,8 @@ void AddCryptObjects(int x1, int y1, int x2, int y2)
void AddSlainHero()
{
int x;
int y;
GetRndObjLoc(5, &x, &y);
AddObject(OBJ_SLAINHERO, { x + 2, y + 2 });
Point rndObjLoc = GetRndObjLoc(5);
AddObject(OBJ_SLAINHERO, rndObjLoc + Displacement { 2, 2 });
}
void InitObjects()

2
Source/player.cpp

@ -3308,7 +3308,7 @@ void SyncInitPlrPos(Player &player)
std::optional<Point> nearPosition = FindClosestValidPosition(
[&player](Point testPosition) {
return PosOkPlayer(player, testPosition) && !PosOkPortal(currlevel, testPosition.x, testPosition.y);
return PosOkPlayer(player, testPosition) && !PosOkPortal(currlevel, testPosition);
},
player.position.tile,
1, // skip the starting tile since that was checked in the previous loop

11
Source/portal.cpp

@ -38,10 +38,10 @@ void InitPortals()
}
}
void SetPortalStats(int i, bool o, int x, int y, int lvl, dungeon_type lvltype, bool isSetLevel)
void SetPortalStats(int i, bool o, Point position, int lvl, dungeon_type lvltype, bool isSetLevel)
{
Portals[i].open = o;
Portals[i].position = { x, y };
Portals[i].position = position;
Portals[i].level = lvl;
Portals[i].ltype = lvltype;
Portals[i].setlvl = isSetLevel;
@ -170,10 +170,13 @@ void GetPortalLvlPos()
}
}
bool PosOkPortal(int lvl, int x, int y)
bool PosOkPortal(int lvl, Point position)
{
for (auto &portal : Portals) {
if (portal.open && portal.level == lvl && ((portal.position.x == x && portal.position.y == y) || (portal.position.x == x - 1 && portal.position.y == y - 1)))
if (portal.open
&& portal.level == lvl
&& ((portal.position == position)
|| (portal.position == position - Displacement { 1, 1 })))
return true;
}
return false;

4
Source/portal.h

@ -23,7 +23,7 @@ struct Portal {
extern Portal Portals[MAXPORTAL];
void InitPortals();
void SetPortalStats(int i, bool o, int x, int y, int lvl, dungeon_type lvltype, bool isSetLevel);
void SetPortalStats(int i, bool o, Point position, int lvl, dungeon_type lvltype, bool isSetLevel);
void AddWarpMissile(int i, Point position, bool sync);
void SyncPortals();
void AddInTownPortal(int i);
@ -34,6 +34,6 @@ void RemovePortalMissile(int id);
void SetCurrentPortal(size_t p);
void GetPortalLevel();
void GetPortalLvlPos();
bool PosOkPortal(int lvl, int x, int y);
bool PosOkPortal(int lvl, Point position);
} // namespace devilution

Loading…
Cancel
Save