I am new to Rust and new to coding with an ECS.
I am building a simulated economy, and using it as an opportunity to learn Rust and ECS.
The core idea is that you have all these economic agents (people, food stores, farms, etc) interacting by engaging in transactions (trading food, trading labor etc).
The cycle I want to use is that at each iteration of the main loop
- Some entities advertise a passive offer. For example, each food store will advertise the prices and quantities it is willing to trade food at.
- Some entities will browse available offers and select an optimal offer to to try and trade with. Optimality can be based on any arbitrary function of the offer such as proximity and price.
- Trades are matched with offers in random order for each offerer and are accepted for as long as the offerer has stock to satisfy the trade.
Then Offerers update their offers based on new stock levels or some other information and we loop back.
So what is an effective way to model this? My thoughts so far, focusing on the case of a
Food from a
It seems clear the following components are required
#[derive(Component)] struct Money(f64); #[derive(Component)] struct Food(f64);
I probably need a marker component to identify a
Person or a
#[derive(Default)] #[derive(Component, Debug)] #[storage(NullStorage)] struct Person; #[derive(Default)] #[derive(Component, Debug)] #[storage(NullStorage)] struct FoodStore;
It bothers me that these 2 aren’t mutually exclusive so that already suggests a design failure.
And I expect I need a system for each of the elements of the above mentioned loop.
Thinking about the first system: setting offers. An offer could, in principle, be a component of a food store.
So the first system will focus on those entities that have
FoodStore markers and (maybe) food offers. It then adds a new offer component, or mutates an existing one, or deletes an existing one as deemed necessary.
Moving on to the second system. This will focus on those entities that have
Person markers and for each person, it needs to get all available offers and choose the best one. But how does each
Person get a list of available food offers?
FoodOffer is a component attached to a
FoodStore then I can’t think of how this can be easily accomplished. Since the
run function in the
System trait filters entities based on components specified in
Person) and then runs a loop, I can’t immediately see how to then specify an independent filter for
FoodStore so that each
Person can iterate over the resulting list.
FoodOffer be it’s own entity? I don’t immediately see how that would help.
The only solution I can think of is to have a
Billboard resource that
FoodStores advertise to and
Person s browse. Not sure if this is considered best practice here. I would expect there to be an idiomatic ECS way to model this kind of interaction where one entity choose some other entity from a list of candidate entities.
It occurs to me that a similar problem would exists in a RTS game, whereby each unit that can attack needs to select an enemy unit to attack based on some optimality condition (usually proximity and how many other units are attacking the same enemy unit). So I am guessing this is a common enough requirement for there to be a well developed pattern.
Any advice would be greatly appreciated.