-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwarhexer.py
3513 lines (2785 loc) · 106 KB
/
warhexer.py
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
# -*- coding: UTF-8 -*-
################################################################################
# #
# WarHexer: Roguelike Epic Fanasty Battles #
# #
################################################################################
# Built on Python 2.7.3 and libtcod 1.5.1
# Default Resolution: 1800 x 912
# Unit Portraits: 30 x 26 px, shown as 15 x 13 characters
##### Libraries #####
import libtcodpy as libtcod # roguelike library
import os # for an SDL window instruction
from math import sqrt, ceil # math functions
from math import atan2, degrees, pi # more math functions
from textwrap import wrap # for breaking up game messages
import shelve # for saving and loading
from random import shuffle # for shuffling lists of items (used in map generation)
#import time # for animation timing
##### Constants #####
VERSION = '0.1g' # arbitrary desgination
SCREEN_WIDTH = 166 # width of the game window in characters
SCREEN_HEIGHT = 57 # height "
MAP_WIDTH = 136 # width of map console in characters
MAP_HEIGHT = 70 # height "
BATTLE_CONSOLE_WIDTH = 70 # width of battle window console in characters
BATTLE_CONSOLE_HEIGHT = 18 # height "
CON_WIDTH = 43 # width of info console in characters
TERRAIN_CON_HEIGHT = 4 # height of terrain info window
STAT_CON_HEIGHT = 22 # height of unit stat console
MSG_CON_HEIGHT = 18 # height of message window
UNIT_WIDTH = 11 # width of unit sprite
UNIT_HEIGHT = 7 # height "
LIMIT_FPS = 30 # maximum frames-per-second displayed
# terrain type codes
OPEN_GROUND = 0
FOREST = 1
TOWN = 2
RUINS = 3
OPEN_GROUND_COLOR = libtcod.Color(0, 64, 0)
FOREST_COLOR = libtcod.Color(0, 28, 0)
ROAD_COLOR = libtcod.Color(32, 40, 32)
# Debug flags
FREE_AP = False
# change in hx, hy values for hexes in each direction
DESTHEX = [
(0, 1),
(1, 1),
(1, 0),
(0, -1),
(-1, -1),
(-1, 0)
]
################################################################################
##### Classes #####
################################################################################
# player: contains information on a particular player, current roster, items, experience, etc.
#class Player:
# def __init__(self):
# pass
# unit type: defines stats and abilities for a given type of unit, each spawned unit bases
# its stats off of its type
class UnitType:
def __init__(self, stats):
self.name = stats[0]
self.civ_num = stats[1]
self.rating = stats[2]
self.unit_class = stats[3]
self.unit_char = stats[4]
self.melee = stats[5]
self.ranged = stats[6]
self.attack_range = stats[7]
self.defense = stats[8]
self.skill = stats[9]
self.morale = stats[10]
self.columns = stats[11]
self.portrait_file = stats[12]
self.special = stats[13]
self.points_cost = stats[14]
self.description = stats[15]
# set up max AP based on unit rating and class
if self.unit_class == 'Artillery':
self.max_ap = 2
elif self.unit_class == 'Infantry':
self.max_ap = 3
elif self.unit_class == 'Cavalry':
if self.rating == 'Light':
self.max_ap = 5
else:
self.max_ap = 4
UNIT_CLASS_DEFS = [
# Name, civ_number, rating, type, character, melee, ranged, ranged range, defense,
# skill, morale, columns, portrait filename, special, points cost, description
# Human Kingdoms
['Hearthguard', 0, 'Heavy', 'Infantry', 'h', 8, 0, 0, 5, 9, 7, 7, 'human_hearthguard.png', ['Polearms'], 120,
'Well-equipped and well-trained elite infantry. They are armed with long halberds that give them an advantage against enemy cavalry.'],
['Irregulars', 0, 'Light', 'Infantry', 'i', 6, 0, 0, 5, 7, 7, 7, '', ['Shields', 'Mobility'], 100,
'Armed with one-handed weapons and shields, often used to harrass enemy forces and protect the flanks of heavier troops.'],
['Longbowmen', 0, 'Light', 'Infantry', 'a', 0, 7, 5, 4, 9, 7, 7, 'human_longbowmen.png', [], 110,
'Ranged troops equipped with the highly effective longbow.'],
['Knights', 0, 'Heavy', 'Cavalry', 'K', 9, 0, 0, 6, 10, 8, 5, 'human_knights.png', ['Shields', 'Charge'], 170,
'The elite cavalry of the kingdoms, powerful charge attack and highly motivated.'],
['Noble Riders', 0, 'Light', 'Cavalry', 'R', 8, 0, 0, 5, 8, 7, 5, '', ['Mobility'], 150,
'Younger sons of the noble families often ride to war together, eager to prove their bravery with sword and flail.'],
# Undead Lords
['Ghouls', 1, 'Heavy', 'Infantry', 'g', 8, 0, 0, 6, 8, 8, 7, 'undead_ghouls.png', ['Polearms'], 110, 'Test'],
['Skeletal Host', 1, 'Light', 'Infantry', 's', 7, 0, 0, 6, 8, 7, 7, '', ['Shields'], 90, 'Test'],
['Skeleton Archers', 1, 'Light', 'Infantry', 'a', 0, 6, 5, 4, 8, 7, 7, 'skeleton_archers.png', [], 100, 'Test'],
['Knightmares', 1, 'Heavy', 'Cavalry', 'K', 8, 0, 0, 7, 8, 8, 5, 'undead_knightmares.png', ['Charge'], 160, 'Test'],
['Vampire Lords', 1, 'Light', 'Cavalry', 'V', 9, 0, 0, 6, 7, 7, 5, '', ['Shields', 'Mobility'], 140, 'Test']
]
# battle object, keeps track of everything going on in the battle
class Battle:
def __init__(self):
self.map_hexes = [] # hex terrain
self.rivers = [] # coordinates of rivers (hx1, hy1, hx2, hy2)
self.roads = [] # coordinates of roads "
self.units = [] # units in the battle
self.melee_locks = [] # list of pairs of units locked in melee
self.messages = [] # list of game messages
self.selected = None # currently selected unit, if any
self.current_turn = 1 # current battle turn
self.turn_limit = 8 # at the end of this turn, the battle ends
self.active_player = 0 # currently active player
self.player0_score = 0 # TEMP: player 1's score
self.player1_score = 0 # " 2
# create a new melee lock between two enemy units
def CreateMeleeLock(self, obj1, obj2):
self.melee_locks.append((obj1, obj2))
# update unit flags and their stat consoles
for obj in [obj1, obj2]:
obj.melee_locked = True
obj.ApplyMods()
obj.UpdateStatConsole()
# returns true if the two units share a melee lock
def IsMeleeLocked(self, obj1, obj2):
if (obj1, obj2) in self.melee_locks or (obj2, obj1) in self.melee_locks:
return True
return False
# break any melee locks that this unit is in
def BreakLocks(self, obj):
for (obj1, obj2) in self.melee_locks[::-1]:
if obj1 == obj or obj2 == obj:
self.melee_locks.remove((obj1, obj2))
obj1.UpdateStatConsole()
obj2.UpdateStatConsole()
# reset unit melee lock flags
for obj in battle.units:
obj.melee_locked = False
for (obj1, obj2) in self.melee_locks:
if not obj1.melee_locked:
obj1.melee_locked = True
obj1.UpdateStatConsole()
if not obj2.melee_locked:
obj2.melee_locked = True
obj2.UpdateStatConsole()
# session object, holds stuff unique to the gaming session and not saved between games
class Session:
def __init__(self):
self.mouseover = (0, 0) # current mouse position on screen
# create the map console and battle window console
self.map_console = libtcod.console_new(MAP_WIDTH, MAP_HEIGHT)
self.battle_console = libtcod.console_new(BATTLE_CONSOLE_WIDTH, BATTLE_CONSOLE_HEIGHT)
# create terrain console
self.terrain_con = libtcod.console_new(CON_WIDTH-4, TERRAIN_CON_HEIGHT)
# create message console
self.msg_con = libtcod.console_new(CON_WIDTH-4, MSG_CON_HEIGHT)
# update the terrain console with info from hex
def UpdateTerrainCon(self, hx, hy):
libtcod.console_clear(self.terrain_con)
if not HexIsOnMap(hx, hy):
return
h = GetHexFromMap(hx, hy)
text = ''
if h.landmark_name is not None:
text += h.landmark_name + ', '
if h.terrain_type == OPEN_GROUND:
text += 'Open Ground'
elif h.terrain_type == FOREST:
text += 'Forest'
elif h.terrain_type == TOWN:
text += 'Town'
elif h.terrain_type == RUINS:
text += 'Ruins'
if h.river:
text += ', River'
if h.road:
text += ', Road'
libtcod.console_print_ex(self.terrain_con, (CON_WIDTH-4)/2, 0, libtcod.BKGND_NONE, libtcod.CENTER, text)
text = 'AP Cost: ' + str(h.move_cost)
libtcod.console_print(self.terrain_con, 0, 2, text)
text = 'Defense: '
if h.defense_mod == 0:
text += '-'
else:
if h.defense_mod > 0:
text += '+'
text += str(h.defense_mod)
libtcod.console_print_ex(self.terrain_con, CON_WIDTH-5, 2, libtcod.BKGND_NONE, libtcod.RIGHT, text)
# update the message console with game messages
# color not used for now
def UpdateMsgConsole(self):
libtcod.console_clear(self.msg_con)
y = 0
for (line, color) in battle.messages:
# fade out old messages
s = 255-((len(battle.messages) - y)*13)
shade = libtcod.Color(s, s, s)
libtcod.console_set_default_foreground(self.msg_con, shade)
libtcod.console_print(self.msg_con, 0, y, line)
y += 1
# records type of terrain in a given hex
class Hex:
def __init__(self, hx, hy, terrain_type):
self.hx = hx
self.hy = hy
self.road = False # no road by default
self.river = False # no river by default
self.landmark_name = None # no landmark name
self.higher_ground = False # not higher ground by default
self.terrain_type = terrain_type # terrain type code
# set hex terrain features
def SetTerrain(self):
if self.terrain_type == OPEN_GROUND:
self.color = OPEN_GROUND_COLOR
self.move_cost = 1
self.defense_mod = 0
elif self.terrain_type == FOREST:
self.color = FOREST_COLOR
self.move_cost = 2
self.defense_mod = 2
elif self.terrain_type == TOWN:
self.color = ROAD_COLOR
self.move_cost = 2
self.defense_mod = 1
elif self.terrain_type == RUINS:
self.color = OPEN_GROUND_COLOR
self.move_cost = 2
self.defense_mod = 1
# river in hex increases AP cost and decreases defense modifier
# road in hex cancels this
if self.river and not self.road:
self.move_cost += 1
self.defense_mod -= 1
# unit in the battle: infantry, cavalry, etc.
class Unit:
def __init__(self, name, player, hx, hy, facing):
self.name = name # unit type name
self.player = player # owning player number, 0-1
self.hx = hx # hex location
self.hy = hy
self.facing = facing # hex facing, 0 being up, increasing clockwise
self.broken = False # broken unit flag
self.attack_mod = 0 # total attack value modification
self.defense_mod = 0 # total defense "
self.melee_locked = False # flag to note if unit is melee locked
self.free_attempt = False # flag to note is unit has attempted to break
# from melee lock
self.x_offset = 0 # used for animating unit on the map
self.y_offset = 0 # "
self.ranks = 3 # " ranks
# find this unit's stats in the list of unit types
self.unit_type = None
for t in unit_classes:
if t.name == self.name:
self.unit_type = t
break
# if could not find unit type, break with error
if self.unit_type == None:
print 'ERROR: Could not find unit type: ' + self.name
return
# set up unit stats based on unit type
self.civ_num = self.unit_type.civ_num
self.rating = self.unit_type.rating
self.unit_class = self.unit_type.unit_class
self.unit_char = self.unit_type.unit_char
self.melee = self.unit_type.melee
self.ranged = self.unit_type.ranged
self.attack_range = self.unit_type.attack_range
self.defense = self.unit_type.defense
self.skill = self.unit_type.skill
self.morale = self.unit_type.morale
self.columns = self.unit_type.columns
self.portrait_file = self.unit_type.portrait_file
self.special = self.unit_type.special
self.max_ap = self.unit_type.max_ap
self.ap = self.max_ap
# set up fighter ranks and columns
self.max_fighters = self.columns * self.ranks
self.fighters = self.max_fighters
# number of fighters in each rank, int
self.rank_pop = list()
for r in range(self.ranks):
self.rank_pop.append(self.columns)
# record of current filled ranks
self.current_ranks = self.ranks
# set up unit consoles and sprite
self.SetupConsoles()
# select this unit
def SelectMe(self):
if battle.selected is not None:
battle.selected.DeselectMe()
battle.selected = self
self.DrawSprite()
# de-select this unit
def DeselectMe(self):
battle.selected = None
self.DrawSprite()
# set up consoles for new battle, after loading a game, or after saving a game
# also re-draws sprite
def SetupConsoles(self):
self.portrait = libtcod.console_new(15, 13)
self.LoadPortrait()
self.stat_console = libtcod.console_new(CON_WIDTH-4, STAT_CON_HEIGHT)
self.UpdateStatConsole()
self.sprite = libtcod.console_new(UNIT_WIDTH, UNIT_HEIGHT)
self.DrawSprite()
# draw the facing indicator for this unit to the given console
def DrawFacing(self, console, x, y, color):
# set color to player color
libtcod.console_set_default_foreground(console, color)
# draw facing indicator characters
if self.facing == 0:
libtcod.console_put_char(console, x-2, y-2, 30)
libtcod.console_put_char(console, x, y-2, 30)
libtcod.console_put_char(console, x+2, y-2, 30)
elif self.facing == 1:
libtcod.console_put_char(console, x+3, y-2, '/')
libtcod.console_put_char(console, x+4, y-1, '/')
libtcod.console_put_char(console, x+5, y, '/')
elif self.facing == 2:
libtcod.console_put_char(console, x+3, y+2, '\\')
libtcod.console_put_char(console, x+4, y+1, '\\')
libtcod.console_put_char(console, x+5, y, '\\')
elif self.facing == 3:
libtcod.console_put_char(console, x-2, y+2, 31)
libtcod.console_put_char(console, x, y+2, 31)
libtcod.console_put_char(console, x+2, y+2, 31)
elif self.facing == 4:
libtcod.console_put_char(console, x-3, y+2, '/')
libtcod.console_put_char(console, x-4, y+1, '/')
libtcod.console_put_char(console, x-5, y, '/')
elif self.facing == 5:
libtcod.console_put_char(console, x-3, y-2, '\\')
libtcod.console_put_char(console, x-4, y-1, '\\')
libtcod.console_put_char(console, x-5, y, '\\')
# reset console color
libtcod.console_set_default_foreground(console, libtcod.white)
# draw or re-draw the unit sprite to its sprite console
def DrawSprite(self):
libtcod.console_clear(self.sprite)
if battle.selected == self:
fg = libtcod.white
else:
if self.player == 0:
if self.broken:
fg = libtcod.dark_azure
else:
fg = libtcod.azure
else:
if self.broken:
fg = libtcod.dark_flame
else:
fg = libtcod.flame
libtcod.console_set_default_foreground(self.sprite, fg)
# determine if unit is facing up or down
up = False
if self.facing == 0 or self.facing == 1 or self.facing == 5:
up = True
# draw ranks from front to back
if up:
y1 = 2
ys = 1
else:
y1 = 4
ys = -1
x = int(UNIT_WIDTH/2)
# grab each number of fighters in each rank and draw them
for rank_pop in self.rank_pop:
text = self.unit_char * rank_pop
libtcod.console_print_ex(self.sprite, x, y1, libtcod.BKGND_NONE, libtcod.CENTER, text)
y1 += ys
# draw stats with location based on facing
if up:
dy = 5
else:
dy = 1
if self.melee > 0:
text = str(self.melee + self.attack_mod)
elif self.ranged > 0:
text = str(self.ranged + self.attack_mod)
else:
text = '0'
text += ' - ' + str(self.defense + self.defense_mod)
libtcod.console_print_ex(self.sprite, x, dy, libtcod.BKGND_NONE, libtcod.CENTER, text)
# draw broken indicator if applicable
if self.broken:
libtcod.console_set_default_foreground(self.sprite, libtcod.dark_red)
libtcod.console_put_char(self.sprite, x, dy, 'B', flag=libtcod.BKGND_NONE)
libtcod.console_set_default_foreground(self.sprite, libtcod.white)
# draw facing indicator
self.DrawFacing(self.sprite, x, 3, fg)
# reset unit for new turn
def Reset(self):
self.ap = self.max_ap # replenish action points
self.free_attempt = False # reset break attempt flag
self.UpdateStatConsole()
# remove from game
def DestroyMe(self):
Message(self.name + ' has been destroyed!')
battle.BreakLocks(self) # remove self from any melee locks
battle.units.remove(self) # remove self from list of active units
if battle.selected == self: # deselect if was selected
self.DeselectMe()
# TODO award points to opponent
#if self.unit_class == 'Infantry':
# value = 1
#else:
# value = 2
#if self.player == 0:
# battle.player1_score += value
#else:
# battle.player0_score += value
# calculate total active attack and defense modifiers
def ApplyMods(self):
self.attack_mod = 0
# terrain defense bonus
self.defense_mod = GetHexFromMap(self.hx, self.hy).defense_mod
# rank modifiers
if self.current_ranks < self.ranks:
self.attack_mod -= 1
if self.current_ranks == 1:
self.defense_mod -= 1
# melee lock modifiers
if 'Mobility' not in self.special:
num_locks = 0
for (obj1, obj2) in battle.melee_locks:
if obj1 == self or obj2 == self:
num_locks += 1
if num_locks > 1:
self.defense_mod -= num_locks - 1
# TEMP: constrain defense modifier so that defense is at least 2
if self.defense - self.defense_mod < 2:
self.defense_mod = 2 - self.defense
self.UpdateStatConsole()
self.DrawSprite()
# draw a representation of the unit to the console
def DrawMe(self, console):
# get top left of location on screen
# if we're animating, use the offset location instead
if self.x_offset > 0 or self.y_offset > 0:
x, y = self.x_offset, self.y_offset
else:
x, y = Hex2Screen(self.hx, self.hy)
# blit sprite to screen with background alpha
libtcod.console_blit(self.sprite, 0, 0, UNIT_WIDTH, UNIT_HEIGHT, console, x+3, y+4, 1.0, 0.0)
# load unit portrait and blit to portrait console
def LoadPortrait(self):
# fill in placeholder if no portrait
if self.portrait_file == '':
libtcod.console_set_default_background(self.portrait, libtcod.grey)
libtcod.console_clear(self.portrait)
libtcod.console_print_ex(self.portrait, 7, 4, libtcod.BKGND_NONE, libtcod.CENTER, 'Portrait')
libtcod.console_print_ex(self.portrait, 7, 5, libtcod.BKGND_NONE, libtcod.CENTER, 'will go here')
return
# load portrait from file and blit to portrait console
temp = libtcod.image_load(self.portrait_file)
libtcod.image_blit_2x(temp, self.portrait, 0, 0)
del temp
# update info in stat console
def UpdateStatConsole(self):
libtcod.console_clear(self.stat_console)
y = 0
libtcod.console_print_ex(self.stat_console, (CON_WIDTH-4)/2, y, libtcod.BKGND_NONE, libtcod.CENTER, self.name)
y += 1
if self.civ_num == 0:
text = 'Human '
else:
text = 'Undead '
text += self.rating + ' ' + self.unit_class
libtcod.console_print_ex(self.stat_console, (CON_WIDTH-4)/2, y+1, libtcod.BKGND_NONE, libtcod.CENTER, text)
# broken status
if self.broken:
libtcod.console_set_default_foreground(self.stat_console, libtcod.red)
libtcod.console_print_ex(self.stat_console, (CON_WIDTH-4)/2, y+2, libtcod.BKGND_NONE, libtcod.CENTER, 'BROKEN')
libtcod.console_set_default_foreground(self.stat_console, libtcod.white)
# stats
# melee value
libtcod.console_print(self.stat_console, 1, y+3, 'Melee: ')
if self.melee < 1:
text = '-'
else:
melee = self.melee
# positive or negative bonus applies
if self.attack_mod > 0:
melee += self.attack_mod
libtcod.console_set_default_foreground(self.stat_console, libtcod.green)
elif self.attack_mod < 0:
melee += self.attack_mod
libtcod.console_set_default_foreground(self.stat_console, libtcod.red)
text = str(melee)
libtcod.console_print(self.stat_console, 10, y+3, text)
libtcod.console_set_default_foreground(self.stat_console, libtcod.white)
# ranged value
libtcod.console_print(self.stat_console, 1, y+4, 'Ranged: ')
# original stat of 0 means no ranged attack
if self.ranged == 0:
libtcod.console_print(self.stat_console, 10, y+4, '-')
else:
ranged = self.ranged
if self.attack_mod > 0:
ranged += self.attack_mod
libtcod.console_set_default_foreground(self.stat_console, libtcod.green)
elif self.attack_mod < 0:
ranged += self.attack_mod
libtcod.console_set_default_foreground(self.stat_console, libtcod.red)
libtcod.console_print(self.stat_console, 10, y+4, str(ranged))
libtcod.console_set_default_foreground(self.stat_console, libtcod.white)
attack_range = self.attack_range
libtcod.console_print(self.stat_console, 12, y+4, '(' + str(attack_range) + ')')
# defense value
libtcod.console_print(self.stat_console, 1, y+5, 'Defense: ')
defense = self.defense
# positive or negative bonus applies
if self.defense_mod > 0:
defense += self.defense_mod
libtcod.console_set_default_foreground(self.stat_console, libtcod.green)
elif self.defense_mod < 0:
defense += self.defense_mod
libtcod.console_set_default_foreground(self.stat_console, libtcod.red)
libtcod.console_print(self.stat_console, 10, y+5, str(defense))
libtcod.console_set_default_foreground(self.stat_console, libtcod.white)
# skill value
libtcod.console_print(self.stat_console, 1, y+6, 'Skill: ')
libtcod.console_print(self.stat_console, 10, y+6, str(self.skill))
# morale value
libtcod.console_print(self.stat_console, 1, y+7, 'Morale: ')
libtcod.console_print(self.stat_console, 10, y+7, str(self.morale))
# AP, only show if not broken
if self.player == battle.active_player and not self.broken:
libtcod.console_print(self.stat_console, 1, y+9, 'Action Points:')
DrawBar(self.stat_console, self.ap, self.max_ap, libtcod.green, libtcod.darker_green, 1, y+10, 20)
# total fighters
libtcod.console_print(self.stat_console, 1, y+11, 'Fighters:')
DrawBar(self.stat_console, self.fighters, self.max_fighters, libtcod.red, libtcod.darker_red, 1, y+12, 20)
# display if melee locked
if self.melee_locked:
libtcod.console_set_default_foreground(self.stat_console, libtcod.light_red)
libtcod.console_print(self.stat_console, 1, y+14, 'Melee Locked')
libtcod.console_set_default_foreground(self.stat_console, libtcod.white)
if self.player == battle.active_player and not self.broken and not self.free_attempt:
libtcod.console_print(self.stat_console, 1, y+15, 'Attempt to [F]ree')
# list specials
if len(self.special) > 0:
y = y+18
for l in self.special:
libtcod.console_print(self.stat_console, 1, y, l)
y += 1
# blit unit portrait to stat console
libtcod.console_print_frame(self.stat_console, CON_WIDTH-21, 4, 17, 15)
libtcod.console_blit(self.portrait, 0, 0, 15, 13, self.stat_console, CON_WIDTH-20, 5)
# attempt to spend ap, returns false if not enough remaining
def SpendAP(self, ap_cost):
if self.ap >= ap_cost:
if not FREE_AP: self.ap -= ap_cost
self.UpdateStatConsole()
return True
return False
# changes facing and keeps within 0-5 hexside system
def GetFacing(self, new_facing):
if new_facing < 0:
new_facing += 6
elif new_facing > 5:
new_facing -= 6
return new_facing
# pivot unit in place
def ChangeFacing(self, change):
if self.broken:
Message(self.name + ' is broken.')
return
# check for melee lock
if self.melee_locked:
Message(self.name + ' is melee locked.')
return
self.facing = self.GetFacing(self.facing + change)
self.DrawSprite()
# attempt to move forward one hex
def MoveForward(self):
if self.broken:
Message(self.name + ' is broken.')
return
# check for melee lock
if self.melee_locked:
Message(self.name + ' is melee locked.')
return
hx2, hy2 = GetHexInDir(self.hx, self.hy, self.facing)
# check to see that destination hex is on the game map
if not HexIsOnMap(hx2, hy2):
return
# check to see if there's a unit in the destination hex
for obj in battle.units:
if obj.hx == hx2 and obj.hy == hy2:
if obj.player == self.player:
# try to initiate unit swap
self.UnitSwap(obj)
return
else:
Message('Enemy unit in target hex')
return
# get AP cost to move into hex
cost = GetMoveCost(self, hx2, hy2)
# check to see that we have enough AP remaining
if not self.SpendAP(cost):
Message('Not enough AP to move (' + str(cost) +' required)')
return
# animate into new hex
x1, y1 = Hex2Screen(self.hx, self.hy)
x2, y2 = Hex2Screen(hx2, hy2)
points = GetLine(x1, y1, x2, y2)
for (x, y) in points:
self.x_offset = x
self.y_offset = y
RenderAll()
self.x_offset = 0
self.y_offset = 0
# move into new hex
self.hx = hx2
self.hy = hy2
# apply modifiers of new hex location
self.ApplyMods()
# try to swap positions with a friendly unit
def UnitSwap(self, obj):
if obj.broken:
Message('Target platoon is Broken.')
return
if obj.melee_locked:
Message('Target platoon is in melee combat.')
return
# check that both units have enough AP
cost1 = GetMoveCost(self, obj.hx, obj.hy)
if self.ap < cost1:
Message('Not enough AP to move (' + str(cost1) +' required)')
return
cost2 = GetMoveCost(obj, self.hx, self.hy)
if obj.ap < cost2:
Message('Target platoon has insufficent AP to attempt swap (' + str(cost2) +' required)')
return
text = 'Attempt position swap with ' + obj.name + '?'
if not GetYNWindow(text):
Message('Swap canceled')
return
# TODO: require a skill test if any enemy units are adjacent to either hex
#d1, d2 = Roll2D6()
#Message('Skill roll is ' + str(d1) + ',' + str(d2))
#if d1+d2 > self.skill:
# Message('Skill roll unsuccessful')
# self.SpendAP(cost1)
# return
Message('Swap successful!')
# swap the units
self.hx, obj.hx = obj.hx, self.hx
self.hy, obj.hy = obj.hy, self.hy
self.SpendAP(cost1) # also update stat consoles
obj.SpendAP(cost2)
# attempt to move along a path to a destination
# does not spend any AP
def MovePath(self, hx2, hy2, freemove=False):
path, cost = GetPath(self, self.hx, self.hy, hx2, hy2)
if cost < 1:
Message('Error: No path possible!')
return False
else:
if not freemove:
if not self.SpendAP(cost):
return False
for (hx, hy) in path:
self.facing = GetDirToHex(self.hx, self.hy, hx, hy)
self.DrawSprite() # in case we turned
# animate into new hex
x1, y1 = Hex2Screen(self.hx, self.hy)
x2, y2 = Hex2Screen(hx, hy)
points = GetLine(x1, y1, x2, y2)
for (x, y) in points:
self.x_offset = x
self.y_offset = y
RenderAll()
self.x_offset = 0
self.y_offset = 0
# move into new hex
self.hx = hx
self.hy = hy
RenderAll()
return True
# initiate an attack on enemy unit in hex
def InitAttack(self, hx, hy):
if self.broken: return
# get the unit in the target hex
for obj in battle.units:
if obj.hx == hx and obj.hy == hy:
# don't target self!
if obj == self:
return
# don't target allies
if obj.player == self.player:
return
# if target is not adjacent or attacker has no melee
# attack, do a ranged attack
if self.melee < 1 or GetHexDistance(self.hx, self.hy, obj.hx, obj.hy) > 1:
self.RangedAttack(obj)
return
else:
self.MeleeAttack(obj)
return
# start a melee attack on enemy unit
def MeleeAttack(self, obj):
# see if we are already melee locked
if self.melee_locked:
# make sure we're attacking an enemy with whom we are locked
if not battle.IsMeleeLocked(self, obj):
Message('Target not part of melee combat, cannot attack.')
return
# check AP
if not self.SpendAP(1):
Message('Not enough AP to attack (1 required)')
return
# check for existing melee lock and create a new one if none exists
turn_to_face = False
charge_bonus = False
if not battle.IsMeleeLocked(self, obj):
# if defender is not already involved in melee, it will turn to
# face this attacker after the first attack
if not obj.melee_locked:
turn_to_face = True
battle.CreateMeleeLock(self, obj)
# possible charge bonus
if 'Charge' in self.special:
defense_mod = GetHexFromMap(obj.hx, obj.hy).defense_mod
if defense_mod <= 0 and 'Polearms' not in obj.special:
Message('Charge bonus!') # TEMP
charge_bonus = True
# possible polearm bonus
if 'Polearms' in self.special:
if obj.unit_class == 'Cavalry':
Message('Polearm bonus!') # TEMP
charge_bonus = True
# turn attacker to face target
self.facing = GetDirToHex(self.hx, self.hy, obj.hx, obj.hy)
self.DrawSprite()
# show melee attack message
Message(self.name + ' attacks ' + obj.name)
RenderAll()
libtcod.sys_sleep_milli(400)
# do attack, get number of hits on enemy, counter flag, and morale test flag
hits, counter, def_morale_test = self.Attack(obj, charge=charge_bonus)
# apply any damage
obj.TakeHits(hits)
own_hits = 0
# if a counterattack was triggered, work it out now
if counter:
Message(obj.name + ' counterattacks!')
own_hits, null, att_morale_test = obj.Attack(self, counter=True)
self.TakeHits(own_hits)
else:
att_morale_test = False
# pause to show casualties
if hits > 0 or own_hits > 0:
RenderAll()
libtcod.sys_sleep_milli(600)
# do any break tests and remove dead units
if hits > 0:
obj.UnitCheck()
if own_hits > 0:
self.UnitCheck()
# if target was broken, return
if obj.broken: return
# if target is ranged and took at least one hit, it must fall back
# ranged targets can't counter attack so attacker won't have to take
# hits
if hits > 0 and self.melee > 0 and obj.ranged > 0 and obj in battle.units:
Message(obj.name + ' must fall back.')
obj.FallBackTest(self, auto=True)
else:
# work out morale tests to fall back if any;
# can't be both since counterattack needs a win
if def_morale_test:
obj.FallBackTest(self)
elif att_morale_test:
self.FallBackTest(obj)
# if defender is still adjacent to attacker, they may turn to face them
if GetHexDistance(self.hx, self.hy, obj.hx, obj.hy) == 1 and turn_to_face:
obj.facing = GetDirToHex(obj.hx, obj.hy, self.hx, self.hy)
obj.DrawSprite()
# try to do a ranged attack on target unit
def RangedAttack(self, obj):
# no ranged attack
if self.ranged < 1:
Message('Attacker does not have a ranged attack')
return
# if we're melee locked, make sure we're attacking an enemy with whom we are locked
if self.melee_locked and not battle.IsMeleeLocked(self, obj):
Message('Target not part of melee combat, cannot attack.')
return
# get range to target
dist = GetHexDistance(self.hx, self.hy, obj.hx, obj.hy)
if dist > self.attack_range:
Message('Target is out of range (' + str(dist) + ' hexes)')
return
# check if target is melee locked and not with us
half_hits = False
if obj.melee_locked and not battle.IsMeleeLocked(self, obj):
Message('Target in melee, hits halved.')
half_hits = True
# display and check LoS