Player getting stuck in deep water

Place to get help with not working mods / modding interface.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Player getting stuck in deep water

Post by Pi-C »

I'm trying to improve this abandoned mod that allows you to link a vehicle to a car key and summon that vehicle by clicking on a location near you. While the original author apparently only had cars and tanks in mind, the mod works with all kinds of vehicles -- including the boat from Cargo ships.

If you happen to be in the vehicle you just summoned, the vehicle will be copied to the new position -- but the player will be "ejected" and left in the original position. This can be a problem if you're driving in a boat over deep water: The player would be stuck in the water without a chance to get into the boat again or moving anywhere (placing a landfill helps, but that is a dirty workaround!). Therefore, I check whether the summoned car has a driver (that will be the player because only he has the key to the car), and if it does, i copy the driver over to the new car:

Code: Select all

	local driver = car.get_driver()
        if driver then
        	debug("Driver: " .. tostring(driver.player.name))
                debug("Driver position collides with player layer: " .. tostring(driver.player.surface.get_tile(entity.position).collides_with("player-layer")))
                 global.cars[index].set_driver(driver)
        end
This works with a car on land, and with a boat on normal/shallow water -- in other words, it works if driver.player.surface.get_tile(entity.position).collides_with("player-layer") is false. However, if the boat is on deep water -- i.e. driver.player.surface.get_tile(entity.position).collides_with("player-layer") == true -- just the boat is moved to the new location, while the player is left behind. Is there any way I could work around this?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

I can't reproduce this. I spawned a car and teleported it onto a lake, and i can set myself as a driver just fine, even though once in the car i can't get out due to it being on a lake. Teleporting the car around with myself already inside also works fine.
Pi-C wrote: ↑
Wed Sep 04, 2019 1:35 pm
If you happen to be in the vehicle you just summoned, the vehicle will be copied to the new position -
Why are you copying the vehicle instead of teleporting it? Do you have to support cross-surface summoning or some other black magic?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Player getting stuck in deep water

Post by Pi-C »

eradicator wrote: ↑
Wed Sep 04, 2019 1:52 pm
I can't reproduce this. I spawned a car and teleported it onto a lake, and i can set myself as a driver just fine, even though once in the car i can't get out due to it being on a lake. Teleporting the car around with myself already inside also works fine.
Pi-C wrote: ↑
Wed Sep 04, 2019 1:35 pm
If you happen to be in the vehicle you just summoned, the vehicle will be copied to the new position -
Why are you copying the vehicle instead of teleporting it? Do you have to support cross-surface summoning or some other black magic?
Because I didn't know about teleporting. The original mod worked with copying the vehicle, and I just added some changes. But it really seems like teleporting would be much easier, thanks for the hint! I'll give it a try now. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Player getting stuck in deep water

Post by Pi-C »

Thanks, it works great -- and the teleport() function saves a heap of code! I wonder why the author of the original mod didn't use it. Could it be that teleport() has not been available at the time (the original was published about 20 months ago)?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

Pi-C wrote: ↑
Thu Sep 05, 2019 4:42 pm
Thanks, it works great -- and the teleport() function saves a heap of code! I wonder why the author of the original mod didn't use it. Could it be that teleport() has not been available at the time (the original was published about 20 months ago)?
I actually looked at the doc and it's been there since at least 12.35 (earlier docs are not available online). It's possible that there are some edge cases that teleport doesn't handle well, or that they simply didn't know it existed.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

mrvn
Smart Inserter
Smart Inserter
Posts: 5709
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Player getting stuck in deep water

Post by mrvn »

I noticed 2 problems with teleporting so far:

1) if you teleport into a map chunk that isn't generated yet bad things happen

- The player character disappears or assigning one is ignored or something. Anyway, suddenly no character is visible.
- The map chunk will be generated with the auto placement rules and shown before on_chunk_generated() is called.
- Same for surrounding chunks but with a delay.
- When on_chunk_generated() finally is called don't destroy the player character if you destroy entities.

Generate the chunk first and force generation of the outstanding chunks even if that makes the game pause for a second. I don't force generate surrounding chunks though.

2) Characters can't be assigned to players on different surfaces. Which I believes applies to drivers or passengers of vehicles too.

Teleport to the surface where the character is, assign it and then teleport where you need to go.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Player getting stuck in deep water

Post by Pi-C »

Sorry for taking so long to reply!
mrvn wrote: ↑
Wed Sep 11, 2019 6:55 pm
I noticed 2 problems with teleporting so far:

1) if you teleport into a map chunk that isn't generated yet bad things happen

Generate the chunk first and force generation of the outstanding chunks even if that makes the game pause for a second. I don't force generate surrounding chunks though.
This shouldn't be an issues in my case: Normally, you teleport the vehicle to a place next to the player character, where "next to" means it is within the character's reach (i.e. distance between player position and desired vehicle location is less than or equal to player.reach_distance). Also, per default long reach is disabled (i.e. max_distance = player.reach_distance - player.character_reach_distance_bonus) for summoning a vehicle. There is a startup option, though, that would allow to use long reach (i.e., max_distance = player.reach_distance). Would one be able to summon a vehicle into an uncharted chunk by clicking there?
2) Characters can't be assigned to players on different surfaces. Which I believes applies to drivers or passengers of vehicles too.
I suppose this wouldn't be relevant either. The way I understand the original mod's code, once a vehicle has been claimed by one player, no other player should be able to use it, so no vehicle claimed by somebody could have passengers. This would leave the case that a driver (which must be the owner, i.e. the player ordering the teleporting) is in the vehicle. If the owner is in the vehicle, he could only summon the vehicle to a location on his surface -- right? If the player is on a different surface, there would be no players inside the vehicle and teleporting it to the player's surface shouldn't have side effects. Did I get that correctly?

I'm pretty sure the mod works correctly. However, there are some little things I couldn't test because I only play in single player mode and these things are only relevant for multiplayer. Is there a cheap way to do multiplayer tests locally? I guess I would have to install the headless server and connect to that with two instances of the normal game. Would that even work -- without overburdening my computer (about 6 years old, with 16 GB of RAM and an Geforce GTX 1050 with 2 GB)? Or is there a fool-proof way to use only one instance of Factorio by creating a character of a second force (with console commands) and then switching control between these characters? Note the "fool-proof" requirement, please! It wouldn't help much if I could mess up something in creating the second force. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

Pi-C wrote: ↑
Sun Sep 15, 2019 3:54 pm
mrvn wrote: ↑
Wed Sep 11, 2019 6:55 pm
1) if you teleport into a map chunk that isn't generated yet bad things happen
Normally, you teleport the vehicle to a place next to the player character, where "next to" means it is within the character's reach
Does that mean the player can chose the exact position? I assumed the position would just be randomly chosen with LuaSurface.find_non_colliding_position_in_box(), which - if you constrain it to the chunk that the player is *in* currently - also solve the ungenerated chunk problem. The player can't move into ungenerated chunks on it's own as far as i know.
Pi-C wrote: ↑
Sun Sep 15, 2019 3:54 pm
mrvn wrote: ↑
Wed Sep 11, 2019 6:55 pm
2) Characters can't be assigned to players on different surfaces. Which I believes applies to drivers or passengers of vehicles too.
I thought that with the new teleport method you're not un/assigning characters from/to players at all anymore. In which case this never occurs.
Pi-C wrote: ↑
Sun Sep 15, 2019 3:54 pm
Note the "fool-proof" requirement, please! It wouldn't help much if I could mess up something in creating the second force. :-)
Install it again under a different user, then use "switch users" to jump between the two running instancs? Dunno if that works or is fool-proof. Creating extra characters doesn't simulate a second player so it won't work for that, neither does running headless.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Player getting stuck in deep water

Post by Pi-C »

eradicator wrote: ↑
Sun Sep 15, 2019 4:51 pm
Pi-C wrote: ↑
Sun Sep 15, 2019 3:54 pm
mrvn wrote: ↑
Wed Sep 11, 2019 6:55 pm
1) if you teleport into a map chunk that isn't generated yet bad things happen
Normally, you teleport the vehicle to a place next to the player character, where "next to" means it is within the character's reach
Does that mean the player can chose the exact position?
Bad phrasing, perhaps? With "normally", I meant the default behavior (i.e. "long reach" off) in the context of my mod. You craft the vehicle keys. Then, you click somewhere on the ground with your keys to summon the vehicle there. The mod listens to defines.events.on_built_entity to decide whether the vehicle can be teleported to the new position.
eradicator wrote: ↑
Sun Sep 15, 2019 4:51 pm
Pi-C wrote: ↑
Sun Sep 15, 2019 3:54 pm
mrvn wrote: ↑
Wed Sep 11, 2019 6:55 pm
2) Characters can't be assigned to players on different surfaces. Which I believes applies to drivers or passengers of vehicles too.
I thought that with the new teleport method you're not un/assigning characters from/to players at all anymore. In which case this never occurs.
OK, I'll try if I can test this. :-)
eradicator wrote: ↑
Sun Sep 15, 2019 4:51 pm
Pi-C wrote: ↑
Sun Sep 15, 2019 3:54 pm
Note the "fool-proof" requirement, please! It wouldn't help much if I could mess up something in creating the second force. :-)
Install it again under a different user, then use "switch users" to jump between the two running instancs? Dunno if that works or is fool-proof. Creating extra characters doesn't simulate a second player so it won't work for that, neither does running headless.
But wouldn't I need headless to host the game that both Factorio clients connect to?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

Still means that the player chose the location. Which makes it impossible to enforce summoning to exactly the same chunk (which would lead to generally weird situations anyway i guess).

One of the clients can host the game.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Player getting stuck in deep water

Post by Pi-C »

eradicator wrote: ↑
Sun Sep 15, 2019 5:40 pm
Still means that the player chose the location. Which makes it impossible to enforce summoning to exactly the same chunk
The vehicle sits somewhere -- that chunk must be charted. The player location definitely is in a charted chunk. Without long reach, the new position must be either in the same chart as the player location, or in an adjacent chunk. The only problem would be if some other mod would give "infinite" reach to the player, so that he would have a reach of thousands of tiles and could build something in uncharted area (would the game even allow that?).
One of the clients can host the game.
How? I guess I need to pass some options to ./factorio. I did

Code: Select all

./factorio --create test
./factorio --start-server test.zip
./factorio --start-server test.zip --mp-connect 0.0.0.0:34197
The server starts:

Code: Select all

   2.872 Info UDPSocket.cpp:27: Opening socket at (IP ADDR:({0.0.0.0:34197}))
   2.872 Hosting game at IP ADDR:({0.0.0.0:34197})
   2.975 Info HttpSharedState.cpp:54: Downloading https://auth.factorio.com/generate-server-padlock-2
   3.564 Info AuthServerConnector.cpp:109: Obtained serverPadlock for serverHash (…) from the auth server.
   3.564 Info ServerMultiplayerManager.cpp:776: updateTick(0) changing state from(CreatingGame) to(InGame)
Then nothing happens until I send an interrupt -- no loading screen, just nothing. Must be something stupid I've forgotten. :-D
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

Under "normal circumstances" i don't think the player can ever "see" an uncharted chunk, i.e. have camera view on one. But if the map is really slow and generation is lagging and the player is really fast they might be able to run faster than map gen. It's all very edgy edge cases, force_generate_chunks might cause lag..., how about simply calling is_chunk_generated() onto the target position and if it's not generated silently fail. That should prevent any possible bugs and is simple enough to not cause additional ones.

The most popular mod "Long Reach" by default gives the player a reach of 10km (10000 tiles). I think that's "infinite" enough for any edge cases it might apply to.

@Hosting, dunno, i meant this:
host.png
host.png (22.21 KiB) Viewed 2040 times
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Player getting stuck in deep water

Post by Pi-C »

eradicator wrote: ↑
Sun Sep 15, 2019 6:36 pm
how about simply calling is_chunk_generated() onto the target position and if it's not generated silently fail.
Makes sense! (Might fail with a message, though.) Would it make sense to do the same thing for surfaces? I.e., if the vehicle is on another surface than the player, let the action fail as well? It would prevent all the problems mrvn has pointed out -- but would it be acceptable for players? I've never played on more surfaces than one …
@Hosting, dunno, i meant this:
host.png
Thanks, that was so easy, worked almost immediately! I've made the mistake to look for network related settings under "Settings" -- never noticed the Multiplayer button in the "Play" menu before. :-)

Good thing, too, that I can test multiplayer mode now. There are already some things that don't work as expected. Now, if only the two instances of the game wouldn't slow down everything so much …
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

Pi-C wrote: ↑
Sun Sep 15, 2019 7:25 pm
eradicator wrote: ↑
Sun Sep 15, 2019 6:36 pm
how about simply calling is_chunk_generated() onto the target position and if it's not generated silently fail.
Makes sense! (Might fail with a message, though.) Would it make sense to do the same thing for surfaces? I.e., if the vehicle is on another surface than the player, let the action fail as well? It would prevent all the problems mrvn has pointed out -- but would it be acceptable for players? I've never played on more surfaces than one …
I said "silently" because i think if the player clicks a "black" chunk and it doesn't work it should be pretty obvious to them why it didn't work. And more messages mean more translation mean more work :p. And you (probably) can't spawn a message at the cursor position because the chunk doesn't exist!

Just get Factorissimo2 and do your own surface experiements. It's probably the most popular surface based mod. Making it generally fail for cross-surface just because there's a 0.1% chance that it might fail seems like a bad solution. As long as there's no passenger/driver in the car it'll always work. And even if there's one it'll still work if the character is attached to a player. So all you need to do is "if not character.player then fail() end" [Edit 1:see below] on both the passenger and the driver. (All "as far as i know", don't forget to do your own testing ;).)

Edit 1:

Code: Select all

local function check(car)
  local x = car.get_driver() --LuaEntity or LuaPlayer or nil!
  local y = car.get_passenger()
  if x and not (x.is_player() or x.player) then return false end
  if y and not (y.is_player() or y.player) then return false end
  return true
  end
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

Edit 2:
All the trapholes to avoid. Slightly shorter version.

Code: Select all

local function can_teleport(car)
  for _,x in pairs{car.get_driver(), car.get_passenger()} do
    if not (x.is_player() or x.player) then return false end
    end
  return true
  end
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

mrvn
Smart Inserter
Smart Inserter
Posts: 5709
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Player getting stuck in deep water

Post by mrvn »

Pi-C wrote: ↑
Sun Sep 15, 2019 3:54 pm
Sorry for taking so long to reply!
mrvn wrote: ↑
Wed Sep 11, 2019 6:55 pm
2) Characters can't be assigned to players on different surfaces. Which I believes applies to drivers or passengers of vehicles too.
I suppose this wouldn't be relevant either. The way I understand the original mod's code, once a vehicle has been claimed by one player, no other player should be able to use it, so no vehicle claimed by somebody could have passengers. This would leave the case that a driver (which must be the owner, i.e. the player ordering the teleporting) is in the vehicle. If the owner is in the vehicle, he could only summon the vehicle to a location on his surface -- right? If the player is on a different surface, there would be no players inside the vehicle and teleporting it to the player's surface shouldn't have side effects. Did I get that correctly?
1) Vehicles can have passengers.
2) When you enter for example the BlueprintLabl while in a vehicle the vehicle remains behind as does the character. The player could then summon the vehicle to the BlueprintLab. No idea what happens then when the player exits the BlueprintLab (should be put back into the vehicle?) or then the vehicle (character on wrong surface? Does the game teleport the character with the vehicle or does the mod have to do that?).

mrvn
Smart Inserter
Smart Inserter
Posts: 5709
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Player getting stuck in deep water

Post by mrvn »

eradicator wrote: ↑
Sun Sep 15, 2019 6:36 pm
Under "normal circumstances" i don't think the player can ever "see" an uncharted chunk, i.e. have camera view on one. But if the map is really slow and generation is lagging and the player is really fast they might be able to run faster than map gen. It's all very edgy edge cases, force_generate_chunks might cause lag..., how about simply calling is_chunk_generated() onto the target position and if it's not generated silently fail. That should prevent any possible bugs and is simple enough to not cause additional ones.

The most popular mod "Long Reach" by default gives the player a reach of 10km (10000 tiles). I think that's "infinite" enough for any edge cases it might apply to.

@Hosting, dunno, i meant this:
host.png
It's easy to run over the edge of the map, especially in sandbox mode.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

mrvn wrote: ↑
Mon Sep 16, 2019 12:03 pm
eradicator wrote: ↑
Sun Sep 15, 2019 6:36 pm
[if] player is really fast [...]
It's easy to run over the edge of the map, especially in sandbox mode.
Hate to repeat myself so i'll just quote...
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

mrvn
Smart Inserter
Smart Inserter
Posts: 5709
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Player getting stuck in deep water

Post by mrvn »

eradicator wrote: ↑
Mon Sep 16, 2019 12:44 pm
mrvn wrote: ↑
Mon Sep 16, 2019 12:03 pm
eradicator wrote: ↑
Sun Sep 15, 2019 6:36 pm
[if] player is really fast [...]
It's easy to run over the edge of the map, especially in sandbox mode.
Hate to repeat myself so i'll just quote...
If by "really fast" you mean driving a car then yes.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Player getting stuck in deep water

Post by eradicator »

mrvn wrote: ↑
Mon Sep 16, 2019 12:46 pm
If by "really fast" you mean driving a car then yes.
Maybe my toaster is simply too fast. But w/e, checking is_chunk_generated() is trival as i already said. What's your point?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to β€œModding help”