Completes locked room generation

This commit is contained in:
Linus Probert 2019-03-20 09:53:35 +01:00
parent 18987e2c2a
commit 2ae6ada297
7 changed files with 156 additions and 32 deletions

View File

@ -4,9 +4,87 @@
++#-c--c--c--#++
++#----------#++
++#-c--c--c--#++
++G----------S++
++G----------G++
++#-c--c--c--#++
++#----------#++
++"#####G####"++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++"#####S####"++
++#-c--c--c--#++
++#----------#++
++#-c--c--c--#++
++S----------S++
++#-c--c--c--#++
++#----------#++
++"#####S####"++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++/####-#####/++
++#--c#-#c---#++
++S---#-#----G++
++#####-######++
++------------++
++#####-######++
++S--c#-#c---G++
++/####-#####/++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++----#S#-----++
++----#c#-----++
++----#c#-----++
++----#c#-----++
++----###-----++
++------------++
++------------++
++------------++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++------------++
++------------++
++------------++
++---###S##---++
++---#cccc#---++
++---######---++
++------------++
++------------++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++----#G#-----++
++----#c#-----++
++----#c#-----++
++----#c#-----++
++----###-----++
++------------++
++------------++
++------------++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++------------++
++------------++
++------------++
++---###G##---++
++---#cccc#---++
++---######---++
++------------++
++------------++
++++++++++++++++
++++++++++++++++

View File

@ -14,6 +14,8 @@ local LEFT = 2
local RIGHT = 3
local DOWN = 4
local lockedDoorsAdded = false
-- BEGIN FUNCTIONS
local function matrix_coverage (matrix)
local cov = 0
@ -128,11 +130,13 @@ local function generate_path ()
for j=1,10 do
room = map_matrix[i][j]
if room then
if roomCount > 4 and shopLevel and not shopAdded then
if roomCount > 4 and shopLevel and not shopAdded and not room.goal then
room.type = "shop"
shopAdded = true
elseif random(8) == 1 and not room.goal then
room.type = "locked"
lockedDoorsAdded = true
end
room.type = "locked"
roomCount = roomCount + 1
room_builder.build_room(room, i-1, j-1)
if room.type ~= "shop" then
@ -178,4 +182,8 @@ for i=1,10 do
end
end
end
if lockedDoorsAdded then
add_keybearers(map)
end
-- END SCRIPT

View File

@ -155,15 +155,15 @@ item_builder_build_treasure(Treasure type, double goldAmt)
static void
pickup_silver_key(Item *item, Player *player)
{
UNUSED(item);
player->equipment.keys &= LOCK_SILVER;
gui_log("You pickup %s", item->label);
player->equipment.keys |= LOCK_SILVER;
}
static void
pickup_gold_key(Item *item, Player *player)
{
UNUSED(item);
player->equipment.keys &= LOCK_GOLD;
gui_log("You pickup %s", item->label);
player->equipment.keys |= LOCK_GOLD;
}
Item *

View File

@ -37,6 +37,7 @@ create_room(void)
room = ec_malloc(sizeof(Room));
room->modifier.type = RMOD_TYPE_NONE;
room->visited = false;
room->lockTypes = 0;
for (i=0; i < MAP_ROOM_WIDTH; ++i) {
for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
room->tiles[i][j] = NULL;
@ -63,6 +64,7 @@ map_create(void)
map->currentRoom = (Position) { 0, 0 };
map->monsterMoveTimer = _timer_create();
map->level = 1;
map->lockTypes = 0;
for (i=0; i < MAP_H_ROOM_COUNT; ++i) {
for (j=0; j < MAP_V_ROOM_COUNT; ++j) {
@ -150,6 +152,9 @@ map_add_door(Map *map, Position *tile_pos, MapTile *tile)
tile->door = true;
tile->sprite->texture_index = 0;
tile->sprite->animate = false;
map->lockTypes |= tile->lockType;
room->lockTypes |= tile->lockType;
}
void

View File

@ -60,6 +60,7 @@ typedef struct Room_t {
Trap* traps[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
RoomModifierData modifier;
bool visited;
unsigned int lockTypes;
} Room;
typedef struct Map_t {
@ -72,6 +73,7 @@ typedef struct Map_t {
Position currentRoom;
Timer *monsterMoveTimer;
int level;
unsigned int lockTypes;
} Map;
Map*

View File

@ -390,20 +390,48 @@ l_add_chest(lua_State *L)
return 0;
}
static int
l_add_key_to_random_monster(lua_State *L)
static bool
monster_is_in_room_with_locktype(Map *map, Monster *m, DoorLockType lockType)
{
Map *map = luaL_checkmap(L, 0);
int keytype = (int) luaL_checkinteger(L, 1);
Position roomPos = position_to_room_coords(&m->sprite->pos);
Room *room = map->rooms[roomPos.x][roomPos.y];
return room->lockTypes & lockType;
}
unsigned int max = linkedlist_size(&map->monsters);
unsigned int index = bh_map_rand(max - 1);
static void
add_keybearer_to_map(Map *map, int keyType)
{
for (size_t tries = 0; tries < 10; tries++) {
unsigned int max = linkedlist_size(map->monsters);
unsigned int index = bh_map_rand() % max;
Monster *m = linkedlist_get(&map->monsters, index);
if (!m)
return 0;
Monster *m = linkedlist_get(&map->monsters, index);
if (!m)
continue;
m->items.keyType = keytype;
if (m->items.keyType != LOCK_NONE)
continue;
if (!monster_is_in_room_with_locktype(map, m, keyType)) {
debug("Adding key %d to monster '%s' (%u)", keyType, m->label, index);
m->items.keyType = keyType;
return;
} else {
debug("Looking for another monster to be the keybearer");
}
}
error("Failed to find a suitable keybearer (%d)", keyType);
}
static int
l_add_key_to_random_monsters(lua_State *L)
{
Map *map = luaL_checkmap(L, 1);
if (map->lockTypes & LOCK_GOLD)
add_keybearer_to_map(map, LOCK_GOLD);
if (map->lockTypes & LOCK_SILVER)
add_keybearer_to_map(map, LOCK_SILVER);
return 0;
}
@ -644,6 +672,9 @@ generate_map(unsigned int level, const char *file, GameMode gameMode, Player *pl
lua_pushcfunction(L, l_add_monster);
lua_setglobal(L, "add_monster");
lua_pushcfunction(L, l_add_key_to_random_monsters);
lua_setglobal(L, "add_keybearers");
lua_pushcfunction(L, l_get_random_seed);
lua_setglobal(L, "get_random_seed");

View File

@ -690,25 +690,25 @@ monster_drop_loot(Monster *monster, Map *map, Player *player)
linkedlist_append(&map->artifacts, a);
Item *treasure = item_builder_build_item(TREASURE, map->level*2);
treasure->sprite->pos = monsterTilePos;
linkedlist_append(&map->items, treasure);
}
if (strcmp(monster->label, "The Trader") == 0) {
Item *treasure = item_builder_build_treasure(PLATINUM, 10 * monster->stats.lvl);
treasure->sprite->pos = monsterTilePos;
linkedlist_append(&map->items, treasure);
}
if (strcmp(monster->label, "A Fairy") == 0) {
Item *treasure = item_builder_build_treasure(PLATINUM, 3 * monster->stats.lvl);
treasure->sprite->pos = monsterTilePos;
linkedlist_append(&map->items, treasure);
linkedlist_push(&map->items, treasure);
}
if (monster->items.keyType != LOCK_NONE) {
Item *key = item_builder_build_key(monster->items.keyType);
key->sprite->pos = monsterTilePos;
linkedlist_append(&map->items, key);
linkedlist_push(&map->items, key);
}
if (strcmp(monster->label, "The Trader") == 0) {
Item *treasure = item_builder_build_treasure(PLATINUM, 10 * monster->stats.lvl);
treasure->sprite->pos = monsterTilePos;
linkedlist_push(&map->items, treasure);
}
if (strcmp(monster->label, "A Fairy") == 0) {
Item *treasure = item_builder_build_treasure(PLATINUM, 3 * monster->stats.lvl);
treasure->sprite->pos = monsterTilePos;
linkedlist_push(&map->items, treasure);
}
if (monster->stats.lvl > 2 && get_random(29) == 0) {