Compare commits
4 Commits
4fd9493380
...
e0a4d684fc
| Author | SHA1 | Date | |
|---|---|---|---|
| e0a4d684fc | |||
| abbc3e5aa7 | |||
| e74f1255a6 | |||
| e3c561f396 |
@@ -6,8 +6,8 @@ TARGET = main
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): main.c random.c random.h
|
||||
$(CC) $(CFLAGS) -o $@ main.c random.c
|
||||
$(TARGET): main.c random.c random.h world.c world.h
|
||||
$(CC) $(CFLAGS) -o $@ main.c random.c world.c
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET)
|
||||
|
||||
@@ -2,16 +2,53 @@
|
||||
#include <stdio.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() {
|
||||
uint32_t seed;
|
||||
printf("Enter seed: ");
|
||||
scanf("%u", &seed);
|
||||
set_random_seed(seed);
|
||||
uint32_t num = random_range(20);
|
||||
for (uint32_t i = 0; i < num; i++) {
|
||||
printf("%u\n", random_u32());
|
||||
|
||||
world_t world;
|
||||
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;
|
||||
}
|
||||
|
||||
40
iris_c/world.c
Normal file
40
iris_c/world.c
Normal 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];
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#define WORLD_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define WORLD_WIDTH 256
|
||||
#define WORLD_HEIGHT 256
|
||||
@@ -17,7 +18,22 @@ typedef enum {
|
||||
Q_QUASAR,
|
||||
Q_SUPERNOVA,
|
||||
Q_ANOMALY,
|
||||
Q_SINGULARITY
|
||||
Q_SINGULARITY,
|
||||
Q_COUNT
|
||||
} 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
|
||||
|
||||
Reference in New Issue
Block a user