9b22a762 by Barry

Moved several files to a frozen folder that contains files that had to be

moved to the modules folder in the baked in bin so they could be read from
flash instead of ram.
1 parent d809b92a
...@@ -49,7 +49,7 @@ def look(id, mud, players, tokens): ...@@ -49,7 +49,7 @@ def look(id, mud, players, tokens):
49 mud.send_message(id, "Exits are: {}".format(", ".join(room_data.get('exits')))) 49 mud.send_message(id, "Exits are: {}".format(", ".join(room_data.get('exits'))))
50 50
51 # send player a message containing the list of exits from this room 51 # send player a message containing the list of exits from this room
52 if len(room_data.get('inventory').keys()) > 0: 52 if len(room_data.get('inventory')) > 0:
53 mud.send_message(id, "Items here: {}".format(", ".join(room_data.get('inventory').keys()))) 53 mud.send_message(id, "Items here: {}".format(", ".join(room_data.get('inventory').keys())))
54 54
55 # send player a message containing the list of players in the room 55 # send player a message containing the list of players in the room
......
...@@ -3,6 +3,7 @@ def save(id, players, mud): ...@@ -3,6 +3,7 @@ def save(id, players, mud):
3 mud.send_message(id, "Saving...") 3 mud.send_message(id, "Saving...")
4 utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"])) 4 utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
5 mud.send_message(id, "Save complete") 5 mud.send_message(id, "Save complete")
6 return True
6 7
7 save(id, players, mud) 8 save(id, players, mud)
8 del save 9 del save
...\ No newline at end of file ...\ No newline at end of file
......
1 import json 1 import json
2 import sys 2 from urandom import getrandbits
3 if 'esp' in sys.platform:
4 from urandom import getrandbits
5 else:
6 import random
7 3
8 4
9 def get_color(name): 5 def get_color(name):
...@@ -32,25 +28,22 @@ def load_object_from_file(filename): ...@@ -32,25 +28,22 @@ def load_object_from_file(filename):
32 return None 28 return None
33 29
34 def randrange(start, stop=None): 30 def randrange(start, stop=None):
35 if 'esp' in sys.platform: 31 if start == 1:
36 if start == 1: 32 return 0
37 return 0 33 if stop is None:
38 if stop is None: 34 stop = start
39 stop = start 35 start = 0
40 start = 0 36 upper = stop - start
41 upper = stop - start 37 bits = 0
42 bits = 0 38 pwr2 = 1
43 pwr2 = 1 39 while upper > pwr2:
44 while upper > pwr2: 40 pwr2 <<= 1
45 pwr2 <<= 1 41 bits += 1
46 bits += 1 42 while True:
47 while True: 43 r = getrandbits(bits)
48 r = getrandbits(bits) 44 if r < upper:
49 if r < upper: 45 break
50 break 46 return r + start
51 return r + start
52 else:
53 return random.randrange(start)
54 47
55 def get_att(d): 48 def get_att(d):
56 att = 0 49 att = 0
......
...@@ -6,17 +6,28 @@ MudServer author: Mark Frimston - mfrimston@gmail.com ...@@ -6,17 +6,28 @@ MudServer author: Mark Frimston - mfrimston@gmail.com
6 Micropython port and expansion author: Barry Ruffner - barryruffner@gmail.com 6 Micropython port and expansion author: Barry Ruffner - barryruffner@gmail.com
7 """ 7 """
8 8
9 from time import sleep
10 from sys import platform 9 from sys import platform
11 if 'esp' in platform: 10 if 'esp' in platform:
12 from gc import collect, mem_free 11 from gc import collect, mem_free
13 from machine import Pin 12 from machine import Pin
13 if 'esp' in platform:
14 collect()
15 from time import sleep
16 if 'esp' in platform:
17 collect()
14 # import the MUD server class 18 # import the MUD server class
15 from mudserver import MudServer 19 from mudserver import MudServer
20 if 'esp' in platform:
21 collect()
16 22
17 from utils import load_object_from_file, save_object_to_file 23 from utils import load_object_from_file, save_object_to_file
24 if 'esp' in platform:
25 collect()
18 from math import floor 26 from math import floor
19 27 if 'esp' in platform:
28 collect()
29 import micropython
30 micropython.mem_info(1)
20 print('STARTING MUD\r\n\r\n\r\n') 31 print('STARTING MUD\r\n\r\n\r\n')
21 32
22 if 'esp' in platform: 33 if 'esp' in platform:
...@@ -64,6 +75,8 @@ while True: ...@@ -64,6 +75,8 @@ while True:
64 except Exception as e: 75 except Exception as e:
65 print('spawner error:') 76 print('spawner error:')
66 print(e) 77 print(e)
78 if 'esp' in platform:
79 collect()
67 if tick >= 1: 80 if tick >= 1:
68 if 'esp' in platform: 81 if 'esp' in platform:
69 print(mem_free()) 82 print(mem_free())
...@@ -71,8 +84,13 @@ while True: ...@@ -71,8 +84,13 @@ while True:
71 tick = 0 84 tick = 0
72 # try: 85 # try:
73 ldict = {} 86 ldict = {}
87 if 'esp' in platform:
88 print(mem_free())
89 collect()
74 with open('mobs.txt', 'r', encoding='utf-8') as f: 90 with open('mobs.txt', 'r', encoding='utf-8') as f:
75 exec(f.read(), globals(), ldict) 91 exec(f.read(), globals(), ldict)
92 if 'esp' in platform:
93 collect()
76 # except Exception as e: 94 # except Exception as e:
77 # print('mob error:') 95 # print('mob error:')
78 # print(e) 96 # print(e)
...@@ -161,6 +179,8 @@ while True: ...@@ -161,6 +179,8 @@ while True:
161 179
162 else: 180 else:
163 from commandhandler import CommandHandler 181 from commandhandler import CommandHandler
182 if 'esp' in platform:
183 collect()
164 cmd_handler = CommandHandler() 184 cmd_handler = CommandHandler()
165 185
166 handler_results = cmd_handler.parse(id, command, params, mud, players) 186 handler_results = cmd_handler.parse(id, command, params, mud, players)
......
...@@ -10,19 +10,12 @@ def run_mobs(players, mud): ...@@ -10,19 +10,12 @@ def run_mobs(players, mud):
10 if player['sta'] < player['maxsta']: 10 if player['sta'] < player['maxsta']:
11 players[pid]['sta'] += player['star'] 11 players[pid]['sta'] += player['star']
12 room_monsters = load_object_from_file('rooms/{}_monsters.json'.format(player['room'])) 12 room_monsters = load_object_from_file('rooms/{}_monsters.json'.format(player['room']))
13 if not room_monsters:
14 continue
15 for mon_name, monster in room_monsters.items(): 13 for mon_name, monster in room_monsters.items():
16 monster_template = load_object_from_file('mobs/{}.json'.format(mon_name)) 14 monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
17 if not monster_template:
18 continue
19 for active_monster_idx, active_monster in enumerate(monster['active']): 15 for active_monster_idx, active_monster in enumerate(monster['active']):
20 mp = active_monster['mp']
21 hp = active_monster['hp']
22 sta = active_monster['sta'] 16 sta = active_monster['sta']
23 if mp < active_monster['maxmp']: 17 if active_monster['mp'] < active_monster['maxmp']:
24 mp += monster_template['mpr'] 18 active_monster['mp'] += monster_template['mpr']
25 active_monster['mp'] = mp
26 if sta < active_monster['maxsta']: 19 if sta < active_monster['maxsta']:
27 sta += monster_template['star'] 20 sta += monster_template['star']
28 active_monster['sta'] = sta 21 active_monster['sta'] = sta
...@@ -34,11 +27,10 @@ def run_mobs(players, mud): ...@@ -34,11 +27,10 @@ def run_mobs(players, mud):
34 else: 27 else:
35 att = get_att(player['aa']) 28 att = get_att(player['aa'])
36 mud.send_message(pid, "You hit the %s for %d" % (mon_name, att,), color='yellow') 29 mud.send_message(pid, "You hit the %s for %d" % (mon_name, att,), color='yellow')
37 hp -= att 30 active_monster['hp'] -= att
38 if hp <= 0: 31 if active_monster['hp'] <= 0:
39 del room_monsters[mon_name]['active'][active_monster_idx] 32 del room_monsters[mon_name]['active'][active_monster_idx]
40 mud.send_message(pid, "The %s dies." % (mon_name,), color=['bold', 'blue']) 33 mud.send_message(pid, "The %s dies." % (mon_name,), color=['bold', 'blue'])
41 # TODO: Create a corpse
42 break 34 break
43 if active_monster.get("weapon"): 35 if active_monster.get("weapon"):
44 weapon = load_object_from_file('inventory/{}.json'.format(active_monster['weapon'])) 36 weapon = load_object_from_file('inventory/{}.json'.format(active_monster['weapon']))
...@@ -48,13 +40,10 @@ def run_mobs(players, mud): ...@@ -48,13 +40,10 @@ def run_mobs(players, mud):
48 att = get_att(monster_template['aa']) 40 att = get_att(monster_template['aa'])
49 mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,), color='magenta') 41 mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,), color='magenta')
50 players[pid]['hp'] -= att 42 players[pid]['hp'] -= att
51 # wait until at least 50% of stamina or mp are regenerated or hp is less than 25% 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):
52
53 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):
54 magic_cast = False 44 magic_cast = False
55 if mp > 0: 45 if active_monster['mp'] > 0:
56 att, mp = calc_att(mud, pid, monster_template['sp'], mp) 46 att, active_monster['mp'] = calc_att(mud, pid, monster_template['sp'], active_monster['mp'])
57 active_monster['mp'] = mp
58 players[pid]['hp'] -= att 47 players[pid]['hp'] -= att
59 if att > 0: 48 if att > 0:
60 magic_cast = True 49 magic_cast = True
......
...@@ -14,12 +14,9 @@ BAUDRATE = 115200 ...@@ -14,12 +14,9 @@ BAUDRATE = 115200
14 folders = ['help', 'rooms', 'inventory', 'commands', 'mobs'] 14 folders = ['help', 'rooms', 'inventory', 'commands', 'mobs']
15 15
16 files = [ 16 files = [
17 "commandhandler.py",
18 "main.py", 17 "main.py",
19 "mobs.txt", 18 "mobs.txt",
20 "spawner.txt", 19 "spawner.txt",
21 "mudserver.py",
22 "utils.py",
23 "welcome.txt", 20 "welcome.txt",
24 "wifiweb.py", 21 "wifiweb.py",
25 "defaultplayer.json" 22 "defaultplayer.json"
...@@ -44,7 +41,9 @@ with serial.Serial(PORT, BAUDRATE, timeout=1) as ser: ...@@ -44,7 +41,9 @@ with serial.Serial(PORT, BAUDRATE, timeout=1) as ser:
44 41
45 print('Creating folders.') 42 print('Creating folders.')
46 # we are missing folders so they need to be created. 43 # we are missing folders so they need to be created.
47 for folder in folders: 44 tmp_folders = folders
45 tmp_folders.append('players')
46 for folder in tmp_folders:
48 if folder in root: 47 if folder in root:
49 continue 48 continue
50 print('Creating folder: {}'.format(folder)) 49 print('Creating folder: {}'.format(folder))
......
...@@ -30,4 +30,5 @@ def spawn_mobs(players): ...@@ -30,4 +30,5 @@ def spawn_mobs(players):
30 mud.send_message(pid, "a {} arrived".format(mon_name)) 30 mud.send_message(pid, "a {} arrived".format(mon_name))
31 save_object_to_file(room_monsters, 'rooms/{}'.format(room)) 31 save_object_to_file(room_monsters, 'rooms/{}'.format(room))
32 32
33 spawn_mobs(players)
...\ No newline at end of file ...\ No newline at end of file
33 spawn_mobs(players)
34 del spawn_mobs
...\ No newline at end of file ...\ No newline at end of file
......