private static int BeginWorldPathfinding(IntPtr L)
    {
        int result;

        try
        {
            ToLua.CheckArgsCount(L, 8);
            WorldPathfinding worldPathfinding = (WorldPathfinding)ToLua.CheckObject(L, 1, typeof(WorldPathfinding));
            int               toMap           = (int)LuaDLL.luaL_checknumber(L, 2);
            int               x        = (int)LuaDLL.luaL_checknumber(L, 3);
            float             y        = (float)LuaDLL.luaL_checknumber(L, 4);
            int               z        = (int)LuaDLL.luaL_checknumber(L, 5);
            float             ra       = (float)LuaDLL.luaL_checknumber(L, 6);
            LuaTypes          luaTypes = LuaDLL.lua_type(L, 7);
            Move.PathFinished callback;
            if (luaTypes != LuaTypes.LUA_TFUNCTION)
            {
                callback = (Move.PathFinished)ToLua.CheckObject(L, 7, typeof(Move.PathFinished));
            }
            else
            {
                LuaFunction func = ToLua.ToLuaFunction(L, 7);
                callback = (DelegateFactory.CreateDelegate(typeof(Move.PathFinished), func) as Move.PathFinished);
            }
            bool midPoint = LuaDLL.luaL_checkboolean(L, 8);
            worldPathfinding.BeginWorldPathfinding(toMap, x, y, z, ra, callback, midPoint);
            result = 0;
        }
        catch (Exception e)
        {
            result = LuaDLL.toluaL_exception(L, e, null);
        }
        return(result);
    }
    private static int DoCallBack(IntPtr L)
    {
        int result;

        try
        {
            ToLua.CheckArgsCount(L, 2);
            WorldPathfinding  worldPathfinding = (WorldPathfinding)ToLua.CheckObject(L, 1, typeof(WorldPathfinding));
            LuaTypes          luaTypes         = LuaDLL.lua_type(L, 2);
            Move.PathFinished callback;
            if (luaTypes != LuaTypes.LUA_TFUNCTION)
            {
                callback = (Move.PathFinished)ToLua.CheckObject(L, 2, typeof(Move.PathFinished));
            }
            else
            {
                LuaFunction func = ToLua.ToLuaFunction(L, 2);
                callback = (DelegateFactory.CreateDelegate(typeof(Move.PathFinished), func) as Move.PathFinished);
            }
            worldPathfinding.DoCallBack(callback);
            result = 0;
        }
        catch (Exception e)
        {
            result = LuaDLL.toluaL_exception(L, e, null);
        }
        return(result);
    }
Esempio n. 3
0
        public override void Act(WorldMap w)
        {
            var path = WorldPathfinding.Pathfind(w, u.h, h);

            if (path == null)
            {
                return;
            }

            if (path.Count < 2)
            {
                return;
            }
            var next = path.First.Next.Value;


            if (next.unit != null && next.unit != u)
            {
                //am = h.w.gm.audiom;
                //am.audioS.PlayOneShot(heroAttackSound);
                next.unit.ApplyDamage(1, w.hero);
            }

            if (next.unit == null)
            {
                u.h = next;
            }
        }
Esempio n. 4
0
        public override void TurnActions()
        {
            List <Hex> neighbs = h.Neighbors();

            if (!persuing && w.hero.h.loc.Distance(h.loc) < 6)
            {
                var hero    = w.hero;
                Hex heroHex = hero.h;
                var path    = WorldPathfinding.Pathfind(w, h, heroHex);

                if (path.Count <= 5)
                {
                    setPersuing();
                }
            }

            if (persuing)
            {
                if (spawnTimer == 0)
                {
                    if (spawnMeleeEnemy())
                    {
                        spawnTimer = spawnCD;
                        Updated    = true;
                        return;
                    }
                }

                List <Hex> openNeighbs = new List <Hex>();
                var        dist        = h.loc.Distance(target.h.loc);
                foreach (Hex neighb in neighbs)
                {
                    if (neighb.Passable() && neighb.loc.Distance(target.h.loc) > dist)
                    {
                        if (neighb.unit == null)
                        {
                            openNeighbs.Add(neighb);
                        }
                    }
                }

                if (openNeighbs.Count > 0)
                {
                    System.Random rng = new System.Random();
                    int           i   = rng.Next(openNeighbs.Count);
                    h       = openNeighbs[i];
                    Updated = true;
                }
            }
        }
    private static int GetInstance(IntPtr L)
    {
        int result;

        try
        {
            ToLua.CheckArgsCount(L, 0);
            WorldPathfinding instance = WorldPathfinding.GetInstance();
            ToLua.Push(L, instance);
            result = 1;
        }
        catch (Exception e)
        {
            result = LuaDLL.toluaL_exception(L, e, null);
        }
        return(result);
    }
    private static int Clear(IntPtr L)
    {
        int result;

        try
        {
            ToLua.CheckArgsCount(L, 1);
            WorldPathfinding worldPathfinding = (WorldPathfinding)ToLua.CheckObject(L, 1, typeof(WorldPathfinding));
            worldPathfinding.Clear();
            result = 0;
        }
        catch (Exception e)
        {
            result = LuaDLL.toluaL_exception(L, e, null);
        }
        return(result);
    }
    private static int IsInWorldPathfinding(IntPtr L)
    {
        int result;

        try
        {
            ToLua.CheckArgsCount(L, 1);
            WorldPathfinding worldPathfinding = (WorldPathfinding)ToLua.CheckObject(L, 1, typeof(WorldPathfinding));
            bool             value            = worldPathfinding.IsInWorldPathfinding();
            LuaDLL.lua_pushboolean(L, value);
            result = 1;
        }
        catch (Exception e)
        {
            result = LuaDLL.toluaL_exception(L, e, null);
        }
        return(result);
    }
Esempio n. 8
0
    public static ImprovementObjectCity FindNearest(TileObject tile, Nation nation)
    {
        var cities   = nation.Cities;
        int lastCost = int.MaxValue;
        int index    = 0;

        for (int i = 0; i < cities.Count; i++)
        {
            var newPath = WorldPathfinding.FindPath(tile, cities[i].Tile);
            if (newPath.TotalCost < lastCost)
            {
                index    = i;
                lastCost = newPath.TotalCost;
            }
        }

        return(cities[index]);
    }
    private static int set_init(IntPtr L)
    {
        object obj = null;
        int    result;

        try
        {
            obj = ToLua.ToObject(L, 1);
            WorldPathfinding worldPathfinding = (WorldPathfinding)obj;
            bool             init             = LuaDLL.luaL_checkboolean(L, 2);
            worldPathfinding.init = init;
            result = 0;
        }
        catch (Exception ex)
        {
            result = LuaDLL.toluaL_exception(L, ex, (obj != null) ? ex.Message : "attempt to index init on a nil value");
        }
        return(result);
    }
    private static int get_targetInfo(IntPtr L)
    {
        object obj = null;
        int    result;

        try
        {
            obj = ToLua.ToObject(L, 1);
            WorldPathfinding            worldPathfinding = (WorldPathfinding)obj;
            WorldPathfinding.TargetInfo targetInfo       = worldPathfinding.targetInfo;
            ToLua.PushObject(L, targetInfo);
            result = 1;
        }
        catch (Exception ex)
        {
            result = LuaDLL.toluaL_exception(L, ex, (obj != null) ? ex.Message : "attempt to index targetInfo on a nil value");
        }
        return(result);
    }
    private static int FindPath(IntPtr L)
    {
        int result;

        try
        {
            ToLua.CheckArgsCount(L, 3);
            WorldPathfinding worldPathfinding = (WorldPathfinding)ToLua.CheckObject(L, 1, typeof(WorldPathfinding));
            int startMap = (int)LuaDLL.luaL_checknumber(L, 2);
            int endMap   = (int)LuaDLL.luaL_checknumber(L, 3);
            List <GraphNode <WorldPathfinding.MapPathInfo> > o = worldPathfinding.FindPath(startMap, endMap);
            ToLua.PushObject(L, o);
            result = 1;
        }
        catch (Exception e)
        {
            result = LuaDLL.toluaL_exception(L, e, null);
        }
        return(result);
    }
Esempio n. 12
0
        public override void TurnActions()
        {
            if (!persuing && w.hero.h.loc.Distance(h.loc) < 5)
            {
                var hero    = w.hero;
                Hex heroHex = hero.h;
                var path    = WorldPathfinding.Pathfind(w, h, heroHex);

                if (path.Count <= 4)
                {
                    setPersuing();
                }
                else
                {
                    Updated = true;
                }
            }

            if (persuing)
            {
                var dist = h.loc.Distance(target.h.loc);
                if (dist == 1)
                {
                    AudioManager.effects.PlayOneShot(AudioManager.meleeSound);
                    target.ApplyDamage(1, this);
                    attacking   = true;
                    attackStart = timer;
                    Updated     = true;
                }
                else
                {
                    var path = WorldPathfinding.Pathfind(w, h, target.h);
                    var next = path.First.Next.Value;
                    if (next.unit == null)
                    {
                        h       = next;
                        Updated = true;
                    }
                }
            }
        }
    private static int AddWorldPathfindingInfo(IntPtr L)
    {
        int result;

        try
        {
            ToLua.CheckArgsCount(L, 6);
            WorldPathfinding worldPathfinding = (WorldPathfinding)ToLua.CheckObject(L, 1, typeof(WorldPathfinding));
            int   startMap = (int)LuaDLL.luaL_checknumber(L, 2);
            int   endMap   = (int)LuaDLL.luaL_checknumber(L, 3);
            int   x        = (int)LuaDLL.luaL_checknumber(L, 4);
            int   z        = (int)LuaDLL.luaL_checknumber(L, 5);
            float y        = (float)LuaDLL.luaL_checknumber(L, 6);
            worldPathfinding.AddWorldPathfindingInfo(startMap, endMap, x, z, y);
            result = 0;
        }
        catch (Exception e)
        {
            result = LuaDLL.toluaL_exception(L, e, null);
        }
        return(result);
    }
Esempio n. 14
0
        public static List <MoveCommand> pathfind(WorldMap w, Actor a, Unit u, Hex target)
        {
            var ll = WorldPathfinding.Pathfind(w, u.h, target);

            if (ll == null)
            {
                throw new Exception("Path could not be found to " + target);
            }

            if ((ll.Count() - 1) > u.actions)
            {
                throw new Exception("Unit doesn't have enough movement points!");
            }

            var moves = new List <MoveCommand>(ll.Count);

            foreach (Hex h in ll)
            {
                moves.Add(new MoveCommand(a, u, h));
            }

            return(moves);
        }
Esempio n. 15
0
    private void HandleInput()
    {
        Ray        ray  = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit  = new RaycastHit();
        TileObject tile = null;

        if (Physics.Raycast(ray, out hit))
        {
            MousePosition = hit.point + Camera.main.transform.forward * 0.1f;

            HexCoordinate coordinates = HexCoordinate.FromPosition(MousePosition);
            int           index       = coordinates.X + coordinates.Z * Config.GameSave.width + coordinates.Z / 2;
            if (WorldManager.Tiles.Length > index && index >= 0)
            {
                tile = WorldManager.Tiles[index];
            }
            else
            {
                tile = null;
            }
        }
        else
        {
            MousePosition = Vector3.zero;
            tile          = null;
        }

        if (Input.GetMouseButtonUp(0) && !CameraManager.Dragging)
        {
            Config.Temporary.selectedTile = tile;
            CanvasLayerInGame.RefreshSelectedTile();
        }
        if (Config.Temporary.selectedUnit)
        {
            if (Input.GetMouseButtonDown(1))
            {
                path = WorldPathfinding.FindPath(Config.Temporary.selectedTile, tile).ToList();
            }

            if (Input.GetMouseButton(1))
            {
                if (tile)
                {
                    Gizmos.DrawTile(tile.transform.position + Vector3.up * 0.05f, WorldManager.InnerRadius, Color.white);
                }

                if (lastTile != tile && tile)
                {
                    var newPath = WorldPathfinding.FindPath(Config.Temporary.selectedTile, tile);
                    if (newPath != null)
                    {
                        path = newPath.ToList();
                    }
                }

                if (path != null && tile)
                {
                    for (int i = 0; i < path.Count; i++)
                    {
                        float size = (Mathf.Sin((Time.time + i * 0.2f) * 3f) + 5f) / 8f;

                        Color color = Color.yellow;
                        if (path.Count - i > Config.Temporary.selectedUnit.Moves + 1)
                        {
                            color = Color.red;
                        }

                        Gizmos.DrawTile(path[i].transform.position + Vector3.up * 0.05f, WorldManager.InnerRadius * size, color);
                    }
                }
            }
            if (Input.GetMouseButtonUp(1))
            {
                if (tile)
                {
                    int movesToSub = 0;
                    for (int i = 0; i < path.Count; i++)
                    {
                        if (i <= Config.Temporary.selectedUnit.Moves)
                        {
                            movesToSub++;
                            Config.Temporary.selectedUnit.Tile = path[(path.Count - 1) - i];
                            Config.Temporary.selectedTile      = path[(path.Count - 1) - i];
                        }
                    }

                    Config.Temporary.selectedUnit.Moves -= movesToSub - 1;
                    CanvasLayerInGame.RefreshSelectedTile();
                }
            }
        }

        lastTile = tile;
    }
Esempio n. 16
0
        public override void TurnActions()
        {
            List <Hex> neighbs  = h.Neighbors();
            List <Hex> hneighbs = w.hero.h.Neighbors();

            if (!persuing && w.hero.h.loc.Distance(h.loc) < 6)
            {
                var hero    = w.hero;
                Hex heroHex = hero.h;
                var path    = WorldPathfinding.Pathfind(w, h, heroHex);

                if (path.Count <= 5)
                {
                    setPersuing();
                }
                else
                {
                    Updated = true;
                }
            }

            if (persuing)
            {
                int dist = h.loc.Distance(target.h.loc);

                var targets = GetAttackPattern();

                foreach (Hex t in targets)
                {
                    if (t.unit == target)
                    {
                        Arrow a = new GameObject("arrow").AddComponent <Arrow>();
                        a.transform.parent = transform;

                        Hex middleHex = w.map[h.loc + ((target.h.loc - h.loc) / 2)];
                        if (middleHex.unit != null)
                        {
                            if (middleHex.unit.GetType() == typeof(BoulderUnit))
                            {
                                a.init(this, middleHex.unit);
                                Updated = true;
                                return;
                            }
                        }

                        a.init(this, target);
                        Updated = true;
                        return;
                    }
                }

                if (dist == 1)
                {
                    var nhex = h.loc + (h.loc - target.h.loc);
                    if (w.map.ContainsKey(nhex) && w.map[nhex].Passable())
                    {
                        if (w.map[nhex].unit == null)
                        {
                            h       = w.map[nhex];
                            Updated = true;
                        }
                    }

                    if (!Updated)
                    {
                        bool isSafe;

                        foreach (Hex neighb in neighbs)
                        {
                            if (Updated == false)
                            {
                                isSafe = true;

                                foreach (Hex hneighb in hneighbs)
                                {
                                    if (neighb == hneighb)
                                    {
                                        isSafe = false;
                                    }
                                }

                                if (isSafe)
                                {
                                    if (neighb.unit == null && neighb.Passable())
                                    {
                                        h       = neighb;
                                        Updated = true;
                                    }
                                }
                            }
                        }
                    }
                }
                else if (dist == 2)
                {
                    List <Hex> attackHexes = new List <Hex>();

                    foreach (HexLoc dir in HexLoc.hex_directions)
                    {
                        var nloc = w.hero.h.loc + dir + dir;

                        if (w.map.ContainsKey(nloc))
                        {
                            if (w.map[nloc].tileType == TileType.Normal)
                            {
                                attackHexes.Add(w.map[nloc]);
                            }
                        }
                    }

                    foreach (Hex neighbor in neighbs)
                    {
                        if (!Updated)
                        {
                            foreach (Hex t in attackHexes)
                            {
                                if (neighbor == t)
                                {
                                    if (t.unit == null)
                                    {
                                        h       = t;
                                        Updated = true;
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    var path = WorldPathfinding.Pathfind(w, h, target.h);
                    var next = path.First.Next.Value;
                    if (next.unit == null)
                    {
                        h       = next;
                        Updated = true;
                    }
                }
            }
        }