Skip to content

Conversation

@wsfsbvchr
Copy link
Contributor

@wsfsbvchr wsfsbvchr commented Jun 8, 2023

Followup to DFHack/dfhack#1784 (comment)

Fix for emigration.lua not working since the citizenship logic was changed.

It works at least in 0.47.05, based on some testing by myself. It can be tested relatively easy with stress-inducing scripts, such as this one I created while testing this: https://github.com/wsfsbvchr/dfhack-utils/blob/main/stress-tests.lua

The 50.* version seems to have some different things in the plugin part of the script, so at least adding those might be necessary. Also the exact location of unit's stress has apparently changed?

Fix for emigration.lua not working since the citizenship logic was changed.
@myk002
Copy link
Member

myk002 commented Jun 8, 2023

I'm having a hard time following the changes in this PR. it looks like you've overwritten all the improvements made since 0.47, such as compliance with the enabled API needed for autostart. could you integrate your changes into the existing code instead?

wsfsbvchr added 2 commits June 9, 2023 00:09
Integrated the changes to existing code.
Removed some excess whitespace.
@wsfsbvchr
Copy link
Contributor Author

Yeah, I did the integration and it also seems to passes the checks.

@wsfsbvchr
Copy link
Contributor Author

Of course there's still the fact that I haven't tested if it works in 50.* as well as in 0.47.*.

emigration.lua Outdated
@@ -1,4 +1,5 @@
--Allow stressed dwarves to emigrate from the fortress
-- Updated for 0.47.05 and potentially for 50.08 by wsfsbvchr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I encourage you to add your name to https://github.com/DFHack/dfhack/blob/develop/docs/about/Authors.rst but it's not really appropriate to add it here. Your name will already be recorded in the git history and be visible here: https://github.com/DFHack/scripts/graphs/contributors

@myk002
Copy link
Member

myk002 commented Jun 8, 2023

code looks reasonable (and professional, I might add). could you tell us when you've had a chance to do some testing in v50 to make sure it actually works as advertised? In particular, I'm not confident I understand the long-term implications of assigning a unit to another site.

@wsfsbvchr
Copy link
Contributor Author

I'd imagine they live their lives like any of the other histfigs in those non-player site entities. I got the idea for that while experiencing the bug where the dwarves just immediately return after leaving the map; I figured if they're members in another place, they surely will go there. It's admittedly bit cheaty though.

I'm also not sure what happens without any new entity links. I suppose they might settle somewhere anyway.

Oh, and, I just tried v50 in wine, and apparently in v50 df.global.ui doesn't exist, so I'll need to figure out where it stores the current fortress entity.

Adjusted to use of df.global.plotinfo.
@wsfsbvchr
Copy link
Contributor Author

Apparently it's df.global.plotinfo. v50 feels so clunky and complicated, but looks like there are no errors now, and two dwarves succesfully left my newly started fortress in search for better life.

Comment on lines 4 to 5
--@module = true
--@enable = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two lines are required for the enabled API to function. see https://docs.dfhack.org/en/latest/docs/dev/Lua%20API.html#script-enable-api for details.

emigration.lua Outdated
-- old version: http://dffd.bay12games.com/file.php?id=8404
--@module = true
--@enable = true
--[====[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs have moved to separate .rst files. If you made any changes to the docs, they should go in https://github.com/DFHack/scripts/blob/master/docs/emigration.rst
script files shouldn't have duplicate docs since they just get out of date.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anything relevant to the docs has changed.

Though I don't know if the part in bold is technically true: "Dwarves who can leave in friendly company (e.g. a dwarven merchant caravan) will choose to do so, but extremely stressed dwarves can choose to leave alone, or even in the company of a visiting elven bard!", but I suppose it hasn't been true in many years.

Perhaps it's not intended literally, but as a description of how despaired some of the emigrating dwarves must be?

Comment on lines 7 to 27
local json = require('json')
local persist = require('persist-table')
A happy dwarf (ie with negative stress) will never emigrate.
local GLOBAL_KEY = 'emigration' -- used for state change hooks and persistence
Usage::
enabled = enabled or false
emigration enable|disable
]====]

function isEnabled()
return enabled
end
enabled = enabled or false

local function persist_state()
persist.GlobalTable[GLOBAL_KEY] = json.encode({enabled=enabled})
local args = {...}
if args[1] == "enable" then
enabled = true
elseif args[1] == "disable" then
enabled = false
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part looks like it was accidentally copied from an earlier version. Could you fix the merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I must've uploaded the wrong file or something. Now the correct file should be there.

Comment on lines 115 to 153
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc == SC_MAP_UNLOADED then
enabled = false
return
end

if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
return
dfhack.onStateChange.loadEmigration = function(code)
if code==SC_MAP_LOADED then
if enabled then
print("Emigration enabled.")
event_loop()
else
print("Emigration disabled.")
end
end

local persisted_data = json.decode(persist.GlobalTable[GLOBAL_KEY] or '')
enabled = (persisted_data or {enabled=false})['enabled']
event_loop()
end

if dfhack_flags.module then
return
end

if df.global.gamemode ~= df.game_mode.DWARF or not dfhack.isMapLoaded() then
dfhack.printerr('emigration needs a loaded fortress map to work')
return
if dfhack.isMapLoaded() then
dfhack.onStateChange.loadEmigration(SC_MAP_LOADED)
end

local args = {...}
if dfhack_flags and dfhack_flags.enable then
args = {dfhack_flags.enable_state and 'enable' or 'disable'}
end

if args[1] == "enable" then
enabled = true
elseif args[1] == "disable" then
enabled = false
else
return
end

event_loop()
persist_state()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part also looks like it was overwritten with old code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry!

Proper fix for stuff, hopefully.
One more fix, maybe now...
@wsfsbvchr
Copy link
Contributor Author

I think it should now have the v50 base with my updated emigration solutions, but I can't focus at the moment so here's to hoping.

Removed excess whitespace
Copy link
Member

@myk002 myk002 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good. thanks!

@myk002 myk002 merged commit 011376d into DFHack:master Jun 12, 2023
@wsfsbvchr wsfsbvchr deleted the patch-3 branch June 20, 2023 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants