Пример #1
0
        public void Activate()
        {
            UnitController.GetInstance();
            EnemyController.GetInstance();
            FriendlyUnitPositionObserver.GetInstance();

            MovementMaps.GetInstance();
            BuildMap.GetInstance();
            Metal.GetInstance().Init();
            LosMap.GetInstance();
            BuildPlanner.GetInstance();

            BuildTable.GetInstance();

            CommanderList.GetInstance();
            Level1ConstructorList.GetInstance();
            Level1FactoryList.GetInstance();

            tankcontroller = new TankController(TankList.GetInstance().defbyid, BuildTable.GetInstance().UnitDefByName["armstump"]);
            tankcontroller.Activate();
            helicoptercontroller = new TankController(HelicopterList.GetInstance().defbyid, BuildTable.GetInstance().UnitDefByName["armbrawl"]);
            helicoptercontroller.Activate();
            new ScoutControllerRaider().Activate();

            UnitController.GetInstance().LoadExistingUnits();
            EnemyController.GetInstance().LoadExistingUnits();

            CheckIdleUnits();

            //BuildSolarCell(CommanderList.GetInstance().defbyid.Keys.GetEnumerator().Current);

            csai.UnitIdleEvent += new CSAI.UnitIdleHandler(csai_UnitIdleEvent);
            csai.TickEvent     += new CSAI.TickHandler(csai_TickEvent);
        }
Пример #2
0
        void csai_UnitIdleEvent(int deployedunitid)
        {
            IUnitDef unitdef = UnitController.GetInstance().UnitDefByDeployedId[deployedunitid];

            if (!unitdef.canBuild)
            {
                //logfile.WriteLine("cantbuild");
                return;
            }
            logfile.WriteLine("unitidleevent " + deployedunitid + " " + unitdef.name + " " + unitdef.humanName);
            if (ActiveConstructors.Contains(deployedunitid))
            {
                ActiveConstructors.Remove(deployedunitid);
            }
            Ownership.GetInstance().SignalConstructorIsIdle(deployedunitid);
            double       highestpriority = 0;
            List <Order> bestorders      = new List <Order>();

            foreach (Order order in orders)
            {
                double thispriority = order.priority;
                if (thispriority >= highestpriority)
                {
                    int currentunits = order.unitsunderconstruction.Count;
                    if (UnitController.GetInstance().UnitDefsByName.ContainsKey(order.unitname))
                    {
                        currentunits += UnitController.GetInstance().UnitDefsByName[order.unitname].Count;
                    }
                    if (currentunits < order.quantity)
                    {
                        if (BuildTree.GetInstance().CanBuild(unitdef.name.ToLower(), order.unitname))
                        {
                            //if( CanBuild(deployedunitid,
                            if (thispriority > highestpriority)
                            {
                                highestpriority = thispriority;
                                bestorders      = new List <Order>();
                                bestorders.Add(order);
                                csai.DebugSay("Possible order: " + order.ToString());
                            }
                            else if (thispriority == highestpriority)
                            {
                                bestorders.Add(order);
                                csai.DebugSay("Possible order: " + order.ToString());
                            }
                        }
                    }
                }
            }
            //if (bestorders.Count == 0)
            //  {
            //      csai.DebugSay("No orders found");
            //      return;
            //  }
            List <Order> possibleorders = new List <Order>(); // get orders this unit can build
            bool         metalneeded    = false;
            bool         energyneeded   = false;
            IUnitDef     deftobuild     = null;

            foreach (Order order in bestorders)
            {
                csai.DebugSay("bestorder " + order.unitname);
                //if( BuildTree.GetInstance().CanBuild( unitdef.name.ToLower(), order.unitname ) )
                //{
                deftobuild = BuildTable.GetInstance().UnitDefByName[order.unitname];
                if (MetalController.GetInstance().CanBuild(deftobuild))
                {
                    if (EnergyController.GetInstance().CanBuild(deftobuild))
                    {
                        possibleorders.Add(order);
                        csai.DebugSay("possible: " + order.unitname);
                    }
                    else
                    {
                        csai.DebugSay("needs energy");
                        energyneeded = true;
                    }
                }
                else
                {
                    csai.DebugSay("needs metal");
                    metalneeded = true;
                }
                //}
            }
            if (possibleorders.Count == 0)
            {
                if (Level1ConstructorList.GetInstance().defbyid.Count < 1 &&
                    !UnitController.GetInstance().UnitDefsByName.ContainsKey("armcom"))
                {
                    if (BuildConstructionVehicle(deployedunitid, unitdef))
                    {
                        return;
                    }
                }

                if (energyneeded || aicallback.GetEnergy() < aicallback.GetEnergyStorage() / 5)
                {
                    if (BuildSolarCell(deployedunitid))
                    {
                        logfile.WriteLine("building solarcell");
                        if (AssistingConstructors.ContainsKey(deployedunitid))
                        {
                            AssistingConstructors.Remove(deployedunitid);
                        }
                        return;
                    }
                }
                if (metalneeded || aicallback.GetMetal() < aicallback.GetMetalStorage() / 5)
                {
                    Float3 reclaimpos = ReclaimHelper.GetNearestReclaim(aicallback.GetUnitPos(deployedunitid), deployedunitid);
                    if (reclaimpos != null)
                    {
                        GiveOrderWrapper.GetInstance().Reclaim(deployedunitid, reclaimpos, 100);
                        return;
                    }
                    if (BuildMex(deployedunitid))
                    {
                        logfile.WriteLine("building mex");
                        if (AssistingConstructors.ContainsKey(deployedunitid))
                        {
                            AssistingConstructors.Remove(deployedunitid);
                        }
                        return;
                    }
                }


                logfile.WriteLine("offering assistance");
                OfferAssistance(deployedunitid);
                return;
            }
            Order ordertodo = possibleorders[random.Next(0, possibleorders.Count)];

            if (ordertodo.unitname == "armmex")
            {
                BuildMex(deployedunitid);
                if (AssistingConstructors.ContainsKey(deployedunitid))
                {
                    AssistingConstructors.Remove(deployedunitid);
                }
            }
            else
            {
                //ordertodo.unitsunderconstruction += 1;
                deftobuild = BuildTable.GetInstance().UnitDefByName[ordertodo.unitname];
                Float3           pos            = BuildUnit(deployedunitid, ordertodo.unitname);
                Ownership.IOrder ownershiporder = Ownership.GetInstance().RegisterBuildingOrder(this,
                                                                                                deployedunitid,
                                                                                                deftobuild, pos);
                logfile.WriteLine("building: " + ordertodo.unitname);
                ordertodo.unitsunderconstruction.Add(ownershiporder);
                if (AssistingConstructors.ContainsKey(deployedunitid))
                {
                    AssistingConstructors.Remove(deployedunitid);
                }
            }
        }