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.
That's fine to put them there. The game overwrites the global values on load so it's save/load safe.
It wouldn't hurt to put them in on_init but it doesn't hurt to have them here either.
Re: [MOD 0.14] VehicleSnap
Posted: Fri Dec 16, 2016 7:49 pm
by Arumba
So for the next MP session we do, should we leave it disabled, manually modify and share a version of it based on the previous comments, or is it going to be updated on the portal? I'm more than willing to provide a solid MP testing ground for it. I can get ~50+ players in a game very quickly if it needs to be stress tested or something.
Re: [MOD 0.14] VehicleSnap
Posted: Fri Dec 16, 2016 8:05 pm
by Zaflis
Would you test with the version i uploaded in previous page? download/file.php?id=19401
If that makes a difference i'll upload it to mod portal.
No, you don't need to disable it. The recent 1.14.21 version seemed to fix all the errors even related to mod updating.
Re: [MOD 0.14] VehicleSnap
Posted: Fri Dec 16, 2016 8:14 pm
by Arumba
Zaflis wrote:Would you test with the version i uploaded in previous page? download/file.php?id=19401
If that makes a difference i'll upload it to mod portal.
No, you don't need to disable it. The recent 1.14.21 version seemed to fix all the errors even related to mod updating.
Okay, will try it with MASA session and then later tonight with the large scale MP session we've been running this week. Thanks! Will report back if it breaks the whole world.
*Edit*
We recorded ~3 hours today without any issues. Then again we had no issues in our previous session, but still, things look good on this end.
Re: [MOD 0.14] VehicleSnap
Posted: Sat Dec 17, 2016 4:06 pm
by Nexela
Zaflis wrote:Current start of the script is now:
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.
The player index will stay the same
Re: [MOD 0.14] VehicleSnap
Posted: Sat Dec 17, 2016 8:24 pm
by Zaflis
Good to know. I'll upload it to portal.
Re: [MOD 0.14] VehicleSnap
Posted: Mon May 01, 2017 6:52 am
by MainTango
This mod is absolutely blissful. I have a request but I don't know if it's possible or not. Would you consider adding a hotkey to toggle the mod's functionality on/off during gameplay?
Any key suggestions? It might be that to change keys one would have to open the zip file and edit a file. Shift-V is 1 option that comes to mind.
Also since it's multiplayer compatible mod, i'd have to globally sync the snapping state per player and finding which one is the local self. I just hope it would be simple...
Any key suggestions? It might be that to change keys one would have to open the zip file and edit a file. Shift-V is 1 option that comes to mind.
Also since it's multiplayer compatible mod, i'd have to globally sync the snapping state per player and finding which one is the local self. I just hope it would be simple...
I wish I could be of assistance with the multiplayer thing but I'm just garbage with these things.
As far as key suggestions, shift+V sounds good. You could even do shift+W if you ask me. No idea whether other mods already use that.
Users can remap it should there be any conflicts. The control settings menu even has a Mods tab nowadays
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
--This is the event fired when the hotkey is pressed, It just toggles a variable between true and false
script.on_event("HOTKEYNAME",
function(event)
global.players[event.player_index].snap = not global.players[event.player_index].snap
end
)
--This is ran everytime the game is changed (adding mods upgrading etc) and installed.
local function run_install()
--No harm in running this all the times
global.tick = global.tick or 40
global.players = global.players or {}
for i in pairs(game.players) do
global.players[i] = global.players[i] or {
snap=true,
player_ticks = 0,
last_orientation = 0
}
end
end
script.on_init(run_install)
script.on_configuration_changed(run_install)
--Any time a new player is created run this.
script.on_event(defines.events.on_player_created,
function(event)
global.players[event.player_index] = {
snap = true,
player_ticks = 0,
last_orientation = 0
}
end
)
--The tick handler mostly your original but stores everything in the pdata variable (player data in this case)
script.on_event(defines.events.on_tick,
function()
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 _, player in pairs(game.connected_players) do
local pdata = global.players[player.index]
if player.vehicle 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 math.abs(o - pdata.last_orientation) < 0.001 then
if pdata.player_ticks > 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
pdata.player_ticks = pdata.player_ticks + 1
end
else
pdata.player_ticks = 0
end
pdata.last_orientation = o
end
end
end
end
end
end
)
Re: [MOD 0.15] VehicleSnap
Posted: Mon May 01, 2017 1:19 pm
by Zaflis
That's really nice code. You changed so much though that i don't know for sure if things still work or not. I had checks to ensure that mod works even in the case that it's added after the world was created, or if mod was updated.
That's first reference to player_ticks. Is it safe to compare it to number if it doesn't exist yet? I used to have some crashes in my testing with similar code. I may not have time to test today.
Error while running event VehicleSnap::on_tick (ID 0)
__VehicleSnap__/control.lua:46: attempt to perform arithmetic on field 'lastorientation' (a nil value)
data:extend({
{
type = "custom-input",
name = "VehicleSnap-toggle",
key_sequence = "SHIFT + V",
consuming = "script-only"
}
})
and related locale for controls menu. Will see how goes...
Re: [MOD 0.15] VehicleSnap
Posted: Tue May 02, 2017 10:34 am
by Nexela
Zaflis wrote:That's really nice code. You changed so much though that i don't know for sure if things still work or not. I had checks to ensure that mod works even in the case that it's added after the world was created, or if mod was updated.
That's first reference to player_ticks. Is it safe to compare it to number if it doesn't exist yet? I used to have some crashes in my testing with similar code. I may not have time to test today.
Oooops good catch, fixed above
Looks like I forgot the on_init, The above code is fixed and should handle everything correctly.
All of it is untested though, I will leave that up to you.
Re: [MOD 0.15] VehicleSnap
Posted: Tue May 02, 2017 10:47 am
by Nexela
Just noticed some spots say lastorientation and others say last_orientation fixing above so they are consistent
Re: [MOD 0.15] VehicleSnap
Posted: Tue May 02, 2017 11:21 am
by Zaflis
Yeah i noticed those earlier too, also i needed to use the snap variable in player loop for toggle functionality. I got everything working as far as i can tell, so 1.15.1 is released. I only tested in singleplayer and private multiplayer without others. The mod toggle key can be changed from Factorio's controls menu. And thanks for helps
Re: [MOD 0.15] VehicleSnap
Posted: Tue May 02, 2017 9:49 pm
by MainTango
Hotkey works like a charm for me. Thanks a lot for implementing it
Re: [MOD 0.15] VehicleSnap
Posted: Fri Jul 21, 2017 10:43 pm
by widders
Hey, love the mod although it still can't make up for my inept driving.
One thing I noticed in 0.15.28-30 at least it doesn't work when driving backwards. I don't know if that was intentional but it's a bit annoying sometimes.
Re: [MOD 0.15] VehicleSnap
Posted: Fri Jul 21, 2017 10:54 pm
by widders
I thought I'd have a look and I found the problem. Line 51 of control.lua:
needs a math.abs(player.vehicle.speed) to cover backwards as well, I tried it seems to work fine :p
Re: [MOD 0.15] VehicleSnap
Posted: Fri Jul 21, 2017 11:02 pm
by Zaflis
It was a little delicate subject, in the end i thought reverse is so slow and rarely used that it wouldn't benefit much of snapping. Did you test if tanks reach reverse speed of -0.1? If the number is put too low, one old bug would resurface... That was a tricky one too, that vehicle gets stuck on anything solid such as structures or shoreline. That's why i had to not snap in low speeds.