Add state machine for eating food

master
Tanner Collin 3 years ago
parent 029ef72ea5
commit 7217686716
  1. 67
      game.py
  2. 15
      items.py
  3. 73
      jobs.py
  4. 2
      main.py

@ -336,6 +336,7 @@ class Game:
register(self.handle_respawn, clientbound.play.RespawnPacket)
register(self.handle_player_list, clientbound.play.PlayerListItemPacket)
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_packet, Packet, early=True)
@ -477,10 +478,6 @@ class Game:
reply = 'ok'
raise
if command == 'break':
self.break_block(blocks.TEST_BLOCK)
reply = 'ok'
if command == 'inv':
inv_list = []
for i in self.g.inv.values():
@ -497,27 +494,6 @@ class Game:
item = int(data)
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':
reply = str(self.g.chunks.get_loaded_area())
@ -651,6 +627,10 @@ class Game:
else:
reply += ', I need a bed'
if command == 'loiter':
self.g.job.state = self.g.job.loiter
reply = 'ok'
if command == 'stop':
bot.init(self.g)
reply = 'ok'
@ -745,6 +725,33 @@ class Game:
else:
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 ##########################
if authed:
@ -1081,6 +1088,16 @@ class Game:
self.g.player_names[action.uuid] = action.name
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):
if self.g.breaking:
self.animate()

@ -49,6 +49,15 @@ SAPLINGS = [
'dark_oak_sapling',
]
FOOD = [
'cooked_porkchop',
'cooked_beef',
'bread',
'cooked_chicken',
'cooked_cod',
'cooked_salmon',
]
BED_IDS = set()
for item_name in BEDS:
BED_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id'])
@ -61,6 +70,10 @@ AXE_IDS = set()
for item_name in AXES:
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()
for item_name in SAPLINGS:
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')
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])

@ -1707,6 +1707,62 @@ class FillBlocksStates:
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:
def idle(self):
return []
@ -1726,6 +1782,7 @@ class JobStates:
self.check_threats_states = CheckThreatsStates(self.g)
self.gather_wart_states = GatherWartStates(self.g)
self.gather_crop_states = GatherCropStates(self.g)
self.eat_food_states = EatFoodStates(self.g)
def run_machines(self, machines):
for m in machines:
@ -1744,6 +1801,7 @@ class JobStates:
self.grab_sand_states,
self.cache_items_states,
self.sleep_with_bed_states,
self.eat_food_states,
]
return machines
@ -1755,6 +1813,7 @@ class JobStates:
self.grab_sand_states,
self.cache_items_states,
self.sleep_with_bed_states,
self.eat_food_states,
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
@ -1780,6 +1839,7 @@ class JobStates:
machines = [
self.gather_wood_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
return machines
@ -1792,6 +1852,7 @@ class JobStates:
self.plant_tree_states,
self.grab_sapling_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
self.sleep_with_bed_states.silent = True
@ -1805,6 +1866,7 @@ class JobStates:
machines = [
self.gather_wart_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
self.sleep_with_bed_states.silent = True
@ -1815,6 +1877,7 @@ class JobStates:
machines = [
self.gather_crop_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
self.sleep_with_bed_states.silent = True
@ -1826,6 +1889,7 @@ class JobStates:
self.grab_supplies_states,
self.fill_blocks_states,
self.sleep_with_bed_states,
self.eat_food_states,
]
self.sleep_with_bed_states.silent = True
@ -1838,6 +1902,15 @@ class JobStates:
}
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):
self.init_machines()
self.state = self.idle

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

Loading…
Cancel
Save