Skip to content

Commit f8a9686

Browse files
use pos type for getQualifyingPathLocNear (#731)
Replaces the 4 coordinate parameters (2 out, 2 in) with a single input pos and a return pos value.
1 parent 411bc06 commit f8a9686

File tree

9 files changed

+76
-76
lines changed

9 files changed

+76
-76
lines changed

src/brogue/Architect.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3528,8 +3528,11 @@ void restoreMonster(creature *monst, short **mapToStairs, short **mapToPit) {
35283528
// so clearing it might screw up an existing monster.)
35293529
pmap[*x][*y].flags &= ~HAS_MONSTER;
35303530
}
3531-
getQualifyingPathLocNear(x, y, *x, *y, true, T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(monst->info)), 0,
3531+
3532+
pos qualifiedPosition = getQualifyingPathLocNear((pos){ *x, *y }, true, T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(monst->info)), 0,
35323533
avoidedFlagsForMonster(&(monst->info)), (HAS_MONSTER | HAS_PLAYER | HAS_STAIRS | IS_IN_MACHINE), true);
3534+
*x = qualifiedPosition.x;
3535+
*y = qualifiedPosition.y;
35333536
}
35343537
pmap[*x][*y].flags |= HAS_MONSTER;
35353538
monst->bookkeepingFlags &= ~(MB_PREPLACED | MB_APPROACHING_DOWNSTAIRS | MB_APPROACHING_UPSTAIRS | MB_APPROACHING_PIT | MB_ABSORBING);

src/brogue/Combat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ void magicWeaponHit(creature *defender, item *theItem, boolean backstabbed) {
683683

684684
for (i = 0; i < (weaponImageCount(enchant)); i++) {
685685
newMonst = generateMonster(MK_SPECTRAL_IMAGE, true, false);
686-
getQualifyingPathLocNear(&(newMonst->loc.x), &(newMonst->loc.y), defender->loc.x, defender->loc.y, true,
686+
newMonst->loc = getQualifyingPathLocNear(defender->loc, true,
687687
T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(newMonst->info)), HAS_PLAYER,
688688
avoidedFlagsForMonster(&(newMonst->info)), (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS), false);
689689
newMonst->bookkeepingFlags |= (MB_FOLLOWER | MB_BOUND_TO_LEADER | MB_DOES_NOT_TRACK_LEADER | MB_TELEPATHICALLY_REVEALED);

src/brogue/Grid.c

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -245,64 +245,62 @@ void randomLocationInGrid(short **grid, short *x, short *y, short validValue) {
245245
}
246246

247247
// Finds the lowest positive number in a grid, chooses one location with that number randomly and returns it as (x, y).
248-
// If there are no valid locations, returns (-1, -1).
249-
static void randomLeastPositiveLocationInGrid(short **grid, short *x, short *y, boolean deterministic) {
248+
// If there are no valid locations, returns INVALID_POS, aka (-1, -1).
249+
static pos randomLeastPositiveLocationInGrid(short **grid, boolean deterministic) {
250250
const short targetValue = leastPositiveValueInGrid(grid);
251-
short locationCount;
252-
short i, j, index;
253251

254252
if (targetValue == 0) {
255-
*x = *y = -1;
256-
return;
253+
return INVALID_POS;
257254
}
258255

259-
locationCount = 0;
260-
for(i = 0; i < DCOLS; i++) {
261-
for(j = 0; j < DROWS; j++) {
256+
short locationCount = 0;
257+
for(int i = 0; i < DCOLS; i++) {
258+
for(int j = 0; j < DROWS; j++) {
262259
if (grid[i][j] == targetValue) {
263260
locationCount++;
264261
}
265262
}
266263
}
267264

265+
short index;
268266
if (deterministic) {
269267
index = locationCount / 2;
270268
} else {
271269
index = rand_range(0, locationCount - 1);
272270
}
273271

274-
for(i = 0; i < DCOLS && index >= 0; i++) {
275-
for(j = 0; j < DROWS && index >= 0; j++) {
272+
for(int i = 0; i < DCOLS && index >= 0; i++) {
273+
for(int j = 0; j < DROWS && index >= 0; j++) {
276274
if (grid[i][j] == targetValue) {
277275
if (index == 0) {
278-
*x = i;
279-
*y = j;
276+
return (pos){ .x = i, .y = j };
280277
}
281278
index--;
282279
}
283280
}
284281
}
285-
return;
282+
// This should not be reachable, since we should have already hit
283+
// the unique 'index == 0' point.
284+
return INVALID_POS;
286285
}
287286

288-
boolean getQualifyingPathLocNear(short *retValX, short *retValY,
289-
short x, short y,
290-
boolean hallwaysAllowed,
291-
unsigned long blockingTerrainFlags,
292-
unsigned long blockingMapFlags,
293-
unsigned long forbiddenTerrainFlags,
294-
unsigned long forbiddenMapFlags,
295-
boolean deterministic) {
287+
pos getQualifyingPathLocNear(
288+
pos target,
289+
boolean hallwaysAllowed,
290+
unsigned long blockingTerrainFlags,
291+
unsigned long blockingMapFlags,
292+
unsigned long forbiddenTerrainFlags,
293+
unsigned long forbiddenMapFlags,
294+
boolean deterministic
295+
) {
296296
short **grid, **costMap;
297297

298298
// First check the given location to see if it works, as an optimization.
299-
if (!cellHasTerrainFlag((pos){ x, y }, blockingTerrainFlags | forbiddenTerrainFlags)
300-
&& !(pmap[x][y].flags & (blockingMapFlags | forbiddenMapFlags))
301-
&& (hallwaysAllowed || passableArcCount(x, y) <= 1)) {
299+
if (!cellHasTerrainFlag(target, blockingTerrainFlags | forbiddenTerrainFlags)
300+
&& !(pmapAt(target)->flags & (blockingMapFlags | forbiddenMapFlags))
301+
&& (hallwaysAllowed || passableArcCount(target.x, target.y) <= 1)) {
302302

303-
*retValX = x;
304-
*retValY = y;
305-
return true;
303+
return target;
306304
}
307305

308306
// Allocate the grids.
@@ -320,8 +318,8 @@ boolean getQualifyingPathLocNear(short *retValX, short *retValY,
320318
}
321319

322320
// Run the distance scan.
323-
grid[x][y] = 1;
324-
costMap[x][y] = 1;
321+
grid[target.x][target.y] = 1;
322+
costMap[target.x][target.y] = 1;
325323
dijkstraScan(grid, costMap, true);
326324
findReplaceGrid(grid, 30000, 30000, 0);
327325

@@ -332,7 +330,7 @@ boolean getQualifyingPathLocNear(short *retValX, short *retValY,
332330
}
333331

334332
// Get the solution.
335-
randomLeastPositiveLocationInGrid(grid, retValX, retValY, deterministic);
333+
pos retLoc = randomLeastPositiveLocationInGrid(grid, deterministic);
336334

337335
// dumpLevelToScreen();
338336
// displayGrid(grid);
@@ -345,21 +343,20 @@ boolean getQualifyingPathLocNear(short *retValX, short *retValY,
345343
freeGrid(costMap);
346344

347345
// Fall back to a pathing-agnostic alternative if there are no solutions.
348-
if (*retValX == -1 && *retValY == -1) {
349-
pos loc;
350-
if (getQualifyingLocNear(&loc, (pos){ x, y }, hallwaysAllowed, NULL,
351-
(blockingTerrainFlags | forbiddenTerrainFlags),
352-
(blockingMapFlags | forbiddenMapFlags),
353-
false, deterministic)) {
354-
*retValX = loc.x;
355-
*retValY = loc.y;
356-
return true; // Found a fallback solution.
357-
} else {
358-
return false; // No solutions.
359-
}
346+
if (isPosInMap(retLoc)) {
347+
return retLoc;
348+
}
349+
350+
pos loc;
351+
if (getQualifyingLocNear(&loc, target, hallwaysAllowed, NULL,
352+
(blockingTerrainFlags | forbiddenTerrainFlags),
353+
(blockingMapFlags | forbiddenMapFlags),
354+
false, deterministic)) {
355+
return loc;
360356
} else {
361-
return true; // Found a primary solution.
357+
return retLoc;
362358
}
359+
363360
}
364361

365362
static void cellularAutomataRound(short **grid, char birthParameters[9], char survivalParameters[9]) {

src/brogue/Items.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4728,7 +4728,7 @@ static void detonateBolt(bolt *theBolt, creature *caster, short x, short y, bool
47284728
case BE_CONJURATION:
47294729
for (i = 0; i < (staffBladeCount(theBolt->magnitude * FP_FACTOR)); i++) {
47304730
monst = generateMonster(MK_SPECTRAL_BLADE, true, false);
4731-
getQualifyingPathLocNear(&(monst->loc.x), &(monst->loc.y), x, y, true,
4731+
monst->loc = getQualifyingPathLocNear((pos){ x, y }, true,
47324732
T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(monst->info)) & ~T_SPONTANEOUSLY_IGNITES, HAS_PLAYER,
47334733
avoidedFlagsForMonster(&(monst->info)) & ~T_SPONTANEOUSLY_IGNITES, (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS), false);
47344734
monst->bookkeepingFlags |= (MB_FOLLOWER | MB_BOUND_TO_LEADER | MB_DOES_NOT_TRACK_LEADER);
@@ -6632,7 +6632,7 @@ static void summonGuardian(item *theItem) {
66326632
creature *monst;
66336633

66346634
monst = generateMonster(MK_CHARM_GUARDIAN, false, false);
6635-
getQualifyingPathLocNear(&(monst->loc.x), &(monst->loc.y), x, y, true,
6635+
monst->loc = getQualifyingPathLocNear((pos){ x, y }, true,
66366636
T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(monst->info)) & ~T_SPONTANEOUSLY_IGNITES, HAS_PLAYER,
66376637
avoidedFlagsForMonster(&(monst->info)) & ~T_SPONTANEOUSLY_IGNITES, (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS), false);
66386638
monst->bookkeepingFlags |= (MB_FOLLOWER | MB_BOUND_TO_LEADER | MB_DOES_NOT_TRACK_LEADER);

src/brogue/Monsters.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ creature *cloneMonster(creature *monst, boolean announce, boolean placeClone) {
597597
// getQualifyingLocNear(loc, monst->loc.x, monst->loc.y, true, 0, forbiddenFlagsForMonster(&(monst->info)), (HAS_PLAYER | HAS_MONSTER), false, false);
598598
// newMonst->loc.x = loc[0];
599599
// newMonst->loc.y = loc[1];
600-
getQualifyingPathLocNear(&(newMonst->loc.x), &(newMonst->loc.y), monst->loc.x, monst->loc.y, true,
600+
newMonst->loc = getQualifyingPathLocNear(monst->loc, true,
601601
T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(newMonst->info)), HAS_PLAYER,
602602
avoidedFlagsForMonster(&(newMonst->info)), (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS), false);
603603
pmapAt(newMonst->loc)->flags |= HAS_MONSTER;
@@ -718,7 +718,7 @@ static boolean spawnMinions(short hordeID, creature *leader, boolean summoned, b
718718
monst = generateMonster(theHorde->memberType[iSpecies], itemPossible, !summoned);
719719
failsafe = 0;
720720
do {
721-
getQualifyingPathLocNear(&(monst->loc.x), &(monst->loc.y), x, y, summoned,
721+
monst->loc = getQualifyingPathLocNear((pos){ x, y }, summoned,
722722
T_DIVIDES_LEVEL & forbiddenTerrainFlags, (HAS_PLAYER | HAS_STAIRS),
723723
forbiddenTerrainFlags, HAS_MONSTER, false);
724724
} while (theHorde->spawnsIn && !cellHasTerrainType(monst->loc, theHorde->spawnsIn) && failsafe++ < 20);
@@ -2909,7 +2909,7 @@ boolean resurrectAlly(const pos loc) {
29092909
removeCreature(&purgatory, monToRaise);
29102910
prependCreature(monsters, monToRaise);
29112911

2912-
getQualifyingPathLocNear(&monToRaise->loc.x, &monToRaise->loc.y, loc.x, loc.y, true,
2912+
monToRaise->loc = getQualifyingPathLocNear(loc, true,
29132913
(T_PATHING_BLOCKER | T_HARMFUL_TERRAIN), 0,
29142914
0, (HAS_PLAYER | HAS_MONSTER), false);
29152915
pmapAt(monToRaise->loc)->flags |= HAS_MONSTER;
@@ -3835,7 +3835,7 @@ boolean moveMonster(creature *monst, short dx, short dy) {
38353835
pmapAt(monst->loc)->flags |= HAS_MONSTER;
38363836

38373837
if (monsterAvoids(defender, (pos){x, y})) { // don't want a flying monster to swap a non-flying monster into lava!
3838-
getQualifyingPathLocNear(&(defender->loc.x), &(defender->loc.y), x, y, true,
3838+
defender->loc = getQualifyingPathLocNear((pos){ x, y }, true,
38393839
forbiddenFlagsForMonster(&(defender->info)), HAS_PLAYER,
38403840
forbiddenFlagsForMonster(&(defender->info)), (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS), false);
38413841
} else {
@@ -4064,13 +4064,15 @@ boolean getQualifyingGridLocNear(pos *loc,
40644064
}
40654065

40664066
void makeMonsterDropItem(creature *monst) {
4067-
short x, y;
4068-
getQualifyingPathLocNear(&x, &y, monst->loc.x, monst->loc.y, true,
4069-
(T_DIVIDES_LEVEL), 0,
4070-
T_OBSTRUCTS_ITEMS, (HAS_PLAYER | HAS_STAIRS | HAS_ITEM), false);
4071-
placeItemAt(monst->carriedItem, (pos){ x, y });
4067+
pos dropLocation = getQualifyingPathLocNear(
4068+
monst->loc, true,
4069+
(T_DIVIDES_LEVEL), 0,
4070+
T_OBSTRUCTS_ITEMS, (HAS_PLAYER | HAS_STAIRS | HAS_ITEM),
4071+
false
4072+
);
4073+
placeItemAt(monst->carriedItem, dropLocation);
40724074
monst->carriedItem = NULL;
4073-
refreshDungeonCell((pos){ x, y });
4075+
refreshDungeonCell(dropLocation);
40744076
}
40754077

40764078
void checkForContinuedLeadership(creature *monst) {
@@ -4156,11 +4158,8 @@ void toggleMonsterDormancy(creature *monst) {
41564158

41574159
// Does it need a new location?
41584160
if (pmapAt(monst->loc)->flags & (HAS_MONSTER | HAS_PLAYER)) { // Occupied!
4159-
getQualifyingPathLocNear(
4160-
&(monst->loc.x),
4161-
&(monst->loc.y),
4162-
monst->loc.x,
4163-
monst->loc.y,
4161+
monst->loc = getQualifyingPathLocNear(
4162+
monst->loc,
41644163
true,
41654164
T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(monst->info)),
41664165
HAS_PLAYER,

src/brogue/Movement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ boolean playerMoves(short direction) {
11881188
defender->loc.x = x;
11891189
defender->loc.y = y;
11901190
if (monsterAvoids(defender, (pos){x, y})) {
1191-
getQualifyingPathLocNear(&(defender->loc.x), &(defender->loc.y), player.loc.x, player.loc.y, true, forbiddenFlagsForMonster(&(defender->info)), 0, 0, (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS), false);
1191+
defender->loc = getQualifyingPathLocNear(player.loc, true, forbiddenFlagsForMonster(&(defender->info)), 0, 0, (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS), false);
11921192
}
11931193
//getQualifyingLocNear(loc, player.loc.x, player.loc.y, true, NULL, forbiddenFlagsForMonster(&(defender->info)) & ~(T_IS_DF_TRAP | T_IS_DEEP_WATER | T_SPONTANEOUSLY_IGNITES), HAS_MONSTER, false, false);
11941194
//defender->loc.x = loc[0];

src/brogue/Rogue.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,14 +3345,15 @@ extern "C" {
33453345
void getTMGrid(short **grid, short value, unsigned long TMflags);
33463346
short validLocationCount(short **grid, short validValue);
33473347
void randomLocationInGrid(short **grid, short *x, short *y, short validValue);
3348-
boolean getQualifyingPathLocNear(short *retValX, short *retValY,
3349-
short x, short y,
3350-
boolean hallwaysAllowed,
3351-
unsigned long blockingTerrainFlags,
3352-
unsigned long blockingMapFlags,
3353-
unsigned long forbiddenTerrainFlags,
3354-
unsigned long forbiddenMapFlags,
3355-
boolean deterministic);
3348+
pos getQualifyingPathLocNear(
3349+
pos target,
3350+
boolean hallwaysAllowed,
3351+
unsigned long blockingTerrainFlags,
3352+
unsigned long blockingMapFlags,
3353+
unsigned long forbiddenTerrainFlags,
3354+
unsigned long forbiddenMapFlags,
3355+
boolean deterministic
3356+
);
33563357
void createBlobOnGrid(short **grid,
33573358
short *retMinX, short *retMinY, short *retWidth, short *retHeight,
33583359
short roundCount,

src/brogue/RogueMain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,8 @@ void startLevel(short oldLevelNumber, short stairDirection) {
847847
}
848848
}
849849
if (!placedPlayer) {
850-
getQualifyingPathLocNear(&loc.x, &loc.y,
851-
player.loc.x, player.loc.y,
850+
loc = getQualifyingPathLocNear(
851+
player.loc,
852852
true,
853853
T_DIVIDES_LEVEL, 0,
854854
T_PATHING_BLOCKER, (HAS_MONSTER | HAS_STAIRS | IS_IN_MACHINE),

src/brogue/Time.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ static void monsterEntersLevel(creature *monst, short n) {
18891889
monst->targetCorpseLoc = INVALID_POS;
18901890

18911891
if (!pit) {
1892-
getQualifyingPathLocNear(&(monst->loc.x), &(monst->loc.y), monst->loc.x, monst->loc.y, true,
1892+
monst->loc = getQualifyingPathLocNear(monst->loc, true,
18931893
T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(monst->info)), 0,
18941894
avoidedFlagsForMonster(&(monst->info)), HAS_STAIRS, false);
18951895
}
@@ -1899,7 +1899,7 @@ static void monsterEntersLevel(creature *monst, short n) {
18991899
// Monsters using the stairs will displace any creatures already located there, to thwart stair-dancing.
19001900
creature *prevMonst = monsterAtLoc(monst->loc);
19011901
brogueAssert(prevMonst);
1902-
getQualifyingPathLocNear(&(prevMonst->loc.x), &(prevMonst->loc.y), monst->loc.x, monst->loc.y, true,
1902+
prevMonst->loc = getQualifyingPathLocNear(monst->loc, true,
19031903
T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(prevMonst->info)), 0,
19041904
avoidedFlagsForMonster(&(prevMonst->info)), (HAS_MONSTER | HAS_PLAYER | HAS_STAIRS), false);
19051905
pmapAt(monst->loc)->flags &= ~(HAS_PLAYER | HAS_MONSTER);

0 commit comments

Comments
 (0)