-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcheckModuleMatching.lua
More file actions
67 lines (56 loc) · 1.73 KB
/
checkModuleMatching.lua
File metadata and controls
67 lines (56 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--[[ Copyright (c) 2019 robot256 (MIT License)
* Project: Multiple Unit Train Control
* File: checkModuleMatching.lua
* Description: Checks if a pair of locos are RET modular type, and if the module configurations match.
* Returns true if they are not modular locos.
--]]
local moduleTypes = nil
local function isRelevantModule(name)
if moduleTypes == nil then
moduleTypes = {}
game.print("MUTC is Caching RET module types")
-- Cache table of allowable modules
if prototypes.technology["ret-modular-locomotives"] then
for _,effect in pairs(prototypes.technology["ret-modular-locomotives"].effects) do
if effect.type == "unlock-recipe" and prototypes.equipment[effect.recipe] then
moduleTypes[effect.recipe] = true
--game.print("Cached module type " .. effect.recipe)
end
end
end
end
if moduleTypes[name] then
return true
else
return false
end
end
function checkModuleMatching(loco1, loco2)
-- Given a pair of locos that have same type and are in the right spot in the train to be a pair
-- Check if they have modular locomotive grids
if loco1.grid and loco1.grid.prototype.name=="modular-locomotive-grid" then
-- Read grid contents of both locos
local gridOne = loco1.grid.get_contents()
local gridTwo = loco2.grid.get_contents()
local matched = true
for k,v in pairs(gridOne) do
if isRelevantModule(k) and not(gridTwo[k] and gridTwo[k]==v) then
matched = false
break
end
end
if matched then
for k,v in pairs(gridTwo) do
if isRelevantModule(k) and not(gridOne[k] and gridOne[k]==v) then
matched = false
break
end
end
end
return matched
else
-- Not a modular locomotive, always able to replace
return true
end
end
return checkModuleMatching