Skip to content
Snippets Groups Projects
Asshole.java 3.7 KiB
Newer Older
package loadPoles;

import java.util.List;

import repast.simphony.context.Context;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.query.space.grid.GridCell;
import repast.simphony.query.space.grid.GridCellNgh;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.graph.Network;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.SimUtilities;

public class Asshole extends Human {
	
	boolean needsCharging;

	public Asshole(Context<Object> context) {
		super(context);
		this.carType = "a";
		// TODO Auto-generated constructor stub
		
	}
	
	@ScheduledMethod(start = 1, interval = 1, priority = 2, shuffle = true) 
	public void parkCar() {
		//do we even use a car?
		if (!carUser) 
			return;
		
		System.out.println("Parkingname: " + this.name);
		
		//Find best empty spot
		//Get location of home
		Grid<Object> grid = (Grid<Object>) context.getProjection("dwellingsGrid");
		Network<Object> livingin = (Network<Object>) context.getProjection("livingin");
		
		Iterable<Object> home = livingin.getAdjacent(this);
		GridPoint homeLocation = grid.getLocation(home.iterator().next());
		
		//Now look for parking spots
		Grid<Object> parkingSpots = (Grid<Object>) context.getProjection("parkingspacesGrid");
		
		//get all cells around the home location
		GridCellNgh<ParkingSpace> nghCreator = new GridCellNgh<ParkingSpace>(parkingSpots, homeLocation, ParkingSpace.class, 2, 2);
		List<GridCell<ParkingSpace>> gridCells = nghCreator.getNeighborhood(true);
		SimUtilities.shuffle(gridCells, RandomHelper.getUniform());

		//Check the neighbourhood for the closest empty spot
		// The spot has either no agent in it or an agent that isn't parked
		double minDistance = Double.MAX_VALUE, parkDistance = 0;
		GridCell<ParkingSpace> closestCell = null;
		ParkingSpace closestSpot = null;
		for (GridCell<ParkingSpace> cell : gridCells) {
			
			//Iterable<Object> spotUsers = parkingSpots.getObjectsAt(cell.getPoint().getX(),cell.getPoint().getY());
			//System.out.println("spotUsers: " + spotUsers + " size: "+ spotUsers.spliterator().estimateSize() + " somone parked: " + isSomeoneParked(parkingSpots, cell));
			
			ParkingSpace spot = (ParkingSpace) parkingSpots.getObjectAt(cell.getPoint().getX(),cell.getPoint().getY());
			
			System.out.println("this carType: " + this.carType + " and spot type: " + spot.getType());
			
			//if( spotUser != null) System.out.println("Spot "+ cell.getPoint().getX() + "," + cell.getPoint().getY() + " is not null but first occupant is parked?: " + spotUser.getHasParked());
			//if( spotUser == null || isSomeoneParked(parkingSpots, cell) == false) {
			if (!spot.getOccupied() && this.carType == spot.getType()) {
				parkDistance = parkingSpots.getDistance(homeLocation, cell.getPoint());
				if (parkDistance < minDistance) {
					minDistance = parkDistance;
					if(parkDistance == 0.0) System.out.println("is someone parked: " + spot.getOccupied());
					System.out.println("parkDistance: " + parkDistance);
					closestCell = cell;
					closestSpot = spot;
				}
			}
		}
		
		//If we have found a parking spot: park!
		if(closestCell != null) {
			System.out.println("I am moving to: " + closestCell.getPoint().getX() + "," + closestCell.getPoint().getY());
			parkingSpots.moveTo(this, closestCell.getPoint().getX(),closestCell.getPoint().getY());
			closestSpot.setOccupied(true);
			this.hasParked = true;
			
			if(closestSpot.getType() == "a" && this.happiness < 1) 
				this.happiness = (float) (this.happiness + 0.1 - (parkDistance * parkDistance * 0.01));
			
		} else {
			System.out.println("wasnt able to find a spot!!!!!!");
			if (this.happiness > 0) this.happiness = (float) (this.happiness - 0.3);
		}
		
	}
	


}