forked from Naburimannu/libtcodpy-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.py
266 lines (216 loc) · 7.24 KB
/
actions.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
"""
Implementation of actions.
Includes those which might be used by the AI (movement and combat)
and those which are currently only offered to the player.
Magical effects and targeting (spells.py) could also live here.
Conditionals and interfaces for the player sit up top in roguelike.py.
"""
import libtcodpy as libtcod
import copy
import log
import algebra
from components import *
def move(o, direction):
"""
Moves object by (dx, dy).
Returns true if move succeeded.
"""
goal = o.pos + direction
if not o.current_map.is_blocked_at(goal):
o.pos = goal
return True
return False
def move_towards(o, target_pos):
"""
Moves object one step towards target location.
Returns true if move succeeded.
"""
dir = algebra.Direction(target_pos.x - o.x, target_pos.y - o.y)
dir.normalize()
return move(o, dir)
def attack(fighter, target, report=True):
"""
A simple formula for attack damage.
"""
damage = fighter.power - target.fighter.defense
if damage > 0:
if report:
log.message(
fighter.owner.name.capitalize() + ' attacks ' + target.name +
' for ' + str(damage) + ' hit points.')
inflict_damage(fighter.owner, target.fighter, damage)
elif report:
log.message(
fighter.owner.name.capitalize() + ' attacks ' + target.name +
' but it has no effect!')
def inflict_damage(actor, fighter, damage):
"""
Apply damage.
"""
if damage > 0:
fighter.hp -= damage
if fighter.hp <= 0:
function = fighter.death_function
if function is not None:
function(fighter.owner)
actor.fighter.xp += fighter.xp
def heal(fighter, amount):
"""
Heal by the given amount, without going over the maximum.
"""
fighter.hp += amount
if fighter.hp > fighter.max_hp:
fighter.hp = fighter.max_hp
def pick_up(actor, o, report=True):
"""
Add an Object to the actor's inventory and remove from the map.
"""
for p in actor.inventory:
if o.item.can_combine(p):
p.item.count += o.item.count
actor.current_map.objects.remove(o)
if report:
log.message(actor.name.capitalize() + ' picked up a ' + o.name + '!', libtcod.green)
return True
if len(actor.inventory) >= 22:
if report:
log.message(actor.name.capitalize() + ' inventory is full, cannot pick up ' +
o.name + '.', libtcod.red)
return False
else:
actor.inventory.append(o)
actor.current_map.objects.remove(o)
if report:
log.message(actor.name.capitalize() + ' picked up a ' + o.name + '!', libtcod.green)
# Special case: automatically equip if the corresponding equipment slot is unused.
equipment = o.equipment
if equipment and _get_equipped_in_slot(actor, equipment.slot) is None:
equip(actor, equipment)
return True
def drop(actor, o, report=True):
"""
Remove an Object from the actor's inventory and add it to the map
at the player's coordinates.
If it's equipment, dequip before dropping.
"""
must_split = False
if o.item.count > 1:
o.item.count -= 1
must_split = True
else:
if o.equipment:
dequip(actor, o.equipment, True)
actor.inventory.remove(o)
combined = False
for p in actor.current_map.objects:
if p.pos == actor.pos and o.item.can_combine(p):
p.item.count += 1
combined = True
break
if not combined:
new_o = o
if must_split:
new_o = copy.deepcopy(o)
new_o.item.count = 1
new_o.pos = actor.pos
actor.current_map.objects.append(new_o)
if report:
log.message(actor.name.capitalize() + ' dropped a ' + o.name + '.', libtcod.yellow)
def use(actor, o, report=True):
"""
If the object has the Equipment component, toggle equip/dequip.
Otherwise invoke its use_function and (if not cancelled) destroy it.
"""
if o.equipment:
_toggle_equip(actor, o.equipment, report)
return
if o.item.use_function is None:
if report:
log.message('The ' + o.name + ' cannot be used.')
else:
if o.item.use_function(actor) != 'cancelled':
if o.item.count > 1:
o.item.count -= 1
else:
actor.inventory.remove(o)
def _toggle_equip(actor, eqp, report=True):
if eqp.is_equipped:
dequip(actor, eqp, report)
else:
equip(actor, eqp, report)
def equip(actor, eqp, report=True):
"""
Equip the object (and log unless report=False).
Ensure only one object per slot.
"""
old_equipment = _get_equipped_in_slot(actor, eqp.slot)
if old_equipment is not None:
dequip(actor, old_equipment, report)
eqp.is_equipped = True
if report:
log.message('Equipped ' + eqp.owner.name + ' on ' + eqp.slot + '.', libtcod.light_green)
def dequip(actor, eqp, report=True):
"""
Dequip the object (and log).
"""
if not eqp.is_equipped:
return
eqp.is_equipped = False
if report:
log.message('Dequipped ' + eqp.owner.name + ' from ' + eqp.slot + '.', libtcod.light_yellow)
def _get_equipped_in_slot(actor, slot):
"""
Returns Equipment in a slot, or None.
"""
if hasattr(actor, 'inventory'):
for obj in actor.inventory:
if obj.equipment and obj.equipment.slot == slot and obj.equipment.is_equipped:
return obj.equipment
return None
class _MockMap(object):
def is_blocked_at(self, pos):
return False
def _test_move():
o = Object(algebra.Location(0, 0), 'o', 'test object', libtcod.white)
o.current_map = _MockMap()
assert o.pos == algebra.Location(0, 0)
move(o, algebra.south)
assert o.pos == algebra.Location(0, 1)
move(o, algebra.southeast)
assert o.pos == algebra.Location(1, 2)
def _test_move_towards():
o = Object(algebra.Location(0, 0), 'o', 'test object', libtcod.white)
o.current_map = _MockMap()
assert o.pos == algebra.Location(0, 0)
move_towards(o, algebra.Location(10, 10))
assert o.pos == algebra.Location(1, 1)
move_towards(o, algebra.Location(10, 10))
assert o.pos == algebra.Location(2, 2)
move_towards(o, algebra.Location(-10, 2))
assert o.pos == algebra.Location(1, 2)
move_towards(o, o.pos)
assert o.pos == algebra.Location(1, 2)
def _test_attack():
af = Fighter(100, 0, 10, 0)
df = Fighter(100, 0, 0, 0)
a = Object(algebra.Location(0, 0), 'a', 'test attacker', libtcod.white, fighter=af)
d = Object(algebra.Location(1, 1), 'd', 'test defender', libtcod.white, fighter=df)
assert af.hp == 100
assert df.hp == 100
# if defense == 0, full damage is done
attack(af, d, False)
assert df.hp == 90
df.base_defense = 5
attack(af, d, False)
assert df.hp == 85
# if defense > attack, no damage is done
df.base_defense = 15
attack(af, d, False)
assert df.hp == 85
def _test_actions():
_test_move()
_test_move_towards()
_test_attack()
if __name__ == '__main__':
_test_actions()
print('Action tests complete.')