Skip to content

Commit 5a892c3

Browse files
committed
Fix bugs found while working on luacheck
1 parent 7f18d2c commit 5a892c3

22 files changed

+152
-151
lines changed

fix/blood-del.lua

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Makes it so that future caravans won't bring barrels full of blood, ichor, or go
88
99
]====]
1010
local my_entity=df.historical_entity.find(df.global.ui.civ_id)
11-
local sText=" "
1211
local k=0
1312
local v=1
1413

@@ -17,34 +16,34 @@ for x,y in pairs(df.global.world.entities.all) do
1716
k=0
1817
while k < #my_entity.resources.misc_mat.extracts.mat_index do
1918
v=my_entity.resources.misc_mat.extracts.mat_type[k]
20-
sText=dfhack.matinfo.decode(v,my_entity.resources.misc_mat.extracts.mat_index[k])
21-
if (sText==nil) then
19+
local mat=dfhack.matinfo.decode(v,my_entity.resources.misc_mat.extracts.mat_index[k])
20+
if (mat==nil) then
2221
--LIQUID barrels
2322
my_entity.resources.misc_mat.extracts.mat_type:erase(k)
2423
my_entity.resources.misc_mat.extracts.mat_index:erase(k)
2524
k=k-1
2625
else
27-
if(sText.material.id=="BLOOD") then
26+
if(mat.material.id=="BLOOD") then
2827
my_entity.resources.misc_mat.extracts.mat_type:erase(k)
2928
my_entity.resources.misc_mat.extracts.mat_index:erase(k)
3029
k=k-1
3130
end
32-
if(sText.material.id=="ICHOR") then
31+
if(mat.material.id=="ICHOR") then
3332
my_entity.resources.misc_mat.extracts.mat_type:erase(k)
3433
my_entity.resources.misc_mat.extracts.mat_index:erase(k)
3534
k=k-1
3635
end
37-
if(sText.material.id=="GOO") then
36+
if(mat.material.id=="GOO") then
3837
my_entity.resources.misc_mat.extracts.mat_type:erase(k)
3938
my_entity.resources.misc_mat.extracts.mat_index:erase(k)
4039
k=k-1
4140
end
42-
if(sText.material.id=="SWEAT") then
41+
if(mat.material.id=="SWEAT") then
4342
my_entity.resources.misc_mat.extracts.mat_type:erase(k)
4443
my_entity.resources.misc_mat.extracts.mat_index:erase(k)
4544
k=k-1
4645
end
47-
if(sText.material.id=="TEARS") then
46+
if(mat.material.id=="TEARS") then
4847
my_entity.resources.misc_mat.extracts.mat_type:erase(k)
4948
my_entity.resources.misc_mat.extracts.mat_index:erase(k)
5049
k=k-1

fix/diplomats.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ for _,ent in pairs(df.global.world.entities.all) do
5151
if ent.type == df.historical_entity_type.Civilization and ent.entity_raw.flags.TREE_CAP_DIPLOMACY then
5252
checked = checked + 1
5353

54-
update = true
54+
local update = true
5555
local found_position
5656
-- see if we need to add a new position or modify an existing one
5757
for _,pos in pairs(ent.positions.own) do
@@ -72,12 +72,12 @@ for _,ent in pairs(df.global.world.entities.all) do
7272
if update then
7373
-- either there's no diplomat, or there is one and it's got the wrong responsibilities
7474
if not found_position then
75-
found_position = add_guild_rep( ent )
75+
found_position = update_pos( ent )
7676
end
7777
-- assign responsibilities
78-
found_position.responsibilities.MAKE_INTRODUCTIONS = true
79-
found_position.responsibilities.MAKE_PEACE_AGREEMENTS = true
80-
found_position.responsibilities.MAKE_TOPIC_AGREEMENTS = true
78+
found_position.responsibilities.MAKE_INTRODUCTIONS = 1
79+
found_position.responsibilities.MAKE_PEACE_AGREEMENTS = 1
80+
found_position.responsibilities.MAKE_TOPIC_AGREEMENTS = 1
8181
end
8282
-- make sure the diplomat position, whether we created it or not, is set up for proper assignment
8383
local assign = true

fix/dry-buckets.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ local emptied = 0
1313
local water_type = dfhack.matinfo.find('WATER').type
1414

1515
for _,item in ipairs(df.global.world.items.all) do
16-
container = dfhack.items.getContainer(item)
16+
local container = dfhack.items.getContainer(item)
1717
if container ~= nil
1818
and container:getType() == df.item_type.BUCKET
1919
and not (container.flags.in_job or container.flags.in_building)

fix/merchants.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ for _,ent in pairs(df.global.world.entities.all) do
5050
if ent.type == df.historical_entity_type.Civilization and ent.entity_raw.flags.MERCHANT_NOBILITY then
5151
checked = checked + 1
5252

53-
update = true
53+
local update = true
5454
-- see if we need to add a new position or modify an existing one
5555
local found_position
5656
for _,pos in pairs(ent.positions.own) do
@@ -72,8 +72,8 @@ for _,ent in pairs(df.global.world.entities.all) do
7272
found_position = add_guild_rep(ent)
7373
end
7474
-- assign responsibilities
75-
found_position.responsibilities.ESTABLISH_COLONY_TRADE_AGREEMENTS = true
76-
found_position.responsibilities.TRADE=true
75+
found_position.responsibilities.ESTABLISH_COLONY_TRADE_AGREEMENTS = 1
76+
found_position.responsibilities.TRADE = 1
7777
end
7878

7979
-- make sure the guild rep position, whether we created it or not, is set up for proper assignment

fix/retrieve-units.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ such as the following:
2323
2424
]====]
2525

26-
utils = require('utils')
26+
local utils = require('utils')
2727

2828
function shouldRetrieve(unit)
2929
if unit.flags1.incoming then

fix/stable-temp.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ local args = {...}
1313
local apply = (args[1] == 'apply')
1414

1515
local count = 0
16-
local types = {}
16+
local types = {} --as:number[]
1717

1818
local function update_temp(item,btemp)
1919
if item.temperature.whole ~= btemp then
@@ -34,7 +34,7 @@ local function update_temp(item,btemp)
3434
end
3535
end
3636

37-
for _,sub in ipairs(dfhack.items.getContainedItems(item)) do
37+
for _,sub in ipairs(dfhack.items.getContainedItems(item)) do --as:df.item_actual
3838
update_temp(sub,btemp)
3939
end
4040

@@ -45,7 +45,7 @@ end
4545

4646
local last_frame = df.global.world.frame_counter-1
4747

48-
for _,item in ipairs(df.global.world.items.all) do
48+
for _,item in ipairs(df.global.world.items.all) do --as:df.item_actual
4949
if item.flags.on_ground and df.item_actual:is_instance(item) and
5050
item.temp_updated_frame == last_frame then
5151
local pos = item.pos

fix/stuck-merchants.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- Based on "dismissmerchants" by PatrikLundell:
33
-- http://www.bay12forums.com/smf/index.php?topic=159297.msg7257447#msg7257447
44

5-
help = [====[
5+
local help = [====[
66
77
fix/stuck-merchants
88
===================

fix/tile-occupancy.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function findUnit(x, y, z)
2626
return false
2727
end
2828

29-
cursor = df.global.cursor
30-
changed = false
29+
local cursor = df.global.cursor
30+
local changed = false
3131
function report(flag)
3232
print('Cleared occupancy flag: ' .. flag)
3333
changed = true
@@ -37,10 +37,10 @@ if cursor.x == -30000 then
3737
qerror('Cursor not active.')
3838
end
3939

40-
occ = dfhack.maps.getTileBlock(pos2xyz(cursor)).occupancy[cursor.x % 16][cursor.y % 16]
40+
local occ = dfhack.maps.getTileBlock(pos2xyz(cursor)).occupancy[cursor.x % 16][cursor.y % 16]
4141

42-
if occ.building ~= 0 and not dfhack.buildings.findAtTile(pos2xyz(cursor)) then
43-
occ.building = 0
42+
if occ.building and not dfhack.buildings.findAtTile(pos2xyz(cursor)) then
43+
occ.building = df.tile_building_occ.None
4444
report('building')
4545
end
4646

modtools/outside-only.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ Arguments::
2424
local eventful = require 'plugins.eventful'
2525
local utils = require 'utils'
2626

27+
--luacheck: global
2728
buildingType = utils.invert({'EITHER','OUTSIDE_ONLY','INSIDE_ONLY'})
28-
registeredBuildings = registeredBuildings or {}
29+
--luacheck: global
30+
registeredBuildings = registeredBuildings or {} --as:number[]
31+
--luacheck: global
2932
checkEvery = checkEvery or 100
33+
--luacheck: global
3034
timeoutId = timeoutId or nil
3135

3236
eventful.enableEvent(eventful.eventType.UNLOAD,1)
@@ -98,7 +102,7 @@ eventful.onBuildingCreatedDestroyed.outsideOnly = function(buildingId)
98102
checkBuildings()
99103
end
100104

101-
validArgs = utils.invert({
105+
local validArgs = utils.invert({
102106
'help',
103107
'clear',
104108
'checkEvery',

modtools/projectile-trigger.lua

+10-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ This triggers dfhack commands when projectiles hit their targets. Usage::
2727
local eventful = require 'plugins.eventful'
2828
local utils = require 'utils'
2929

30-
materialTriggers = materialTriggers or {}
30+
--luacheck: global
31+
materialTriggers = materialTriggers or {} --as:{_type:table,command:__arg,material:__arg}[][]
3132

3233
eventful.enableEvent(eventful.eventType.UNLOAD,1)
3334
eventful.onUnload.projectileTrigger = function()
@@ -56,18 +57,18 @@ end
5657

5758
eventful.onProjItemCheckImpact.expansion = function(projectile)
5859
local matStr = dfhack.matinfo.decode(projectile.item):getToken()
59-
local table = {}
60-
table.pos = projectile.cur_pos
61-
table.projectile = projectile
62-
table.item = projectile.item
6360
for _,args in ipairs(materialTriggers[matStr] or {}) do
64-
utils.fillTable(args,table)
65-
processTrigger(args)
66-
utils.unfillTable(args,table)
61+
processTrigger({
62+
pos = projectile.cur_pos,
63+
projectile = projectile,
64+
item = projectile.item,
65+
command = args.command,
66+
material = args.material
67+
})
6768
end
6869
end
6970

70-
validArgs = utils.invert({
71+
local validArgs = utils.invert({
7172
'help',
7273
'clear',
7374
'command',

modtools/random-trigger.lua

+14-12
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ Arguments::
7676
local utils = require 'utils'
7777
local eventful = require 'plugins.eventful'
7878

79-
outcomeLists = outcomeLists or {}
79+
--luacheck: global
80+
outcomeLists = outcomeLists or {} --as:{total:number,outcomes:'{weight:number,command:__arg}[]'}[]
81+
--luacheck: global
8082
randomGen = randomGen or dfhack.random.new()
8183

8284
eventful.enableEvent(eventful.eventType.UNLOAD, 1)
8385
eventful.onUnload.randomTrigger = function()
8486
outcomeLists = {}
8587
end
8688

87-
validArgs = utils.invert({
89+
local validArgs = utils.invert({
8890
'help',
8991
'command',
9092
'outcomeListName',
@@ -132,11 +134,11 @@ end
132134
if args.weight and not tonumber(args.weight) then
133135
error ('Invalid weight: ' .. args.weight)
134136
end
135-
args.weight = (args.weight and tonumber(args.weight)) or 1
136-
if args.weight ~= math.floor(args.weight) then
137+
local weight = (args.weight and tonumber(args.weight)) or 1
138+
if weight ~= math.floor(weight) then
137139
error 'Noninteger weight.'
138140
end
139-
if args.weight < 0 then
141+
if weight < 0 then
140142
error 'invalid weight: must be non-negative'
141143
end
142144

@@ -148,11 +150,11 @@ args.outcomeListName = args.outcomeListName or ''
148150
args.outcomeListName = 'outcomeList ' .. args.outcomeListName
149151

150152
if args.withProbability then
151-
args.withProbability = tonumber(args.withProbability)
152-
if not args.withProbability or args.withProbability < 0 or args.withProbability > 1 then
153-
error('Invalid withProbability: ' .. (args.withProbability or 'nil'))
153+
local withProbability = tonumber(args.withProbability)
154+
if not withProbability or withProbability < 0 or withProbability > 1 then
155+
error('Invalid withProbability: ' .. (withProbability or 'nil'))
154156
end
155-
if randomGen:drandom() < args.withProbability then
157+
if randomGen:drandom() < withProbability then
156158
dfhack.run_command(table.unpack(args.command))
157159
end
158160
end
@@ -190,9 +192,9 @@ if not outcomeList then
190192
outcomeList = outcomeLists[args.outcomeListName]
191193
end
192194

193-
outcomeList.total = args.weight + (outcomeList.total or 0)
194-
local outcome = {}
195-
outcome.weight = args.weight
195+
outcomeList.total = weight + (outcomeList.total or 0)
196+
local outcome = {} --as:{weight:number,command:__arg}
197+
outcome.weight = weight
196198
outcome.command = args.command
197199
outcomeList.outcomes = outcomeList.outcomes or {}
198200
table.insert(outcomeList.outcomes, outcome)

modtools/raw-lint.lua

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ Checks for simple issues with raw files. Can be run automatically.
88
99
]====]
1010

11-
utils = require 'utils'
11+
local utils = require 'utils'
1212

13+
--luacheck: global
1314
enabled = enabled or false
1415

1516
if dfhack.filesystem == nil or dfhack.filesystem.listdir_recursive == nil then
1617
qerror('This script requires DFHack 0.40.24-r2 or newer')
1718
end
1819

19-
perr_prefix = ''
20+
local perr_prefix = ''
2021
function perr(msg, ...)
2122
dfhack.printerr((#perr_prefix > 0 and perr_prefix .. ': ' or '') .. tostring(msg):format(...))
2223
end
2324

24-
valid_objnames = utils.invert{
25+
local valid_objnames = utils.invert{
2526
'BODY_DETAIL_PLAN',
2627
'BODY',
2728
'BUILDING',
@@ -41,7 +42,7 @@ valid_objnames = utils.invert{
4142
'TISSUE_TEMPLATE',
4243
}
4344

44-
objname_overrides = {
45+
local objname_overrides = {
4546
b_detail_plan = 'BODY_DETAIL_PLAN',
4647
c_variation = 'CREATURE_VARIATION',
4748
}
@@ -120,7 +121,7 @@ dfhack.onStateChange.raw_lint = function(event)
120121
end
121122
end
122123

123-
args = {...}
124+
local args = {...}
124125
if dfhack_flags and dfhack_flags.enable then
125126
table.insert(args, dfhack_flags.enable_state and 'enable' or 'disable')
126127
end

modtools/reaction-product-trigger.lua

+4-12
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@ local eventful = require 'plugins.eventful'
2929
local utils = require 'utils'
3030

3131
--TODO: onUnload
32-
productHooks = productHooks or {}
33-
34-
reactionInputItems = reactionInputItems
35-
36-
function preserveReagents()
37-
reactionInputItems:resize(0)
38-
end
32+
--luacheck: global
33+
productHooks = productHooks or {} --as:{clear:__arg,reactionName:__arg,command:__arg}[][]
3934

4035
eventful.enableEvent(eventful.eventType.UNLOAD,1)
4136
eventful.onUnload.reactionProductTrigger = function()
@@ -88,12 +83,10 @@ local function afterProduce(reaction,reaction_product,unit,input_items,input_rea
8883
end
8984

9085
eventful.onReactionComplete.reactionProductTrigger = function(reaction,reaction_product,unit,input_items,input_reagents,output_items)
91-
reactionInputItems = input_items
9286
afterProduce(reaction,reaction_product,unit,input_items,input_reagents,output_items)
93-
reactionInputItems = nil
9487
end
9588

96-
validArgs = utils.invert({
89+
local validArgs = utils.invert({
9790
'help',
9891
'clear',
9992
'reactionName',
@@ -104,8 +97,7 @@ if moduleMode then
10497
return
10598
end
10699

107-
local args = {...} or {}
108-
args = utils.processArgs(args, validArgs)
100+
local args = utils.processArgs({...}, validArgs)
109101

110102
if args.help then
111103
print(usage)

0 commit comments

Comments
 (0)