Example #1
0
  public void GoToDestination() {
    currGrid = cityData.cityGrid[personGui.getX() / 20][personGui.getY() / 20];
    if (walk == true) {
      personGui.DoGoToBuilding(destinationBuilding.buildingNumber);

      // PersonGui.DoWalk(if you ever hit an RGrid, acquire it first, and then walk through, and
      // don't walk through bgrids)
      walking = true;
      while (walking) {

        if (crossingRoad) {
          try {
            currRGrid.occupied.acquire();
            // System.out.println("1");
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          try {
            nextRGrid.occupied.acquire();
            // System.out.println("2");
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          personGui.crossRoad();
          try {
            gridding.acquire();
          } catch (Exception e) {
          }
          currRGrid.occupied.release();
          // System.out.println("1");
          nextRGrid.occupied.release();
          // System.out.println("0");
          // ACQUIRE CURRENT AND NEXT
        } else {
          //				// go from grid to grid
          //				currGrid = nextGrid
          ////				if undo
          ////				if(curr instanceof RGrid) {
          ////					person.acquire those next two semaphores in that direction
          ////					personGui.crossRoad(direction);
          ////					move to next
          ////					release roads
          ////				}
          //				else {
          //				//personGui.MoveToNextGrid;
          //				}
          // print("hello");

          personGui.MoveToNextGrid(currGrid);
          try {
            gridding.acquire();
          } catch (Exception e) {
          }
        }
      }
    }
    if (car == true) {
      currentBuilding.closest.acquireGrid();
      currRGrid = currentBuilding.closest;
      personGui.DoWalkToRGrid(currentBuilding.buildingNumber);
      // personGui.//acquire the semaphore of the
      // road(currentBuilding.closest.index1(),currentBuilding.closest.index2())
      // then he moves like a bus until he gets to his destinatoin's rgrid, gets off road
      // then he releases last road semaphore
      isMoving.drainPermits();
      try {
        isMoving.acquire();
      } catch (Exception e) {
      }

      System.err.println("hello");

      personGui.getInOrOutCar();
      personGui.DriveToDestination(destinationBuilding);
      // personGui.DriveToClosestRGrid(destinationBuilding); **** have similar mechanisms to
      // busgridbehavior
      // if person's rgrid is destination.closestRgrid, then, walk to building

      driving = true;
      while (driving) {

        if (gridToAcquire != null) {
          MoveToGrid();
          continue;
        }

        // puts this "mini-scheduler" to sleep
        try {
          isMoving.acquire();
        } catch (Exception e) {
        }
      }
      // remember to release current and previous grids
      currRGrid.releaseGrid();
      prevRGrid.releaseGrid();
      gridToAcquire.releaseGrid();

      // WALKT TO THE BUILDING NOW

      currRGrid = currentBuilding.closest;

      /*currRGrid = currentBuilding.closest;
      >>>>>>> 0a17ff0b1dfacf5cdfd33fa5a6faac8714cda482
      			//personGui.;
      			driving = true;
      			while(driving) {
      				try
      				{
      					//.acquire();
      				}
      				catch(Exception e){}

      				personGui.getInOrOutCar();
      				//personGui.DriveToClosestRGrid(destinationBuilding); **** have similar mechanisms to busgridbehavior
      				//if person's rgrid is destination.closestRgrid, then, walk to building

      				try
      				{
      					isMoving.acquire();
      				}
      				catch(Exception e){}
      				driving = false;
      			}*/
      // personGui
      // have similar mechanisms to busgridbehavior
      // if person's rgrid is destination.closestRgrid, then, walk to building
      // YOU WILL TURN INTO A ROBOT
    }
    if (bus == true) {
      destinationBusStop = currentBuilding.busStop;
      personGui.DoGoToBusStop(destinationBusStop);
      try {
        isMoving.acquire();
      } catch (Exception e) {
      }
      currentBusStop = destinationBusStop;
      destinationBusStop = destinationBuilding.busStop;

      currentBusStop.msgWaitingAtStop(this, destinationBusStop);
      try {
        isMoving.acquire();
      } catch (Exception e) {
      }

      if (currentBus == null) {
        try {
          isMoving.acquire();
        } catch (Exception e) {
        }
      }

      personGui.DoGoToBus(currentBus);
      try {
        isMoving.acquire();
      } catch (Exception e) {
      }

      cityData.guis.remove(personGui);
      currentBus.msgOnBus();
      try {
        isMoving.acquire();
      } catch (Exception e) {
      }

      cityData.guis.add(personGui);
      personGui.setXPos(currentBus.getX());
      personGui.setYPos(currentBus.getY());
      currentBus.msgOnBus();
      personGui.DoGoToBusStop(destinationBusStop); // THIS SHOULD ACQUIRE ROAD SEMAPHORE
      // if it needs to
      try {
        isMoving.acquire();
      } catch (Exception e) {
      }
    }

    currentBuilding = destinationBuilding;
  }