public void SetElement(HexElement element)
    {
        this.element = element;

        foreach (Transform child in transform)
        {
            child.gameObject.SetActive(false);
        }

        switch (element)
        {
        case (HexElement.Black):
            elementMarkerBlack.gameObject.SetActive(true);
            break;

        case (HexElement.White):
            elementMarkerWhite.gameObject.SetActive(true);
            break;

        case (HexElement.Red):
            elementMarkerRed.gameObject.SetActive(true);
            break;

        case (HexElement.Green):
            elementMarkerGreen.gameObject.SetActive(true);
            break;

        case (HexElement.Blue):
            elementMarkerBlue.gameObject.SetActive(true);
            break;

        default:
            break;
        }
    }
Exemplo n.º 2
0
 public void Visit(Anchor anchor, byte data)
 {
     anchor.OriginalFormat.Visit(this, data);
     if (NewCell != null)
     {
         NewCell = new HexElement(NewCell.Value, new Anchor(NewCell.Format, anchor.Name, anchor.Format, anchor.Sources));
     }
 }
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            UnitBehaviorContext unitContext = context as UnitBehaviorContext;

            if (unitContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            BaseEnvironment envi = unitContext.Unit.Environment;

            if (envi == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            bool       foundPosition  = false;
            HexElement position       = new HexElement();
            StaticUnit warpMagnetUnit = BaseEnvironment.GetActiveWarpMagnet(envi) as StaticUnit;

            if (warpMagnetUnit == null)
            {
                Builder unit = unitContext.Unit as Builder;
                position.Q    = unit.Q;
                position.R    = unit.R;
                foundPosition = true;
            }
            else
            {
                float distance = warpMagnetUnit.TotalSensorsEnergy + 1;
                foreach (var coord in HexMap.Neighbors)
                {
                    position.Q = (short)(warpMagnetUnit.Q + distance * coord.x);
                    position.R = (short)(warpMagnetUnit.R + distance * coord.y);
                    if (envi.UnitsMap.ContainsKey(position.HexMapKey) || !envi.PathFindingGraph.VertexHexCache.ContainsKey(position.HexMapKey))
                    {
                        continue;
                    }

                    foundPosition = true;
                    break;
                }
            }

            if (!foundPosition)
            {
                unitContext.EnvironmentTarget = null;
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            unitContext.EnvironmentTarget = position;
            returnCode = BehaviorReturnCode.Success;
            return(returnCode);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;

            if (playerContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            Player player = playerContext.Player;

            foreach (var body in player.StarSystemBodies)
            {
                Planet planet = body as Planet;
                if (planet == null)
                {
                    continue;
                }

                BaseEnvironment envi        = planet.Environment;
                List <BaseUnit> enviUnits   = player.Units.Where(u => u.Environment == envi).ToList();
                HexElement      defenseUnit = enviUnits.FirstOrDefault(u => u is DefenseTowerUnit || u is Station);
                if (defenseUnit == null)
                {
                    BaseUnit existingUnit = enviUnits.FirstOrDefault(u => u is Builder && u.Behavior == null);
                    playerContext.BehaviorTarget = existingUnit;
                    if (existingUnit != null)
                    {
                        playerContext.EnvironmentTarget = planet.Environment;
                        returnCode = BehaviorReturnCode.Success;
                        return(returnCode);
                    }
                }
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Exemplo n.º 5
0
    private void Initialize(Vector3 center)
    {
        hexElement      = GetComponentInChildren <HexElement>();
        gridCanvas      = GetComponentInChildren <Canvas>();
        hexElement.name = "HexElement";
        Mesh mesh = GetComponent <MeshFilter>().mesh;

        transform.position = center;

        mesh.Clear();

        vertices = new Vector3[] {
            new Vector3(0f, 0f, 0f),
            new Vector3(0f, 0f, outerRadius),
            new Vector3(innerRadius, 0f, 0.5f * outerRadius),
            new Vector3(innerRadius, 0f, -0.5f * outerRadius),
            new Vector3(0f, 0f, -outerRadius),
            new Vector3(-innerRadius, 0f, -0.5f * outerRadius),
            new Vector3(-innerRadius, 0f, 0.5f * outerRadius),
            new Vector3(0f, 0f, outerRadius)
        };
        mesh.vertices = vertices;

        for (int i = 1; i < 7; i++)
        {
            addTriangle(0, i, i + 1);
        }
        mesh.triangles = triangles;

        void addTriangle(int v1, int v2, int v3)
        {
            triangles[triangles.Length] = v1;
            triangles[triangles.Length] = v2;
            triangles[triangles.Length] = v3;
        }
    }
        public NewMediumLevelRecordEditor(Plugin p, Record r, SubRecord sr, SubrecordStructure ss)
        {
            InitializeComponent();
            Icon = Resources.tesv_ico;
            SuspendLayout();
            this.sr = sr;
            this.ss = ss;
            this.p  = p;
            this.r  = r;

            // walk each element in standard fashion
            int panelOffset = 0;

            try
            {
                foreach (var elem in ss.elements)
                {
                    Control c = null;
                    if (elem.options != null && elem.options.Length > 1)
                    {
                        c = new OptionsElement();
                    }
                    else if (elem.flags != null && elem.flags.Length > 1)
                    {
                        c = new FlagsElement();
                    }
                    else
                    {
                        switch (elem.type)
                        {
                        case ElementValueType.LString:
                            c = new LStringElement();
                            break;

                        case ElementValueType.FormID:
                            c = new FormIDElement();
                            break;

                        case ElementValueType.Blob:
                            c = new HexElement();
                            break;

                        default:
                            c = new TextElement();
                            break;
                        }
                    }
                    if (c is IElementControl)
                    {
                        var ec = c as IElementControl;
                        ec.formIDLookup = p.GetRecordByID;
                        ec.formIDScan   = p.EnumerateRecords;
                        ec.strIDLookup  = p.LookupFormStrings;
                        ec.Element      = elem;

                        if (elem.repeat > 0)
                        {
                            var ge = new RepeatingElement();
                            c        = ge;
                            c.Left   = 8;
                            c.Width  = fpanel1.Width - 16;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            ge.InnerControl = ec;
                            ge.Element      = elem;
                            ec = ge;
                        }
                        else if (elem.optional)
                        {
                            var re = new OptionalElement();
                            c        = re;
                            c.Left   = 8;
                            c.Width  = fpanel1.Width - 16;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            re.InnerControl = ec;
                            re.Element      = elem;
                            ec = re;
                            c  = re;
                        }
                        else
                        {
                            c.Left   = 8;
                            c.Width  = fpanel1.Width - 16;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;
                        }
                        c.MinimumSize = c.Size;

                        controlMap.Add(elem, ec);
                        fpanel1.Controls.Add(c);
                        panelOffset = c.Bottom;
                    }
                }

                foreach (Element elem in r.EnumerateElements(sr, true))
                {
                    var es = elem.Structure;

                    IElementControl c;
                    if (controlMap.TryGetValue(es, out c))
                    {
                        if (c is IGroupedElementControl)
                        {
                            var gc = c as IGroupedElementControl;
                            gc.Elements.Add(elem.Data);
                        }
                        else
                        {
                            c.Data = elem.Data;
                        }
                    }
                }
            }
            catch
            {
                strWarnOnSave =
                    "The subrecord doesn't appear to conform to the expected structure.\nThe formatted information may be incorrect.";
                Error.SetError(bSave, strWarnOnSave);
                Error.SetIconAlignment(bSave, ErrorIconAlignment.MiddleLeft);
                AcceptButton = bCancel; // remove save as default button when exception occurs
                CancelButton = bCancel;
                UpdateDefaultButton();
            }
            ResumeLayout();
        }
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            UnitBehaviorContext unitContext = context as UnitBehaviorContext;

            if (unitContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            BaseEnvironment envi = unitContext.Unit.Environment;

            if (envi == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            float  maxPlanetUtility = float.MinValue;
            Planet selectedPlanet   = null;
            var    player           = unitContext.Unit.Owner;

            foreach (var galaxyBodyKey in player.ExploredEnvironments)
            {
                HexElement elem = null;
                if (!player.GameSession.Galaxy.EnvironmentMap.TryGetValue(galaxyBodyKey, out elem))
                {
                    continue;
                }

                // TODO: rogue planets colonization
                StarSystem system = elem as StarSystem;
                if (system == null || system.NumberOfPlanets == 0 || !system.HasHighestInfluence(player))
                {
                    continue;
                }

                int systemDistance = HexMap.Distance(envi, system);
                if (envi is Galaxy)
                {
                    systemDistance = HexMap.Distance(unitContext.Unit, system);
                }

                foreach (var planet in system.Planets)
                {
                    if (planet == null || planet.Owner != null || !player.CanColonizePlanetType(planet.PlanetType))
                    {
                        continue;
                    }

                    var existingColonyShip = player.Units.FirstOrDefault(u => u.Item.UnitConfig.Actions.HasFlag(UnitActions.Colonize) &&
                                                                         u != unitContext.Unit &&
                                                                         u.BehaviorContext.EnvironmentTarget == planet);
                    if (existingColonyShip != null)
                    {
                        continue;
                    }

                    float planetUtility = GetPlanetUtility(player, planet);

                    planetUtility -= systemDistance * DistanceSystemUtilityCoefficient;

                    if (envi == system)
                    {
                        planetUtility -= HexMap.Distance(unitContext.Unit, planet);
                    }

                    var isHomePlanet = player.GameSession.Players.FirstOrDefault(p => p.HomePlanet == planet);
                    if (isHomePlanet != null)
                    {
                        selectedPlanet = planet;
                        break;
                    }

                    if (maxPlanetUtility > planetUtility)
                    {
                        continue;
                    }

                    maxPlanetUtility = planetUtility;
                    selectedPlanet   = planet;
                }
            }

            if (selectedPlanet != null)
            {
                unitContext.EnvironmentTarget = selectedPlanet;
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            UnitBehaviorContext unitContext = context as UnitBehaviorContext;

            if (unitContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            MoveableUnit unit = unitContext.Unit as MoveableUnit;

            if (unit == null || !unit.CanMiniJump)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            if (unitContext.EnvironmentTarget == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            short destinationQ = unitContext.EnvironmentTarget.Q;
            short destinationR = unitContext.EnvironmentTarget.R;

            Planet       planet          = unitContext.EnvironmentTarget as Planet;
            MoveableUnit destinationUnit = unitContext.EnvironmentTarget as MoveableUnit;

            if (planet != null)
            {
                destinationQ = planet.NextTurnQ;
                destinationR = planet.NextTurnR;
            }
            else if (destinationUnit != null)
            {
                // find hex around unit
                short nq = destinationUnit.Q;
                short nr = destinationUnit.R;
                destinationUnit.GetMiniJumpDestination(out nq, out nr);
                int        key          = HexMap.CalculateKey(nq, nr);
                HexElement existingUnit = null;
                var        envi         = destinationUnit.Environment;

                bool  foundHex     = false;
                int   searchRadius = 1;
                short centerQ      = nq;
                short centerR      = nr;
                while (!foundHex)
                {
                    var range = HexMap.GetRing(centerQ, centerR, searchRadius);
                    foreach (var coord in range)
                    {
                        nq = coord.Item1;
                        nr = coord.Item2;
                        if (envi.MapRadius > HexMap.Distance(0, nq, 0, nr) &&
                            !envi.UnitsMap.TryGetValue(HexMap.CalculateKey(nq, nr), out existingUnit))
                        {
                            foundHex = true;
                            break;
                        }
                    }

                    searchRadius++;
                }

                destinationQ = nq;
                destinationR = nr;
            }
            else if (unitContext.EnvironmentTarget is Anomaly)
            {
                destinationQ = unitContext.EnvironmentTarget.Q;
                destinationR = unitContext.EnvironmentTarget.R;
            }
            else
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            MiniWarpJumpTask task = new MiniWarpJumpTask(unit, destinationQ, destinationR);

            task.Execute();

            if (task.IsTaskFinished)
            {
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }
Exemplo n.º 9
0
        public NewMediumLevelRecordEditor(Plugin p, Record r, SubRecord sr, SubrecordStructure ss)
        {
            this.InitializeComponent();
            Icon = Resources.tesv_ico;
            SuspendLayout();
            this.sr = sr;
            this.ss = ss;
            this.p  = p;
            this.r  = r;

            // walk each element in standard fashion
            int panelOffset = 0;

            try
            {
                this.fpanel1.ColumnStyles[0] = new ColumnStyle(SizeType.Percent, 100.0f);
                int maxWidth   = this.fpanel1.Width - SystemInformation.VerticalScrollBarWidth - 8;
                int leftOffset = 0; // 8;
                foreach (var elem in ss.elements)
                {
                    Control c = null;
                    if (elem.options != null && elem.options.Length > 1)
                    {
                        c = new OptionsElement();
                    }
                    else if (elem.flags != null && elem.flags.Length > 1)
                    {
                        c = new FlagsElement();
                    }
                    else
                    {
                        switch (elem.type)
                        {
                        case ElementValueType.LString:
                            c = new LStringElement();
                            break;

                        case ElementValueType.FormID:
                            c = new FormIDElement();
                            break;

                        case ElementValueType.Blob:
                            c = new HexElement();
                            break;

                        default:
                            c = new TextElement();
                            break;
                        }
                    }

                    if (c is IElementControl)
                    {
                        var ec = c as IElementControl;
                        ec.formIDLookup = p.GetRecordByID;
                        ec.formIDScan   = p.EnumerateRecords;
                        ec.strIDLookup  = p.LookupFormStrings;
                        ec.Element      = elem;

                        if (elem.repeat > 0)
                        {
                            var ge = new RepeatingElement();
                            c        = ge;
                            c.Left   = leftOffset;
                            c.Width  = maxWidth;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            ge.InnerControl = ec;
                            ge.Element      = elem;
                            ec = ge;
                        }
                        else if (elem.optional > 0)
                        {
                            var re = new OptionalElement();
                            c        = re;
                            c.Left   = leftOffset;
                            c.Width  = maxWidth;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            re.InnerControl = ec;
                            re.Element      = elem;
                            ec = re;
                            c  = re;
                        }
                        else
                        {
                            c.Left   = leftOffset;
                            c.Width  = maxWidth;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;
                        }

                        this.controlMap.Add(elem, ec);
                        int idx = this.fpanel1.RowCount - 1;
                        this.fpanel1.Controls.Add(c, 0, idx);
                        var info = new RowStyle(SizeType.Absolute, c.Size.Height + 2);
                        if (idx == 0)
                        {
                            this.fpanel1.RowStyles[0] = info;
                        }
                        else
                        {
                            this.fpanel1.RowStyles.Add(info);
                        }
                        panelOffset = 0;
                        ++this.fpanel1.RowCount;
                    }
                }

                foreach (Element elem in r.EnumerateElements(sr, true))
                {
                    var es = elem.Structure;

                    IElementControl c;
                    if (this.controlMap.TryGetValue(es, out c))
                    {
                        if (c is IGroupedElementControl)
                        {
                            var gc = c as IGroupedElementControl;
                            gc.Elements.Add(elem.Data);
                        }
                        else
                        {
                            c.Data = elem.Data;
                        }
                    }
                }
            }
            catch
            {
                this.strWarnOnSave = "The subrecord doesn't appear to conform to the expected structure.\nThe formatted information may be incorrect.";
                this.Error.SetError(this.bSave, this.strWarnOnSave);
                this.Error.SetIconAlignment(this.bSave, ErrorIconAlignment.MiddleLeft);
                AcceptButton = this.bCancel; // remove save as default button when exception occurs
                CancelButton = this.bCancel;
                UpdateDefaultButton();
            }

            ResumeLayout();
        }
Exemplo n.º 10
0
        public void SetContext(Record r, SubRecord sr, bool hexView)
        {
            if (this.r == r && this.sr == sr && this.hexView == hexView)
            {
                return;
            }

            if (r == null || sr == null)
            {
                this.ClearControl();
                return;
            }

            // walk each element in standard fashion
            int panelOffset = 0;

            try
            {
                this.BeginUpdate();

                this.ClearControl();
                SuspendLayout();
                this.fpanel1.SuspendLayout();
                this.fpanel1.Width = Parent.Width;
                this.controlMap.Clear();

                this.hexView = hexView;
                this.r       = r;
                this.sr      = sr;
                var p = this.GetPluginFromNode(r);
                this.ss = sr.Structure;

                // default to blob if no elements
                if (this.ss == null || this.ss.elements == null || hexView)
                {
                    var c = new HexElement();
                    c.Left   = 8;
                    c.Width  = this.fpanel1.Width - 16;
                    c.Top    = panelOffset;
                    c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                    var elem = r.EnumerateElements(sr, true).FirstOrDefault();
                    if (elem != null)
                    {
                        this.controlMap.Add(elem.Structure, c);
                        this.fpanel1.Controls.Add(c);
                        c.Data = elem.Data;
                    }
                }
                else
                {
                    foreach (var elem in this.ss.elements)
                    {
                        Control c = null;
                        if (elem.options != null && elem.options.Length > 1)
                        {
                            c = new OptionsElement();
                        }
                        else if (elem.flags != null && elem.flags.Length > 1)
                        {
                            c = new FlagsElement();
                        }
                        else
                        {
                            switch (elem.type)
                            {
                            case ElementValueType.LString:
                                c = new LStringElement();
                                break;

                            case ElementValueType.FormID:
                                c = new FormIDElement();
                                break;

                            case ElementValueType.Blob:
                                c = new HexElement();
                                break;

                            default:
                                c = new TextElement();
                                break;
                            }
                        }

                        if (c is IElementControl)
                        {
                            var ec = c as IElementControl;
                            ec.formIDLookup = p.GetRecordByID;
                            ec.formIDScan   = p.EnumerateRecords;
                            ec.strIDLookup  = p.LookupFormStrings;
                            ec.Element      = elem;

                            if (elem.repeat > 0)
                            {
                                var ge = new RepeatingElement();
                                c        = ge;
                                c.Left   = 8;
                                c.Width  = this.fpanel1.Width - 16;
                                c.Top    = panelOffset;
                                c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                                ge.InnerControl = ec;
                                ge.Element      = elem;
                                ec = ge;
                            }
                            else if (elem.optional > 0)
                            {
                                var re = new OptionalElement();
                                c        = re;
                                c.Left   = 8;
                                c.Width  = this.fpanel1.Width - 16;
                                c.Top    = panelOffset;
                                c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                                re.InnerControl = ec;
                                re.Element      = elem;
                                ec = re;
                                c  = re;
                            }
                            else
                            {
                                c.Left   = 8;
                                c.Width  = this.fpanel1.Width - 16;
                                c.Top    = panelOffset;
                                c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;
                            }

                            c.MinimumSize = c.Size;

                            this.controlMap.Add(elem, ec);
                            this.fpanel1.Controls.Add(c);
                            panelOffset = c.Bottom;
                        }
                    }

                    foreach (Element elem in r.EnumerateElements(sr, true))
                    {
                        var es = elem.Structure;

                        IElementControl c;
                        if (this.controlMap.TryGetValue(es, out c))
                        {
                            if (c is IGroupedElementControl)
                            {
                                var gc = c as IGroupedElementControl;
                                gc.Elements.Add(elem.Data);
                            }
                            else
                            {
                                c.Data = elem.Data;
                            }
                        }
                    }
                }

                Enabled = true;
            }
            catch
            {
                this.strWarnOnSave = "The subrecord doesn't appear to conform to the expected structure.\nThe formatted information may be incorrect.";
            }
            finally
            {
                this.fpanel1.ResumeLayout();
                ResumeLayout();
                this.EndUpdate();
                Refresh();
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            UnitBehaviorContext unitContext = context as UnitBehaviorContext;

            if (unitContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            MoveableUnit unit = unitContext.Unit as MoveableUnit;

            if (unit == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            if (unit.Target == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            if (unit.Environment != unit.Target.Environment)
            {
                unit.Target = null;
                returnCode  = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            /*
             * Vector targetCoords = new Vector();
             *
             * Vector unitCoords = new Vector();
             * unitCoords.X = unit.Q;
             * unitCoords.Y = unit.R;
             *
             * Vector direction = Vector.Normalize(targetCoords - unitCoords);
             * direction.X = (float)Math.Round(direction.X, 0);
             * direction.Y = (float)Math.Round(direction.Y, 0);
             *
             * if (PathTargetPosition)
             * {
             *  direction = Vector.Zero;
             * }
             *
             * unit.SelectedPath = null;
             * int range = (int) unit.SensorsEnergy;
             * while (unit.SelectedPath == null && range > 0)
             * {
             *  targetCoords -= (direction * range);
             *  unit.CalculatePath(unit.Environment, (short) targetCoords.X, (short) targetCoords.Y);
             *  range--;
             * }
             */

            short      nq           = unit.Target.Q;
            short      nr           = unit.Target.R;
            HexElement existingUnit = null;
            bool       foundHex     = false;
            int        searchRadius = (int)unit.SensorsEnergy;
            short      centerQ      = nq;
            short      centerR      = nr;

            while (!foundHex && searchRadius > 0)
            {
                var range = HexMap.GetRing(centerQ, centerR, searchRadius);
                foreach (var coord in range)
                {
                    nq = coord.Item1;
                    nr = coord.Item2;
                    if (unit.Environment.MapRadius > HexMap.Distance(0, nq, 0, nr) &&
                        !unit.Environment.UnitsMap.TryGetValue(HexMap.CalculateKey(nq, nr), out existingUnit))
                    {
                        foundHex = true;
                        break;
                    }
                }

                searchRadius--;
            }

            if (foundHex)
            {
                unit.CalculatePath(unit.Environment, nq, nr);
            }

            if (!foundHex || unit.SelectedPath == null)
            {
                unit.Target = null;
                returnCode  = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Success;
            return(returnCode);
        }
Exemplo n.º 12
0
 // Start is called before the first frame update
 void Start()
 {
     HexElement hexElement = new HexElement(new Vector3(0, 0, 0));
 }
Exemplo n.º 13
0
        /// <summary>
        /// Executes behavior with given context
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override BehaviorReturnCode Behave(IBehaviorContext context)
        {
            PlayerBehaviorContext playerContext = context as PlayerBehaviorContext;

            if (playerContext == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            Player player = playerContext.Player;

            float  maxPlanetUtility = float.MinValue;
            Planet planetToColonize = null;

            foreach (var galaxyBodyKey in player.ExploredEnvironments)
            {
                HexElement elem = null;
                if (!player.GameSession.Galaxy.EnvironmentMap.TryGetValue(galaxyBodyKey, out elem))
                {
                    continue;
                }

                StarSystem system = elem as StarSystem;
                if (system == null || system.NumberOfPlanets == 0)
                {
                    continue;
                }

                foreach (var planet in system.Planets)
                {
                    if (planet == null || planet.Owner != null || !player.CanColonizePlanetType(planet.PlanetType))
                    {
                        continue;
                    }

                    /*
                     * int planetScanned = player.ScannedStarSystemBodies.Contains(planet.GlobalKey) ? 1 : 0;
                     * float planetPreference = player.Race.PlanetTypePreferences[(int)planet.PlanetType] / 100f;
                     * float planetUtility = (planet.AvailResources * planetPreference) / planet.Mass + planet.AvailEnergy / planet.Mass + (planet.RareResources.Count * planetScanned) + planet.MaxSlotsCount;
                     */

                    float planetUtility = FindPlanetToColonize.GetPlanetUtility(player, planet);
                    if (maxPlanetUtility > planetUtility)
                    {
                        continue;
                    }

                    maxPlanetUtility = planetUtility;
                    planetToColonize = planet;
                }
            }

            if (planetToColonize == null)
            {
                returnCode = BehaviorReturnCode.Failure;
                return(returnCode);
            }

            float  maxUtility = float.MinValue;
            Planet bestPlanet = null;

            foreach (var body in player.StarSystemBodies)
            {
                Planet planet = body as Planet;
                if (planet == null)
                {
                    continue;
                }

                float distance = HexMap.Distance(planetToColonize, planet);
                if (planetToColonize.Environment != planet.Environment)
                {
                    distance = HexMap.Distance(planetToColonize.Environment, planet.Environment) * DistanceSystemUtilityCoefficient;
                }

                float utility = planet.AvailProduction + planet.Population - distance;
                if (maxUtility > utility)
                {
                    continue;
                }

                maxUtility = utility;
                bestPlanet = planet;
            }

            if (bestPlanet != null)
            {
                playerContext.BehaviorTarget = bestPlanet;
                returnCode = BehaviorReturnCode.Success;
                return(returnCode);
            }

            returnCode = BehaviorReturnCode.Failure;
            return(returnCode);
        }