<commit_before>package transportation.Agents;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.Semaphore;

import astar.astar.Position;
import simcity.PersonAgent;
import simcity.interfaces.Person;
import transportation.GUIs.BusGui;
import transportation.Objects.BusRider;
import transportation.Objects.BusStop;
import transportation.Objects.MovementTile;

public class BusAgent extends MobileAgent{

	private final float fare = 0.00f;
	private float collectedFare = 0.00f;
	private Position currentPosition;
	private List<BusRider> busRiders;
	private Queue<Position> route;
	BusStop currentBusStop;
	BusGui gui;
	Semaphore animSem;

	TransportationController master;

	public BusAgent(TransportationController master, Position position) {
		collectedFare = 0;
		currentBusStop = null;
		busRiders = Collections.synchronizedList(new ArrayList<BusRider>());
		route = new LinkedList<Position>();
		this.master = master;
		currentPosition = position;
		try {
			master.getGrid()[currentPosition.getX()][currentPosition.getY()].acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		animSem = new Semaphore(0, true);
		createRoute();
		
	}

	//+++++++++++++++++MESSAGES+++++++++++++++++
	public void msgPayFare(Person person, float fare) {
		for(BusRider busRider : busRiders) {
			if(busRider.getPerson() == person) {
				busRider.state = BusRider.RiderState.RIDING;
				collectedFare += fare;
				stateChanged();
			}
		}
	}

	public void msgReachedBusStop(BusStop busStop) {
		currentBusStop = busStop;
		stateChanged();
	}
	
	public void msgHalfway() {//Releases semaphore at halfway point to prevent sprites from colliding majorly
		if(master.getGrid()[currentPosition.getX()][currentPosition.getY()].availablePermits() == 0)
			master.getGrid()[currentPosition.getX()][currentPosition.getY()].release();
		//System.out.println(String.valueOf(master.getGrid()[currentPosition.getX()][currentPosition.getY()].availablePermits()));
	}

	public void msgDestination() {
		animSem.release();
	}
	
	//+++++++++++++++++SCHEDULER+++++++++++++++++
	@Override
	protected boolean pickAndExecuteAnAction() {
		if(currentBusStop != null) {
			dropOffRiders();
			pickUpRiders();
		}

		synchronized(busRiders) {
			for(BusRider busRider : busRiders) {
				if(busRider.state == BusRider.RiderState.HASTOPAY) {
					gui.setStill();
					return false;
				}
			}
		}

		moveToNextBusStop();
		return true;
	}

	//+++++++++++++++++ACTIONS+++++++++++++++++
	private void dropOffRiders() {
		for(BusRider busRider : busRiders) {
			if(busRider.getFinalStop() == currentBusStop){
				master.msgWantToGo(currentBusStop.getName(), busRider.getDestination(), busRider.getPerson(), "Walk", "Edgar");
				//send request to spawn walker to transportation
				busRider.state = BusRider.RiderState.GOTOFF;
			}
		}
	}

	private void pickUpRiders() {
		List<BusRider> tempList = currentBusStop.getBusWaiters();
		for(BusRider busRider : tempList) {
			System.out.println("gotRider");
			busRiders.add(busRider);
			busRider.getPerson().msgPayFare(fare);
			busRider.state = BusRider.RiderState.HASTOPAY;
			//msgPayFare(busRider.getPerson(), fare);
			//busRider.state = BusRider.RiderState.RIDING;
		}
		currentBusStop.clearRiders();
	}

	//Also removes BusRiders that aren't on the bus anymore
	private void moveToNextBusStop() {
		synchronized(busRiders) {
			for(int i = busRiders.size() - 1 ; i >= 0; i--) {
				if(busRiders.get(i).state == BusRider.RiderState.GOTOFF)
					busRiders.remove(i);
			}
		}

		MovementTile[][] grid = master.grid;
		while(true) {
			//currentBusStop = null;
			Position nextPosition = route.poll();
			route.add(nextPosition);//bus must be able to loop
			try {
				grid[nextPosition.getX()][nextPosition.getY()].acquire();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();<commit_msg>Bus now pauses in front of bus stops<commit_after>package transportation.Agents;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.Semaphore;

import astar.astar.Position;
import simcity.PersonAgent;
import simcity.interfaces.Person;
import transportation.GUIs.BusGui;
import transportation.Objects.BusRider;
import transportation.Objects.BusStop;
import transportation.Objects.MovementTile;

public class BusAgent extends MobileAgent{

	private final float fare = 0.00f;
	private float collectedFare = 0.00f;
	private Position currentPosition;
	private List<BusRider> busRiders;
	private Queue<Position> route;
	BusStop currentBusStop;
	BusGui gui;
	Semaphore animSem;

	TransportationController master;

	public BusAgent(TransportationController master, Position position) {
		collectedFare = 0;
		currentBusStop = null;
		busRiders = Collections.synchronizedList(new ArrayList<BusRider>());
		route = new LinkedList<Position>();
		this.master = master;
		currentPosition = position;
		try {
			master.getGrid()[currentPosition.getX()][currentPosition.getY()].acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		animSem = new Semaphore(0, true);
		createRoute();
		
	}

	//+++++++++++++++++MESSAGES+++++++++++++++++
	public void msgPayFare(Person person, float fare) {
		for(BusRider busRider : busRiders) {
			if(busRider.getPerson() == person) {
				busRider.state = BusRider.RiderState.RIDING;
				collectedFare += fare;
				stateChanged();
			}
		}
	}

	public void msgReachedBusStop(BusStop busStop) {
		currentBusStop = busStop;
		stateChanged();
	}
	
	public void msgHalfway() {//Releases semaphore at halfway point to prevent sprites from colliding majorly
		if(master.getGrid()[currentPosition.getX()][currentPosition.getY()].availablePermits() == 0)
			master.getGrid()[currentPosition.getX()][currentPosition.getY()].release();
		//System.out.println(String.valueOf(master.getGrid()[currentPosition.getX()][currentPosition.getY()].availablePermits()));
	}

	public void msgDestination() {
		animSem.release();
	}
	
	//+++++++++++++++++SCHEDULER+++++++++++++++++
	@Override
	protected boolean pickAndExecuteAnAction() {
		if(currentBusStop != null) {
			dropOffRiders();
			pickUpRiders();
		}

		synchronized(busRiders) {
			for(BusRider busRider : busRiders) {
				if(busRider.state == BusRider.RiderState.HASTOPAY) {
					gui.setStill();
					return false;
				}
			}
		}

		moveToNextBusStop();
		return true;
	}

	//+++++++++++++++++ACTIONS+++++++++++++++++
	private void dropOffRiders() {
		for(BusRider busRider : busRiders) {
			if(busRider.getFinalStop() == currentBusStop){
				master.msgWantToGo(currentBusStop.getName(), busRider.getDestination(), busRider.getPerson(), "Walk", "Edgar");
				//send request to spawn walker to transportation
				busRider.state = BusRider.RiderState.GOTOFF;
			}
		}
	}

	private void pickUpRiders() {
		gui.setStill();
		try { Thread.sleep(500); }
		catch (Exception e){}
		List<BusRider> tempList = currentBusStop.getBusWaiters();
		for(BusRider busRider : tempList) {
			System.out.println("gotRider");
			busRiders.add(busRider);
			busRider.getPerson().msgPayFare(fare);
			busRider.state = BusRider.RiderState.HASTOPAY;
			//msgPayFare(busRider.getPerson(), fare);
			//busRider.state = BusRider.RiderState.RIDING;
		}
		currentBusStop.clearRiders();
	}

	//Also removes BusRiders that aren't on the bus anymore
	private void moveToNextBusStop() {
		synchronized(busRiders) {
			for(int i = busRiders.size() - 1 ; i >= 0; i--) {
				if(busRiders.get(i).state == BusRider.RiderState.GOTOFF)
					busRiders.remove(i);
			}
		}

		MovementTile[][] grid = master.grid;
		while(true) {
			//currentBusStop = null;
			Position nextPosition = route.poll();
			route.add(nextPosition);//bus must be able to loop
			try {
				grid[nextPosition.getX()][nextPosition.getY()].acquire();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
{'fraction_non_alphanumeric': 0.1471382952591501, 'fraction_numerical': 0.0030704986489805946, 'mean_word_length': 4.585048010973937, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3818556', 'n_tokens_mistral': 3007, 'n_tokens_neox': 2644, 'n_words': 512}