/
hiveminder.zig
2034 lines (1937 loc) · 77.3 KB
/
hiveminder.zig
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const c = @import("interface.zig");
const Haathi = @import("haathi.zig").Haathi;
const colors = @import("colors.zig");
const MouseState = @import("inputs.zig").MouseState;
const pi = std.math.pi;
const helpers = @import("helpers.zig");
const Vec2 = helpers.Vec2;
const Vec2i = helpers.Vec2i;
const Vec4 = helpers.Vec4;
const Rect = helpers.Rect;
const Button = helpers.Button;
const FONT_1 = "18px JetBrainsMono";
const FONT_2 = "12px JetBrainsMono";
const FONT_3 = "14px JetBrainsMono";
const HEX_SCALE = 30;
const HIVE_SIZE = 7;
const HIVE_ORIGIN = Vec2{ .x = (1280 - HIVE_STATS_WIDTH) / 2, .y = 720 / 2 };
const NUM_SLOTS = 6;
const BEE_TRAVEL_SPEED = 0.35;
const BEE_REACH_DISTANCE_SQR = HEX_SCALE * 0.2;
const POLLEN_WAYPOINT = Vec2i{ .x = -8, .y = 16 };
/// number of ticks between reduction of health / rest for bees
const BEE_TICK_RATE = 50;
/// number of ticks between reduction of health for rooms
const ROOM_MAINTENANCE_TICK_RATE = 350;
const BEE_SPEED_VARIATION = 0.2;
const PRINT_F_DEBUG = false;
const PRINT_F_DEBUG_RENDER = false;
const BEE_STATUS_BAR_HEIGHT = 5;
const BEE_STATUS_BAR_WIDTH = 80;
const BEE_HEALTH_STATUS_X = 1280 - BEE_STATUS_BAR_WIDTH - BEE_STATUS_BAR_WIDTH - 20;
const BEE_REST_STATUS_X = 1280 - BEE_STATUS_BAR_WIDTH - 10;
const NUM_BEES = 100;
const DELTA_T_CAP = 200;
const BEE_AGE_LIMIT = 900;
const NUM_START_BEES = 6;
const QUEEN_ADDRESS = Vec2i{ .x = 0, .y = 0 };
const BEES_START_ADDRESS = Vec2i{ .x = -5, .y = 5 };
const BEE_BIRTH_TICKS = 20;
const OCCUPIED_HIVE_COLOR = colors.solarized_base1;
const UNOCCUPIED_CELL_COLOR = colors.solarized_base2;
const CELL_OUTLINE_COLOR = colors.solarized_base00;
const BUTTON_ROW_WIDTH = BEE_HEALTH_STATUS_X;
const BUTTON_PADDING = 20;
const BUTTON_HEIGHT = 22;
const BUTTON_ROW_Y = 720 - BUTTON_HEIGHT - BUTTON_PADDING;
const NUM_BUILDERS_REQUIRED_PER_ROOM = 6;
const FULL_CELL_SCALE = 0.95;
const ZERO_CONSTRUCTED_CELL_SCALE = 0.35;
const SLOT_LERP_AMOUNT = 0.6;
const BEE_FALL_ACCELERATION = 0.02;
const BEE_FULL_HEALTH = 550;
const BEE_HUNGRY_THRESHOLD = 400;
const BEE_STARVING_THRESHOLD = 80;
const HIVE_STATS_WIDTH = 400;
const HIVE_STATS_PADDING = 20;
const HIVE_STATS_HEADER = "hive stats";
const HIVE_STATS_POPULATION = "population";
const HIVE_STATS_FOOD = "food";
const HIVE_STATS_REST = "rest";
const HIVE_STATS_JOBS = "jobs";
const HIVE_STATS_AGE = "age";
const QMARK = "?";
const CONTROLS_Y = 720 - BUTTON_HEIGHT - (HIVE_STATS_PADDING * 1);
const DATA_ROW_SPACING = 22;
const CELL_BUTTON_CENTER = Vec2{ .x = 1280 - (HIVE_STATS_WIDTH / 2), .y = 300 };
const TOOLTIPS = [_][]const u8{
"alive / retired / abandoned (not fed)",
"starving / hungry / well fed",
"exhausted / tired / energised",
"young / adult / old",
};
const BASE_INSTRUCTIONS = [3][]const u8{
"drag rooms onto the hive",
"rooms have 2 cell radius of effect",
"right click to delete room",
};
const STORAGE_INSTRUCTIONS = [3][]const u8{
"storage room",
"stores food for bees and babees",
"must be in range of rooms: i, g",
};
const BABYSITTING_INSTRUCTIONS = [3][]const u8{
"babeesitting room",
"takes care of babees in incubator",
"must be in range of rooms: i",
};
const CONSTRUCTION_INSTRUCTIONS = [3][]const u8{
"constructors room",
"constructs rooms in the area",
"required for any room construction",
};
const REST_INSTRUCTIONS = [3][]const u8{
"rest room",
"room for bees to rest in",
"harwdworking bees need their rest",
};
const INCUBATOR_INSTRUCTIONS = [3][]const u8{
"incubator room",
"eggs are brought here to hatch",
"must be in range of rooms: b, s",
};
const GATHERING_INSTRUCTIONS = [3][]const u8{
"gatherers room",
"required for collection of food",
"must be in range of rooms: s",
};
const MAINTENANCE_INSTRUCTIONS = [3][]const u8{
"maintainers room",
"maintains rooms in the area",
"required for any room maintenance",
};
const Address = Vec2i;
const HEX_OFFSETS = [6]Vec2{
.{ .x = @cos(2 * pi * (0.0 / 6.0)), .y = @sin(2 * pi * (0.0 / 6.0)) },
.{ .x = @cos(2 * pi * (1.0 / 6.0)), .y = @sin(2 * pi * (1.0 / 6.0)) },
.{ .x = @cos(2 * pi * (2.0 / 6.0)), .y = @sin(2 * pi * (2.0 / 6.0)) },
.{ .x = @cos(2 * pi * (3.0 / 6.0)), .y = @sin(2 * pi * (3.0 / 6.0)) },
.{ .x = @cos(2 * pi * (4.0 / 6.0)), .y = @sin(2 * pi * (4.0 / 6.0)) },
.{ .x = @cos(2 * pi * (5.0 / 6.0)), .y = @sin(2 * pi * (5.0 / 6.0)) },
};
const NEIGHBOURS_1 = [6]Vec2i{
.{ .x = 1, .y = 1 },
.{ .x = 1, .y = -1 },
.{ .x = -1, .y = -1 },
.{ .x = -1, .y = 1 },
.{ .x = 0, .y = 2 },
.{ .x = 0, .y = -2 },
};
const NEIGHBOURS_2 = [12]Vec2i{
.{ .x = 0, .y = 4 },
.{ .x = 1, .y = 3 },
.{ .x = 2, .y = 2 },
.{ .x = 2, .y = 0 },
.{ .x = 2, .y = -2 },
.{ .x = 1, .y = -3 },
.{ .x = 0, .y = -4 },
.{ .x = -1, .y = -3 },
.{ .x = -2, .y = -2 },
.{ .x = -2, .y = 0 },
.{ .x = -2, .y = 2 },
.{ .x = -1, .y = 3 },
};
const SELF = [1]Vec2i{.{ .x = 0, .y = 0 }};
const NEIGHBOURS = NEIGHBOURS_1 ++ NEIGHBOURS_2;
const MAINTAINING_NEIGHBOURS = SELF ++ NEIGHBOURS;
// Hex Indexing.
// Our hexes are drawn with one set of parallel edges parallel to the x axis
// The way that we chose to do hex indexing is:
// If y is even, then x is also even
// If y is odd, then x is also odd.
// There is no cell where x is even and y is odd or vice versa
// for a given cell, its neighbors are (1,1), (1,-1), (-1, 1), (-1, -1), (0,2), (0,-2)
pub const Cell = struct {
const Self = @This();
points: [6]Vec2,
address: Address = .{},
pub fn init(address: Vec2i, scale: f32) Self {
var self: Self = undefined;
const center = Cell.addressToPos(address);
for (HEX_OFFSETS, 0..) |ho, i| {
self.points[i] = center.add(ho.scale(HEX_SCALE * scale));
}
self.address = address;
return self;
}
pub fn initPos(center: Vec2, scale: f32) Self {
var self: Self = undefined;
for (HEX_OFFSETS, 0..) |ho, i| {
self.points[i] = center.add(ho.scale(HEX_SCALE * scale));
}
return self;
}
pub fn isValidCellAddress(address: Vec2i) bool {
const x_even = @mod(address.x, 2) == 0;
const y_even = @mod(address.y, 2) == 0;
return x_even == y_even;
}
pub fn resetScale(self: *Self, scale: f32) void {
const center = Cell.addressToPos(self.address);
for (HEX_OFFSETS, 0..) |ho, i| {
self.points[i] = center.add(ho.scale(HEX_SCALE * scale));
}
}
pub fn containsPoint(self: *const Self, pos: Vec2) bool {
const bounding_box = Rect{
.position = .{ .x = self.points[3].x, .y = self.points[5].y },
.size = .{ .x = self.points[0].x - self.points[3].x, .y = self.points[1].y - self.points[5].y },
};
return helpers.polygonContainsPoint(self.points[0..], pos, bounding_box);
}
pub fn centerPos(self: *const Self) Vec2 {
return self.points[0].lerp(self.points[3], 0.5);
}
/// returns the center of the cell
pub fn addressToPos(address: Address) Vec2 {
const origin = HIVE_ORIGIN;
const center = origin.add(.{ .x = (HEX_SCALE + (HEX_SCALE * HEX_OFFSETS[5].x - HEX_OFFSETS[4].x)) * @as(f32, @floatFromInt(address.x)), .y = (HEX_SCALE * HEX_OFFSETS[5].y) * @as(f32, @floatFromInt(address.y)) });
return center;
}
/// returns the points at the center of the edges
pub fn slotOffsets(self: *const Self) [6]Vec2 {
const center = Cell.addressToPos(self.address);
var slots: [6]Vec2 = undefined;
for (self.points, 0..) |p0, i| {
const p1 = if (i == 5) self.points[0] else self.points[i + 1];
slots[i] = center.lerp(p0.lerp(p1, 0.5), SLOT_LERP_AMOUNT);
}
return slots;
}
pub fn slotPos(location: Location) Vec2 {
return Cell.init(location.address, FULL_CELL_SCALE).slotOffsets()[location.slot_index];
}
};
pub const RoomType = enum {
queen,
babysitting,
collection,
building,
maintaining,
rest,
storage,
incubator,
};
const NUM_ROOMS = @typeInfo(RoomType).Enum.fields.len;
const ROOM_TITLES = [NUM_ROOMS][]const u8{
"queen",
"babee-sitting",
"gatherers",
"constructors",
"maintainers",
"resting",
"storage",
"incubator",
};
const ROOM_LABELS = [NUM_ROOMS][]const u8{
"q",
"b",
"g",
"c",
"m",
"r",
"s",
"i",
};
const ROOM_INSTRUCIONS = [_]*const [3][]const u8{
&BABYSITTING_INSTRUCTIONS,
&GATHERING_INSTRUCTIONS,
&CONSTRUCTION_INSTRUCTIONS,
&MAINTENANCE_INSTRUCTIONS,
&REST_INSTRUCTIONS,
&STORAGE_INSTRUCTIONS,
&INCUBATOR_INSTRUCTIONS,
};
pub const RoomData = union(RoomType) {
const Self = @This();
queen: void,
babysitting: void,
collection: void,
building: void,
maintaining: void,
rest: void,
storage: void,
/// 0 is empty - request egg
/// 1 is egg - request food
/// 2 is has food - request bee
/// 3+ has everything, needs time to grow
incubator: u8,
pub fn fromType(room: RoomType) Self {
return switch (room) {
.queen => .queen,
.babysitting => .babysitting,
.collection => .collection,
.building => .building,
.maintaining => .maintaining,
.rest => .rest,
.storage => .storage,
.incubator => .{ .incubator = 0 },
};
}
pub fn employsBees(self: *const Self) bool {
return switch (self.*) {
.queen,
.storage,
.incubator,
.rest,
=> false,
.babysitting,
.collection,
.building,
.maintaining,
=> true,
};
}
};
pub const Room = struct {
const Self = @This();
room: RoomData,
/// For each bee that does a construction job, add one to this.
constructed: u8 = 0,
address: Vec2i,
slots_available: [NUM_SLOTS]bool = [_]bool{true} ** NUM_SLOTS,
/// flag if that slot has already released a signal.
slots_signals: [NUM_SLOTS]bool = [_]bool{false} ** NUM_SLOTS,
health: u8 = 255,
maintenance_signal: bool = false,
pub fn isConstructed(self: *const Self) bool {
return self.constructed >= NUM_BUILDERS_REQUIRED_PER_ROOM;
}
pub fn isUnusable(self: *const Self) bool {
return self.health == 0;
}
pub fn needsMaintaining(self: *const Self) bool {
return (self.maintenance_signal == false) and self.health <= 100;
}
pub fn needsMoreConstruction(self: *const Self) bool {
return self.constructed + self.liveSignalCount() < NUM_BUILDERS_REQUIRED_PER_ROOM;
}
pub fn initSlots(self: *Self) void {
self.slots_signals = [_]bool{false} ** NUM_SLOTS;
self.slots_available = [_]bool{true} ** NUM_SLOTS;
}
pub fn tryConsumeSignal(self: *Self, signal: *const Signal) ?Signal {
if (!self.isConstructed()) return null;
if (self.isUnusable()) return null;
if (signal.consumed) return null;
if (self.room == .collection and signal.signal == .storage_space_available) {
for (self.slots_available, 0..) |avail, i| {
if (self.slots_signals[i]) continue;
if (avail) {
self.slots_signals[i] = true;
return Signal{
.signal = .collection_bee_required,
.room = .{ .address = self.address, .slot_index = @as(u8, @intCast(i)) },
.waypoint = .{ .address = POLLEN_WAYPOINT, .slot_index = 0 },
.destination = signal.room,
};
}
}
}
if (self.room == .building and signal.signal == .room_construction_required) {
for (self.slots_available, 0..) |avail, i| {
if (self.slots_signals[i]) continue;
if (avail) {
self.slots_signals[i] = true;
return Signal{
.signal = .building_bee_required,
// room is building room
.room = .{ .address = self.address, .slot_index = @as(u8, @intCast(i)) },
.waypoint = undefined,
// room to be built
.destination = signal.room,
};
}
}
}
if (self.room == .incubator and self.slots_signals[0] == false) { // not yet sent signal
const stage = self.room.incubator;
if (stage == 1 and signal.signal == .storage_food_available) {
self.slots_signals[0] = true;
return Signal{
.signal = .incubator_food_required,
// room is the incubator
.room = .{ .address = self.address, .slot_index = 0 },
// waypoint is location of the food
.waypoint = signal.room,
// destination is undefined
.destination = undefined,
};
}
}
if (self.room == .babysitting) {
for (self.slots_available, 0..) |avail, i| {
if (self.slots_signals[i]) continue;
if (!avail) continue;
switch (signal.signal) {
.incubator_food_required => {
self.slots_signals[i] = true;
return Signal{
.signal = .babysitting_food_required,
// room is the babysitting
.room = .{ .address = self.address, .slot_index = @as(u8, @intCast(i)) },
// waypoint is the location of the food
.waypoint = signal.waypoint,
// destination is incubator
.destination = signal.room,
};
},
.incubator_egg_required => {
self.slots_signals[i] = true;
return Signal{
.signal = .babysitting_egg_required,
// room is the babysitting
.room = .{ .address = self.address, .slot_index = @as(u8, @intCast(i)) },
// waypoint is the location of the egg
.waypoint = signal.waypoint,
// destination is incubator
.destination = signal.room,
};
},
.incubator_attention_required => {
self.slots_signals[i] = true;
return Signal{
.signal = .babysitting_attention_required,
// room is the babysitting
.room = .{ .address = self.address, .slot_index = @as(u8, @intCast(i)) },
// waypoint is undefined
.waypoint = undefined,
// destination is incubator
.destination = signal.room,
};
},
else => {},
}
}
}
return null;
}
pub fn slotCount(self: *const Self) u8 {
var count: u8 = 0;
for (self.slots_available) |slot| {
if (slot) count += 1;
}
return count;
}
pub fn liveSignalCount(self: *const Self) u8 {
var count: u8 = 0;
for (self.slots_signals) |slot| {
if (slot) count += 1;
}
return count;
}
};
const Location = struct {
address: Address,
slot_index: u8,
};
const JobState = enum {
to_room,
room_to_waypoint,
waypoint_to_destination,
room_to_destination,
at_destination,
};
pub const Role = enum {
const Self = @This();
babysitting_egg,
babysitting_food,
babysitting_attention,
collection,
building,
maintaining,
eating,
rest,
pub fn ticksLength(self: *const Self) u64 {
return switch (self.*) {
.collection => 10,
else => 1000,
};
}
/// A room consumed the initial signal. The bee now needs to close the signal that
/// was left open by the room
pub fn shouldCloseDestinationSignals(self: *const Self) bool {
return switch (self.*) {
.collection,
.building,
.babysitting_egg,
.babysitting_food,
.babysitting_attention,
=> true,
// maintenance will not be using standard signals.
.maintaining,
.eating,
.rest,
=> false,
};
}
/// A room consumed the initial signal. The bee now needs to lost the signal that
/// was left open by the room
pub fn shouldCloseWaypointSignals(self: *const Self) bool {
return switch (self.*) {
//
.babysitting_food => true,
.collection,
.babysitting_egg,
.babysitting_attention,
.building,
.maintaining,
.eating,
.rest,
=> false,
};
}
};
pub const Job = struct {
const Self = @This();
role: Role,
/// room is the location where the bee is assigned
room: Location,
/// waypoint is a mid location that the bee must visit
waypoint: ?Location,
/// destination is the location where the job is done
/// some jobs may not have destinations.
destination: ?Location,
// when the job was created
ticks_created: u64 = 0,
ticks_room_reached: u64 = 0,
ticks_waypoint_reached: u64 = 0,
ticks_destination_reached: u64 = 0,
pub fn started(self: *const Self) bool {
return self.ticks_room_reached > 0;
}
/// the job is either
pub fn getCurrentStage(self: *const Self) JobState {
if (self.ticks_room_reached == 0) return .to_room;
if (self.waypoint == null and self.destination == null) return .at_destination;
if (self.waypoint) |_| {
if (self.ticks_waypoint_reached == 0) return .room_to_waypoint;
if (self.ticks_destination_reached == 0) return .waypoint_to_destination;
}
if (self.destination) |_| {
if (self.ticks_destination_reached == 0) return .room_to_destination;
return .at_destination;
}
c.debugPrint("could not getCurrentStage for job");
unreachable;
}
/// address of where the bee should be going now
pub fn getCurrentTargetLocation(self: *const Self) Location {
return switch (self.getCurrentStage()) {
.to_room => self.room,
.room_to_waypoint => self.waypoint.?,
.waypoint_to_destination, .room_to_destination => self.destination.?,
.at_destination => {
c.debugPrint("try to getCurrentTargetLocation of .at_destination");
unreachable;
},
};
}
/// the bee has just reached its target cell. what now.
pub fn nextStage(old_job: *const Self, bee: *Bee, ticks: u64) Self {
var self = old_job.*;
// update the appropriate tick_counter
switch (self.getCurrentStage()) {
.to_room => self.ticks_room_reached = ticks,
.room_to_waypoint => self.ticks_waypoint_reached = ticks,
.waypoint_to_destination, .room_to_destination => self.ticks_destination_reached = ticks,
.at_destination => {
c.debugPrint("job is already at final stage. Can't go next.");
unreachable;
},
}
// see if bee has to keep moving.
switch (self.getCurrentStage()) {
.to_room => {
c.debugPrint("job cannot still be in first stage");
unreachable;
},
// TODO (11 Jul 2023 sam): depending on the waypoint, we might need to do more hive things here.
.room_to_waypoint,
.waypoint_to_destination,
.room_to_destination,
=> bee.moving = Cell.slotPos(self.getCurrentTargetLocation()),
.at_destination => bee.moving = null,
}
return self;
}
/// how many ticks the job has been performed for.
/// if not at_destination, then 0
/// otherwise appropriately, ticks-dest or ticks-room
pub fn jobTicksPerformed(self: *const Self, ticks: u64) u64 {
if (self.getCurrentStage() != .at_destination) return 0;
if (self.destination != null) return ticks - self.ticks_destination_reached;
return ticks - self.ticks_room_reached;
}
};
pub const Bee = struct {
const Self = @This();
/// bee will only be moving to job room or job destination
moving: ?Vec2 = null,
job: ?Job = null,
/// slowly goes up, eventually bee will die.
age: u16 = 0,
/// health goes up and down based on food consumption
health: u16 = BEE_FULL_HEALTH,
/// rest goes up and down based on time spent on job and rest
rest: u8 = 255,
speed: f32 = 1,
position: Vec2 = HIVE_ORIGIN,
fall_speed: f32 = 0,
starved: bool = false,
address: Address = .{},
pub fn tryConsumeSignal(self: *Self, signal: *const Signal) bool {
if (signal.consumed) return false;
if (self.job != null) return false;
switch (signal.signal) {
.storage_food_available => {
if (self.health > 128) return false;
self.job = .{
.role = .eating,
.room = signal.room,
.waypoint = null,
.destination = null,
};
self.moving = Cell.slotPos(signal.room);
return true;
},
.collection_bee_required => {
self.job = .{
.role = .collection,
.room = signal.room,
.waypoint = signal.waypoint,
.destination = signal.destination,
};
self.moving = Cell.slotPos(signal.room);
return true;
},
.rest_slot_available => {
if (self.rest > 128) return false;
self.job = .{
.role = .rest,
.room = signal.room,
.waypoint = null,
.destination = null,
};
self.moving = Cell.slotPos(signal.room);
return true;
},
.babysitting_food_required => {
self.job = .{
.role = .babysitting_food,
.room = signal.room,
.waypoint = signal.waypoint,
.destination = signal.destination,
};
self.moving = Cell.slotPos(signal.room);
return true;
},
.babysitting_egg_required => {
self.job = .{
.role = .babysitting_egg,
.room = signal.room,
.waypoint = signal.waypoint,
.destination = signal.destination,
};
self.moving = Cell.slotPos(signal.room);
return true;
},
.babysitting_attention_required => {
self.job = .{
.role = .babysitting_attention,
.room = signal.room,
.waypoint = null,
.destination = signal.destination,
};
self.moving = Cell.slotPos(signal.room);
return true;
},
.building_bee_required => {
self.job = .{
.role = .building,
.room = signal.room,
.waypoint = null,
.destination = signal.destination,
};
self.moving = Cell.slotPos(signal.room);
return true;
},
.maintenance_bee_required => {
self.job = .{
.role = .maintaining,
.room = signal.room,
.waypoint = null,
.destination = signal.destination,
};
self.moving = Cell.slotPos(signal.room);
return true;
},
else => {
return false;
},
}
}
pub fn dead(self: *const Self) bool {
return self.starved or self.age >= BEE_AGE_LIMIT;
}
};
const SignalType = enum {
const Self = @This();
storage_food_available,
storage_space_available,
rest_slot_available,
room_maintenance_required,
room_construction_required,
building_bee_required,
maintenance_bee_required,
incubator_food_required,
incubator_attention_required,
incubator_egg_required,
queen_egg_available,
queen_attention_required,
babysitting_food_required,
babysitting_egg_required,
babysitting_attention_required,
collection_bee_required,
pub fn isBeeWork(self: *const Self) bool {
return switch (self.*) {
.storage_space_available,
.storage_food_available,
.rest_slot_available,
.room_maintenance_required,
.room_construction_required,
.incubator_food_required,
.incubator_attention_required,
.incubator_egg_required,
.queen_egg_available,
.queen_attention_required,
=> false,
.babysitting_food_required,
.babysitting_egg_required,
.babysitting_attention_required,
.collection_bee_required,
.building_bee_required,
.maintenance_bee_required,
=> true,
};
}
};
pub const Signal = struct {
signal: SignalType,
room: Location,
waypoint: Location,
destination: Location,
consumed: bool = false,
};
pub const Census = struct {
bees_alive: u32 = 0,
bees_age_dead: u32 = 0,
bees_food_dead: u32 = 0,
bees_not_tired: u32 = 0,
bees_tired: u32 = 0,
bees_very_tired: u32 = 0,
bees_age_young: u32 = 0,
bees_age_adult: u32 = 0,
bees_age_older: u32 = 0,
bees_starving: u32 = 0,
bees_hungry: u32 = 0,
bees_fed: u32 = 0,
bees_awaiting_job: u32 = 0,
jobs_available: u32 = 0,
};
pub const Hive = struct {
const Self = @This();
rng: std.rand.Xoshiro256,
food: usize = 0,
ticks: u64 = 1,
bees: std.ArrayList(Bee),
rooms: std.ArrayList(Room),
jobs: std.ArrayList(Job),
cells: std.ArrayList(Cell),
signals: std.ArrayList(Signal),
food_queue: std.ArrayList(usize),
work_queue: std.ArrayList(usize),
rest_queue: std.ArrayList(usize),
room_map: std.AutoHashMap(Address, usize),
/// the previous time that health rest etc was reduced
prev_bee_tick_down: u64 = 0,
prev_room_tick_down: u64 = 0,
working_bees: usize = 0,
speed_up: f32 = 0,
census: Census = .{},
allocator: std.mem.Allocator,
arena: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator, arena: std.mem.Allocator) Self {
var self = Self{
.rng = std.rand.DefaultPrng.init(42),
.bees = std.ArrayList(Bee).init(allocator),
.jobs = std.ArrayList(Job).init(allocator),
.cells = std.ArrayList(Cell).init(allocator),
.rooms = std.ArrayList(Room).initCapacity(allocator, 512) catch unreachable,
.room_map = std.AutoHashMap(Address, usize).init(allocator),
.food_queue = std.ArrayList(usize).init(allocator),
.work_queue = std.ArrayList(usize).init(allocator),
.rest_queue = std.ArrayList(usize).init(allocator),
.signals = std.ArrayList(Signal).init(allocator),
.allocator = allocator,
.arena = arena,
};
self.setupHive();
// self.debugRooms();
return self;
}
pub fn deinit(self: *Self) void {
self.bees.deinit();
self.food_queue.deinit();
self.work_queue.deinit();
self.rest_queue.deinit();
self.jobs.deinit();
self.rooms.deinit();
self.cells.deinit();
self.signals.deinit();
self.room_map.deinit();
}
// HIVEUPDATE
pub fn update(self: *Self, raw_delta_t: u64, arena: std.mem.Allocator) void {
const delta_t = @as(u64, @intFromFloat((@as(f32, @floatFromInt(raw_delta_t)) * self.speed_up)));
self.ticks += delta_t;
const f_delta_t = @as(f32, @floatFromInt(delta_t));
self.arena = arena;
if (PRINT_F_DEBUG) c.debugPrint("hive_update_0");
// iterate through all bees. move them if required. check if job is complete
for (self.bees.items, 0..) |*bee, b| {
if (bee.dead()) {
bee.position.y += bee.fall_speed;
bee.fall_speed += BEE_FALL_ACCELERATION * f_delta_t;
continue;
}
if (bee.job) |*job| {
if (bee.moving) |target_pos| {
// bee is either moving to job room or job destination
const travel = target_pos.subtract(bee.position).normalize().scale(f_delta_t * BEE_TRAVEL_SPEED * bee.speed);
// we don't want the bee to overshoot when sped up.
if (bee.position.distanceSqr(target_pos) < travel.lengthSqr()) {
bee.position = target_pos;
} else {
bee.position = bee.position.add(travel);
}
const old_stage = job.getCurrentStage();
if (target_pos.distanceSqr(bee.position) < BEE_REACH_DISTANCE_SQR) {
bee.job = job.nextStage(bee, self.ticks);
}
const new_stage = bee.job.?.getCurrentStage();
if (job.role.shouldCloseWaypointSignals() and old_stage == .room_to_waypoint and new_stage == .waypoint_to_destination) {
self.roomAt(job.waypoint.?.address).?.slots_available[job.waypoint.?.slot_index] = true;
self.roomAt(job.waypoint.?.address).?.slots_signals[job.waypoint.?.slot_index] = false;
}
} else {
// bee is not moving. bee is doing job.
if (job.jobTicksPerformed(self.ticks) > job.role.ticksLength()) {
// job is complete. mark bee as free, and the room that emplyed it also
self.markJobComplete(bee, job);
self.work_queue.append(b) catch unreachable;
}
}
}
}
if (PRINT_F_DEBUG) c.debugPrint("hive_update_1");
// iterate through all the hungry bees, and see if there is a signal to feed them
{
var fed = std.ArrayList(usize).init(self.arena);
for (self.food_queue.items, 0..) |bi, i| {
var bee = &self.bees.items[bi];
if (bee.job != null) continue;
if (bee.dead()) continue;
for (self.signals.items) |*signal| {
if (signal.consumed) continue;
if (signal.signal != .storage_food_available) continue;
if (bee.tryConsumeSignal(signal)) {
// TODO (11 Jul 2023 sam): Check distance also
fed.append(i) catch unreachable;
signal.consumed = true;
std.debug.assert(bee.job != null);
break;
}
}
}
// remove fed bees from list
if (fed.items.len > 0) {
var i: usize = fed.items.len - 1;
while (i >= 0) : (i -= 1) {
_ = self.food_queue.orderedRemove(fed.items[i]);
if (i == 0) break;
}
}
}
if (PRINT_F_DEBUG) c.debugPrint("hive_update_2");
// iterate through all the rooms that emit signals.
for (self.rooms.items) |*room| {
if (!room.isConstructed()) {
if (!room.needsMoreConstruction()) continue;
for (room.slots_available, 0..) |avail, i| {
if (!avail) continue;
if (room.slots_signals[i]) continue;
const signal = Signal{
.signal = .room_construction_required,
.room = .{
.address = room.address,
.slot_index = @as(u8, @intCast(i)),
},
.waypoint = undefined,
.destination = undefined,
};
self.signals.append(signal) catch unreachable;
room.slots_signals[i] = true;
}
break;
}
if (room.isUnusable()) continue;
switch (room.room) {
.storage => {
for (room.slots_available, 0..) |avail, i| {
if (room.slots_signals[i]) continue;
if (avail) {
// space is available
const signal = Signal{
.signal = .storage_space_available,
.room = .{
.address = room.address,
.slot_index = @as(u8, @intCast(i)),
},
.waypoint = undefined,
.destination = undefined,
};
self.signals.append(signal) catch unreachable;
room.slots_signals[i] = true;
} else {
// food is available
const signal = Signal{
.signal = .storage_food_available,
.room = .{
.address = room.address,
.slot_index = @as(u8, @intCast(i)),
},
.waypoint = undefined,
.destination = undefined,
};
self.signals.append(signal) catch unreachable;
room.slots_signals[i] = true;
}
}
},
.rest => {
for (room.slots_available, 0..) |avail, i| {
if (room.slots_signals[i]) continue;
if (avail) {
const signal = Signal{
.signal = .rest_slot_available,