// save cache to speed up load times // we store the MetalClassVersion to enable cache rebuild if algo changed void SaveCache() { string MapName = aicallback.GetMapName(); string cachefilepath = Path.Combine(csai.CacheDirectoryPath, MapName + "_metal.xml"); XmlDocument cachedom = XmlHelper.CreateDom(); XmlElement metadata = XmlHelper.AddChild(cachedom.DocumentElement, "metadata"); metadata.SetAttribute("type", "MetalCache"); metadata.SetAttribute("map", MapName); metadata.SetAttribute("version", MetalClassVersion); metadata.SetAttribute("datecreated", DateTime.Now.ToString()); metadata.SetAttribute("ismetalmap", isMetalMap.ToString()); metadata.SetAttribute("extractorradius", aicallback.GetExtractorRadius().ToString()); metadata.SetAttribute("mapheight", aicallback.GetMapHeight().ToString()); metadata.SetAttribute("mapwidth", aicallback.GetMapWidth().ToString()); XmlElement metalspots = XmlHelper.AddChild(cachedom.DocumentElement, "metalspots"); foreach (MetalSpot metalspot in MetalSpots) { XmlElement metalspotnode = XmlHelper.AddChild(metalspots, "metalspot"); //metalspotnode.SetAttribute( "pos", metalspot.Pos.ToCsv() ); Float3Helper.WriteFloat3ToXmlElement(metalspot.Pos, metalspotnode); metalspotnode.SetAttribute("amount", metalspot.Amount.ToString()); } cachedom.Save(cachefilepath); }
public void DumpLos(string cmd, string[] cmdsplit, int player) { logfile.WriteLine("calling getlosmap..."); bool[] LosMap = aicallback.GetLosMap(); logfile.WriteLine("...done"); int loswidth = aicallback.GetMapWidth() / 2; int losheight = aicallback.GetMapHeight() / 2; logfile.WriteLine("losmap width: " + loswidth + " losheight: " + losheight); ArrayIndexer arrayindexer = new ArrayIndexer(loswidth, losheight); for (int y = 0; y < losheight; y++) { string line = ""; for (int x = 0; x < loswidth; x++) { if (LosMap[arrayindexer.GetIndex(x, y)]) { line += "*"; // aicallback.DrawUnit( "ARMCOM", new Float3( x * 16, 0, y * 16 ), 0.0f, 100, aicallback.GetMyAllyTeam(), true, true); } else { line += "-"; } } logfile.WriteLine(line); } aicallback.SendTextMsg("los dumped to logfile", 0); }
LosMap() { csai = CSAI.GetInstance(); aicallback = csai.aicallback; logfile = LogFile.GetInstance(); unitcontroller = UnitController.GetInstance(); unitcontroller.UnitAddedEvent += new UnitController.UnitAddedHandler(UnitAdded); unitcontroller.UnitRemovedEvent += new UnitController.UnitRemovedHandler(UnitRemoved); friendlyunitpositionobserver = FriendlyUnitPositionObserver.GetInstance(); csai.TickEvent += new CSAI.TickHandler(Tick); mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); logfile.WriteLine("LosMap, create losarray"); LastSeenFrameCount = new int[mapwidth / 2, mapheight / 2]; logfile.WriteLine("losarray created, initializing..."); for (int y = 0; y < mapheight / 2; y++) { for (int x = 0; x < mapwidth / 2; x++) { LastSeenFrameCount[x, y] = -1000000; } } logfile.WriteLine("losarray initialized"); if (csai.DebugOn) { csai.RegisterVoiceCommand("dumplosmap", new CSAI.VoiceCommandHandler(DumpLosMap)); } }
public void Init() { mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); logfile.WriteLine("EnemyMap.Init finished()"); }
// returns figure group public static int DrawMap(bool[,] map) { int thismapwidth = map.GetUpperBound(0) + 1; int thismapheight = map.GetUpperBound(1) + 1; IAICallback aicallback = CSAI.GetInstance().aicallback; int multiplier = (aicallback.GetMapWidth() / thismapwidth) * 8; int figuregroup = 0; for (int y = 0; y < thismapheight; y++) { for (int x = 0; x < thismapwidth; x++) { double elevation = aicallback.GetElevation(x * multiplier, y * multiplier) + 10; if (x < (thismapwidth - 1) && map[x, y] != map[x + 1, y]) { figuregroup = aicallback.CreateLineFigure(new Float3((x + 1) * multiplier, elevation, y * multiplier), new Float3((x + 1) * multiplier, elevation, (y + 1) * multiplier), 10, false, 200, figuregroup); } if (y < (thismapheight - 1) && map[x, y] != map[x, y + 1]) { figuregroup = aicallback.CreateLineFigure(new Float3(x * multiplier, elevation, (y + 1) * multiplier), new Float3((x + 1) * multiplier, elevation, (y + 1) * multiplier), 10, false, 200, figuregroup); } } } return(figuregroup); }
Float3 GetRandomDestination() { Float3 destination = new Float3(); 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); return(destination); }
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())); }
BuildPlanner() { csai = CSAI.GetInstance(); aicallback = csai.aicallback; logfile = LogFile.GetInstance(); unitdefhelp = new UnitDefHelp(aicallback); mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); }
BuildPlanner() { csai = CSAI.GetInstance(); aicallback = csai.aicallback; logfile = LogFile.GetInstance(); unitdefhelp = new UnitDefHelp( aicallback ); mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); }
// ported from Spring's ReadMap.cpp by Hugh Perkins public double[,] GetSlopeMap() { int mapwidth = aicallback.GetMapWidth(); int mapheight = aicallback.GetMapHeight(); int slopemapwidth = mapwidth / 2; int slopemapheight = mapheight / 2; int squaresize = MovementMaps.SQUARE_SIZE; // jsut to save typing... logfile.WriteLine("Getting heightmap, this could take a while... "); double[,] HeightMap = new double[mapwidth + 1, mapheight + 1]; //double[]heightmap = aicallback.GetHeightMap(); // cant use heightmap, it is centre heightmap, we need cornermap for (int x = 0; x < mapwidth + 1; x++) { for (int y = 0; y < mapheight + 1; y++) { HeightMap[x, y] = aicallback.GetElevation(x * squaresize, y * squaresize); } } // ArrayIndexer heightmapindexer = new ArrayIndexer( mapwidth + 1, mapheight + 1 ); logfile.WriteLine("calculating slopes..."); logfile.WriteLine("mapwidth: " + slopemapwidth + " " + slopemapheight); double[,] SlopeMap = new double[slopemapwidth, slopemapheight]; for (int y = 2; y < mapheight - 2; y += 2) { for (int x = 2; x < mapwidth - 2; x += 2) { AdvancedFloat3 e1 = new AdvancedFloat3(-squaresize * 4, HeightMap[x - 1, y - 1] - HeightMap[x + 3, y - 1], 0); AdvancedFloat3 e2 = new AdvancedFloat3(0, HeightMap[x - 1, y - 1] - HeightMap[x - 1, y + 3], -squaresize * 4); AdvancedFloat3 n = e2.Cross(e1); n.Normalize(); e1 = new AdvancedFloat3(squaresize * 4, HeightMap[x + 3, y + 3] - HeightMap[x - 1, y + 3], 0); e2 = new AdvancedFloat3(0, HeightMap[x + 3, y + 3] - HeightMap[x + 3, y - 1], squaresize * 4); AdvancedFloat3 n2 = e2.Cross(e1); n2.Normalize(); SlopeMap[x / 2, y / 2] = 1 - (n.y + n2.y) * 0.5; } } logfile.WriteLine("... slopes calculated"); return(SlopeMap); }
// can pass in pointer to a hashtable in another class if we want // ie other class can directly modify our hashtable public SpreadSearchPackCoordinatorWithSearchGrid(Dictionary<int, IUnitDef> UnitDefListByDeployedId) { this.UnitDefListByDeployedId = UnitDefListByDeployedId; csai = CSAI.GetInstance(); aicallback = csai.aicallback; logfile = LogFile.GetInstance(); mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); debugon = csai.DebugOn; csai.TickEvent += new CSAI.TickHandler( this.Tick ); csai.UnitIdleEvent += new CSAI.UnitIdleHandler( UnitIdle ); }
// can pass in pointer to a hashtable in another class if we want // ie other class can directly modify our hashtable public SpreadSearchPackCoordinatorWithSearchGrid(Hashtable UnitDefListByDeployedId) { this.UnitDefListByDeployedId = UnitDefListByDeployedId; csai = CSAI.GetInstance(); aicallback = csai.aicallback; logfile = LogFile.GetInstance(); mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); debugon = csai.DebugOn; csai.TickEvent += new CSAI.TickHandler(this.Tick); csai.UnitIdleEvent += new CSAI.UnitIdleHandler(UnitIdle); }
public double[,] GetHeightMap() { LogFile.GetInstance().WriteLine("Getting heightmap, this could take a while... "); IAICallback aicallback = CSAI.GetInstance().aicallback; int mapwidth = aicallback.GetMapWidth(); int mapheight = aicallback.GetMapHeight(); double[,] HeightMap = new double[mapwidth + 1, mapheight + 1]; for (int x = 0; x < mapwidth + 1; x++) { for (int y = 0; y < mapheight + 1; y++) { HeightMap[x, y] = aicallback.GetElevation(x * MovementMaps.SQUARE_SIZE, y * MovementMaps.SQUARE_SIZE); } } return(HeightMap); }
public void GenerateMaps() { logfile.WriteLine("MovementMaps.GenerateMaps start"); slopemap = new SlopeMap().GetSlopeMap(); heightmap = aicallback.GetCentreHeightMap(); mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); GenerateInfantryAccessibleMap(); GenerateVehicleAccessibleMap(); GenerateBoatAccessibleMap(); infantryareas = CreateAreas(infantryareasizes, infantrymap); vehicleareas = CreateAreas(vehicleareasizes, vehiclemap); boatareas = CreateAreas(boatareasizes, boatmap); logfile.WriteLine("MovementMaps.GenerateMaps done"); }
public void Init() { // plan: mark every square available, then remove squares as we build // also, remove planned buildings, ie: metal extractors // note: get metal do do this for us // so we need UnitController to alert us about buildings in progress etc logfile.WriteLine("BuildMap.Init()"); mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); SquareAvailable = new bool[mapwidth, mapheight]; for (int x = 0; x < mapwidth; x++) { for (int y = 0; y < mapheight; y++) { SquareAvailable[x, y] = true; } } logfile.WriteLine("BuildMap.Init finished()"); }
public ScoutControllerRaider( IPlayStyle playstyle ) { this.playstyle = playstyle; csai = CSAI.GetInstance(); aicallback = csai.aicallback; logfile = LogFile.GetInstance(); random = new Random(); terrainwidth = aicallback.GetMapWidth() * MovementMaps.SQUARE_SIZE; terrainheight = aicallback.GetMapHeight() * MovementMaps.SQUARE_SIZE; int[,] sectorlastcheckedtickcount = new int[ terrainwidth, terrainheight ]; bool[,] sectorispriority = new bool[ terrainwidth, terrainheight ];; unitcontroller = UnitController.GetInstance(); buildtable = BuildTable.GetInstance(); enemycontroller = EnemyController.GetInstance(); searchcoordinator = new SpreadSearchPackCoordinator( ScoutUnitDefsById ); logfile.WriteLine( "*ScoutControllerRaider initialized*" ); }
public ScoutControllerRaider(IPlayStyle playstyle) { this.playstyle = playstyle; csai = CSAI.GetInstance(); aicallback = csai.aicallback; logfile = LogFile.GetInstance(); random = new Random(); terrainwidth = aicallback.GetMapWidth() * MovementMaps.SQUARE_SIZE; terrainheight = aicallback.GetMapHeight() * MovementMaps.SQUARE_SIZE; int[,] sectorlastcheckedtickcount = new int[terrainwidth, terrainheight]; bool[,] sectorispriority = new bool[terrainwidth, terrainheight];; unitcontroller = UnitController.GetInstance(); buildtable = BuildTable.GetInstance(); enemycontroller = EnemyController.GetInstance(); searchcoordinator = new SpreadSearchPackCoordinator(ScoutUnitDefsById); logfile.WriteLine("*ScoutControllerRaider initialized*"); }
LosMap() { csai = CSAI.GetInstance(); aicallback = csai.aicallback; logfile = LogFile.GetInstance(); unitcontroller = UnitController.GetInstance(); unitcontroller.UnitAddedEvent += new UnitController.UnitAddedHandler( UnitAdded ); unitcontroller.UnitRemovedEvent += new UnitController.UnitRemovedHandler( UnitRemoved ); friendlyunitpositionobserver = FriendlyUnitPositionObserver.GetInstance(); csai.TickEvent += new CSAI.TickHandler( Tick ); mapwidth = aicallback.GetMapWidth(); mapheight = aicallback.GetMapHeight(); logfile.WriteLine( "LosMap, create losarray" ); LastSeenFrameCount = new int[ mapwidth / 2, mapheight / 2 ]; logfile.WriteLine( "losarray created, initializing..." ); for( int y = 0; y < mapheight / 2; y++ ) { for( int x = 0; x < mapwidth / 2; x++ ) { LastSeenFrameCount[ x, y ] = -1000000; } } logfile.WriteLine( "losarray initialized" ); if (csai.DebugOn) { csai.RegisterVoiceCommand("dumplosmap", new CSAI.VoiceCommandHandler(DumpLosMap)); } }
// note: need to check compatible area public Float3 GetNearestUnseen(Float3 currentpos, IUnitDef unitdef, int unseensmeansthismanyframes) { LosMap losmap = LosMap.GetInstance(); IAICallback aicallback = CSAI.GetInstance().aicallback; int mapwidth = aicallback.GetMapWidth(); int mapheight = aicallback.GetMapHeight(); int currentunitarea = MovementMaps.GetInstance().GetArea(unitdef, currentpos); int losmapwidth = losmap.LastSeenFrameCount.GetUpperBound(0) + 1; int losmapheight = losmap.LastSeenFrameCount.GetUpperBound(0) + 1; int maxradius = (int)Math.Sqrt(losmapheight * losmapheight + losmapwidth * losmapwidth); int unitlosradius = (int)unitdef.losRadius; // this is in map / 2 units, so it's ok Int2[] circlepoints = CreateCirclePoints(unitlosradius); int bestradius = 10000000; int bestarea = 0; Float3 bestpos = null; int unitmapx = (int)(currentpos.x / 16); int unitmapy = (int)(currentpos.y / 16); int thisframecount = aicallback.GetCurrentFrame(); // step around in unitlosradius steps for (int radiuslosunits = unitlosradius * 2; radiuslosunits <= maxradius; radiuslosunits += unitlosradius) { // calculate angle for a unitlosradius / 2 step at this radius. // DrawingUtils.DrawCircle(currentpos, radiuslosunits * 16); double anglestepradians = 2 * Math.Asin((double)unitlosradius / 2 / (double)radiuslosunits); //csai.DebugSay("anglestepradians: " + anglestepradians); //return null; for (double angleradians = 0; angleradians <= Math.PI * 2; angleradians += anglestepradians) { int unseenarea = 0; int searchmapx = unitmapx + (int)((double)radiuslosunits * Math.Cos(angleradians)); int searchmapy = unitmapy + (int)((double)radiuslosunits * Math.Sin(angleradians)); if (searchmapx >= 0 && searchmapy >= 0 && searchmapx < (mapwidth / 2) && searchmapy < (mapheight / 2)) { // if (csai.DebugOn) // { // int groupnumber = DrawingUtils.DrawCircle(new Float3(searchmapx * 16, 50 + aicallback.GetElevation( searchmapx * 16, searchmapy * 16 ), searchmapy * 16), unitlosradius * 16); // aicallback.SetFigureColor(groupnumber, 1, 1, 0, 0.5); // } int thisareanumber = MovementMaps.GetInstance().GetArea(unitdef, new Float3(searchmapx * 16, 0, searchmapy * 16)); if (thisareanumber == currentunitarea) {// //if (csai.DebugOn) // { // int groupnumber = DrawingUtils.DrawCircle(new Float3(searchmapx * 16, 100, searchmapy * 16), unitlosradius * 16); // aicallback.SetFigureColor(groupnumber, 1, 1, 0, 0.5); // } foreach (Int2 point in circlepoints) { int thismapx = searchmapx + point.x; int thismapy = searchmapy + point.y; if (thismapx >= 0 && thismapy >= 0 && thismapx < mapwidth / 2 && thismapy < mapheight / 2) { if (thisframecount - losmap.LastSeenFrameCount[thismapx, thismapy] > unseensmeansthismanyframes) { unseenarea++; } } } if (unseenarea >= (circlepoints.GetUpperBound(0) + 1) * 8 / 10) { int groupnumber = DrawingUtils.DrawCircle(new Float3(searchmapx * 16, 100 * aicallback.GetElevation(searchmapx * 16, searchmapy * 16), searchmapy * 16), unitlosradius * 16); aicallback.SetFigureColor(groupnumber, 1, 0, 1, 0.5); return(new Float3(searchmapx * 16, 0, searchmapy * 16)); } // return new Float3(searchmapx * 16, 0, searchmapy * 16); // for debugging, remove later } } } } return(null); }