Compare commits

...

4 Commits

Author SHA1 Message Date
e0a4d684fc Generate a world and show the centre quadrant 2026-02-17 14:44:39 +01:00
abbc3e5aa7 Add world files to build 2026-02-17 14:40:49 +01:00
e74f1255a6 Create world.c 2026-02-17 14:39:19 +01:00
e3c561f396 Pretty sure this is world.h done for now 2026-02-17 14:06:53 +01:00
4 changed files with 100 additions and 7 deletions

View File

@@ -6,8 +6,8 @@ TARGET = main
all: $(TARGET) all: $(TARGET)
$(TARGET): main.c random.c random.h $(TARGET): main.c random.c random.h world.c world.h
$(CC) $(CFLAGS) -o $@ main.c random.c $(CC) $(CFLAGS) -o $@ main.c random.c world.c
clean: clean:
rm -f $(TARGET) rm -f $(TARGET)

View File

@@ -2,16 +2,53 @@
#include <stdio.h> #include <stdio.h>
#include "random.h" #include "random.h"
#include "world.h"
static const char *quadrant_type_name(quadrant_type type) {
switch (type) {
case Q_EMPTY: return "Empty";
case Q_ASTEROID_FIELD: return "Asteroid field";
case Q_STAR_SYSTEM: return "Star system";
case Q_NEBULA: return "Nebula";
case Q_BLACK_HOLE: return "Black hole";
case Q_WHITE_HOLE: return "White hole";
case Q_PULSAR: return "Pulsar";
case Q_QUASAR: return "Quasar";
case Q_SUPERNOVA: return "Supernova";
case Q_ANOMALY: return "Anomaly";
case Q_SINGULARITY: return "Singularity";
case Q_COUNT: return "Count";
default: return "Undefined";
}
}
int main() { int main() {
uint32_t seed; uint32_t seed;
printf("Enter seed: "); printf("Enter seed: ");
scanf("%u", &seed); scanf("%u", &seed);
set_random_seed(seed); set_random_seed(seed);
uint32_t num = random_range(20);
for (uint32_t i = 0; i < num; i++) { world_t world;
printf("%u\n", random_u32()); world_generate(&world);
uint16_t x = WORLD_WIDTH / 2;
uint16_t y = WORLD_HEIGHT / 2;
const quadrant_t *centre = world_get_quadrant(&world, x, y);
if (centre == NULL) {
printf("Failed to read centre quadrant.\n");
return 1;
} }
printf("%u random numbers\n", num);
printf("Generated world: %ux%u\n", WORLD_WIDTH, WORLD_HEIGHT);
printf(
"Centre (%u, %u): type=%s (%d), old=%s, visited=%s, discovered=%s\n",
x, y,
quadrant_type_name(centre->type),
(int)centre->type,
centre->is_old ? "true" : "false",
centre->is_visited ? "true" : "false",
centre->is_discovered ? "true" : "false"
);
return 0; return 0;
} }

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

@@ -2,6 +2,7 @@
#define WORLD_H #define WORLD_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#define WORLD_WIDTH 256 #define WORLD_WIDTH 256
#define WORLD_HEIGHT 256 #define WORLD_HEIGHT 256
@@ -17,7 +18,22 @@ typedef enum {
Q_QUASAR, Q_QUASAR,
Q_SUPERNOVA, Q_SUPERNOVA,
Q_ANOMALY, Q_ANOMALY,
Q_SINGULARITY Q_SINGULARITY,
Q_COUNT
} quadrant_type; } quadrant_type;
typedef struct {
quadrant_type type;
bool is_visited;
bool is_discovered;
bool is_old;
} quadrant_t;
typedef struct {
quadrant_t quadrants[WORLD_HEIGHT][WORLD_WIDTH];
} world_t;
void world_generate(world_t *w);
const quadrant_t * world_get_quadrant(const world_t *w, uint16_t x, uint16_t y);
#endif #endif