Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: add remote interface to add a new locomotive type
  • Loading branch information
KoharaKazuya committed Dec 17, 2020
1 parent a67c537 commit 3a0f889
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelog.txt
Expand Up @@ -3,6 +3,8 @@ Version: 1.2.0
Date: 2020.12.12
Features:
- More mods compatibility. Battery Locomotive has come to handle `script_raised_built` and `script_raised_destroy` events.
Modding:
- Added remote interface to add a new battery locomotive type.
---------------------------------------------------------------------------------------------------
Version: 1.1.0
Date: 2020.12.05
Expand Down
17 changes: 17 additions & 0 deletions control.lua
@@ -1,10 +1,27 @@
require "remote-interface"

local key = require "utils.key"
local train = require "utils.train"
local receiver = require "utils.receiver"

script.on_init(function()
global = {}
global.entities = {}
global.locomotive_type_names = {
key.locomotive, key.locomotive_mk2, key.locomotive_mk3
}
global.schema_version = 2
end)

script.on_configuration_changed(function(data)
-- migrate global table
local version = global.schema_version or 1
if version < 2 then
global.locomotive_type_names = {
key.locomotive, key.locomotive_mk2, key.locomotive_mk3
}
end
global.schema_version = 2
end)

local function create_receiver(locomotive)
Expand Down
2 changes: 1 addition & 1 deletion info.json
@@ -1,6 +1,6 @@
{
"name": "BatteryLocomotive",
"version": "1.1.0",
"version": "1.2.0",
"title": "Battery Locomotive",
"author": "KoharaKazuya",
"factorio_version": "1.1",
Expand Down
12 changes: 12 additions & 0 deletions remote-interface.lua
@@ -0,0 +1,12 @@
--- register a new locomotive type
-- @param name string of lomotive type name
local function register_locomotive_type(name)
local contains = false
for _, v in pairs(global.locomotive_type_names) do
if v == name then contains = true end
end
if not (contains) then table.insert(global.locomotive_type_names, name) end
end

remote.add_interface("BatteryLocomotive",
{["register_locomotive_type"] = register_locomotive_type})
6 changes: 4 additions & 2 deletions utils/train.lua
Expand Up @@ -45,8 +45,10 @@ end
local function is_battery_locomotive(entity)
if not entity then return false end
local name = entity.name
return (name == key.locomotive) or (name == key.locomotive_mk2) or
(name == key.locomotive_mk3)
for _, v in pairs(global.locomotive_type_names) do
if v == name then return true end
end
return false
end

return {
Expand Down

0 comments on commit 3a0f889

Please sign in to comment.