-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevlife.vim
More file actions
1616 lines (1344 loc) · 56.4 KB
/
devlife.vim
File metadata and controls
1616 lines (1344 loc) · 56.4 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
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
" ====================================================================
" DevLife v1.1, Jan 2024
" ========================
"
" Small VIM game to kill time
" (c)2024 Evgueni Antonov
"
" CHANGELOG:
" 2024-01-07: v1.1 Updated for Vim 8.2, tested on Lubuntu 22.04
" 2013-05-10: v1.0 Initial version release for Vim 7.3, tested on Windows 7
"
" SETUP:
" No need to do anything. But if you want to change your savegame path,
" just edit the value on line 40: g:savegamePath = '/tmp/'
" Please make sure the path ends with a slash!
" Example: 'c:\proj\hal9000\' or if on Linux: '/blah/proj/hal9000/'
"
" RUN ON VIM 8.2:
" :call RunDevLife()
"
" RUN ON VIM 7.3:
" :source devlife.vim
" :call RunDevLife()
"
" TO RUN WITH SHORTCUTS, ADD TO VIMRC:
" :nmap \s :source devlife.vim
" :nmap \b :call RunDevLife()
" then use \s to load the source and \b to run the game.
" You may also specify your path to devlife.vim .
"
" NOTE:
" It seems Vim 8 loads automatically the plugins, so no need to load the source.
"
" HOW TO PLAY:
" Just run the game. Detailed instructions are available ingame.
" ====================================================================
" SETUP: This is the default savegame path.
" Put your savegame path in between the '' chars.
" Always put a slash/backslash in the end!
let g:savegamePath = '/tmp/'
" Game global
let g:game = {
\'nameShort' : 'DevLife',
\'nameFull' : 'DEVELOPER''s LIFE',
\'version' : '1.1',
\'authorName' : 'Evgueni Antonov',
\'authorContact' : 'https://www.linkedin.com/in/eantonoff/',
\'versionDate' : '2024-01-07',
\'createdOn' : '2013-05-10',
\'pathSavegame' : g:savegamePath,
\'whereToGet' : 'https://github.com/StrayFeral/DevLife.git',
\'license' : 'http://www.gnu.org/licenses/gpl-3.0.html'
\}
" Loaded instance check
if ( exists( 'devlife_is_loaded' ) )
call g:PPP( 'DevLife is already loaded. Loading skipped.' )
finish
endif
let g:devlife_is_loaded = 1
" ====================================================================
" MAIN RUN: Call this to start the game.
function! RunDevLife()
call GameInit()
call g:PPP( 'WELCOME!' ) | call g:PPP( '' )
call ShowIntro()
call Main()
endfunction
" ====================================================================
" MAIN FUNCTION
" Main loop
function! Main()
" Main loop
let userChoice = ''
while ( userChoice != g:commandGlobalQuit && !GotFired() )
let userChoiceDefault = ''
let userChoice = g:GetMenuChoice( userChoiceDefault )
" Processing commands
if ( userChoice ==# 'n' ) | " NEW GAME
call StartNewGame()
call BackupAge( 0 )
elseif ( userChoice ==# 'c' ) | " CLEAR SCREEN
call g:CLS()
elseif ( userChoice ==# 'p' ) | " PLAYER STATS
call PrintPlayer()
elseif ( userChoice ==# 's' ) | " SAVE GAME
if ( !GameStarted() )
call g:PPP( 'ERROR: This command is available while in a game. Please start a new game.' )
else
call SaveGame()
endif
elseif ( userChoice ==# 'l' ) | " LOAD GAME
call LoadGame()
elseif ( userChoice ==# 'h' ) | " HIGHSCORES
call ShowHighscores()
elseif ( userChoice ==# 'c' ) | " CLEAR SCREEN
call g:PPP( "cls" )
elseif ( userChoice ==# '1' ) | " WORK
if ( !GameStarted() )
call g:PPP( 'ERROR: This command is available while in a game. Please start a new game.' )
else
call DoWork()
call CalcScore()
call PrintMgrHappyMessage()
endif
elseif ( userChoice ==# '3' ) | " HAVE FUN
if ( !GameStarted() )
call g:PPP( 'ERROR: This command is available while in a game. Please start a new game.' )
else
call HaveFun()
call CalcScore()
call PrintMgrHappyMessage()
endif
elseif ( userChoice ==# '2' ) | " CREATE CODE BACKUP
if ( !GameStarted() )
call g:PPP( 'ERROR: This command is available while in a game. Please start a new game.' )
else
call CreateBackup()
"call ChangeStat( g:stats.experience, 0 )
call CalcScore()
call PrintMgrHappyMessage()
endif
elseif ( userChoice ==# '?' ) | " HELP
call ShowHelp()
elseif ( userChoice ==# ' ' ) | " BOSS KEY
call ShowBossScreen()
elseif ( userChoice ==# 'q' ) | " QUIT GAME
if ( !ConfirmQuit() )
let userChoice = ''
endif
endif
if ( GameStarted() )
call PrintGameStatus()
endif
endwhile
if ( userChoice ==# 'q' )
" END GAME
call g:PPP( '' )
call g:PPP( 'Thanks for playing ' . g:game.nameShort . '! You can now quit the editor.' )
call g:PPP( '' )
else
" Got fired
call g:PPP( '' )
call g:PPP( '[GOT FIRED] Your manager asks to see you in private. When you enter the room, you see a young girl. She presents herself as a member of the Human Resources Department and gives you an envelope. You open it and you see a letter of termination of your contract. Your manager says your services are no longer needed and asks you to leave the office immediatelly. You leave the office.' )
call g:PPP( '' )
call ShowHighscores()
call g:PPP( '' )
call g:PPP( '[GAME OVER] Your game is now over. Thank you for playing!' )
call g:PPP( '' )
endif
endfunction
" ====================================================================
" WRAPPERS
" Score calculator
function! CalcScore()
let g:player.score = g:player.fun +
\g:player.motivation +
\g:player.productivity +
\float2nr( round( g:player.mgrHappy ) ) +
\g:player.level
endfunction
" Got Lazy
function! GotLazy()
call g:PPP( '' )
call g:PPP( '[GOT LAZY] ' . GetLazyMessage() )
call g:PPP( '' )
let g:player.fun -= 5
let g:player.motivation -= 20
let g:player.productivity -= 20
let g:player.mgrHappy -= 2.5
if ( g:player.fun < 0 )
let g:player.fun = 0
endif
if ( g:player.motivation < 0 )
let g:player.motivation = 0
endif
if ( g:player.productivity < 0 )
let g:player.productivity = 0
endif
if ( g:player.mgrHappy < 0 )
let g:player.mgrHappy = 0
endif
endfunction
" Got Workaholic
function! GotWorkaholic()
call g:PPP( '' )
call g:PPP( '[GOT WORKAHOLIC] ' . GetWorkaholicMessage() )
call g:PPP( '' )
let g:player.fun -= 5
let g:player.motivation -= 20
if ( g:player.fun < 0 )
let g:player.fun = 0
endif
if ( g:player.motivation < 0 )
let g:player.motivation = 0
endif
endfunction
" Got fired?
function! GotFired()
if ( GameStarted() && g:player.mgrHappy == 0 )
return 1
endif
return 0
endfunction
" Pump fun gauge
function! PumpFunGauge()
let g:funGauge += 1
if ( TooMuchFun() )
call ReleaseFunGauge()
call ReleaseFunGauge()
call GotLazy()
endif
endfunction
" Release fun gauge
function! ReleaseFunGauge()
let g:funGauge -= 1
if ( g:funGauge < 0 )
let g:funGauge = 0
endif
endfunction
" Pump work gauge
function! PumpWorkGauge()
let g:workGauge += 1
if ( TooMuchWork() )
call ReleaseWorkGauge()
call ReleaseWorkGauge()
call GotWorkaholic()
endif
endfunction
" Release work gauge
function! ReleaseWorkGauge()
let g:workGauge -= 1
if ( g:workGauge < 0 )
let g:workGauge = 0
endif
endfunction
" Too much fun
function! TooMuchFun()
if ( g:funGauge > g:funMax )
return 1
endif
return 0
endfunction
" Too much work
function! TooMuchWork()
if ( g:workGauge > g:workMax )
return 1
endif
return 0
endfunction
" Intro
function! ShowIntro()
call g:PrintArray( g:msgIntro )
endfunction
" Help
function! ShowHelp()
call g:PPP( 'GAME KEYS' )
call g:HR()
call g:PrintHash( g:validCommands, 3 )
endfunction
" Boss key
function! ShowBossScreen()
call g:PrintArray( g:msgBossKey )
call getchar() | " Just waiting for any key...
endfunction
" Performing game setup
function! GameInit()
if !empty( $DEVLIFESAVEPATH )
g:savegamePath = $DEVLIFESAVEPATH
endif
let g:backupAge = 0
let g:funGauge = 0
let g:workGauge = 0
let g:msgHappyMaxShown = 0
" Player stats initialization
let g:player = {
\g:stats.name : '*Noname*',
\g:stats.motivation : 0,
\g:stats.productivity : 0,
\g:stats.experience : 0,
\g:stats.level : 0,
\g:stats.fun : 0,
\g:stats.company : '*No company*',
\g:stats.score : 0,
\g:stats.mgrHappy : 1,
\g:stats.title : '',
\g:stats.status : g:status.gameNone
\}
endfunction
" Print player stats
function! PrintPlayer()
call g:PPP( 'PLAYER STATS' )
call g:PPP( '---------------------------' )
let offset = 15
for key in keys( g:player )
let val = g:player[ key ]
let s = key
while strlen( s ) < offset
let s .= ' '
endwhile
if ( key == 'status' )
if ( val == g:status.gameNone )
let val = '** Game not started **'
elseif ( val == g:status.gameOver )
let val = '** GAME OVER **'
elseif ( val == g:status.gameNotSaved )
let val = '* GAME NOT SAVED'
elseif ( val == g:status.gameSaved )
let val = 'Game saved.'
endif
endif
if ( key == g:stats.mgrHappy )
let s .= printf( '%.2f', val )
else
let s .= val
endif
call g:PPP( s )
endfor
endfunction
" Game check
function! GameStarted()
if ( g:player.status == g:status.gameNone )
return 0
else
return 1
endif
endfunction
" Print game status
function! PrintGameStatus()
let status1 =
\g:msgGameStatus[ g:player.status ] .
\' ' . g:player.name .
\', ' . g:player.title .
\' at ' . g:player.company
let status2 =
\'Experience: ' . g:player.experience .
\' Productivity: ' . g:player.productivity .
\' Level: ' . g:player.level .
\' Score: ' . g:player.score
call g:PPP( '' )
call g:PPP( '___' )
call g:PPP( status1 )
call g:PPP( status2 )
call g:PPP( '' )
endfunction
" Start new game
function! StartNewGame()
" Global variables initialization
let g:backupAge = 0
let g:funGauge = 0
let g:workGauge = 0
let g:msgHappyMaxShown = 0
" Player stats initialization
let g:player = {
\g:stats.name : 'Player1',
\g:stats.motivation : 50,
\g:stats.productivity : 20,
\g:stats.experience : 0,
\g:stats.level : 0,
\g:stats.fun : 2,
\g:stats.company : CompanyNameGenerator(),
\g:stats.score : 0,
\g:stats.mgrHappy : 5.00,
\g:stats.title : g:msgProgressDescription[ 0 ][ 0 ],
\g:stats.status : g:status.gameNotSaved
\} | " status.gameNone is 'not in a game'
let g:player.name = g:GetString( '[NEW GAME] Please enter your name (Alowed: A-z0-9_): ', g:emptyValue.reject, g:player.name, g:namesMaxLen )
call g:PPP( '' )
call PrintPlayer()
call g:PPP( '' )
call PrintWelcomeMessage()
endfunction
" Save game
function! SaveGame()
let buf = [
\g:player.name,
\g:player.motivation,
\g:player.productivity,
\g:player.experience,
\g:player.level,
\g:player.fun,
\g:player.company,
\g:player.score,
\printf( '%.2f', g:player.mgrHappy ),
\g:player.title,
\g:backupAge,
\g:funGauge,
\g:workGauge,
\g:msgHappyMaxShown
\]
let fname = g:player.name . g:fext.savegame
let fullPath = g:game.pathSavegame . fname
let res = writefile( buf, fullPath )
call g:PPP( '' )
if ( res == -1 )
call g:PPP( '[SAVE GAME] ERROR SAVING GAME! Cannot save your game.' )
elseif ( res == 0 )
call g:PPP( '[SAVE GAME] Game saved (' . fname . ').' )
let g:player.status = g:status.gameSaved
endif
endfunction
" Load game
function! LoadGame()
let pName = g:GetString( '[LOAD GAME] Please enter your name (Alowed: A-z0-9_): ', g:emptyValue.reject, g:player.name, g:namesMaxLen )
call g:PPP( '' )
let fname = pName . g:fext.savegame
let fullPath = g:game.pathSavegame . fname
if ( !filereadable( fullPath ) )
call g:PPP( '[LOAD GAME] ERROR: File not found or file not readable (' . fullPath . '). Try another file or check permissions.' )
return
endif
let buf = readfile( fullPath )
if ( len( buf ) != 14 )
tHRow( 'ERROR: Savegame file is modified. Aborting.' )
endif
let g:player.name = buf[ 0 ]
let g:player.motivation = str2nr( buf[ 1 ] )
let g:player.productivity = str2nr( buf[ 2 ] )
let g:player.experience = str2nr( buf[ 3 ] )
let g:player.level = str2nr( buf[ 4 ] )
let g:player.fun = str2nr( buf[ 5 ] )
let g:player.company = buf[ 6 ]
let g:player.score = str2nr( buf[ 7 ] )
let g:player.mgrHappy = str2float( buf[ 8 ] )
let g:player.title = buf[ 9 ]
let g:player.status = g:status.gameSaved
let g:backupAge = str2nr( buf[ 10 ] )
let g:funGauge = str2nr( buf[ 11 ] )
let g:workGauge = str2nr( buf[ 12 ] )
let g:msgHappyMaxShown = str2nr( buf[ 13 ] )
call g:PPP( '' )
call g:PPP( '[LOAD GAME] Game loaded.' )
endfunction
" Highscores
function! ShowHighscores()
let wild = '*' . g:fext.savegame
let flist = split( glob( wild ) , '\n' )
let scores = []
let names = []
let titles = []
let levels = []
call g:PPP( '' )
call g:PPP( 'TOP 15 DEVELOPERS' )
call g:HR()
" Loading
for fname in flist
if ( !filereadable( fname ) )
call g:PPP( 'ERROR: File not readable (' . fname . '). Please check permissions.' )
return
endif
let buf = readfile( fname )
if ( len( buf ) != 14 )
tHRow( 'ERROR: Savegame file (' . fname . ') is modified. Aborting.' )
endif
let names = add( names, buf[ 0 ] )
let scores = add( scores, str2nr( buf[ 7 ] ) )
" let titles = add( titles, buf[ 9 ] . ' at ' . buf[ 6 ] )
let titles = add( titles, buf[ 9 ] )
let levels = add( levels, str2nr( buf[ 4 ] ) )
endfor
" Adding current player
if ( g:player.status != g:status.gameNone )
let names = add( names, g:player.name )
let levels = add( levels, g:player.level )
" g:player.company
let scores = add( scores, g:player.score )
let titles = add( titles, g:player.title )
endif
if ( !len( scores ) )
call g:PPP( '*** No developers found ***' )
call g:PPP( '' )
call g:PPP( 'Either you just installed the game or you never did a savegame or you should hire another recruitment company.' )
call g:PPP( '' )
return
endif
" Bubles!
let swapped = 1
let j = 0
if ( len( scores ) > 1 )
while ( swapped )
let swapped = 0
let j += 1
for i in range( len( scores ) - j )
if ( scores[ i ] > scores[ i + 1 ] )
let swapped = 1
let tmp = scores[ i ]
let scores[ i ] = scores[ i + 1 ]
let scores[ i + 1 ] = tmp
let tmp = names[ i ]
let names[ i ] = names[ i + 1 ]
let names[ i + 1 ] = tmp
let tmp = titles[ i ]
let titles[ i ] = titles[ i + 1 ]
let titles[ i + 1 ] = tmp
let tmp = levels[ i ]
let levels[ i ] = levels[ i + 1 ]
let levels[ i + 1 ] = tmp
endif
endfor
endwhile
endif
let j = len( scores ) - 15
if ( j < 0 )
let j = 0
endif
for i in range( len( scores ) - 1, j, -1 )
let s = names[ i ]
while ( strlen( s ) < 25 )
let s .= ' '
endwhile
let s .= titles[ i ]
while ( strlen( s ) < 60 )
let s .= ' '
endwhile
let s .= printf( '%3d', levels[ i ] )
while ( strlen( s ) < 65 )
let s .= ' '
endwhile
let s .= printf( '%6d', scores[ i ] )
call g:PPP( s )
endfor
call g:PPP( '' )
endfunction
" Prints the company's welcome message
function! PrintWelcomeMessage()
call g:PPP( 'Incoming email from the Human Resources department:' )
call g:PPP( '' )
call g:PPP( '~~~~' )
call g:PPP( 'WELCOME to ' . g:player.company . ', ' . g:player.name . '!' )
call g:PPP( '' )
call g:PPP( 'We are glad to have you as our new team member.' )
call g:PPP( 'As this is your first day, please make yourself familiar with the employee handbook, located on your desk. Inside you will find information about the company, our corporate culture and history. An HR representative would contact you shortly, to inform you about your available health benefits and other items of our employee social package.' )
call g:PPP( 'LOYALTY and DEVOTION are our corporate core values and we are determined to have all of our team members striving to pursue them.' )
call g:PPP( '' )
call g:PPP( 'Please also make sure to sign the Letter of Confidentiality, located on your desk and submit it to your line manager within an hour.' )
call g:PPP( '' )
call g:PPP( 'Enjoy your time at ' . g:player.company . ' and welcome aboard!' )
call g:PPP( '' )
call g:PPP( 'Kindly: ' . g:player.company . ' Human Resources Department' )
call g:PPP( '~~~~' )
call g:PPP( '' )
call g:PPP( 'You take a look around. Your desk contains just a phone, computer and the things the HR lady mentioned about. You finish all the boring paperwork and you are ready to work. Your line manager assigns you a new project, all your accounts are set and ready. You login to Bugzilla and discover you have been assigned a bunch of tasks to do.' )
call g:PPP( 'Well... off we go!' )
call g:PPP( '' )
call g:PPP( '[New game have just started. All menu keys are now enabled.]' )
endfunction
" Company name generation
function! CompanyNameGenerator()
let idx = Choose( len( g:msgCompanyName1 ) )
let name1 = g:msgCompanyName1[ idx ]
let idx = Choose( len( g:msgCompanyName2 ) )
let name2 = g:msgCompanyName2[ idx ]
let idx = Choose( len( g:msgCompanyTitle ) )
let title = g:msgCompanyTitle[ idx ]
let name = name1 . ' ' . name2 . ' ' . title
return name
endfunction
" Code lost event generator
function! CodeLostEvent()
let num = Choose( 20 )
if ( num % 20 == 0 )
return 1
endif
return 0
endfunction
" Create Backup
function! CreateBackup()
call BackupAge( 0 )
call g:PPP( '[BACKUP] ' . GetBackupCreatedMessage() )
endfunction
" Restore Backup
function! RestoreBackup()
call g:PPP( '' )
if ( g:backupAge <= g:backupTooOld )
" Restoring backup
call g:PPP( '[RESULT: Backup restored] ' . GetBackupRestoredMessage() )
else
" No recent backup
call g:PPP( '[RESULT: Code lost] ' . GetBackupNoMessage() )
if ( g:player.motivation > 0 )
let g:player.motivation -= 1
endif
endif
call BackupAge( -2 )
endfunction
" Work
function! DoWork()
call g:PPP( '[WORK] ' . GetWorkMessage() )
call ChangeStat( g:stats.experience, 0 )
call PumpWorkGauge()
call ReleaseFunGauge()
call BackupAge( 1 )
call CodeLostDestiny()
endfunction
" Have Fun
function! HaveFun()
call g:PPP( '[FUN] ' . GetFunMessage() )
call ChangeStat( g:stats.fun, 0 )
call PumpFunGauge()
call ReleaseWorkGauge()
call BackupAge( 1 )
call CodeLostDestiny()
endfunction
" Random message generator
function! GetRandomMessage( array )
let idx = Choose( len( a:array ) )
return a:array[ idx ]
endfunction
" Random message interval generator
function! OkToPrint()
let rnd = Choose( g:randomMessageFactorRange )
if ( rnd == g:randomMessageFactor )
return 1
endif
return 0
endfunction
" Code Lost event check/action
function! CodeLostDestiny()
if ( CodeLostEvent() )
call g:PPP( '' )
call g:PPP( '[CODE LOST] ' . GetCodeLostReason() )
call g:PPP( '' )
call RestoreBackup()
endif
endfunction
" Get Lazy message
function! GetLazyMessage()
return GetRandomMessage( g:msgLazyPerson )
endfunction
" Get Workaholic message
function! GetWorkaholicMessage()
return GetRandomMessage( g:msgWorkaholicPerson )
endfunction
" Get Code Lost reason
function! GetCodeLostReason()
return GetRandomMessage( g:msgCodeLostReasons )
endfunction
" Get Backup Created message
function! GetBackupCreatedMessage()
return GetRandomMessage( g:msgBackupCreated )
endfunction
" Get Backup Restored message
function! GetBackupRestoredMessage()
return GetRandomMessage( g:msgBackupRestored )
endfunction
" Get No Backup message
function! GetBackupNoMessage()
return GetRandomMessage( g:msgBackupNo )
endfunction
" Get Manager Happy message
function! GetManagerHappyMessage()
return g:msgManagerHappy[ float2nr( g:player.mgrHappy ) ]
endfunction
" Get work message
function! GetWorkMessage()
if ( OkToPrint() )
return GetRandomMessage( g:msgDoingWork )
endif
return ''
endfunction
" Get fun message
function! GetFunMessage()
if ( OkToPrint() )
return GetRandomMessage( g:msgHavingFun )
endif
return ''
endfunction
" Input command validator
function! CommandValid( commandChar )
let valid = 0
if ( strlen( a:commandChar ) > 1 )
tHRow( "CommandValid( '" . a:commandChar . "' ): ERROR: Argument length is more than 1. Not a single char." )
endif
for okCommand in keys( g:validCommands )
if ( a:commandChar ==# okCommand )
let valid = 1
endif
endfor
return valid
endfunction
" Sets/resets the backup age
function! BackupAge( factor )
if ( a:factor )
let g:backupAge += 1 | " Increase
elseif ( a:factor == 0 )
let g:backupAge = 0 | " Reset
else
let g:backupAge -= a:factor | " Decrease
endif
endfunction
" Backup age check
function! BackupTooOld()
if ( g:backupAge > g:backupTooOld )
return 1
endif
return 0
endfunction
" Level Up check and action
function! LevelUp()
if ( g:player.level > len( g:msgProgressDescription ) -1 )
return
endif
if ( g:player.experience >= keys( g:msgProgressDescription[ g:player.level ] )[ -1 ] )
let g:player.level += 1
let g:player.experience = 0
let g:player.title = g:msgProgressDescription[ g:player.level ][ 0 ]
else
for [ pts, msg ] in items( g:msgProgressDescription[ g:player.level ] )
if ( g:player.experience >= pts )
let g:player.title = msg
endif
endfor
endif
endfunction
" Change stat
" step is not mandatory
function! ChangeStat( stat, step )
if ( !has_key( g:stats, a:stat ) )
tHRow( 'ChangeStat(' . a:stat . ', ' . a:step . '): ERROR: stat does not exist.' )
endif
let min = 0
let thisStep = a:step
if ( a:step == min )
let thisStep = g:statRange[ a:stat ][ 'step' ]
endif
let g:player[ a:stat ] += thisStep
if ( g:player[ a:stat ] < min )
let g:player[ a:stat ] = min
endif
if ( g:statRange[ a:stat ][ 'max' ] && g:player[ a:stat ] > g:statRange[ a:stat ][ 'max' ] )
let g:player[ a:stat ] = g:statRange[ a:stat ][ 'max' ]
endif
if ( has_key( g:statRelations, a:stat ) )
let nextStep = 0
if ( a:stat ==# g:stats.experience )
call LevelUp()
elseif ( ( a:stat ==# g:stats.experience ) || ( a:stat ==# g:stats.motivation ) )
let nextStep = g:stats.motivation + ( g:stats.experience * ( g:stats.level + 1 ) )
else
let nextStep = g:statRange[ g:statRelations[ a:stat ] ][ 'step' ]
endif
call ChangeStat( g:statRelations[ a:stat ], nextStep )
endif
endfunction
function! PrintMgrHappyMessage()
if ( ( float2nr( g:player.mgrHappy * 100 ) % 100 == 0 ) && !g:msgHappyMaxShown )
call g:PPP( '' )
call g:PPP( '[MANAGER] ' . GetManagerHappyMessage() )
" Bugfix with the max value
if ( g:player.mgrHappy == 10.00 )
let g:msgHappyMaxShown = 1
else
g:msgHappyMaxShown = 0
endif
endif
endfunction
" Confirm Quit
function! ConfirmQuit()
call g:PPP( '' )
call g:PPP( '[QUIT GAME] Are you sure? Press "y" or "Y" to QUIT, any other key to go back to game...' )
let userChoice = toupper( nr2char( getchar() ) )
if ( userChoice ==? 'Y' )
return 1
endif
return 0
endfunction
" ====================================================================
" TOOLS
" Horizontal ruler
function! g:HR()
call g:PPP( '--------------------' )
endfunction
" Menu input
function! g:GetMenuChoice( defaultChoice )
let userChoice = ''
let validCommand = 0
while ( !validCommand )
let userChoice = nr2char( getchar() )
let validCommand = CommandValid( userChoice )
if ( empty( userChoice ) )
userChoice = a:defaultChoice
endif
if ( !validCommand )
call g:PPP( 'ERROR: Invalid key ('.userChoice.'). Valid keys: ' . join( keys( g:validCommands ) ) )
endif
endwhile
return userChoice
endfunction
" Function to get a string of characters in the class [A-z0-9_]
" Control keys accepted: Enter and Backspace
function! g:GetString( promptS, allowEmpty, valueDefault, maxLength )
let value = a:valueDefault
let lineS = a:promptS
let cursorX = '_'
let maxLen = 256
if ( a:maxLength =~ '\v^[0-9]+$' )
let maxLen = a:maxLength
endif
call append( line( "$" ), lineS . value . cursorX )
call g:SCROLL()
let keyPress = ''
while ( 1 )
let keyPress = getchar()
if ( keyPress == '13' )
if ( !empty( value ) )
return value
elseif ( a:allowEmpty )
return value
endif
elseif ( keyPress == "\<BS>" && strlen( value ) > 0 )
let value = value[:-2]
else
let keyPress = nr2char( keyPress )