Merge branch 'master' of github.com:tollyx/dungeon

This commit is contained in:
Adrian Hedqvist 2018-03-26 22:25:50 +02:00
commit d437c2c571
9 changed files with 88 additions and 32 deletions

View file

@ -24,7 +24,13 @@ include_directories("libs/kaguya-1.3.2/include")
file(GLOB SOURCES "src/*.cpp" "src/*.c" "src/*.h") file(GLOB SOURCES "src/*.cpp" "src/*.c" "src/*.h")
add_executable(${TARGET} ${SOURCES}) add_executable(${TARGET} ${SOURCES})
#target_link_libraries(${TARGET} PUBLIC glbinding::glbinding)
#target_link_libraries(${TARGET} PUBLIC ${SDL2_LIBRARY})
#target_link_libraries(${TARGET} PUBLIC ${OPENGL_LIBRARY})
#target_link_libraries(${TARGET} PUBLIC ${LUA_LIBRARY})
target_link_libraries(${TARGET} PUBLIC glbinding::glbinding) target_link_libraries(${TARGET} PUBLIC glbinding::glbinding)
target_link_libraries(${TARGET} PUBLIC ${SDL2_LIBRARY}) target_link_libraries(${TARGET} PUBLIC SDL2)
target_link_libraries(${TARGET} PUBLIC ${OPENGL_LIBRARY}) target_link_libraries(${TARGET} PUBLIC OpenGL)
target_link_libraries(${TARGET} PUBLIC ${LUA_LIBRARY}) target_link_libraries(${TARGET} PUBLIC lua)

View file

@ -2,7 +2,7 @@
A work-in-progress, cross-platform roguelike written in C++ using SDL2 and OpenGL. A work-in-progress, cross-platform roguelike written in C++ using SDL2 and OpenGL.
![Screenshot](https://i.imgur.com/apzKOmm.png) ![Screenshot](https://i.imgur.com/aKUhgCz.png)
## Compiling ## Compiling
@ -26,8 +26,8 @@ I heavily recommend you to use [vcpkg](https://github.com/Microsoft/vcpkg) to in
* [ ] Data editors? * [ ] Data editors?
* [ ] Dungeon generation * [ ] Dungeon generation
* [x] Generate rooms & corridors * [x] Generate rooms & corridors
* [ ] Place entrance & exit * [x] Place entrance & exit
* [ ] Place enemies * [x] Place enemies
* [ ] Prefabs * [ ] Prefabs
* [ ] Items * [ ] Items
* [ ] Inventory * [ ] Inventory

View file

@ -49,6 +49,26 @@ local tiles = {
description = "A stone floor", description = "A stone floor",
tags = { "dungeon", "stone", "floor" } tags = { "dungeon", "stone", "floor" }
}, },
-- dungeon_floor2 = {
-- glyph = '.',
-- fg = {.85,.85,.9,1},
-- bg = {.15,.15,.25,1},
-- passable = true,
-- opaque = false,
-- wall = false,
-- description = "A stone floor",
-- tags = { "dungeon", "stone", "floor" }
-- },
-- dungeon_floor3 = {
-- glyph = '.',
-- fg = {.75,.75,.95,1},
-- bg = {.1,.1,.25,1},
-- passable = true,
-- opaque = false,
-- wall = false,
-- description = "A stone floor",
-- tags = { "dungeon", "stone", "floor" }
-- },
dungeon_entrance = { dungeon_entrance = {
glyph = '>', glyph = '>',
fg = {0.8,0.8,1.0,1.0}, fg = {0.8,0.8,1.0,1.0},
@ -69,6 +89,26 @@ local tiles = {
description = "Stone stairs going downwards", description = "Stone stairs going downwards",
tags = { "dungeon", "stone", "exit" } tags = { "dungeon", "stone", "exit" }
}, },
-- grass = {
-- glyph = '"',
-- fg = {.4,.95,.4,1},
-- bg = {.1,.15,.2,1},
-- passable = true,
-- opaque = false,
-- wall = false,
-- description = "A patch of grass",
-- tags = { "dungeon", "floor", "grass" }
-- },
-- moss = {
-- glyph = '.',
-- fg = {.4,.95,.4,1},
-- bg = {.1,.15,.2,1},
-- passable = true,
-- opaque = false,
-- wall = false,
-- description = "A patch of moss",
-- tags = { "dungeon", "floor", "moss" }
-- },
} }
local dijkstra_debug_amount = 100 local dijkstra_debug_amount = 100

View file

@ -1,5 +1,5 @@
#include "Mapgen.h" #include "Mapgen.h"
#include "vec2i.h" #include "vec2i.h"
#include <vector> #include <vector>
#include <queue> #include <queue>
#include <random> #include <random>
@ -20,6 +20,10 @@ bool aabb(Room &a, Room &b) {
a.pos.y <= b.pos.y + b.size.y && a.pos.y + a.size.y >= b.pos.y; a.pos.y <= b.pos.y + b.size.y && a.pos.y + a.size.y >= b.pos.y;
} }
template<class T> T rand_entry(std::vector<T> &vec, Rng &rng) {
return vec[rng.get_int(vec.size()-1)];
}
void maze_fill(Tilemap& map, int x, int y, std::string wall, std::string floor, Rng &rng) { void maze_fill(Tilemap& map, int x, int y, std::string wall, std::string floor, Rng &rng) {
if (!map.get_tile(x, y).wall) return; if (!map.get_tile(x, y).wall) return;
@ -55,7 +59,7 @@ void maze_fill(Tilemap& map, int x, int y, std::string wall, std::string floor,
} }
} }
if (!options.empty()) { if (!options.empty()) {
stack.emplace(options.at(rng.get_int(options.size() - 1))); stack.emplace(rand_entry<vec2i>(options,rng));
} }
else { else {
stack.pop(); stack.pop();
@ -89,10 +93,10 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height, TileSet& tile
#endif #endif
// Set the whole map to walls // Set the whole map to walls
for (int y = 0; y < height; ++y) { for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) { for (int x = 0; x < width; ++x) {
map.set_tile(x, y, wall_tiles[rng.get_int(0, wall_tiles.size()-1)]); map.set_tile(x, y, rand_entry<std::string>(wall_tiles, rng));
} }
} }
// Room placement // Room placement
@ -113,13 +117,13 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height, TileSet& tile
if (!coll) { if (!coll) {
rooms.emplace_back(room); rooms.emplace_back(room);
} }
} }
// Fill the rooms with floor tiles // Fill the rooms with floor tiles
for (Room r : rooms) { for (Room r : rooms) {
for (int x = r.pos.x+1; x < r.pos.x + r.size.x-1; x++) { for (int x = r.pos.x+1; x < r.pos.x + r.size.x-1; x++) {
for (int y = r.pos.y+1; y < r.pos.y + r.size.y-1; y++) { for (int y = r.pos.y+1; y < r.pos.y + r.size.y-1; y++) {
map.set_tile(x, y, floor_tiles[rng.get_int(0, floor_tiles.size() - 1)]); map.set_tile(x, y, rand_entry<std::string>(floor_tiles, rng));
} }
} }
} }
@ -137,7 +141,7 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height, TileSet& tile
} }
// If this tile is a wall and is completely surrounded by other walls, start generating a maze here. // If this tile is a wall and is completely surrounded by other walls, start generating a maze here.
if (count >= 8) { if (count >= 8) {
maze_fill(map, x, y, wall_tiles[rng.get_int(0, wall_tiles.size() - 1)], floor_tiles[rng.get_int(0, floor_tiles.size() - 1)], rng); maze_fill(map, x, y, rand_entry<std::string>(wall_tiles, rng), floor_tiles[rng.get_int(0, floor_tiles.size() - 1)], rng);
maze_start_points.emplace_back(vec2i(x, y)); maze_start_points.emplace_back(vec2i(x, y));
} }
} }
@ -190,7 +194,7 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height, TileSet& tile
int r = rng.get_int(potential_doors.size()-1); int r = rng.get_int(potential_doors.size()-1);
vec2i pos = potential_doors.at(r); vec2i pos = potential_doors.at(r);
map.set_tile(pos.x, pos.y, door_tiles[rng.get_int(0, door_tiles.size() - 1)]); map.set_tile(pos.x, pos.y, rand_entry<std::string>(door_tiles, rng));
potential_doors.erase(r + potential_doors.begin()); potential_doors.erase(r + potential_doors.begin());
for (int j = potential_doors.size() - 1; j >= 0; j--) { for (int j = potential_doors.size() - 1; j >= 0; j--) {
if ((pos - potential_doors[j]).dist() <= 4) { if ((pos - potential_doors[j]).dist() <= 4) {
@ -235,11 +239,11 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height, TileSet& tile
} }
} }
if (count == 1) { if (count == 1) {
map.set_tile(pos.x, pos.y, wall_tiles[rng.get_int(0, wall_tiles.size() - 1)]); map.set_tile(pos.x, pos.y, rand_entry<std::string>(wall_tiles, rng));
new_dead_ends.emplace_back(next); new_dead_ends.emplace_back(next);
} }
else if (count == 0) { else if (count == 0) {
map.set_tile(pos.x, pos.y, wall_tiles[rng.get_int(0, wall_tiles.size() - 1)]); map.set_tile(pos.x, pos.y, rand_entry<std::string>(wall_tiles, rng));
} }
} }
dead_ends = new_dead_ends; dead_ends = new_dead_ends;
@ -255,7 +259,8 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height, TileSet& tile
// Find the room furthest away from the entrance and make it the exit // Find the room furthest away from the entrance and make it the exit
Pathfinder::DijkstraMap dijk; Pathfinder::DijkstraMap dijk;
const float maxv = width+height; const float maxv = width+height;
Pathfinder::calc_dijkstra_map(map, std::vector<vec2i>{ startpos }, dijk, maxv); std::vector<vec2i> goals = { startpos };
Pathfinder::calc_dijkstra_map(map, goals, dijk, maxv);
float exitroomval = 0; float exitroomval = 0;
Room* exitroom = &startroom; Room* exitroom = &startroom;
@ -320,6 +325,6 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height, TileSet& tile
} }
} }
} }
//*/ //*/
return map; return map;
} }

View file

@ -17,7 +17,7 @@
#include "Rng.h" #include "Rng.h"
#include "TileSet.h" #include "TileSet.h"
#include "LuaHandler.h" #include "LuaHandler.h"
#include <kaguya\kaguya.hpp> #include <kaguya/kaguya.hpp>
InputAction player_action; InputAction player_action;
TileSet tileset; TileSet tileset;
@ -203,6 +203,7 @@ void PlayState::draw(double delta) {
ImGui::End(); ImGui::End();
} }
/*
if (debug_actors) { if (debug_actors) {
ImGui::Begin("Actors", &debug_actors); ImGui::Begin("Actors", &debug_actors);
@ -225,6 +226,7 @@ void PlayState::draw(double delta) {
ImGui::End(); ImGui::End();
} }
*/
} }
const vec2i asciisize = { const vec2i asciisize = {
@ -258,7 +260,8 @@ void PlayState::draw(double delta) {
int sprite = var->get_sprite_id(); int sprite = var->get_sprite_id();
Color fg = var->get_sprite_color()*0.35f; Color fg = var->get_sprite_color()*0.35f;
app->renderer->draw_sprite(ascii->get_sprite(sprite), fg, black, margin.x + (offset.x + pos.x) * asciisize.x, margin.y + (offset.y + pos.y) * asciisize.y); Color bg = tilemap->get_tile(pos.x, pos.y).bg;
app->renderer->draw_sprite(ascii->get_sprite(sprite), fg, bg, margin.x + (offset.x + pos.x) * asciisize.x, margin.y + (offset.y + pos.y) * asciisize.y);
} }
} }

View file

@ -1,7 +1,7 @@
#include "Shader.h" #include "Shader.h"
#include <SDL2\SDL_log.h> #include <SDL2/SDL_log.h>
#include <glbinding\gl\gl.h> #include <glbinding/gl/gl.h>
#include <glm\mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <map> #include <map>

View file

@ -3,8 +3,8 @@
#include <string> #include <string>
#include <map> #include <map>
#include <fstream> #include <fstream>
#include <glbinding\gl\types.h> #include <glbinding/gl/types.h>
#include <glm\common.hpp> #include <glm/common.hpp>
#include "Renderer.h" #include "Renderer.h"
class Shader { class Shader {

View file

@ -1,6 +1,6 @@
#include "TileSet.h" #include "TileSet.h"
#include <kaguya\kaguya.hpp> #include <kaguya/kaguya.hpp>
#include <SDL2\SDL_log.h> #include <SDL2/SDL_log.h>
Tile null = Tile(); Tile null = Tile();
@ -14,7 +14,7 @@ int TileSet::count() const {
} }
void TileSet::add_tile(std::string name, Tile tile) { void TileSet::add_tile(std::string name, Tile tile) {
tiles.insert_or_assign(name, tile); tiles[name] = tile;
} }
Tile const& TileSet::get_tile(std::string name) { Tile const& TileSet::get_tile(std::string name) {

View file

@ -158,10 +158,12 @@ void Tilemap::draw(Renderer *renderer, SpriteAtlas* sprites, int x, int y, int t
} }
void Tilemap::debug_print() { void Tilemap::debug_print() {
/*
for (int y = 0; y < height; ++y) { for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) { for (int x = 0; x < width; ++x) {
printf("\t%d", get_tile(x, y)); printf("\t%d", get_tile(x, y));
} }
printf("\n"); printf("\n");
} }
*/
} }