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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
const std = @import("std");
const rl = @import("raylib");
const rlg = @import("raygui");
const assert = std.debug.assert;
const cell_size = 32;
const cells_width = 16.0;
const cells_height = 9.0;
const world_width = cells_width * cell_size;
const world_height = cells_height * cell_size;
const bg_color = rl.Color.init(0x20, 0x2e, 0x37, 0xFF);
var screenWidth: i32 = 1280;
var screenHeight: i32 = 720;
pub fn main() anyerror!void {
// Initialization
//--------------------------------------------------------------------------------------
rl.initWindow(screenWidth, screenHeight, "Hackathon");
defer rl.closeWindow(); // Close window and OpenGL context
// rl.setWindowState(.{ .window_resizable = true });
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
rl.setExitKey(.caps_lock);
//--------------------------------------------------------------------------------------
var game = Game.init();
defer game.deinit();
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
game.process();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
game.render();
//----------------------------------------------------------------------------------
}
}
const BugKind = enum {
nullptr_deref,
stack_overflow,
infinite_loop,
const count = @typeInfo(BugKind).Enum.fields.len;
};
const Bug = struct {
kind: BugKind,
health: f32,
position: rl.Vector2,
previous: rl.Vector2, // Center of prev
target: rl.Vector2, // Center of next cell
// TODO: bugs need to know their "previous" square, so they know their next
pub fn init(kind: BugKind, position: rl.Vector2) Bug {
return Bug{
.kind = kind,
.health = maxHealth(kind),
.position = position,
.previous = position,
.target = position.add(rl.Vector2.init(cell_size, 0)), // 1 unit to the right
};
}
pub fn maxHealth(kind: BugKind) f32 {
return switch (kind) {
.nullptr_deref => return 30,
.stack_overflow => return 100,
.infinite_loop => return 10,
};
}
fn speed(kind: BugKind) f32 {
return switch (kind) {
.nullptr_deref => return 30,
.stack_overflow => return 10,
.infinite_loop => return 100,
};
}
fn damage(self: Bug) f32 {
return switch (self.kind) {
.nullptr_deref => return 1,
.stack_overflow => return 2,
.infinite_loop => return 0.5,
} * self.health;
}
};
const Condition = union(enum) {
always,
memory_leak: f32, // Health percentage, ram<0.25 or ram<0.5 (randomized)
bug: BugKind, // Against this specific enemy type
idle: f32, // Idle time in seconds, 1s-2s (randomized)
};
const Instruction = struct {
condition: Condition,
opcode: Opcode,
const Opcode = union(enum) {
sleep: f32, // Slows enemies (multiplier of enemy speed, 0.5-0.9 randomized)
prefetch: f32, // Deals more damage (multiplier of cache size, 1.5-2 randomized)
overclock: f32, // Faster firerate (multiplier of clock speed, 1.1-1.5 randomized)
};
};
const Cpu = struct {
clock_speed: f32 = 1, // Fire rate, every how many seconds to fire
cache_size: f32 = 1, // Cache size, damage dealt
debugs: u32 = 0, // How many bugs were killed
instructions: []Instruction, // modifiers
fn cores(self: Cpu) u32 {
return switch (self.debugs) {
0...10 => 1,
10...25 => 2,
25...50 => 3,
50...100 => 4,
else => 5,
};
}
};
const Cell = union(enum) {
none,
socket,
cpu: Cpu,
ai,
lane,
color: rl.Color,
fn isLaneConnected(self: Cell) bool {
return switch (self) {
.lane => false,
.ai => true,
else => false,
};
}
};
const SpawnRule = struct {
from_time_s: f32,
to_time_s: f32,
bugs: [Bug.count]u32, // index is enum
};
const Wave = struct {
arena: std.heap.ArenaAllocator,
map: [cells_height][cells_width]Cell = [_][cells_width]Cell{[_]Cell{.none} ** cells_width} ** cells_height,
bugs: std.ArrayList(Bug),
time_since_start: f32,
fn init() Wave {
const height_middle = 4;
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
var wave = Wave{
.arena = arena,
.bugs = std.ArrayList(Bug).init(arena.allocator()),
};
for (1..cells_height - 1) |y| {
for (1..cells_width - 1) |x| {
const isEven = (y + x) % 2 == 0;
if (isEven) {
wave.map[y][x] = .{ .color = rl.Color.blue };
} else {
wave.map[y][x] = .socket;
}
}
}
wave.map[height_middle][0] = .ai;
for (1..cells_width - 1) |x| {
wave.map[height_middle][x] = .lane;
}
return wave;
}
fn update(self: *Wave, delta_time: f32) void {
// TODO: quite a bit of logic here
self.time_since_start += delta_time;
}
fn deinit(self: *Wave) void {
self.arena.deinit();
}
fn get(self: *Wave, x: usize, y: usize) Cell {
if (x >= cells_width or y >= cells_height) {
return .none;
}
return self.map[y][x];
}
};
const TextureKind = enum {
socket,
lane,
ai,
};
const Game = struct {
global_arena: std.heap.ArenaAllocator,
frame_arena: std.heap.ArenaAllocator,
texture_map: std.AutoHashMap(TextureKind, rl.Texture2D),
font_title: rl.Font,
font_normal: rl.Font,
camera: rl.Camera2D,
wave: Wave,
screen_state: union(enum) {
main: ScreenMainMenu,
battle: ScreenBattle,
},
fn init() Game {
var global_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const ga = global_arena.allocator();
var texture_map = std.AutoHashMap(TextureKind, rl.Texture2D).init(ga);
const lane = rl.loadTexture("assets/img/lane.png") catch unreachable;
const socket = rl.loadTexture("assets/img/socket.png") catch unreachable;
const ai = rl.loadTexture("assets/img/ai.png") catch unreachable;
texture_map.put(.socket, socket) catch unreachable;
texture_map.put(.lane, lane) catch unreachable;
texture_map.put(.ai, ai) catch unreachable;
const font_title = rl.loadFont("assets/font/DepartureMonoNerdFontMono-Regular.otf") catch unreachable;
const font_normal = rl.loadFont("assets/font/GohuFont14NerdFontMono-Regular.ttf") catch unreachable;
return .{
.camera = .{
.target = .{ .x = 128, .y = 128 },
.offset = .{
.x = @as(f32, @floatFromInt(screenWidth)) / 2,
.y = @as(f32, @floatFromInt(screenHeight)) / 2,
},
.rotation = 0,
.zoom = @as(f32, @floatFromInt(screenHeight)) / world_height,
},
.global_arena = global_arena,
.frame_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator),
.wave = .init(),
.texture_map = texture_map,
.font_title = font_title,
.font_normal = font_normal,
.screen_state = .{
//.battle = .{},
.main = .{},
},
};
}
fn deinit(self: *Game) void {
self.global_arena.deinit();
self.frame_arena.deinit();
}
fn frameStart(self: *Game) void {
const reset_successful = self.frame_arena.reset(.retain_capacity);
assert(reset_successful);
}
fn process(self: *Game) void {
std.debug.print("Delta time: {d}\n", .{rl.getFrameTime()});
switch (self.screen_state) {
.main => |m| {
var mut_m = m;
mut_m.update(self);
},
.battle => |b| {
var mut_b = b;
mut_b.update(self);
},
}
self.updateCamera();
}
fn render(self: *Game) void {
switch (self.screen_state) {
.main => |m| {
var mut_m = m;
mut_m.render(self);
},
.battle => |b| {
var mut_b = b;
mut_b.render(self);
},
}
}
fn updateCamera(self: *Game) void {
screenWidth = rl.getScreenWidth();
screenHeight = rl.getScreenHeight();
self.camera.offset = .{
.x = @as(f32, @floatFromInt(screenWidth)) / 2,
.y = @as(f32, @floatFromInt(screenHeight)) / 2,
};
self.camera.target = .{
.x = world_width / 2.0,
.y = world_height / 2.0,
};
// Take the average between the ratios
// This avoids "cheating" by changing the ratio to an extreme value
// in order to see more terrain in a certain axis
const width_ratio = @as(f32, @floatFromInt(screenWidth)) / world_width;
const height_ratio = @as(f32, @floatFromInt(screenHeight)) / world_height;
self.camera.zoom = (width_ratio + height_ratio) / 2;
}
};
const ScreenMainMenu = struct {
fn update(self: *ScreenMainMenu, game: *Game) void {
_ = self;
_ = game;
}
fn render(self: *ScreenMainMenu, game: *Game) void {
_ = self;
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(bg_color);
rl.drawTextEx(game.font_title, "Test", rl.Vector2.init(20, 20), 40, 4, rl.Color.white);
if (rlg.button(.{ .x = 30, .y = 30, .width = 200, .height = 100 }, "Start")) {
game.screen_state = .{ .battle = .{} };
}
}
};
const ScreenBattle = struct {
fn update(self: *ScreenBattle, game: *Game) void {
_ = self;
_ = game;
}
fn render(self: *ScreenBattle, game: *Game) void {
const a = game.frame_arena.allocator();
const map = game.wave.map;
const camera = game.camera;
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(bg_color);
{
rl.beginMode2D(camera);
defer rl.endMode2D();
// TODO: make bugs render between (on top of) lanes and (under) AI
for (0..map.len) |y| {
for (0..map[0].len) |x| {
self.drawCell(game, x, y);
}
}
}
rl.drawRectangle(
10,
10,
@divTrunc(screenWidth, 5),
@divTrunc(screenHeight, 5),
rl.fade(rl.Color.white, 0.6),
);
rl.drawRectangleLines(
10,
10,
@divTrunc(screenWidth, 5),
@divTrunc(screenHeight, 5),
rl.Color.black,
);
const font_size = @divTrunc(screenWidth, 80);
const debug_info = std.fmt.allocPrintZ(
a,
\\FPS: {}
\\Screen: {}x{}
\\World: {}x{} ({})
,
.{ rl.getFPS(), screenWidth, screenHeight, world_width, world_height, cell_size },
) catch return;
rl.drawText(debug_info, 20, 20, font_size, .black);
}
fn drawCell(self: *ScreenBattle, game: *Game, x: usize, y: usize) void {
_ = self;
switch (game.wave.map[y][x]) {
.none => return,
.socket => {
const texture = game.texture_map.get(.socket).?;
rl.drawTexture(texture, @intCast(x * cell_size), @intCast(y * cell_size), rl.Color.white);
},
.lane => {
const texture = game.texture_map.get(.lane).?;
const lane_left = game.wave.get(x - 1, y).isLaneConnected();
const lane_right = game.wave.get(x + 1, y).isLaneConnected();
const lane_top = game.wave.get(x, y - 1).isLaneConnected();
const lane_bottom = game.wave.get(x, y + 1).isLaneConnected();
var offset: f32 = undefined;
// Choose the correct sprite
if (lane_top and lane_left) {
offset = 3;
} else if (lane_top and lane_right) {
offset = 2;
} else if (lane_bottom and lane_left) {
offset = 5;
} else if (lane_bottom and lane_right) {
offset = 4;
} else if (lane_left or lane_right) {
offset = 0;
} else if (lane_top or lane_bottom) {
offset = 1;
}
rl.drawTextureRec(
texture,
.{ .x = 0, .y = offset * cell_size, .width = cell_size, .height = cell_size },
.{ .x = @floatFromInt(x * cell_size), .y = @floatFromInt(y * cell_size) },
rl.Color.white,
);
},
.cpu => {},
.ai => {
const texture = game.texture_map.get(.ai).?;
rl.drawTexture(texture, @intCast(x * cell_size), @intCast(y * cell_size), rl.Color.white);
},
.color => |color| {
rl.drawRectangle(
@intCast(x * cell_size),
@intCast(y * cell_size),
cell_size,
cell_size,
color,
);
},
}
}
};
|