Add option to group bots

This commit is contained in:
oldnapalm
2025-06-16 18:49:39 -03:00
parent c78b4232cf
commit cddbd28b73
3 changed files with 25 additions and 8 deletions

View File

@@ -319,6 +319,7 @@ To enable support for multiple users perform the steps below:
* Create a file ``enable_bots.txt`` inside the ``storage`` folder to load ghosts as bots, they will keep riding around regardless of the route you are riding.
* Optionally, ``enable_bots.txt`` can contain a multiplier value (be careful, if the resulting number of bots is too high, it may cause performance issues or not work at all).
* Type ``.groupbots`` in chat to group the bots.
* Names, nationalities and equipment can be customized by creating a file ``bot.txt`` inside the ``storage`` folder. The script ``get_pro_names.py`` can be used to populate this file.
* If you want some random bots, check [this repository](https://github.com/oldnapalm/zoffline-bots).
</details>

View File

@@ -444,14 +444,11 @@ def regroup_ghosts(player_id):
if not ghosts.started and ghosts.play:
ghosts.started = True
for g in ghosts.play:
states = [(s.roadTime, s.distance) for s in g.route.states if zo.road_id(s) == zo.road_id(p) and zo.is_forward(s) == zo.is_forward(p)]
if states:
c = min(states, key=lambda x: sum(abs(r - d) for r, d in zip((p.roadTime, p.distance), x)))
g.position = 0
while g.route.states[g.position].roadTime != c[0] or g.route.states[g.position].distance != c[1]:
g.position += 1
if is_ahead(p, g.route.states[g.position].roadTime):
g.position += 1
n = zo.nearest(p, g)
if n != None:
if is_ahead(p, g.route.states[n].roadTime):
n += 1
g.position = n
ghosts.last_play = 0
def load_pace_partners():

View File

@@ -3354,6 +3354,16 @@ def save_bookmark(state, name):
with open(os.path.join(bookmarks_dir, name + '.bin'), 'wb') as f:
f.write(state.SerializeToString())
def nearest(p, b):
i = None
states = [(s.roadTime, s.distance) for s in b.route.states if road_id(s) == road_id(p) and is_forward(s) == is_forward(p)]
if states:
i = 0
n = min(states, key=lambda x: sum(abs(r - d) for r, d in zip((p.roadTime, p.distance), x)))
while b.route.states[i].roadTime != n[0] or b.route.states[i].distance != n[1]:
i += 1
return i
@app.route('/relay/worlds/attributes', methods=['POST'])
@jwt_to_session_cookie
@login_required
@@ -3373,6 +3383,15 @@ def relay_worlds_attributes():
command = chat_message.message[1:]
if command == 'regroup':
regroup_ghosts(chat_message.player_id)
elif command == 'groupbots':
if not MULTIPLAYER or chat_message.player_id == 1 or current_user.is_admin:
for bot in global_bots.keys():
if bot % 1000000 < 10000: # not a duplicate
n = nearest(state, global_bots[bot])
if n != None:
global_bots[bot].position = n
else:
send_message('Permission denied', recipients=[chat_message.player_id])
elif command == 'position':
logger.info('course %s road %s isForward %s roadTime %s route %s' % (get_course(state), road_id(state), is_forward(state), state.roadTime, state.route))
elif command.startswith('bookmark') and len(command) > 9: