Esempio n. 1
0
        public static bool AttackToIndex(GameUnit unit, Vector3Int posTarget)
        {
            var goodNeighbour = AI_Calculation.GetNearestNeighbour(unit.ChunkNumber, unit.CurrentPos, posTarget);

            if (goodNeighbour == Vector3Int.zero ||
                !PathCalcManager.IsReaching(ChunkUtil.GetDovvner(unit.CurrentPos), goodNeighbour))
            {
                return(false);
            }

            var path = PresetPath(unit, goodNeighbour);

            if (path == null)
            {
                return(false);
            }

            path.Add(new TilePosition(
                         Util.Get3DPosByIndex(posTarget.x, posTarget.y, posTarget.z + 1)
                         , posTarget));

            // Debug.Log("Attack order from: " + ent);
            GameOrderManager.DoOrder(unit, path, OrderTypes.AttackOrder);
            return(true);
        }
Esempio n. 2
0
        public static DynValue FindNearestReacheble(GameUnit unit, Table t)
        {
            var        dist    = -1;
            GameEntity nearest = null;

            foreach (var v in t.Values)
            {
                var ent = v.ToObject <GameEntity>();
                if (ent == null)
                {
                    continue;
                }

                if (!PathCalcManager.IsReaching(ChunkUtil.GetDovvner(unit.CurrentPos),
                                                ChunkUtil.GetDovvner(ent.CurrentPos)))
                {
                    continue;
                }

                var d = AI_Calculation.GetDistTo(unit.CurrentPos, ent.CurrentPos, false);
                if (dist == -1 || d < dist)
                {
                    dist    = d;
                    nearest = ent;
                }
            }
            return(nearest == null ? DynValue.Nil : DynValue.FromObject(LuaManager.ScriptObj, nearest));
        }
Esempio n. 3
0
        public static Vector3Int GetNearestNeighbour(int chunkNumber, Vector3Int myPos, Vector3Int pos)
        {
            var list = GetGoodNeighbors(chunkNumber, pos);

            var minDist = -1;
            var p       = new Vector3Int();

            foreach (var point in list)
            {
                var dist = GetDistTo(myPos, point, true);
                if (minDist != -1 && dist > minDist)
                {
                    continue;
                }
                if ((ChunkUtil.IsAnyEntity(chunkNumber, ChunkUtil.GetUpper(point)) &&
                     myPos != ChunkUtil.GetUpper(point)) ||
                    !ChunkUtil.IsCanStayHere(chunkNumber, ChunkUtil.GetDovvner(point)) ||
                    !PathCalcManager.IsReaching(ChunkUtil.GetDovvner(myPos), ChunkUtil.GetDovvner(point))
                    )
                {
                    continue;
                }

                minDist = dist;
                p       = point;
            }


            return(p);
        }
Esempio n. 4
0
        void RghtClickedOnGameObject(GameObject obj)
        {
            if (_choosed)
            {
                var from = _choosedObj.GetComponent <GameUnit>();
                if (from == null || !from.IsMy())
                {
                    return;
                }
                var to = obj.GetComponent <GameEntity>();

                if (to == null)
                {
                    return;
                }

                if (!GroupUtil.isCreatureGroup(from.Group))
                {
                    return;
                }


                if (!FieldOfView.IsVisible(to) && !GroupUtil.IsGround(to.Group))
                {
                    ClickedOnSpace();
                    return;
                }

                Vector3Int f;
                if (from.MovingTo != null)
                {
                    f = from.MovingTo.Index;
                }
                else
                {
                    f = from.CurrentPos;
                    f.z--;
                }

                //Cant move ?
                if (from.UpgradedStats.MoveSpeed <= 0f)
                {
                    SimpleOrderManager.CancelOrders(from);
                    ErrorBar_HTML.SetupError("This unit can`t move!");
                    return;
                }

                //Attack
                var t             = to.CurrentPos;
                var buildingCheck = false;
                if (ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(t)))
                {
                    var underGround = ChunkManager.CurrentChunk.GetGameObjectByIndex(ChunkUtil.GetUpper(t));
                    var underEnt    = underGround.GetComponent <GameEntity>();
                    if (SecondaryGroundLvL.isSecondaryGroup(underEnt.Group))
                    {
                        buildingCheck = true;
                    }

                    if (!GroupUtil.IsGround(underEnt.Group) && underEnt.Owner != PlayersManager.GetMyPlayer())
                    {
                        var underP = underEnt.CurrentPos;
                        underP.z++;

                        if (!ChunkManager.CurrentChunk.IsMapPos(underP) ||
                            !ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(underEnt.CurrentPos)))
                        {
                            if (from.UpgradedStats.Dmg > 0)
                            {
                                SimpleOrderManager.AttackToIndex(from, t);
                            }
                            else if (from.UpgradedStats.Dmg == 0)
                            {
                                ErrorBar_HTML.SetupError("This unit can`t attack!");
                            }
                            return;
                        }
                    }
                }


                //Cancel or Move
                if ((ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(t)) ||
                     !PathCalcManager.IsReaching(f, t)) && !buildingCheck)
                {
                    ErrorBar_HTML.SetupError("Can`t reach that place!");
                    SimpleOrderManager.CancelOrders(from);
                }
                else if (ChunkUtil.IsCanStayHere(from.ChunkNumber, t) || buildingCheck)
                {
                    SimpleOrderManager.MoveToIndex(from, t);
                }
            }
            else
            {
                var clicked = obj.GetComponent <GameEntity>();
                if (clicked == null)
                {
                    return;
                }

                var pos = clicked.CurrentPos;

                if (FlagManager.IsFlagAtPos(pos))
                {
                    FlagManager.RemoveFlag(pos);
                }
                else
                {
                    FlagManager.SetupFlag(pos);
                }
            }
        }