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; } } }
public void UnitRemoved(int deployedid) { if (!isMetalMap) { if (Extractors.Contains(deployedid)) { Float3 mexpos = Extractors[deployedid] as Float3; logfile.WriteLine("Metal.UnitRemoved, pos " + mexpos.ToString()); double squareextractorradius = ExtractorRadius * ExtractorRadius; foreach (MetalSpot metalspot in MetalSpots) { if (Float3Helper.GetSquaredDistance(metalspot.Pos, mexpos) < squareextractorradius) { if (MetalSpotsUsed.Contains(metalspot)) { logfile.WriteLine("Marking metal spot free: " + metalspot.Pos.ToString()); MetalSpotsUsed.Remove(metalspot); } } } Extractors.Remove(deployedid); } } }
// this is going to have to interact with all sorts of stuff in the future // for now keep it simple // for now we look for nearby buildings, then nearby enemy units with low speed, then anything public Float3 ChooseAttackPoint(Float3 ourpos) { bool gotbuilding = false; bool gotknownunit = false; double BestSquaredDistance = 100000000000; int bestid = 0; IUnitDef defforbestid = null; Float3 posforbestid = null; // logfile.WriteLine( "EnemySelector: checking mobile... " ); foreach (KeyValuePair <int, IUnitDef> kvp in enemycontroller.EnemyUnitDefByDeployedId) { int thisenemyid = kvp.Key; IUnitDef unitdef = kvp.Value; Float3 enemypos = aicallback.GetUnitPos(thisenemyid); //Float3 enemypos = EnemyMap.GetInstance(). // logfile.WriteLine( "Found building " + if (MovementMaps.GetInstance().GetArea(typicalunitdef, enemypos) == startarea) { if (Float3Helper.GetSquaredDistance(new Float3(0, 0, 0), enemypos) > 1) { double thissquareddistance = Float3Helper.GetSquaredDistance(ourpos, enemypos); // logfile.WriteLine( "EnemySelector: Potential enemy at " + enemypos.ToString() + " squareddistance: " + thissquareddistance ); if (unitdef != null) { // logfile.WriteLine( "unitdef not null " + unitdef.humanName + " ismobile: " + unitdefhelp.IsMobile( unitdef ).ToString() ); // logfile.WriteLine( "gotbuilding = " + gotbuilding.ToString() ); if (gotbuilding) { if (!unitdefhelp.IsMobile(unitdef)) { if (thissquareddistance < BestSquaredDistance) { // logfile.WriteLine( "best building so far" ); bestid = thisenemyid; gotbuilding = true; gotknownunit = true; posforbestid = enemypos; defforbestid = unitdef; BestSquaredDistance = thissquareddistance; } } } else { if (unitdef.speed < maxenemyspeed) // if we already have building we dont care { if (thissquareddistance < BestSquaredDistance) { // logfile.WriteLine( "best known so far" ); bestid = thisenemyid; gotknownunit = true; posforbestid = enemypos; defforbestid = unitdef; BestSquaredDistance = thissquareddistance; } } } } else // if unitdef unknown { // logfile.WriteLine( "gotknownunit = " + gotknownunit.ToString() ); if (!gotknownunit) // otherwise just ignore unknown units { if (thissquareddistance < BestSquaredDistance) { // logfile.WriteLine( "best unknown so far" ); bestid = thisenemyid; posforbestid = enemypos; defforbestid = unitdef; BestSquaredDistance = thissquareddistance; } } } } } } foreach (KeyValuePair <int, Float3> kvp in enemycontroller.EnemyStaticPosByDeployedId) { // logfile.WriteLine( "EnemySelector: checking static... " ); int thisenemyid = kvp.Key; Float3 enemypos = kvp.Value; double thissquareddistance = Float3Helper.GetSquaredDistance(ourpos, enemypos); // logfile.WriteLine( "EnemySelector: Potential enemy at " + enemypos.ToString() + " squareddistance: " + thissquareddistance ); if (thissquareddistance < BestSquaredDistance) { // logfile.WriteLine( "EnemySelector: best distance so far" ); bestid = thisenemyid; gotbuilding = true; gotknownunit = true; posforbestid = enemypos; //defforbestid = unitdef; } } //if( enemycontroller.EnemyStaticPosByDeployedId.Contains( bestid ) ) // { // enemycontroller.EnemyStaticPosByDeployedId.Remove( bestid ); // } return(posforbestid); }
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 Reappraise() { foreach (DictionaryEntry scoutde in ScoutUnitDefsById) { int scoutdeployedid = (int)scoutde.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 (DictionaryEntry de in EnemyController.GetInstance().EnemyUnitDefByDeployedId) { int deployedid = (int)de.Key; IUnitDef unitdef = de.Value as IUnitDef; if (unitdef != null) { Float3 thispos = aicallback.GetUnitPos(deployedid); if (IsPriorityTarget(unitdef)) { double thissquareddistance = Float3Helper.GetSquaredDistance(scoutpos, thispos); if (thissquareddistance < bestsquareddistance) { bool nolasertowersnear = true; foreach (DictionaryEntry attackerde in EnemyController.GetInstance().EnemyUnitDefByDeployedId) { int attackerid = (int)attackerde.Key; IUnitDef attackerunitdef = attackerde.Value as IUnitDef; 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) { aicallback.GiveOrder(scoutdeployedid, new Command(Command.CMD_ATTACK, new double[] { targetid })); if (!attackingscouts.Contains(scoutdeployedid)) { attackingscouts.Add(scoutdeployedid); } } else { if (attackingscouts.Contains(scoutdeployedid)) { ExploreWith(scoutdeployedid); attackingscouts.Remove(scoutdeployedid); } } } }
public static Float3 GetNearestReclaim(Float3 mypos, int constructorid) { if (CSAI.GetInstance().aicallback.GetCurrentFrame() == 0) // check ticks first, beacuse metal shows as zero at start { return(null); } LogFile logfile = LogFile.GetInstance(); IAICallback aicallback = CSAI.GetInstance().aicallback; IUnitDef unitdef = UnitController.GetInstance().UnitDefByDeployedId[constructorid]; if (!new UnitDefHelp(aicallback).IsMobile(unitdef)) { return(null); } //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.GetInstance().DebugOn) { aicallback.DrawUnit("ARMMEX", reclaimpos, 0.0f, 200, aicallback.GetMyAllyTeam(), true, true); } return(reclaimpos); //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(null); } }