Ejemplo n.º 1
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);
                }
            }
            }
        }
Ejemplo n.º 2
0
        public void carFrame(RoadIntersection roadIntersection, RoadIntersection intersectingRoadIntersection)
        {
            for (int i = 0; i < roadIntersection.getRoad().getLanes(Settings.TRAFFIC_WEST_NORTH).Count(); i++)
            {
                Lane l = roadIntersection.getRoad().getLanes(Settings.TRAFFIC_WEST_NORTH)[i];

                for (int j = 0; j < l.getCars().Count(); j++)
                {
                    Car c = l.getCars()[j];
                    carFrameProcess(roadIntersection, intersectingRoadIntersection, l, c, Settings.TRAFFIC_WEST_NORTH);
                }
            }

            for (int i = 0; i < roadIntersection.getRoad().getLanes(Settings.TRAFFIC_EAST_SOUTH).Count(); i++)
            {
                Lane l = roadIntersection.getRoad().getLanes(Settings.TRAFFIC_EAST_SOUTH)[i];

                for (int j = 0; j < l.getCars().Count(); j++)
                {
                    Car c = l.getCars()[j];
                    carFrameProcess(roadIntersection, intersectingRoadIntersection, l, c, Settings.TRAFFIC_EAST_SOUTH);
                }
            }
        }