Esempio n. 1
0
    //returns the mid unit at the specified su or null if one does not exist
    public TransportUnit getMid(SurfaceUnit su)
    {
        //quick check to see if this mid is out of range on the side
        //later it will be transformed into a unit of other side in order to connect sides
        if (su.u < -halfmidperglob || su.u >= halfmidperglob || su.v < -halfmidperglob || su.v >= halfmidperglob)
        {
            return(null);
        }

        TransportUnit mu = null;

        //if it is in the mid list, return it
        if (midTUs.TryGetValue(su, out mu))
        {
            return(mu);
        }
        //if it's not in the mid list, check if a large unit will contain it

        //the coordinates of the proposed large unit
        SurfaceUnit lus = getLargeSU(su);
        //retrieve the large unit
        TULarge lu = getLarge(lus);

        //Debug.Log("proposed " + lus);
        //if the lu has already been populated, the mu will never exist, so return null
        if (lu.populated)
        {
            return(null);
        }
        else
        {
            populateLarge(lu, lus);
            //populate it with mid units(later will be moved to a separate function)

            /*	int startu = lus.u*largeTUWidth;
             *      int startv = lus.v*largeTUWidth;
             *      //loop through all mid units in the large unit and create them
             *      for(int i = startu; i<startu+largeTUWidth; i++)
             *      {
             *              for(int j = startv; j<startv+largeTUWidth; j++)
             *              {
             *                      //create a new base unit, set its properties, and add it to the base list
             *                      TransportUnit newTU = new TransportUnit();
             *                      newTU.conUp = true;//Random.value>0.5f;
             *                      newTU.conRight = true;//Random.value>0.5f;
             *                      newTU.indexI = i;
             *                      newTU.indexJ = j;
             *                      newTU.conPoint = new Vector2((i+0.5f)*midTUWidth + Random.value-0.5f,(j+0.5f)*midTUWidth + Random.value-0.5f);
             *                      //newTU.conPoint = new Vector2((i+0.5f)*midTUWidth,(j+0.5f)*midTUWidth);
             *                      //newTU.conPoint = new Vector2((i+Random.value)*midTUWidth,(j+Random.value)*midTUWidth);
             *                      midTUs.Add(new SurfaceUnit(su.side, i, j), newTU);
             *              }
             *      }*/
            lu.populated = true;
            //use recursion to return to the top of the function and return the newly created(or not) mid unit
            return(getMid(su));
        }
    }
Esempio n. 2
0
 public bool DeleteTable(TransportUnit unit)
 {
     try
     {
         using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Buses.db")))
         {
             connection.Delete(unit);
             return(true);
         }
     }
     catch (SQLiteException ex)
     {
         Log.Info("SQLiteEx", ex.Message);
         return(false);
     }
 }
Esempio n. 3
0
    //returns the mid unit from midTUs at index u,v and creates one if necessary
    private TransportUnit buildMid(int i, int j, PSide side)
    {
        TransportUnit mu = null;
        SurfaceUnit   su = new SurfaceUnit(side, i, j);

        //Debug.Log(su);
        if (!midTUs.TryGetValue(su, out mu))
        {
            mu        = new TransportUnit();
            mu.indexI = i;
            mu.indexJ = j;
            midTUs.Add(su, mu);
            indexList.Add(mu);
        }
        return(mu);
    }
Esempio n. 4
0
        public bool UpdateTable(TransportUnit unit)
        {
            try
            {
                using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Buses.db")))
                {
                    connection.Query <TransportUnit>("UPDATE Buses set wayTo=?,wayFrom=? Where number=?", unit.wayTo, unit.wayFrom, unit.number);

                    return(true);
                }
            }
            catch (SQLiteException ex)
            {
                Log.Info("SQLiteEx", ex.Message);
                return(false);
            }
        }
Esempio n. 5
0
    public Dictionary <SurfaceUnit, TUBase> populate(TransportUnit mu, SurfaceUnit su)
    {
        //nullify baseList so it can be reused for this mid unit
        baseList = null;
        midU     = mu;

        //an array of base transport units that will eventually be returned
        //baseList = new TransportUnit[midTUWidth, midTUWidth];
        baseList = new Dictionary <SurfaceUnit, TUBase>();

        midU       = mu;
        baseIndexI = midU.indexI * midTUWidth;
        baseIndexJ = midU.indexJ * midTUWidth;
        fill(mu, su);

        return(baseList);
    }
Esempio n. 6
0
 //connects two transport units based on their relation to each other
 public void connectUnits(TransportUnit u1, TransportUnit u2, int lev) //the two units to set connectivity, lev is basically street size(1 is largest)
 {
     if (u1.indexI + 1 == u2.indexI && u1.indexJ == u2.indexJ)         //if the second unit is directly to the right of the first one
     {
         u1.conRight = true;                                           //connect the first u to the right because the second u is on the right
         u1.RightLev = lev;
     }
     else if (u1.indexI - 1 == u2.indexI && u1.indexJ == u2.indexJ)         //if the second u unit is directly to the left of the first one
     {
         u2.conRight = true;
         u2.RightLev = lev;
     }
     else if (u1.indexI == u2.indexI && u1.indexJ + 1 == u2.indexJ)         //if the second u unit is directly to the top of the first one
     {
         u1.conUp = true;
         u1.UpLev = lev;
     }
     else if (u1.indexI == u2.indexI && u1.indexJ - 1 == u2.indexJ)         //if the second u unit is directly to the bottom of the first one
     {
         u2.conUp = true;
         u2.UpLev = lev;
     }
 }
Esempio n. 7
0
    //fills a mid unit with base units
    //NOTE: target refers to the intersection while build direction refers to an unmodified straight street path
    public void fill(TransportUnit mu, SurfaceUnit su)
    {
        //set the conPoint of the base unit that contains the mid unit's conPoint
        int    powIndexX = Mathf.FloorToInt(mu.conPoint.x);
        int    powIndexY = Mathf.FloorToInt(mu.conPoint.y);
        TUBase powUnit   = getBase(powIndexX, powIndexY);

        powUnit.conPoint = mu.conPoint;
        powUnit.conSet   = true;

        //MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(su.side, powUnit.conPoint.x, powUnit.conPoint.y),
        //                     WorldManager.curPlanet.radius, 64*16), 3);

        //Debug.Log(mu.conPoint + " " + powUnit.conPoint);
        //Debug.Log(powIndexX + " " + powIndexY);

        //the mid transport units to the left and bottom of the current mid unit
        //TransportUnit leftTU = tran.getAdjustedMid(new SurfaceUnit(su.side,su.u - 1, su.v));
        TransportUnit leftTU  = tran.getMid(new SurfaceUnit(su.side, su.u - 1, su.v));
        TransportUnit downTU  = tran.getMid(new SurfaceUnit(su.side, su.u, su.v - 1));
        TransportUnit rightTU = tran.getMid(new SurfaceUnit(su.side, su.u + 1, su.v));
        TransportUnit upTU    = tran.getMid(new SurfaceUnit(su.side, su.u, su.v + 1));

        //the directions a street will connect to from the center
        bool conRight = false;
        bool conLeft  = false;
        bool conUp    = false;
        bool conDown  = false;


        //the conPoint of the mid unit above, below, to the right and left of this one
        //have to initially set them to something whether they are used or not
        Vector2 conPointRight = Vector2.zero;
        Vector2 conPointLeft  = Vector2.zero;
        Vector2 conPointUp    = Vector2.zero;
        Vector2 conPointDown  = Vector2.zero;


        //initialize street levels
        int rightLev = 0;
        int leftLev  = 0;
        int upLev    = 0;
        int downLev  = 0;

        //check if each mid unit exists, and set connections as necesary HOW DO YOU SPELL NECESSARY?!?!?!?!?!?!??!???!
        if (rightTU != null)
        {
            conPointRight = rightTU.conPoint;
            conRight      = mu.conRight;
            rightLev      = mu.RightLev;
        }
        if (leftTU != null)
        {
            conPointLeft = leftTU.conPoint;
            conLeft      = leftTU.conRight;       //if the unit to the left connects to the right, then this unit will connect to the left
            leftLev      = leftTU.RightLev;
        }
        if (upTU != null)
        {
            conPointUp = upTU.conPoint;
            conUp      = mu.conUp;
            upLev      = mu.UpLev;
        }
        if (downTU != null)
        {
            conPointDown = downTU.conPoint;
            conDown      = downTU.conUp;
            downLev      = downTU.UpLev;
        }

        //Debug.Log(conPointUp + " " + conPointRight + " " + conPointLeft + " " + conPointDown);


        //the slope that all streets aim for when they converge in the middle
        float targetSlopeRight = 0;
        float targetSlopeLeft  = 0;
        float targetSlopeUp    = 0;
        float targetSlopeDown  = 0;

        //sets all the target slopes based on what sides connect
        if (conUp && conDown && conRight && conLeft)
        {         //form a 4 way perpindicular intersection
            //vector representing the direction from the bottom to top street point
            Vector2 DownUpVec = conPointUp - conPointDown;

            //vector representing the direction from the left to right street point
            Vector2 LeftRightVec = conPointRight - conPointLeft;

            //the vector perpindicular to the the left right vector used to find the target inter line
            Vector2 LeftRightPerp = new Vector2(-LeftRightVec.y, LeftRightVec.x);            //opposite reciprocal

            //the target vector that the street coming from above will aim for at the intersection
            Vector2 targetVecUpDown = (DownUpVec.normalized + LeftRightPerp.normalized) / 2;

            //the slope the roads going from up to down should have at the intersection, average of up down slope and slope perpindicular to left right slope
            //float targetSlopeUpDown = (slopeUpDown + slopeLeftRightR)/2;
            //float targetSlopeLeftRight = (slopeLeftRight + slopeUpDownR)/2;

            float targetSlopeUpDown = targetVecUpDown.y / targetVecUpDown.x;

            //	Debug.Log(targetSlopeUpDown);
            //adjust the slope if infinite
            if (targetSlopeUpDown == Mathf.Infinity)
            {
                targetSlopeUpDown = 100;                //float.MinValue;
            }
            //	Debug.Log(targetSlopeUpDown);

            //float targetSlopeUpDown = findSlope(conPointUp, conPointDown);
            float targetSlopeLeftRight = -1 / targetSlopeUpDown;            //perpindicular to vertical target slope

            targetSlopeRight = targetSlopeLeft = targetSlopeLeftRight;
            targetSlopeUp    = targetSlopeDown = targetSlopeUpDown;
            //Debug.Log(targetSlopeUp + " " + targetSlopeRight);
        }
        else if (conUp && conDown)       //if the top and bottom are connected but all four sides are not
        {
            targetSlopeUp = targetSlopeDown = GridMath.findSlope(conPointUp, conPointDown);

            if (conRight)
            {
                targetSlopeRight = GridMath.perp(targetSlopeUp);                //this street will come in aand connect perpindicular to the up and down street
            }
            else if (conLeft)
            {
                targetSlopeLeft = GridMath.perp(targetSlopeUp);
            }
        }
        else if (conRight && conLeft)       //if the left and right are connected but all four sides are not
        {
            targetSlopeRight = targetSlopeLeft = GridMath.findSlope(conPointRight, conPointLeft);

            if (conUp)
            {
                targetSlopeUp = GridMath.perp(targetSlopeRight);                //this street will come in aand connect perpindicular to the right and left street(has no influence on the slope)
            }
            else if (conDown)
            {
                targetSlopeDown = GridMath.perp(targetSlopeRight);
            }
        }
        else if (conUp && conRight)       //if the street connects up and right but nowhere else
        {
            targetSlopeUp = targetSlopeRight = GridMath.findSlope(conPointUp, conPointRight);
        }
        else if (conUp && conLeft)
        {
            targetSlopeUp = targetSlopeLeft = GridMath.findSlope(conPointUp, conPointLeft);
        }
        else if (conDown && conRight)
        {
            targetSlopeDown = targetSlopeRight = GridMath.findSlope(conPointDown, conPointRight);
        }
        else if (conDown && conLeft)       //if the street connects down and left but nowhere else
        {
            targetSlopeDown = targetSlopeLeft = GridMath.findSlope(conPointDown, conPointLeft);
        }
        else if (conUp)       //if it only connects to the top mid unit, make slope between the top bl point and this bl point
        {
            targetSlopeUp = GridMath.findSlope(conPointUp, mu.conPoint);
        }
        else if (conDown)
        {
            targetSlopeDown = GridMath.findSlope(conPointDown, mu.conPoint);
        }
        else if (conRight)
        {
            targetSlopeRight = GridMath.findSlope(conPointRight, mu.conPoint);
        }
        else if (conLeft)
        {
            targetSlopeLeft = GridMath.findSlope(conPointLeft, mu.conPoint);
        }

        //Debug.Log(targetSlopeUp + " " + targetSlopeRight + " " + targetSlopeLeft + " " + targetSlopeDown);

        /*MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, conPointUp.x, conPointUp.y),
         *                                      WorldManager.curPlanet.radius, 64*16));
         * MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, conPointDown.x, conPointDown.y),
         *                                      WorldManager.curPlanet.radius, 64*16));
         * MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, conPointRight.x, conPointRight.y),
         *                                      WorldManager.curPlanet.radius, 64*16));
         * MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, conPointLeft.x, conPointLeft.y),
         *                                      WorldManager.curPlanet.radius, 64*16));
         */

        /*if(targetSlopeUp==Mathf.Infinity || targetSlopeUp==float.NaN)
         *      targetSlopeUp=500;
         * if(targetSlopeDown==Mathf.Infinity || targetSlopeDown==float.NaN)
         *      targetSlopeDown=-500;
         * if(targetSlopeRight==float.NaN)
         *      targetSlopeRight=0;
         * if(targetSlopeLeft==float.NaN)
         *      targetSlopeLeft=0;*/

        //actually build the streets
        if (conUp)
        {
            buildStreetCurve(conPointUp, targetSlopeUp, Dir.UP, powIndexX, powIndexY, mu, upLev);
        }

        if (conDown)       //if the mid unit below connects up, make a street down
        {
            buildStreetCurve(conPointDown, targetSlopeDown, Dir.DOWN, powIndexX, powIndexY, mu, downLev);
        }

        if (conRight)
        {
            buildStreetCurve(conPointRight, targetSlopeRight, Dir.RIGHT, powIndexX, powIndexY, mu, rightLev);
        }

        if (conLeft)       //if the mid unit below connects up, make a street down
        {
            buildStreetCurve(conPointLeft, targetSlopeLeft, Dir.LEFT, powIndexX, powIndexY, mu, leftLev);
        }
    }
Esempio n. 8
0
    //builds one of four possible streets in a mid unit (up down left right);
    //outsideConPoint is the conPoint of the streetpoint that is outside of the mid unit (left, right up down), targetSlope is the slope the road tries to have at the mid street point
    //buildToSide is the side the street will be built and connected to
    //startIndex x and y is the index of the base unit that contains the mid unit conPoint and the roads will be build from
    public void buildStreetCurve(Vector2 outsideConPoint, float targetSlope, Dir buildToSide, int startIndexX, int startIndexY, TransportUnit mu, int lev)
    {
        //the direction to build the road, adjacent street point - cur street point normalized, might need to change distance for a higher sample rate
        Vector2 buildDir = (outsideConPoint - mu.conPoint).normalized * 0.8f;        //1 is the lenght of the build vector(distance between checked street points


        //Debug.Log(outsideConPoint+ " " +  targetSlope + " " +  buildToSide+ " " + startIndexX + " " + startIndexY + " " + mu.conPoint);
        //goal line slope
        float gps = GridMath.findSlope(mu.conPoint, outsideConPoint);
        float gpx, gpy;      //the x and y coordinate of the goal point
        int   gix, giy;      //the goal base unit that is touching the edge and the goal point

        //this block will find the goal point and goal base unit
        if (buildToSide == Dir.UP)                                                           //if a street is being build to connect to the up side
        {
            gpy = (mu.indexJ + 1) * midTUWidth;                                              //IF FACING UP!!! goal point y value , touches the top of the mid unit
            gpx = GridMath.findX(gpy, mu.conPoint, gps);                                     //finds x point from y point

            GridMath.findBaseIndexfromPoint(new Vector2(gpx, gpy - 0.5f), out gix, out giy); //finds index of base unit right below the goal point, 0.5 gets the point inside the base unit for sure

            TUBase goalBU = getBase(gix, giy);
            goalBU.conUp = true;            //this top unit will connect to the one above it in the topp mid unit
            goalBU.UpLev = lev;

            if (!goalBU.conSet)
            {
                goalBU.conPoint = new Vector2(gpx, gpy - 0.0001f);                //sets the bl point of this goal base unit in case it is not later set
            }
        }
        else if (buildToSide == Dir.DOWN)
        {
            gpy = (mu.indexJ) * midTUWidth;                                                  //IF FACING DOWN!!! goal point y value , touches the bottom of the mid unit
            gpx = GridMath.findX(gpy, mu.conPoint, gps);
            GridMath.findBaseIndexfromPoint(new Vector2(gpx, gpy + 0.5f), out gix, out giy); //finds index of base unit right above the goal point

            TUBase goalBU = getBase(gix, giy);
            if (!goalBU.conSet)
            {
                goalBU.conPoint = new Vector2(gpx, gpy + 0.0001f);                //sets the bl point of this goal base unit in case it is not later set
            }
        }
        else if (buildToSide == Dir.RIGHT)
        {
            gpx = (mu.indexI + 1) * midTUWidth;             //+1 includes the width of the current unit to find the right goal point
            gpy = GridMath.findY(gpx, mu.conPoint, gps);
            //need to change this probably!!
            //gpy = gps * (gpx - mu.conPoint.x) + mu.conPoint.y;//finds y point from x point using point slope

            //y-y1=m(x-x1)
            //y=m(x-x1)+y1

            GridMath.findBaseIndexfromPoint(new Vector2(gpx - 0.5f, gpy), out gix, out giy);             //finds index of base unit right to the left the goal point

            TUBase goalBU = getBase(gix, giy);
            goalBU.conRight = true;            //this right unit will connect to the one next to it in the right mid unit
            goalBU.RightLev = lev;

            if (!goalBU.conSet)
            {
                goalBU.conPoint = new Vector2(gpx - 0.0001f, gpy);                //sets the bl point of this goal base unit in case it is not later set
            }
        }
        else if (buildToSide == Dir.LEFT)
        {
            gpx = (mu.indexI) * midTUWidth;             //+1 includes the width of the current unit to find the right goal point

            //need to change this probably!!
            gpy = GridMath.findY(gpx, mu.conPoint, gps);            //finds y point from x point using point slope

            //y-y1=m(x-x1)
            //y=m(x-x1)+y1

            GridMath.findBaseIndexfromPoint(new Vector2(gpx + 0.5f, gpy), out gix, out giy);            //finds index of base unit right to the right the goal point

            TUBase goalBU = getBase(gix, giy);
            if (!goalBU.conSet)
            {
                goalBU.conPoint = new Vector2(gpx + 0.0001f, gpy);                //sets the bl point of this goal base unit in case it is not later set
            }
            //makeMarker(blToWorldUnits(baseList[gix, giy].streetPointBL));
        }
        else         //will never happen
        {
            gpx = 0;
            gpy = 0;
            gix = 0;
            giy = 0;
        }

        //Debug.Log(gpx + " " + gpy + " " + getBase(gix, giy).conPoint);
        //MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, gpx, gpy),
        //                                      WorldManager.curPlanet.radius, 64*16));
        //the point on the buildDir line that is on the edge of the mid unit, used to calculate interpolation percents
        Vector2 goalPoint = new Vector2(gpx, gpy);


        //total distance between the mid street point and point on the edge(goal point)
        float totalDist = Vector2.Distance(mu.conPoint, goalPoint);

        TUBase lastBaseUnit = getBase(startIndexX, startIndexY);        //the base unit used as a refernce for the loop after it

        //this loop builds the road out from the center(mid conPoint) to the edge(goal point)
        //20 or whatever is the limit
        for (int i = 1; i < 20; i++)
        {
            //a point in the buildDir Direction (x2,y2)
            Vector2 buildDirPoint = mu.conPoint + buildDir * i;

            //finds the point on the target slope line that forms a perpindicular line with the buildDirPoint so they can be interpolated between
            Vector2 pointOnTarget = GridMath.findPoint(mu.conPoint.x, mu.conPoint.y, targetSlope, buildDirPoint.x, buildDirPoint.y, -1 / targetSlope);            //targetSlopeUpDown

            //distance from the build dir point and goal point on edge (less than total dist)
            float curDist = Vector2.Distance(buildDirPoint, goalPoint);

            //the percentage curDist is of total dist used to interpolate between buildDirPoint and pointOnTarget to get the final point
            float percentDist = curDist / totalDist;

            //the distance between the buildDirPoint and the pointOnTarget
            Vector2 distTargets = buildDirPoint - pointOnTarget;

            //the partial distance that the final point is from buildDirPoint to pointOnTarget
            Vector2 partDistTargets = distTargets * percentDist;

            Vector2 finalPoint = buildDirPoint - partDistTargets;                  //final point is partDist from build dir point to pointOnTarget

            int fIndexX, fIndexY;                                                  //index of the base unit that contains the final point

            GridMath.findBaseIndexfromPoint(finalPoint, out fIndexX, out fIndexY); //finds index of base unit right below the goal point

            //Debug.Log(targetSlope + " " + finalPoint);
            //	MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, finalPoint.x, finalPoint.y),
            //                                      WorldManager.curPlanet.radius, 64*16));

            //if the final point is out of range in the current mid unit, stop the loop
            //also, this means that the goal base unit was never reached, so the last base unit needs to be connected to it
            if (fIndexX >= baseIndexI + midTUWidth || fIndexY >= baseIndexJ + midTUWidth || fIndexX < baseIndexI || fIndexY < baseIndexJ)
            {
                connectBases(getBase(gix, giy), lastBaseUnit, lev);                //connect the last used base unit to the goal base unit because they were not connected automaticaly
                break;
            }

            TUBase curBaseUnit = getBase(fIndexX, fIndexY);
            //the current base unit to modify


            if (!curBaseUnit.conSet)                          //if the bl point of the current base unit has not already been set, set it. can change this to use a property in the subase class(probably should)
            {
                curBaseUnit.conPoint = finalPoint;            //make bl point of the base that contains the final point the final point
                curBaseUnit.conSet   = true;                  //can no longer set the street point of this base unit

                connectBases(curBaseUnit, lastBaseUnit, lev); //connect the current base unit to the last one
                //lastBaseUnit = curBaseUnit;//set the new last base unit for reference in the next loop
            }

            if (fIndexX == gix && fIndexY == giy)           //end the loop if the current point is in the base unit on the edge
            {
                break;
            }

            lastBaseUnit = curBaseUnit;            //set the new last base unit for reference in the next loop
        }
    }
Esempio n. 9
0
    //will return a TransportUnit object from the requested a base unit
    //OH YES THIS USES RECURSION OH MAN!!!!!!!!!!!!!!!! I'M SO PROUD OF MYSELF!!!!!
    public TUBase getBase(SurfaceUnit su)
    {
        //quick test check, see getMid\
        if (su.u < -halfSideLength || su.u >= halfSideLength || su.v < -halfSideLength || su.v >= halfSideLength)
        {
            return(null);
        }


        //if(su.side==PSide.NONE)
        //	return null;


        //the base unit to be returned eventually
        TUBase bu = null;

        //if the base unit exists in the list, return it
        if (baseTUs.TryGetValue(su, out bu))
        {
            return(bu);
        }

        //if the base unit is not in the list, check if a mid unit is

        //the coordinates of the mid unit
        SurfaceUnit mus = getMidSU(su);

        //retrieve the actual mid unit (or not if it will never exist)
        TransportUnit mu = getMid(mus);

        //if the mid unit will never exist, the base unit will never exist
        if (mu == null)
        {
            return(null);
        }

        //if the mu has already been populated, the base unit will never exist
        if (mu.populated)
        {
            return(null);
        }
        else
        {
            //populate it

            /*int startu = mus.u*midTUWidth;
             * int startv = mus.v*midTUWidth;
             * for(int i = startu; i<startu+midTUWidth; i++)
             * {
             *      for(int j = startv; j<startv+midTUWidth; j++)
             *      {
             *              //create a new base unit, set its properties, and add it to the base list
             *              TUBase newTU = new TUBase();
             *              newTU.conUp = Random.value>0.5f;
             *              newTU.conRight = Random.value>0.5f;
             *              newTU.conPoint = new Vector2(i + Random.value, j + Random.value);
             *              newTU.conPointWorld = UnitConverter.getWP(new SurfacePos(su.side, newTU.conPoint.x, newTU.conPoint.y),
             *                                                     WorldManager.curPlanet.radius, sideLength);
             *
             *              baseTUs.Add(new SurfaceUnit(su.side, i, j), newTU);
             *      }
             * }*/
            Dictionary <SurfaceUnit, TUBase> bases = midfill.populate(mu, mus);
            foreach (KeyValuePair <SurfaceUnit, TUBase> pair in bases)
            {
                //Debug.Log(su.side+" " + pair.Key.u + " " + pair.Key.v);
                SurfaceUnit newKey = new SurfaceUnit(su.side, pair.Key.u, pair.Key.v);
                //if the key is not already in the list, add it
                if (!baseTUs.ContainsKey(newKey))
                {
                    //find the world connection point
                    Vector3 conPointWorld = UnitConverter.getWP(new SurfacePos(su.side, pair.Value.conPoint.x, pair.Value.conPoint.y),
                                                                UniverseSystem.curPlanet.radius, sideLength);
                    conPointWorld = planet.noise.altitudePos(conPointWorld);
                    //Debug.Log(su.side+" " + pair.Key.u + " " + pair.Key.v + " ");
                    pair.Value.conPointWorld = conPointWorld;
                    baseTUs.Add(newKey, pair.Value);
                }
                //Debug.Log(pair.Key);
            }

            mu.populated = true;

            //use recursion to return to the top of the function and get the base unit from the list(or not if it was not generated)
            return(getBase(su));
        }
    }
Esempio n. 10
0
    //builds a street from the center of a large unit to the edge in the given direction
    private void buildMidCurve(TULarge lu, Vector2 outsideConPoint, Dir dir, TransportUnit powMid, PSide side) //powmid is the mid unit that contains the large's conpoint
    {
        int gix = 0, giy = 0;                                                                                  //goal index x and y of the goal mid unit

        float outSlope = GridMath.findSlope(lu.conPoint, outsideConPoint);                                     //find the average slope from this conPoint to the outsideConPoint

        Debug.DrawLine(UnitConverter.getWP(new SurfacePos(PSide.TOP, lu.conPoint.x, lu.conPoint.y), 10000, sideLength),
                       UnitConverter.getWP(new SurfacePos(PSide.TOP, outsideConPoint.x, outsideConPoint.y), 10000, sideLength), Color.blue, Mathf.Infinity);
        //determine the goal mid unit
        if (dir == Dir.RIGHT)
        {
            //find the index of the goal mid unit
            float goalPosX = (lu.indexI + 1) * sideLengthLarge;               //the x value of the very right side of the large unit in mid units
            float goalPosY = GridMath.findY(goalPosX, lu.conPoint, outSlope); //the y value on the line between teh two conpoints
            GridMath.findMidIndexfromPoint(new Vector2(goalPosX - 0.5f, goalPosY), midTUWidth, out gix, out giy);

            TransportUnit goalMid = buildMid(gix, giy, side);
            goalMid.conRight = true;
            goalMid.RightLev = 1;
            //Debug.DrawLine(UnitConverter.getWP(new SurfacePos(PSide.TOP, lu.conPoint.x, lu.conPoint.y), 10000, 1024),
            //             UnitConverter.getWP(new SurfacePos(PSide.TOP, goalPosX, goalPosY), 10000, 1024), Color.blue, Mathf.Infinity);
        }
        else if (dir == Dir.LEFT)
        {
            //find the index of the goal mid unit
            float goalPosX = (lu.indexI) * sideLengthLarge;                   //the x value of the very right side of the large unit in mid units
            float goalPosY = GridMath.findY(goalPosX, lu.conPoint, outSlope); //the y value on the line between teh two conpoints
            GridMath.findMidIndexfromPoint(new Vector2(goalPosX + 0.5f, goalPosY), midTUWidth, out gix, out giy);
            //buildMid(gix, giy, side).conLeft=true;
        }
        else if (dir == Dir.UP)
        {
            //find the index of the goal mid unit
            float goalPosY = (lu.indexJ + 1) * sideLengthLarge;               //the x value of the very right side of the large unit in mid units
            float goalPosX = GridMath.findX(goalPosY, lu.conPoint, outSlope); //the y value on the line between teh two conpoints
            GridMath.findMidIndexfromPoint(new Vector2(goalPosX, goalPosY - 0.5f), midTUWidth, out gix, out giy);
            TransportUnit goalMid = buildMid(gix, giy, side);
            goalMid.conUp = true;
            goalMid.UpLev = 1;
        }
        else if (dir == Dir.DOWN)
        {
            //find the index of the goal mid unit
            float goalPosY = (lu.indexJ) * sideLengthLarge;                   //the x value of the very right side of the large unit in mid units
            float goalPosX = GridMath.findX(goalPosY, lu.conPoint, outSlope); //the y value on the line between teh two conpoints
            GridMath.findMidIndexfromPoint(new Vector2(goalPosX, goalPosY + 0.5f), midTUWidth, out gix, out giy);
        }
        //the x and y index of the mid unit last created in the loop(starts as if pows were last created)
        //int lastix = powIndexX, lastiy = powIndexY;
        TransportUnit lastMid = powMid;

        while (true)
        {
            //the difference in indexes between the current and goal mid units
            int xdif = gix - lastMid.indexI;
            int ydif = giy - lastMid.indexJ;

            //the direction to move in this loop iteration(1=x 2=y)
            int movedir;

            //if they are both not on the goal index, pick a random one to change
            if (xdif != 0 && ydif != 0)
            {
                movedir = lu.rng.NextDouble() > 0.5 ? 1:2;
            }
            else if (xdif != 0)
            {
                movedir = 1;
            }
            else if (ydif != 0)
            {
                movedir = 2;
            }
            else            //if they are both zero we are done maybe?
            {
                break;      //?
            }
            //the index of the mid unit to be created
            int curix = lastMid.indexI, curiy = lastMid.indexJ;

            //if moving in the x direction
            if (movedir == 1)
            {
                if (xdif > 0)
                {
                    curix++;
                }
                else
                {
                    curix--;
                }
            }
            else            //movedir==2
            {
                if (ydif > 0)
                {
                    curiy++;
                }
                else
                {
                    curiy--;
                }
            }

            //create or retrieve the new mid unit
            TransportUnit curMid = buildMid(curix, curiy, side);

            //set its conpoint if it has not already been set
            curMid.setConPoint(new Vector2((curix + (float)lu.rng.NextDouble()) * midTUWidth, (curiy + (float)lu.rng.NextDouble()) * midTUWidth));

            //MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, curMid.conPoint.x, curMid.conPoint.y), 10000, 1024), 5);

            connectUnits(curMid, lastMid, 1);

            lastMid = curMid;
        }
    }
Esempio n. 11
0
    //builds a level 2 street of length from startMid within curLarge transport unit
    private void buildLev2(TULarge curLarge, TransportUnit startMid, int length, PSide side)
    {
        //the max and min indexes that the street can be built to(stay within the large unit)
        //move out of this function so it is not recalculated every time
        int maxI = (curLarge.indexI + 1) * largeTUWidth - 1;
        int minI = (curLarge.indexI) * largeTUWidth;
        int maxJ = (curLarge.indexJ + 1) * largeTUWidth - 1;
        int minJ = (curLarge.indexJ) * largeTUWidth;

        //the last mid unit to build away from
        TransportUnit lastMid = startMid;
        Dir           lastDir;

        for (int i = 0; i < length; i++)
        {
            //the direction to build
            //NOTE: modify later to not build back from the same direction
            Dir dir = (Dir)(curLarge.rng.Next(1, 5));

            //the index of the new mid unit to modify
            int curix = lastMid.indexI;
            int curiy = lastMid.indexJ;

            if (dir == Dir.RIGHT)
            {
                curix++;
                if (curix > maxI)
                {
                    lastMid.conRight = true;
                    lastMid.RightLev = 2;
                    break;
                }
            }
            if (dir == Dir.LEFT)
            {
                curix--;
                if (curix < minI)
                {
                    break;
                }
            }
            if (dir == Dir.UP)
            {
                curiy++;
                if (curiy > maxJ)
                {
                    lastMid.conUp = true;
                    lastMid.UpLev = 2;
                    break;
                }
            }
            if (dir == Dir.DOWN)
            {
                curiy--;
                if (curiy < minJ)
                {
                    break;
                }
            }

            //build (or just retrieve) the mid unit at the new indexes
            TransportUnit curMid = buildMid(curix, curiy, side);

            //set its conpoint if it has not already been set
            curMid.setConPoint(new Vector2((curix + (float)curLarge.rng.NextDouble()) * midTUWidth, (curiy + (float)curLarge.rng.NextDouble()) * midTUWidth));

            //MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, curMid.conPoint.x, curMid.conPoint.y), 10000, 1024), 10);

            //connect on level 2
            connectUnits(curMid, lastMid, 2);

            lastMid = curMid;
        }
    }
Esempio n. 12
0
    //populates a large unit with mid units
    private void populateLarge(TULarge lu, SurfaceUnit lus)
    {
        //Debug.Log("built " + lus);

        //clear the index list
        indexList.Clear();

        //find the mid unit that the large unit's conpoint falls in
        int powIndexX, powIndexY;

        GridMath.findMidIndexfromPoint(lu.conPoint, midTUWidth, out powIndexX, out powIndexY);
        TransportUnit powMid = buildMid(powIndexX, powIndexY, lus.side);

        powMid.conPoint = lu.conPoint;        //same conpoint, or could change to not be, doesn't really  matter
        powMid.conSet   = true;

        TULarge rightLU = getLarge(new SurfaceUnit(lus.side, lus.u + 1, lus.v));
        TULarge leftLU  = getLarge(new SurfaceUnit(lus.side, lus.u - 1, lus.v));
        TULarge upLU    = getLarge(new SurfaceUnit(lus.side, lus.u, lus.v + 1));
        TULarge downLU  = getLarge(new SurfaceUnit(lus.side, lus.u, lus.v - 1));
        //Vector2 conPointRight = rightLU.conPoint;

        //determine in which direction the streets should be built
        bool conRight = lu.conRight;
        bool conLeft  = false;
        bool conUp    = lu.conUp;
        bool conDown  = false;

        //check for null large units
        if (leftLU != null)
        {
            conLeft = leftLU.conRight;
        }
        if (downLU != null)
        {
            conDown = downLU.conUp;
        }

        //list of the index of all the mid transport units that are connected in some way
        if (conRight)
        {
            buildMidCurve(lu, rightLU.conPoint, Dir.RIGHT, powMid, lus.side);
        }
        if (conLeft)
        {
            buildMidCurve(lu, leftLU.conPoint, Dir.LEFT, powMid, lus.side);
        }
        if (conUp)
        {
            buildMidCurve(lu, upLU.conPoint, Dir.UP, powMid, lus.side);
        }
        if (conDown)
        {
            buildMidCurve(lu, downLU.conPoint, Dir.DOWN, powMid, lus.side);
        }

        //build some level 2 streets coming off the level 1 streets or other level 2 streets
        int numStreets = 80;        //(int)(Random.value*5);

        for (int i = 0; i < numStreets; i++)
        {
            int           startNum = lu.rng.Next(0, indexList.Count);
            TransportUnit startMid = indexList[startNum];
            buildLev2(lu, startMid, lu.rng.Next(2, 80), lus.side);
        }


        Vector3 topright = UnitConverter.getWP(new SurfacePos(lus.side, (lu.indexI + 1) * sideLengthLarge, (lu.indexJ + 1) * sideLengthLarge), 10000, sideLength);
        Vector3 topleft  = UnitConverter.getWP(new SurfacePos(lus.side, (lu.indexI) * sideLengthLarge, (lu.indexJ + 1) * sideLengthLarge), 10000, sideLength);
        Vector3 botright = UnitConverter.getWP(new SurfacePos(lus.side, (lu.indexI + 1) * sideLengthLarge, (lu.indexJ) * sideLengthLarge), 10000, sideLength);
        Vector3 botleft  = UnitConverter.getWP(new SurfacePos(lus.side, (lu.indexI) * sideLengthLarge, (lu.indexJ) * sideLengthLarge), 10000, sideLength);

        Debug.DrawLine(topleft, botleft, Color.red, Mathf.Infinity);
        Debug.DrawLine(topleft, topright, Color.red, Mathf.Infinity);
        Debug.DrawLine(topright, botright, Color.red, Mathf.Infinity);
        Debug.DrawLine(botright, botleft, Color.red, Mathf.Infinity);


        lu.populated = true;
    }