bool BuildConstructionVehicle(int constructorid, IUnitDef constructordef) { IUnitDef deftobuild = null; if (BuildTree.GetInstance().CanBuild(constructordef.name.ToLower(), "armcv")) { deftobuild = BuildTable.GetInstance().UnitDefByName["armcv"]; } else if (BuildTree.GetInstance().CanBuild(constructordef.name.ToLower(), "armacv")) { deftobuild = BuildTable.GetInstance().UnitDefByName["armacv"]; } else { return(false); } Float3 pos = BuildUnit(constructorid, deftobuild.name.ToLower()); Ownership.IOrder ownershiporder = Ownership.GetInstance().RegisterBuildingOrder(this, constructorid, deftobuild, pos); logfile.WriteLine("building: " + deftobuild.name.ToLower()); //ordertodo.unitsunderconstruction.Add(ownershiporder); if (AssistingConstructors.ContainsKey(constructorid)) { AssistingConstructors.Remove(constructorid); } return(true); }
bool BuildMex(int constructorid) { if (!BuildTree.GetInstance().CanBuild(UnitController.GetInstance().UnitDefByDeployedId[constructorid].name.ToLower(), "armmex")) { return(false); } IUnitDef unitdef = BuildTable.GetInstance().UnitDefByName["armmex"]; Float3 buildsite = Metal.GetInstance().GetNearestMetalSpot(aicallback.GetUnitPos(constructorid)); buildsite = aicallback.ClosestBuildSite(unitdef, buildsite, 100, 0); if (!ActiveConstructors.Contains(constructorid)) { ActiveConstructors.Add(constructorid); } //aicallback.GiveOrder(constructorid, new Command(-unitdef.id, buildsite.ToDoubleArray())); GiveOrderWrapper.GetInstance().BuildUnit(constructorid, unitdef.name, buildsite); return(true); }
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); } } }