aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-10-04 00:09:00 +1000
committerquou <quou@disroot.org>2024-10-04 00:09:00 +1000
commit7e74f4402ead8422c151e93190c5c259ddbaecb9 (patch)
treed0543748c3440fda75965ae93818de1b110aa5b8
parent16513f894829462fc788d951018a92babe4e9000 (diff)
WIP level generation
-rw-r--r--1bitjam.c4
-rw-r--r--Makefile1
-rw-r--r--intermediate/map.bmpbin30858 -> 41098 bytes
-rw-r--r--intermediate/rooms.bmpbin0 -> 2538 bytes
-rw-r--r--map.c160
-rw-r--r--map.h7
6 files changed, 133 insertions, 39 deletions
diff --git a/1bitjam.c b/1bitjam.c
index bd9687b..547eee7 100644
--- a/1bitjam.c
+++ b/1bitjam.c
@@ -26,9 +26,7 @@ int entrypoint(int argc, const char** argv, Arena* m) {
init_fps(&f, default_mpf);
world = arena_alloc(m, sizeof world);
init_world(world);
- generate_floor(&world->map, 0);
- inst_enemy(world, enemy_demon, 100 << fbits, 100 << fbits);
- inst_enemy(world, enemy_demon, 200 << fbits, 100 << fbits);
+ generate_floor(&world->map, world);
while (a->o) {
fps_begin(&f);
while (f.now >= f.next && a->o) {
diff --git a/Makefile b/Makefile
index 65f4b5d..2b6252e 100644
--- a/Makefile
+++ b/Makefile
@@ -69,6 +69,7 @@ image_sources = \
$(int_dir)/map.bmp \
$(int_dir)/mask.bmp \
$(int_dir)/npc.bmp \
+ $(int_dir)/rooms.bmp \
anim_sources = \
$(int_dir)/demon_fly_left.anm \
diff --git a/intermediate/map.bmp b/intermediate/map.bmp
index 13ee910..fd32c56 100644
--- a/intermediate/map.bmp
+++ b/intermediate/map.bmp
Binary files differ
diff --git a/intermediate/rooms.bmp b/intermediate/rooms.bmp
new file mode 100644
index 0000000..b9562e4
--- /dev/null
+++ b/intermediate/rooms.bmp
Binary files differ
diff --git a/map.c b/map.c
index c2d9e0d..9566aa2 100644
--- a/map.c
+++ b/map.c
@@ -1,7 +1,9 @@
#include "asset.h"
+#include "random.h"
#include "map.h"
#include "rect.h"
#include "render.h"
+#include "world.h"
#define null_tile 0xffff
@@ -12,44 +14,132 @@ Rect tile_rects[] = {
#undef x
};
-void generate_floor(Map* m, int seed) {
+void generate_room(Map* m) {
+ const Bitmap* b = get_bitmap(asset_id_rooms_img);
+ const unsigned* p = (const unsigned*)&b[1];
+ const int max_rooms = b->h / map_h;
+ const int r = /*get_r() % max_rooms;*/ 1;
+ int o = r * map_w * map_h;
int e = map_w * map_h, i;
- for (i = 0; i < e; i++) {
- m->tiles[i] = null_tile;
- m->collision[i] = 0;
+ for (i = 0; i < e; i++, o++) {
+ int bit = 1 << (o & 0x1f);
+ if (p[o >> 5] & bit)
+ m->tiles[i] = tile_brick;
+ else
+ m->tiles[i] = null_tile;
}
- e = map_w * (map_h - 1);
- for (i = 0; i < map_w; i++) {
- int idx = i + e;
- m->tiles[idx] = tile_stone;
- m->collision[idx] = 1;
+}
+
+unsigned short get_collider(Map_Tile t) {
+ switch (t) {
+ case tile_stone:
+ case tile_block1:
+ case tile_block2:
+ case tile_window_top:
+ case tile_window_mid:
+ case tile_window_bot:
+ case tile_brick_floor:
+ case tile_brick: return 1;
+ case tile_brick_ramp5:
+ case tile_stone_ramp5: return 2;
+ case tile_brick_ramp6:
+ case tile_stone_ramp6: return 3;
+ case tile_brick_ramp7:
+ case tile_stone_ramp7: return 4;
+ case tile_brick_ramp8:
+ case tile_stone_ramp8: return 5;
+ default:
+ return 0;
}
-#define set(x, y) \
- m->tiles[x + y * map_w] = tile_brick; \
- m->collision[x + y * map_w] = 1;
- set(0, 13);
- set(0, 12);
- set(0, 11);
- set(1, 11);
- set(2, 11);
- set(3, 11);
- set(4, 11);
- set(6, 11);
- set(7, 11);
- set(8, 11);
- set(19, 13);
- set(19, 12);
- set(19, 11);
- set(19, 10);
- set(19, 9);
- set(19, 8);
-#undef set
- m->tiles[18 + 13 * map_w] = tile_stone_ramp9;
- m->collision[18 + 13 * map_w] = 6;
- m->tiles[1 + 12 * map_w] = tile_stone_ramp13;
- m->collision[1 + 12 * map_w] = 10;
- m->tiles[2 + 12 * map_w] = tile_stone_ramp14;
- m->collision[2 + 12 * map_w] = 11;
+}
+
+void generate_collision(Map* m) {
+ int e = map_w * map_h, i;
+ for (i = 0; i < e; i++)
+ m->collision[i] = get_collider(m->tiles[i]);
+}
+
+int imp_check(Map* m, Map_Tile t, int x, int y) {
+ return
+ x >= 0 &&
+ y >= 0 &&
+ x < map_w &&
+ y < map_h &&
+ m->tiles[x + y * map_w] == t;
+}
+
+#define check(ox, oy, t) \
+ imp_check(m, t, x + ox, y + oy)
+#define place(t) \
+ m->tiles[x + y * map_w] = t
+#define place_o(t, ox, oy) \
+ m->tiles[x + ox + (y + oy) * map_w] = t
+
+void generate_ramps(Map* m) {
+ int x, y;
+ for (y = 0; y < map_h; y++)
+ for (x = 0; x < map_w; x++) {
+ if (
+ check( 0, 0, null_tile) &&
+ check( 1, 0, null_tile) &&
+ check(-1, 0, tile_brick) &&
+ !check(-1, -1, tile_brick) &&
+ check( 0, 1, tile_brick) &&
+ check( 1, 1, tile_brick)
+ ) {
+ place(tile_brick_ramp7);
+ place_o(tile_brick_ramp8, 1, 0);
+ }
+ if (
+ check( 0, 0, null_tile) &&
+ check(-1, 0, null_tile) &&
+ check( 1, 0, tile_brick) &&
+ !check( 1, -1, tile_brick) &&
+ check( 0, 1, tile_brick) &&
+ check(-1, 1, tile_brick)
+ ) {
+ place(tile_brick_ramp6);
+ place_o(tile_brick_ramp5, -1, 0);
+ }
+ if (
+ check(0, 0, tile_brick) &&
+ check(0, 1, tile_brick) &&
+ check(0, 2, tile_brick) && ((
+ check(1, 0, null_tile) &&
+ check(1, 1, null_tile) &&
+ check(1, 2, null_tile)) || (
+ check(-1, 0, null_tile) &&
+ check(-1, 1, null_tile) &&
+ check(-1, 2, null_tile)))
+ ) {
+ place(tile_window_top);
+ place_o(tile_window_mid, 0, 1);
+ place_o(tile_window_bot, 0, 2);
+ }
+ }
+}
+
+void generate_floors(Map* m) {
+ int x, y;
+ for (y = 0; y < map_h; y++)
+ for (x = 0; x < map_w; x++) {
+ if (
+ check(0, 0, tile_brick) &&
+ check(0, -1, null_tile)
+ ) place(tile_brick_floor);
+ }
+}
+
+#undef check
+#undef place
+
+void generate_floor(Map* m, World* w) {
+ generate_room(m);
+ generate_ramps(m);
+ generate_floors(m);
+ generate_collision(m);
+ w->player.x = 16 << fbits;
+ w->player.y = 16 << fbits;
}
void render_map(const Map* m, Renderer* r) {
diff --git a/map.h b/map.h
index 806de4d..6a3021d 100644
--- a/map.h
+++ b/map.h
@@ -4,6 +4,7 @@
#include "config.h"
struct Renderer;
+struct World;
#define tiles_xmacro \
x(tile_stone, 0, 0) \
@@ -35,6 +36,10 @@ struct Renderer;
x(tile_brick_ramp11, 96, 32) \
x(tile_brick_ramp12, 112, 32) \
x(tile_brick_ramp13, 128, 32) \
+ x(tile_window_top, 0, 48) \
+ x(tile_window_mid, 16, 48) \
+ x(tile_window_bot, 32, 48) \
+ x(tile_brick_floor, 48, 48)
typedef enum {
#define x(n, x, y) \
@@ -54,7 +59,7 @@ typedef struct Map {
*/
} Map;
-void generate_floor(Map* m, int seed);
+void generate_floor(Map* m, struct World* w);
void render_map(const Map* m, struct Renderer* r);
#endif