|
| 1 | + |
| 2 | +import random |
| 3 | +from typing import Optional |
| 4 | +from tale import base, util, cmds |
| 5 | +from tale import magic |
| 6 | +from tale.cmds import cmd |
| 7 | +from tale.errors import ActionRefused, ParseError |
| 8 | +from tale.magic import MagicType, MagicSkill, Spell |
| 9 | +from tale.player import Player |
| 10 | + |
| 11 | + |
| 12 | +@cmd("heal") |
| 13 | +def do_heal(player: Player, parsed: base.ParseResult, ctx: util.Context) -> None: |
| 14 | + """ Heal someone or something """ |
| 15 | + |
| 16 | + skillValue = player.stats.magic_skills.get(MagicType.HEAL, None) |
| 17 | + if not skillValue: |
| 18 | + raise ActionRefused("You don't know how to heal") |
| 19 | + spell = magic.spells[MagicType.HEAL] # type: Spell |
| 20 | + |
| 21 | + num_args = len(parsed.args) |
| 22 | + |
| 23 | + level = player.stats.level |
| 24 | + if num_args == 2: |
| 25 | + level = int(parsed.args[1]) |
| 26 | + |
| 27 | + if not spell.check_cost(player.stats.magic_points, level): |
| 28 | + raise ActionRefused("You don't have enough magic points") |
| 29 | + |
| 30 | + if num_args < 1: |
| 31 | + raise ParseError("You need to specify who or what to heal") |
| 32 | + try: |
| 33 | + entity = str(parsed.args[0]) |
| 34 | + except ValueError as x: |
| 35 | + raise ActionRefused(str(x)) |
| 36 | + |
| 37 | + result = player.location.search_living(entity) # type: Optional[base.Living] |
| 38 | + if not result or not isinstance(result, base.Living): |
| 39 | + raise ActionRefused("Can't heal that") |
| 40 | + |
| 41 | + |
| 42 | + player.stats.magic_points -= spell.base_cost * level |
| 43 | + |
| 44 | + if random.randint(1, 100) > skillValue: |
| 45 | + player.tell("Your healing spell fizzles out", evoke=True, short_len=True) |
| 46 | + return |
| 47 | + |
| 48 | + result.stats.replenish_hp(5 * level) |
| 49 | + player.tell("You cast a healing spell that heals %s for %d hit points" % (result.name, 5 * level), evoke=True) |
| 50 | + player.tell_others("%s casts a healing spell that heals %s" % (player.name, result.name), evoke=True) |
| 51 | + |
| 52 | + |
| 53 | +@cmd("bolt") |
| 54 | +def do_bolt(player: Player, parsed: base.ParseResult, ctx: util.Context) -> None: |
| 55 | + """ Cast a bolt of energy """ |
| 56 | + |
| 57 | + skillValue = player.stats.magic_skills.get(MagicType.BOLT, None) |
| 58 | + if not skillValue: |
| 59 | + raise ActionRefused("You don't know how to cast a bolt") |
| 60 | + |
| 61 | + spell = magic.spells[MagicType.BOLT] # type: Spell |
| 62 | + |
| 63 | + num_args = len(parsed.args) |
| 64 | + |
| 65 | + level = player.stats.level |
| 66 | + if num_args == 2: |
| 67 | + level = int(parsed.args[1]) |
| 68 | + |
| 69 | + if not spell.check_cost(player.stats.magic_points, level): |
| 70 | + raise ActionRefused("You don't have enough magic points") |
| 71 | + |
| 72 | + if num_args < 1: |
| 73 | + raise ParseError("You need to specify who or what to attack") |
| 74 | + |
| 75 | + try: |
| 76 | + entity = str(parsed.args[0]) |
| 77 | + except ValueError as x: |
| 78 | + raise ActionRefused(str(x)) |
| 79 | + |
| 80 | + result = player.location.search_living(entity) # type: Optional[base.Living] |
| 81 | + if not result or not isinstance(result, base.Living): |
| 82 | + raise ActionRefused("Can't attack that") |
| 83 | + |
| 84 | + player.stats.magic_points -= spell.base_cost * level |
| 85 | + |
| 86 | + if random.randint(1, 100) > skillValue: |
| 87 | + player.tell("Your bolt spell fizzles out", evoke=True, short_len=True) |
| 88 | + return |
| 89 | + |
| 90 | + hp = random.randint(1, level) |
| 91 | + result.stats.hp -= hp |
| 92 | + player.tell("You cast an energy bolt that hits %s for %d damage" % (result.name, hp), evoke=True) |
| 93 | + player.tell_others("%s casts an energy bolt that hits %s for %d damage" % (player.name, result.name, hp), evoke=True) |
| 94 | + |
| 95 | +@cmd("drain") |
| 96 | +def do_drain(player: Player, parsed: base.ParseResult, ctx: util.Context) -> None: |
| 97 | + """ Drain energy from someone or something """ |
| 98 | + |
| 99 | + skillValue = player.stats.magic_skills.get(MagicType.DRAIN, None) |
| 100 | + if not skillValue: |
| 101 | + raise ActionRefused("You don't know how to drain") |
| 102 | + |
| 103 | + spell = magic.spells[MagicType.DRAIN] # type: Spell |
| 104 | + |
| 105 | + num_args = len(parsed.args) |
| 106 | + |
| 107 | + level = player.stats.level |
| 108 | + if num_args == 2: |
| 109 | + level = int(parsed.args[1]) |
| 110 | + |
| 111 | + if not spell.check_cost(player.stats.magic_points, level): |
| 112 | + raise ActionRefused("You don't have enough magic points") |
| 113 | + |
| 114 | + if num_args < 1: |
| 115 | + raise ParseError("You need to specify who or what to drain") |
| 116 | + |
| 117 | + try: |
| 118 | + entity = str(parsed.args[0]) |
| 119 | + except ValueError as x: |
| 120 | + raise ActionRefused(str(x)) |
| 121 | + |
| 122 | + result = player.location.search_living(entity) # type: Optional[base.Living] |
| 123 | + if not result or not isinstance(result, base.Living): |
| 124 | + raise ActionRefused("Can't drain that") |
| 125 | + |
| 126 | + player.stats.magic_points -= spell.base_cost * level |
| 127 | + |
| 128 | + if random.randint(1, 100) > skillValue: |
| 129 | + player.tell("Your drain spell fizzles out", evoke=True, short_len=True) |
| 130 | + return |
| 131 | + |
| 132 | + points = random.randint(1, level) |
| 133 | + result.stats.combat_points -= points |
| 134 | + result.stats.magic_points -= points |
| 135 | + |
| 136 | + player.stats.magic_points += points |
| 137 | + |
| 138 | + player.tell("You cast a 'drain' spell that drains %s of %d combat and magic points" % (result.name, points), evoke=True) |
| 139 | + player.tell_others("%s casts a 'drain' spell that drains energy from %s" % (player.name, result.name), evoke=True) |
| 140 | + |
| 141 | + |
0 commit comments