TFWR. Drone coordination.
Last time we discussed how to create drones. Today let's discuss how to coordinate their activity.
In the game there is quite a limited number of tricks that allow you to coordinate drones. Let's use the approach with "the central coordinator drone" and use the number of drones as a synchronization tool.
I think one of the simplest tasks is to plant sunflowers, so let's do it. The pool of tasks is the pool of columns. I want to keep alive the main drone we have on screen and ask it to spread tasks over the other drones. Let's discuss the nuts and bolts of this approach.
Let's check the spawn_drone function. It takes as a parameter a function which operates a new drone. I have 32 drones and 32 columns. So it's necessary to create a tool for the creation of these parameterized drone functions. Let's do it with the "factory function" approach. It takes the number of a column as a parameter and returns a drone function with proper parameterization.
def f(column):
def g():
one_drone_plant_job(column)
return g
A simple function to plant all sunflowers in a given column:
def one_drone_plant_job(column):
n = get_world_size()
for y in range(n):
nav(column, y)
if can_harvest():
harvest()
if get_ground_type() == Grounds.GrassLand:
till()
plant(Entities.Sunflower)
And, last but not least, the code for the manager
Let's create a list of tasks
tasks = []
for t in range(get_world_size()):
tasks.append(t)
Dispatch them between drones
while True:
tasks = []
for t in range(get_world_size()):
tasks.append(t)
while tasks:
t = tasks.pop()
while num_drones() == 16: # max_drones():
pass
spawn_drone(f(t))
This "pass" loop is an "interprocess synchronization" tool that uses the number of drones as a synchronization object. When the "while tasks" loop finishes, the job is done - the field is full of sunflowers.
Code on github