-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml5.js
2100 lines (1936 loc) · 64.2 KB
/
html5.js
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
/* Task List
- start putting in the travel encounters
- road travel and getting lostSave
- river crossings, getting lost, and peing in partial positions.
- combat with more player characters.
- negotiate and evade
- combat surprise round
- bribe rules
- 2 hex movement if mounted. 3 hex flying movement.
- sleeping / nighttime animation
- Replace this with a measurement of the height using the current font.
- Surprise, Escape, and Routs.
- put in the gold splitting when you get gold
*/
function mapInfo()
{
this.widthInTiles = 20;
this.heightInTiles = 23;
this.tile1Center = new v2(178,161);
this.tileSize = new v2(68, 78);
}
var canvas;
var context;
var imagesToLoad = [];
var images = [];
var map = new mapInfo();
var mapFileName = "map.jpg";
var playerFileName = "player.png";
var moveArrowFileName = "moveArrow.png";
var tileHighlightGreyFileName = "tileHighlightGrey.png";
var tileHighlightYellowFileName = "tileHighlightYellow.png";
var day = 1;
var food = 5;
var gold = 0;
var party = [];
var playerTilePosition = new v2(0, 0);
var playerDrawOffset = new v2(-8, -25);
var combat = {};
resetCombatState();
var activity = {}
clearActivity();
var ui = new Object();
ui.interacting = null;
ui.hot = null;
ui.nextHot = null;
var debugTextMax = 20;
var debugText = [debugTextMax];
window.onload = initialize;
window.addEventListener("keypress", onKeyPress, true);
var lineHeight = 18;
var isSplittingGold = false;
var goldToSplit = 0;
Phase = {
playerAction: 0,
mercenaryCheck : 1,
hunting : 2,
starvation: 3,
dayEnd: 4
}
var phase = Phase.playerAction;
var debugDisplay = false;
var defaultTextStyle = new textStyle('Courier New', 14, 'black');
var debugTextStyle = new textStyle('Lucida Console', 10, '#000000ff');
function clearActivity()
{
activity.canEnd = false;
activity.active = false;
activity.text = [];
activity.options = [];
activity.characters = [];
}
function addDebugText(text)
{
if(debugText.length == debugTextMax)
{
debugText.shift();
}
debugText.push(text);
}
function initialize()
{
resetGame();
canvas = document.getElementById('canvasId');
canvas.addEventListener("click", onClick, true);
canvas.addEventListener("mousemove", onMouseMove, true);
context = canvas.getContext("2d");
setTextStyle(defaultTextStyle);
imagesToLoad.push(mapFileName);
imagesToLoad.push(playerFileName);
imagesToLoad.push(tileHighlightGreyFileName);
imagesToLoad.push(tileHighlightYellowFileName);
loadImagesThenStart();
// Note(ian): Don't do anything after load images then draw as it relies on events to fire off Draw.
}
function resetGame()
{
clearActivity();
resetCombatState();
party.length = 0;
party.push({});
party[0].name = "Cal Arath";
party[0].title = "Barbarian Prince";
party[0].weaponName = "Bonebiter";
party[0].combatSkill = 8;
party[0].maxEndurance = 9;
party[0].currentEndurance = 9;
party[0].witAndWiles = d6();
if(party[0].witAndWiles = 1)
{
party[0].witAndWiles = 2;
}
party[0].mounted = false;
gold = getWealthFromCode(2);
var spawn = d6();
if(spawn == 1)
{
playerTilePosition.x = 0;
playerTilePosition.y = 0;
}
else if(spawn == 2)
{
playerTilePosition.x = 6;
playerTilePosition.y = 0;
}
else if(spawn == 3)
{
playerTilePosition.x = 8;
playerTilePosition.y = 0;
}
else if(spawn == 4)
{
playerTilePosition.x = 12;
playerTilePosition.y = 0;
}
else if(spawn == 5)
{
playerTilePosition.x = 14;
playerTilePosition.y = 0;
}
else if(spawn == 6)
{
playerTilePosition.x = 18;
playerTilePosition.y = 0;
}
phase = Phase.playerAction;
setEvent('e', 001);
}
function loadImagesThenStart()
{
if(imagesToLoad.length > 0)
{
var name = imagesToLoad.pop();
images[name] = new Image();
images[name].onload = loadImagesThenStart;
images[name].src = "images/" + name;
}
else
{
main();
}
}
function main()
{
drawAndUpdate();
requestAnimationFrame(main);
}
function isValidTilePosition(tilePosition)
{
return isValidTilePosition(tilePosition.x, tilePosition.y);
}
function isValidTilePosition(x, y)
{
return x >= 0 && x < map.widthInTiles - 1 && y >= 0 && y < map.heightInTiles - 1;
}
function conciousPlayerCount()
{
var numConciousPlayers = 0;
for (var partyIndex = 0; partyIndex < party.length; partyIndex++)
{
if (party[partyIndex].currentEndurance > 1)
{
numConciousPlayers++;
}
}
return numConciousPlayers;
}
function drawAndUpdate()
{
keysPressed = nextKeysPressed;
nextKeysPressed = {};
mouseClicked = nextMouseClicked;
nextMouseClicked = false;
if (keysPressed[tildeKeyCode])
{
debugDisplay = !debugDisplay;
}
context.clearRect(0, 0, canvas.width, canvas.height);
if (debugDisplay)
{
setTextStyle(debugTextStyle);
var y = 12;
for(var debugIndex = debugText.length - 1;
debugIndex >= 0;
debugIndex--)
{
context.fillText(debugText[debugIndex], 10, y);
y += 12;
}
setTextStyle(defaultTextStyle);
}
else if(conciousPlayerCount() == 0)
{
drawText(10, 10, 'You died, press Enter to start a new game', 'default');
if (keysPressed[enterKeyCode])
{
resetGame();
}
}
else if (isSplittingGold)
{
// Make a list of people who expect a split and allow toggling them on and off.
// The looters come and go as a group so you should have to select all or none of them?
}
else if(combat.active)
{
var overflowIsEnemies = combat.combatants.length > party.length;
var numArenas = Math.min(combat.combatants.length, party.length);
if (!combat.combatInitialized)
{
combat.combatInitialized = true;
for (var arenaIndex = 0; arenaIndex < numArenas; arenaIndex++)
{
var arena = {};
arena.unitList = [];
arena.unitList[0] = party[arenaIndex];
arena.unitList[1] = combat.combatants[arenaIndex];
combat.combatArenas[arenaIndex] = arena;
}
var maxUnits = Math.max(combat.combatants.length, party.length);
var arenaIndex = 0;
for (var unitIndex = numArenas; unitIndex < maxUnits; unitIndex++)
{
var arena = combat.combatArenas[arenaIndex];
if (numArenas < party.length)
{
arena.unitList[arena.unitList.length] = party[unitIndex];
}
else
{
arena.unitList[arena.unitList.length] = combat.combatants[unitIndex];
}
arenaIndex++;
arenaIndex %= numArenas;
}
}
var arenaSectionSize = new v2(canvas.width / numArenas, canvas.height / 4);
for (var arenaIndex = 0; arenaIndex < numArenas; arenaIndex++)
{
var arena = combat.combatArenas[arenaIndex];
for (var unitIndex = 0; unitIndex < arena.unitList.length; unitIndex++)
{
var unit = arena.unitList[unitIndex];
var verticalSectionIndex = 3;
if (unitIndex == 0)
{
verticalSectionIndex = 2;
}
else if (unitIndex == 1)
{
verticalSectionIndex = 1;
}
else if (overflowIsEnemies)
{
verticalSectionIndex = 0;
}
var topLeft = new v2(arenaIndex * arenaSectionSize.x, verticalSectionIndex * arenaSectionSize.y);
var bottomRight = new v2(topLeft.x + arenaSectionSize.x, topLeft.y + arenaSectionSize.y);
if (unitIndex > 1)
{
var numOverflowUnits = arena.unitList.length - 2;
var overflowWidth = arenaSectionSize.x / numOverflowUnits;
var overflowIndex = unitIndex - 2;
topLeft.x += overflowWidth * overflowIndex;
bottomRight.x -= overflowWidth * (numOverflowUnits - overflowIndex - 1);
}
//drawLine(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y, 'red');
var name = unit.name;
var textPosition = v2Copy(topLeft);
var unitMaxWidth = bottomRight.x - topLeft.x;
var nameStyle = new textStyle(defaultTextStyle.name, defaultTextStyle.size, defaultTextStyle.color);
var textWidth = context.measureText(name).width;
for (; textWidth > unitMaxWidth; )
{
nameStyle.size--;
setTextStyle(nameStyle);
textWidth = context.measureText(name).width;
}
textPosition.x += (unitMaxWidth - textWidth) / 2.0;
drawText(textPosition.x, textPosition.y, name, 'default');
setTextStyle(defaultTextStyle);
textPosition.y += lineHeight;
drawText(textPosition.x, textPosition.y, unit.combatSkill);
textPosition.y += lineHeight;
drawText(textPosition.x, textPosition.y, unit.currentEndurance + '/' + unit.maxEndurance, 'default');
}
}
// Apply the combat.
if (keysPressed[enterKeyCode])
{
for (var arenaIndex = 0; arenaIndex < numArenas; arenaIndex++)
{
var arena = combat.combatArenas[arenaIndex];
for (var attackTurn = 0; attackTurn <= 1; attackTurn++)
{
var overflowAttackTurn;
var playerAttackTurn;
var victim;
var victimIndex = attackTurn;
if (combat.playerStrikesFirst)
{
victimIndex++;
}
victimIndex = victimIndex % 2;
victim = arena.unitList[victimIndex];
if (combat.playerStrikesFirst)
{
playerAttackTurn = 0;
if (overflowIsEnemies)
{
overflowAttackTurn = 1;
}
else
{
overflowAttackTurn = 0;
}
}
else
{
playerAttackTurn = 1;
if (overflowIsEnemies)
{
overflowAttackTurn = 0;
}
else
{
overflowAttackTurn = 1;
}
}
for (var unitIndex = 0; unitIndex < arena.unitList.length; unitIndex++)
{
var unit = arena.unitList[unitIndex];
if(unit.currentEndurance > 1 &&
((unitIndex == 0 && attackTurn == playerAttackTurn) ||
(unitIndex == 1 && attackTurn != playerAttackTurn) ||
(unitIndex > 1 && attackTurn == overflowAttackTurn)))
{
var unitCombatSkill = unit.combatSkill - unit.starvation;
var victimCombatSkill = victim.combatSkill - victim.starvation;
var attackValue = unitCombatSkill - victimCombatSkill;
// +2 Target of strike has wounds equalling half or more his endurance
if (victim.currentEndurance <= (victim.maxEndurance / 2))
{
attackValue += 2;
}
// -1 Striker has one or more wounds
if (unit.currentEndurance != unit.maxEndurance)
{
attackValue--;
}
// -1 Striker has wounds equalling half or more his endurance (in addition to the above)
if (unit.currentEndurance <= (unit.maxEndurance / 2))
{
attackValue--;
}
attackValue += d6() + d6();
// Note(ian): Because the wound table has a -1 entry we had to shift it up 1 so it could start at 0.
attackValue++;
var wounds = 0;
if (attackValue >= 0 && attackValue < woundTable.length)
{
wounds = woundTable[attackValue];
}
victim.currentEndurance -= wounds;
}
}
}
}
// Evaluate the combat state and arena unit positions.
var numConciousEnemies = 0;
for (var enemyIndex = 0; enemyIndex < combat.combatants.length; enemyIndex++)
{
if (combat.combatants[enemyIndex].currentEndurance > 1)
{
numConciousEnemies++;
}
}
if (conciousPlayerCount() == 0)
{
}
else if (numConciousEnemies == 0)
{
var goldGained = 0;
for (var enemyIndex = 0; enemyIndex < combat.combatants.length; enemyIndex++)
{
var enemy = combat.combatants[enemyIndex];
goldGained += getWealthFromCode(enemy.wealth);
}
gold += goldGained;
resetCombatState();
clearActivity();
}
else
{
// Todo(ian): Highlight arenas in red if they have not valid targets (and prevent processing the round);
// Todo(ian): make sure the max number of possible arenas is being used.
// Todo(ian): Give the players the ability to drag and drop units.
for (var arenaIndex = 0; arenaIndex < numArenas; arenaIndex++)
{
var arena = combat.combatArenas[arenaIndex];
var enemy = arena.unitList[1];
if (overflowIsEnemies &&
enemy.currentEndurance <= 1)
{
var matchFound = false;
for (var unitIndex = 2; unitIndex < arena.unitList.length && !matchFound; unitIndex++)
{
var unit = arena.unitList[unitIndex];
if (unit.currentEndurance > 1)
{
matchFound = true;
var temp = arena.unitList[1];
arena.unitList[1] = arena.unitList[unitIndex];
arena.unitList[unitIndex] = temp;
}
}
}
}
}
}
}
else if(activity.active)
{
var panel = new Object();
panel.AtY = 0;
panel.AtX = 0;
drawWrappedText(panel, activity.text, canvas.width);
for (var optionIndex = 0; optionIndex < activity.options.length; optionIndex++)
{
var option = activity.options[optionIndex];
var clicked = doButton(panel, option.text);
if (clicked)
{
if (option.type == ActivityOptionType.EndActivity)
{
clearActivity();
}
else if (option.type == ActivityOptionType.Random)
{
processOption(option.outcomes);
}
else if (option.type == ActivityOptionType.Hire)
{
for (var hireIndex = 0; hireIndex < option.numToHire; hireIndex++)
{
var character = activity.characters[hireIndex];
gold -= character.dailyFee;
character.advancePay = true;
party[party.length] = character;
}
clearActivity();
}
else if (option.type == ActivityOptionType.Event)
{
setEvent(option.letter, option.number);
}
}
}
if(activity.canEnd && continueButtonPressed())
{
clearActivity();
}
}
else if (phase == Phase.mercenaryCheck)
{
if ((playerTilePosition.y == 0 && playerTilePosition.x != 13) ||
(playerTilePosition.y == 1 && (playerTilePosition.x == 2 || playerTilePosition.x == 4)))
{
phase = phase.hunting;
setEvent('e', 2);
}
}
else if (phase == Phase.hunting)
{
if (isTown() || isCastle() || isTemple())
{
/*
Hunting is prohibited in any hex with a town, castle, or temple.
Purchased Meals (r215d): if you are in a town, castle, or village you can purchase food for each character in
your party. Normal cost is 1 gold piece per character for food that day. Animals cost 1 gold piece per day to
feed at the stables of the town/castle/village. If you don't purchase food, you must eat stores, as hunting is
prohibited in these hexes.
Food Stores (r215e): food units can be stored and transported (r206) by yourself, other characters, and/or
mounts. Food stores can be purchased in a town, castle or village for 1 gold piece per food unit, but only if you
spent the entire day in the hex. Each food unit is one (1), load to transport.
Animal Fodder (r215f): animals can graze and eat fodder for themselves in any terrain where hunting is
possible except swamps (i.e., in farmland, countryside, forest, or hills). No hunting, stores or purchases are
necessary. If you spend the entire day in a town, castle or temple hex you must stable your animals which cost
one (1) gold piece for food, unless you provide the stable with food stores (two units per animal) to feed them.
Note: the cost of food is separate from the cost of lodging. In towns, castles and temples you must also pay for
lodging; see r217.
r217 Lodging in Towns, Castles and Temples
If your party finishes the day in a town, castle or temple hex, after eating you normally buy lodging for your
party. Each room costs one gold piece for the night. You and any priests, monks, magicians, wizards, or
witches in your party each require a single room. All other followers can share, two per room, if you wish.
Animals are placed in stables at one gold piece per mount.
If you decide to not purchase rooms (due to lack of funds, or a desire to save money), you must roll two dice for
each character in your party, and then subtract your wit & wiles from the result. If the total is "4" or more, the
character deserts - he refuses to serve such a penurious leader! If a mount is without stables, roll one die for
each mount, a 4 or higher means thieves steal the mount during the night, it is permanently lost.
*/
phase = Phase.starvation;
}
else
{
var tileType = terrain[playerTilePosition.x, playerTilePosition.y];
if (!(tileType == Terrain.farmland ||
tileType == Terrain.countryside ||
tileType == Terrain.forest ||
tileType == Terrain.hill ||
tileType == Terrain.swamp))
{
phase = Phase.starvation;
}
else
{
if (doButton(panel, 'Don\'t hunt'))
{
phase = Phase.starvation;
}
if (doButton(panel, 'Hunt'))
{
// Todo(ian): Let the player select the hunter and assistants.
var hunter = party[0];
var huntCheckDice = d6() + d6();
var hunterCombatSkill = hunter.combatSkill - hunter.starvation;
var foodUnits = hunterCombatSkill + Math.floor(hunter.currentEndurance / 2) - huntCheckDice;
if (hunter.isGuide)
{
foodUnits++;
}
if (huntCheckDice == 12)
{
var wounds = d6();
hunter.currentEndurance -= wounds;
}
if (hunter.currentEndurance > 1)
{
// Todo: you can also get the food units if your hunting party had assistants.
food += foodUnits;
}
// Todo: If you rested in the hex today you can select assistant hunters, each adds +1 to the food units and an additional +1 if they are a guide.
if (tileType == Terrain.farmland)
{
var check = d6();
if (check == 5)
{
setEvent('e', 17);
}
else if (check == 6)
{
setEvent('e', 50);// Todo: we are supposed to add 2 to the die roll in this event
}
}
phase = Phase.starvation;
}
}
}
phase = Phase.starvation;
}
else if (phase == Phase.starvation)
{
// Todo(ian): Ability to abandon party members / mounts at any time (except when they are about to divy up gold per gold sharing rules.)
var tileType = terrain[playerTilePosition.x, playerTilePosition.y];
var requiredFood = 0;
for (var partyIndex = 0; partyIndex < party.length; partyIndex++)
{
requiredFood++;
var isFarmCountryForestOrHill =
tileType == Terrain.farmland ||
tileType == Terrain.countryside ||
tileType == Terrain.forest ||
tileType == Terrain.hill;
if (!isFarmCountryForestOrHill &&
party[partyIndex].mounted == true)
{
requiredFood += 2;
}
}
if (tileType == Terrain.desert && !isOasis())
{
requiredFood *= 2;
}
var desertion = false;
var starvation = 0;
var actionSelected = false;
if (doButton(panel, 'Don\'t Eat'))
{
actionSelected = true;
desertion = true;
starvation = 1;
}
if (0 < food && food < requiredFood &&
doButton(panel, 'Partition remaining food'))
{
actionSelected = true;
desertion = true;
food = 0;
}
if (food >= requiredFood && doButton(panel, 'Eat'))
{
actionSelected = true;
food -= requiredFood;
starvation = -1;
}
if (food >= 2 * requiredFood && doButtion(panel, 'Double Meal'))
{
actionSelected = true;
food -= 2 * requiredFood;
starvation = -2;
}
if (actionSelected)
{
for (var partyIndex = party.length - 1; partyIndex > 0; partyIndex--)
{
var partyMember = party[partyIndex];
partyMember.starvation += starvation;
if (partyMember.starvation < 0)
{
partyMember.starvation = 0;
}
if (desertion && partyIndex != 0)
{
var desertionCheck = d6() + d6() - party[0].witAndWiles;
if (desertionCheck >= 4)
{
party.splice(partyIndex, 1);
}
}
}
phase = Phase.dayEnd;
}
/*
Character Starvation (r216b): if a character goes, without food for a day, on the following day his ability to carry loads (r206) is halved, with fractions rounded down, and his combat skill is reduced by one. If he goes without food again, load carrying and combat skill is reduced again. When food is available and eaten again,
each day's normal meal also eliminates the effect of one day of starvation. A double meal can be eaten to eliminate the effect of two days of starvation, but triple or larger meals have no additional effect. A character cannot die of starvation within the scope of the game, but after a certain point progressive starvation makes him nearly worthless!
Mount Starvation; if animals (mounts) in the party go without food, their carrying capacity is halved for each day of starvation, just like characters. When carrying capacity reaches zero, the mount dies. If a winged mount goes without food, it is unable to fly. Unlike characters, as soon as a mount gets a normal meal, it recovers from all starvation effects. */
phase = Phase.dayEnd
}
else if (phase == Phase.dayEnd)
{
addDebugText("End Of Day");
phase = Phase.playerAction;
var partySalary = 0;
for (var partyIndex = 1; partyIndex < party.length; partyIndex++)
{
var partyMember = party[partyIndex];
if (partyMember.advancePay)
{
partyMember.advancePay = false;
}
else
{
partySalary += partyMember.dailyFee;
}
}
gold -= partySalary;
day++;
}
else
{
var topLeftMapPosition = v2Add(map.tile1Center, v2Hadamard(map.tileSize, playerTilePosition));
if(!isOnEvenTile())
{
topLeftMapPosition.y += 0.5 * map.tileSize.y;
}
topLeftMapPosition.x -= 0.5 * canvas.width;
topLeftMapPosition.y -= 0.5 * canvas.height;
if(topLeftMapPosition.x < 0)
{
topLeftMapPosition.x = 0;
}
if(topLeftMapPosition.y < 0)
{
topLeftMapPosition.y = 0;
}
if(topLeftMapPosition.x + canvas.width > images[mapFileName].width)
{
topLeftMapPosition.x = images[mapFileName].width - canvas.width;
}
if(topLeftMapPosition.y + canvas.height > images[mapFileName].height)
{
topLeftMapPosition.y = images[mapFileName].height - canvas.height;
}
context.drawImage(images[mapFileName], topLeftMapPosition.x, topLeftMapPosition.y, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
var firstTileCenter = v2Subtract(map.tile1Center, topLeftMapPosition);
// Tile under mouse.
var hotTile;
{
var mouseRelativeToFirstTileCenter = v2Subtract(mousePosition, firstTileCenter);
var evenGrid = v2HadamardDivision(mouseRelativeToFirstTileCenter, map.tileSize);
var evenGridRounded = v2Copy(evenGrid);
evenGridRounded.x /= 2;
v2RoundAssign(evenGridRounded);
evenGridRounded.x *= 2;
var evenGridDistance = v2Length(v2Subtract(evenGrid, evenGridRounded));
var oddGrid = new v2(evenGrid.x, evenGrid.y - 0.5);
var oddGridRounded = v2Copy(oddGrid);
oddGridRounded.x -= 1;
oddGridRounded.x /= 2;
v2RoundAssign(oddGridRounded);
oddGridRounded.x *= 2;
oddGridRounded.x += 1;
var oddGridDistance = v2Length(v2Subtract(oddGrid, oddGridRounded));
hotTile = evenGridRounded;
if(oddGridDistance < evenGridDistance)
{
hotTile = oddGridRounded;
}
}
// Highlight Valid Moves
{
var moveTiles = [];
moveTiles.push(new v2(playerTilePosition.x, playerTilePosition.y - 1));
if(playerTilePosition.x % 2 == 0)
{
moveTiles.push(new v2(playerTilePosition.x + 1, playerTilePosition.y - 1));
moveTiles.push(new v2(playerTilePosition.x - 1, playerTilePosition.y - 1));
}
moveTiles.push(new v2(playerTilePosition.x + 1, playerTilePosition.y));
moveTiles.push(new v2(playerTilePosition.x - 1, playerTilePosition.y));
if(playerTilePosition.x % 2 == 1)
{
moveTiles.push(new v2(playerTilePosition.x + 1, playerTilePosition.y + 1));
moveTiles.push(new v2(playerTilePosition.x - 1, playerTilePosition.y + 1));
}
moveTiles.push(new v2(playerTilePosition.x, playerTilePosition.y + 1));
for(var tileIndex = 0;
tileIndex < moveTiles.length;
tileIndex++)
{
var tile = moveTiles[tileIndex];
if(tile.x >= 0 && tile.x < map.widthInTiles - 1 && tile.y >= 0 && tile.y < map.heightInTiles - 1)
{
if(!v2Equals(tile, hotTile))
{
var center = getDrawLocationFromTile(firstTileCenter, tile, new v2(images[tileHighlightGreyFileName].width, images[tileHighlightGreyFileName].height), new v2(0, 0));
context.drawImage(images[tileHighlightGreyFileName], center.x, center.y);
}
else
{
var center = getDrawLocationFromTile(firstTileCenter, hotTile, new v2(images[tileHighlightYellowFileName].width, images[tileHighlightYellowFileName].height), new v2(0, 0));
context.drawImage(images[tileHighlightYellowFileName], center.x, center.y);
if(mouseClicked)
{
phase = Phase.mercenaryCheck;
// 0 - Farmland
// 1 - Countryside
// 2 - Forset
// 3 - Hills
// 4 - Mountains
// 5 - Swamp
// 6 - Desert
var currentTerrainType = terrain[playerTilePosition.y][playerTilePosition.x];
// TODO(ian): Handle these cases.
// 7 - Cross River
// 8 - On Road
// 9 - Airborne
// 10 - Rafting
var lostSave = travelTableLost[currentTerrainType];
var lostSaveRoll = d6() + d6();
addDebugText("Lost Save: " + lostSaveRoll + " (needed below " + lostSave + ").");
if(lostSaveRoll >= lostSave)
{
// TODO(ian): lose any remaining moves if mounted
addDebugText("You Got Lost");
terrainEvent(currentTerrainType);
}
else
{
var newTerrainType = terrain[hotTile.y][hotTile.x];
terrainEvent(newTerrainType);
playerTilePosition = hotTile;
}
}
}
}
}
}
var playerPosition = getDrawLocationFromTile(firstTileCenter, playerTilePosition, new v2(images[playerFileName].width, images[playerFileName].height), playerDrawOffset);
context.drawImage(images[playerFileName], playerPosition.x, playerPosition.y);
// Todo(ian): Draw these on the canvas.
document.getElementById('remainingFood').innerHTML = food;
document.getElementById('hitPoints').innerHTML = party[0].currentEndurance + "/" + party[0].maxEndurance;
document.getElementById('gold').innerHTML = gold;
// for(var y = 0; y < terrain.length; y++)
// {
// for(var x = 0; x < terrain[y].length; x++)
// {
// if ((y == 0 && x != 13) ||
// (y == 1 && (x == 2 || x == 4)))
// {
// //var center = getDrawLocationFromTile(firstTileCenter, new v2(x, y), new v2(0, 0), new v2(0, 0));
// //drawCircle(10, center.x, center.y, '#a07a39')
// }
// }
// }
/*for(var y = 0; y < terrain.length; y++)
{
for(var x = 0; x < terrain[y].length; x++)
{
// 0 - Farmland
// 1 - Countryside
// 2 - Forset
// 3 - Hills
// 4 - Mountains
// 5 - Swamp
// 6 - Desert
var color = '#FFFFFF';
switch(terrain[y][x])
{
case 0:
color = '#a07a39';
break;
case 1:
color = '#d9b886';
break;
case 2:
color = '#54603a';
break;
case 3:
color = '#d49557';
break;
case 4:
color = '#434739';
break;
case 5:
color = '#bf8b7f';
break;
case 6:
color = '#fdeddc';
break;
}
var center = getDrawLocationFromTile(firstTileCenter, new v2(x, y), new v2(0, 0), new v2(0, 0));
drawCircle(10, center.x, center.y, color)
}
}*/
}
}
function resetCombatState()
{
combat.active = false;
combat.playerStrikesFirst = true;
combat.surpriseAdvantage = 'none';
combat.bribe = 0;
combat.combatants = [];
combat.combatArenas = [];
combat.combatInitialized = false;
}
function processOption(optionMatrix)
{
if(optionMatrix != null)
{
var optionIndex = d6() - 1;
var selectedOption = optionMatrix[optionIndex];
if(selectedOption.length == 3)
{
combat.bribe = selectedOption[2];
}
setEvent(selectedOption[0], selectedOption[1]);
}
}
function getCharacter(name, combatSkill, endurance, wealth)
{
var result = {};
result.name = name;
result.combatSkill = combatSkill;
result.currentEndurance = endurance;
result.maxEndurance = endurance;
result.wealth = wealth;
result.dailyFee = 0;
result.advancePay = false;
result.sharePay = false;
return result;
}
function getDrawLocationFromTile(firstTileCenter, tilePosition, size, offset)
{
var result = v2Add(firstTileCenter, offset);
v2AddAssign(result, v2Hadamard(map.tileSize, tilePosition));
v2SubtractAssign(result, v2Multiply(size, 0.5));
if(!(tilePosition.x % 2 == 0))
{
result.y += 0.5 * map.tileSize.y;
}
return result;
}
function continueButtonPressed()
{
return keysPressed[ascii(" ")] || keysPressed[enterKeyCode] || mouseClicked;
}
function doButton(panel, text)
{
var result = false;
var style = 'default';
var size = new v2(context.measureText(text).width, lineHeight);
if(panel.AtX <= mousePosition.x && mousePosition.x <= (panel.AtX + size.x) &&
panel.AtY <= mousePosition.y && mousePosition.y <= (panel.AtY + size.y))
{