bool DoReclaim( int constructorid ) { if( totalticks == 0 )// check ticks first, beacuse metal shows as zero at start { return false; } Float3 mypos = aicallback.GetUnitPos( constructorid ); //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 ) { 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( BuildTable.ArmMex.ToUpper(), 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; }
public void DumpFeaturesMap(string cmd, string[] cmdsplit, int player) { int featuremappospersquare = 64; int width = aicallback.GetMapWidth() * 8 / featuremappospersquare; int height = aicallback.GetMapHeight() * 8 / featuremappospersquare; bool[,] featuremap = new bool[width, height]; logfile.WriteLine("requesting features"); int[] featureids = aicallback.GetFeatures(); logfile.WriteLine("got features"); foreach (int featureid in featureids) { Float3 pos = aicallback.GetFeaturePos(featureid); int featuremapposx = (int)(pos.x / featuremappospersquare); // implies Floor int featuremapposy = (int)(pos.z / featuremappospersquare); // implies Floor IFeatureDef featuredef = aicallback.GetFeatureDef(featureid); if (featuredef.metal > 0) { featuremap[featuremapposx, featuremapposy] = true; } } for (int y = 0; y < height; y += 1) { string line = ""; for (int x = 0; x < width; x += 1) { if (featuremap[x, y]) { // aicallback.DrawUnit( "ARMRAD", new Float3( x * 64, 0, y * 64 ), 0.0f, 100, aicallback.GetMyAllyTeam(), true, true); line += "*"; } else { line += "-"; } } logfile.WriteLine(line); } aicallback.SendTextMsg("features dumped to logfile", 0); }
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); } }