Example #1
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);
                }
            }
        }
Example #2
0
        // note to self: all this should be added to some sort of requester architecture, cf FactoryController
        // question to self: factorycontroller will be a requester in this architecture???
        // question to self: use same architecture as for factorycontroller or create separate architecture?
        void BuildSomething(int constructorid)
        {
            logfile.WriteLine("ConstructorController.BuildSomething()");
            IUnitDef radardef = buildtable.UnitDefByName["armrad"] as IUnitDef;

            if (aicallback.GetEnergy() * 100 / aicallback.GetEnergyStorage() < 10)
            {
                logfile.WriteLine("constructor building energy");
                BuildPower(constructorid);
            }
            else if (aicallback.GetMetal() * 100 / aicallback.GetMetalStorage() < 50)
            {
                logfile.WriteLine("constructor building metal");
                if (!DoReclaim(constructorid))
                {
                    logfile.WriteLine("constructor building extractor");
                    BuildExtractor(constructorid);
                }
            }
            // only build radar if we have at least one factory already
            //else if( factorycontroller.FactoryUnitDefByDeployedId.Count > 0 &&
            //    EnergyController.GetInstance().CanBuild( radardef ) &&
            //    radarcontroller.NeedRadarAt( aicallback.GetUnitPos( constructorid ) ) )
            // {
            //    BuildRadar( constructorid );
            //}
            else
            {
                double         highestpriority    = 0;
                IUnitRequester requestertosatisfy = null;
                IUnitDef       constructordef     = UnitDefByUnitId[constructorid] as IUnitDef;
                logfile.WriteLine("ConstructorController asking requesters");
                foreach (object requesterobject in Requesters)
                {
                    IUnitRequester requester = requesterobject as IUnitRequester;
                    logfile.WriteLine("ConstructorController asking " + requester.ToString());
                    double thisrequesterpriority = requester.DoYouNeedSomeUnits(new Factory(constructorid, constructordef));
                    if (thisrequesterpriority > 0 && thisrequesterpriority > highestpriority)
                    {
                        requestertosatisfy = requester;
                        highestpriority    = thisrequesterpriority;
                        logfile.WriteLine("ConstructorController: Potential requester to use " + requestertosatisfy.ToString() + " priority: " + highestpriority);
                    }
                }

                if (requestertosatisfy != null)
                {
                    IUnitDef unitdef = requestertosatisfy.WhatUnitDoYouNeed(new Factory(constructorid, constructordef));
                    logfile.WriteLine("constructorcontroller constructor " + constructorid + " going with request from " + requestertosatisfy.ToString() + " for " + unitdef.humanName);
                    requestertosatisfy.WeAreBuildingYouA(unitdef);
                    if (MetalController.GetInstance().CanBuild(unitdef))
                    {
                        if (EnergyController.GetInstance().CanBuild(unitdef))
                        {
                            Build(constructorid, unitdef);
                        }
                        else
                        {
                            BuildPower(constructorid);
                        }
                    }
                    else
                    {
                        if (!DoReclaim(constructorid))
                        {
                            BuildExtractor(constructorid);
                        }
                    }
                }
            }
        }