[MOD 0.17] VehicleSnap

Topics and discussion about specific mods
lxl
Inserter
Inserter
Posts: 49
Joined: Fri Sep 19, 2014 7:59 pm
Contact:

Re: [MOD 0.12.x] VehicleSnap 1.0.2

Post by lxl »

Hello,

When I enter the train i get the following msg.
Error while running the event handler: __VehicleSnap__/control.lua:13: Callable only on car entity type.
(reproduced w/o mods on a new map)

But I truly do love your mod. Now i only crash into my base in corners and not on straight tracks.

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.12.x] VehicleSnap 1.0.2

Post by Zaflis »

Wow, and it so happened i didn't use trains in my most recent world :oops: I have a fix (1.0.3) coming up shortly, i have to test the fixed version. Because of this i can't have unique speed tests per car type anymore (unless i sacrifice tiny bit of performance for more string comparisons), so car's value will lower to that of tanks; 0.1 down from 0.14.

update: I still couldn't get the car stuck to wall in tests while it starts snapping a little earlier.
And 1.0.3 is released.

lxl
Inserter
Inserter
Posts: 49
Joined: Fri Sep 19, 2014 7:59 pm
Contact:

Re: [MOD 0.12.x] VehicleSnap 1.0.2

Post by lxl »

Wow. 45 min until fixed. :mrgreen:

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.13.x] VehicleSnap 1.1.0

Post by Zaflis »

Mod is now updated for 0.13. All features seem working same as before. (Installing mods will disable Steam achievements for your save.)

Jürgen Erhard
Filter Inserter
Filter Inserter
Posts: 298
Joined: Sun Jun 12, 2016 11:29 pm
Contact:

Re: [MOD 0.13.x] VehicleSnap 1.1.0

Post by Jürgen Erhard »

Upload to mod portal, please.

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.13.x] VehicleSnap 1.1.0

Post by Zaflis »

Jürgen Erhard wrote:Upload to mod portal, please.
Done. ( https://mods.factorio.com/mods/Zaflis/VehicleSnap )

Jürgen Erhard
Filter Inserter
Filter Inserter
Posts: 298
Joined: Sun Jun 12, 2016 11:29 pm
Contact:

Re: [MOD 0.13.x] VehicleSnap 1.1.0

Post by Jürgen Erhard »

Just saw you post, I've already noticed it and installed it last night. :D Thanks!

TheTom
Fast Inserter
Fast Inserter
Posts: 185
Joined: Tue Feb 09, 2016 8:33 am
Contact:

Re: [MOD 0.13.x] VehicleSnap 1.1.0

Post by TheTom »

Any chance for a .14 update?

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.13.x] VehicleSnap 1.1.0

Post by Zaflis »

TheTom wrote:Any chance for a .14 update?
Can try in a moment. 0.13 to 0.14 should be just about changing the number to 14 in json file.

Updated and seems to work still.

TheTom
Fast Inserter
Fast Inserter
Posts: 185
Joined: Tue Feb 09, 2016 8:33 am
Contact:

Re: [MOD 0.13.x] VehicleSnap 1.1.0

Post by TheTom »

Zaflis wrote:
TheTom wrote:Any chance for a .14 update?
Can try in a moment. 0.13 to 0.14 should be just about changing the number to 14 in json file.

Updated and seems to work still.
Great. Any reason not to upload it to the mod portal? (Not nagging, really asking - the versioning is NOT properly thought out there, sadly).

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.13.x] VehicleSnap 1.1.0

Post by Zaflis »

TheTom wrote:
Zaflis wrote:Great. Any reason not to upload it to the mod portal? (Not nagging, really asking - the versioning is NOT properly thought out there, sadly).
Sorry, i've not paid attention and even still 0.14 is an experimental version ;) It's updated https://mods.factorio.com/mods/Zaflis/V ... /downloads

I should have propably added this forum link (homepage) in the json too, just didn't want to update the mod for that little change.

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.14.x] VehicleSnap 1.14.0

Post by Zaflis »

New version is out again, hope it still works for you as it did on my tests. I've seen this mod use around 0.02ms of tick time per frame, which was about same as most mods, but some mods managed to get off with less too. So i figured, what processing do i really need to do when nobody is even in a car? LUA is weird, the commands that were used were nothing significant. A for loop from 1 to 1 for example shouldn't be expected to do much, but it does... something does.

So after fixing things, the mod's idle time has gone down to 0.003ms and 0.025 while driving. I simply made the mod wait for 40 ticks if no cars were found during the check. That should be plenty of time to account for player entering a vehicle and gaining speed to start snapping again.

Another fix is for variables, they are now unique per player. I would imagine it may have caused odd behavior in multiplayer when there were at least 2 people driving at the same time.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2915
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: [MOD 0.14] VehicleSnap

Post by Optera »

You could also register your tick function only when a player enters a car. and unregister once he leaves.
My mods use this method to have no drain unless an entity is built.

Arumba
Long Handed Inserter
Long Handed Inserter
Posts: 62
Joined: Sat Aug 29, 2015 6:46 pm
Contact:

Re: [MOD 0.14] VehicleSnap

Post by Arumba »

Any chance we can get this mod updated to be MP friendly? We were using it last night in a ~60 player game and finally identified that this mod was the issue causing our desyncs. I looked at the discussion tab on the modportal and found this from Rseding91:
You need to store your mods data variables in the global table so they're saved/loaded with the game.

Right now because they're stored as local variables they get reset every time a new player joins the game or every time the game is saved, exited, and loaded.

This results in desyncs in MP and breaks the replay in single player.
But.. I really like this mod. So I want to re-enable it. How can we fix it?

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.14] VehicleSnap

Post by Zaflis »

I wouldn't have known that kind of details. If i understand him correctly, you would need to change

Code: Select all

local lastorientation = {}
local player_ticks = {}
local tick = 2
to

Code: Select all

lastorientation = {}
player_ticks = {}
tick = 2
But if they are named like that as global variables, what is the scope for those variable names? Will other scripts see them? If the scope interferes with other mods or main Factorio itself, they could be

Code: Select all

vehiclesnap_lastorientation = {}
vehiclesnap_player_ticks = {}
vehiclesnap_tick = 2
and then rename all the uses for those variable names too.

I could upload this change to see if it helps. (edit: Deleted)
Last edited by Zaflis on Fri Dec 16, 2016 3:46 pm, edited 1 time in total.

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: [MOD 0.14] VehicleSnap

Post by daniel34 »

Rseding91 wrote:You need to store your mods data variables in the global table so they're saved/loaded with the game.

Right now because they're stored as local variables they get reset every time a new player joins the game or every time the game is saved, exited, and loaded.
What he means is using the global.x variables (as in global.lastorientation) to store your data.
See here: https://wiki.factorio.com/Lua/Data_Lifecycle#global

You never need to prefix your variables (like your vehiclesnap_) because each mod runs in a separate instance and can't collide with other mods. (except in data.lua, but you usually won't have that issue there if you properly introduce your variables and the probability of someone else using the same variables is very low)
quick links: log file | graphical issues | wiki

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.14] VehicleSnap

Post by Zaflis »

I'm getting error on line 11: attempt to perform arithmetic on field 'tick' (a nil value)

Code: Select all

global.tick = global.tick - 1
They are initialized in the beginning like

Code: Select all

global.lastorientation = {}
global.player_ticks = {}
global.tick = 2
About those one, i would propably need to check if those are null before assigning default value? But that's related to save sync, and the error above doesn't make sense to me yet.

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: [MOD 0.14] VehicleSnap

Post by daniel34 »

Using the following control.lua (adapted from the current mod portal version) it worked for me:

Code: Select all

local vehiclesnap_amount = 16.0
-- snap amount is the amount of different angles car can drive on, 
-- (360 / vehiclesnap_amount) is the difference between 2 axis
-- car will slowly turn towards such angle axis

global.lastorientation = {}
global.player_ticks = {}
global.tick = 2

script.on_event(defines.events.on_tick, function(event)
  global.tick = global.tick - 1
  if global.tick == 0 then
    -- If noone is in vehicles, take longer delay to do this whole check
    global.tick = 40
    for index, player in pairs(game.players) do
      if player.vehicle and player.vehicle.valid then
        local v = player.vehicle.type
        if v == "car" or v == "tank" then
          global.tick = 2
          if player.vehicle.speed > 0.1 then
            local o = player.vehicle.orientation
            if global.lastorientation[index] == nil then global.lastorientation[index] = 0 end
            if global.player_ticks[index] == nil then global.player_ticks[index] = 0 end
            if math.abs(o - global.lastorientation[index]) < 0.001 then
              if global.player_ticks[index] > 1 then
                local o2 = math.floor(o * vehiclesnap_amount + 0.5) / vehiclesnap_amount
                o = (o * 4.0 + o2) * 0.2
                player.vehicle.orientation = o
              else
                global.player_ticks[index] = global.player_ticks[index] + 1
              end
            else
              global.player_ticks[index] = 0
            end
            global.lastorientation[index] = o;
          end
        end
      end
    end
  end
end)
I tested it only in singleplayer, no guarantees for multiplayer. I'm not sure if Rseding91's advice actually avoids the desync (EDIT: it probably does, as globals are stored with the savegame before a new player downloads it).
local vehiclesnap_amount doesn't need a global as it's always constant.
quick links: log file | graphical issues | wiki

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.14] VehicleSnap

Post by Zaflis »

That seems to only work when you make a new map. But when loading a map with older version of the mod it errors back to main menu.

I was trying to add this aswell (based on https://wiki.factorio.com/Lua/Data_Lifecycle#global the on_load should be correct for this) to make new mod's compatibility for old saves

Code: Select all

script.on_event(script.on_load, function(event)
  if global.lastorientation == nil then global.lastorientation = {} end
  if global.player_ticks == nil then global.player_ticks = {} end
  if global.tick == nil then global.tick = 2 end
end)
But it's giving error aswell that doesn't get logged, but i guess workaround would be to save the map with mod disabled.

edit: That was wrong, should've been

Code: Select all

script.on_load(function()
Last edited by Zaflis on Fri Dec 16, 2016 5:31 pm, edited 1 time in total.

Zaflis
Filter Inserter
Filter Inserter
Posts: 414
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: [MOD 0.14] VehicleSnap

Post by Zaflis »

Current start of the script is now:

Code: Select all

global.lastorientation = {}
global.player_ticks = {}
global.tick = 2

script.on_configuration_changed(function(data)
  if data.mod_changes == nil then return end
  if global.lastorientation == nil then global.lastorientation = {} end
  if global.player_ticks == nil then global.player_ticks = {} end
  if global.tick == nil then global.tick = 2 end
end)
Unless you convince me to put first 3 lines inside on_init, i'd consider it solved :) It worked in all test cases i could think of, but obviously i cannot test multiplayer sync.

Also the mod is using the game.players directly. Say your player index is 4 and you're driving a car. If player index 3 leaves the game, you will now be index 3, no? If that happens you might see your snap variable reset.
Attachments
VehicleSnap_1.14.21.zip
(1.26 KiB) Downloaded 172 times

Post Reply

Return to “Mods”