Add state machine for eating food

This commit is contained in:
Tanner Collin 2021-01-05 05:57:06 +00:00
parent 029ef72ea5
commit 7217686716
4 changed files with 131 additions and 26 deletions

67
game.py
View File

@ -336,6 +336,7 @@ class Game:
register(self.handle_respawn, clientbound.play.RespawnPacket) register(self.handle_respawn, clientbound.play.RespawnPacket)
register(self.handle_player_list, clientbound.play.PlayerListItemPacket) register(self.handle_player_list, clientbound.play.PlayerListItemPacket)
register(self.handle_entity_teleport, EntityTeleport) register(self.handle_entity_teleport, EntityTeleport)
register(self.handle_update_health, clientbound.play.UpdateHealthPacket)
#register(self.handle_entity_velocity, clientbound.play.EntityVelocityPacket) #register(self.handle_entity_velocity, clientbound.play.EntityVelocityPacket)
#register(self.handle_packet, Packet, early=True) #register(self.handle_packet, Packet, early=True)
@ -477,10 +478,6 @@ class Game:
reply = 'ok' reply = 'ok'
raise raise
if command == 'break':
self.break_block(blocks.TEST_BLOCK)
reply = 'ok'
if command == 'inv': if command == 'inv':
inv_list = [] inv_list = []
for i in self.g.inv.values(): for i in self.g.inv.values():
@ -497,27 +494,6 @@ class Game:
item = int(data) item = int(data)
reply = str(self.count_items([item])) reply = str(self.count_items([item]))
if command == 'open':
self.open_container(blocks.TEST_BLOCK)
if command == 'close':
if self.g.window:
self.close_window()
else:
reply = 'nothing open'
if command == 'click' and data:
if self.g.window:
slot, button, mode = [int(x) for x in data.split(' ')]
try:
item = self.g.window.contents[slot]
except KeyError:
item = Slot(present=False, item_id=None, item_count=None, nbt=None)
print(item)
self.click_window(slot, button, mode, item)
else:
reply = 'nothing open'
if command == 'loaded': if command == 'loaded':
reply = str(self.g.chunks.get_loaded_area()) reply = str(self.g.chunks.get_loaded_area())
@ -651,6 +627,10 @@ class Game:
else: else:
reply += ', I need a bed' reply += ', I need a bed'
if command == 'loiter':
self.g.job.state = self.g.job.loiter
reply = 'ok'
if command == 'stop': if command == 'stop':
bot.init(self.g) bot.init(self.g)
reply = 'ok' reply = 'ok'
@ -745,6 +725,33 @@ class Game:
else: else:
reply = 'no path' reply = 'no path'
if command == 'break':
self.break_block(blocks.TEST_BLOCK)
reply = 'ok'
if command == 'open':
self.open_container(blocks.TEST_BLOCK)
if command == 'close':
if self.g.window:
self.close_window()
else:
reply = 'nothing open'
if command == 'click' and data:
if self.g.window:
slot, button, mode = [int(x) for x in data.split(' ')]
try:
item = self.g.window.contents[slot]
except KeyError:
item = Slot(present=False, item_id=None, item_count=None, nbt=None)
print(item)
self.click_window(slot, button, mode, item)
else:
reply = 'nothing open'
if command == 'use':
self.use_item(0)
################# Authorized commands ########################## ################# Authorized commands ##########################
if authed: if authed:
@ -1081,6 +1088,16 @@ class Game:
self.g.player_names[action.uuid] = action.name self.g.player_names[action.uuid] = action.name
self.g.player_names[action.name] = action.uuid # porque no los dos? self.g.player_names[action.name] = action.uuid # porque no los dos?
def handle_update_health(self, packet):
print(packet)
self.g.health = packet.health
self.g.food = packet.food
def use_item(self, hand):
packet = serverbound.play.UseItemPacket()
packet.hand = hand
self.g.connection.write_packet(packet)
def tick(self): def tick(self):
if self.g.breaking: if self.g.breaking:
self.animate() self.animate()

View File

@ -49,6 +49,15 @@ SAPLINGS = [
'dark_oak_sapling', 'dark_oak_sapling',
] ]
FOOD = [
'cooked_porkchop',
'cooked_beef',
'bread',
'cooked_chicken',
'cooked_cod',
'cooked_salmon',
]
BED_IDS = set() BED_IDS = set()
for item_name in BEDS: for item_name in BEDS:
BED_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id']) BED_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id'])
@ -61,6 +70,10 @@ AXE_IDS = set()
for item_name in AXES: for item_name in AXES:
AXE_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id']) AXE_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id'])
FOOD_IDS = set()
for item_name in FOOD:
FOOD_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id'])
SAPLING_IDS = set() SAPLING_IDS = set()
for item_name in SAPLINGS: for item_name in SAPLINGS:
SAPLING_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id']) SAPLING_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id'])
@ -82,5 +95,5 @@ POTATO_ID = get_id('potato')
WHEAT_SEEDS_ID = get_id('wheat_seeds') WHEAT_SEEDS_ID = get_id('wheat_seeds')
BEETROOT_SEEDS_ID = get_id('beetroot_seeds') BEETROOT_SEEDS_ID = get_id('beetroot_seeds')
NEEDED_ITEMS = BED_IDS | SHOVEL_IDS | AXE_IDS | set([CHEST_ID]) NEEDED_ITEMS = BED_IDS | SHOVEL_IDS | AXE_IDS | FOOD_IDS | set([CHEST_ID])
WANTED_ITEMS = SAPLING_IDS | set([NETHERWART_ID, CARROT_ID, POTATO_ID, WHEAT_SEEDS_ID, BEETROOT_SEEDS_ID]) WANTED_ITEMS = SAPLING_IDS | set([NETHERWART_ID, CARROT_ID, POTATO_ID, WHEAT_SEEDS_ID, BEETROOT_SEEDS_ID])

73
jobs.py
View File

@ -1707,6 +1707,62 @@ class FillBlocksStates:
self.state() self.state()
class EatFoodStates:
def idle(self):
return None
def init(self):
if self.g.food < 12:
print('Hungry, eating')
self.state = self.select_food
return
if self.g.health < 20 and self.g.food < 18:
print('Low health, eating')
self.state = self.select_food
return
print('Don\'t need to eat, aborting')
self.state = self.cleanup
def select_food(self):
if self.g.game.select_item(items.FOOD_IDS):
self.state = self.eat_food
else:
print('No food, aborting')
self.state = self.cleanup
def eat_food(self):
self.g.game.use_item(0)
print('Eating food')
self.wait_time = 3
self.state = self.wait
def wait(self):
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.cleanup
def cleanup(self):
self.state = self.done
def done(self):
# never gets ran, placeholder
return None
def __init__(self, global_state):
self.g = global_state
self.state = self.idle
self.wait_time = 0
def run(self):
self.state()
class JobStates: class JobStates:
def idle(self): def idle(self):
return [] return []
@ -1726,6 +1782,7 @@ class JobStates:
self.check_threats_states = CheckThreatsStates(self.g) self.check_threats_states = CheckThreatsStates(self.g)
self.gather_wart_states = GatherWartStates(self.g) self.gather_wart_states = GatherWartStates(self.g)
self.gather_crop_states = GatherCropStates(self.g) self.gather_crop_states = GatherCropStates(self.g)
self.eat_food_states = EatFoodStates(self.g)
def run_machines(self, machines): def run_machines(self, machines):
for m in machines: for m in machines:
@ -1744,6 +1801,7 @@ class JobStates:
self.grab_sand_states, self.grab_sand_states,
self.cache_items_states, self.cache_items_states,
self.sleep_with_bed_states, self.sleep_with_bed_states,
self.eat_food_states,
] ]
return machines return machines
@ -1755,6 +1813,7 @@ class JobStates:
self.grab_sand_states, self.grab_sand_states,
self.cache_items_states, self.cache_items_states,
self.sleep_with_bed_states, self.sleep_with_bed_states,
self.eat_food_states,
] ]
self.sleep_with_bed_states.silent = True self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True self.cache_items_states.silent = True
@ -1780,6 +1839,7 @@ class JobStates:
machines = [ machines = [
self.gather_wood_states, self.gather_wood_states,
self.sleep_with_bed_states, self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states, self.cache_items_states,
] ]
return machines return machines
@ -1792,6 +1852,7 @@ class JobStates:
self.plant_tree_states, self.plant_tree_states,
self.grab_sapling_states, self.grab_sapling_states,
self.sleep_with_bed_states, self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states, self.cache_items_states,
] ]
self.sleep_with_bed_states.silent = True self.sleep_with_bed_states.silent = True
@ -1805,6 +1866,7 @@ class JobStates:
machines = [ machines = [
self.gather_wart_states, self.gather_wart_states,
self.sleep_with_bed_states, self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states, self.cache_items_states,
] ]
self.sleep_with_bed_states.silent = True self.sleep_with_bed_states.silent = True
@ -1815,6 +1877,7 @@ class JobStates:
machines = [ machines = [
self.gather_crop_states, self.gather_crop_states,
self.sleep_with_bed_states, self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states, self.cache_items_states,
] ]
self.sleep_with_bed_states.silent = True self.sleep_with_bed_states.silent = True
@ -1826,6 +1889,7 @@ class JobStates:
self.grab_supplies_states, self.grab_supplies_states,
self.fill_blocks_states, self.fill_blocks_states,
self.sleep_with_bed_states, self.sleep_with_bed_states,
self.eat_food_states,
] ]
self.sleep_with_bed_states.silent = True self.sleep_with_bed_states.silent = True
@ -1838,6 +1902,15 @@ class JobStates:
} }
return machines return machines
def loiter(self):
machines = [
self.check_threats_states,
self.sleep_with_bed_states,
self.eat_food_states,
]
self.sleep_with_bed_states.silent = True
return machines
def stop(self): def stop(self):
self.init_machines() self.init_machines()
self.state = self.idle self.state = self.idle

View File

@ -30,6 +30,8 @@ g.job = None
g.correction_count = 0 g.correction_count = 0
g.holding = 0 g.holding = 0
g.afk = False g.afk = False
g.health = 20
g.food = 20
@app.route('/') @app.route('/')
def hello_world(): def hello_world():