Accessing Wagon Cargo Quantity
-
- Long Handed Inserter
- Posts: 92
- Joined: Sun Jun 18, 2017 2:21 pm
- Contact:
Accessing Wagon Cargo Quantity
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?
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
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
-
- Long Handed Inserter
- Posts: 92
- Joined: Sun Jun 18, 2017 2:21 pm
- Contact:
Re: Accessing Wagon Cargo Quantity
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
I would count stacks of items instead of items. That way a car weighs as much as 100 iron plates.
-
- Long Handed Inserter
- Posts: 92
- Joined: Sun Jun 18, 2017 2:21 pm
- Contact:
Re: Accessing Wagon Cargo Quantity
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...mrvn wrote: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
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.)

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
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
If you want to get ahold of me I'm almost always on Discord.
Re: Accessing Wagon Cargo Quantity
That isn't what I mean.robertpaulson wrote: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...mrvn wrote:I would count stacks of items instead of items. That way a car weighs as much as 100 iron plates.
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.
-
- Inserter
- Posts: 26
- Joined: Thu Jul 21, 2016 5:31 pm
- Contact:
Re: Accessing Wagon Cargo Quantity
Rseding91 wrote: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
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
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.
-
- Long Handed Inserter
- Posts: 92
- Joined: Sun Jun 18, 2017 2:21 pm
- Contact:
Re: Accessing Wagon Cargo Quantity
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.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.)
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....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. .
Re: Accessing Wagon Cargo Quantity
maybe... I don't know if you can make invisible stocksrobertpaulson 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.

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
Except if you add it at the end whichever way it is going then ...d3x0r wrote:maybe... I don't know if you can make invisible stocksrobertpaulson 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.
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
my trains go both waysmrvn wrote:Except if you add it at the end whichever way it is going then ...d3x0r wrote:maybe... I don't know if you can make invisible stocksrobertpaulson 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.
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...
