021a4bc9 by Barry

Moved spawner and mobs to main to ease memory use and installation /

builds.
1 parent c6f62199
1 import utils 1 import utils
2 import sys
2 3
3 global_aliases = { 4 global_aliases = {
4 'n': 'north', 5 'n': 'north',
...@@ -65,8 +66,7 @@ class CommandHandler(object): ...@@ -65,8 +66,7 @@ class CommandHandler(object):
65 del ldict 66 del ldict
66 return True 67 return True
67 except Exception as e: 68 except Exception as e:
68 print('Something happened...') 69 print('Command handler exception:')
69 import sys
70 if 'esp' not in sys.platform: 70 if 'esp' not in sys.platform:
71 import traceback 71 import traceback
72 traceback.print_exc() 72 traceback.print_exc()
......
...@@ -9,10 +9,11 @@ Micropython port and expansion author: Barry Ruffner - barryruffner@gmail.com ...@@ -9,10 +9,11 @@ Micropython port and expansion author: Barry Ruffner - barryruffner@gmail.com
9 from time import sleep 9 from time import sleep
10 from math import floor 10 from math import floor
11 from sys import platform 11 from sys import platform
12 from os import listdir
12 13
13 from mudserver import MudServer 14 from mudserver import MudServer
14 from commandhandler import CommandHandler 15 from commandhandler import CommandHandler
15 from utils import load_object_from_file, save_object_to_file, password_hash 16 from utils import load_object_from_file, save_object_to_file, password_hash, calc_att, get_att
16 17
17 if 'esp' in platform: 18 if 'esp' in platform:
18 from gc import collect, mem_free 19 from gc import collect, mem_free
...@@ -50,6 +51,92 @@ tick = 0.0 ...@@ -50,6 +51,92 @@ tick = 0.0
50 spawn = 0.0 51 spawn = 0.0
51 cmd_handler = CommandHandler() 52 cmd_handler = CommandHandler()
52 53
54 def spawn_mobs(players):
55 rooms = listdir('rooms')
56 for room in rooms:
57 if '_monsters.json' not in room:
58 continue
59 room_monsters = load_object_from_file('rooms/{}'.format(room))
60 for mon_name, monster in room_monsters.items():
61 monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
62 if not monster_template:
63 continue
64 while len(room_monsters[mon_name]['active']) < monster['max']:
65 print('Spawning {} in {}'.format(mon_name, room))
66 mp = get_att(monster_template['spawn']["mp"])
67 hp = get_att(monster_template['spawn']["hp"])
68 sta = get_att(monster_template['spawn']["sta"])
69 new_active = {
70 "mp": mp,
71 "maxhp": hp,
72 "hp": hp,
73 "sta": sta,
74 "maxmp": mp,
75 "target": "",
76 "action": "attack",
77 "maxsta": sta}
78 room_monsters[mon_name]['active'].append(new_active)
79 for pid, pl in players.items():
80 if players[pid]['room'].lower() == room.split('_')[0]:
81 mud.send_message(pid, "a {} arrived".format(mon_name))
82 save_object_to_file(room_monsters, 'rooms/{}'.format(room))
83
84 def run_mobs(players, mud):
85 for pid, player in players.items():
86 if not player or not player.get("name") or not player.get('password'):
87 continue
88 if player['mp'] < player['maxmp']:
89 players[pid]['mp'] += player['mpr']
90 if player['sta'] < player['maxsta']:
91 players[pid]['sta'] += player['star']
92 room_monsters = load_object_from_file('rooms/{}_monsters.json'.format(player['room']))
93 for mon_name, monster in room_monsters.items():
94 monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
95 for active_monster_idx, active_monster in enumerate(monster['active']):
96 sta = active_monster['sta']
97 if active_monster['mp'] < active_monster['maxmp']:
98 active_monster['mp'] += monster_template['mpr']
99 if sta < active_monster['maxsta']:
100 sta += monster_template['star']
101 active_monster['sta'] = sta
102 if active_monster['action'] == "attack" and active_monster['target'] == player['name']:
103 if player.get("weapon"):
104 weapon = load_object_from_file('inventory/{}.json'.format(player['weapon']))
105 att = get_att(weapon['damage'])
106 mud.send_message(pid, "Your %s strikes the %s for %d" % (weapon['title'], mon_name, att,), color='yellow')
107 else:
108 att = get_att(player['aa'])
109 mud.send_message(pid, "You hit the %s for %d" % (mon_name, att,), color='yellow')
110 active_monster['hp'] -= att
111 if active_monster['hp'] <= 0:
112 del room_monsters[mon_name]['active'][active_monster_idx]
113 mud.send_message(pid, "The %s dies." % (mon_name,), color=['bold', 'blue'])
114 break
115 if active_monster.get("weapon"):
116 weapon = load_object_from_file('inventory/{}.json'.format(active_monster['weapon']))
117 att = get_att(weapon['damage'])
118 mud.send_message(pid, "The %s strikes you with a %s for %d" % (mon_name, weapon['title'], att,), color='magenta')
119 else:
120 att = get_att(monster_template['aa'])
121 mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,), color='magenta')
122 players[pid]['hp'] -= att
123 if (active_monster['hp']/active_monster['maxhp'] < 0.25) or (active_monster['mp'] > 0 and active_monster['mp']/active_monster['maxmp'] > 0.5) or (sta > 0 and sta/active_monster['maxsta'] > 0.5):
124 magic_cast = False
125 if active_monster['mp'] > 0:
126 att, active_monster['mp'] = calc_att(mud, pid, monster_template['sp'], active_monster['mp'])
127 players[pid]['hp'] -= att
128 if att > 0:
129 magic_cast = True
130 if not magic_cast:
131 if sta > 0:
132 att, sta = calc_att(mud, pid, monster_template['at'], sta)
133 active_monster['sta'] = sta
134 players[pid]['hp'] -= att
135
136 room_monsters[mon_name]['active'][active_monster_idx] = active_monster
137
138 save_object_to_file(room_monsters, 'rooms/{}_monsters.json'.format(player['room']))
139
53 # main game loop. We loop forever (i.e. until the program is terminated) 140 # main game loop. We loop forever (i.e. until the program is terminated)
54 while True: 141 while True:
55 if 'esp' in platform: 142 if 'esp' in platform:
...@@ -65,9 +152,7 @@ while True: ...@@ -65,9 +152,7 @@ while True:
65 if spawn >= 10: 152 if spawn >= 10:
66 spawn = 0 153 spawn = 0
67 try: 154 try:
68 ldict = {} 155 spawn_mobs(players)
69 with open('spawner.txt', 'r', encoding='utf-8') as f:
70 exec(f.read(), globals(), ldict)
71 except Exception as e: 156 except Exception as e:
72 print('spawner error:') 157 print('spawner error:')
73 print(e) 158 print(e)
...@@ -78,18 +163,11 @@ while True: ...@@ -78,18 +163,11 @@ while True:
78 print(mem_free()) 163 print(mem_free())
79 spawn += tick 164 spawn += tick
80 tick = 0 165 tick = 0
81 # try: 166 try:
82 ldict = {} 167 run_mobs(players, mud)
83 if 'esp' in platform: 168 except Exception as e:
84 print(mem_free()) 169 print('mob error:')
85 collect() 170 print(e)
86 with open('mobs.txt', 'r', encoding='utf-8') as f:
87 exec(f.read(), globals(), ldict)
88 if 'esp' in platform:
89 collect()
90 # except Exception as e:
91 # print('mob error:')
92 # print(e)
93 # 'update' must be called in the loop to keep the game running and give 171 # 'update' must be called in the loop to keep the game running and give
94 # us up-to-date information 172 # us up-to-date information
95 mud.update() 173 mud.update()
......
1
2 def run_mobs(players, mud):
3
4 from utils import load_object_from_file, save_object_to_file, calc_att, get_att
5 for pid, player in players.items():
6 if not player or not player.get("name") or not player.get('password'):
7 continue
8 if player['mp'] < player['maxmp']:
9 players[pid]['mp'] += player['mpr']
10 if player['sta'] < player['maxsta']:
11 players[pid]['sta'] += player['star']
12 room_monsters = load_object_from_file('rooms/{}_monsters.json'.format(player['room']))
13 for mon_name, monster in room_monsters.items():
14 monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
15 for active_monster_idx, active_monster in enumerate(monster['active']):
16 sta = active_monster['sta']
17 if active_monster['mp'] < active_monster['maxmp']:
18 active_monster['mp'] += monster_template['mpr']
19 if sta < active_monster['maxsta']:
20 sta += monster_template['star']
21 active_monster['sta'] = sta
22 if active_monster['action'] == "attack" and active_monster['target'] == player['name']:
23 if player.get("weapon"):
24 weapon = load_object_from_file('inventory/{}.json'.format(player['weapon']))
25 att = get_att(weapon['damage'])
26 mud.send_message(pid, "Your %s strikes the %s for %d" % (weapon['title'], mon_name, att,), color='yellow')
27 else:
28 att = get_att(player['aa'])
29 mud.send_message(pid, "You hit the %s for %d" % (mon_name, att,), color='yellow')
30 active_monster['hp'] -= att
31 if active_monster['hp'] <= 0:
32 del room_monsters[mon_name]['active'][active_monster_idx]
33 mud.send_message(pid, "The %s dies." % (mon_name,), color=['bold', 'blue'])
34 break
35 if active_monster.get("weapon"):
36 weapon = load_object_from_file('inventory/{}.json'.format(active_monster['weapon']))
37 att = get_att(weapon['damage'])
38 mud.send_message(pid, "The %s strikes you with a %s for %d" % (mon_name, weapon['title'], att,), color='magenta')
39 else:
40 att = get_att(monster_template['aa'])
41 mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,), color='magenta')
42 players[pid]['hp'] -= att
43 if (active_monster['hp']/active_monster['maxhp'] < 0.25) or (active_monster['mp'] > 0 and active_monster['mp']/active_monster['maxmp'] > 0.5) or (sta > 0 and sta/active_monster['maxsta'] > 0.5):
44 magic_cast = False
45 if active_monster['mp'] > 0:
46 att, active_monster['mp'] = calc_att(mud, pid, monster_template['sp'], active_monster['mp'])
47 players[pid]['hp'] -= att
48 if att > 0:
49 magic_cast = True
50 if not magic_cast:
51 if sta > 0:
52 att, sta = calc_att(mud, pid, monster_template['at'], sta)
53 active_monster['sta'] = sta
54 players[pid]['hp'] -= att
55
56 room_monsters[mon_name]['active'][active_monster_idx] = active_monster
57
58 save_object_to_file(room_monsters, 'rooms/{}_monsters.json'.format(player['room']))
59
60 run_mobs(players, mud)
61 del run_mobs
...\ No newline at end of file ...\ No newline at end of file
1 {"cricket": {"max": 1, "active": [{"hp": 15, "mp": 10, "maxmp": 10, "maxhp": 15, "sta": 10, "maxsta": 10, "target": "", "action": "attack"}]}}
...\ No newline at end of file ...\ No newline at end of file
1 {"cricket": {"max": 1, "active": [{"mp": 10, "maxhp": 14, "hp": 14, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}]}}
...\ No newline at end of file ...\ No newline at end of file
......
1 def spawn_mobs(players):
2 from os import listdir
3 from utils import get_att
4 rooms = listdir('rooms')
5 for room in rooms:
6 if '_monsters.json' not in room:
7 continue
8 room_monsters = load_object_from_file('rooms/{}'.format(room))
9 for mon_name, monster in room_monsters.items():
10 monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
11 if not monster_template:
12 continue
13 while len(room_monsters[mon_name]['active']) < monster['max']:
14 print('Spawning {} in {}'.format(mon_name, room))
15 mp = get_att(monster_template['spawn']["mp"])
16 hp = get_att(monster_template['spawn']["hp"])
17 sta = get_att(monster_template['spawn']["sta"])
18 new_active = {
19 "mp": mp,
20 "maxhp": hp,
21 "hp": hp,
22 "sta": sta,
23 "maxmp": mp,
24 "target": "",
25 "action": "attack",
26 "maxsta": sta}
27 room_monsters[mon_name]['active'].append(new_active)
28 for pid, pl in players.items():
29 if players[pid]['room'].lower() == room.split('_')[0]:
30 mud.send_message(pid, "a {} arrived".format(mon_name))
31 save_object_to_file(room_monsters, 'rooms/{}'.format(room))
32
33 spawn_mobs(players)
34 del spawn_mobs
...\ No newline at end of file ...\ No newline at end of file