Add player sparring command
This commit is contained in:
1 parent
1b9f973db3
commit
dbd2831dda
3 files changed
+34
-9
No files matched your search
@@ -477,6 +477,11 @@ class Commands:
|
||||
self.g.job.state = self.g.job.combat
|
||||
reply = 'ok'
|
||||
|
||||
## 1kill - spars with nearby players within 16 blocks
|
||||
if command == 'kill':
|
||||
self.g.job.state = self.g.job.kill
|
||||
reply = 'ok'
|
||||
|
||||
## 1sleep [new] - use a bed; `new` forces bed placement
|
||||
if command == 'sleep':
|
||||
self.g.job.sleep_with_bed_states.force_new_bed = (
|
||||
|
||||
@@ -266,6 +266,8 @@ class JobStates(LegacyJobStates):
|
||||
def __init__(self, global_state):
|
||||
self.g = global_state
|
||||
self.combat_states = combat.CombatStates(global_state)
|
||||
self.kill_states = combat.CombatStates(
|
||||
global_state, target_players=True)
|
||||
self.gather_wood_states = gather_wood.GatherWoodStates(global_state)
|
||||
self.gather_crop_states = gather_crop.GatherCropStates(global_state)
|
||||
self.gather_wart_states = gather_wart.GatherWartStates(global_state)
|
||||
@@ -333,6 +335,9 @@ class JobStates(LegacyJobStates):
|
||||
def combat(self):
|
||||
return [self.combat_states]
|
||||
|
||||
def kill(self):
|
||||
return [self.kill_states]
|
||||
|
||||
def eat(self):
|
||||
return [self.eat_food_states]
|
||||
|
||||
@@ -362,6 +367,8 @@ class JobStates(LegacyJobStates):
|
||||
self.g.game.leave_bed()
|
||||
self.combat_states.state = self.combat_states.idle
|
||||
self.combat_states.target_id = None
|
||||
self.kill_states.state = self.kill_states.idle
|
||||
self.kill_states.target_id = None
|
||||
self.gather_wood_states.state = self.gather_wood_states.idle
|
||||
self.gather_crop_states.state = self.gather_crop_states.idle
|
||||
self.gather_sand_states.state = self.gather_sand_states.idle
|
||||
|
||||
+22
-9
@@ -59,6 +59,10 @@ class CombatStates:
|
||||
self._log('no main-hand sword available; fighting unarmed')
|
||||
self.state = self.find_target
|
||||
|
||||
def _target_entities(self):
|
||||
key = 'players' if self.target_players else 'mobs'
|
||||
return self.g.get(key, {})
|
||||
|
||||
def _coordinates(self, mob):
|
||||
return (float(mob.x), float(mob.y), float(mob.z))
|
||||
|
||||
@@ -102,26 +106,34 @@ class CombatStates:
|
||||
return utils.phyp(approach, grounded_target)
|
||||
|
||||
def _describe(self, mob):
|
||||
if self.target_players:
|
||||
player_uuid = str(mob.get('player_uuid', ''))
|
||||
return self.g.get('player_names', {}).get(player_uuid, 'player')
|
||||
return mobs.MOB_NAMES.get(mob.get('type'), 'unknown mob')
|
||||
|
||||
def find_target(self):
|
||||
search_started = time.monotonic()
|
||||
candidates = []
|
||||
for entity_id, mob in self.g.get('mobs', {}).items():
|
||||
for entity_id, mob in self._target_entities().items():
|
||||
if entity_id in self.skipped_ids:
|
||||
continue
|
||||
mob_type = mob.get('type')
|
||||
if (mob_type not in mobs.MOB_NAMES or
|
||||
mob_type in mobs.NON_MOB_ENTITY_IDS):
|
||||
continue
|
||||
if self.target_players:
|
||||
if entity_id == self.g.get('eid'):
|
||||
continue
|
||||
else:
|
||||
mob_type = mob.get('type')
|
||||
if (mob_type not in mobs.MOB_NAMES or
|
||||
mob_type in mobs.NON_MOB_ENTITY_IDS):
|
||||
continue
|
||||
distance = self._distance(mob)
|
||||
if distance <= self.ACQUIRE_RADIUS:
|
||||
candidates.append((distance, entity_id, mob))
|
||||
|
||||
search_elapsed = time.monotonic() - search_started
|
||||
if not candidates:
|
||||
self._log('no mobs within 16 blocks (scan {:.3f}s)'.format(
|
||||
search_elapsed))
|
||||
noun = 'players' if self.target_players else 'mobs'
|
||||
self._log('no {} within 16 blocks (scan {:.3f}s)'.format(
|
||||
noun, search_elapsed))
|
||||
self.cleanup()
|
||||
return
|
||||
|
||||
@@ -220,7 +232,7 @@ class CombatStates:
|
||||
self.next_path_at = time.monotonic() + self.PATH_RETRY_INTERVAL
|
||||
|
||||
def engage(self):
|
||||
target = self.g.get('mobs', {}).get(self.target_id)
|
||||
target = self._target_entities().get(self.target_id)
|
||||
if target is None:
|
||||
self._log_target_end('lost target entity')
|
||||
self.target_id = None
|
||||
@@ -317,8 +329,9 @@ class CombatStates:
|
||||
self._log('state {} took {:.3f}s'.format(
|
||||
getattr(state, '__name__', 'unknown'), duration))
|
||||
|
||||
def __init__(self, global_state):
|
||||
def __init__(self, global_state, target_players=False):
|
||||
self.g = global_state
|
||||
self.target_players = target_players
|
||||
self.state = self.idle
|
||||
self.target_id = None
|
||||
self.path_target = None
|
||||
|
||||
Reference in new issue
Block a user