Пример #1
0
        public Car getCarInfront(Car car, bool trafficDirection)
        {
            Car closestCar = null;
            foreach (Car c in cars)
            {
                if (trafficDirection == Settings.TRAFFIC_EAST_SOUTH)
                {
                    if (c.getLanePosition() > car.getLanePosition())
                    {
                        if (closestCar == null)
                        {
                            closestCar = c;
                        }
                        else
                        {
                            if (c.getLanePosition() < closestCar.getLanePosition())
                            {
                                closestCar = c;
                            }
                        }
                    }
                }

                else
                {
                    if (c.getLanePosition() < car.getLanePosition())
                    {
                        if (closestCar == null)
                        {
                            closestCar = c;
                        }
                        else
                        {
                            if (c.getLanePosition() > closestCar.getLanePosition())
                            {
                                closestCar = c;
                            }
                        }
                    }
                }
            }

            if (closestCar == null)
            {
                //If there is not a car, set it to itself
                return car;
            }

            else
            {
                return closestCar;
            }
        }
Пример #2
0
        private void carFrameProcess(RoadIntersection roadIntersection, RoadIntersection intersectingRoadIntersection, Lane lane, Car car, Boolean trafficDirection)
        {
            int trafficCycleDistance;
            int middleLine;
            int stopLine;

            if (trafficDirection == Settings.TRAFFIC_EAST_SOUTH)
            {
                trafficCycleDistance = roadIntersection.getRoad().getRoadSpeed();
            }
            else
            {
                trafficCycleDistance = (-1 * roadIntersection.getRoad().getRoadSpeed());
            }

            //Move The Car Forward
            if(car.getStopped() == false) {
            if(randGen.NextDouble()*Settings.BREAKDOWN_PROBABILITY_LIMIT < Settings.getSimSettings().getBreakdownProbability()) {
                car.breakdown();
            } else {
                car.moveCar(trafficCycleDistance);
            }

            //Calculated the Middle Line Position

            middleLine = intersectingRoadIntersection.getIntersectionCenter();

            //Calculate the Stop Line Position

            if(Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_LEFT_HAND_TRAFFIC) {
                if(trafficDirection == Settings.TRAFFIC_EAST_SOUTH) {
                    stopLine = roadIntersection.getIntersectionCenter()-(intersectingRoadIntersection.getRoad().getNoLanes(Settings.TRAFFIC_WEST_NORTH)*Settings.LANE_WIDTH);
                } else {
                    stopLine = roadIntersection.getIntersectionCenter()+(intersectingRoadIntersection.getRoad().getNoLanes(Settings.TRAFFIC_EAST_SOUTH)*Settings.LANE_WIDTH);
                }
            } else {
                if(trafficDirection == Settings.TRAFFIC_EAST_SOUTH) {
                    stopLine = roadIntersection.getIntersectionCenter()-(intersectingRoadIntersection.getRoad().getNoLanes(Settings.TRAFFIC_EAST_SOUTH)*Settings.LANE_WIDTH);
                } else {
                    stopLine = roadIntersection.getIntersectionCenter()+(intersectingRoadIntersection.getRoad().getNoLanes(Settings.TRAFFIC_WEST_NORTH)*Settings.LANE_WIDTH);
                }
            }

            //-------------------------------------------//
            // Intersection Rules - Stopping and Turning //
            //-------------------------------------------//
            // Note: many of the conditional statements in this section are
            // multi-lined and indented for easier reading and understanding

            int directionMultiplier = 0;

            if(trafficDirection == Settings.TRAFFIC_EAST_SOUTH) {
                directionMultiplier = 1;
            } else {
                directionMultiplier = -1;
            }

            if(car.intersects(stopLine, roadIntersection.getRoad().getRoadSpeed(), trafficDirection)) {
                //Rules for when cars should stop.
                if (
                     roadIntersection.getLightState() != RoadIntersection.GREEN_LIGHT
                ) {
                    car.stopLight();
                    car.moveCar(-directionMultiplier*Settings.CAR_MOVE);
                }

                if (
                     roadIntersection.getLightState() != RoadIntersection.TURNING_GREEN_LIGHT
                     && car.getTurningRight() == true
                     && Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_LEFT_HAND_TRAFFIC
                     && ((roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH && trafficDirection == Settings.TRAFFIC_EAST_SOUTH) || (roadIntersection.getRoadOrientation() == Settings.ROAD_EAST_WEST && trafficDirection == Settings.TRAFFIC_WEST_NORTH))
                     && lane == roadIntersection.getRoad().getLane(trafficDirection, 0)
                ) {
                    car.stopLight();
                } else if (
                     roadIntersection.getLightState() != RoadIntersection.TURNING_GREEN_LIGHT
                     && car.getTurningRight() == true
                     && Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_LEFT_HAND_TRAFFIC
                     && ((roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH && trafficDirection == Settings.TRAFFIC_WEST_NORTH) || (roadIntersection.getRoadOrientation() == Settings.ROAD_EAST_WEST && trafficDirection == Settings.TRAFFIC_EAST_SOUTH))
                     && lane == roadIntersection.getRoad().getLane(trafficDirection, (roadIntersection.getRoad().getNoLanes(trafficDirection)-1))
                ) {
                    car.stopLight();
                } else if (
                     roadIntersection.getLightState() != RoadIntersection.TURNING_GREEN_LIGHT
                     && car.getTurningLeft() == true
                     && Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_RIGHT_HAND_TRAFFIC
                     && ((roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH && trafficDirection == Settings.TRAFFIC_WEST_NORTH) || (roadIntersection.getRoadOrientation() == Settings.ROAD_EAST_WEST && trafficDirection == Settings.TRAFFIC_EAST_SOUTH))
                     && lane == roadIntersection.getRoad().getLane(trafficDirection, 0)
                ) {
                    car.stopLight();
                } else if (
                     roadIntersection.getLightState() != RoadIntersection.TURNING_GREEN_LIGHT
                     && car.getTurningLeft() == true
                     && Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_RIGHT_HAND_TRAFFIC
                     && ((roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH && trafficDirection == Settings.TRAFFIC_EAST_SOUTH) || (roadIntersection.getRoadOrientation() == Settings.ROAD_EAST_WEST && trafficDirection == Settings.TRAFFIC_WEST_NORTH))
                     && lane == roadIntersection.getRoad().getLane(trafficDirection, (roadIntersection.getRoad().getNoLanes(trafficDirection)-1))
                ) {
                    car.stopLight();
                }

            } else  if(
                    car.intersects((stopLine+((Settings.CAR_LENGTH/2)*directionMultiplier)), roadIntersection.getRoad().getRoadSpeed(), trafficDirection)
                    && (Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_LEFT_HAND_TRAFFIC)
                    && (car.getTurningLeft() == true)
                    && (roadIntersection.getLightState() == RoadIntersection.GREEN_LIGHT)
            ) {
                //Car is turning left into first lane

                Lane turningLane = null;
                int laneLocation = 0;

                if(trafficDirection == Settings.TRAFFIC_EAST_SOUTH) {
                    if(lane == roadIntersection.getRoad().getLane(trafficDirection, (roadIntersection.getRoad().getNoLanes(trafficDirection)-1))) {
                        turningLane = intersectingRoadIntersection.getRoad().getLane(trafficDirection, 0);
                        laneLocation = intersectingRoadIntersection.getIntersectionCenter()+(((roadIntersection.getRoad().getNoLanes(trafficDirection)-1)*Settings.LANE_WIDTH));
                        car.turn(turningLane, lane, laneLocation);
                    }
                } else  {
                    if(lane == roadIntersection.getRoad().getLane(trafficDirection, 0)) {
                        turningLane = intersectingRoadIntersection.getRoad().getLane(trafficDirection, (intersectingRoadIntersection.getRoad().getNoLanes(trafficDirection)-1));
                        laneLocation = intersectingRoadIntersection.getIntersectionCenter()-(((roadIntersection.getRoad().getNoLanes(trafficDirection)-1)*Settings.LANE_WIDTH))-Settings.CAR_LENGTH;
                        car.turn(turningLane, lane, laneLocation);
                    }
                }

            } else if(
                    car.intersects((stopLine+(Settings.CAR_LENGTH/2)*directionMultiplier), roadIntersection.getRoad().getRoadSpeed(), trafficDirection)
                    && (Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_RIGHT_HAND_TRAFFIC)
                    && (car.getTurningRight() == true)
                    && (roadIntersection.getLightState() == RoadIntersection.GREEN_LIGHT)
            ) {
                //Car is turning right into first lane

                Lane turningLane = null;
                int laneLocation = 0;

                if((trafficDirection == Settings.TRAFFIC_EAST_SOUTH && roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH) || (trafficDirection == Settings.TRAFFIC_WEST_NORTH && roadIntersection.getRoadOrientation() == Settings.ROAD_EAST_WEST)) {
                    // Traffic heading South or West
                    if(lane == roadIntersection.getRoad().getLane(trafficDirection, 0)) {
                        // Traffic in the Left/Top Lane
                        if(roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH) {
                            // Traffic Heading South turning West
                            turningLane = intersectingRoadIntersection.getRoad().getLane(Settings.TRAFFIC_WEST_NORTH, 0);
                        } else {
                            // Traffic Heading West turning North
                            turningLane = intersectingRoadIntersection.getRoad().getLane(Settings.TRAFFIC_WEST_NORTH, (intersectingRoadIntersection.getRoad().getNoLanes(trafficDirection)-1));
                        }
                        laneLocation = intersectingRoadIntersection.getIntersectionCenter()-(((roadIntersection.getRoad().getNoLanes(trafficDirection))*Settings.LANE_WIDTH))-(Settings.CAR_LENGTH/2);
                        car.turn(turningLane, lane, laneLocation);
                    }
                } else {
                    // Traffic heading North or East
                    if(lane == roadIntersection.getRoad().getLane(trafficDirection, (roadIntersection.getRoad().getNoLanes(trafficDirection)-1))) {
                        // Traffic in the Bottom/Right Lane
                        if(roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH) {
                            // Traffic heading North turning East
                            turningLane = intersectingRoadIntersection.getRoad().getLane(Settings.TRAFFIC_EAST_SOUTH, (intersectingRoadIntersection.getRoad().getNoLanes(trafficDirection)-1));
                        } else {
                            // Traffic heading East turning South
                            turningLane = intersectingRoadIntersection.getRoad().getLane(Settings.TRAFFIC_EAST_SOUTH, 0);
                        }
                        laneLocation = intersectingRoadIntersection.getIntersectionCenter()+(((roadIntersection.getRoad().getNoLanes(trafficDirection))*Settings.LANE_WIDTH)-(Settings.CAR_LENGTH/2));
                        car.turn(turningLane, lane, laneLocation);
                    }
                }

            } else if(
                    car.intersects((middleLine+(Settings.CAR_LENGTH/2)*directionMultiplier), roadIntersection.getRoad().getRoadSpeed(), trafficDirection)
                    && (Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_LEFT_HAND_TRAFFIC)
                    && (car.getTurningRight() == true)
                    && (roadIntersection.getLightState() == RoadIntersection.TURNING_GREEN_LIGHT)
            ) {
                //Car is turning right into second lane

                Lane turningLane = null;
                int laneLocation = 0;

                if((trafficDirection == Settings.TRAFFIC_EAST_SOUTH && roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH) || (trafficDirection == Settings.TRAFFIC_WEST_NORTH && roadIntersection.getRoadOrientation() == Settings.ROAD_EAST_WEST)) {
                    // Traffic heading South or West
                    if(lane == roadIntersection.getRoad().getLane(trafficDirection, 0)) {
                        // Traffic in the Left/Top Lane
                        if(roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH) {
                            // Traffic Heading South turning West
                            turningLane = intersectingRoadIntersection.getRoad().getLane(Settings.TRAFFIC_WEST_NORTH, 0);
                        } else {
                            // Traffic Heading West turning North
                            turningLane = intersectingRoadIntersection.getRoad().getLane(Settings.TRAFFIC_WEST_NORTH, (intersectingRoadIntersection.getRoad().getNoLanes(trafficDirection)-1));
                        }
                        laneLocation = intersectingRoadIntersection.getIntersectionCenter()-(Settings.CAR_LENGTH/2);
                        car.turn(turningLane, lane, laneLocation);
                    }
                } else {
                    // Traffic heading North or East
                    if(lane == roadIntersection.getRoad().getLane(trafficDirection, (roadIntersection.getRoad().getNoLanes(trafficDirection)-1))) {
                        // Traffic in the Bottom/Right Lane
                        if(roadIntersection.getRoadOrientation() == Settings.ROAD_SOUTH_NORTH) {
                            // Traffic heading North turning East
                            turningLane = intersectingRoadIntersection.getRoad().getLane(Settings.TRAFFIC_EAST_SOUTH, (intersectingRoadIntersection.getRoad().getNoLanes(trafficDirection)-1));
                        } else {
                            // Traffic heading East turning South
                            turningLane = intersectingRoadIntersection.getRoad().getLane(Settings.TRAFFIC_EAST_SOUTH, 0);
                        }
                        laneLocation = intersectingRoadIntersection.getIntersectionCenter()+(Settings.CAR_LENGTH/2);
                        car.turn(turningLane, lane, laneLocation);
                    }
                }

            } else if(
                    car.intersects((middleLine+(Settings.CAR_LENGTH/2)), roadIntersection.getRoad().getRoadSpeed(), trafficDirection)
                    && (Settings.getSimSettings().getTrafficFlow() == Settings.TRAFFIC_FLOW_RIGHT_HAND_TRAFFIC)
                    && (car.getTurningLeft() == true)
                    //&& (roadIntersection.getRoad().getLane(trafficDirection, (roadIntersection.getRoad().getNoLanes(trafficDirection)-1)) == lane)
                    && (roadIntersection.getLightState() == RoadIntersection.TURNING_GREEN_LIGHT)
            ) {
                //TO BE IMPLEMENTED
            }

            if(car.intersects(lane.getCarInfront(car, trafficDirection))) {

                bool lanesChanged = false;

                foreach (Lane nl in roadIntersection.getRoad().getNeighbouringLanes(lane, trafficDirection)) {
                    if(nl.isLaneClear((car.getLanePosition()+(trafficCycleDistance*2)))) {
                        roadIntersection.getRoad().trafficChangeLane(lane, car, nl);

                        lanesChanged = true;
                        break;
                    }
                }
                car.setStopped(!lanesChanged);
                if(lanesChanged == false) {
                    car.moveCar(-2*trafficCycleDistance);
                }
            }
            } else {

            Car fakeCar = new Car(car.getLanePosition());
            fakeCar.moveCar(trafficCycleDistance*2);

            if(fakeCar.intersects(lane.getCarInfront(car, trafficDirection)) && lane.getCarInfront(car, trafficDirection).getBrokenDown() == true) {
                bool lanesChanged = false;

                foreach (Lane nl in roadIntersection.getRoad().getNeighbouringLanes(lane, trafficDirection)) {
                    if(nl.isLaneClear(car.getLanePosition()+trafficCycleDistance)) {
                        roadIntersection.getRoad().trafficChangeLane(lane, car, nl);

                        lanesChanged = true;
                        break;
                    }
                }

                if(lanesChanged == true) {
                    car.setStopped(false);
                }

            } else if(car.getBrokenDown() != true && car.getLightStopped() != true) {
                if(!fakeCar.intersects(lane.getCarInfront(car, trafficDirection))) {
                    //car infront has moved!
                    car.setStopped(false);
                }
            }
            }
        }
Пример #3
0
        public void run()
        {
            frameCount++;

            // Generate New Cars
            if (frameCount % Settings.CAR_FREQUENCY == 0)
            {
                //Horizontal Road
                if (randGen.Next(100) < (Settings.getSimSettings().getHCarProbability() * 100))
                {
                    Car workingCar = null;
                    Road roadH = modelIntersection.gethRoadIntersection().getRoad();
                    int randHEast = 0, randHWest = 0;
                    Console.WriteLine(roadH.getNoLanes(Settings.TRAFFIC_WEST_NORTH));
                    Console.WriteLine(roadH.getRoadLength());
                    if (randGen.Next(roadH.getNoLanes(Settings.TRAFFIC_WEST_NORTH)) > 1)
                    {
                        randHWest = randGen.Next(roadH.getNoLanes(Settings.TRAFFIC_WEST_NORTH));
                    }
                    else
                    {
                        randHWest = 0;
                    }

                    if (randGen.Next(roadH.getNoLanes(Settings.TRAFFIC_EAST_SOUTH)) > 1)
                    {
                        randHEast = randGen.Next(roadH.getNoLanes(Settings.TRAFFIC_EAST_SOUTH));
                    }
                    else
                    {
                        randHEast = 0;
                    }

                    Console.WriteLine(randHWest);
                    Console.WriteLine(randHEast);

                    int lanePositionHWest = roadH.getRoadLength();
                    int lanePositionHEast = 0;

                    if (this.trafficJam(modelIntersection.gethRoadIntersection().getRoad(), Settings.TRAFFIC_WEST_NORTH) == false)
                    {
                        modelIntersection.gethRoadIntersection().getRoad().getLane(Settings.TRAFFIC_WEST_NORTH, randHWest).addCar(workingCar = new Car(lanePositionHWest));

                        if (randGen.Next(100) < (Settings.getSimSettings().getTurnLeftProbability() * 100))
                        {
                            workingCar.setTurningLeft(true);
                        }

                        if (randGen.Next(100) < (Settings.getSimSettings().getTurnRightProbability() * 100))
                        {
                            workingCar.setTurningRight(true);
                        }
                    }

                    workingCar = null;

                    if (this.trafficJam(modelIntersection.gethRoadIntersection().getRoad(), Settings.TRAFFIC_EAST_SOUTH) == false)
                    {
                        modelIntersection.gethRoadIntersection().getRoad().getLane(Settings.TRAFFIC_EAST_SOUTH, randHEast).addCar(workingCar = new Car(lanePositionHEast));

                        if (randGen.Next(100) < (Settings.getSimSettings().getTurnLeftProbability() * 100))
                        {
                            workingCar.setTurningLeft(true);
                        }

                        if (randGen.Next(100) < (Settings.getSimSettings().getTurnRightProbability() * 100))
                        {
                            workingCar.setTurningRight(true);
                        }
                    }

                }

                if (randGen.Next(100) < (Settings.getSimSettings().getVCarProbability() * 100))
                {
                    //Vertical Road
                    Car workingCar = null;
                    Road roadV = modelIntersection.getvRoadIntersection().getRoad();
                    int randVNorth = randGen.Next(roadV.getNoLanes(Settings.TRAFFIC_WEST_NORTH));
                    int randVSouth = randGen.Next(roadV.getNoLanes(Settings.TRAFFIC_EAST_SOUTH));
                    int lanePositionVNorth = roadV.getRoadLength();
                    int lanePositionVSouth = 0;

                    if (this.trafficJam(modelIntersection.getvRoadIntersection().getRoad(), Settings.TRAFFIC_WEST_NORTH) == false)
                    {
                        modelIntersection.getvRoadIntersection().getRoad().getLane(Settings.TRAFFIC_WEST_NORTH, randVNorth).addCar(workingCar = new Car(lanePositionVNorth));

                        if (randGen.Next(100) < (Settings.getSimSettings().getTurnLeftProbability() * 100))
                        {
                            workingCar.setTurningLeft(true);
                        }

                        if (randGen.Next(100) < (Settings.getSimSettings().getTurnRightProbability() * 100))
                        {
                            workingCar.setTurningRight(true);
                        }
                    }

                    workingCar = null;

                    if (this.trafficJam(modelIntersection.getvRoadIntersection().getRoad(), Settings.TRAFFIC_EAST_SOUTH) == false)
                    {
                        modelIntersection.getvRoadIntersection().getRoad().getLane(Settings.TRAFFIC_EAST_SOUTH, randVSouth).addCar(workingCar = new Car(lanePositionVSouth));

                        if (randGen.Next(100) < (Settings.getSimSettings().getTurnLeftProbability() * 100))
                        {
                            workingCar.setTurningLeft(true);
                        }

                        if (randGen.Next(100) < (Settings.getSimSettings().getTurnRightProbability() * 100))
                        {
                            workingCar.setTurningRight(true);
                        }

                    }

                }
            }
            carFrame(modelIntersection.getvRoadIntersection(), modelIntersection.gethRoadIntersection());
            carFrame(modelIntersection.gethRoadIntersection(), modelIntersection.getvRoadIntersection());

            window.Refresh();
        }
Пример #4
0
 public void trafficChangeLane(Lane l, Car c, Lane nl)
 {
     Car tempCar = c;
     l.removeCar(c);
     nl.addCar(tempCar);
 }
Пример #5
0
 public void addCar(Car car)
 {
     this.cars.Add(car);
 }
Пример #6
0
 public void removeCar(Car car)
 {
     this.cars.Remove(car);
 }
Пример #7
0
 public carBreakdownTask(Car car)
 {
     this.car = car;
     run();
 }
Пример #8
0
        public bool intersects(Car car, int offset)
        {
            int lanePositionStart = this.getLanePosition() - Math.Abs(offset);
            int lanePositionEnd = this.getLanePosition() + Settings.CAR_LENGTH + Math.Abs(offset);

            if (((car.getLanePosition() < lanePositionEnd) && (lanePositionEnd < (car.getLanePosition() + Settings.CAR_LENGTH)) || ((lanePositionStart < this.getLanePosition()) && (lanePositionStart < (car.getLanePosition() + Settings.CAR_LENGTH)))))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Пример #9
0
 public bool intersects(Car car)
 {
     if (((car.getLanePosition() < (this.getLanePosition() + Settings.CAR_LENGTH)) && ((this.getLanePosition() + Settings.CAR_LENGTH) < (car.getLanePosition() + Settings.CAR_LENGTH)) || ((car.getLanePosition() < this.getLanePosition()) && (this.getLanePosition() < (car.getLanePosition() + Settings.CAR_LENGTH)))))
     {
         return true;
     }
     else
     {
         return false;
     }
 }