Simplify state machines

This commit is contained in:
Tanner Collin 2020-12-03 03:30:54 +00:00
parent 6ba9288fd7
commit 5a7a2f0113
4 changed files with 109 additions and 155 deletions

View File

@ -16,6 +16,8 @@ for name, data in JSON_BLOCKS.items():
for state in data['states']: for state in data['states']:
BLOCKS[state['id']] = name.replace('minecraft:', '') BLOCKS[state['id']] = name.replace('minecraft:', '')
BREAK_DISTANCE = 5
AIR = 0 AIR = 0
SAND = 66 SAND = 66
SINGLE_SNOW = 3921 SINGLE_SNOW = 3921

36
game.py
View File

@ -306,7 +306,7 @@ class Game:
if solution: if solution:
solution = list(solution) solution = list(solution)
self.g.path = solution self.g.path = solution
self.g.job.state = self.g.job.stop self.g.job.stop()
print(len(solution)) print(len(solution))
print(solution) print(solution)
print(round(time.time() - start, 3), 'seconds') print(round(time.time() - start, 3), 'seconds')
@ -350,6 +350,11 @@ class Game:
self.g.command_lock = False self.g.command_lock = False
return return
if text == 'You are now AFK.':
self.g.afk = True
elif text == 'You are no longer AFK.':
self.g.afk = False
match1 = re.match(r'<(\w+)> (.*)', text) match1 = re.match(r'<(\w+)> (.*)', text)
match2 = re.match(r'\[(\w+) -> me] (.*)', text) match2 = re.match(r'\[(\w+) -> me] (.*)', text)
if match1: if match1:
@ -360,6 +365,9 @@ class Game:
else: else:
return return
if text == 'zzz':
text = '!zzz'
if text.startswith('! '): if text.startswith('! '):
text = text[2:] text = text[2:]
elif text.startswith('!'): elif text.startswith('!'):
@ -430,8 +438,9 @@ class Game:
reply += ', I need a bed' reply += ', I need a bed'
if command == 'stop': if command == 'stop':
self.g.job.state = self.g.job.stop self.g.job.stop()
self.g.path = [] self.g.path = []
self.g.look_at = None
reply = 'ok' reply = 'ok'
if command == 'inv': if command == 'inv':
@ -522,6 +531,18 @@ class Game:
print(traceback.format_exc()) print(traceback.format_exc())
reply = 'error' reply = 'error'
if command == 'zzz':
if not self.g.afk:
reply = '/afk'
if command == 'print':
try:
reply = str(eval(data))
except BaseException as e:
import traceback
print(traceback.format_exc())
reply = 'Error: {} - {}\n'.format(e.__class__.__name__, e)
if reply: if reply:
print(reply) print(reply)
if private and not reply.startswith('/'): if private and not reply.startswith('/'):
@ -545,8 +566,14 @@ class Game:
g.item_lock = False g.item_lock = False
def break_block(self, location): def break_block(self, location):
p = utils.pint(self.g.pos)
if utils.phyp(p, location) > blocks.BREAK_DISTANCE:
return False
bid = self.g.chunks.get_block_at(*location) bid = self.g.chunks.get_block_at(*location)
if bid != 0: if bid == 0:
return False
packet = PlayerDiggingPacket() packet = PlayerDiggingPacket()
packet.status = 0 packet.status = 0
packet.location = location packet.location = location
@ -555,6 +582,7 @@ class Game:
self.g.breaking = location self.g.breaking = location
self.g.break_time = time.time() + utils.break_time(bid, self.g.holding) self.g.break_time = time.time() + utils.break_time(bid, self.g.holding)
return True
def break_finish(self): def break_finish(self):
packet = PlayerDiggingPacket() packet = PlayerDiggingPacket()
@ -733,8 +761,8 @@ class Game:
obj.item_count = entry.value.item_count obj.item_count = entry.value.item_count
def handle_spawn_living(self, packet): def handle_spawn_living(self, packet):
return
print(packet) print(packet)
return
def handle_entity_position(self, packet): def handle_entity_position(self, packet):
obj = self.g.objects.get(packet.entity_id, None) obj = self.g.objects.get(packet.entity_id, None)

208
jobs.py
View File

@ -21,8 +21,6 @@ importlib.reload(items)
import data import data
importlib.reload(data) importlib.reload(data)
BREAK_DISTANCE = 5
class FindGappleStates: class FindGappleStates:
def idle(self): def idle(self):
@ -339,7 +337,7 @@ class GatherSandStates:
self.sand = check self.sand = check
break break
if utils.phyp(p, self.sand) > BREAK_DISTANCE: if utils.phyp(p, self.sand) > blocks.BREAK_DISTANCE:
self.state = self.nav_to_sand self.state = self.nav_to_sand
else: else:
self.state = self.dig_sand self.state = self.dig_sand
@ -895,7 +893,7 @@ class ClearLeavesStates:
w = self.g.world w = self.g.world
p = utils.pint(self.g.pos) p = utils.pint(self.g.pos)
for l in w.find_leaves(p, BREAK_DISTANCE): for l in w.find_leaves(p, blocks.BREAK_DISTANCE):
self.leaves.append(l) self.leaves.append(l)
self.state = self.break_leaves self.state = self.break_leaves
@ -1008,135 +1006,9 @@ class GrabSaplingStates:
class JobStates: class JobStates:
def idle(self): def idle(self):
return None return []
def cache_items(self): def init_machines(self):
s1 = self.cache_items_states
if s1.state == s1.idle:
s1.state = s1.init
elif s1.state == s1.done:
self.state = self.idle
s1.run()
def find_gapple(self):
s1 = self.find_gapple_states
if s1.state == s1.idle:
s1.state = s1.init
elif s1.state == s1.done:
s1.state = s1.tp_to_coord
s1.run()
def gather_sand(self):
s1 = self.gather_sand_states
s2 = self.grab_sand_states
s3 = self.sleep_with_bed_states
s4 = self.cache_items_states
if s1.state == s1.idle:
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
s4.state = s4.init
elif s1.state == s1.done:
if s2.state != s2.done:
s2.run()
return
if s3.state != s3.done:
s3.run()
return
if s4.state != s4.done:
s4.run()
return
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
s4.state = s4.init
return
s1.run()
def gather_wood(self):
s1 = self.gather_wood_states
s2 = self.sleep_with_bed_states
s3 = self.cache_items_states
if s1.state == s1.idle:
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
elif s1.state == s1.done:
if s2.state != s2.done:
s2.run()
return
if s3.state != s3.done:
s3.run()
return
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
return
s1.run()
def farm_wood(self):
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
s1 = self.gather_wood_states
s2 = self.plant_tree_states
s3 = self.clear_leaves_states
s4 = self.grab_sapling_states
s5 = self.sleep_with_bed_states
s6 = self.cache_items_states
if s1.state == s1.idle:
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
s4.state = s4.init
s5.state = s5.init
s6.state = s6.init
elif s1.state == s1.done:
if s2.state != s2.done:
s2.run()
return
if s3.state != s3.done:
s3.run()
return
if s4.state != s4.done:
s4.run()
return
if s5.state != s5.done:
s5.run()
return
if s6.state != s6.done:
s6.run()
return
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
s4.state = s4.init
s5.state = s5.init
s6.state = s6.init
return
s1.run()
def stop(self):
self.gather_wood_states = GatherWoodStates(self.g) self.gather_wood_states = GatherWoodStates(self.g)
self.gather_sand_states = GatherSandStates(self.g) self.gather_sand_states = GatherSandStates(self.g)
self.sleep_with_bed_states = SleepWithBedStates(self.g) self.sleep_with_bed_states = SleepWithBedStates(self.g)
@ -1146,22 +1018,72 @@ class JobStates:
self.clear_leaves_states = ClearLeavesStates(self.g) self.clear_leaves_states = ClearLeavesStates(self.g)
self.grab_sapling_states = GrabSaplingStates(self.g) self.grab_sapling_states = GrabSaplingStates(self.g)
self.grab_sand_states = GrabSandStates(self.g) self.grab_sand_states = GrabSandStates(self.g)
def run_machines(self, machines):
for m in machines:
if m.state == m.idle:
continue
if m.state != m.done:
m.run()
return
# if we went through them all
for m in machines:
m.state = m.init
def gather_sand(self):
machines = [
self.gather_sand_states,
self.grab_sand_states,
self.cache_items_states,
self.sleep_with_bed_states,
]
return machines
def cache_items(self):
machines = [
self.cache_items_states,
]
return machines
def find_gapple(self):
machines = [
self.find_gapple_states,
]
return machines
def gather_wood(self):
machines = [
self.gather_wood_states,
self.sleep_with_bed_states,
self.cache_items_states,
]
return machines
def farm_wood(self):
machines = [
self.gather_wood_states,
self.plant_tree_states,
self.clear_leaves_states,
self.grab_sapling_states,
self.sleep_with_bed_states,
self.cache_items_states,
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
return machines
def stop(self):
self.init_machines()
self.state = self.idle self.state = self.idle
def __init__(self, global_state): def __init__(self, global_state):
self.g = global_state self.g = global_state
self.init_machines()
self.state = self.idle self.state = self.idle
self.prev_state = None
self.gather_wood_states = GatherWoodStates(self.g)
self.gather_sand_states = GatherSandStates(self.g)
self.sleep_with_bed_states = SleepWithBedStates(self.g)
self.cache_items_states = CacheItemsStates(self.g)
self.find_gapple_states = FindGappleStates(self.g)
self.plant_tree_states = PlantTreeStates(self.g)
self.clear_leaves_states = ClearLeavesStates(self.g)
self.grab_sapling_states = GrabSaplingStates(self.g)
self.grab_sand_states = GrabSandStates(self.g)
def tick(self): def tick(self):
self.state() self.run_machines(self.state())

View File

@ -20,10 +20,12 @@ g.mcdata = False
g.pos = False g.pos = False
g.inv = {} g.inv = {}
g.objects = {} g.objects = {}
g.mobs = {}
g.window = None g.window = None
g.job = None g.job = None
g.correction_count = 0 g.correction_count = 0
g.holding = 0 g.holding = 0
g.afk = False
@app.route('/') @app.route('/')
def hello_world(): def hello_world():