-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
166 lines (132 loc) · 4.48 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <raylib.h>
#include <stdlib.h>
#include <math.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define BLOCK_SIZE 1.0f
#define MAP_X 12
#define MAP_Y 10
#define MAP_Z 7
typedef enum { GRASS, STONE, DIRT, WATER, AIR } BlockType;
typedef struct {
BlockType type;
} Block;
Block map[MAP_X][MAP_Y][MAP_Z];
void generateHills() {
for (int x = 1; x < MAP_X - 1; x++) {
for (int y = 1; y < MAP_Y - 1; y++) {
// Skip generating hills if there is water at this location
if (map[x][y][2].type == WATER) {
continue;
}
if (rand() % 20 == 0) {
const int height = rand() % (MAP_Z - 2) + 2;
const int width = rand() % 10 + 2;
int startZ = MAP_Z - height;
for (int z = startZ; z < MAP_Z; z++) {
for (int w = -width / 2; w <= width / 2; w++) {
float decreasingTop = width - z;
if (x + w + (int)round(decreasingTop) >= 0 && x + w + (int)round(decreasingTop) < MAP_X &&
y + w + (int)round(decreasingTop) >= 0 && y + w + (int)round(decreasingTop) < MAP_Y) {
map[x + w + (int)round(decreasingTop)][y + w + (int)round(decreasingTop)][z].type = STONE;
}
}
}
}
}
}
}
void placeWater(int x, int y) {
if (rand() % 40 == 0) {
map[x][y][2].type = WATER;
int seaSize = rand() % 4 + 1;
for (int i = 1; i <= seaSize; i++) {
for (int dirX = -1; dirX <= 1; dirX++) {
for (int dirY = -1; dirY <= 1; dirY++) {
int newX = x + i * dirX;
int newY = y + i * dirY;
if (newX >= 0 && newX < MAP_X && newY >= 0 && newY < MAP_Y) {
map[newX][newY][2].type = WATER;
}
}
}
}
}
}
void generateMap() {
for (int x = 0; x < MAP_X; x++) {
for (int y = 0; y < MAP_Y; y++) {
for (int z = 0; z < MAP_Z; z++) {
if (z < 2) {
map[x][y][z].type = DIRT; // Base layer set to have an minecraft junk look
} else if (z == 2) {
map[x][y][z].type = GRASS;
placeWater(x, y);
} else {
map[x][y][z].type = AIR;
}
}
}
}
generateHills();
}
void drawMap() {
for (int i = 0; i < MAP_X; i++) {
for (int j = 0; j < MAP_Y; j++) {
for (int z = 0; z < MAP_Z; z++) {
Vector3 position = { i * BLOCK_SIZE, z * BLOCK_SIZE, j * BLOCK_SIZE };
Color color;
switch (map[i][j][z].type) {
case GRASS:
color = GREEN;
break;
case STONE:
color = GRAY;
break;
case DIRT:
color = BROWN;
break;
case WATER:
color = BLUE;
break;
case AIR:
continue; // Skip drawing for air blocks
default:
color = WHITE;
break;
}
DrawCube(position, BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, color);
}
}
}
}
int main(void) {
// Initialization
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Procedural Map Generation 3D");
Camera3D camera = { 0 };
camera.position = (Vector3){ 20.0f, 20.0f, 20.0f };
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 45.0f;
SetTargetFPS(80);
generateMap();
while (!WindowShouldClose()) {
// Uncomment line below for camera control
// UpdateCamera(&camera, 0);
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && GetMouseX() > SCREEN_WIDTH - 50 && GetMouseY() < 50) {
generateMap();
}
// Draw
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
drawMap();
EndMode3D();
DrawFPS(10, 10);
DrawRectangle(SCREEN_WIDTH - 60, 10, 60, 30, GRAY);
DrawText("Generate", SCREEN_WIDTH - 55, 15, 10, WHITE);
EndDrawing();
}
CloseWindow();
return 0;
}