-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.lua
1151 lines (1151 loc) · 25.7 KB
/
source.lua
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
tostring = function(value)
return value
end
cloneTable = function(source)
local clone = { }
for key, value in pairs(source) do
clone[key] = value
end
return clone
end
write = function(text, x, y, width, center, alignBottom)
local maxCharactersPerLine = flr(width / 4)
local remainingText = text
local lines = { }
while remainingText do
if #remainingText > maxCharactersPerLine then
local farthestSpaceIndex = maxCharactersPerLine
if #remainingText < 2 * maxCharactersPerLine then
farthestSpaceIndex = #remainingText / 2
end
while farthestSpaceIndex > 0 and sub(remainingText, farthestSpaceIndex, farthestSpaceIndex) ~= " " do
farthestSpaceIndex = farthestSpaceIndex - 1
end
if farthestSpaceIndex > 0 then
local line = sub(remainingText, 0, farthestSpaceIndex)
remainingText = sub(remainingText, farthestSpaceIndex + 1)
add(lines, line)
end
else
add(lines, remainingText)
remainingText = nil
end
end
local top = y
if alignBottom then
top = top - #lines * 6
end
for _index_0 = 1, #lines do
local line = lines[_index_0]
local left = x
if center then
local lineWidth = #line * 4
left = x + flr((width - lineWidth) / 2)
end
print(line, left, top)
top = top + 6
end
end
do
local _class_0
local _base_0 = {
reset = function(self)
self.quantity = 0
self.introduced = false
self.available = self.initiallyAvailable
end,
introduce = function(self)
add(gameState.introducedResources, self)
self.introduced = true
self.available = true
end,
draw = function(self, x, y, nameColor, change, maxLength, alignRight, alignCenter)
if change == nil then
change = 0
end
maxLength = maxLength or #self.name
if change ~= 0 then
maxLength = maxLength - 2
end
if abs(change) > 9 then
maxLength = maxLength - 1
end
local name = sub(self.name, 1, maxLength)
local left = x
local width = 8 + #name * 4
if alignRight then
left = left - width
end
if alignCenter then
left = left - (width / 2)
end
spr(self.index, left, y)
color(nameColor)
print(name, left + 8, y + 1)
if change ~= 0 then
color((function()
if change > 0 then
return 11
else
return 8
end
end)())
return print(tostring((function()
if change > 0 then
return "+"
else
return ""
end
end)()) .. tostring(change), left + 8 + 4 * #name, y + 1)
end
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, options)
for key, value in pairs(options) do
self[key] = value
end
Resources[self.id] = self
self.name = self.name or self.id
if self.initiallyAvailable == nil then
self.initiallyAvailable = self.consumption ~= nil
end
return self:reset()
end,
__base = _base_0,
__name = "Resource"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Resource = _class_0
end
Resources = { }
Resource({
index = 0,
id = 'sleep',
hourCost = 3
})
Resource({
index = 1,
id = 'body',
hourCost = 2,
consumption = {
sleep = 1
}
})
Resource({
index = 2,
id = 'mind',
hourCost = 2,
consumption = {
sleep = 1
}
})
Resource({
index = 3,
id = 'work',
hourCost = 2,
initiallyAvailable = false,
consumption = {
sleep = 1
}
})
Resource({
index = 4,
id = 'people',
hourCost = 1
})
Resource({
index = 5,
id = 'emotions',
hourCost = 1
})
Resource({
index = 6,
id = 'travel',
hourCost = 3
})
Resource({
index = 7,
id = 'school',
hourCost = 2
})
Resource({
index = 8,
id = 'computer',
hourCost = 1
})
Resource({
index = 9,
id = 'exercise',
hourCost = 2,
consumption = {
body = 1
}
})
Resource({
index = 10,
id = 'sport',
hourCost = 1,
consumption = {
exercise = 1,
play = 1
}
})
Resource({
index = 11,
id = 'athlete',
hourCost = 1,
consumption = {
sport = 1,
work = 1
}
})
Resource({
index = 12,
id = 'play',
hourCost = 1,
consumption = {
friends = 1,
mind = 1
}
})
Resource({
index = 13,
id = 'videoGames',
name = 'video games',
hourCost = 1,
consumption = {
computer = 1,
play = 1
}
})
Resource({
index = 14,
id = 'esports',
hourCost = 1,
consumption = {
videoGames = 1,
work = 1
}
})
Resource({
index = 15,
id = 'friends',
hourCost = 1,
consumption = {
people = 1
}
})
Resource({
index = 16,
id = 'love',
hourCost = 1,
consumption = {
friends = 1,
emotions = 1
}
})
Resource({
index = 17,
id = 'marriage',
hourCost = 1,
initiallyAvailable = false,
consumption = {
love = 1,
work = 1
}
})
Resource({
index = 18,
id = 'kids',
hourCost = 3,
initiallyAvailable = false,
consumption = {
love = 1,
people = 1
}
})
Resource({
index = 19,
id = 'astronaut',
hourCost = 10,
consumption = {
travel = 1,
science = 5
}
})
Resource({
index = 20,
id = 'driver',
hourCost = 1,
consumption = {
travel = 1,
practice = 1
}
})
Resource({
index = 21,
id = 'theory',
hourCost = 1,
consumption = {
mind = 1,
school = 1
}
})
Resource({
index = 22,
id = 'science',
hourCost = 1,
consumption = {
theory = 1,
work = 1
}
})
Resource({
index = 23,
id = 'programing',
hourCost = 1,
consumption = {
computer = 1,
work = 1
}
})
Resource({
index = 24,
id = 'practice',
hourCost = 1,
consumption = {
school = 1,
body = 1
}
})
Resource({
index = 25,
id = 'service',
hourCost = 1,
consumption = {
practice = 1,
work = 1
}
})
Resource({
index = 26,
id = 'doctor',
hourCost = 1,
consumption = {
theory = 1,
service = 1
}
})
Resource({
index = 27,
id = 'business',
hourCost = 1,
consumption = {
school = 1,
work = 1
}
})
Resource({
index = 28,
id = 'startup',
hourCost = 1,
consumption = {
business = 1,
computer = 1
}
})
Resource({
index = 29,
id = 'art',
hourCost = 1,
consumption = {
emotions = 1,
work = 1
}
})
Resource({
index = 30,
id = 'dance',
hourCost = 1,
consumption = {
emotions = 1,
exercise = 1
}
})
Resource({
index = 31,
id = 'acting',
hourCost = 1,
consumption = {
art = 1,
body = 1
}
})
Resource({
index = 32,
id = 'music',
hourCost = 1,
consumption = {
emotions = 1,
play = 1
}
})
Resource({
index = 33,
id = 'band',
hourCost = 1,
consumption = {
music = 1,
friends = 1
}
})
Resource({
index = 34,
id = 'graphicDesign',
name = 'graphic design',
hourCost = 1,
consumption = {
art = 1,
computer = 1
}
})
Resource({
index = 35,
id = 'therapy',
hourCost = 1,
consumption = {
emotions = 1,
theory = 1
}
})
Resource({
index = 36,
id = 'politics',
hourCost = 1,
consumption = {
people = 1,
theory = 1
}
})
Resource({
index = 37,
id = 'socialMedia',
name = 'social media',
hourCost = 1,
consumption = {
people = 1,
computer = 1
}
})
Resource({
index = 38,
id = 'influencer',
hourCost = 1,
consumption = {
socialMedia = 1,
work = 1
}
})
do
local _class_0
local _base_0 = { }
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, options)
for key, value in pairs(options) do
self[key] = value
end
return add(Events, self)
end,
__base = _base_0,
__name = "Event"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Event = _class_0
end
Events = { }
Event({
description = 'you are born. all you can do is sleep.',
condition = function()
return gameState.year == 0
end,
provide = function()
return {
Resources.sleep
}
end
})
Event({
description = 'trade time to develop your potential.',
condition = function()
return gameState.year == 1
end
})
Event({
description = 'you have a loving family.',
condition = function()
return gameState.year == 2 and rnd(2) < 1
end,
provide = function()
return {
Resources.people
}
end
})
Event({
description = 'you get a sibling.',
condition = function()
if Resources.people.introduced then
return
end
return gameState.year > 2 and gameState.year < 6 and rnd(5) < 1
end,
provide = function()
return {
Resources.people
}
end
})
Event({
description = 'you discover you have emotions.',
condition = function()
return gameState.year == 4
end,
provide = function()
return {
Resources.emotions
}
end
})
Event({
description = 'you start going to school.',
condition = function()
return gameState.year == 6
end,
provide = function()
return {
Resources.school
}
end
})
Event({
description = 'you meet your classmates.',
condition = function()
if Resources.people.introduced then
return
end
return gameState.year == 6
end,
provide = function()
return {
Resources.people
}
end
})
Event({
description = 'you are old enough to work.',
condition = function()
return gameState.year == 13
end,
provide = function()
return {
Resources.work
}
end
})
Event({
description = 'your parents buy you a computer.',
condition = function()
if Resources.computer.introduced then
return
end
return gameState.year > rnd(30)
end,
provide = function()
return {
Resources.computer
}
end
})
Event({
description = 'you buy yourself a computer.',
condition = function()
if Resources.computer.introduced then
return
end
return gameState.year - 10 > rnd(10)
end,
provide = function()
return {
Resources.computer
}
end
})
Event({
description = 'you get a passport.',
condition = function()
if Resources.travel.introduced then
return
end
return gameState.year > rnd(20)
end,
provide = function()
return {
Resources.travel
}
end
})
Event({
description = 'you become of age.',
condition = function()
return gameState.year == 18
end,
enable = function()
return {
Resources.marriage,
Resources.kids
}
end
})
do
local _class_0
local _base_0 = {
start = function(self) end,
update = function(self)
if btnp() > 0 then
return startTurnState(TurnStates.Story)
end
end,
draw = function(self)
return print("year " .. tostring(gameState.year), 52, 62)
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self) end,
__base = _base_0,
__name = "StartTurn"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
StartTurn = _class_0
end
do
local _class_0
local _base_0 = {
reset = function(self) end,
start = function(self)
self.availableResources = cloneTable(gameState.introducedResources)
self.hours = {
sleep = 24
}
self:updateChanges()
self.selectedIndex = 1
end,
update = function(self)
if self.selectedIndex > 0 then
local selectedResource = self.availableResources[self.selectedIndex]
local oldHours = cloneTable(self.hours)
if btnp(0) and self.hours[selectedResource.id] and self.hours[selectedResource.id] > 0 then
self.hours[selectedResource.id] = self.hours[selectedResource.id] - 1
self.hours.sleep = self.hours.sleep + 1
self:updateChanges()
end
if btnp(1) and self.hours.sleep > 0 then
self.hours[selectedResource.id] = (self.hours[selectedResource.id] or 0) + 1
self.hours.sleep = self.hours.sleep - 1
self:updateChanges()
end
if not (self.validAllocation) then
self.hours = oldHours
self:updateChanges()
end
end
local topIndex
if self.validAllocation then
topIndex = 0
else
topIndex = 1
end
if btnp(2) then
self.selectedIndex = self.selectedIndex - 1
if self.selectedIndex < topIndex then
self.selectedIndex = #self.availableResources
end
end
if btnp(3) then
self.selectedIndex = self.selectedIndex + 1
if self.selectedIndex > #self.availableResources then
self.selectedIndex = topIndex
end
end
if (btnp(4) or btnp(5)) and self.selectedIndex == 0 then
return startTurnState(TurnStates.Outcomes)
end
end,
updateChanges = function(self)
self.changes = { }
for resourceId, hours in pairs(self.hours) do
local resource = Resources[resourceId]
local production = flr(hours / resource.hourCost)
self.changes[resourceId] = (self.changes[resourceId] or 0) + production
if resource.consumption then
for requiredResourceId, cost in pairs(resource.consumption) do
self.changes[requiredResourceId] = (self.changes[requiredResourceId] or 0) - production * cost
end
end
end
for resourceId, change in pairs(self.changes) do
if change > 0 then
self.changes[resourceId] = flr(change)
else
self.changes[resourceId] = ceil(change)
end
end
self.validAllocation = true
for resourceId, resource in pairs(Resources) do
if resource.quantity + (self.changes[resource.id] or 0) < 0 then
self.validAllocation = false
end
end
end,
draw = function(self)
local offset
if self.selectedIndex > 0 then
offset = self.selectedIndex
else
offset = #self.availableResources + 1
end
offset = max(7, min(#self.availableResources - 5, offset))
local resourcesTop = 57 - offset * 8
local resourcesLeft = 9
local iconLeft = resourcesLeft
local barLeft = iconLeft + 8
for index, resource in pairs(self.availableResources) do
local top = resourcesTop + 8 * (index - 1)
if index == 1 then
top = 1
end
if index == 2 then
clip(0, 8, 128, 128)
end
if index == self.selectedIndex then
color(1)
rectfill(0, top - 1, 128, top + 7)
end
local invalid = resource.quantity + (self.changes[resource.id] or 0) < 0
color((function()
if invalid then
return 8
else
return 7
end
end)())
local quantity = min(99, flr(resource.quantity))
if quantity > 0 or invalid then
local quantityLeft = iconLeft - 4
if quantity > 9 then
quantityLeft = quantityLeft - 4
end
print(quantity, quantityLeft, top + 1)
end
spr(resource.index, iconLeft, top)
local hours = self.hours[resource.id] or 0
local barRight = barLeft + hours * 3
color((function()
if self.selectedIndex == index then
return 10
else
return 7
end
end)())
rectfill(barLeft, top + 1, barRight, top + 5)
color(7)
if hours > 0 then
print(hours .. "h", barRight + 2, top + 1)
end
local change = self.changes[resource.id] or 0
if change ~= 0 then
local changeLeft = barRight + 2
if hours > 0 then
changeLeft = changeLeft + 8
end
if hours > 9 then
changeLeft = changeLeft + 4
end
color((function()
if change > 0 then
return 11
else
return 8
end
end)())
print(tostring((function()
if change > 0 then
return "+"
else
return ""
end
end)()) .. tostring(change), changeLeft, top + 1)
end
end
clip()
if self.validAllocation then
local confirmTop = resourcesTop + 2 + 8 * #self.availableResources
if self.selectedIndex == 0 then
color(1)
rectfill(0, confirmTop - 2, 128, confirmTop + 6)
end
color((function()
if self.selectedIndex == 0 then
return 10
else
return 7
end
end)())
print("confirm", resourcesLeft, confirmTop)
end
if self.selectedIndex > 0 then
local infoTop = 105
color(0)
rectfill(0, infoTop, 128, 128)
color(7)
line(0, infoTop, 128, infoTop)
local selectedResource = self.availableResources[self.selectedIndex]
local change = self.changes[selectedResource.id] or 0
local invalid = selectedResource.quantity + change < 0
local nameLeft = 1
local nameTop = infoTop + 4
selectedResource:draw(nameLeft, nameTop, 7, 0)
local quantityLeft = nameLeft + 8
local quantityTop = nameTop + 8
color(7)
print(selectedResource.quantity, quantityLeft, quantityTop)
if change ~= 0 then
local changeLeft = quantityLeft + 4
if selectedResource.quantity > 9 then
changeLeft = changeLeft + 4
end
local changeTop = quantityTop
color((function()
if change > 0 then
return 11
else
return 8
end
end)())
print(tostring((function()
if change > 0 then
return "+"
else
return ""
end
end)()) .. tostring(change), changeLeft, changeTop)
end
if selectedResource.consumption then
local requiredResourceLeft = 72
local requiredResourceTop = nameTop
for requiredResourceId, cost in pairs(selectedResource.consumption) do
local time = self.hours[requiredResourceId] or 0
local consumption = -time * cost
local requiredResource = Resources[requiredResourceId]
requiredResource:draw(requiredResourceLeft, requiredResourceTop, 8, consumption, 12)
requiredResourceTop = requiredResourceTop + 8
end
end
end
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self)
return self:reset()
end,
__base = _base_0,
__name = "Allocation"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Allocation = _class_0
end
do
local _class_0
local _base_0 = {
start = function(self)
for resourceId, change in pairs(TurnStates.Allocation.changes) do
Resources[resourceId].quantity = Resources[resourceId].quantity + change
end
self.newResources = { }
for resourceId, resource in pairs(Resources) do
local _continue_0 = false
repeat
if not (resource.consumption and not resource.introduced and resource.available) then
_continue_0 = true
break
end
local requiredResourcesAvailable = true
for requiredResourceId, cost in pairs(resource.consumption) do
if not (Resources[requiredResourceId]) then
printh("Missing " .. requiredResourceId)
end
if not (Resources[requiredResourceId].quantity > 0) then
requiredResourcesAvailable = false
break
end
end
if not (requiredResourcesAvailable) then
_continue_0 = true
break
end
add(self.newResources, resource)
_continue_0 = true
until true
if not _continue_0 then
break
end
end
do
local _accum_0 = { }
local _len_0 = 1
local _list_0 = gameState.introducedResources
for _index_0 = 1, #_list_0 do
local resource = _list_0[_index_0]
if resource.quantity > 0 then
_accum_0[_len_0] = resource
_len_0 = _len_0 + 1
end
end
self.displayedResource = _accum_0
end
self.displayedNewResource = nil
end,
update = function(self)
if btnp() > 0 then
if #self.newResources > 0 then
self.displayedNewResource = self.newResources[1]
del(self.newResources, self.displayedNewResource)
return self.displayedNewResource:introduce()
else
return startTurnState(TurnStates.EndTurn)
end
end
end,
draw = function(self)
color(7)
if self.displayedNewResource then
print("-", 58, 62)
print("-", 61, 62)
print(">", 62, 62)
self.displayedNewResource:draw(70, 61, 7)
local count = 0
for resourceId, cost in pairs(self.displayedNewResource.consumption) do
count = count + 1
end
local index = 0
local previousWidth
for resourceId, cost in pairs(self.displayedNewResource.consumption) do
local top = 61 - (count - 1) * 10 + index * 20
local requiredResource = Resources[resourceId]
requiredResource:draw(53, top, 7, 0, nil, true)
if index > 0 then
print("+", 51 - previousWidth / 2, 62)
end
previousWidth = #requiredResource.name * 4
index = index + 1
end
else
color(7)
local top = 0
local left = 9
for index, resource in pairs(self.displayedResource) do
local quantityLeft = left - 4
if resource.quantity > 9 then
quantityLeft = quantityLeft - 4
end
print(resource.quantity, quantityLeft, top + 1)
local maxLength
if index + 16 < #self.displayedResource then
maxLength = 11
else
maxLength = nil
end
resource:draw(left, top, 7, nil, maxLength)