Skip to content

Commit

Permalink
* Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
avosirenfal committed Jan 9, 2017
0 parents commit b9540bc
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 avosirenfal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# What does this mod do?

This mod makes automated trains (NOT manual trains) automatically color themselves whenever they start moving on a path with an appropriate color based on the resource they're carrying. If a train is carrying more than one type of resource the color will blend appropriately (e.g. iron is blue, copper is red- carrying 50% iron and 50% copper will make the train purple)

- Colors
- Coal: Black
- Steel Plate: White
- Iron Ore/Iron Plate: Blue-ish
- Copper Ore/Copper Plate: Red-ish
- Raw wood/wood: Brown

# (For developers) Compatibility

I have not added an API or support for any other mods, but if you would me to add an API let me know. Alternatively, you may contact me and let me know appropriate RGB colors (same as in game) for each of your resource types, as well as their internal name and I'll add them to the base mod.
78 changes: 78 additions & 0 deletions Train_Ore_Color_1.0.0/colorblend.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local rgb_scale = 255.0
local cmyk_scale = 100.0

-- color mixing
-- http://stackoverflow.com/a/30079700/1316748

local function rgb_to_cmyk(r,g,b)
if r == 0 and g == 0 and b == 0 then
return {
['c'] = 0,
['m'] = 0,
['y'] = 0,
['k'] = cmyk_scale,
}
end

-- rgb [0,255] -> cmy [0,1]
c = 1 - r / rgb_scale
m = 1 - g / rgb_scale
y = 1 - b / rgb_scale

-- extract out k [0,1]
min_cmy = math.min(c, m, y)
c = (c - min_cmy)
m = (m - min_cmy)
y = (y - min_cmy)
k = min_cmy

-- rescale to the range [0,cmyk_scale]
return {
['c'] = c*cmyk_scale,
['m'] = m*cmyk_scale,
['y'] = y*cmyk_scale,
['k'] = k*cmyk_scale,
}
end

local function _clamp(v, min, max)
if(v < min) then
return min
elseif(v > max) then
return max
else
return v
end
end

local function cmyk_to_rgb(c,m,y,k)
r = rgb_scale*(1.0-(c+k)/cmyk_scale)
g = rgb_scale*(1.0-(m+k)/cmyk_scale)
b = rgb_scale*(1.0-(y+k)/cmyk_scale)

return {
['r'] = _clamp(math.floor(r), 0, 255),
['g'] = _clamp(math.floor(g), 0, 255),
['b'] = _clamp(math.floor(b), 0, 255),
}
end

function ink_add_for_rgb(colors)
-- input: list of rgb, opacity (r,g,b,o) colours to be added, o acts as weights (may be nil)
local C = 0
local M = 0
local Y = 0
local K = 0

for _, color in pairs(colors) do
local cmyk = rgb_to_cmyk(color.r, color.g, color.b)
local o = color.o or 0.5

C = C + (o*cmyk.c)
M = M + (o*cmyk.m)
Y = Y + (o*cmyk.y)
K = K + (o*cmyk.k)
end

return cmyk_to_rgb(C, M, Y, K)
end
107 changes: 107 additions & 0 deletions Train_Ore_Color_1.0.0/control.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require('util')
require('colorblend')
-- for k,v in pairs(ink_add_for_rgb({
-- {['r'] = 255, ['g'] = 100, ['b'] = 0},
-- {['r'] = 0, ['g'] = 0, ['b'] = 255},
-- })) do
-- print(tostring(k) .. ': ' .. tostring(v))
-- end

colors = {
[{['coal']=true}] = {
['r'] = 0,
['g'] = 0,
['b'] = 0,
},
[{['steel-plate']=true}] = {
['r'] = 255,
['g'] = 255,
['b'] = 255,
},
[{['iron-ore']=true, ['iron-plate']=true}] = {
['r'] = 0,
['g'] = 50,
['b'] = 200,
},
[{['copper-ore']=true, ['copper-plate']=true}] = {
['r'] = 255,
['g'] = 50,
['b'] = 0,
},
[{['raw-wood']=true, ['wood']=true}] = {
['r'] = 116,
['g'] = 37,
['b'] = 0,
},
}

script.on_event(defines.events.on_train_changed_state, function(event)
local train = event.train
if(train.state == defines.train_state.on_the_path and not train.manual_mode and #train.cargo_wagons > 0) then
local total = 0
local hit = 0
calc = {}

for name, count in pairs(train.get_contents()) do
for key, color in pairs(colors) do
if(key[name] ~= nil) then

if(calc[key] ~= nil) then
calc[key] = calc[key] + count
else
hit = hit + 1
calc[key] = count
end

total = total + count
end
end
end
-- for _, cargo in pairs(train.cargo_wagons) do
-- game.print(#(cargo.get_inventory(defines.inventory.chest)))
-- end

if(hit < 1) then
return
elseif(hit == 1) then
-- lua is stupid...
local asdf = nil

for k,v in pairs(calc) do
asdf = k
break
end

if(asdf == nil) then
error('Train_Ore_Color: nil color selection')
end

color = colors[asdf]
else
color_mix = {}

for key, count in pairs(calc) do
r = table.deepcopy(colors[key])
r['o'] = count / total
table.insert(color_mix, r)
end

color = ink_add_for_rgb(color_mix)
end

final_color = {
['r'] = color.r / 255,
['g'] = color.g / 255,
['b'] = color.b / 255,
['a'] = 0.498, -- vanilla UI uses this alpha
}

for _, locomotive in pairs(train.locomotives['front_movers']) do
locomotive.color = final_color
end

for _, locomotive in pairs(train.locomotives['back_movers']) do
locomotive.color = final_color
end
end
end)
9 changes: 9 additions & 0 deletions Train_Ore_Color_1.0.0/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Train_Ore_Color",
"version": "1.0.0",
"title": "Train Ore Color",
"author": "Sirenfal",
"description": "Sets train colors based on their contents",
"dependencies": ["base >= 0.14.0"],
"factorio_version": "0.14.0"
}

0 comments on commit b9540bc

Please sign in to comment.