bool DoReclaim(int constructorid) { if (totalticks == 0) // check ticks first, beacuse metal shows as zero at start { return(false); } IUnitDef unitdef = UnitDefByUnitId[constructorid] as IUnitDef; Float3 mypos = aicallback.GetUnitPos(constructorid); MovementMaps movementmaps = MovementMaps.GetInstance(); int currentarea = movementmaps.GetArea(unitdef, mypos); //double nearestreclaimdistancesquared = 1000000; //Float3 nearestreclaimpos = null; double bestmetaldistanceratio = 0; int bestreclaimid = 0; int metalspace = (int)(aicallback.GetMetalStorage() - aicallback.GetMetal()); logfile.WriteLine("available space in metal storage: " + metalspace); int[] nearbyfeatures = aicallback.GetFeatures(mypos, maxreclaimradius); bool reclaimfound = false; foreach (int feature in nearbyfeatures) { IFeatureDef featuredef = aicallback.GetFeatureDef(feature); if (featuredef.metal > 0 && featuredef.metal <= metalspace) { Float3 thisfeaturepos = aicallback.GetFeaturePos(feature); double thisdistance = Math.Sqrt(Float3Helper.GetSquaredDistance(thisfeaturepos, mypos)); double thismetaldistanceratio = featuredef.metal / thisdistance; if (thismetaldistanceratio > bestmetaldistanceratio && movementmaps.GetArea(unitdef, thisfeaturepos) == currentarea) { logfile.WriteLine("Potential reclaim, distance = " + thisdistance + " metal = " + featuredef.metal + " ratio = " + thismetaldistanceratio); bestmetaldistanceratio = thismetaldistanceratio; bestreclaimid = feature; // nearestreclaimpo reclaimfound = true; } } } if (reclaimfound && (bestmetaldistanceratio > (1.0 / (100 * reclaimradiusperonehundredmetal)))) { Float3 reclaimpos = aicallback.GetFeaturePos(bestreclaimid); logfile.WriteLine("Reclaim found, pos " + reclaimpos.ToString()); if (csai.DebugOn) { aicallback.DrawUnit("ARMMEX", reclaimpos, 0.0f, 200, aicallback.GetMyAllyTeam(), true, true); } aicallback.GiveOrder(constructorid, new Command(Command.CMD_RECLAIM, new double[] { reclaimpos.x, reclaimpos.y, reclaimpos.z, 10 })); } else { logfile.WriteLine("No reclaim within parameters"); } return(reclaimfound); }
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); } } }