-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroguelike.vale
More file actions
436 lines (381 loc) · 11.1 KB
/
Copy pathroguelike.vale
File metadata and controls
436 lines (381 loc) · 11.1 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import v.builtins.print.*;
import v.builtins.arith.*;
import stdlib.math.*;
import stdlib.stdin.*;
// TODO: bring in stdin
func ==<A', B'>(a A'str, b B'str) bool {
streq(a, 0, a.len(), b, 0, b.len())
}
func between(x int, min int, max int) bool {
min <= x and x <= max
}
struct XS32Rand {
seed! i64;
}
/// Re-enable tuples
struct RandNextResult {
num i64;
rand XS32Rand;
}
// From https://github.com/cgmb/euler/blob/a906355343dc0d320858a7b00504040d04c053f7/misc/rng.c
// State must point to a non-zero value.
// Based on "An Experimental Exploration of Marsaglia's xorshift Generators, Scrambled" (2016)
func Next(self XS32Rand) RandNextResult {
x = self.seed;
set x = x xor (x rshift 12);
set x = x xor (x lshift 25);
set x = x xor (x rshift 27);
set self.seed = x;
return RandNextResult(abs(x * 2685821657736338717i64) rshift 32, self);
}
struct Goblin {
row! int;
col! int;
}
func MakeBoard(num_rows int, num_cols int) [][]str {
rows = [][]str(num_rows);
/// foreach _ in 0..num_rows {
row_i = 0;
while row_i < num_rows {
row = []str(num_cols);
/// foreach _ in 0..num_cols {
col_i = 0;
while col_i < num_cols {
row.push(
if row_i == 0 { "#" }
else if col_i == 0 { "#" }
else if row_i == num_rows - 1 { "#" }
else if col_i == num_cols - 1 { "#" }
else { "." });
set col_i = col_i + 1;
}
rows.push(row);
set row_i = row_i + 1;
}
return rows;
}
pure func Display<r'>(
board &r'[][]str,
goblins &r'[]Goblin,
playerRow int,
playerCol int)
{
toPrint = "";
/// foreach row_i in 0..board.len() {
row_i = 0;
while row_i < board.len() {
row = board[row_i];
/// foreach col_i in 0..row.len() {
col_i = 0;
while col_i < row.len() {
if row_i == playerRow and col_i == playerCol {
set toPrint = toPrint + "@";
} else if GoblinAt(&goblins, row_i, col_i) >= 0 {
set toPrint = toPrint + "g";
} else {
set toPrint = toPrint + row[col_i];
}
set col_i = col_i + 1;
}
set toPrint = toPrint + "\n";
set row_i = row_i + 1;
}
print(toPrint);
}
struct Direction {
row_delta int;
col_delta int;
}
struct Location {
row int;
col int;
}
/// Remove, bring back tuples
#!DeriveStructDrop
struct GoRandomDirectionResult {
row_delta int;
col_delta int;
new_rand XS32Rand;
}
/// func GoRandomDirection(rand XS32Rand) (Direction, XS32Rand)
func GoRandomDirection(rand XS32Rand) GoRandomDirectionResult {
[row_delta_i64, set rand] = Next(rand);
[col_delta_i64, set rand] = Next(rand);
row_delta = TruncateI64ToI32((row_delta_i64 mod 3i64) - 1i64);
col_delta = TruncateI64ToI32((col_delta_i64 mod 3i64) - 1i64);
/// return (Direction(row_delta, col_delta), rand);
return GoRandomDirectionResult(row_delta, col_delta, rand);
}
pure func GetGoblinAction<r'>(
board &r'[][]str,
goblins &r'[]Goblin,
rand_seed i64,
goblin_i int,
player_row int,
player_col int,
) Location {
rand = XS32Rand(-1i64 * rand_seed);
goblin = goblins[goblin_i];
rows_to_player = player_row - goblin.row;
cols_to_player = player_col - goblin.col;
if between(rows_to_player, -3, 3) and between(cols_to_player, -3, 3) {
// Player is near, charge!
new_row = goblin.row + signum(rows_to_player);
new_col = goblin.col + signum(cols_to_player);
if between(new_row, 0, board.len()) and between(new_col, 0, board[0].len()) {
if board[new_row][new_col] == "." {
if GoblinAt(goblins, new_row, new_col) == -1 {
return Location(new_row, new_col);
}
}
}
// continues
} else {
// Player isn't near, wander.
[row_delta, col_delta, set rand] = GoRandomDirection(rand);
new_row = goblin.row + row_delta;
new_col = goblin.col + col_delta;
if between(new_row, 0, board.len()) and between(new_col, 0, board[0].len()) {
if board[new_row][new_col] == "." {
if GoblinAt(goblins, new_row, new_col) == -1 {
return Location(new_row, new_col);
}
}
}
// continues
}
// Stay at current position
return Location(goblin.row, goblin.col);
}
/// Change to use a Result or Opt
struct InputDirectionResult {
row_delta int;
col_delta int;
quit bool;
}
func InputDirection() InputDirectionResult {
while true {
key = getch();
if (key == 81) { // q
return InputDirectionResult(0, 0, true);
} else if (key == 119) { // w
return InputDirectionResult(-1, 0, false);
} else if (key == 115) { // s
return InputDirectionResult(1, 0, false);
} else if (key == 97) { // a
return InputDirectionResult(0, -1, false);
} else if (key == 100) { // d
return InputDirectionResult(0, 1, false);
} else if (key == 10) {
// Enter key, do nothing
}
// Continue
}
print("Unreachable InputKey\n");
__vbi_panic();
}
struct GetPlayerActionResult {
player_row int;
player_col int;
quit bool;
}
pure func GetPlayerAction<r'>(
manual_control bool,
board &r'[][]str,
goblins &r'[]Goblin,
rand_seed i64,
player_row int,
player_col int,
) GetPlayerActionResult {
if manual_control {
[row_delta, col_delta, quit] = InputDirection();
if quit {
return GetPlayerActionResult(player_row, player_col, true);
} else {
player_new_row = player_row + row_delta;
player_new_col = player_col + col_delta;
if between(player_new_row, 0, board.len()) and between(player_new_col, 0, board[0].len()) {
if board[player_new_row][player_new_col] == "." {
return GetPlayerActionResult(player_new_row, player_new_col, false);
}
}
// Stay still
return GetPlayerActionResult(player_row, player_col, false);
}
} else {
rand = XS32Rand(-1i64 * rand_seed);
if goblins.len() == 0 {
return GetPlayerActionResult(player_row, player_col, true);
} else {
// Pick arbitrary goblin to head towards
goblin = goblins[0];
player_new_row = player_row + signum(goblin.row - player_row);
player_new_col = player_col + signum(goblin.col - player_col);
if between(player_new_row, 0, board.len()) and between(player_new_col, 0, board[0].len()) {
if board[player_new_row][player_new_col] == "." {
return GetPlayerActionResult(player_new_row, player_new_col, false);
}
}
// Stay still
return GetPlayerActionResult(player_row, player_col, false);
}
}
}
// Returns the index in `goblins` of the goblin at that location.
// Returns -1 if there isn't a goblin there
pure func GoblinAt<r'>(
goblins &r'[]Goblin,
row int,
col int)
int {
/// foreach goblin_i in 0..goblins.len() {
goblin_i = 0;
while goblin_i < goblins.len() {
[goblinRow, goblinCol] = goblins[goblin_i];
if row == goblinRow and col == goblinCol {
return goblin_i;
}
set goblin_i = goblin_i + 1;
}
return -1;
}
/// Remove, bring back tuples
#!DeriveStructDrop
struct ExtractGoblinResult {
goblin Goblin;
goblins []Goblin;
}
/// func ExtractGoblin(goblins []Goblin, index int) (Goblin, []Goblin) {
func ExtractGoblin(goblins []Goblin, index int) ExtractGoblinResult {
/// Add a dedicated borrow-element operation for this, so it's less verbose.
return ExtractGoblinResult(
if index == goblins.len() - 1 {
goblins.pop()
} else {
// Swap end goblin to this position
set goblins[index] = goblins.pop()
},
goblins);
}
func InsertGoblin(goblins []Goblin, index int, goblin Goblin) []Goblin {
/// Add a dedicated borrow-element operation for this, so it's less verbose.
if index == goblins.len() {
goblins.push(goblin);
} else {
// Swap the goblin here to the end
goblins.push(set goblins[index] = goblin);
}
return goblins;
}
/// Remove, bring back tuples
#!DeriveStructDrop
struct AddGoblinResult {
goblins []Goblin;
rand XS32Rand;
}
func AddGoblin(
goblins []Goblin,
rand XS32Rand,
num_rows int,
num_cols int)
/// ([]Goblin, XS32Rand) {
AddGoblinResult {
/// foreach _ in 0..100 {
attempt = 0;
while attempt < 100 {
[goblin_row_i64, set rand] = Next(rand);
goblin_row = (TruncateI64ToI32(goblin_row_i64) mod (num_rows - 2)) + 1;
[goblin_col_i64, set rand] = Next(rand);
goblin_col = (TruncateI64ToI32(goblin_col_i64) mod (num_cols - 2)) + 1;
if between(goblin_row, 1, num_rows - 2) and between(goblin_col, 1, num_cols - 2) {
if GoblinAt(&goblins, goblin_row, goblin_col) == -1 {
goblins.push(Goblin(goblin_row, goblin_col));
break;
}
}
set attempt = attempt + 1;
}
return AddGoblinResult(goblins, rand);
}
exported func main() int {
manual_control =
(numMainArgs() > 1 and getMainArg(1) == "--manual_control") or
(numMainArgs() > 2 and getMainArg(2) == "--manual_control");
display =
(numMainArgs() > 1 and getMainArg(1) == "--display") or
(numMainArgs() > 2 and getMainArg(2) == "--display");
num_rows = 250;
num_cols = 1000;
rand = XS32Rand(1337i64);
board = MakeBoard(num_rows, num_cols);
playerRow = num_rows / 2;
playerCol = num_cols / 2;
initial_num_goblins = 500;
goblins = []Goblin(initial_num_goblins);
/// foreach _ in 0..initial_num_goblins {
igi = 0;
while igi < initial_num_goblins {
[set goblins, set rand] = AddGoblin(goblins, rand, num_rows, num_cols);
set igi = igi + 1;
}
/// foreach _ in 0..1000 {
num_turns = 0;
while num_turns < 1000 {
if display {
Display(&board, &goblins, playerRow, playerCol);
}
[gpa_rand_seed, set rand] = Next(rand);
[new_player_row, new_player_col, quit] =
GetPlayerAction(manual_control, &board, &goblins, gpa_rand_seed, playerRow, playerCol);
if quit {
if display {
Display(&board, &goblins, playerRow, playerCol);
print("Game ended!\n");
}
break;
}
killed_goblin_index = GoblinAt(&goblins, new_player_row, new_player_col);
if killed_goblin_index >= 0 {
[killed_goblin, set goblins] = ExtractGoblin(goblins, killed_goblin_index);
} else {
set playerRow = new_player_row;
set playerCol = new_player_col;
}
/// foreach goblin_i in 0..goblins.len() {
goblin_i = 0;
while goblin_i < goblins.len() {
[gga_rand_seed, set rand] = Next(rand);
[goblin_new_row, goblin_new_col] =
GetGoblinAction(&board, &goblins, gga_rand_seed, goblin_i, playerRow, playerCol);
if goblin_new_row == playerRow and goblin_new_col == playerCol {
// Hit player logic would go here
} else {
[goblin, set goblins] = ExtractGoblin(goblins, goblin_i);
set goblin.row = goblin_new_row;
set goblin.col = goblin_new_col;
//[old_row, old_col] = goblin;
//set goblin = Goblin(goblin_new_row, goblin_new_col);
set goblins = InsertGoblin(goblins, goblin_i, goblin);
}
set goblin_i = goblin_i + 1;
}
set num_turns = num_turns + 1;
}
/// Re-enable struct drop
[zzz] = rand;
/// Re-enable array drop
while goblins.len() > 0 {
[row, col] = goblins.pop();
}
[] = goblins;
/// Re-enable array drop
while board.len() > 0 {
row = board.pop();
while row.len() > 0 {
row.pop();
}
[] = row;
}
[] = board;
return 0;
}