X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=plomrogue%2Ftasks.py;h=10e4055e4c3799745757c6f3a8bf9d001eb0d49c;hb=1a8eb9fa4e8dfaae9a0508882f49575ab68fc45c;hp=9335aeddb0df450953a4ffb01bf4710142c9c150;hpb=d9c9b5b7d5cac2469ac075010c4d729e1adf0cc4;p=plomrogue2 diff --git a/plomrogue/tasks.py b/plomrogue/tasks.py index 9335aed..10e4055 100644 --- a/plomrogue/tasks.py +++ b/plomrogue/tasks.py @@ -1,23 +1,28 @@ from plomrogue.errors import PlayError, GameError -from plomrogue.mapping import YX +from plomrogue.misc import quote class Task: argtypes = '' - todo = 3 + todo = 1 def __init__(self, thing, args=()): self.thing = thing self.args = args + def _get_move_target(self): + if self.args[0] == 'HERE': + return self.thing.position + return self.thing.game.map_geometry.move_yxyx(self.thing.position, + self.args[0]) + def check(self): pass class Task_WAIT(Task): - todo = 1 def do(self): pass @@ -25,30 +30,58 @@ class Task_WAIT(Task): class Task_MOVE(Task): - todo = 1 argtypes = 'string:direction' - def get_move_target(self): - return self.thing.game.map_geometry.move_yxyx(self.thing.position, - self.args[0]) - def check(self): - test_yxyx = self.get_move_target() + test_yxyx = self._get_move_target() + move_blockers = self.thing.game.get_movement_blockers() if test_yxyx in [t.position for t in self.thing.game.things - if t.blocking]: + if t.blocks_movement]: raise PlayError('blocked by other thing') - elif self.thing.game.maps[test_yxyx[0]][test_yxyx[1]] != '.': + elif self.thing.game.maps[test_yxyx[0]][test_yxyx[1]] in move_blockers: raise PlayError('blocked by impassable tile') def do(self): - self.thing.position = self.get_move_target() + if self.thing.type_ == 'Player' and not self.thing.standing: + self.thing.standing = True + self.thing.send_msg('CHAT "You get up."') + self.thing.game.record_change(self.thing.position, 'other') + if self.thing.blocks_light: + self.thing.game.record_change(self.thing.position, 'fov') + self.thing.position = self._get_move_target() + for t in [t for t in self.thing.game.things + if t.type_ == 'Player' and not t == self.thing + and t.position == self.thing.position]: + self.thing.send_msg('CHAT %s' % + quote('You get awkwardly close to %s.' % t.name)) + for c_id in self.thing.game.sessions: + if self.thing.game.sessions[c_id]['thing_id'] == t.id_: + t.send_msg('CHAT %s' % + quote('%s gets awkwardly close to you.' % + self.thing.name)) + break + self.thing.game.record_change(self.thing.position, 'other') + terrain = \ + self.thing.game.maps[self.thing.position[0]][self.thing.position[1]] + if terrain in self.thing.game.terrains: + terrain_type = self.thing.game.terrains[terrain] + if 'sittable' in terrain_type.tags: + self.thing.standing = False + self.thing.send_msg('CHAT "You sink into the %s. ' + 'Staying here will replenish your energy."' + % terrain_type.description) + self.thing.invalidate('fov') + if self.thing.blocks_light: + self.thing.game.record_change(self.thing.position, 'fov') if self.thing.carrying: self.thing.carrying.position = self.thing.position + if self.thing.carrying.type_ == 'Crate': + for t in self.thing.carrying.content: + t.position = self.thing.position class Task_WRITE(Task): - todo = 1 argtypes = 'string:char string' def check(self): @@ -60,53 +93,298 @@ class Task_WRITE(Task): big_yx = self.thing.position[0] little_yx = self.thing.position[1] self.thing.game.maps[big_yx][little_yx] = self.args[0] + self.thing.game.record_change((big_yx, little_yx), 'fov') class Task_FLATTEN_SURROUNDINGS(Task): - todo = 10 argtypes = 'string' def check(self): pass def do(self): - for yxyx in[self.thing.position] + \ - list(self.thing.game.map_geometry.get_neighbors_yxyx(self.thing.position).values()): + for yxyx in [self.thing.position] + \ + list(self.thing.game.map_geometry.get_neighbors_yxyx( + self.thing.position).values()): if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]): continue - self.thing.game.maps[yxyx[0]][yxyx[1]] = '.' + self.thing.game.maps[yxyx[0]][yxyx[1]] = self.thing.game.get_flatland() + self.thing.game.record_change(yxyx, 'fov') class Task_PICK_UP(Task): - todo = 1 + argtypes = 'int:pos' def check(self): if self.thing.carrying: raise PlayError('already carrying something') - nothing_to_pick_up = True - for t in [t for t in self.thing.game.things - if t != self.thing and t.position == self.thing.position - and t.type_ != 'Player']: - nothing_to_pick_up = False - break - if nothing_to_pick_up: - raise PlayError('nothing to pick up') + to_pick_up = self.thing.game.get_thing(self.args[0]) + neighbors = self.thing.game.map_geometry.\ + get_neighbors_yxyx(self.thing.position).values() + reach = [self.thing.position] + list(neighbors) + if to_pick_up is None: + raise PlayError('no such thing exists') + elif to_pick_up == self.thing: + raise PlayError('cannot pick up oneself') + elif to_pick_up.type_ == 'Player': + raise PlayError('cannot pick up player') + elif to_pick_up.carried: + raise PlayError('thing already carried by a player') + elif to_pick_up.position not in reach: + raise PlayError('thing not in reach') + elif not to_pick_up.portable: + raise PlayError('thing not portable') def do(self): - to_pick_up = [t for t in self.thing.game.things - if t != self.thing and t.position == self.thing.position][0] + to_pick_up = self.thing.game.get_thing(self.args[0]) + to_pick_up.position = self.thing.position[:] self.thing.carrying = to_pick_up + to_pick_up.carried = True + for t in [t for t in self.thing.game.things + if t.type_ == 'Crate' and to_pick_up in t.content]: + t.remove_from_crate(to_pick_up) + self.thing.send_msg('CHAT "You take the item out of the crate."') + break + self.thing.game.record_change(self.thing.position, 'other') class Task_DROP(Task): - todo = 1 + argtypes = 'string:direction+here' def check(self): if not self.thing.carrying: raise PlayError('nothing to drop') + target_position = self._get_move_target() + targets = [t for t in self.thing.game.things + if t.position == target_position + and not t == self.thing.carrying] + for target in targets: + if target.type_ == 'CookieSpawner' and\ + not self.thing.carrying.cookable: + raise PlayError('cannot cook items of this type') + elif target.type_ == 'BottleDeposit': + if not self.thing.carrying.type_ == 'Bottle': + raise PlayError('cannot only put bottle into bottle deposit') + if self.thing.carrying.full: + raise PlayError('cannot drop full ' + 'bottle into bottle deposit') + elif target.type_ == 'Crate' and\ + self.thing.carrying.type_ == 'Crate': + raise PlayError('cannot put crate into crate') + + def do(self): + target_position = self._get_move_target() + dropped = self.thing.uncarry() + dropped.position = target_position + if dropped.type_ == 'Crate': + for item in dropped.content: + item.position = target_position + targets = [t for t in self.thing.game.things + if t.position == dropped.position and not t == dropped] + for target in targets: + if target.type_ == 'CookieSpawner': + target.accept(dropped) + self.thing.game.remove_thing(dropped) + break + elif target.type_ == 'BottleDeposit': + target.accept() + self.thing.game.remove_thing(dropped) + break + elif target.type_ == 'Crate': + target.accept(dropped) + self.thing.send_msg('CHAT "You put the item into the crate."') + break + elif target.type_ == 'HatRemixer': + t.accept(dropped) + break + self.thing.game.record_change(self.thing.position, 'other') + + + +class Task_DOOR(Task): + + def check(self): + action_radius = list(self.thing.game.map_geometry. + get_neighbors_yxyx(self.thing.position).values()) + reachable_doors = [t for t in self.thing.game.things if + t.type_ == 'Door' and t.position in action_radius] + if len(reachable_doors) == 0: + raise PlayError('not standing next to a door to open/close') + for door in reachable_doors: + if not door.blocks_movement: + return + if not door.locked: + return + if self.thing.carrying and self.thing.carrying.type_ == 'DoorKey'\ + and self.thing.carrying.door == door: + return + raise PlayError('cannot open locked door without its key') + + def do(self): + action_radius = list(self.thing.game.map_geometry. + get_neighbors_yxyx(self.thing.position).values()) + for t in [t for t in self.thing.game.things if + t.type_ == 'Door' and t.position in action_radius]: + if t.blocks_movement: + t.open() + else: + t.close() + if self.thing.carrying and\ + self.thing.carrying.type_ == 'DoorKey' and\ + self.thing.carrying.door == t: + self.thing.send_msg('CHAT "You lock the door."') + t.lock() + self.thing.game.record_change(t.position, 'other') + self.thing.game.record_change(t.position, 'fov') + + + +class Task_INTOXICATE(Task): + + def check(self): + if self.thing.carrying is None: + raise PlayError('carrying nothing to consume') + if not self.thing.carrying.consumable: + raise PlayError('cannot consume this kind of thing') + if self.thing.carrying.type_ == 'Bottle' and\ + not self.thing.carrying.full: + raise PlayError('bottle is empty') + + def do(self): + if self.thing.carrying.type_ == 'Bottle': + self.thing.carrying.full = False + self.thing.carrying.empty() + self.thing.send_msg('CHAT "You are drunk now."') + self.thing.need_for_toilet += 1 + self.thing.drunk += 10000 + self.thing.invalidate('fov') + self.thing.game.record_change(self.thing.position, 'other') + elif self.thing.carrying.type_ == 'Psychedelic': + self.thing.tripping += 10000 + self.thing.send_msg('CHAT "You start tripping."') + self.thing.send_msg('RANDOM_COLORS') + eaten = self.thing.uncarry() + self.thing.game.remove_thing(eaten) + elif self.thing.carrying.type_ == 'Cookie': + self.thing.send_msg('CHAT ' + quote('You eat a cookie and gain the ability to draw the following character: "%s"' % self.thing.carrying.thing_char)) + self.thing.add_cookie_char(self.thing.carrying.thing_char) + eaten = self.thing.uncarry() + self.thing.game.remove_thing(eaten) + elif self.thing.carrying.type_ == 'Stimulant': + self.thing.send_msg('CHAT "You feel a flash of energy."') + self.thing.energy += 50 + eaten = self.thing.uncarry() + self.thing.game.remove_thing(eaten) + + + +class Task_COMMAND(Task): + argtypes = 'string' + + def check(self): + if self.thing.carrying is None: + raise PlayError('nothing to command') + if not self.thing.carrying.commandable: + raise PlayError('cannot command this item type') + + def do(self): + reply_lines = self.thing.carrying.interpret(self.args[0]) + for line in reply_lines: + self.thing.send_msg('REPLY ' + quote(line)) + + + +class Task_INSTALL(Task): + argtypes = 'string' + + def _get_uninstallables(self): + return [t for t in self.thing.game.things + if t != self.thing + and hasattr(t, 'installable') and t.installable + and (not t.portable) + and t.position == self.thing.position] + + def check(self): + if not self.thing.game.can_do_tile_with_pw(*self.thing.position, + self.args[0]): + raise GameError('wrong password for tile') + if self.thing.carrying: + if not hasattr(self.thing.carrying, 'installable')\ + or not self.thing.carrying.installable: + raise PlayError('carried thing not installable') + elif len(self._get_uninstallables()) == 0: + raise PlayError('nothing to uninstall here') + + def do(self): + if self.thing.carrying: + t = self.thing.uncarry() + t.install() + self.thing.send_msg('CHAT "You install the thing you carry."') + else: + self._get_uninstallables()[0].uninstall() + self.thing.send_msg('CHAT "You uninstall the thing here."') + self.thing.game.record_change(self.thing.position, 'other') + + + +class Task_WEAR(Task): + + def check(self): + if self.thing.name in self.thing.game.hats: + return + if not self.thing.carrying: + raise PlayError('carrying nothing to wear') + if self.thing.name in self.thing.game.hats: + raise PlayError('already wearing a hat') + if self.thing.carrying.type_ not in {'Hat', 'Bottle'}: + raise PlayError('can not wear the kind of thing you hold') + + def do(self): + if self.thing.name in self.thing.game.hats: + t = self.thing.game.add_thing('Hat', self.thing.position) + t.design = self.thing.game.hats[self.thing.name] + del self.thing.game.hats[self.thing.name] + self.thing.send_msg('CHAT "You drop your hat."') + for remixer in [t for t in self.thing.game.things + if t.type_ == 'HatRemixer' + and t.position == self.thing.position]: + remixer.accept(t) + break + else: + if self.thing.carrying.type_ == 'Bottle': + self.thing.send_msg('CHAT "Your attempt to wear a bottle on ' + 'your head fails."') + self.thing.carrying.sound('BOTTLE', 'SMASH') + elif self.thing.carrying.type_ == 'Hat': + self.thing.game.hats[self.thing.name] =\ + self.thing.carrying.design + self.thing.send_msg('CHAT "You put on a hat."') + dropped = self.thing.uncarry() + self.thing.game.remove_thing(dropped) + self.thing.game.record_change(self.thing.position, 'other') + + + +class Task_SPIN(Task): + + def check(self): + if not self.thing.carrying: + raise PlayError('holding nothing to spin') + if not hasattr(self.thing.carrying, 'spinnable'): + raise PlayError('held object not spinnable') + + def do(self): + self.thing.carrying.spin() + self.thing.send_msg('CHAT "You spin this object."') + + + +class Task_DANCE(Task): def do(self): - self.thing.carrying = None + self.thing.send_msg('CHAT "You dance."') + self.thing.dancing += 10 + self.thing.game.changed = True