PersonAgent - ellenvoegtli/simcity GitHub Wiki

Interaction Diagram

Design Document

Data:

enum PersonState {normal, working, inBuilding, waiting, boardingBus, inCar, walkingFromBus, walkingFromCar}
public enum PersonEvent {none, arrivedAtHome, arrivedAtWork, arrivedAtMarket, arrivedAtMarket2, arrivedAtRestaurant, arrivedAtBank, timeToWork, needMarket, needMarket2, gotHungry, gotFood, chooseRestaurant, decidedRestaurant, needToBank, maintainWork,goHome, arrivedRenterApartment}
enum CityLocation {home, restaurant[1], ... , restauraunt[n], bank, bank2, market, market2, renterHome}
PersonGui gui;
String name;
double cash;
double accountnumber;
boolean traveling;
boolean onBreak;
boolean hasCar; 
boolean alive;
BusAgent currentBus; 
Building homePlace;
int time;
int day;
Job job;
PersonState state;
PersonEvent event;
CityLocation destination;
Semaphore isMoving;
Map<ActionType, Role> roles;
PriorityQueue<Action> actions;
Action currentAction;
Queue<Actions> actions; //some sort of priority queue of actions
enum ActionState {new, inProgress, Done}
enum ActionType {work, fixing, maintenance, self_maintenance, hungry, homeAndEat, restaurant, market, market2, bankWithdraw, bankDeposit, bankLoan, bankRob, bankWithdraw2, bankDeposit2, bankLoan2, bankRob2, home}
    
class Action {
    ActionState state;
    ActionType type;
    int priority;
}
 
class Job {
    String occupation;
    int shiftBegin;
    int shiftEnd;
}
```

## Messages:
	//A message received from the GUI
	public void msgAtDestination() {
		//no longer traveling
		//release isMoving
	}

	//A message received from the transportation bus when arrived at destination
	public void msgArrivedAtDestination() {
		//no longer traveling, go inside building
	}

	//A message received from the car when arrived at destination
	public void msgArrivedAtDestinationInCar() { 
		//get out of car
	}
        //A message received to go fix something, similar to going to work
        public void msgNeedToFix() {
	        //add action to go fix something
	}

        //A message received from the bus to board it
	public void msgBusHasArrived() {
		//change state to boarding bus
	}

	//A message received from the system or GUI to tell person to go to work
	public void msgGoToWork() {
		//add new action to list of actions of type work
	}

	//A message received from the system or GUI to tell person to get hungry
	public void msgGotHungryForHome() {
		//add new action to list of actions of type restaurant or home
	}

        //A message received from the system or GUI to tell person to get hungry
	public void msgGoToRestaurant() {
		//add new action to list of actions of type restaurant
	}

	//A message received from the HomeAgent or GUI (possibly?) to go to the market
	public void msgGoToMarket() {
		//add new action to list of actions of type market
	}

	//A message received to tell the person to go to the bank with parameter of what they need to do
	public void msgGoToBank(String action) {
		//add new action to list of actions of type bank (with desired action)
	}

	//A message received to tell the person to go home
	public void msgGoHome() {
		//add new action to list of actions of type home
	}	

	//A message received from the vehicle object to remove the person
	public void msgHitByVehicle() {
		//no longer alive
	}

## Scheduler:

```
if(roles isn't empty) {
	for(Role r in roles and r is active) {
		return r.pickAndExecuteAnAction();
	}
}

if(alive) {
       //call function to respawn person
}

if(actions isn't empty && currentAction == null ) {
	//set current action to first action then handle action appropriately
}

if(currentAction != null && state == normal && !traveling) {
	if(event == arrivedAtHome) {
		if(!actions.isEmpty()) //If there's other stuff to do, don't go inside yet
			//set current action to done
								
		//find appropriate role, set active
		//enter building
	}
if(event == PersonEvent.arrivedRenterApartment) {
	//set appropriate landlord role active and enter building
}
if(event == arrivedAtWork) {
	//get appropriate role and set active
	//enter building
}

if(event == arrivedAtMarket) {
	//find and set appropriate role active
	//check occupant role to see food they need to order
}
			
if(event == arrivedAtRestaurant) {
	//find and set appropriate role active based on restaurant
	//enterBuilding
}
		
if(event == arrivedAtBank) {
	//find and set appropriate bank role active
	//enter building
}

if(event == timeToWork) {
	goToWork();
}
if(event == needMarket) {
	goToMarket();
}
if(event == gotFood || event == goHome) {
	goHome();
}
if(event == gotHungry) {
	decideWhereToEat();
}
if(event == maintenance) {
	goToRenters();
}
if(event == chooseRestaurant) {
	chooseRestaurant();
}
if(event == decidedRestaurant) {
	goToRestaurant();
}
if(event == needToBank) {
	goToBank();
}	
if(state == boardingBus) {
	boardBus();
}
if(state == walkingFromBus) {
	//get off bus
	travelToLocation(destination);
}
if(state == walkingFromCar) {
	//walk inside
}
if(actions is empty && state == normal && !traveling) {
	actions.add(new home action);
}
```

## Actions:
	private void enterBuilding() {
		//set currentAction.state to done
		//tell gui to go inside and set appropriate state of personagent
	}

	private void checkSelf() {
		//called every clock cycle. ~15 seconds
		//calls and sets appropriate actions based on state of person (ie cash amount, etc.
	}

	private void handleRole(ActionType action) {
		//get appropriate role and add it to the list of roles based on the action
	}
	
	private void handleAction(ActionType action) {
		//set appropriate state based on action
	}

	private void travelToLocation(CityLocation d) {
		//choose to either walk or take bus to destination
	}

	private void chooseRestaurant() {
		//randomly choose which restaurant
	}
	
	private void decideWhereToEat() {
		//randomly choose where to eat: home or at restaurant
	}
	
	private void goToWork() {
		//set appropriate destination based on the person's occupation
	}

	private void goToRenters() {
		//go into the home to fix
	}
	
	private void goToMarket() {
		//choose market and then travelToLocation
		event = arrivedAtMarket;
	}

	private void goHome()  {
		travelToLocation(home);
		event = arrivedAtHome;
	}

	private void goToRestaurant() {
		//destination set earlier when choosing restaurant
		travelToLocation(destination);
		event = arrivedAtRestaurant;
	}

	private void goToBank() {
		//choose bank then travelToLocation()
		event = arrivedAtBank;
	}
	
	private void goToBank() {
		//set back to life, remove from gui
		//set person to inside their home
	}
	
	private void boardBus() {
		//go to the bus stop and then wait for it to pass
	}

	//---Other Actions functions---//
	public void addRole(ActionType type, Role role) {
		roles.put(type, role);
	}
	
	public void roleInactive() { //an action that lets the person continue on after going in a building
		state = normal;
		gui.DoGoOutside();
	}
	
	public void stateChanged() { //calls this agent's statechanged
		super.stateChanged();
	}

	public void updateClock(int newTime) {
		//update time then check self
		checkSelf();
	}
	public void setHomePlace(boolean renter) {
		//assign a person a home or apartment based on the input
	}
⚠️ **GitHub.com Fallback** ⚠️