-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRubble.pl
More file actions
1753 lines (1576 loc) · 61.4 KB
/
Copy pathRubble.pl
File metadata and controls
1753 lines (1576 loc) · 61.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
#!/usr/bin/perl
# Rubble is very simple game whose object is to dominate your neighbor's
# kingdom. You may do this by destroying their castle, assassinating
# their king, or destroying their army completely.
use strict;
use warnings;
use Time::HiRes;
my $InitialMenuInput = 0;
my $MainMenuResult = 0;
my $InputIsValid = 0;
my $Player1_Name = "";
my $Player2_Name = "";
my $Player1_Type = "";
my $Player2_Type = "";
my $Player1_AIType = "";
my $Player2_AIType = "";
my $RoundCounter = 1; #initializes the RoundCounter variable to 1
my $RandMessage = "Test";
my $val = 1;
my $ResourceTotal = 0;
my $round_cnt = 0;
my $cnt3 = 0;
my $SleepTime = 5; #Sleep time between round (in seconds)
my $Victor = ""; #Variable to hold the name of the victor.
my $CompDefaultAction = 1;
my $Message = "";
my $UserIntInput = 0; #Stores the integer interpretation of the users's input
my $CombatIntensity = 1.0; #Combat Intensity value
my $CombatLength = 1.0; #Combat Length value
my $SuccessfulSub = 0; # Checks the return value of subs to see if they were
# executed successful
my $Trashvar = 0; # Stores garbage input from user in <pause> situations.
my $Player2Win = 0; # Did Player 2 win?
my $Player1Win = 0; # Did player 1 win?
my $TA_AttackerOriginalStrength = 0;
my $dice2 = 0;
my $Attacker_RoundSoldLoss = 0;
my $Attacker_RoundHeroLoss = 0;
my $Defender_RoundSoldLoss = 0;
my $Defender_RoundHeroLoss = 0;
my $Loss_Val = 0;
# Initialize combat variables
my $TA_AttackerLosses = 0;
my $TA_DefenderLosses = 0;
my $TA_AttackerHLosses = 0;
my $TA_DefenderHLosses = 0;
my $TA_AttackerSoldiers = 0;
my $TA_AttackerUnits = 0;
my $TA_AttackerHeroes = 0;
my $TA_DefenderSoldiers = 0;
my $TA_DefenderUnits = 0;
my $TA_DefenderHeroes = 0;
my $TA_DefenderWalls = 0;
my $TA_AttackerTotal = 0;
my $TA_DefenderTotal = 0;
# Array of AI First Names
my @AI_FirstNames = ("Blue", "Happy", "Quick", "Steely", "Random", "Quirky", "Smelly", "Red", "Heavy", "Sad", "Dangerous", "Timid", "Flawed", "Worried", "Open", "Big", "Strange", "Slow", "Tiny", "Flat", "Long", "Short", "Svelt", "Frumpy", "Scared", "Terrible", "Shiny", "Angry", "Mad", "Serious", "Sleepy", "Worried", "Green", "Yellow", "Purple", "Tired", "Serious", "Slippery", "Honest", "Flabby");
# Array of AI Middle Names
my @AI_MiddleNames = ("John", "Lisa", "Paul", "Emily", "Isaac", "Mary", "Stephen", "Helene", "Marcelus", "Flavia", "Quintus", "Cornelia", "Gaius", "Caesarea", "Edgar", "Maria", "Horrace", "Madeline", "Omar", "Christina", "Ahmed", "Phyllis", "Mohammed", "Jane", "Howard", "Amy", "Tom", "Wendy", "Mick", "Monica", "Lance", "Bethel", "Mikhail", "Eve", "Oxbow", "Mulva", "Chun", "Ingrid", "Gabic", "Elsah", "Fallow", "Aria", "Wilbur", "Cat", "Elmo", "Oriol", "Burt", "Vicky", "Walt", "Selma", "Ibrahim", "Sara", "Slayer", "Betty", "John", "Dagny", "Prince", "Edith", "Warren", "Carrol");
# Array of AI Last Names
my @AI_LastNames = ("Smith", "Michaels", "Wong", "Capulet", "Montague", "Pierre", "Dove", "Winkle", "Boyd", "Floyd", "Walpole", "Betard", "Shackleford", "Wallace", "McCloud", "Bertrand", "Sagan", "Curie", "Feynman", "Newton", "Stockton", "Reardon", "Galt", "Taggert");
# Array of weather types
my @WeatherTypes = ("Rainy", "Rainy", "Rainy", "Hot", "Hot", "Cold", "Freezing", "Mild", "Mild", "Mild", "Mild", "Windy", "Windy", "Mild", "Rainy", "Foggy", "Foggy", "Cool", "Mild");
my $WeatherToday = "";
# Array for storing possible number of rounds of combat in troop attack
my @PossibleRounds = (1,1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,6,7,7,8,9,10,11,12,15);
# Hash for storing Computer player's assets
my %Player2_Assets = (
soldiers => 0,
fortifications => 0,
assassins => 0,
guards => 0,
engineers => 0,
heroes => 0,
catapults => 0,
food => 0,
peasants => 0,
gold => 0
);
# Rubble values (Rubble is a liability, rather than an asset, and is tracked
# independently).
my $Player2_Rubble = 0;
my $Player1_Rubble = 0;
# Hash for storing player assets
my %Player1_Assets = %Player2_Assets;
# Hash for storing cost/value of each resource type
my %AssetCost = (
soldiers => 20,
fortifications => 5,
assassins => 30000,
guards => 1000,
engineers => 500,
heroes => 5000,
catapults => 25000,
food => .1,
peasants => 10,
gold => 1
);
# Hashes representing minimum percentages of resources
my %Player1_Share = (
soldiers => 0.154,
fortifications => 0.17,
assassins => 0.0,
guards => 0.05,
engineers => 0.008,
heroes => 0.008,
catapults => 0.01,
food => 0.025,
peasants => 0.501,
gold => 0.077
);
my %Player2_Share = %Player1_Share;
my %Player2_ExtraShare = (
soldiers => 0.0,
fortifications => 0.0,
assassins => 0.0,
guards => 0.0,
engineers => 0.0,
heroes => 0.0,
catapults => 0.0,
food => 0.0,
peasants => 0.0,
gold => 0.0);
my %Player1_ExtraShare = %Player2_ExtraShare;
# Variables for keeping score
my $Player2_Score = 0;
my $Player1_Score = 0;
# Variable for tracking whose turn it is
my $TurnTracker = "Player1";
my $Attacker = "Player1";
my $Defender = "Player2";
# Hash for storing Player Action subs
my %ActionSubs = (
1 => "TroopAttack",
2 => "TroopRaid",
3 => "KillCastle",
4 => "PassTurn",
5 => "GetSoldiers",
6 => "GetPeasants",
7 => "GetFood",
8 => "GetEngineers",
9 => "GetGuards",
10 => "GetAssassins",
11 => "GetFort",
12 => "ClearRubble",
13 => "SendAssassins",
14 => "GetCatapults"
);
# Array for storing jingoistic exhortations
my @action_to_take = (
"Your army awaits your orders, milord!",
"Sire?",
"Let us bring this pretender to his knees!",
"Our catapults are loaded and ready, your majesty!",
"What are your orders, sire?",
"It is a good day to die!",
"Let us drown the fields in blood!",
"The omens are most ominous, your majesty.",
"Let us use all the means at our disposal, sire.",
"Mind the defenses, milord.",
"Our armies grow bored, your excellency.",
"The men are restless, sire.",
"How shall we smite our foe, King?",
"The omens are full of portents, my liege.",
"An old man claims to have seen two snakes devouring each other, lord.",
"Let us lead from behind, O king!",
"Our spears are sharpened, and our soldiers hungry.",
"What better day to finally end this conflict!",
"That coward can't hide behind his walls forever!",
"The men grow restless as this war drags on...",
"We must send this wretch into the abyss of history!",
"Shall we attack directly... or use the side door?",
"Do not forget to consider all options, your majesty."
);
my $menu_item = "";
# Resource_Initializer
#
# This sub creates a random value for the total resources each player will be
# alotted. Each player gets the same total value of resources, however which
# resources each player receives is random, with certain guaranteed minimums.
sub Resource_Initializer {
my $RandResourceTotalVal = 0;
# This loop generates a random total value for the resources
for ($cnt3 = 15; $cnt3 >=1; $cnt3--) {
$RandResourceTotalVal = $RandResourceTotalVal + (int(rand(300)) * int(rand(300)));
}
# Generate random values to divy remainder of resources for computer
# and player
my $Player2_ExtraTotal = 0;
my $Player1_ExtraTotal = 0;
for my $item (keys(%Player2_ExtraShare)) {
$Player2_ExtraShare{$item} = rand(100);
$Player1_ExtraShare{$item} = rand(100);
}
for my $item (keys(%Player2_ExtraShare)) {
$Player2_ExtraTotal+=$Player2_ExtraShare{$item};
$Player1_ExtraTotal+=$Player1_ExtraShare{$item};
}
for my $item (keys(%Player2_ExtraShare)) {
$Player2_ExtraShare{$item} = $Player2_ExtraShare{$item}/($Player2_ExtraTotal /.25);
$Player1_ExtraShare{$item} = $Player1_ExtraShare{$item}/($Player1_ExtraTotal/.25);
}
# Add (randomized) ExtraShare to Share hash
for my $item (keys(%Player1_Share)) {
$Player1_Share{$item}+=$Player1_ExtraShare{$item};
$Player2_Share{$item}+=$Player2_ExtraShare{$item};
}
# Convert: Share * Total Resource Value / item cost
# This loop converts the value of the items in the _Share hashes to
# actual assets in the _Assets hashes.
for my $item (keys(%Player2_Assets)) {
$Player2_Assets{$item} = int($Player2_Share{$item} * $RandResourceTotalVal / $AssetCost{$item});
$Player1_Assets{$item} = int($Player1_Share{$item} * $RandResourceTotalVal / $AssetCost{$item});
}
$Player2_Assets{peasants}=int($Player2_Assets{peasants}/2);
$Player1_Assets{peasants}=int($Player1_Assets{peasants}/2);
}
# TurnMenu
#
# This sub redraws the status screen at the beginning of each turn for the player.
sub TurnMenu {
#system("cls");
print "\n\n\n ** Round Number: ", $RoundCounter, " ** \n";
print "The weather today: ", $WeatherToday, "\n";
print $Message, "\n";
my $TM_TempString = "\$".$Attacker."_Name;";
print "King ", eval $TM_TempString, "'s Turn\n";
printf "%-18s%-30s%-30s\n", "Unit ", "King ".$Player1_Name, "King ".$Player2_Name, "\n";
print "----------------------------------------------------------------------\n";
printf "%-18s%-30s%-30s\n", "Soldiers: ", $Player1_Assets{soldiers},$Player2_Assets{soldiers};
printf "%-18s%-30s%-30s\n", "Fortifications: ", $Player1_Assets{fortifications}, $Player2_Assets{fortifications};
printf "%-18s%-30s%-30s\n", "Rubble: ", $Player1_Rubble, $Player2_Rubble;
printf "%-18s%-30s%-30s\n", "Assassins: ", $Player1_Assets{assassins}, $Player2_Assets{assassins};
printf "%-18s%-30s%-30s\n", "Guards: ", $Player1_Assets{guards}, $Player2_Assets{guards};
printf "%-18s%-30s%-30s\n", "Engineers: ", $Player1_Assets{engineers}, $Player2_Assets{engineers};
printf "%-18s%-30s%-30s\n", "Heroes: ", $Player1_Assets{heroes},$Player2_Assets{heroes};
printf "%-18s%-30s%-30s\n", "Catapults: ", $Player1_Assets{catapults}, $Player2_Assets{catapults};
printf "%-18s%-30s%-30s\n", "Food: ", $Player1_Assets{food}, $Player2_Assets{food};
printf "%-18s%-30.2f%-30.2f\n", "Food (Turns): ", FoodCalc("Player1"), FoodCalc("Player2");
printf "%-18s%-30s%-30s\n", "Peasants: ", $Player1_Assets{peasants}, $Player2_Assets{peasants};
printf "%-18s%-30s%-30s\n", "Gold: ", $Player1_Assets{gold}, $Player2_Assets{gold};
ScoreCalc();
print "----------------------------------------------------------------------\n";
# printf "%-20s%-20s%-20s\n", "Scores: ", $Player1_Score, $Player2_Score;
print "(1) Attack, (2) Raid, (3) Fire at Castle, (4) Pass Turn (no action),\n";
print "(5) Draft Soldiers, (6) Release Soldiers, (7) Buy Food, (8) Recruit Engineers,\n";
print "(9) Hire Guards, (10) Hire Assassins, (11) Build Fortifications,\n";
print "(12) Clean up Rubble, (13) Send Assassins, (14) Buy Catapults";
print "\n";
print "How shall we proceed, your grace? ";
my $user_input = <stdin>;
return $user_input;
}
# ScoreCalc
#
# Sub totals the value of all assets for player and computer
sub ScoreCalc {
$Player1_Score = 0;
$Player2_Score = 0;
for my $item (keys(%Player1_Assets)) {
$Player1_Score += $Player1_Assets{$item} * $AssetCost{$item};
# print " _Player ", $item, " - ", $Player1_Assets{$item}, " - ", $Player1_Assets{$item} * $AssetCost{$item}, "\n";
$Player2_Score += $Player2_Assets{$item} * $AssetCost{$item};
# print " _Computer ", $item, " - ", $Player2_Assets{$item}, " - ", $Player2_Assets{$item} * $AssetCost{$item}, "\n";
}
}
# MotivateMessage
#
# This sub randomly picks from the available motivation messages to display
# at the top of the menu screen.
sub MotivateMessage {
$menu_item = int(rand(@action_to_take));
$RandMessage = $action_to_take[$menu_item];
return $RandMessage;
}
# TroopAttack
#
#
sub TroopAttack {
# Reset variables for storing lost units
$Attacker_RoundSoldLoss = 0;
$Attacker_RoundHeroLoss = 0;
$Defender_RoundSoldLoss = 0;
$Defender_RoundHeroLoss = 0;
$TA_AttackerHLosses = 0;
$TA_AttackerLosses = 0;
$TA_DefenderHLosses = 0;
$TA_DefenderLosses = 0;
# Get the attacker's original strength. This value is used to
# determine if the attacker should retreat
$TA_AttackerOriginalStrength = 0;
my $TA_TempString = "\$".$Attacker."_Assets{soldiers}";
my $TA_AttackerOriginalStrength = eval $TA_TempString;
# Determine Base Intensity
my $TAcnt = 0;
$CombatIntensity = rand(40)+20;
$CombatIntensity = int($CombatIntensity);
# Apply intensity modifiers.
if ($WeatherToday eq "Rainy") { $CombatIntensity = $CombatIntensity*1.3};
if ($WeatherToday eq "Hot") { $CombatIntensity = $CombatIntensity*0.4 };
if ($WeatherToday eq "Cold") { $CombatIntensity =$CombatIntensity*0.75};
if ($WeatherToday eq "Freezing") { $CombatIntensity =$CombatIntensity*0.55 };
if ($WeatherToday eq "Mild") { $CombatIntensity = $CombatIntensity*1.5 };
if ($WeatherToday eq "Windy") { $CombatIntensity = $CombatIntensity*0.8};
if ($WeatherToday eq "Foggy") { $CombatIntensity=$CombatIntensity*0.6};
if ($WeatherToday eq "Cool") { $CombatIntensity+=20};
# Determine max number of rounds from array storing possible values
my $TA_Rounds = @PossibleRounds[int(rand(@PossibleRounds))-1];
if ($TA_AttackerOriginalStrength < 100) {
print "We have only ", $TA_AttackerOriginalStrength, " soldiers...\n";
print "Sire, we dare not storm the walls with so few!\n";
print "Press <enter> to continue.\n";
$Trashvar = <stdin>;
return 0;
}
# Get the rest of the original strengths for attackers and defenders.
# These values will be displayed at the end
my $TA_AS = $TA_AttackerOriginalStrength;
$TA_TempString = "\$".$Attacker."_Assets{heroes};";
my $TA_AH = eval $TA_TempString;
$TA_TempString = "\$".$Defender."_Assets{soldiers};";
my $TA_DS = eval $TA_TempString;
$TA_TempString = "\$".$Defender."_Assets{heroes};";
my $TA_DH = eval $TA_TempString;
my $TAcnt2 = 0;
# Reset Loss Tracker Variables
$TA_AttackerLosses = 0;
$TA_DefenderLosses = 0;
$TA_AttackerHLosses = 0;
$TA_DefenderHLosses = 0;
# Loop (rounds) or until attackers have lost half strength
for ($TAcnt=1; $TAcnt<=$TA_Rounds; $TAcnt++) {
sleep (1.0);
# Reset variables
$Attacker_RoundSoldLoss = 0;
$Attacker_RoundHeroLoss = 0;
$Defender_RoundSoldLoss = 0;
$Defender_RoundHeroLoss = 0;
my $Temp_Losses = 0;
# Determine number of assault units of attacker
$TA_TempString = "\$".$Attacker."_Assets{soldiers}" ;
$TA_AttackerSoldiers = eval $TA_TempString;
$TA_AttackerUnits = int($TA_AttackerSoldiers/100);
# Determine number of hero units of attacker
$TA_TempString = "\$".$Attacker."_Assets{heroes}";
$TA_AttackerHeroes = eval $TA_TempString;
# Determine number of defense units of attacker
$TA_TempString = "\$".$Defender."_Assets{soldiers}" ;
$TA_DefenderSoldiers = eval $TA_TempString;
$TA_DefenderUnits = int($TA_DefenderSoldiers/100) + 1;
# Determine number of fortification units of defenders
$TA_TempString = "int(\$".$Defender."_Assets{fortifications}/2500)-1" ;
$TA_DefenderWalls = eval $TA_TempString;
# Soldiers can only have one wall protecting them.
if ($TA_DefenderWalls > $TA_DefenderUnits) {
$TA_DefenderWalls = $TA_DefenderUnits;
}
# Determine number of hero units of defender
$TA_TempString = "\$".$Defender."_Assets{heroes}";
$TA_DefenderHeroes = eval $TA_TempString;
# Total offensive and defensive units
$TA_AttackerTotal = $TA_AttackerUnits + $TA_AttackerHeroes;
$TA_DefenderTotal = $TA_DefenderUnits + $TA_DefenderHeroes + $TA_DefenderWalls;
print "\nPreparing our assault, milord, round ", $TAcnt, ".\n";
print "We have ", $TA_AttackerTotal, " units. The enemy has ", $TA_DefenderTotal, " units.\nIt begins.\n\n";
if (($TA_AttackerTotal / 2) > $TA_DefenderTotal) {
$CombatIntensity = int($CombatIntensity * ($TA_AttackerTotal / $TA_DefenderTotal));
}
# Run assault units
for ($TAcnt2 = 1; $TAcnt2<=$TA_AttackerUnits; $TAcnt2++) {
ResultDeterminer();
};
# Run hero assaults
for ($TAcnt2 = 1; $TAcnt2<=$TA_AttackerHeroes; $TAcnt2++) {
HeroResultDeterminer();
};
$Loss_Val = 0;
# Subtract Lost Attacker Soldiers
for ($TAcnt2 = 1; $TAcnt2<=$Attacker_RoundSoldLoss; $TAcnt2++) {
$Loss_Val = int(rand($CombatIntensity)+1);
if ($Loss_Val > 100) { $Loss_Val = 100; }
$TA_AttackerLosses+=$Loss_Val;
$Temp_Losses += $Loss_Val;
$TA_TempString = "\$".$Attacker."_Assets{soldiers}-=\$Loss_Val" ;
eval $TA_TempString;
}
print "Round ", $TAcnt, " losses:\n Attacker Soldiers lost: ", $Temp_Losses, " Heroes lost: ", $Attacker_RoundHeroLoss, "\n";
# Subtract Lost Attacker Heroes
$TA_TempString = "\$".$Attacker."_Assets{heroes}-=\$Attacker_RoundHeroLoss" ;
eval $TA_TempString;
# Subtract Lost Defender Soldiers
$Temp_Losses = 0;
for ($TAcnt2 = 1; $TAcnt2<=$Defender_RoundSoldLoss; $TAcnt2++) {
$Loss_Val = int(rand($CombatIntensity)+1);
if ($Loss_Val > 100) { $Loss_Val = 100; }
$Temp_Losses += $Loss_Val;
$TA_DefenderLosses+=$Loss_Val;
$TA_TempString = "\$".$Defender."_Assets{soldiers}-=\$Loss_Val" ;
eval $TA_TempString;
}
# Make sure defender doesn't lose more soldiers than he has.
if ($TA_DefenderLosses > $TA_DS) {
# Calculate extra automatic hero kills
my $TA_HeroExtraKills = int(($TA_DefenderLosses - $TA_DS)/100);
# Set total soldier losses = original soldier value.
$TA_TempString = "\$".$Defender."_Assets{soldiers} = 0";
eval $TA_TempString;
$Defender_RoundHeroLoss+=$TA_HeroExtraKills;
if ($Defender_RoundHeroLoss > $TA_DH) { $Defender_RoundHeroLoss = $TA_DH; }
$TA_DefenderLosses = $TA_DS;
}
my $DefenderHeroLoss_ThisRound = 0;
# Subtract Lost Defender Heroes
$TA_TempString = "\$".$Defender."_Assets{heroes}";
$DefenderHeroLoss_ThisRound = eval $TA_TempString;
$TA_TempString = "\$".$Defender."_Assets{heroes}-=\$Defender_RoundHeroLoss" ;
eval $TA_TempString;
$TA_TempString = "\$".$Defender."_Assets{heroes}";
$DefenderHeroLoss_ThisRound -= eval $TA_TempString;
my $TempHeroes = eval $TA_TempString;
if ($TempHeroes < 1) {
$TA_TempString = "\$".$Defender."_Assets{heroes} = 0";
eval $TA_TempString;
}
# Clean up defender loss values and report to players
if ($Temp_Losses > $TA_DS) { $Temp_Losses = $TA_DS; }
print " Defender Soldiers lost: ", $Temp_Losses, " Heroes lost: ", $DefenderHeroLoss_ThisRound, "\n";
# Make sure defender doesn't lose more heroes than he has
if ($TA_DefenderHLosses > $TA_DH) { $TA_DefenderHLosses = $TA_DH};
# Check to see if attacker has lost more than half his force.
# If so, he will withdraw, unless the battle is going well
if (($TA_AttackerLosses > $TA_AS/2) && (($TA_DefenderLosses * 1.25) < ($TA_DS/2))) {
$TAcnt = $TA_Rounds;
print "Sire, we must fall back before we have no army left to defend our own walls!\n";
print "Press <enter> to continue.";
$Trashvar = <stdin>;
}
# Adjust variables used for tracking overall Hero Losses
$TA_AttackerHLosses+=$Attacker_RoundHeroLoss;
$TA_DefenderHLosses+=$Defender_RoundHeroLoss;
# Check to see if defender has been vanquished
if (($TA_DefenderLosses >= $TA_DS) && ($TA_DefenderHLosses >= $TA_DH)) {
$TAcnt = $TA_Rounds;
}
}
# Report outcome of assault
# Attacker's Losses
print "\n------------------------------------------------\n";
print "Battle Results:\n";
print "------------------------------------------------\n";
printf "Sir, we attacked with %s soldiers, and %s heroes.\n", $TA_AS, $TA_AH;
printf "%s soldiers and %s heroes were lost.\n\n", $TA_AttackerLosses, $TA_AttackerHLosses;
# Defender's Losses
printf "The enemy met our assault with %s soldiers, and %s heroes.\n", $TA_DS, $TA_DH;
printf "%s of his soldiers and %s of his heroes will trouble us no longer.\n\n", $TA_DefenderLosses, $TA_DefenderHLosses;
# Hold for user input.
print "Press <enter> to continue.\n";
$Trashvar = <stdin>;
# Check to see if attack was successful and castle was stormed
if (($TA_DS <= $TA_DefenderLosses) && ($TA_DH <= $TA_DefenderHLosses)) {
return 2;
}
else {
return 1;
}
}
# ResultDeterminer
#
# This sub determines the result of an individual sortie of soldiers
sub ResultDeterminer {
# $dice determines which defender the attacker is against
my $dice = int(rand($TA_DefenderTotal)) + 1;
# $dice2 is used to determine the outcome of the encounter
$dice2 = int(rand(100) + 1);
if ($dice <= $TA_DefenderUnits) {
# Attacker encounters a soldier
if ($dice2 < 50) {
# Attacker wins
$Defender_RoundSoldLoss++;
}
elsif ($dice2 < 100) {
# Defender wins
$Attacker_RoundSoldLoss++;
}
}
elsif ($dice <= ($TA_DefenderUnits + $TA_DefenderHeroes)) {
# Attacker encounters a hero
if ($dice2 <= 8) {
# Defending Hero dies
$Defender_RoundHeroLoss++;
print "We have slain an enemy hero!\n";
}
elsif ($dice2 < 80) {
# Attacking soldier dies
$Attacker_RoundSoldLoss++;
}
}
elsif ($dice <= ($TA_DefenderUnits + $TA_DefenderHeroes + $TA_DefenderWalls)) {
# Attacking soldier encounters a wall
if ($dice2 < 30) {
# Attacking Soldier dies
$Attacker_RoundSoldLoss++;
}
}
}
# HeroResultDeterminer
#
# This sub determines the result of an individual sortie of a hero
sub HeroResultDeterminer {
my $dice = int(rand($TA_DefenderTotal)) + 1;
$dice2 = int(rand(100) + 1);
if ($dice <= $TA_DefenderUnits) {
# Attacking hero encounters a soldier
if ($dice2 < 50) {
# Attacker wins
$Defender_RoundSoldLoss++;
}
elsif ($dice2 < 58) {
# Defender wins
$Attacker_RoundHeroLoss++;
print "One of our heroes has fallen...\n";
}
}
elsif ($dice <= ($TA_DefenderUnits + $TA_DefenderHeroes)) {
# Attacking hero encounters a hero
if ($dice2 < 10) {
# Defending Hero dies
$Defender_RoundHeroLoss++;
print "Huzzah, one of their heroes is slain!\n";
}
elsif ($dice2 < 20) {
# Attacking Hero dies
$Attacker_RoundHeroLoss++;
print "Sire... a hero is no longer with us...\n";
}
}
elsif ($dice <= ($TA_DefenderUnits + $TA_DefenderHeroes + $TA_DefenderWalls)) {
# Attacking hero encounters a wall
if ($dice2 <= 1) {
# Attacking Hero dies
$Attacker_RoundHeroLoss++;
print "Oh no! One of our heroes has met with misfortune!\n";
}
}
}
# TroopRaid
#
# This command sends a raid to seize enemy materiel
sub TroopRaid {
print "My lord, we have hand-picked the best man from each unit to launch a daring\nraid on our enemy!\n";
my $TR_TempString = "\$".$Attacker."_Assets{heroes}";
my $TR_AttackerHeroes = eval $TR_TempString;
$TR_TempString = "\$".$Attacker."_Assets{soldiers}";
my $TR_AttackerSoldiers = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{fortifications}";
my $TR_DefenderFortifications = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{food}";
my $TR_DefenderFood = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{soldiers}";
my $TR_DefenderSoldiers = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{peasants}";
my $TR_DefenderPeasants = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{guards}";
my $TR_DefenderGuards = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{assassins}";
my $TR_DefenderAssassins = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{catapults}";
my $TR_DefenderCatapults = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{engineers}";
my $TR_DefenderEngineers = eval $TR_TempString;
$TR_TempString = "\$".$Defender."_Assets{heroes}";
my $TR_DefenderHeroes = eval $TR_TempString;
my $TR_Raiders = int($TR_AttackerSoldiers / 100);
if ($TR_Raiders < 1) {
print "Sire, with our forces stretched so thin, we dare not risk even one soldier on such a mission.\n";
return 0; # Cannot send raid because there are too few soldiers
}
else {
print "The men are good to go, sire.\nThere are ", $TR_Raiders, " raiders ready.";
}
my $TotalGuards = ($TR_DefenderGuards + (10 * $TR_DefenderHeroes));
my $FortRatio = $TR_DefenderFortifications / $TotalGuards;
print "Ratio is: ", $FortRatio, "\n";
my $RaidSuccess = 25; # Tweak this number to affect chances of getting caught on approach (i.e. bonus to small raid forces)
my $TRcnt = 0;
my $TR_SightingVar = 0;
for ($TRcnt = 1; $TRcnt <= $TR_Raiders; $TRcnt++) {
$TR_SightingVar = rand(500 - $TotalGuards);
print "Sightvar: ", $TR_SightingVar, " | SuccessVar: ", $RaidSuccess, "\n";
if ($TR_SightingVar < $RaidSuccess) {
print "One of our raiders was spotted: ", $TRcnt, "\n";
}
}
}
# KillCastle
#
# This command fires catapults at the enemy's fortifications.
sub KillCastle {
# The effectiveness of firing catapults at the castle depends on
# several factors. First, weather can have an adverse effect on hit
# accuracy. Windy, rainy, and (to a lesser extent) cold days make
# shots less likely to hit walls. Engineers will provide a bonus not
# just to hit accuracy, but to damage if a shot hits.
#
# Shots that hit are tested to see if they destroy the wall. Engineers
# once again provide a bonus to this roll. If the shot destroys the
# wall entirely, then damage is rolled to see what is destroyed behind
# the wall (soldiers, peasants, catapults, etc).
#
# Players get 3 shots per catapult, and if they get a hit, they get
# an extra shot. Additionally, if a shot destroys a wall entirely,
# they get another hit.
# Determine if attacker has catapults. If not, abort attack.
my $KC_TempString = "\$".$Attacker."_Assets{catapults}";
my $KC_AttackerCatapults = eval $KC_TempString;
if ($KC_AttackerCatapults < 1) {
return 0; # Attack aborts due to insufficient catapults
}
# Initialize Defender damage variables
my $EngineerLosses = 0;
my $SoldierLosses = 0;
my $GuardLosses = 0;
my $AssassinLosses = 0;
my $CatapultLosses = 0;
my $FortificationLosses = 0;
my $FoodLosses = 0;
my $HeroLosses = 0;
my $PeasantLosses = 0;
# Get other Attacker and Defender stats
$KC_TempString = "\$".$Attacker."_Assets{engineers}";
my $KC_AttackerEngineers = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{soldiers}";
my $KC_DefenderSoldiers = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{peasants}";
my $KC_DefenderPeasants = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{guards}";
my $KC_DefenderGuards = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{fortifications}";
my $KC_DefenderFortifications = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{assassins}";
my $KC_DefenderAssassins = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{catapults}";
my $KC_DefenderCatapults = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{food}";
my $KC_DefenderFood = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{heroes}";
my $KC_DefenderHeroes = eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{engineers}";
my $KC_DefenderEngineers = eval $KC_TempString;
# Engineers make targeting the fortifications more effective
# First Roll for effectiveness of bombardment (effectiveness = potential damage/shot)
my $Effectiveness = 0;
my $BaseEffectiveness = 20;
my $EngineerBonus = int($KC_AttackerEngineers ** .7);
print "\nEngineer Bonus: ", $EngineerBonus, "\n";
# Effectiveness is the number that a roll must beat to hit the target
$Effectiveness = int($BaseEffectiveness + $EngineerBonus);
print "Effectiveness before weather: ", $Effectiveness, "\n";
# Determine weather bonuses
if ($WeatherToday eq "Windy") {
$Effectiveness = int($Effectiveness/1.5);
}
elsif ($WeatherToday eq "Rainy") {
$Effectiveness = int($Effectiveness/1.1);
}
elsif ($WeatherToday eq "Mild") {
$Effectiveness *= 2;
}
elsif ($WeatherToday eq "Cold") {
$Effectiveness -= 10;
}
if ($Effectiveness < 3) {
$Effectiveness = 3;
}
print "Effectiveness post-weather: ", $Effectiveness, "\n";
# Determine targets
my $DefenderWalls = int(sqrt($KC_DefenderFortifications));
print "\nDefender Walls: ", $DefenderWalls, "\n";
my $EngineersPerWall = int(1000 * $KC_DefenderEngineers / $DefenderWalls);
my $SoldiersPerWall = int (1000 * $KC_DefenderSoldiers / $DefenderWalls);
my $FoodPerWall = int (1000 * $KC_DefenderFood / $DefenderWalls);
my $AssassinsPerWall = int (1000 * $KC_DefenderAssassins / $DefenderWalls);
my $GuardsPerWall = int (1000 * $KC_DefenderGuards / $DefenderWalls);
my $HeroesPerWall = int (1000 * $KC_DefenderHeroes / $DefenderWalls);
my $FortificationsPerWall = int (1000 * $KC_DefenderFortifications / $DefenderWalls) + 200;
my $CatapultsPerWall = int (1000 * $KC_DefenderCatapults / $DefenderWalls);
my $PeasantsPerWall = int (1000 * $KC_DefenderPeasants / $DefenderWalls);
# Calculate chance of catapult being destroyed while firing
my $CatapultShatter = 10;
if ($WeatherToday eq "Cold") { $CatapultShatter *= 2; }
if ($WeatherToday eq "Freezing") { $CatapultShatter *= 3; }
# Next loop for each shot and determine damage
my $KC_Shots = $KC_AttackerCatapults * 3;
my $KC_cnt = 0;
my $KC_TargetDice = 0;
my $KC_Delay = 0.3;
$KC_Delay = 0.3 - ($KC_Shots - (.2 / 30));
if ($KC_Delay < 0.1) { $KC_Delay = 0.1; }
for ($KC_cnt = 0; $KC_cnt < $KC_Shots; $KC_cnt++) {
$KC_TargetDice = int(rand(100 + $Effectiveness));
# Check to see if shot hits
use Time::HiRes qw ( sleep );
sleep ($KC_Delay);
if ($KC_TargetDice < $Effectiveness) {
$KC_cnt--;
# Calculate whether wall is completely destroyed:
my $KC_WallDice = int(rand(20 + $EngineerBonus));
my $DamageToWall = $KC_WallDice / (30 + int(sqrt($DefenderWalls)));
# printf "Amount of wall destroyed: %s\n", $DamageToWall;
$FortificationLosses += int($FortificationsPerWall * $DamageToWall / 300);
# Determine Damage to non-wall assets, which happens
# when wall is completely destroyed
if ($DamageToWall > 1) {
if ($DamageToWall > 1.5) {
print "< BOOOOM! >\n";
$DamageToWall *= (rand(2) + 1);
}
else {
print "< BANG! >\n";
}
# Determine remaining damage to be distributed
my $ExtraDamage = $DamageToWall - 1;
#if ($ExtraDamage > 1) { $ExtraDamage = 1; }
$HeroLosses += int(rand($HeroesPerWall * 20) * $ExtraDamage);
$SoldierLosses += int(rand($SoldiersPerWall * 2) * $ExtraDamage);
$EngineerLosses += int(rand($EngineersPerWall * 20) * $ExtraDamage);
$PeasantLosses += int(rand($PeasantsPerWall * 2) * $ExtraDamage);
$CatapultLosses += int(rand($CatapultsPerWall * 50) * $ExtraDamage);
$AssassinLosses += int(rand($AssassinsPerWall * 2) * $ExtraDamage);
$GuardLosses += int(rand($GuardsPerWall * 2) * $ExtraDamage);
$FoodLosses += int(rand($FoodPerWall * 20) * $ExtraDamage);
}
else {
alarm 0;
print "< BAM! >\n";
}
}
else {
print "* MISS! *\n";
}
}
sleep (0.3);
sleep (0.3);
print "\n";
$PeasantLosses = int($PeasantLosses / 200);
$GuardLosses = int($GuardLosses / 50);
$AssassinLosses = int($AssassinLosses / 200);
$SoldierLosses = int($SoldierLosses / 50);
$HeroLosses = int($HeroLosses / 200);
$FoodLosses = int($FoodLosses / 500);
$CatapultLosses = int($CatapultLosses / 1000);
$EngineerLosses = int($EngineerLosses / 500);
# Check to make sure more assets aren't being destroyed than are possessed by enemy
if ($PeasantLosses > $KC_DefenderPeasants) { $PeasantLosses = $KC_DefenderPeasants; }
if ($GuardLosses > $KC_DefenderGuards) { $GuardLosses = $KC_DefenderGuards; }
if ($AssassinLosses > $KC_DefenderAssassins) { $AssassinLosses = $KC_DefenderAssassins; }
if ($SoldierLosses > $KC_DefenderSoldiers) { $SoldierLosses = $KC_DefenderSoldiers; }
if ($HeroLosses > $KC_DefenderHeroes) { $HeroLosses = $KC_DefenderHeroes; }
if ($FortificationLosses > $KC_DefenderFortifications) { $FortificationLosses = $KC_DefenderFortifications; }
if ($FoodLosses > $KC_DefenderFood) { $FoodLosses = $KC_DefenderFood; }
if ($CatapultLosses > $KC_DefenderCatapults) { $CatapultLosses = $KC_DefenderCatapults; }
if ($EngineerLosses > $KC_DefenderEngineers) { $EngineerLosses = $KC_DefenderEngineers; }
print "\n BOMBARDMENT REULTS:\n";
print "------------------------------------------\n";
print "Peasants Killed: ", $PeasantLosses, "\n";
print "Guards Killed: ", $GuardLosses, "\n";
print "Assassins Killed: ", $AssassinLosses, "\n";
print "Soldiers Killed: ", $SoldierLosses, "\n";
print "Heroes Killed: ", $HeroLosses, "\n";
print "Fortifications Destroyed: ", $FortificationLosses, "\n";
print "Food Wasted: ", $FoodLosses, "\n";
print "Catapults Destroyed: ", $CatapultLosses, "\n";
print "Engineers Killed: ", $EngineerLosses, "\n\n";
$KC_DefenderPeasants -= $PeasantLosses;
if ($KC_DefenderPeasants < 1) { $KC_DefenderPeasants = 0; }
$KC_DefenderGuards -= $GuardLosses;
if ($KC_DefenderGuards < 1) { $KC_DefenderGuards = 0; }
$KC_DefenderAssassins -= $AssassinLosses;
if ($KC_DefenderAssassins < 1) { $KC_DefenderAssassins = 0; }
$KC_DefenderSoldiers -= $SoldierLosses;
if ($KC_DefenderSoldiers < 1) { $KC_DefenderSoldiers = 0; }
$KC_DefenderHeroes -= $HeroLosses;
if ($KC_DefenderHeroes < 1 ) { $KC_DefenderHeroes = 0; }
$KC_DefenderFortifications -= $FortificationLosses;
if ($KC_DefenderFortifications < 1 ) { $KC_DefenderFortifications = 0 }
$KC_DefenderFood -= $FoodLosses;
if ($KC_DefenderFood < 1) { $KC_DefenderFood = 0; }
$KC_DefenderCatapults -= $CatapultLosses;
if ($KC_DefenderCatapults < 1) { $KC_DefenderCatapults = 0; }
$KC_DefenderEngineers -= $EngineerLosses;
if ($KC_DefenderEngineers < 1) { $KC_DefenderEngineers = 0; }
$KC_TempString = "\$".$Defender."_Assets{soldiers} = $KC_DefenderSoldiers";
eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{peasants} = $KC_DefenderPeasants";
eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{guards} = $KC_DefenderGuards";
eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{fortifications} = $KC_DefenderFortifications";
eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{assassins} = $KC_DefenderAssassins";
eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{catapults} = $KC_DefenderCatapults";
eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{food} = $KC_DefenderFood";
eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{heroes} = $KC_DefenderHeroes";
eval $KC_TempString;
$KC_TempString = "\$".$Defender."_Assets{engineers} = $KC_DefenderEngineers";
eval $KC_TempString;
# Add Rubble
$KC_TempString = "\$".$Defender."_Rubble += $FortificationLosses";
eval $KC_TempString;
# See if enemy castle was destroyed.
if ($FortificationLosses == $KC_DefenderFortifications) {
print "\nSire! We have reduced the enemy's castle to Rubble! Victory is ours!\n";
return 2;
}
# Firing catapults was successful
return 1;
}
# PassTurn
#
# This sub passes your turn
sub PassTurn {
print "\n\nYes, sire, sometimes discretion is the better part of valor.\n";
return 1;
}
# GetSoldiers
#
# This command recruits soldiers from your peasantry
sub GetSoldiers {
# Determine percentage of peasants to be recruited
my $RecruitShare = int(rand(5) + 1) + 10;
# Get population data
my $TempPlayer = $Attacker;
my $TempString = "\$".$TempPlayer."_Assets{soldiers}";
my $TempSoldiers = eval $TempString;
$TempString = "\$".$TempPlayer."_Assets{peasants}";
my $TempPeasants = eval $TempString;
# Calculate new recruits
my $NewRecruits = int($TempPeasants * ($RecruitShare/100));
print "New recruits: ", $NewRecruits, "\n";
# Add recruits to army.
$TempString = "\$".$TempPlayer."_Assets{soldiers} += $NewRecruits";
eval $TempString;
$TempString = "\$".$TempPlayer."_Assets{peasants} -= $NewRecruits";
eval $TempString;
# Recruiting soldiers was a success
return 1;
}
# GetPeasants
#
# This command releases soldiers from your army to become peasants
sub GetPeasants {
my $GP_TempString = "\$".$Attacker."_Assets{soldiers}";
my $GP_Soldiers = eval $GP_TempString;
my $GP_Releases = int($GP_Soldiers * (40 + int(rand(20)+1)) / 100);
print "Release: ", $GP_Releases, "\n";
$GP_TempString = "\$".$Attacker."_Assets{soldiers}-=$GP_Releases";
eval $GP_TempString;
$GP_TempString = "\$".$Attacker."_Assets{peasants}+=$GP_Releases";
eval $GP_TempString;
# Releasing soldiers was a success.
return 1;
}
# GetFood
#
# This command buys food
sub GetFood {
my $TempString = "\$".$Attacker."_Assets{food} += (\$".$Attacker."_Assets{gold}*10)";
eval $TempString;
$TempString = "\$".$Attacker."_Assets{gold} = 0";
eval $TempString;
# Purchasing Food was a success.
return 1;
}
# GetEngineers
#
# This command recruits engineers
sub GetEngineers {
my $TempString = "int(\$".$Attacker."_Assets{gold}/500)" ;
my $EngCandidates = eval $TempString;
# Generate success rate
my $SuccessVal = int(rand(100)+50);
my $NewEngineers = 0;
my $dice2 = 0;
# Roll dice for each engineer to determine success/fail
for ($cnt3=1; $cnt3<=$EngCandidates; $cnt3++) {
$dice2 = int(rand(100));
if ($dice2 < $SuccessVal) {