In the last lecture, we came up with a plan for making bread and also picking up the flour to make that
bread.
Now we want to add in another plan where we deliver the bread. Delivery of the bread is different
to the other
chain. It doesn't really fit in because we want to deliver bread but only when we've got a certain quantity
of bread.
BakeBread produces just 1 loaf but we want to accumulate them into a delivery.
We're going to begin with the previous project where the Baker was baking bread and picking up flour
as needed and to begin with, we're going to insert a new action for delivering the bread so that we will
get a chain that looks like this...that we have Pickup Flour...Bake Bread and then Deliver Bread.
Notice that the 'doJob' effect is moved to the end of deliver bread and a
'hasBread' True is used to link the baking and the delivery.
So to achieve this you need a new action script, so create
another C# script and call it DeliverBread and it's going to look like this.
So, the DeliverBread is a DeliverBread action.
Remember you can copy this from your other actions to save you typing it all out again and just modify
DeliverBread there and then also in the constructor.
Now the Precondition for delivering bread is that you have bread and the Effect is for doing the job.
So for the Baker to be doing his job completely,
he has to deliver the bread, not only make it.
Now when we deliver bread (if we go back down into our performance) that will mean that the bread level
in the inventory is going to go down by 1.
So make sure that you add that physical environment Effect into your script here.
Save that, then
back in your BakeBread action, you're going to have to change your final Effect here to...
instead of doing the job ('doJob'), it's going to be 'hasBread' is True and then that will link that action with
the DeliverBread action.
So make sure you save that as well.
Now go back into Unity, select the Baker and you want to drag-and-drop the DeliverBread script onto
the Baker and then set the delivery location, which will be the Market Door.
So lets drag-and-drop that there.
Now when we press Play,
we won't initially have any bread in our inventory, so we can't deliver anything
but he will make bread (and you've got bread is 1)
and then go and deliver it and drop it off and then he will come back and he's already got enough flour
so he can still make another loaf of bread and then go and drop that off
and now he doesn't have enough flour so he'll have to go to the mill to grab some flour
and then he will come back to the bakery and bake some more bread...but only bake 1 loaf of bread because
that sets the 'hasBread' to True, which is going to cause that link.
Now the reason that we can't test for how many pieces of bread that we've got in order to do
this is because each action has to have the Preconditions and the Effects that link together, so that
when the A* algorithm is running through the plan and trying to find all of the actions that will
link together,
if it doesn't have a match, then it's not going to work.
So you can't have the bakery say 'hasBread' is False and then have the market say 'hasBread' is True and
expect that chain to be formed in the planning stage, which means that it's never going to have a plan
that just starts with baking bread and then it's going to just break and fall over
and you can try that if you like, it just will not work.
So instead of doing that, you want to add a separate plan. So we'll go back in to our DeliverBread and
at the top, we're going to set this back to 'hasDelivery'. So a delivery will be a bigger thing and it's
going to satisfy the 'doJob'.
Now when a delivery has occurred, let's say that there's 5 loaves of bread in a delivery, so at the
bottom here that will make our inventory level go down by 5.
So that's all well and good.
So save that, let's go back into BakeBread. Now BakeBread is going to go back to what it was before,
so we're going to 'doJob' and continue to do the job. Save that
and then you want to go over to Worker
and we need to inject the 5 loaves of bread as a World State, which will then trigger off the delivery.
So we're going to put it up here in the GetWorldState with the flour,
so up here we'll just add...
worldData.Add
'hasDelivery' and it will be equal to the inventory's breadLevel
greater than 4
which means we have 5 (or more) loaves of bread that we can deliver
and then when that does reach that and 'hasDelivery' is True, then a delivery can occur.
So we'll save that and switch back into Unity and press Play. So, initially we've got no bread so we're
going to start making bread...got 2 loaves...go and get some more flour and get enough flour that we should be
able to make 3 more loaves...those 3 loaves will be made at which point (we hope!) we should be able to
deliver because we'll have 5 lots of bread and there he goes
and then when he gets there and that action completes, it will reduce the bread in the inventory down
to 0.
There's no flour either
and we have to go back to the mill to pick that up.
So now we have a complex series of events with just some really simple chains of actions in
them and you can see from this how easy it is to just keep adding actions to do all sorts of different
tasks. In the next lecture, we'll look at adding a global inventory and another agent.