Accessing Wagon Cargo Quantity

Place to get help with not working mods / modding interface.
Post Reply
robertpaulson
Long Handed Inserter
Long Handed Inserter
Posts: 92
Joined: Sun Jun 18, 2017 2:21 pm
Contact:

Accessing Wagon Cargo Quantity

Post 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?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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

robertpaulson
Long Handed Inserter
Long Handed Inserter
Posts: 92
Joined: Sun Jun 18, 2017 2:21 pm
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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!

mrvn
Smart Inserter
Smart Inserter
Posts: 5683
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Accessing Wagon Cargo Quantity

Post by mrvn »

I would count stacks of items instead of items. That way a car weighs as much as 100 iron plates.

robertpaulson
Long Handed Inserter
Long Handed Inserter
Posts: 92
Joined: Sun Jun 18, 2017 2:21 pm
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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...

d3x0r
Filter Inserter
Filter Inserter
Posts: 316
Joined: Sun Jun 04, 2017 8:56 am
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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.)

Rseding91
Factorio Staff
Factorio Staff
Posts: 13176
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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
If you want to get ahold of me I'm almost always on Discord.

mrvn
Smart Inserter
Smart Inserter
Posts: 5683
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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.

BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

Re: Accessing Wagon Cargo Quantity

Post by BrokenScience »

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
You will have to loop through all items in the game to calculate the number of stacks for the train in this way.
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

robertpaulson
Long Handed Inserter
Long Handed Inserter
Posts: 92
Joined: Sun Jun 18, 2017 2:21 pm
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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....

d3x0r
Filter Inserter
Filter Inserter
Posts: 316
Joined: Sun Jun 04, 2017 8:56 am
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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...

mrvn
Smart Inserter
Smart Inserter
Posts: 5683
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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 ...

d3x0r
Filter Inserter
Filter Inserter
Posts: 316
Joined: Sun Jun 04, 2017 8:56 am
Contact:

Re: Accessing Wagon Cargo Quantity

Post 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 :)

Post Reply

Return to “Modding help”