Пример #1
0
        public void RunScenario(IRequest request, Player player, AiInfo aiInfo)
        {
            var propId = (request as BuyAuctionRequest).PropertyId;
            var prop   = _context.GetTileComponent <Property>(propId);

            if (prop.BasePrice > player.Cash)
            {
                _context.Add(new StartAuction(propId));
                return;
            }

            var buyWeight = prop.BasePrice.PriceCashPow(player.Cash, offset: 1)
                            + _context.PropertyAcquisitionSetWeight(player, prop.SetId);

            var weights = new List <(MonopolyCommand, int)>
            {
                (MonopolyCommand.BuyProperty, buyWeight),
                (MonopolyCommand.AuctionProperty, 20)
            };

            if (weights.ChaosChoice(aiInfo.ChaosFactor) == MonopolyCommand.BuyProperty)
            {
                _context.Add(new BuyProperty(player.Id, propId));
            }
            else
            {
                _context.Add(new StartAuction(propId));
            }
        }
Пример #2
0
            public static AiInfo ReadFrom(PrimitiveReader reader)
            {
                AiInfo info = new AiInfo();

                info.Unknown = reader.ReadUInt16();
                ushort stringsCount = reader.ReadUInt16();

                System.Diagnostics.Debugger.Break();
                info.Unknown2 = reader.ReadUInt32();
                List <string> resources = new List <string>(stringsCount);

                for (int i = 0; i < stringsCount; i++)
                {
                    resources.Add(Utils.ReadUInt32LengthPrefixedString(reader));
                }
                info.Resources = resources;
                info.Unknown3  = reader.ReadBytes(6);
                AiData[] data = new AiData[8];
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = AiData.ReadFrom(reader);
                }
                info.Data = data;

                info.Unknown4 = reader.ReadBytes(104);
                AiTimer[] timers = new AiTimer[8];
                for (int i = 0; i < timers.Length; i++)
                {
                    timers[i] = AiTimer.ReadFrom(reader);
                }
                info.Timers      = timers;
                info.SharedGoals = reader.ReadUInt32Array(256);
                info.Unknown5    = reader.ReadUInt32Array(1024);
                return(info);
            }
Пример #3
0
    public float itterationTime_tracker = 0;  //s


    void Start()
    {
        spawner = GetComponent <NPCspawner>();
        foreach (GameObject item in spawner.npcList)
        {
            AiInfo nextInfo = new AiInfo(item, item.GetComponent <AiPid>(), item.GetComponent <AIDriver>().aiSettings, 0);
            nextInfo.cumulativeError = 0;
            aiInfoList.Add(nextInfo);
        }
        SetRandomPIDvalues();
    }
Пример #4
0
        public void Post(UploadFile uploadFile)
        {
            WriteBinaryToFile($"{AppPath.GetAiDirectory()}\\{uploadFile.FileName}", uploadFile.Content);
            var aiInfo = new AiInfo()
            {
                DisplayName = uploadFile.DisplayName, FileName = uploadFile.FileName
            };
            var aiInfoRepository = new AiInfoRepository();

            aiInfoRepository.Save(aiInfo);
        }
        public void RunScenario(IRequest request, Player player, AiInfo aiInfo)
        {
            var offer = (request as TradeValidationRequest).TradeOffer;

            // a point per 7$ of difference
            var weight = (CountCash(offer.InitiatorAssets) - CountCash(offer.ReceiverAssets)) / 7;

            // the following is really anti-DRY
            // prop acquisition weights
            if (offer.InitiatorAssets.Properties != null)
            {
                var setsIn = offer.InitiatorAssets.Properties
                             .GroupBy(id => _context.GetTileComponent <Property>(id).SetId);
                foreach (var set in setsIn)
                {
                    var ownedSetCardinality = _context.OwnedPropertiesInSet(player, set.Key).Count();
                    var inCardinality       = set.Count();

                    weight += SetWeightSum(ownedSetCardinality + 1, ownedSetCardinality + inCardinality);
                }
            }

            // prop giving-away-for-free-to-stupid-players weight
            if (offer.ReceiverAssets.Properties != null)
            {
                var setsOut = offer.ReceiverAssets.Properties
                              .GroupBy(id => _context.GetTileComponent <Property>(id).SetId);
                foreach (var set in setsOut)
                {
                    var ownedSetCardinality = _context.OwnedPropertiesInSet(player, set.Key).Count();
                    var outCardinality      = set.Count();

                    weight -= SetWeightSum(ownedSetCardinality - outCardinality + 1, ownedSetCardinality);
                }
            }

            int isPositive = Convert.ToInt32(weight > 0);
            var weights    = new List <(MonopolyCommand, int)>
            {
                (MonopolyCommand.AcceptOffer, isPositive *weight),
                (MonopolyCommand.DeclineOffer, -(1 - isPositive) * weight)
            };

            if (weights.ChaosChoice(aiInfo.ChaosFactor) == MonopolyCommand.AcceptOffer)
            {
                _context.Add(new TradeAccept());
            }
            else
            {
                _context.Add(new TradeRefuse());
            }
        }
Пример #6
0
        public void Save(AiInfo saveEntity)
        {
            var entitys = GetEntitys();
            var entity  = entitys.Where(m => m.FileName == saveEntity.FileName).FirstOrDefault();

            if (entity != null)
            {
                entity.DisplayName = saveEntity.DisplayName;
            }
            else
            {
                entitys.Add(saveEntity);
            }

            JsonFileIO.WriteFile(AppPath.GetAiInfosFilePath(), Encoding.UTF8, entitys);
        }
        public void RunScenario(IRequest request, Player player, AiInfo aiInfo)
        {
            Debug.WriteLine($"Running the AITurnScenario for {player.DisplayName}");
            var jailCommands = _context.GetAvailableJailCommands(player);

            if (jailCommands.Count > 0)
            {
                if (jailCommands.Contains(MonopolyCommand.UseJailCard))
                {
                    _context.AddCommand(MonopolyCommand.UseJailCard, player);
                }
                else
                {
                    if (jailCommands.Count == 1)
                    {
                        _context.AddCommand(jailCommands[0], player);
                    }
                    else
                    {
                        var jailFine = _context.GameConfig().JailFine;
                        var weights  = new List <(MonopolyCommand, int)>
                        {
                            (MonopolyCommand.JailRollDice, 6),
                            (MonopolyCommand.PayJailFine, (int)Math.Pow(2, player.Cash / jailFine))
                        };
                        _context.AddCommand(weights.ChaosChoice(aiInfo.ChaosFactor), player);
                    }
                }
                return;
            }

            if (player.CanMove)
            {
                _context.AddCommand(MonopolyCommand.MakeMove, player);
                return;
            }

            PropertyActions(player, aiInfo);
            if (ProposedTrade(player, aiInfo))
            {
                return;
            }

            Debug.WriteLine("Got to AddCommand(EndTurn)");
            _context.AddCommand(MonopolyCommand.EndTurn, player);
            aiInfo.Nullify();
        }
Пример #8
0
        public void RunScenario(IRequest request, Player player, AiInfo aiInfo)
        {
            var auction = _context.AuctionInfo();

            if (auction.AmountBid > player.Cash)
            {
                _context.Add(new AuctionWithdraw(player.Id));
                return;
            }

            var prop = _context.GetTileComponent <Property>(auction.PropertyOnAuctionId);
            var acquisitionWeight = _context.PropertyAcquisitionSetWeight(player, prop.SetId);

            var weights = new List <(int, int)>(4);

            for (int i = 1; i <= 100; i *= 10)
            {
                var newAmountBid   = auction.AmountBid + i;
                var newPriceWeight = (newAmountBid).PriceCashPow(player.Cash);
                if (newAmountBid > prop.BasePrice)
                {
                    newPriceWeight -= (int)((newAmountBid - prop.BasePrice) * 1.5f);
                }

                weights.Add((i, acquisitionWeight + Math.Max(newPriceWeight, 0)));
            }
            weights.Add((0, 20));

            var choice = weights.ChaosChoice(aiInfo.ChaosFactor);

            if (choice == 0)
            {
                _context.Add(new AuctionWithdraw(player.Id));
            }
            else
            {
                _context.Add(new AuctionBid(player.Id, choice));
            }
        }
Пример #9
0
 public AIScenario(IIndex <Type, IAIRequestScenario> index, Player player, AiInfo aiInfo)
 {
     _index  = index;
     _player = player;
     _aiInfo = aiInfo;
 }
        void PropertyActions(Player player, AiInfo aiInfo)
        {
            if (aiInfo.DidPropertyActionsThisTurn)
            {
                return;
            }

            var availableCash = player.Cash;
            var propIds       = player.Properties;
            var config        = _context.GameConfig();

            // unmortgage
            var mortgagedPropIds = propIds
                                   .Where(x => _context.GetTileComponent <Property>(x).IsMortgaged)
                                   .OrderByDescending(x => _context.GetTileComponent <Property>(x).BasePrice);

            if (mortgagedPropIds.Count() > 0)
            {
                foreach (var propId in mortgagedPropIds)
                {
                    var prop          = _context.GetTileComponent <Property>(propId);
                    var unmortgageFee = (int)(prop.BasePrice * config.MortgageFee * (1 + config.MortgageCommission));

                    if (unmortgageFee > availableCash)
                    {
                        continue;
                    }

                    var weights = new List <(MonopolyCommand, int)>
                    {
                        (MonopolyCommand.UnmortgageProperty, unmortgageFee.PriceCashPow(availableCash)),
                        (MonopolyCommand.CancelAction, 15)
                    };
                    var choice = weights.ChaosChoice(aiInfo.ChaosFactor);
                    if (choice == MonopolyCommand.CancelAction)
                    {
                        continue;
                    }

                    _context.UnMortgage(player, propId);
                    availableCash -= unmortgageFee;
                }
            }

            // buyHouses
            var boughtHouses = false;

            do
            {
                boughtHouses = false;
                foreach (var propId in propIds)
                {
                    if (!_context.CanBuildHouse(player, propId))
                    {
                        continue;
                    }

                    var dev = _context.GetTileComponent <PropertyDevelopment>(propId);
                    if (dev.HouseBuyPrice > availableCash)
                    {
                        continue;
                    }

                    var weights = new List <(MonopolyCommand, int)>
                    {
                        (MonopolyCommand.BuyHouse, dev.HouseBuyPrice.PriceCashPow(availableCash)),
                        (MonopolyCommand.CancelAction, 15)
                    };
                    if (weights.ChaosChoice(aiInfo.ChaosFactor) == MonopolyCommand.CancelAction)
                    {
                        continue;
                    }

                    _context.BuyHouse(player, propId);
                    availableCash -= dev.HouseBuyPrice;
                    boughtHouses   = true;
                }
            } while (boughtHouses);

            aiInfo.DidPropertyActionsThisTurn = true;
        }
        bool ProposedTrade(Player player, AiInfo aiInfo)
        {
            var setsMissingAProperty = _context.SetsMissingProperties(player);

            if (setsMissingAProperty.Count == 0)
            {
                return(false);
            }

            var possibleTraders = _context.GetAllPlayers()
                                  .Where(p => p.Id != player.Id)
                                  .Where(p => !aiInfo.TradeCooldowns.ContainsKey(p.Id));

            foreach (var trader in possibleTraders)
            {
                var interestingTradables = _context.TradableProperties(trader.Id)
                                           .Where(propId => setsMissingAProperty.Contains(_context.GetTileComponent <Property>(propId).SetId))
                                           .ToList();
                if (interestingTradables.Count == 0)
                {
                    continue;
                }

                var receiverAssetsSum = interestingTradables.Sum(x => _context.GetTileComponent <Property>(x).BasePrice);
                if (0.75f * receiverAssetsSum > player.Cash)
                {
                    continue;
                }

                var receiverAssets = new PlayerAssets(trader.Id, interestingTradables, 0, 0);
                var weights        = new List <(int, int)>(8);
                weights.Add((-1, 70));

                for (float coeff = 0.75f; coeff < 1.1f; coeff += 0.05f)
                {
                    var moneyGiven = receiverAssetsSum * coeff;
                    if (moneyGiven > player.Cash)
                    {
                        break;
                    }

                    var weight = (int)((coeff - 1) * 120);
                    weight += (int)(player.Cash / moneyGiven * 25);

                    weights.Add(((int)(moneyGiven), weight));
                }
                var choice = weights.ChaosChoice(aiInfo.ChaosFactor);
                if (choice == -1)
                {
                    continue;
                }

                var initiatorAssets = new PlayerAssets(player.Id, null, choice, 0);
                var offer           = new TradeOffer(initiatorAssets, receiverAssets);

                _context.Add(offer);
                aiInfo.TradeCooldowns.Add(trader.Id, 4);

                return(true);
            }

            return(false);
        }
        public void RunScenario(IRequest request, Player player, AiInfo aiInfo)
        {
            var debtLeft = (request as PayOffDebtRequest).DebtAmount - player.Cash;
            var config   = _context.GameConfig();

            // cycles while there is still any debt left
            while (true)
            {
                var availableProperties = player.Properties
                                          .Where(propId => !_context.GetTileComponent <Property>(propId).IsMortgaged)
                                          .GroupBy(propId => {
                    var dev = _context.GetTileComponent <PropertyDevelopment>(propId);
                    if (dev == null)
                    {
                        return(false);
                    }
                    if (dev.HousesBuilt == 0)
                    {
                        return(false);
                    }
                    return(true);
                })
                                          .OrderByDescending(group => group.Key);

                // separate groups for withDev and without
                foreach (var group in availableProperties)
                {
                    foreach (var propId in group)
                    {
                        var prop = _context.GetTileComponent <Property>(propId);

                        // sell all the houses you can
                        if (group.Key == true)
                        {
                            var dev = _context.GetTileComponent <PropertyDevelopment>(propId);
                            while (_context.CanSellHouse(player, propId))
                            {
                                _context.SellHouse(player, propId);
                                debtLeft -= dev.HouseSellPrice;
                                if (debtLeft <= 0)
                                {
                                    _context.Add(new PaidOffDebt(player.Id));
                                    return;
                                }
                            }
                        }

                        if (_context.CanMortgage(player, propId))
                        {
                            _context.Mortgage(player, propId);
                            debtLeft -= (int)(prop.BasePrice * config.MortgageFee);
                            if (debtLeft <= 0)
                            {
                                _context.Add(new PaidOffDebt(player.Id));
                                return;
                            }
                        }
                    }
                }
            }
        }
Пример #13
0
        public void ReadData(string path)
        {
            PrimitiveReader reader = new PrimitiveReader(path);
            SaveGameFile    file   = new SaveGameFile();

            file.Version  = reader.ReadASCIIString(8);
            file.Unknown1 = reader.ReadFloat32();

            bool aiDataIncluded = reader.ReadUInt32() != 0;

            if (aiDataIncluded)
            {
                file.AiInfo2 = AiInfo.ReadFrom(reader);
            }
            file.Unknown2   = reader.ReadUInt32();
            file.GameSpeed1 = reader.ReadUInt32();
            file.Unknown3   = reader.ReadUInt32();
            file.GameSpeed2 = reader.ReadUInt32();
            file.Unknown4   = reader.ReadFloat32();
            file.Unknown5   = reader.ReadUInt32();
            file.Unknown6   = reader.ReadBytes(17);
            file.RecordedGamePlayerNumber = reader.ReadUInt16();
            //System.Diagnostics.Debugger.Break();
            file.PlayersCount = reader.ReadUInt8();
            file.Unknown7     = reader.ReadUInt32();
            file.Unknown8     = reader.ReadBytes(12);
            file.Unknown9     = reader.ReadBytes(14);
            file.Unknown10    = reader.ReadUInt32Array(8);

            //System.Diagnostics.Debugger.Break();

            reader.SeekAbsolute(126);
            int mapWidth  = reader.ReadInt32();
            int mapLength = reader.ReadInt32();

            uint unknownDataCount = reader.ReadUInt32();

            ushort unknown2 = reader.ReadUInt16();

            SaveGameMap map = SaveGameMap.ReadFrom(reader, mapWidth, mapLength);


            /*int unknownIntsArrayCount = reader.ReadInt32();
             * uint[][] unknowns = new uint[unknownIntsArrayCount][];
             * for( int i = 0; i < unknowns.Length; i++ ) {
             *      int intsCount = reader.ReadInt32() - 1;
             *      if( intsCount < 0 ) throw new Exception();
             *      unknowns[i] = reader.ReadUInt32Array( intsCount );
             * }*/
            reader.SeekAbsolute(29132);

            int mapWidth2  = reader.ReadInt32();
            int mapLength2 = reader.ReadInt32();

            uint[] unknownMap2 = reader.ReadUInt32Array(mapWidth2 * mapLength2);

            //byte unknownIntsCount1 = reader.ReadUInt8();
            //uint[] unknownInts = reader.ReadUInt32Array( unknownIntsCount1 );

            reader.SeekAbsolute(88779);
            SavePlayerInfo f = SavePlayerInfo.ReadFrom(reader, 2);

            System.Diagnostics.Debugger.Break();
        }