Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Check-laundry: A DFHack script to help prevent naked dwarves  (Read 5481 times)

Jazz Cat

  • Bay Watcher
  • Adept stringed instrumentalist
    • View Profile

I got sick of scrolling through my entire unit list to see which ones were naked or wearing worn-out clothes. I also have no idea how fast the average dwarf goes through socks, so I didn't want to make a standing manager order. At any rate, here's the solution: The check-laundry script.

Spoiler: check-laundry.lua (click to show/hide)

Running check-laundry in DFHack will display a small pop-up window with some minimal information: It tells you how many dwarves are wearing new clothing, xslightly worn clothingx, Xmoderately worn clothingX, XXheavily worn clothingXX, and no clothing in each of the important slots: Upper body, lower body, and feet, any of which will lead to bad thoughts if left uncovered. Running the verbose mode with check-laundry -v will give you a more detailed list of which dwarves are lacking what clothes.

Copy the code into a file called check-laundry.lua in the hack/scripts directory to install. Works in .44.10 and presumably a few other similar versions.

Now v1.1 with slightly cleaned-up code.
« Last Edit: June 07, 2018, 06:18:42 pm by Jazz Cat »
Logged
Give your dwarves a pet
My holiday mod (only offensive to elves)
The check-laundry script

Quote
Just give the Crossbow weapon the [AMMO:CROSSBOW] tag in the raws. You can make a crossbow that shoots crossbows.

PatrikLundell

  • Bay Watcher
    • View Profile
Re: Check-laundry: A DFHack script to help prevent naked dwarves
« Reply #1 on: June 07, 2018, 03:37:29 am »

Clothes are worn down to the 'x' level in two years, so if you want to keep your dorfs with a full set of pristine clothes each piece needs to have a replacement every two years. Thus, #dorfs/2 per year for each item.
Logged

Jazz Cat

  • Bay Watcher
  • Adept stringed instrumentalist
    • View Profile
Re: Check-laundry: A DFHack script to help prevent naked dwarves
« Reply #2 on: June 07, 2018, 11:16:17 am »

I still prefer having the option to see an overview, though.
Logged
Give your dwarves a pet
My holiday mod (only offensive to elves)
The check-laundry script

Quote
Just give the Crossbow weapon the [AMMO:CROSSBOW] tag in the raws. You can make a crossbow that shoots crossbows.

PatrikLundell

  • Bay Watcher
    • View Profile
Re: Check-laundry: A DFHack script to help prevent naked dwarves
« Reply #3 on: June 07, 2018, 11:36:57 am »

I still prefer having the option to see an overview, though.
My comment was to your statement that you didn't know what the wear rate was, not criticism on the script itself.
Logged

Jazz Cat

  • Bay Watcher
  • Adept stringed instrumentalist
    • View Profile
Re: Check-laundry: A DFHack script to help prevent naked dwarves
« Reply #4 on: June 07, 2018, 12:53:50 pm »

Ah, fair enough. The original bit about that was more for narrative value than anything else.
Logged
Give your dwarves a pet
My holiday mod (only offensive to elves)
The check-laundry script

Quote
Just give the Crossbow weapon the [AMMO:CROSSBOW] tag in the raws. You can make a crossbow that shoots crossbows.

lethosor

  • Bay Watcher
    • View Profile
Re: Check-laundry: A DFHack script to help prevent naked dwarves
« Reply #5 on: June 07, 2018, 01:01:31 pm »

Speaking of criticism of the script, here's some!
Quote
Code: [Select]
validArgs = validArgs or utils.invert({
This is one of the worst things I've seen in scripts. Don't worry, I know it's not your fault - I just fixed around 30 of these that were in DFHack itself, and no doubt you got this pattern from there or someone else who did.

The reason why this is bad is that even if you change the table passed to invert(), validArgs will not change if the script has already been run, because script globals (including validArgs, once it's assigned) persist across calls. So even if you try to add a new valid argument, it simply won't be recognized. I'm guessing someone decided it would help performance to do this, but invert() is barely time-consuming compared to most other things that scripts do, and it makes no sense just to do it for this one variable.

A couple other minor things:
- Ideally you'd use color names in all calls to pen() (you're doing this in most cases, but missed a few, e.g. 15/7)
Code: [Select]
    if torso == 1 and legs == 1 and foot == 1 and otherFoot == 1 then
        return false
- It's probably more idiomatic to return nil to mean "nothing", then use "if [not] x" later.
- I'm not a huge fan of using human-readable keys (e.g. with spaces) in tables like "check" if they're not also displayed to the user. You can also use dot syntax (t.foo == t["foo"]) if you want. But I guess that's more of a personal preference.
- The output of getCitizenList() is stored in citizenList, which is used only once. The function is also called another time, so either populating the cache once in a more appropriate spot and using that or not caching at all would probably be better.
- You could consider using docstring markers (DFHack uses [====[ and ]====]) around the help text so that it appears in DFHack documentation if it gets included (although it would also have to be RST-formatted).

Anyway, this looks good overall! Nice job making everything local, as well as putting effort into formatting, etc..
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

Jazz Cat

  • Bay Watcher
  • Adept stringed instrumentalist
    • View Profile
Re: Check-laundry: A DFHack script to help prevent naked dwarves
« Reply #6 on: June 07, 2018, 02:19:22 pm »

Quote
no doubt you got this pattern from there or someone else who did.

Yep, copied verbatim out of another DFHack script because I have zero knowledge of lua. What would you recommend instead?

Thanks for the advice on the rest of the stuff, though, I'll go through and clean things up for v 1.1 when I get a chance!
Logged
Give your dwarves a pet
My holiday mod (only offensive to elves)
The check-laundry script

Quote
Just give the Crossbow weapon the [AMMO:CROSSBOW] tag in the raws. You can make a crossbow that shoots crossbows.

lethosor

  • Bay Watcher
    • View Profile
Re: Check-laundry: A DFHack script to help prevent naked dwarves
« Reply #7 on: June 07, 2018, 03:23:13 pm »

Quote
no doubt you got this pattern from there or someone else who did.

Yep, copied verbatim out of another DFHack script because I have zero knowledge of lua. What would you recommend instead?
Oops. Just "validArgs = utils.invert..." should do the trick. "validArgs or x" will give validArgs if it's already defined, which leads to the issue I mentioned if "x" is modified intentionally in the code (e.g. during development).
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.