Exemplo n.º 1
0
        void Recoordinate()
        {
            if (!started)
            {
                return;
            }

            // first we scan the list to find the 5 closest units to target, in order
            // next we measure distance of first unit from 5th
            // if distance is less than MaxPackDiameter, we move in
            // otherwise 5th unit holds, and we move units to position of 5th unit
            // pack must have at least 5 units, otherwise we call AttackPackDead event

            if (!CheckIfPackAlive())
            {
                return;
            }

            int packsize = Math.Min(MaxPackToConsider, UnitDefListByDeployedId.Count);

            UnitInfo[] closestunits        = GetClosestUnits(UnitDefListByDeployedId, targetpos, packsize);
            Float3     packheadpos         = closestunits[0].pos;
            Float3     packtailpos         = closestunits[packsize - 1].pos;
            double     packsquareddiameter = Float3Helper.GetSquaredDistance(packheadpos, packtailpos);

            csai.DebugSay("packsize: " + packsize + " packdiameter: " + Math.Sqrt(packsquareddiameter));
            // logfile.WriteLine( "AttackPackCoordinator packheadpos " + packheadpos.ToString() + " packtailpos " + packtailpos.ToString() + " packsquareddiamter " + packsquareddiameter );
            if ((regrouping && (packsquareddiameter < (MaxPackDiameterInnerThreshold * MaxPackDiameterInnerThreshold))) ||
                (!regrouping && (packsquareddiameter < (MaxPackDiameterOuterThreshold * MaxPackDiameterOuterThreshold))))
            {
                regrouping = false;
                csai.DebugSay("attacking");
                Attack(closestunits);
            }
            else
            {
                csai.DebugSay("regrouping");
                regrouping = true;
                Regroup(closestunits[(packsize / 2)].pos);
            }
        }
Exemplo n.º 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);
                }
            }
        }