Exemplo n.º 1
0
    // ---------------------------------------------------------------------------------------
    /*                                  AGENT ACTION METHODS                                */
    // ---------------------------------------------------------------------------------------

    void Attack(int dir)
    {
        // Calculate damage on target
        const int ATTACK_DMG_CONSTANT = 16;
        GameCharacter target;
        HexTile neighborTile;
        float damageApplied;

        if (BattleMap_.mapTiles.TryGetValue(HexCalculator.GetNeighborAtDir(InGamePosition, dir), out neighborTile))
        {
            target = BattleMap_.mapTiles[HexCalculator.GetNeighborAtDir(InGamePosition, dir)].Occupier;

            if (target != null)
            {
                damageApplied = StatCalculator.PhysicalDmgCalc(GetStatValueByName("STR"), ATTACK_DMG_CONSTANT, target.GetStatValueByName("RES"), GetStatusEffectByName("BRAVERY"), target.GetStatusEffectByName("ARMOR"));
                target.ReceiveDamage(damageApplied);

                if (UnitInPredatorList(target))
                {
                    AddReward(0.2f);
                }
                else if (!UnitInTargetList(target))
                {
                    AddReward(-1f);
                }

                return;
            }
        }

        // Position at dir has no occupier!
        // Position at dir has no tile!
        AddReward(-0.5f);
    }
Exemplo n.º 2
0
    public override void Heuristic(float[] action)
    {
        int dir = TargetInRange();

        if (GetStatValueByName("HP") < (Mathf.RoundToInt(MaxHP * 0.35f)))
        {
            action[0] = -1f;
            action[1] = 2f;         // Defend when hp drops under a certain threshold
        }
        if (dir != -1)
        {
            action[0] = 0f;
            action[1] = dir;
        }
        else
        {
            action[0] = 1f;
            dir       = RunnawayDir();

            if (dir != -1)
            {
                action[1] = dir;
            }
            else
            {
                action[1] = HexCalculator.RandomDir();
            }
        }
    }
Exemplo n.º 3
0
    // ---------------------------------------------------------------------------------------
    /*                                   AGENT UNIQUE SKILLS                                */
    // ---------------------------------------------------------------------------------------

    void MothersEmbrace(int dir)
    {
        GameCharacter ally;
        HexTile       neighborTile;

        if (BattleMap_.mapTiles.TryGetValue(HexCalculator.GetNeighborAtDir(InGamePosition, dir), out neighborTile))
        {
            ally = BattleMap_.mapTiles[HexCalculator.GetNeighborAtDir(InGamePosition, dir)].Occupier;

            if (ally != null)
            {
                ally.ApplyStatusEffect("ARMOR", 1.5f);

                if (!UnitInFaction(ally))
                {
                    AddReward(-1f);
                }
                else
                {
                    AddReward(1f);
                }

                return;
            }
        }

        // Position at dir has no occupier!
        // Position at dir has no tile!
        AddReward(-0.5f);
    }
        public IActionResult Index(HexCalculator model)
        {
            SetViewBagValues();
            if (!ModelState.IsValid)
            {
                return(View());
            }

            String selectedValue = model.SelectedOperation;

            switch (selectedValue)
            {
            case "Add":
                model.ResultInAltBase = model.AdditionResultInHex();
                model.ResultInDecimal = model.AdditionResultInDecimal();
                break;

            case "Subtract":
                model.ResultInAltBase = model.SubtractionResultInHex();
                model.ResultInDecimal = model.SubtractionResultInDecimal();
                break;

            case "Multiply":
                model.ResultInAltBase = model.MultiplicationResultInHex();
                model.ResultInDecimal = model.MultiplicationResultInDecimal();
                break;

            case "Divide":
                model.ResultInAltBase = model.DivisionResultInHex();
                model.ResultInDecimal = model.DivisionResultInDecimal();
                break;
            }

            return(View(model));
        }
Exemplo n.º 5
0
    private void Start()
    {
        var center = Vector3.zero;

        hexCalc       = new HexCalculator();
        hexCoordinate = new HexCoordinate(RingSize - 1, (RingSize - 1) * 2);
        var proguression = hexCalc.CalcProgression(0, 6, RingSize);
        var hexNum       = proguression[proguression.Length - 1] + 1;

        hices   = new Hex[hexNum];
        perform = new HexPerform(this);
        shake   = GetComponent <HexShake>();
        for (int i = 0; i < hexNum; i++)
        {
            var obj = Instantiate(HexObject, transform);
            obj.name = "obj" + i;
            obj.transform.localPosition = hexCalc.PositionFromIndex(i, center, 87, proguression);
            // obj.transform.SetParent(transform);
            hices[i]       = obj.GetComponent <Hex>();
            hices[i].Point = hexCalc.CalcCoordinate(i, proguression);
            hices[i].SetHexMaster(this);
            hices[i].ID = i;
            hexCoordinate.SetHex(hices[i]);
        }
        for (int i = 0; i < hexNum; i++)
        {
            hices[i].SetContacted(hexCoordinate.GetContactedHex(hices[i].Point));
        }

        deck = GetComponent <DeckManager>();
    }
Exemplo n.º 6
0
    void InitializeAgents()
    {
        GameObject        go;
        GameCharacter     agent;
        List <Vector2Int> spawntiles;

        int[] spawnIndex = new int[] { -1, -1, -1, -1 };
        int   factionID;

        // Randomly shuffle the spawnList in-place to give enemies a spawn position
        for (int i = 0; i < spawnableTiles_.Count; i++)
        {
            Utils.FisherYatesShuffle <Vector2Int>(spawnableTiles_[i]);
        }


        for (int i = 0; i < battleUnits_.Count; i++)                                    // Place the agents on the battleMap
        {
            agent = battleUnits_[i];
            go    = agent.GameObject();

            factionID = agent.FactionID;
            factions[factionID][agent.ID] = true;
            spawntiles = spawnableTiles_[factionID];

            agent.InGamePosition  = spawntiles[++spawnIndex[factionID]];
            go.transform.position = HexCalculator.CharacterPosition(spawntiles[spawnIndex[factionID]]);

            mapTiles[spawntiles[spawnIndex[factionID]]].Occupier = agent;
        }
    }
Exemplo n.º 7
0
    public override void Heuristic(float[] action)
    {
        int attackDir  = TargetInRange();
        int protectDir = AllyInRange();

        if (protectDir != -1)           // Prioritizes children protection
        {
            action[0] = protectDir;
            action[1] = 3f;
        }
        else if (GetStatValueByName("HP") < (Mathf.RoundToInt(MaxHP * 0.35f)))
        {
            action[0] = -1f;
            action[1] = 2f;         // Defend when hp drops under a certain threshold
        }
        else if (attackDir != -1)
        {
            action[0] = 0f;
            action[1] = attackDir;
        }
        else
        {
            action[0] = 1f;
            attackDir = RunnawayDir();

            if (attackDir != -1)
            {
                action[1] = attackDir;
            }
            else
            {
                action[1] = HexCalculator.RandomDir();
            }
        }
    }
Exemplo n.º 8
0
    protected int RunnawayDir()
    {
        List <GameCharacter> detectedEnemies = PredatorsInSightSensor(); //

        // Choose most inminent predator: proximity criteria
        if (detectedEnemies.Count <= 0)
        {
            return(-1);
        }

        Vector2Int predPosition = ClosestObjective(detectedEnemies);
        HexTile    currentTile  = BattleMap_.mapTiles[InGamePosition];
        HexTile    neighbor;

        List <int> oppositedir = HexCalculator.OppositeDir(HexCalculator.GeneralDirectionTowards(this.InGamePosition, predPosition));    //

        // Check directions available
        for (int i = 0; i < oppositedir.Count; i++)
        {
            if (currentTile.Neighbors.TryGetValue(oppositedir[i], out neighbor))
            {
                if (!neighbor.Occupied)
                {
                    return(oppositedir[i]);          // return most optimal escape route (if possible)
                }
            }
        }

        // If all are occupied, "failed escape"
        return(-1);
    }
Exemplo n.º 9
0
    void Move(int dir)
    {
        // First, check if movement is possible
        // - Does the destination tile exist?
        // - Is it free?
        HexTile destinationTile;
        Vector2Int destination;

        if (BattleMap_.mapTiles[InGamePosition].Neighbors.TryGetValue(dir, out destinationTile))
        {
            if (!destinationTile.Occupied)
            {
                destination = destinationTile.Position;

                Vector3 igPosition = HexCalculator.CharacterPosition(destination);
                gameObject.GetComponent <Rigidbody>().position = igPosition;
                gameObject.transform.position = igPosition;

                BattleMap_.mapTiles[InGamePosition].EmptyTile();

                InGamePosition           = destination;
                destinationTile.Occupier = this;

                return;
            }
        }

        // Position at dir has no tile to move at!
        // Position at dir is occupied!
        AddReward(-0.5f);
    }
Exemplo n.º 10
0
    /// <summary>
    ///     Utilization of hex mathematics to deduce general direction towards a certain tile
    /// </summary>
    /// <returns>
    ///     A suboptimal movement dir towards closest target
    /// </returns>
    protected int ChaseDir()
    {
        List <GameCharacter> detectedEnemies = ObjectivesInSightSensor();

        // Choose most desirable prey: proximity criteria
        if (detectedEnemies.Count <= 0)
        {
            return(-1);
        }

        Vector2Int preyPosition = ClosestObjective(detectedEnemies);
        HexTile    currentTile  = BattleMap_.mapTiles[InGamePosition];
        HexTile    neighbor;

        List <int> dirList = HexCalculator.ForwardDir(HexCalculator.GeneralDirectionTowards(this.InGamePosition, preyPosition));

        for (int i = 0; i < dirList.Count; i++)
        {
            if (currentTile.Neighbors.TryGetValue(dirList[i], out neighbor))
            {
                if (!neighbor.Occupied)
                {
                    return(dirList[i]);
                }
            }
        }

        // If all are occupied, "failed chasing"
        return(-1);
    }
Exemplo n.º 11
0
        public IActionResult HexCalculator(HexCalculator calc, string command)
        {
            if (command == null)
            {
                return(View());
            }

            calc.hexOperation(command);
            return(View(calc));
        }
Exemplo n.º 12
0
    private void Awake()
    {
        if (numberOfMaps > MAX_COLS * MAX_ROWS)
        {
            numberOfMaps = MAX_COLS * MAX_ROWS;
        }

        if (numberOfMaps < 1)
        {
            numberOfMaps = 1;
        }

        battleMapList_ = new BattleMap[numberOfMaps];
        cameraList_    = new Camera[numberOfMaps];

        int j = 0, i = 0;

        for (int k = 0; k < battleMapList_.Length; k++)
        {
            battleMapList_[k] = Instantiate(battleMapPrefab, this.transform).GetComponent <BattleMap>();

            battleMapList_[k].OffsetCol          = HorizOffset * i;
            battleMapList_[k].OffsetRow          = VertOffset * j;
            battleMapList_[k].transform.position = HexCalculator.Position((HorizOffset * i), (VertOffset * j)) + new Vector3(0, 0, -1);

            cameraList_[k]         = battleMapList_[k].GetComponentInChildren <Camera>();
            cameraList_[k].enabled = false;

            battleMapList_[k].StartMap();

            if (j >= MAX_COLS - 1)
            {
                j = 0;

                if (i >= MAX_ROWS)
                {
                    break;
                }
                else
                {
                    i++;
                }
            }
            else
            {
                j++;
            }
        }

        cameraList_[currentCameraIndex].enabled = true;
    }
Exemplo n.º 13
0
    protected float DistanceTowardsClosestTarget()
    {
        List <GameCharacter> detectedEnemies = ObjectivesInSightSensor();

        // Choose most desirable prey: proximity criteria
        if (detectedEnemies.Count <= 0)
        {
            return(-1);
        }

        Vector2Int preyPosition = ClosestObjective(detectedEnemies);

        return(HexCalculator.OffsetDistance(InGamePosition, preyPosition));
    }
Exemplo n.º 14
0
    protected float DistanceTowardsClosestPredator()
    {
        List <GameCharacter> detectedEnemies = PredatorsInSightSensor(); //

        // Choose most inminent predator: proximity criteria
        if (detectedEnemies.Count <= 0)
        {
            return(-1);
        }

        Vector2Int predPosition = ClosestObjective(detectedEnemies);

        return(HexCalculator.OffsetDistance(InGamePosition, predPosition));
    }
Exemplo n.º 15
0
    protected List <float> ProximitySensor()
    {
        List <float>         proximitySensor    = new List <float>(new float[] { 0f, 0f, 0f, 0f, 0f, 0f });
        List <GameCharacter> detectedObjectives = ObjectivesInSightSensor();
        List <GameCharacter> detectedPredators  = PredatorsInSightSensor();
        int dir;

        for (int i = 0; i < detectedObjectives.Count; i++)
        {
            dir = HexCalculator.GeneralDirectionTowards(InGamePosition, detectedObjectives[i].InGamePosition);
            proximitySensor[dir] += 1f;
        }

        for (int i = 0; i < detectedPredators.Count; i++)
        {
            dir = HexCalculator.GeneralDirectionTowards(InGamePosition, detectedPredators[i].InGamePosition);
            proximitySensor[dir] -= 1f;
        }

        return(proximitySensor);
    }
Exemplo n.º 16
0
    void Move(int dir)
    {
        // First, check if movement is possible
        // - Does the destination tile exist?
        // - Is it free?
        HexTile destinationTile;
        Vector2Int destination;

        if (BattleMap_.mapTiles[InGamePosition].Neighbors.TryGetValue(dir, out destinationTile))
        {
            if (!destinationTile.Occupied)
            {
                destination = destinationTile.Position;

                Vector3 igPosition = HexCalculator.CharacterPosition(destination);
                gameObject.GetComponent <Rigidbody>().position = igPosition;
                gameObject.transform.position = igPosition;

                BattleMap_.mapTiles[InGamePosition].EmptyTile();

                InGamePosition           = destination;
                destinationTile.Occupier = this;

                // Give a reward based on distance towards closest predator
                // This will encourage the agent to stay away from predators even if it does not
                // have a chance to live in the long run. ALSO, encourages him to move

                // The bigger the distance, the bigger the reward
                //AddReward(DistanceTowardsClosestPredator() / 10);

                return;
            }
        }

        // Position at dir has no tile to move at!
        // Position at dir is occupied!
        AddReward(-0.5f);
    }
Exemplo n.º 17
0
    /// <summary>
    /// Initializes Map Tiles and Spawn Positions based on a Map File.
    /// Chosen file range depends on the training phase the environment is in
    /// </summary>
    void InitializeMap(int mapMax)
    {
        if (mapMax > mapFiles.Count)
        {
            mapMax = mapFiles.Count;
        }

        string[] mapData = mapFiles[UnityEngine.Random.Range(0, mapMax)].text.Split(' ', '\n');

        int  maxRow = int.MinValue, maxCol = int.MinValue, minRow = int.MaxValue, minCol = int.MaxValue;
        int  row, col;
        bool isSpawn = false;
        int  faction = 0;

        Vector2Int position;
        GameObject tile;

        for (int j = 0; j < 4; j++)
        {
            spawnableTiles_.Add(new List <Vector2Int>());
        }

        int i = 0;

        while (i < mapData.Length)
        {
            for (int j = 0; j < spawnMarkers.Count; j++)
            {
                if (mapData[i].Equals(spawnMarkers[j]))
                {
                    isSpawn = true;
                    faction = j;
                    i++;
                    break;
                }
            }

            row = int.Parse(mapData[i]) + OffsetRow;
            col = int.Parse(mapData[i + 1]) + OffsetCol;

            position = new Vector2Int(row, col);
            tile     = Instantiate(hexTilePrefab, HexCalculator.Position(position.y, position.x), Quaternion.identity, this.transform);

            tile.GetComponent <HexTile>().Position = position;
            mapTiles.Add(position, tile.GetComponent <HexTile>());

            // If this is a spawn tile => Add its position into the spawnable tiles list
            if (isSpawn)
            {
                spawnableTiles_[faction].Add(position);
            }

            // map limits calculation
            if (maxRow < row)
            {
                maxRow = row;
            }
            if (maxCol < col)
            {
                maxCol = col;
            }
            if (minRow > row)
            {
                minRow = row;
            }
            if (minCol > col)
            {
                minCol = col;
            }

            isSpawn = false;
            i      += 2;
        }

        // Now, fill blanks and surroundings with obstacle tiles
        for (int x = minRow - 2; x <= maxRow + 2; x++)
        {
            for (int y = minCol - 2; y <= maxCol + 2; y++)
            {
                position = new Vector2Int(x, y);

                if (!mapTiles.ContainsKey(position))
                {
                    tile = Instantiate(obstaclePrefab, HexCalculator.Position(position.y, position.x), Quaternion.identity, this.transform);
                }
            }
        }

        // For each tile, set its neighbors in the HexTile component
        HexCalculator.SetNeighborsInMap(mapTiles);
    }
Exemplo n.º 18
0
 protected Vector2Int ClosestObjective(List <GameCharacter> detectedEnemies)
 {
     return(HexCalculator.ClosestPosition(InGamePosition, detectedEnemies));
 }