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

View File

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

View File

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

View File

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

View File

@ -390,20 +390,48 @@ l_add_chest(lua_State *L)
return 0; return 0;
} }
static int static bool
l_add_key_to_random_monster(lua_State *L) monster_is_in_room_with_locktype(Map *map, Monster *m, DoorLockType lockType)
{ {
Map *map = luaL_checkmap(L, 0); Position roomPos = position_to_room_coords(&m->sprite->pos);
int keytype = (int) luaL_checkinteger(L, 1); Room *room = map->rooms[roomPos.x][roomPos.y];
return room->lockTypes & lockType;
}
unsigned int max = linkedlist_size(&map->monsters); static void
unsigned int index = bh_map_rand(max - 1); 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); Monster *m = linkedlist_get(&map->monsters, index);
if (!m) if (!m)
return 0; 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; 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_pushcfunction(L, l_add_monster);
lua_setglobal(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_pushcfunction(L, l_get_random_seed);
lua_setglobal(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); linkedlist_append(&map->artifacts, a);
Item *treasure = item_builder_build_item(TREASURE, map->level*2); Item *treasure = item_builder_build_item(TREASURE, map->level*2);
treasure->sprite->pos = monsterTilePos; treasure->sprite->pos = monsterTilePos;
linkedlist_append(&map->items, treasure); linkedlist_push(&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);
} }
if (monster->items.keyType != LOCK_NONE) { if (monster->items.keyType != LOCK_NONE) {
Item *key = item_builder_build_key(monster->items.keyType); Item *key = item_builder_build_key(monster->items.keyType);
key->sprite->pos = monsterTilePos; 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) { if (monster->stats.lvl > 2 && get_random(29) == 0) {