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

convert randomMatchingLocation to take a 'pos' argument #586

Merged
Merged
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
37 changes: 20 additions & 17 deletions src/brogue/Architect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,10 @@ boolean buildAMachine(enum machineTypes bp,

if (chooseLocation) {
// Pick a random origin location.
randomMatchingLocation(&originX, &originY, FLOOR, NOTHING, -1);
pos originLoc = { originX, originY };
randomMatchingLocation(&originLoc, FLOOR, NOTHING, -1);
originX = originLoc.x;
originY = originLoc.y;
}

if (!distanceMap) {
Expand Down Expand Up @@ -1743,7 +1746,7 @@ void addMachines() {
// If buildAreaMachines is true, build ONLY the autogenerators that include machines.
// If false, build all EXCEPT the autogenerators that include machines.
void runAutogenerators(boolean buildAreaMachines) {
short AG, count, x, y, i;
short AG, count, i;
const autoGenerator *gen;
char grid[DCOLS][DROWS];

Expand Down Expand Up @@ -1772,35 +1775,35 @@ void runAutogenerators(boolean buildAreaMachines) {
// Find a location for DFs and terrain generations.
//if (randomMatchingLocation(&x, &y, gen->requiredDungeonFoundationType, NOTHING, -1)) {
//if (randomMatchingLocation(&x, &y, -1, -1, gen->requiredDungeonFoundationType)) {
if (randomMatchingLocation(&x, &y, gen->requiredDungeonFoundationType, gen->requiredLiquidFoundationType, -1)) {

pos foundationLoc;
if (randomMatchingLocation(&foundationLoc, gen->requiredDungeonFoundationType, gen->requiredLiquidFoundationType, -1)) {
// Spawn the DF.
if (gen->DFType) {
spawnDungeonFeature(x, y, &(dungeonFeatureCatalog[gen->DFType]), false, true);
spawnDungeonFeature(foundationLoc.x, foundationLoc.y, &(dungeonFeatureCatalog[gen->DFType]), false, true);

if (D_INSPECT_LEVELGEN) {
dumpLevelToScreen();
hiliteCell(x, y, &yellow, 50, true);
hiliteCell(foundationLoc.x, foundationLoc.y, &yellow, 50, true);
temporaryMessage("Dungeon feature added.", REQUIRE_ACKNOWLEDGMENT);
}
}

// Spawn the terrain if it's got the priority to spawn there and won't disrupt connectivity.
if (gen->terrain
&& tileCatalog[pmap[x][y].layers[gen->layer]].drawPriority >= tileCatalog[gen->terrain].drawPriority) {
&& tileCatalog[pmapAt(foundationLoc)->layers[gen->layer]].drawPriority >= tileCatalog[gen->terrain].drawPriority) {

// Check connectivity.
zeroOutGrid(grid);
grid[x][y] = true;
grid[foundationLoc.x][foundationLoc.y] = true;
if (!(tileCatalog[gen->terrain].flags & T_PATHING_BLOCKER)
|| !levelIsDisconnectedWithBlockingMap(grid, false)) {

// Build!
pmap[x][y].layers[gen->layer] = gen->terrain;
pmapAt(foundationLoc)->layers[gen->layer] = gen->terrain;

if (D_INSPECT_LEVELGEN) {
dumpLevelToScreen();
hiliteCell(x, y, &yellow, 50, true);
hiliteCell(foundationLoc.x, foundationLoc.y, &yellow, 50, true);
temporaryMessage("Terrain added.", REQUIRE_ACKNOWLEDGMENT);
}
}
Expand Down Expand Up @@ -3754,17 +3757,17 @@ void initializeLevel() {
// no creatures, items or stairs and with either a matching liquid and dungeon type
// or at least one layer of type terrainType.
// A dungeon, liquid type of -1 will match anything.
boolean randomMatchingLocation(short *x, short *y, short dungeonType, short liquidType, short terrainType) {
boolean randomMatchingLocation(pos* loc, short dungeonType, short liquidType, short terrainType) {
short failsafeCount = 0;
do {
failsafeCount++;
*x = rand_range(0, DCOLS - 1);
*y = rand_range(0, DROWS - 1);
} while (failsafeCount < 500 && ((terrainType >= 0 && !cellHasTerrainType(*x, *y, terrainType))
|| (((dungeonType >= 0 && pmap[*x][*y].layers[DUNGEON] != dungeonType) || (liquidType >= 0 && pmap[*x][*y].layers[LIQUID] != liquidType)) && terrainType < 0)
|| (pmap[*x][*y].flags & (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS | HAS_ITEM | IS_IN_MACHINE))
loc->x = rand_range(0, DCOLS - 1);
loc->y = rand_range(0, DROWS - 1);
} while (failsafeCount < 500 && ((terrainType >= 0 && !cellHasTerrainType(loc->x, loc->y, terrainType))
|| (((dungeonType >= 0 && pmapAt(*loc)->layers[DUNGEON] != dungeonType) || (liquidType >= 0 && pmapAt(*loc)->layers[LIQUID] != liquidType)) && terrainType < 0)
|| (pmapAt(*loc)->flags & (HAS_PLAYER | HAS_MONSTER | HAS_STAIRS | HAS_ITEM | IS_IN_MACHINE))
|| (terrainType < 0 && !(tileCatalog[dungeonType].flags & T_OBSTRUCTS_ITEMS)
&& cellHasTerrainFlag(*x, *y, T_OBSTRUCTS_ITEMS))));
&& cellHasTerrainFlag(loc->x, loc->y, T_OBSTRUCTS_ITEMS))));
if (failsafeCount >= 500) {
return false;
}
Expand Down
11 changes: 6 additions & 5 deletions src/brogue/Items.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,14 @@ short chooseKind(const itemTable *theTable, short numKinds) {
item *placeItemAt(item *theItem, pos dest) {
enum dungeonLayers layer;
char theItemName[DCOLS], buf[DCOLS];

// If no valid position is supplied by the caller, choose a random one instead.
if (!isPosInMap(dest)) {
randomMatchingLocation(&dest.x, &dest.y, FLOOR, NOTHING, -1);
theItem->loc = dest;
} else {
theItem->loc = dest;
randomMatchingLocation(&dest, FLOOR, NOTHING, -1);
}

theItem->loc = dest;

removeItemFromChain(theItem, floorItems); // just in case; double-placing an item will result in game-crashing loops in the item list
addItemToChain(theItem, floorItems);
pmapAt(theItem->loc)->flags |= HAS_ITEM;
Expand Down Expand Up @@ -671,7 +672,7 @@ void populateItems(pos upstairs) {
pos itemPlacementLoc = INVALID_POS;
if ((theItem->category & FOOD) || ((theItem->category & POTION) && theItem->kind == POTION_STRENGTH)) {
do {
randomMatchingLocation(&itemPlacementLoc.x, &itemPlacementLoc.y, FLOOR, NOTHING, -1); // Food and gain strength don't follow the heat map.
randomMatchingLocation(&itemPlacementLoc, FLOOR, NOTHING, -1); // Food and gain strength don't follow the heat map.
} while (passableArcCount(itemPlacementLoc.x, itemPlacementLoc.y) > 1); // Not in a hallway.
} else {
getItemSpawnLoc(itemSpawnHeatMap, &itemPlacementLoc.x, &itemPlacementLoc.y, &totalHeat);
Expand Down
2 changes: 1 addition & 1 deletion src/brogue/Monsters.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ creature *spawnHorde(short hordeID, short x, short y, unsigned long forbiddenFla
i = 0;
do {
pos loc;
while (!randomMatchingLocation(&loc.x, &loc.y, FLOOR, NOTHING, (hordeCatalog[hordeID].spawnsIn ? hordeCatalog[hordeID].spawnsIn : -1))
while (!randomMatchingLocation(&loc, FLOOR, NOTHING, (hordeCatalog[hordeID].spawnsIn ? hordeCatalog[hordeID].spawnsIn : -1))
|| passableArcCount(loc.x, loc.y) > 1) {
if (!--failsafe) {
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/brogue/Rogue.h
Original file line number Diff line number Diff line change
Expand Up @@ -3000,7 +3000,7 @@ extern "C" {
void freeCreatureList(creatureList *list);
void removeDeadMonsters();
void freeEverything();
boolean randomMatchingLocation(short *x, short *y, short dungeonType, short liquidType, short terrainType);
boolean randomMatchingLocation(pos *loc, short dungeonType, short liquidType, short terrainType);
enum dungeonLayers highestPriorityLayer(short x, short y, boolean skipGas);
enum dungeonLayers layerWithTMFlag(short x, short y, unsigned long flag);
enum dungeonLayers layerWithFlag(short x, short y, unsigned long flag);
Expand Down