-
Notifications
You must be signed in to change notification settings - Fork 1
/
creature.py
237 lines (220 loc) · 7.24 KB
/
creature.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
import libtcodpy as libtcod
import math
DEAD_COLOR = libtcod.light_grey
class Creature:
def __init__(self, name, x, y, character, color, monster=False, ai=None, stats=None, hp=1, sightRadius=5, attackRange=1, armorClass=10):
self.name = name
self.x = x
self.y = y
self.character = character
self.color = color
self.monster = monster
self.ai = ai
self.blocks = True
self.isDead = False
if self.ai:
self.ai.owner = self
self.stats = stats
if self.stats:
self.stats.owner = self
self.hp = hp + self.stats.getConBonus()
self.armorClass = self.armorClass+self.stats.getDexBonus()
else:
self.stats = Stats()
self.stats.owner = self
self.hp = hp
self.armorClass = armorClass
self.sightRadius = sightRadius
self.attackRange = attackRange
self.hostileList = []
self.weapon = None
###
# setName
###
def setName(self, newName):
self.name = newName
###
# moveOrAttack
###
def moveOrAttack(self, dx, dy, theMap):
if theMap.theMap[self.x + dx][self.y + dy].blocked:
return False
else:
for creature in self.hostileList:
if creature.x == self.x + dx and creature.y == self.y + dy and not creature.isDead:
self.attack(creature, self.weapon)
return True
self.x = self.x + dx
self.y = self.y + dy
return True
###
# chase - move towards something
###
def chase(self, target_x, target_y, theMap):
dx = target_x - self.x
dy = target_y - self.y
#vector to find distance
distance = math.sqrt(dx ** 2 + dy ** 2)
#normalize, then round and convert
dx = int(round(dx / distance))
dy = int(round(dy / distance))
self.moveOrAttack(dx, dy, theMap)
###
# isHostile - determines if the creature is hostile to another creature
###
def isHostile(self, other):
for creature in self.hostileList:
if creature.name == other.name:
return True
return False
###
# makeHostile
###
def makeHostile(self, other):
for creature in self.hostileList:
if creature == other:
return
self.hostileList.append(other)
###
# makeFriendly
###
def makeFriendly(self, other):
for creature in hostileList:
if creature.name == other.name:
hostileList.remove(creture)
break
###
# isMonster
###
def isMonster(self):
return self.monster
###
# isAttackable
###
def canAttack(self, targetCreature):
if abs(self.x - targetCreature.x) > self.attackRange or abs(self.y - targetCreature.y) > self.attackRange:
return False
else:
return True
###
# attack - attack another creature.
###
def attack(self, targetCreature, weapon):
attack = libtcod.random_get_int(0, 1, 20)
damage = 0
if attack == 0:
#TODO: handle critical miss
pass
elif attack >= 20 - weapon.criticalThreat:
#TODO: handle critical hit
pass
else:
attack = attack + weapon.attackBonus + self.stats.getStrBonus()
if attack >= targetCreature.armorClass:
#hit!
damage=libtcod.random_get_int(0, weapon.minDamage, weapon.maxDamage)
damage=damage + self.stats.getStrBonus()
print ('The '+ self.name+' hits the '+ targetCreature.name+' for '+str(damage))
else:
#TODO: handle miss
pass
targetCreature.hp = targetCreature.hp - damage
if targetCreature.hp < 1:
targetCreature.blocks = False
targetCreature.isDead = True
targetCreature.color = DEAD_COLOR
print("The "+targetCreature.name+" died!")
###
# lookAround - return a list of all creatures in sight range
###
#FIXME: should be a circle instead of a square
def lookAround(self, creatures):
sightedCreatures = []
for creature in creatures:
if creature.x > self.x + self.sightRadius and creature.x < self.x - self.sightRadius:
continue
if creature.y > self.y + self.sightRadius and creature.y < self.y - self.sightRadius:
continue
sightedCreatures.append(creature)
return sightedCreatures
class Stats:
###
# DND Style!
###
def __init__(self, strength=10, dex=10, con=10, wis=10, intelligence=10, cha=10):
self.strength = strength
self.dex = dex
self.con = con
self.wis = wis
self.intelligence = intelligence
self.cha = cha
###
# get stat bonuses
###
def getStrBonus(self):
return int((self.strength - 10) / 2)
def getDexBonus(self):
return int((self.dex - 10) / 2)
def getConBonus(self):
return int((self.con - 10) / 2)
def getWizBonus(self):
return int((self.wis - 10) / 2)
def getIntBonus(self):
return int((self.intelligence - 10) / 2)
def getChaBonus(self):
return int((self.cha - 10) / 2)
###
# Run when player type classes level up
###
def levelUpFighter(self):
strength = strength + 2
dex = dex + 2
con = con + 2
wis = wis + 1
intelligence = intelligence + 1
cha = cha + 1
def levelUpRogue(self):
self.strength = self.strength + 1
self.dex = self.dex + 3
self.con = self.con + 1
self.wis = self.wis + 1
self.intelligence = self.intelligence + 1
self.cha = self.cha + 3
def levelUpCaster(self):
self.strength = self.strength + 1
self.dex = self.dex + 1
self.con = self.con + 1
self.wis = self.wis + 3
self.intelligence = self.intelligence + 3
self.cha = self.cha + 1
###
# default AI for monsters
###
class BasicMonster:
def __init__(self, theMap):
self.theMap = theMap
def takeTurn(self, sightedCreatures):
targetCreature = None
for creature in sightedCreatures:
if self.owner.isHostile(creature):
targetCreature = creature
if self.owner.canAttack(creature):
self.owner.attack(creature, self.owner.weapon)
return
else:
self.owner.chase(targetCreature.x, targetCreature.y, self.theMap)
return
#if targetCreature:
#if self.owner.canAttack(targetCreature):
# self.owner.attack(targetCreature, self.owner.weapon)
# return
#else:
# self.owner.chase(targetCreature.x, targetCreature.y)
# return
# no hostile creature found; wander around
#50% chance of not moving anywhere
if libtcod.random_get_int(0, 0, 100) > 50:
return
else:
self.owner.moveOrAttack(libtcod.random_get_int(0,0,1), libtcod.random_get_int(0,0,1), self.theMap)
return