How to get the order of events to match online


#1

I am trying to simulate online games of my bot and run them locally so I can further debug scenarios and bugs using my bot’s own logging. I am able to parse the hlt replay file and generate the moves per player, so that I can run my bot against the others using their simulated moves.

The issue I am having is the order of events, particularly in generating/spawning ships. So say in the first turn when every one spawns a ship, the online game will have an event sequence of:

"events": [
                {
                    "type": "spawn",
                    "id": 0,
                    "location": {
                        "y": 33,
                        "x": 33
                    },
                    "energy": 0,
                    "owner_id": 3
                },
                {
                    "type": "spawn",
                    "id": 1,
                    "location": {
                        "y": 33,
                        "x": 14
                    },
                    "energy": 0,
                    "owner_id": 2
                },
                {
                    "type": "spawn",
                    "id": 2,
                    "location": {
                        "y": 14,
                        "x": 14
                    },
                    "energy": 0,
                    "owner_id": 0
                },
                {
                    "type": "spawn",
                    "id": 3,
                    "location": {
                        "y": 14,
                        "x": 33
                    },
                    "energy": 0,
                    "owner_id": 1
                }
            ]

Where as when I try to run the game locally, using the simulated moves, my engine will generate an event sequence of:

"events": [
                {
                    "id": 0,
                    "location": {
                        "y": 14,
                        "x": 14
                    },
                    "type": "spawn",
                    "energy": 0,
                    "owner_id": 0
                },
                {
                    "id": 1,
                    "location": {
                        "y": 14,
                        "x": 33
                    },
                    "type": "spawn",
                    "energy": 0,
                    "owner_id": 1
                },
                {
                    "id": 2,
                    "location": {
                        "y": 33,
                        "x": 14
                    },
                    "type": "spawn",
                    "energy": 0,
                    "owner_id": 2
                },
                {
                    "id": 3,
                    "location": {
                        "y": 33,
                        "x": 33
                    },
                    "type": "spawn",
                    "energy": 0,
                    "owner_id": 3
                }
            ]

In short, online engine generates a sequence of player id 3, 2, 0, 1. Thus player 3 gets to spawn first, then 2, then 0 then 1. Where as in my local machine, the engine has a sequence of player id 0, 1, 2, 3. Initially I thought this was based on who ever sends ‘ready’ or get initialized first, but that doesnt seem to matter.

Does anybody know how I can get around this? Based on the hlt generated on my local machine and the hlt from online, they are both engine version 1.1.5.


#2

I think players order in not deterministic. Everyone send their commands, engine aggregates them and send updated game state to all players. So you can have to sort this list manually if you need order in it for some reason.


#3

I am 100% sure its deterministic. Look at all 4 player games online. Player 3 will always get ship id 0, player 2 ship 1, player 0 ship 2 and player 1 ship 3. Always…


#4

Sorry, I explained it incorrectly. I mean order of how engine process their commands.


#5

So do you know why the online games are always in that order where my local games are always player 0 with ship id 0, player 1 ship id 1, player 2 with ship id 2 and player 3 with shid id 3. I downloaded the latest c++ and python kits and used the halite.exe in it but still getting the same results. Are the online games using a different engine? Though based on the .hlts online, it is the same engine version (1.1.5), so i am not sure whats the difference…


#6

Are you using some community script to parse .hlt file? (replays.py or something similar) Maybe this script do somethng with this order.

Whatever. Can you sort your array of events by owner_id manually? From my point of view it is easier to implement rather than trying to find source of server bug.


#7

I dont think you are understanding me correctly. I created my parser and its parsing everything fine. The issue has nothing to do with the parser or anything else, except on the engine itself. Whether my parser works correctly or not is actually irrelevant. The issue is when i run any 4 player games locally, At turn 0, all players will send a command to spawn ships. At turn 1, player 0 will get ship id 0, player 1 ship id 1, player 2 shid id 2 and player 3 ship id 3. Always. Whereas online, the order is player 3 will always get ship id 0, player 2 ship id 1, player 0 with ship id 2 and player 1 with ship id 3. Always. This is all dependant on the engine. Has nothing to do with hlt replays or parsing anything manually or anything like that. Its how the engine assigns who gets the first ship id. And all im saying is the engine online seems to be acting different than the engine i have locally. And the order is consistent, both online and locally, but the problem is the order doesnt match


#8

Oh, ok. I did not realised that there is a problem with ships ids too.


#9

When you run a 4 player game locally, do you have the same order as mine? Meaning at turn 1 who gets ship id 0, 1, 2, and 3?


#10

Hmm, let me check.


So:
0 1
2 3

And in logs of bot-0 I can see Ship#0 movements and commands.


#11

Thats the same order I am getting locally. Yet all 4 player games online are:
2 3
1 0


#12

Another solution is just in hlt module where player objects is generated hadrcode player id replacement if there is some flag like “python MyBot.py --fix-players-ids”. And if you use python3.7 recreate game.players dict with propoper order after all.


#13

I dont think thats a solution. Its like me placing player 0 to player 3 location, so that he gets ship id 0. But the movement and halite locations will not match and therefore it wont really simulate the online game anymore. I believe the player id is constant with locations
0 1
2 3
Bottom line is what is the difference with online game engine and local game engine? It should act the same way without doing any hacks…


#14

Yes, you are right, it will not work. This is just stupid… So you have to adjust your players iteration order in your bot so it will be 3201.

Or create field player.real_id. Engine will work with his id, but your bot can work with this new field. When players order is normal do player.real_id = player.id while it has weird order replace it with server id. And you have to sort players iteration still.


#15

Yea i guess something like that would work. Seems like its just a pain and unnecessary work. And that if they fix or update the online engines to have a different order, will have to update it again.

Still curious what the difference is lol. Is it posisble the linux version of engine acts differently? I am assuming they run a linux environment online, whereas I am running the windows version. What environment are you using?


#16

Windows too. Also used Linux version for a while when I need to run a lot of games, but I think my bot does not affected by this bug at all. I mostly use commutative operations when iterate over enemies.


#17

Its only a bug if you are trying to simulate an online game on your local machine


#18

I think I do it few times when had some crashes.