6. Multi-level models
This step lets building agents manage people who are inside them. A building captures stationary people as members of a child species and releases them according to their time in the building. The building also tracks infection counts for its occupants.

Define the child species and building state​
people_in_building inherits the attributes and behaviors of people. The empty schedules facet excludes the child species from the scheduler while its parent building manages it. The building's dynamic attributes count its infected and total occupants.
species building {
int nb_infected <- 0 update: self.people_in_building count each.is_infected;
int nb_total <- 0 update: length(self.people_in_building);
float height <- rnd(10 #m, 20 #m);
species people_in_building parent: people schedules: [] {
}
reflex let_people_enter {
capture (people inside self where (each.target = nil)) as: people_in_building;
}
reflex let_people_leave {
ask people_in_building {
staying_counter <- staying_counter + 1;
}
release people_in_building where (flip(each.staying_counter / staying_coeff)) as: people in: world {
target <- any_location_in(one_of(building));
}
}
aspect default {
draw shape color: nb_total = 0 ? #gray : (float(nb_infected) / nb_total > 0.5 ? #red : #green) border: #black depth: height;
}
}
Count agents inside buildings​
The global population count combines free people with people held in each building's people_in_building population. The infection count then includes both groups.
global {
list<people_in_building> list_people_in_buildings update: (building accumulate each.people_in_building);
int nb_people_infected <- nb_infected_init update: (people + list_people_in_buildings) count (each.is_infected);
}
See the complete Incremental Model 6 source for the full experiment, movement and display.