Example #1
0
 public override bool Equals(object obj)
 {
     if (!(obj is IntPoint2D))
     {
         return(false);
     }
     else
     {
         IntPoint2D rhs = (IntPoint2D)obj;
         if (this.xCoord == rhs.xCoord)
         {
             if (this.yCoord == rhs.yCoord)
             {
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
         else
         {
             return(false);
         }
     }
 }
Example #2
0
    public Stack <IntPoint2D> GetRoadPath(IntPoint2D startLoc, IntPoint2D endLoc)
    {
        List <IntPoint2D> startList = GetAdjacentRoadTiles(startLoc);
        List <IntPoint2D> endList   = GetAdjacentRoadTiles(endLoc);

        return(ShortestPath(startList, endList, true));
    }
    public GameObject GetActualTile(IntPoint2D tileLoc)
    {
        GameObject tile = this.CreateRoadTile(actualRoadPrefab, tileLoc);

        tile.SetActive(true);
        return(tile);
    }
    public GameObject GetRedGhost(IntPoint2D tileLoc)
    {
        GameObject tile = this.CreateRoadTile(redPrefab, tileLoc);

        tile.SetActive(true);
        return(tile);
    }
Example #5
0
    public GameObject GetActualHouse(IntPoint2D tileLoc)
    {
        GameObject house = this.CreateHouse(actualHousePrefab, tileLoc);

        house.SetActive(true);
        return(house);
    }
Example #6
0
 public void DistributeWater(IntPoint2D tileIndex)
 {
     if (IsValidTile(tileIndex))
     {
         tileArray [tileIndex.xCoord, tileIndex.yCoord].DistributeWater();
     }
 }
        public void CalculatesPathEfficiently()
        {
            bool[,] map = new bool[5, 5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    map[i, j] = true;
                }
            }

            IntPoint2D from = new IntPoint2D()
            {
                X = 0, Y = 0
            };
            IntPoint2D to = new IntPoint2D()
            {
                X = 4, Y = 4
            };

            int visitedNodesCount;
            var pathLength = AStarPathFinder.AStarPathFinder.GetBestPathLength(from, to, map, out visitedNodesCount);

            Assert.IsTrue(pathLength > 5 && pathLength < 6, "Wrong Path Calculated");
            Assert.IsTrue(visitedNodesCount >= 4 && visitedNodesCount <= 8, "Path Calculated Inefficiently");
        }
Example #8
0
    public GameObject GetWellWalker(IntPoint2D tileLoc, ScenarioMgr.Direction facing)
    {
        //Debug.Log ("Retrieving well walker");
        Vector3    topLeft = scenario.ComputeTopLeftPointOfTile(tileLoc);
        GameObject walker;

        walker = (GameObject)Instantiate(wellWalkerPrefab);
        walker.transform.position = topLeft + new Vector3(walkerHorizOffset, walkerVertOffset, walkerHorizOffset);
        walker.transform.rotation = Quaternion.identity;
        if (facing == ScenarioMgr.Direction.Up)
        {
            walker.transform.Rotate(0, 90, 0);
        }
        else if (facing == ScenarioMgr.Direction.Left)
        {
            walker.transform.Rotate(0, 180, 0);
        }
        else if (facing == ScenarioMgr.Direction.Down)
        {
            walker.transform.Rotate(0, 270, 0);
        }

        walker.SetActive(true);
        return(walker);
    }
Example #9
0
    void DragRoad(Vector3 mouseLoc)
    {
        IntPoint2D curTile = this.ConvertPointToTileIndex(mouseLoc);

        FixFirstDirection(curTile);
        if (firstDir == Direction.Undefined)
        {
            IntPoint2D[] keys = new IntPoint2D[tempObjects.Count];
            tempObjects.Keys.CopyTo(keys, 0);
            // clean up all but first tile
            foreach (IntPoint2D curKey in keys)
            {
                if (curKey != curTile)
                {
                    Destroy(tempObjects [curKey]);
                    tempObjects.Remove(curKey);
                }
            }
        }
        else
        {
            int xLeft, xRight, yUp, yDown, horizY, vertX;
            DetermineEndPoints(curTile, out xLeft, out xRight, out yUp, out yDown, out horizY, out vertX);
            // deal with the interesting problem
            AdjustRoadTiles(xLeft, xRight, yUp, yDown, horizY, vertX);
        }
    }
Example #10
0
    public Stack <IntPoint2D> ComputePath(IntPoint2D start, IntPoint2D end, bool openTravel)
    {
        //Debug.Log ("Computing path from "+start.ToString()+ " to " + end.ToString());
        HashSet <IntPoint2D> closed = new HashSet <IntPoint2D> ();

        closed.Add(start);
        int queueSize = (this.gridHeight * this.gridWidth) * 16;
        HeapPriorityQueue <SearchNode> searchList = new HeapPriorityQueue <SearchNode> (queueSize);
        Stack <IntPoint2D>             path       = new Stack <IntPoint2D> ();
        SearchNode root = new SearchNode(null, start, 0, Math.Abs(start.xCoord - end.xCoord) + Math.Abs(start.yCoord - end.yCoord));

        searchList.Enqueue(root, root.Priority);
        SearchNode        curNode = null;
        bool              found   = false;
        List <SearchNode> children;

        while (!found && searchList.Count > 0)
        {
            curNode = searchList.Dequeue();
            //Debug.Log("checking node: "+curNode.data.ToString());
            if (curNode.AtGoal(end))
            {
                found = true;
            }
            else
            {
                children = curNode.GetChildren(end, this, openTravel);
                foreach (SearchNode child in children)
                {
                    if (searchList.Count == queueSize)
                    {
                        Debug.Log("Priority queue size: " + queueSize.ToString() + " exceeded");
                    }
                    //Debug.Log("enqueueing node: "+child.data.ToString());
                    //Debug.Log ("  Priority: "+child.Priority.ToString());
                    if (!closed.Contains(child.data))
                    {
                        searchList.Enqueue(child, child.cost + child.heuristic);
                        closed.Add(child.data);
                    }
                }
            }
        }
        if (found)
        {
            //Debug.Log ("pushing path");
            while (curNode != null)
            {
                path.Push(curNode.data);
//				Debug.Log(curNode.data.ToString());
                curNode = curNode.parent;
            }
            path.Pop();
        }
        else
        {
            //Debug.Log ("no path found");
        }
        return(path);
    }
Example #11
0
    // private int searchLimit = 100;

    public void SetUp(ScenarioData scenario, IntPoint2D startTile, ScenarioMgr.Direction startingDir, bool wandering,
                      IntPoint2D dest, HouseManager house, bool movingIn)
    {
        Debug.Log("in citizen set up");
        scenarioInfo   = scenario;
        facing         = startingDir;
        curTile        = startTile;
        this.wandering = wandering;
        this.destTile  = dest;
        Debug.Log("start tile: " + startTile.ToString());
        Debug.Log("dest tile: " + destTile.ToString());
        this.myHouse  = house;
        this.movingIn = movingIn;
        havePath      = false;
        startPath     = false;
        if (!wandering)
        {
            Debug.Log("Starting TakePath Coroutine from SetUp");
            StartCoroutine(TakePath());
        }
        else
        {
            StartCoroutine(Wander());
        }
    }
Example #12
0
    void SetUpHouseGhost()
    {
        Vector3    mouseLoc;
        IntPoint2D tileLoc       = IntPoint2D.zero;
        bool       disableObject = false;

        if (this.MouseLocationOnGround(out mouseLoc))
        {
            tileLoc = this.ConvertPointToTileIndex(mouseLoc);
            if (this.scenarioInfo.CanBuildHere(tileLoc))
            {
                curObject             = housePool.GetGreenGhost(tileLoc);
                this.curObjectIsGreen = true;
            }
            else
            {
                curObject             = housePool.GetRedGhost(tileLoc);
                this.curObjectIsGreen = false;
            }
        }
        else
        {
            // making a disabled green one
            curObject             = housePool.GetGreenGhost(tileLoc);
            this.curObjectIsGreen = true;
            disableObject         = true;
        }
        // need to make it
        curObject.SetActive(disableObject);
        this.curObjectSizeInTiles = 1;
    }
Example #13
0
    public StateType[] GetNeighboursStates(IntPoint2D coordinates)
    {
        StateType[] states = new StateType[8];
        sbyte       index  = 0;

        foreach (var i in signs)
        {
            foreach (var j in signs)
            {
                if (i == 0 && j == 0)
                {
                    continue;
                }
                else
                {
                    var newCoords = AdjustCoordinates(new IntPoint2D()
                    {
                        x = coordinates.x + i, y = coordinates.y + j
                    });
                    states[index] = GetState(newCoords);
                    ++index;
                }
            }
        }
        return(states);
    }
        public void CanCalculateComplexPath()
        {
            bool[,] map = new bool[5, 5];
            map[0, 0]   = true;
            map[0, 1]   = true;
            map[0, 2]   = true;
            map[0, 3]   = true;


            map[1, 2] = true;

            map[2, 0] = true;
            map[2, 1] = true;
            map[2, 2] = true;
            map[2, 3] = true;

            map[3, 0] = true;
            map[3, 1] = true;
            map[3, 2] = true;
            map[3, 3] = true;
            IntPoint2D from = new IntPoint2D()
            {
                X = 0, Y = 0
            };
            IntPoint2D to = new IntPoint2D()
            {
                X = 3, Y = 0
            };

            var pathLength = AStarPathFinder.AStarPathFinder.GetBestPathLength(from, to, map);

            Assert.IsTrue(pathLength > 5 && pathLength < 6);
        }
Example #15
0
    void CompleteDelete(bool goodMouseLoc, Vector3 mouseLoc)
    {
        bool actuallyDeleting = false;

        if (goodMouseLoc)
        {
            // we're on the ground, so figure out which tile we're on
            IntPoint2D tile = ConvertPointToTileIndex(mouseLoc);
            // and see if we have an object there
            GameObject delEndObj = scenarioInfo.GetBuilding(tile);
            if (delEndObj == curObject)
            {             // if the two objects are the same
                if (scenarioInfo.IsHouse(tile))
                {
                    popMgr.RemoveHouse(tile);
                }
                actuallyDeleting = true;
                scenarioInfo.DeleteBuilding(tile);
                Destroy(delEndObj);
            }
        }
        if (!actuallyDeleting)
        {
            // fix the mat back
            curObject.GetComponent <MeshRenderer> ().material = savedMat;
        }
        curObject = null;
    }
Example #16
0
    public GameObject GetRedGhost(IntPoint2D tileLoc)
    {
        GameObject house = this.CreateHouse(redPrefab, tileLoc);

        house.SetActive(true);
        return(house);
    }
Example #17
0
 public void MakeOther(GameObject building, int size, IntPoint2D topLeft)
 {
     mobility             = Moveability.Impassable;
     clear                = false;
     this.building        = building;
     buildingSize         = size;
     this.buildingTopLeft = topLeft;
 }
Example #18
0
 public void MakeRoad(GameObject tile, IntPoint2D topLeft)
 {
     mobility             = Moveability.Road;
     clear                = false;
     building             = tile;
     buildingSize         = 1;
     this.buildingTopLeft = topLeft;
 }
Example #19
0
    public GameObject GetActualObject(int arrayIndex, IntPoint2D tileLoc)
    {
        Vector3    topLeft = scenario.ComputeTopLeftPointOfTile(tileLoc);
        GameObject obj     = (GameObject)Instantiate(actualPrefabs[arrayIndex]);

        obj.transform.position = topLeft + GetObjTransform(arrayIndex);
        return(obj);
    }
        private static IntPoint2D VectorToIntPoint(Vector3 vector)
        {
            var point = new IntPoint2D();

            point.X = (int)vector.X;
            point.Y = (int)vector.Y;
            return(point);
        }
Example #21
0
 /// <summary>
 /// コンストラクター
 /// </summary>
 /// <param name="a">A</param>
 /// <param name="b">B</param>
 public IntRectangle(IntPoint2D a, IntPoint2D b)
 {
     Initialize();
     A.X = a.X;
     A.Y = a.Y;
     B.X = b.X;
     B.Y = b.Y;
 }
Example #22
0
 public SearchNode(SearchNode prev, IntPoint2D data, int cost, int heuristic)
 {
     parent         = prev;
     this.data      = data;
     this.cost      = cost;
     this.heuristic = heuristic;
     this.Priority  = cost + heuristic;
 }
Example #23
0
 private void MakeRoadTile(IntPoint2D tileLoc)
 {
     if (scenarioInfo.CanBuildHere(tileLoc))
     {
         GameObject tile = roadPool.GetActualTile(tileLoc);
         scenarioInfo.MakeRoad(tileLoc, tile);
     }
 }
Example #24
0
 public void MakeHouse(GameObject house, IntPoint2D topLeft)
 {
     mobility             = Moveability.Impassable;
     clear                = false;
     building             = house;
     isHouse              = true;
     buildingSize         = 1;
     this.buildingTopLeft = topLeft;
 }
Example #25
0
    public Vector3 ComputeTopLeftPointOfTile(IntPoint2D tileIndex)
    {
        //Debug.Log ("In ComputeTopLeftPointOfTile");
        Vector3 point = new Vector3(tileIndex.xCoord - gridWidth / 2, 0, tileIndex.yCoord - gridHeight / 2);

        //Debug.Log(tileIndex);
        //Debug.Log (point);
        return(point);
    }
Example #26
0
 public void SetUp(GameObject ground, ScenarioData scenario, IntPoint2D tileLoc)
 {
     scenarioInfo = scenario;
     centerTile   = tileLoc;
     popMgr       = ground.GetComponent("PopulationManager") as PopulationManager;
     numWorkers   = 0;
     amountStored = 0;
     StartCoroutine("GetWorkers");
 }
Example #27
0
    public List <SearchNode> GetChildren(IntPoint2D dest, ScenarioData scenario, bool openTravel)
    {
        List <SearchNode> children = new List <SearchNode>();
        int        childCost       = this.cost + 1;
        int        childHeuristic  = 0;
        IntPoint2D childData       = new IntPoint2D(data.xCoord - 1, data.yCoord);

        if (scenario.IsValidTile(childData) && !InAncestorNode(childData) &&
            (scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }
        childData = new IntPoint2D(data.xCoord + 1, data.yCoord);
        if (scenario.IsValidTile(childData) && !InAncestorNode(childData) &&
            (scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }
        childData = new IntPoint2D(data.xCoord, data.yCoord - 1);
        if (scenario.IsValidTile(childData) && !InAncestorNode(childData) &&
            (scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }
        childData = new IntPoint2D(data.xCoord, data.yCoord + 1);
        if (scenario.IsValidTile(childData) && !InAncestorNode(childData) &&
            (scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }
        return(children);
    }
Example #28
0
    public bool MakeRoad(IntPoint2D tileIndex, GameObject tile)
    {
        bool canMake = IsValidTile(tileIndex) && tileArray [tileIndex.xCoord, tileIndex.yCoord].IsClear();

        if (canMake)
        {
            tileArray [tileIndex.xCoord, tileIndex.yCoord].MakeRoad(tile, tileIndex);
        }
        return(canMake);
    }
Example #29
0
    public bool MakeHouse(IntPoint2D houseTile, GameObject house)
    {
        bool canMake = IsValidTile(houseTile) && tileArray [houseTile.xCoord, houseTile.yCoord].IsClear();

        if (canMake)
        {
            tileArray [houseTile.xCoord, houseTile.yCoord].MakeHouse(house, houseTile);
        }
        return(canMake);
    }
Example #30
0
    private IntPoint2D AdjustCoordinates(IntPoint2D coords)
    {
        int x = ((coords.x % width) + width) % width;
        int y = ((coords.y % height) + height) % height;

        return(new IntPoint2D()
        {
            x = x, y = y
        });
    }
Example #31
0
        public void Expand(IntPoint2D point)
        {
            if (point.X < _min.X)
                _min.X = point.X;
            if (point.Y < _min.Y)
                _min.Y = point.Y;

            if (point.X > _max.X)
                _max.X = point.X;
            if (point.Y > _max.Y)
                _max.Y = point.Y;
        }
Example #32
0
 public Box2D(IntPoint2D min, IntPoint2D max)
 {
     _min = min;
     _max = max;
 }
Example #33
0
        public static PerspectiveCamera CreateFromBounds(AxisAlignedBox3D bounds, Viewport3D viewport,
			float fieldOfView, float yaw = 0.0f, float pitch = 0.0f, float zoom = 1.0f)
        {
            // Calculate initial guess at camera settings.
            Matrix3D transform = Matrix3D.CreateFromYawPitchRoll(yaw, pitch, 0);
            Vector3D cameraDirection = Vector3D.Normalize(transform.Transform(Vector3D.Forward));
            PerspectiveCamera initialGuess = new PerspectiveCamera
            {
                FieldOfView = fieldOfView,
                NearPlaneDistance = 1.0f,
                FarPlaneDistance = bounds.Size.Length() * 10,
                Position = bounds.Center - cameraDirection * bounds.Size.Length() * 2,
                LookDirection = cameraDirection,
                UpDirection = Vector3D.Up
            };

            Matrix3D projection = initialGuess.GetProjectionMatrix(viewport.AspectRatio);
            Matrix3D view = initialGuess.GetViewMatrix();

            // Project bounding box corners onto screen, and calculate screen bounds.
            float closestZ = float.MaxValue;
            Box2D? screenBounds = null;
            Point3D[] corners = bounds.GetCorners();
            foreach (Point3D corner in corners)
            {
                Point3D screenPoint = viewport.Project(corner,
                    projection, view, Matrix3D.Identity);

                if (screenPoint.Z < closestZ)
                    closestZ = screenPoint.Z;

                IntPoint2D intScreenPoint = new IntPoint2D((int) screenPoint.X, (int) screenPoint.Y);
                if (screenBounds == null)
                    screenBounds = new Box2D(intScreenPoint, intScreenPoint);
                else
                {
                    Box2D value = screenBounds.Value;
                    value.Expand(intScreenPoint);
                    screenBounds = value;
                }
            }

            // Now project back from screen bounds into scene, setting Z to the minimum bounding box Z value.
            IntPoint2D minScreen = screenBounds.Value.Min;
            IntPoint2D maxScreen = screenBounds.Value.Max;
            Point3D min = viewport.Unproject(new Point3D(minScreen.X, minScreen.Y, closestZ),
                projection, view, Matrix3D.Identity);
            Point3D max = viewport.Unproject(new Point3D(maxScreen.X, maxScreen.Y, closestZ),
                projection, view, Matrix3D.Identity);

            // Use these new values to calculate the distance the camera should be from the AABB centre.
            Vector3D size = Vector3D.Abs(max - min);
            float radius = size.Length();
            float dist = radius / (2 * MathUtility.Tan(fieldOfView * viewport.AspectRatio / 2));

            Point3D closestBoundsCenter = (min + (max - min) / 2);
            Point3D position = closestBoundsCenter - cameraDirection * dist * (1 / zoom);

            return new PerspectiveCamera
            {
                FieldOfView = fieldOfView,
                NearPlaneDistance = 1.0f,
                FarPlaneDistance = dist * 10,
                Position = position,
                LookDirection = cameraDirection,
                UpDirection = Vector3D.Up
            };
        }