Added regneration for magic, stamina. Added handlers for spells and
special attacks for monsters.
Showing
7 changed files
with
86 additions
and
32 deletions
... | @@ -32,12 +32,13 @@ class CommandHandler(object): | ... | @@ -32,12 +32,13 @@ class CommandHandler(object): |
32 | locals()['tokens'] = tokens | 32 | locals()['tokens'] = tokens |
33 | 33 | ||
34 | locals()['next_command'] = None | 34 | locals()['next_command'] = None |
35 | try: | 35 | # try: |
36 | if cmd in utils.load_object_from_file('rooms/' + players[id]["room"] + '.txt').get('exits'): | 36 | if cmd in utils.load_object_from_file('rooms/' + players[id]["room"] + '.txt').get('exits'): |
37 | params = cmd + " " + params.lower().strip() | 37 | params = cmd + " " + params.lower().strip() |
38 | cmd = "go" | 38 | cmd = "go" |
39 | if cmd == '': | 39 | if cmd == '': |
40 | return True | 40 | return True |
41 | command = cmd | ||
41 | ldict = locals() | 42 | ldict = locals() |
42 | with open('commands/{}.txt'.format(cmd), 'r', encoding='utf-8') as f: | 43 | with open('commands/{}.txt'.format(cmd), 'r', encoding='utf-8') as f: |
43 | exec(f.read(), globals(), ldict) | 44 | exec(f.read(), globals(), ldict) |
... | @@ -47,10 +48,10 @@ class CommandHandler(object): | ... | @@ -47,10 +48,10 @@ class CommandHandler(object): |
47 | with open('commands/{}.txt'.format(ldict['next_command']), 'r', encoding='utf-8') as f: | 48 | with open('commands/{}.txt'.format(ldict['next_command']), 'r', encoding='utf-8') as f: |
48 | exec(f.read()) | 49 | exec(f.read()) |
49 | return True | 50 | return True |
50 | except Exception as e: | 51 | # except Exception as e: |
51 | print('Something happened...') | 52 | # print('Something happened...') |
52 | print(e) | 53 | # print(e) |
53 | mud.send_message(id, "Unknown command '{}'".format(cmd)) | 54 | # mud.send_message(id, "Unknown command '{}'".format(cmd)) |
54 | return False | 55 | # return False |
55 | 56 | ||
56 | 57 | ... | ... |
... | @@ -49,4 +49,6 @@ def go(id, params, players, mud, tokens, command): | ... | @@ -49,4 +49,6 @@ def go(id, params, players, mud, tokens, command): |
49 | else: | 49 | else: |
50 | # send back an 'unknown exit' message | 50 | # send back an 'unknown exit' message |
51 | mud.send_message(id, "Unknown exit '{}'".format(params)) | 51 | mud.send_message(id, "Unknown exit '{}'".format(params)) |
52 | if command is None and cmd is not None: | ||
53 | command = cmd | ||
52 | next_command = go(id, params, players, mud, tokens, command) | 54 | next_command = go(id, params, players, mud, tokens, command) |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -102,7 +102,7 @@ while True: | ... | @@ -102,7 +102,7 @@ while True: |
102 | "name": None, | 102 | "name": None, |
103 | "room": None, | 103 | "room": None, |
104 | "inventory": {}, | 104 | "inventory": {}, |
105 | "prompt": "> ", | 105 | "prompt": "hp %hp mp %mp> ", |
106 | "aliases": {}, | 106 | "aliases": {}, |
107 | "hp": 100, | 107 | "hp": 100, |
108 | "mp": 100, | 108 | "mp": 100, | ... | ... |
... | @@ -38,30 +38,90 @@ def run_mobs(players, mud): | ... | @@ -38,30 +38,90 @@ def run_mobs(players, mud): |
38 | for pid, player in players.items(): | 38 | for pid, player in players.items(): |
39 | if not player['name']: | 39 | if not player['name']: |
40 | continue | 40 | continue |
41 | print(player) | 41 | # print(player) |
42 | print('checking actions in room: ' + player['room']) | 42 | # print('checking actions in room: ' + player['room']) |
43 | room_monsters = utils.load_object_from_file('rooms/{}_monsters.json'.format(player['room'])) | 43 | room_monsters = utils.load_object_from_file('rooms/{}_monsters.json'.format(player['room'])) |
44 | if not room_monsters: | 44 | if not room_monsters: |
45 | continue | 45 | continue |
46 | for mon_name, monster in room_monsters.items(): | 46 | for mon_name, monster in room_monsters.items(): |
47 | print(mon_name) | ||
48 | monster_template = utils.load_object_from_file('mobs/{}.json'.format(mon_name)) | 47 | monster_template = utils.load_object_from_file('mobs/{}.json'.format(mon_name)) |
49 | if not monster_template: | 48 | if not monster_template: |
50 | continue | 49 | continue |
51 | print(monster) | 50 | for active_monster_idx, active_monster in enumerate(monster['active']): |
52 | for active_monster in monster['active']: | 51 | # Recover MP and Stamina |
52 | mp = active_monster['mp'] | ||
53 | hp = active_monster['hp'] | ||
54 | sta = active_monster['sta'] | ||
55 | if mp < active_monster['maxmp']: | ||
56 | mp += monster_template['mpr'] | ||
57 | active_monster['mp'] = mp | ||
58 | if sta < active_monster['maxsta']: | ||
59 | sta += monster_template['star'] | ||
60 | active_monster['sta'] = sta | ||
53 | if active_monster['action'] == "attack" and active_monster['target'] == player['name']: | 61 | if active_monster['action'] == "attack" and active_monster['target'] == player['name']: |
54 | if active_monster['hp'] == 0: | 62 | if hp == 0: |
55 | DEAD = 1 | 63 | DEAD = 1 |
56 | if "d" in monster_template['aa']: | 64 | if "d" in monster_template['aa']: |
57 | dice = monster_template['aa'].split('d') | 65 | dice = monster_template['aa'].split('d') |
58 | att = 0 | 66 | att = 0 |
59 | for d in range(int(dice[0])): | 67 | for d in range(int(dice[0])): |
60 | att += randrange(int(dice[1]) + 1) | 68 | att += randrange(int(dice[1])) + 1 |
61 | players[pid]['hp'] -= att | 69 | players[pid]['hp'] -= att |
62 | mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,)) | 70 | mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,)) |
71 | # wait until at least 50% of stamina or mp are regenerated or hp is less than 25% | ||
72 | |||
73 | if (hp/active_monster['maxhp'] < 0.25) or (mp > 0 and mp/active_monster['maxmp'] > 0.5) or (sta > 0 and sta/active_monster['maxsta'] > 0.5): | ||
74 | magic_cast = False | ||
75 | # Try magic first | ||
76 | if mp > 0: | ||
77 | valid_spells = [] | ||
78 | for spell in monster_template['sp']: | ||
79 | if spell['cost'] < mp: | ||
80 | valid_spells.append(spell) | ||
81 | # Select a random spell | ||
82 | if len(valid_spells) > 0: | ||
83 | spell = valid_spells[randrange(len(valid_spells))] | ||
84 | del valid_spells | ||
85 | att = 0 | ||
86 | if 'd' in spell['dmg']: | ||
87 | dice = spell['dmg'].split('d') | ||
88 | for d in range(int(dice[0])): | ||
89 | att += randrange(int(dice[1])) + 1 | ||
90 | else: | ||
91 | att = int(spell['dmg']) | ||
92 | active_monster['mp'] -= spell['cost'] | ||
93 | players[pid]['hp'] -= att | ||
94 | mud.send_message(pid, "%s for %d" % (spell['desc'], att,)) | ||
95 | magic_cast = True | ||
96 | if not magic_cast: | ||
97 | # if mp is exhaused or unavailable switch to stamina actions | ||
98 | if sta > 0: | ||
99 | print(sta) | ||
100 | valid_attacks = [] | ||
101 | for attack in monster_template['at']: | ||
102 | if attack['cost'] < sta: | ||
103 | valid_attacks.append(attack) | ||
104 | # Select a random attack | ||
105 | if len(valid_attacks) > 0: | ||
106 | rand_val = randrange(len(valid_attacks)) | ||
107 | print("Random attack result: {} Len: {}".format(rand_val, len(valid_attacks))) | ||
108 | attack = valid_attacks[rand_val] | ||
109 | del valid_attacks | ||
110 | att = 0 | ||
111 | if 'd' in attack['dmg']: | ||
112 | dice = attack['dmg'].split('d') | ||
113 | for d in range(int(dice[0])): | ||
114 | att += randrange(int(dice[1])) + 1 | ||
63 | else: | 115 | else: |
64 | print(randrange(120)) | 116 | att = int(attack['dmg']) |
117 | active_monster['sta'] -= attack['cost'] | ||
118 | players[pid]['hp'] -= att | ||
119 | mud.send_message(pid, "%s for %d" % (attack['desc'], att,)) | ||
120 | magic_cast = True | ||
121 | |||
122 | room_monsters[mon_name]['active'][active_monster_idx] = active_monster | ||
123 | |||
124 | utils.save_object_to_file(room_monsters, 'rooms/{}_monsters.json'.format(player['room'])) | ||
65 | # { | 125 | # { |
66 | # "cricket": { | 126 | # "cricket": { |
67 | # "max": 1, | 127 | # "max": 1, | ... | ... |
... | @@ -2,16 +2,20 @@ | ... | @@ -2,16 +2,20 @@ |
2 | "name": "cricket", | 2 | "name": "cricket", |
3 | "desc": "A small green chirping insect with large legs suited for jumping.", | 3 | "desc": "A small green chirping insect with large legs suited for jumping.", |
4 | "aa": "1d2", | 4 | "aa": "1d2", |
5 | "mpr": 0.25, | ||
6 | "star": 0.4, | ||
5 | "spawn": { | 7 | "spawn": { |
6 | "hp": "2d2", | 8 | "hp": "2d2", |
7 | "mp": "0", | 9 | "mp": "10", |
8 | "sta": "10" | 10 | "sta": "10" |
9 | }, | 11 | }, |
10 | "say": [ | 12 | "say": [ |
11 | "A cricket quietly chirps", | 13 | "A cricket quietly chirps", |
12 | "A cricket moves quietly" | 14 | "A cricket moves quietly" |
13 | ], | 15 | ], |
14 | "sp": [], | 16 | "sp": [ |
17 | {"name": "zap", "cost":5, "dmg": "2d4", "desc": "The cricket magically sparks lightning by rubbing it's legs together"} | ||
18 | ], | ||
15 | "at": [ | 19 | "at": [ |
16 | {"name": "kick", "cost":5, "dmg": "2d4", "desc": "The cricket kicks with powerful legs"}, | 20 | {"name": "kick", "cost":5, "dmg": "2d4", "desc": "The cricket kicks with powerful legs"}, |
17 | {"name": "antenna tickle", "cost":2, "dmg": "1d3", "desc": "The cricket brushes you with antenna"} | 21 | {"name": "antenna tickle", "cost":2, "dmg": "1d3", "desc": "The cricket brushes you with antenna"} | ... | ... |
1 | {"name": "test", "room": "Outside", "inventory": {"candle": 1}, "prompt": "%hp> ", "aliases": {}, "hp": 96, "mp": 100, "sta": 10} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | {"name": "test", "room": "Tavern", "inventory": {"candle": 1}, "prompt": "%hp> ", "aliases": {}, "hp": 953, "mp": 100, "sta": 10} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | { | ||
2 | "cricket": { | ||
3 | "max": 1, | ||
4 | "active": [ | ||
5 | { | ||
6 | "hp": 100, | ||
7 | "mp": 0, | ||
8 | "sta": 10, | ||
9 | "action": "attack", | ||
10 | "target": "test" | ||
11 | } | ||
12 | ] | ||
13 | } | ||
14 | } | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | {"cricket": {"max": 1, "active": [{"hp": 100, "mp": 4.0, "sta": 2.550000000000001, "maxhp": 100, "maxmp": 10, "maxsta": 10, "action": "attack", "target": "test"}]}} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment