Power as int, not as text

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2904
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Power as int, not as text

Post by darkfrei »

Hi!

We have now
power ="60kW"
Is it possible to make same power as
power = 60000?

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Power as int, not as text

Post by bobingabout »

It would be so much easier if you could. I have some prototypes that are built from formulae, the fact that power needs to be written as text like that makes it difficult (but not impossible) to do in such a way.

Can probably do it with this:

Code: Select all

power = string.format("%dW", <Formula>)
There might be other methods, and you can use %f if you want a floating point number, which would be more useful if you were specifying kW or MW.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2919
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Power as int, not as text

Post by Optera »

It would make sense to have power as int. Although it's only a minor inconvenience to write

Code: Select all

power = value.."W"
or if you want to make extra sure

Code: Select all

power = tostring(value).."W"
Reading is a different beast.
From a test mod rebalancing all equipment power stats. Turned out it broke the bots so they would never complete charging so it went into the trash bin. Hopefully you can use it for something.

Code: Select all

local factor = 0.01

local EquipmentTypes = 
{
  ["night-vision-equipment"] = true,
  ["energy-shield-equipment"] = true,
  ["battery-equipment"] = true,
  ["solar-panel-equipment"] = true,
  ["generator-equipment"] = true,
  ["active-defense-equipment"] = true,
  ["movement-bonus-equipment"] = true,
  ["roboport-equipment"] = true,
  ["belt-immunity-equipment"] = true,
}

local function rebalance(path, parameter, oldValue)
  if type(oldValue) == "string" then
    local num, str, exp, energy = string.match(oldValue, "([%-+]?[0-9]*%.?[0-9]+)(([kKMGTPH]?)([WJ]))")
    --log(tostring(parameter)..": "..tostring(oldValue).." split: "..tostring(num)..", "..tostring(str)..", "..tostring(exp)..", "..tostring(energy))
    if num and str and energy then
      local correctedNum = tonumber(num) * factor
      path[parameter] = correctedNum..str
    end
  end
end

local function parse_table(t)
  for k, v in pairs(t) do
    if type(v) == "table" then
      parse_table(t[k])
    else
      rebalance(t, k, v)
    end      
  end
  return ct
end


for equipment, flag in pairs(EquipmentTypes) do
  for name, item in pairs(data.raw[equipment]) do
    --log("found "..tostring(name))    
    local path = data.raw[equipment][name]
    
    parse_table(path) 
  end
end 

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2904
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Power as int, not as text

Post by darkfrei »

It's not difficult, but why are we need it? Why positions are without meters, speed is without "km/h"?

Here I've used this function, but I can't understend why not just int in Watts?

Code: Select all

function text_to_int (text)
	if not text then return end
	local tabl = {["kW"] = 1000, ["MW"] = 1000000, ["GW"] = 1000000000} 
	for i, v in pairs (tabl) do
		if string.find(text, i) then
			local a = string.gsub(text, i, "") -- example: "kW" is replaced by ""
			local num = tonumber(a) * v
			return num
		end
	end
	return false
end
UPD:
Function for multiplicate power [kW] or energy [kJ], for str = "60 kW" and factor = 10 you get new_str = "600 kW"

Code: Select all

function multiplicate_string (str, factor)
  local value = tonumber(string.match(str, "%d+")) * factor
  local unit = string.match(str, "%a+")
  local new_str = value..unit -- for example 600 .. "kW" --> "600kW"
  return new_str
end

Post Reply

Return to “Modding interface requests”