Package trafficjams.model.classes

Examples of trafficjams.model.classes.Road


        float gorLength = Numbers.mapWidth / (float)crossCount /*- 2*/ ;
        float vertLength = Numbers.mapHeight / (float) crossCount /*- 2*/ ;
        ICrossRoad[][] a_crossRoads= new ICrossRoad[crossCount][crossCount];
        ArrayList<ICrossRoad> l_crossRoads = new ArrayList<ICrossRoad>(crossCount*crossCount);
        ArrayList<IRoad> roads = new ArrayList<IRoad>();
        Road r = null;

        //создаем перекрестки и дороги ввиде сетки, уже смещенной
        for (int i = 0 ; i<crossCount; ++i){
            if (i==0){
                for (int j = 0 ; j < crossCount; ++j){
                    CrossRoad currCross = new CrossRoad(retVal);
                    l_crossRoads.add(currCross);
                    currCross.setCoord(new Point(gorLength*i + gorLength*rand(), vertLength*j + vertLength * rand()));
                    a_crossRoads[i][j] = currCross;
                    if (j==0){
                        continue;
                    }
                    r = new Road(retVal);
                    roads.add(r);
                    r.setFCross(a_crossRoads[i][j-1]);
                    r.setLCross(currCross);

                    ((CrossRoad)a_crossRoads[i][j-1]).setRoadForDirection(r, right);
                    currCross.setRoadForDirection(r, left);

                }
                continue;
            }

            for (int j = 0 ; j < crossCount; ++j){
                CrossRoad currCross = new CrossRoad(retVal);
                l_crossRoads.add(currCross);
                currCross.setCoord(new Point(gorLength*i + gorLength*rand(), vertLength*j + vertLength * rand()));
                a_crossRoads[i][j] = currCross;
                r = new Road(retVal);
                roads.add(r);
                r.setFCross(a_crossRoads[i-1][j]);
                r.setLCross(currCross);
                ((CrossRoad)a_crossRoads[i-1][j]).setRoadForDirection(r, down);
                currCross.setRoadForDirection(r, up);
                if (j==0){
                    continue;
                }
                r = new Road(retVal);
                roads.add(r);
                r.setFCross(a_crossRoads[i][j-1]);
                r.setLCross(currCross);

                ((CrossRoad)a_crossRoads[i][j-1]).setRoadForDirection(r, right);
                currCross.setRoadForDirection(r, left);

            }
        }

        //убиваем координаты выходящие за границы(выставляем в границы)
        this.removeNegatives(l_crossRoads);

        //выбираем дороги для уничтожения
        ArrayList<IRoad> toDelete = new ArrayList<IRoad>();
        for (IRoad __r : roads){
            if (Math.random()<Numbers.percent){
                toDelete.add(__r);
            }
        }

        //удаляем дороги
        for (IRoad __r : toDelete){
            if (!roads.remove(__r))
                throw new Exception("дорога не удалилась!");
            Road _r = (Road) __r;
            ((CrossRoad)_r.getFCross()).deleteRoad(_r);
            ((CrossRoad)_r.getLCross()).deleteRoad(_r);
        }

        //удаляем перекрестки в которые входят 0 или 2 дороги (и соединяем дорогий то что разъединилось)
        for (int i = 0 ; i<crossCount ; ++i){
            for (int j = 0 ; j<crossCount; ++j){
                CrossRoad cr = (CrossRoad) a_crossRoads[i][j];
                if (cr.getType()  == 0){
                    if (!l_crossRoads.remove(a_crossRoads[i][j]))
                        throw new Exception("перекресток не удалился!");
                    a_crossRoads[i][j] = null;
                }
                if (cr.getType()  == 2){
                    if (!l_crossRoads.remove(a_crossRoads[i][j]))
                        throw new Exception("перекресток не удалился!");
                    a_crossRoads[i][j] = null;
                    IRoad[] rd = cr.get2Roads();
                    CrossRoad c0 = (CrossRoad)((Road)rd[0]).getOtherEnd(cr);
                    CrossRoad c1 = (CrossRoad)((Road)rd[1]).getOtherEnd(cr);

                    if (!(roads.remove(rd[0]) && roads.remove(rd[1])))
                        throw new Exception("road remove error");

                    IRoad newRoad = this.createConnector(c0,c1);
                    roads.add(newRoad);
                    ((CrossRoad) c0).setRoadForDirection(newRoad, c0.deleteRoad(rd[0]));
                    ((CrossRoad) c1).setRoadForDirection(newRoad, c1.deleteRoad(rd[1]));
                }
            }
        }

        //проверяем на связность
        if (!this.isCorrect(l_crossRoads, roads, tryCount)){
            return this.makeRoadMap(crossCount,autoCount,--tryCount);
        }

        //проверяем перекрестки и инициализируем очереди
        for (ICrossRoad iCrossRoad: l_crossRoads){
            CrossRoad crossRoad = (CrossRoad) iCrossRoad;
            if (crossRoad.getType()==2){
                return this.makeRoadMap(crossCount, autoCount, --tryCount);
            }
            crossRoad.initQueues();
        }

        ///выставляем длины дорог
        float maxLength = 0;
        float minLength = Math.max(Numbers.mapHeight, Numbers.mapWidth);
        for (IRoad __r : roads){
            Road _r = (Road)__r;
            float l = 0;
            float x1 = _r.getFCross().getCoord().getX();
            float x2 = _r.getLCross().getCoord().getX();
            float y1 = _r.getFCross().getCoord().getY();
            float y2 = _r.getLCross().getCoord().getY();
            l = (float) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
            _r.setLength(l);
            if (l>maxLength){
                maxLength = l;
            }
            if (l<minLength){
                minLength = l ;
View Full Code Here


            cr.setCoord(p);
        }
    }

    private IRoad createConnector(ICrossRoad c0, ICrossRoad c1) {
        Road retVal = new Road(c0.getMap());
        if (c0.getCoord().getY()<c1.getCoord().getY()){
            retVal.setFCross(c0);
            retVal.setLCross(c1);
        }else {
            if (c0.getCoord().getX()<c1.getCoord().getX()){
                retVal.setFCross(c0);
                retVal.setLCross(c1);
            }else {
                retVal.setFCross(c1);
                retVal.setLCross(c0);
            }
        }
        return retVal;
    }
View Full Code Here

TOP

Related Classes of trafficjams.model.classes.Road

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.