Page 1 of 1
Accessing Wagon Cargo Quantity
Posted: Wed Jul 05, 2017 9:46 pm
by robertpaulson
Hi, I would like to make trains more realistic as from what I saw they do not take cargo into account regarding the train weight.
For that I would need to read the wagon contents and be able to count them (I will not go into extend of making different weights for each item, but I wanted to assign 1 item= 1 unit of weight). SO I just want to be able to count the items in each wagon and add that number to weight of the wagon and inheritable to the entire train. This will change the acceleration and braking mechanics.
I can handle the acceleration and braking, but I do not know how to count the wagon cargo. Anyone have done anything similar with wagons/ other containers?
Re: Accessing Wagon Cargo Quantity
Posted: Wed Jul 05, 2017 11:09 pm
by DaveMcW
Code: Select all
local inventory = wagon.get_inventory(defines.inventory.cargo_wagon)
local total = 0
for i = 1, #inventory do
if (inventory[i].valid_for_read) then
total = total + inventory[i].count
end
end
Re: Accessing Wagon Cargo Quantity
Posted: Thu Jul 06, 2017 2:07 am
by robertpaulson
DaveMcW wrote:Code: Select all
local inventory = wagon.get_inventory(defines.inventory.cargo_wagon)
local total = 0
for i = 1, #inventory do
if (inventory[i].valid_for_read) then
total = total + inventory[i].count
end
end
Wow, thank you very much for quick reply and the code!
Re: Accessing Wagon Cargo Quantity
Posted: Thu Jul 06, 2017 9:39 am
by mrvn
I would count stacks of items instead of items. That way a car weighs as much as 100 iron plates.
Re: Accessing Wagon Cargo Quantity
Posted: Sat Jul 22, 2017 9:45 pm
by robertpaulson
mrvn wrote:I would count stacks of items instead of items. That way a car weighs as much as 100 iron plates.
well my idea is to make the loco very heavy, but powerfull, than add cargo wagons weight- when empty makes train fast, but when full (cargo wagons>>>loco weight) makes train super slow...
Re: Accessing Wagon Cargo Quantity
Posted: Sun Jul 23, 2017 1:14 am
by d3x0r
Good luck with that

you don't know when a train is accelerating or decellerating
you can't update weight during run-time
you can adjust speed; but you'd have to implement acceleration yourself... but again you don't know when it is mean to accelerate or decelerate. (maybe you can build a full path system yourself and keep a map of where on the track the train is and override it all yourself)
Even just applying a flat modifier based on track type has proved to be extraordinarily difficult. If I don't pay attention to 'speed > prior speed' then approaching stops on penalized tracks makes the trains slow WAY down; the game computes what it thinks it wants for a deceleration curve over a track peice and if you slow it down below what it expects then it doesn't re-accelerate to get back to expected speed.
https://mods.factorio.com/mods/d3x0r/tr ... ed-limiter (don't recommend use at this time; but maybe a reference.)
Re: Accessing Wagon Cargo Quantity
Posted: Sun Jul 23, 2017 11:51 am
by Rseding91
You can get the count of items in a train much faster by just calling train.get_item_count() ->
http://lua-api.factorio.com/latest/LuaT ... item_count
Re: Accessing Wagon Cargo Quantity
Posted: Mon Jul 24, 2017 10:39 am
by mrvn
robertpaulson wrote:mrvn wrote:I would count stacks of items instead of items. That way a car weighs as much as 100 iron plates.
well my idea is to make the loco very heavy, but powerfull, than add cargo wagons weight- when empty makes train fast, but when full (cargo wagons>>>loco weight) makes train super slow...
That isn't what I mean.
Say you have a train that carries 4000 iron plates. So all 40 stacks in the wagon are full. Now that train should be really heavy. Another train carries 40 cars. Again that's all 40 stacks in the wagon full. But 40 is way less than 4000 so the train will weigh way less. I'm saying that is wrong. A full train should probably weigh the same no matter what good it carries. By counting stacks you should get a good metric for the weight as smaller (lighter) items have larger stack sizes.
I know it isn't perfect. 100 wood probably weigh less than 50 iron ore and more than a car. But should be good enough.
Re: Accessing Wagon Cargo Quantity
Posted: Thu Jul 27, 2017 2:19 am
by BrokenScience
All items have a max stack size found with
LuaItemPrototype.stack_size . Divide the number of items returned from train.get_item_count(item.name) by the stack size and you will have the number of stacks. (item is the LuaItemPrototype)
Code: Select all
local stacks = train.get_item_count(item.name)/item.stack_size
You will have to loop through all items in the game to calculate the number of stacks for the train in this way.
Re: Accessing Wagon Cargo Quantity
Posted: Fri Jul 28, 2017 12:05 am
by robertpaulson
d3x0r wrote:Good luck with that

you don't know when a train is accelerating or decellerating
you can't update weight during run-time
you can adjust speed; but you'd have to implement acceleration yourself... but again you don't know when it is mean to accelerate or decelerate. (maybe you can build a full path system yourself and keep a map of where on the track the train is and override it all yourself)
Even just applying a flat modifier based on track type has proved to be extraordinarily difficult. If I don't pay attention to 'speed > prior speed' then approaching stops on penalized tracks makes the trains slow WAY down; the game computes what it thinks it wants for a deceleration curve over a track peice and if you slow it down below what it expects then it doesn't re-accelerate to get back to expected speed.
https://mods.factorio.com/mods/d3x0r/tr ... ed-limiter (don't recommend use at this time; but maybe a reference.)
Yeah i figured this will be challenging since we have no access to de/acceleration at all... however the bubble burst when you said we can't change the train weight. So what are you saying is that after the train is placed, we can't add weight to it via any means? I was thinking adding dummy wagons to it to make it accelerate slower. I know this wont change breaking since train length doesn't affect the breaking and I would imagine we can't update breaking power on the go.... maybe its possible to change train power value via a function that updates after loading, but that would probably require to replace the loco entity each time we want to change this.. TBH I haven't started this part yet so I'm just throwing some random ideas now.
mrvn wrote:
Say you have a train that carries 4000 iron plates. So all 40 stacks in the wagon are full. Now that train should be really heavy. Another train carries 40 cars. Again that's all 40 stacks in the wagon full. But 40 is way less than 4000 so the train will weigh way less. I'm saying that is wrong. .
a car is less dense than ore, which has less density than smelted plates.... stack size, not perfectly, but accounts for density. Off-course if we go into extent of looking at circuit boards, flying robots, and locomotives then it all breaks down... but for my purpose I would only use it for ore/plates... so a stack of plates should weight more than stack of ore, maybe not double but more....
Re: Accessing Wagon Cargo Quantity
Posted: Fri Jul 28, 2017 1:25 am
by d3x0r
robertpaulson wrote:
I was thinking adding dummy wagons to it to make it accelerate slower. I know this wont change breaking since train length doesn't affect the breaking and I would imagine we can't update breaking power on the go.... maybe its possible to change train power value via a function that updates after loading, but that would probably require to replace the loco entity each time we want to change this.. TBH I haven't started this part yet so I'm just throwing some random ideas now.
maybe... I don't know if you can make invisible stocks

If you have a double ended train; you'd have to match the air_resistance of the last cargo wagon (engine)... for example, a train is much slower if you put cargo-loco, because the cargo wagon has more resistance... so if someone had LCCL and you added an invsible C at the end... it would have to be the same air_resistance as the loco... except if it's LCCC it wouldn't really be a problem to add a caboose at the end.
can track state change (from stopped to running and vice versa) so when it starts, add extra load at the appropriate ends...
Re: Accessing Wagon Cargo Quantity
Posted: Mon Jul 31, 2017 8:48 am
by mrvn
d3x0r wrote:robertpaulson wrote:
I was thinking adding dummy wagons to it to make it accelerate slower. I know this wont change breaking since train length doesn't affect the breaking and I would imagine we can't update breaking power on the go.... maybe its possible to change train power value via a function that updates after loading, but that would probably require to replace the loco entity each time we want to change this.. TBH I haven't started this part yet so I'm just throwing some random ideas now.
maybe... I don't know if you can make invisible stocks

If you have a double ended train; you'd have to match the air_resistance of the last cargo wagon (engine)... for example, a train is much slower if you put cargo-loco, because the cargo wagon has more resistance... so if someone had LCCL and you added an invsible C at the end... it would have to be the same air_resistance as the loco... except if it's LCCC it wouldn't really be a problem to add a caboose at the end.
can track state change (from stopped to running and vice versa) so when it starts, add extra load at the appropriate ends...
Except if you add it at the end whichever way it is going then ...
Re: Accessing Wagon Cargo Quantity
Posted: Mon Jul 31, 2017 1:59 pm
by d3x0r
mrvn wrote:d3x0r wrote:robertpaulson wrote:
I was thinking adding dummy wagons to it to make it accelerate slower. I know this wont change breaking since train length doesn't affect the breaking and I would imagine we can't update breaking power on the go.... maybe its possible to change train power value via a function that updates after loading, but that would probably require to replace the loco entity each time we want to change this.. TBH I haven't started this part yet so I'm just throwing some random ideas now.
maybe... I don't know if you can make invisible stocks

If you have a double ended train; you'd have to match the air_resistance of the last cargo wagon (engine)... for example, a train is much slower if you put cargo-loco, because the cargo wagon has more resistance... so if someone had LCCL and you added an invsible C at the end... it would have to be the same air_resistance as the loco... except if it's LCCC it wouldn't really be a problem to add a caboose at the end.
can track state change (from stopped to running and vice versa) so when it starts, add extra load at the appropriate ends...
Except if you add it at the end whichever way it is going then ...
my trains go both ways
