electric trains

Place to get help with not working mods / modding interface.
@xi@g@me
Inserter
Inserter
Posts: 30
Joined: Wed Jan 28, 2015 6:03 pm
Contact:

electric trains

Post by @xi@g@me »

Hi,
I am trying to create a mod so one can use electric locomotives instead of "diesel" ones.

As it is not easy to make electrifiable rails, I planned to do a locomotive which could "store" energy with batteries (same behavior than the roboport) when in stations, and then use it when running.
So I tried first to modify the base lua files, by changing the energy source from "burner" to "electric", and setting some parameters, as max input power and energy storage capacity. I copied the parameters from the roboport and adapted the values.

However, when I run the game, the locomotive is said to have no electricity plugged, even when I put a pole near it. If I try to open the locomotive (its inventory), the game crashes.

Did I messed up something somewhere ?

EDIT : corrected spelling error
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

the diesel trains have a power attribute, and you can tell it what the max and current are. you can also remove the inventory spots for fuel, which makes it seem electric.

from there, you need some way to add power to it, maybe have it charge off of local robo-ports type thing? or add a consumer under the train when it's parked and have that power up (like an accumulator) then when the train starts up again, pull the consumer and set the trains power to the consumers. I was also looking into this idea as well :P
Will code for Food. I also have 11+ mods!
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: electric trains

Post by Adil »

Electric locomotives are already implemented in 5dim-trains mod. Code-wise, they're not true electric devices, as dummy coal item is inserted into them every once in a while.

From my experience, entity prototype definitions are misleading at best. Even though they have field 'energy_source' and there's 'type' in that table, generally you can not define the former in any way, that is not present in vanilla for that type of entity.

As for crash, did your new energy_source had 'fuel_inventory_size' despite being electric? Pure guess, but usually syntactically correct definitions don't cause crashes even if not functioning correctly. There's also factorio_current.log in the root folder of factorio, sometimes it even manages to dump usable information about runtime errors. (Though its main goal in such case is to provide some crashlog understandable only by devs.)
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
@xi@g@me
Inserter
Inserter
Posts: 30
Joined: Wed Jan 28, 2015 6:03 pm
Contact:

Re: electric trains

Post by @xi@g@me »

Thanks for your answers !
Adil => I removed the whole "energy_source" tag before creating the electric one, so yes I guess that the fuel_inventory_size tag has been removed with. I'll have a look @ the log when coming back home.
Vzyvily => Parhaps I can make an item which makes the train charge when in range by creating some behavior lua code. I'll try this :)
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

To make trains with unlimited energy, this is the code that I just made:

data-updates:

Code: Select all

--data.raw["locomotive"]["diesel-locomotive"].max_power = "60000kW" --enable for super fast trains.
data.raw["locomotive"]["diesel-locomotive"].energy_source.fuel_inventory_size = 0
--raw[<TYPE>][<NAME>]
control:

Code: Select all

require "defines"
game.on_init(function() On_Load() end)
game.on_load(function() On_Load() end)
game.on_event(defines.events.on_tick, function() On_Tick() end)

game.on_event(defines.events.on_built_entity, function(event) On_Built(event) end)
game.on_event(defines.events.on_robot_built_entity, function(event) On_Built(event) end)
game.on_event(defines.events.on_preplayer_mined_item, function(event) On_Removed(event) end)
game.on_event(defines.events.on_robot_pre_mined, function(event) On_Removed(event) end)
game.on_event(defines.events.on_entity_died, function(event) On_Removed(event) end)

function On_Load()
	if not global.trainList then
		global.trainList = {}
	end
end
function On_Tick()
	for i=1,#global.trainList do
		global.trainList[i].energy = 500000000
	end
end
function On_Built(event)
	local entity = event.created_entity
	if entity.name == "diesel-locomotive" then
    	global.trainList[#global.trainList+1] = entity
    end
end
function On_Removed(event)
	local entity = event.entity
	if entity.name == "diesel-locomotive" then
		--this needs the index... to lazy and it's a copy from another mod that does this same thing...
		--that other mod had the issue and wasn't fixed...
		table.remove(global.trainList, entityIndex)
    end
end
I know there is a better way but I think some stuff from the wiki got removed from the last time I was looking up electric trains... I made all this in just afew minutes so there will be issues...
Will code for Food. I also have 11+ mods!
@xi@g@me
Inserter
Inserter
Posts: 30
Joined: Wed Jan 28, 2015 6:03 pm
Contact:

Re: electric trains

Post by @xi@g@me »

If I understand your code, it makes all trains in the game work with unlimited energy. Nice workaround, but I do not like the fact that it is free energy. You don't have to provide electri energy, charge traines, etc...

Did not have time to dig further for now, I'll tell you when I have something done.
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

it does, but I was thinking it could have a new train added, like a solar train and it would only affect those. the recipe could be a normal train with electric engines, and an amount of solar panels and accumulators in the golden ratio amount.
you could even have it be something stupid like 4 times the ratio for the unlimited energy (100 accumulators with 84 solars.)



or you could spend time to find out how to get them to charge off the network :P
Will code for Food. I also have 11+ mods!
@xi@g@me
Inserter
Inserter
Posts: 30
Joined: Wed Jan 28, 2015 6:03 pm
Contact:

Re: electric trains

Post by @xi@g@me »

Yes, indeed, solar trains may be an easier solution... This doesn't seem realistic in any way, due to the power required by an electric locomotive. But if I don't find a way, then I'll turn to this, definitely.
@xi@g@me
Inserter
Inserter
Posts: 30
Joined: Wed Jan 28, 2015 6:03 pm
Contact:

Re: electric trains

Post by @xi@g@me »

OK, so I had a look and confirm that the game crashes if the train does not have any fuel_inventory_size parameter set. (I changed energy_source to electric type)

Also, I guess that the train says it is not plugged to the electrical network because it is not placed on the map, but on a rail segment. I tried to remove the "not-on-map" flag, but no difference. I'll have to find a way to make it belive it is plugged. Perhaps with an onTick function

EDIT :
I looked a bit more in the wiki about modding, and found a lot of interesting info. I tried to display the train's energy and got something like 10666.66666667 (600Kw equivalent in J I suppose). I tried to modify the value using simple lua commands in-game, but whatever I do the energy is reset to the above value. I guess there is an internal behavior for entities like trains which sets the energy depending on available fuel. If this is the case, this makes electric trains creation purely impossible (with / without batteries). Perhaps this explains why the mod you shown me does the "coal trick". I hope that developpers will add a real electric train feature shortly
User avatar
Alekthefirst
Fast Inserter
Fast Inserter
Posts: 104
Joined: Sat Feb 07, 2015 7:39 pm
Contact:

Re: electric trains

Post by Alekthefirst »

@xi@g@me wrote:10666.66666667 (600Kw equivalent in J I suppose)
Watts are defined as Joule per second, 106666.6 doesnt have any link to 600kW unless you can prove that 10666.6 is indeed some energy amount, if so, that would imply that the number points to the total energy usage of some action being performed over a VERY short period of time
Factorio is a game about automating everything. One day, i hope i can automate shitty signatures just like this one.
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

@xi@g@me wrote:OK, so I had a look and confirm that the game crashes if the train does not have any fuel_inventory_size parameter set. (I changed energy_source to electric type)
Try zero.
@xi@g@me wrote:I tried to modify the value using simple lua commands in-game, but whatever I do the energy is reset to the above value. I guess there is an internal behavior for entities like trains which sets the energy depending on available fuel.
I don't think this would really be possible via in-game commands as you need a reference to the actual train engine itself.
@xi@g@me wrote:If this is the case, this makes electric trains creation purely impossible (with / without batteries). Perhaps this explains why the mod you shown me does the "coal trick". I hope that developers will add a real electric train feature shortly
the way I showed did not use the "coal trick" and was a viable way of making electric trains, all that's needed is to get a way to charge them... or like I said, slap a solar label on it and call it a day.

in fact, I'll see about working on my mods today and see if I can't get them out. I'll also try to build a charging set of trains as well and see how that goes. I'll also like the mods here so you can see how it works in practice.
Will code for Food. I also have 11+ mods!
JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: electric trains

Post by JamesOFarrell »

I hate to be a buzz kill but unless wube have done a pass in 0.12 on the train energy system you can't just set the energy. The coal trick was used in wagons (now 5dims train mod) because the train energy system is buggy/broken. You can't reliably read or write the energy from the train entity.
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

JamesOFarrell wrote:I hate to be a buzz kill but unless wube have done a pass in 0.12 on the train energy system you can't just set the energy. The coal trick was used in wagons (now 5dims train mod) because the train energy system is buggy/broken. You can't reliably read or write the energy from the train entity.
I had it working while I was testing on my original post and right now I am working on my mods to get them all updated to 0.12, after that we'll see.
Will code for Food. I also have 11+ mods!
JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: electric trains

Post by JamesOFarrell »

Wube must have fixed the issues with it then which is nice, It was a pain having to use coal. The other issue with the way wagons did things was having a bunch of moving entities under the train to draw power, I always wanted to go back and change it so the entities were under the track instead so they don't move. should fix the FPS issue it caused by having lots of trains
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

JamesOFarrell wrote:Wube must have fixed the issues with it then which is nice, It was a pain having to use coal. The other issue with the way wagons did things was having a bunch of moving entities under the train to draw power, I always wanted to go back and change it so the entities were under the track instead so they don't move. should fix the FPS issue it caused by having lots of trains
I actually just got my mods up and working on the 0.12 update, and it seems that the max is 10666.6666... (coasting/stopped/just after setting to an insane amount) and when moving it will be 666.6666... before the setting of the value... I'm guessing it has something to do with the state or if it can move next tick or something... it seems very odd though...
it switching to coasting
I will look to see if I can set it a better way but till then, I have a cheaty unlimited power train going in my mod tests...



the way I'm setting the power:

Code: Select all

			print("Pre  Train #",i," power: ",global.trainList[i].energy)
			global.trainList[i].energy = 500000000
			print("Post Train #",i," power: ",global.trainList[i].energy)
Will code for Food. I also have 11+ mods!
@xi@g@me
Inserter
Inserter
Posts: 30
Joined: Wed Jan 28, 2015 6:03 pm
Contact:

Re: electric trains

Post by @xi@g@me »

vzybilly : the number I show you is the one I got by typing a command to get the energy roperty of a train which was running. If the train is stopped (no coal inside), the value is 0.

It is possible to get/change the values in game, by using the selection. I get inside the train, run it, the zoom at maximum. Put the cursor on my train, so it is "selected".
Then I just have to use game.player.selection.train to get the train object. From there I tried to read the values and modified them, concluding that whatever value I put, it comes back immediately to 10666.6666666667... (or 0 if no coal).
If the train has no fuel and I set an energy value, the train starts and stops immediately. Also, I tried to use the energy set trick on a train set to use electric energy_source instead of burner (with an accumulator capacity), but the behavior is the same.

So, I can't charge the train when it is stopped on a station with a charging device.

And, indeed, I had a look at wikipedia, 1J is effectively 1W/s. So I don't know why 10666.6666666667..., because the max power for the train is set to 600kW in the base lua file.
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

@xi@g@me wrote:vzybilly : the number I show you is the one I got by typing a command to get the energy roperty of a train which was running. If the train is stopped (no coal inside), the value is 0.

It is possible to get/change the values in game, by using the selection. I get inside the train, run it, the zoom at maximum. Put the cursor on my train, so it is "selected".
Then I just have to use game.player.selection.train to get the train object. From there I tried to read the values and modified them, concluding that whatever value I put, it comes back immediately to 10666.6666666667... (or 0 if no coal).
If the train has no fuel and I set an energy value, the train starts and stops immediately. Also, I tried to use the energy set trick on a train set to use electric energy_source instead of burner (with an accumulator capacity), but the behavior is the same.

So, I can't charge the train when it is stopped on a station with a charging device.

And, indeed, I had a look at wikipedia, 1J is effectively 1W/s. So I don't know why 10666.6666666667..., because the max power for the train is set to 600kW in the base lua file.
yes, the whole thing is quite odd, I've been playing with it for a good bit today but I have found that it is very odd and stupid, I wish there was a way to see what things are in the train but I keep getting userdata types which are pretty much a no go for printing... so far, the best way that I've found for using a train with no coal is to set fuel_inventory_size = 0 then in control:

Code: Select all

function On_Tick()
		for i=1,#global.trainList do
			global.trainList[i].energy = 500000000
		end
end
trainList just being a table of train entities, the energy does get set back down to the 10666 but it will move that tick and on the next tick it will be 666... so it has to be on every tick to keep the train moving as to how or why, we would need to talk to one of the devs about it... because of the userdata issue when trying to view things, otherwise I'd have had it already... I have tried pretty much everything today and can't find the value... on another day I might try a brute force way of getting the data out of it... but that will be another day.
Will code for Food. I also have 11+ mods!
ratchetfreak
Filter Inserter
Filter Inserter
Posts: 952
Joined: Sat May 23, 2015 12:10 pm
Contact:

Re: electric trains

Post by ratchetfreak »

per tick value maybe?

multiply by 60 and you get 640k
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

ratchetfreak wrote:per tick value maybe?

multiply by 60 and you get 640k
Yes, just tested by adjusting max_power in the prototype and shifting it by 10s will shift the value by the same amount! I think this is the energy to be used this/next tick or something like that

Now if only we could find out where the energy buffer is...
Will code for Food. I also have 11+ mods!
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: electric trains

Post by vzybilly »

vzybilly wrote:
ratchetfreak wrote:per tick value maybe?

multiply by 60 and you get 640k
Yes, just tested by adjusting max_power in the prototype and shifting it by 10s will shift the value by the same amount! I think this is the energy to be used this/next tick or something like that

Now if only we could find out where the energy buffer is...
Ok, per watt, the max power per tick is 0.017777777777778 but the train's max power per watt per tick ends up being 0.016666666666667

I feel safer in saying that it's a rounding error or something causing it because at the per watt per tick level, those two numbers look to close while being too far...

mostly just putting up my findings so far but I think I can effectively use this info now... also, still no clue about a buffer solution so I'll work on this abit more and see if I can't get things to work out more...
Will code for Food. I also have 11+ mods!
Post Reply

Return to “Modding help”