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 ExploreWith(int unitid) { Float3 destination = new Float3(); // if( PriorityTargets.Count > 0 ) // { // destination = PriorityTargets.Dequeue() as Float3; // logfile.WriteLine( "dequeued next destination: " + destination.ToString() ); // } // else // { destination.x = random.Next(0, aicallback.GetMapWidth() * MovementMaps.SQUARE_SIZE); destination.z = random.Next(0, aicallback.GetMapHeight() * MovementMaps.SQUARE_SIZE); destination.y = aicallback.GetElevation(destination.x, destination.y); logfile.WriteLine("mapwidth: " + aicallback.GetMapWidth() + " squaresize: " + MovementMaps.SQUARE_SIZE); logfile.WriteLine("ScoutController sending scout " + unitid + " to " + destination.ToString()); // } //aicallback.GiveOrder( unitid, new Command( Command.CMD_MOVE, destination.ToDoubleArray() ) ); GiveOrderWrapper.GetInstance().MoveTo(unitid, destination); }
void MoveTo(Float3 pos) { // check whether we really need to do anything or if order is roughly same as last one if (csai.DebugOn) { aicallback.DrawUnit("ARMSOLAR", pos, 0.0f, 50, aicallback.GetMyAllyTeam(), true, true); } if (restartedfrompause || Float3Helper.GetSquaredDistance(pos, lasttargetpos) > (movetothreshold * movetothreshold)) { foreach (int deployedid in UnitDefListByDeployedId.Keys) { //int deployedid = (int)de.Key; //IUnitDef unitdef = de.Value as IUnitDef; GiveOrderWrapper.GetInstance().MoveTo(deployedid, pos); //aicallback.GiveOrder( deployedid, new Command( Command.CMD_MOVE, pos.ToDoubleArray() ) ); } restartedfrompause = false; lasttargetpos = pos; } }
Float3 BuildUnit(int constructorid, string targetunitname) { csai.DebugSay("workflow, building " + targetunitname); IUnitDef targetunitdef = BuildTable.GetInstance().UnitDefByName[targetunitname]; IUnitDef constructordef = UnitController.GetInstance().UnitDefByDeployedId[constructorid]; if (new UnitDefHelp(aicallback).IsMobile(constructordef)) { logfile.WriteLine("constructor is mobile"); Float3 constructorpos = aicallback.GetUnitPos(constructorid); Float3 buildsite = BuildPlanner.GetInstance().ClosestBuildSite(targetunitdef, constructorpos, 3000, 2); buildsite = aicallback.ClosestBuildSite(targetunitdef, buildsite, 1400, 0); logfile.WriteLine("constructor location: " + constructorpos.ToString() + " Buildsite: " + buildsite.ToString() + " target item: " + targetunitdef.humanName); if (!ActiveConstructors.Contains(constructorid)) { ActiveConstructors.Add(constructorid); } aicallback.DrawUnit(targetunitname.ToUpper(), buildsite, 0.0, 200, aicallback.GetMyAllyTeam(), true, true); GiveOrderWrapper.GetInstance().BuildUnit(constructorid, targetunitname, buildsite); return(buildsite); } else { Float3 factorypos = aicallback.GetUnitPos(constructorid); logfile.WriteLine("factory location: " + factorypos.ToString() + " target item: " + targetunitdef.humanName); if (!ActiveConstructors.Contains(constructorid)) { ActiveConstructors.Add(constructorid); } aicallback.DrawUnit(targetunitdef.name.ToUpper(), factorypos, 0.0, 200, aicallback.GetMyAllyTeam(), true, true); GiveOrderWrapper.GetInstance().BuildUnit(constructorid, targetunitname); return(factorypos); } }
void OfferAssistance(int constructorid) { int bestconstructor = 0; double bestsquareddistance = 1000000000; Float3 ourpos = aicallback.GetUnitPos(constructorid); foreach (int activeconstructor in ActiveConstructors) { Float3 constructorpos = aicallback.GetUnitPos(activeconstructor); if (constructorpos != null) { double thissquareddistance = Float3Helper.GetSquaredDistance(ourpos, constructorpos); if (thissquareddistance < bestsquareddistance) { bestconstructor = activeconstructor; bestsquareddistance = thissquareddistance; } } } if (bestconstructor != 0) { logfile.WriteLine("unit to assist should be " + bestconstructor); if (!AssistingConstructors.ContainsKey(constructorid)) { logfile.WriteLine("assisting " + bestconstructor); GiveOrderWrapper.GetInstance().Guard(constructorid, bestconstructor); AssistingConstructors.Add(constructorid, bestconstructor); } else if (AssistingConstructors[constructorid] != bestconstructor) { logfile.WriteLine("assisting " + bestconstructor); GiveOrderWrapper.GetInstance().Guard(constructorid, bestconstructor); AssistingConstructors[constructorid] = bestconstructor; } } }
void Reappraise() { // logfile.WriteLine("reappraise>>>"); foreach (KeyValuePair <int, IUnitDef> scoutkvp in ScoutUnitDefsById) { int scoutdeployedid = scoutkvp.Key; Float3 scoutpos = aicallback.GetUnitPos(scoutdeployedid); Float3 nearestpos = null; double bestsquareddistance = 100000000; int targetid = 0; // need to add index by position for this, to speed things up foreach (KeyValuePair <int, IUnitDef> enemykvp in EnemyController.GetInstance().EnemyUnitDefByDeployedId) { int deployedid = enemykvp.Key; IUnitDef unitdef = enemykvp.Value; if (unitdef != null) { if (IsPriorityTarget(unitdef)) { logfile.WriteLine("considering unit " + deployedid + " " + unitdef.name); Float3 thispos = aicallback.GetUnitPos(deployedid); double thissquareddistance = Float3Helper.GetSquaredDistance(scoutpos, thispos); if (thissquareddistance < bestsquareddistance) { bool nolasertowersnear = true; foreach (KeyValuePair <int, IUnitDef> attackerkvp in EnemyController.GetInstance().EnemyUnitDefByDeployedId) { int attackerid = attackerkvp.Key; IUnitDef attackerunitdef = attackerkvp.Value; if (attackerunitdef != null) { if (IsLaserTower(attackerunitdef)) { Float3 attackerpos = aicallback.GetUnitPos(attackerid); if (Float3Helper.GetSquaredDistance(attackerpos, thispos) < nearbyforenemiesmeans * nearbyforenemiesmeans) { nolasertowersnear = false; } } } } if (nolasertowersnear) { nearestpos = thispos; bestsquareddistance = thissquareddistance; targetid = deployedid; } } } } } if (nearestpos != null) { GiveOrderWrapper.GetInstance().Attack(scoutdeployedid, targetid); if (!attackingscouts.Contains(scoutdeployedid)) { attackingscouts.Add(scoutdeployedid); } } else { if (attackingscouts.Contains(scoutdeployedid)) { ExploreWith(scoutdeployedid); attackingscouts.Remove(scoutdeployedid); } } } // logfile.WriteLine("reappraise<<<"); }
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); } } }
void Move(int unitid) { GiveOrderWrapper.GetInstance().MoveTo(unitid, targetpos); //aicallback.GiveOrder( unitid, new Command( Command.CMD_MOVE, targetpos.ToDoubleArray() ) ); }
public void ExploreWith(int unitid) { if (lastexploretime.ContainsKey(unitid) && aicallback.GetCurrentFrame() - lastexploretime[unitid] < 30) { return; } bool destinationfound = false; Float3 currentpos = aicallback.GetUnitPos(unitid); MovementMaps movementmaps = MovementMaps.GetInstance(); IUnitDef unitdef = UnitDefListByDeployedId[unitid] as IUnitDef; int currentarea = movementmaps.GetArea(unitdef, currentpos); LosMap losmap = LosMap.GetInstance(); if (csai.DebugOn) { logfile.WriteLine("SpreadSearchWithSearchGrid explorewith unit " + unitid + " " + unitdef.humanName + " area: " + currentarea); } /* * int numtriesleft = 30; // just try a few times then give up * // maybe there is a better way to do this? * while( !destinationfound ) * { * Float3 destination = GetRandomDestination(); * // logfile.WriteLine( "SpreadSearchWithSearchGrid attempt " + destination.ToString() ); * int mapx = (int)( destination.x / 16 ); * int mapy = (int)( destination.z / 16 ); * if( ( movementmaps.GetArea( unitdef, destination ) == currentarea && * losmap.LastSeenFrameCount[ mapx, mapy ] < recentmeansnumframes || numtriesleft <= 0 ) ) * { * logfile.WriteLine( "Looks good. Go. " + numtriesleft + " retriesleft" ); * if( csai.DebugOn ) * { * aicallback.CreateLineFigure( currentpos, destination,10,true,400,0); * aicallback.DrawUnit( "ARMFAV", destination, 0.0f, 400, aicallback.GetMyAllyTeam(), true, true); * } * aicallback.GiveOrder( unitid, new Command( Command.CMD_MOVE, destination.ToDoubleArray() ) ); * return; * } * numtriesleft--; * } */ // find nearest, area that hasnt had los recently //int maxradius = Math.Max( aicallback.GetMapWidth(), aicallback.GetMapHeight() ) / 2; //for( int radius = Float3 nextpoint = new LosHelper().GetNearestUnseen(currentpos, unitdef, 12000); if (nextpoint == null) { nextpoint = GetRandomDestination(); } if (!lastexploretime.ContainsKey(unitid)) { lastexploretime.Add(unitid, aicallback.GetCurrentFrame()); } else { lastexploretime[unitid] = aicallback.GetCurrentFrame(); } GiveOrderWrapper.GetInstance().MoveTo(unitid, nextpoint); }
void ExploreWith(int unitid) { Float3 destination = GetRandomDestination(); GiveOrderWrapper.GetInstance().MoveTo(unitid, destination); }
void SetGuard(int unitid) { GiveOrderWrapper.GetInstance().Guard(unitid, targetid); //aicallback.GiveOrder( unitid, new Command( Command.CMD_GUARD, new double[]{ targetid } ) ); }