Create world.c

This commit is contained in:
2026-02-17 14:39:19 +01:00
parent e3c561f396
commit e74f1255a6
2 changed files with 43 additions and 3 deletions

40
iris_c/world.c Normal file
View File

@@ -0,0 +1,40 @@
#include "world.h"
#include <stddef.h>
#include "random.h"
static quadrant_type random_quadrant_type(void) {
uint8_t roll = random_range(100);
if (roll < 30) return Q_EMPTY;
if (roll < 40) return Q_ASTEROID_FIELD;
if (roll < 50) return Q_STAR_SYSTEM;
if (roll < 60) return Q_NEBULA;
if (roll < 70) return Q_BLACK_HOLE;
if (roll < 80) return Q_WHITE_HOLE;
if (roll < 85) return Q_PULSAR;
if (roll < 90) return Q_SUPERNOVA;
if (roll < 95) return Q_ANOMALY;
return Q_SINGULARITY;
}
void world_generate(world_t *w) {
if (w == NULL) return;
for (uint16_t y = 0; y < WORLD_HEIGHT; y++) {
for (uint16_t x = 0; x < WORLD_WIDTH; x++) {
quadrant_t *q = &w->quadrants[y][x];
q->type = random_quadrant_type();
q->is_old = (random_range(100) < 20);
q->is_visited = false;
q->is_discovered = false;
}
}
}
const quadrant_t *world_get_quadrant(const world_t *w, uint16_t x, uint16_t y) {
if (w == NULL) return NULL;
if (x >= WORLD_WIDTH || y >= WORLD_HEIGHT) return NULL;
return &w->quadrants[y][x];
}

View File

@@ -27,13 +27,13 @@ typedef struct {
bool is_visited;
bool is_discovered;
bool is_old;
} quadrant;
} quadrant_t;
typedef struct {
quadrant quadrants[WORLD_HEIGHT][WORLD_WIDTH];
quadrant_t quadrants[WORLD_HEIGHT][WORLD_WIDTH];
} world_t;
void world_generate(world_t *w);
const quadrant * world_get_quadrant(const world_t *w, uint16_t x, uint16_t y);
const quadrant_t * world_get_quadrant(const world_t *w, uint16_t x, uint16_t y);
#endif