public void Activate() { UnitController.GetInstance(); EnemyController.GetInstance(); FriendlyUnitPositionObserver.GetInstance(); MovementMaps.GetInstance(); BuildMap.GetInstance(); Metal.GetInstance().Init(); LosMap.GetInstance(); BuildPlanner.GetInstance(); BuildTable.GetInstance(); CommanderList.GetInstance(); Level1ConstructorList.GetInstance(); Level1FactoryList.GetInstance(); tankcontroller = new TankController(TankList.GetInstance().defbyid, BuildTable.GetInstance().UnitDefByName["armstump"]); tankcontroller.Activate(); helicoptercontroller = new TankController(HelicopterList.GetInstance().defbyid, BuildTable.GetInstance().UnitDefByName["armbrawl"]); helicoptercontroller.Activate(); new ScoutControllerRaider().Activate(); UnitController.GetInstance().LoadExistingUnits(); EnemyController.GetInstance().LoadExistingUnits(); CheckIdleUnits(); //BuildSolarCell(CommanderList.GetInstance().defbyid.Keys.GetEnumerator().Current); csai.UnitIdleEvent += new CSAI.UnitIdleHandler(csai_UnitIdleEvent); csai.TickEvent += new CSAI.TickHandler(csai_TickEvent); }
void ExploreWith(int unitid) { 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("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 = LosMap.GetInstance().GetNearestUnseen(currentpos, unitdef, 6000); if (nextpoint == null) { nextpoint = GetRandomDestination(); } aicallback.GiveOrder(unitid, new Command(Command.CMD_MOVE, nextpoint.ToDoubleArray())); }
//int numOfUnits = 0; //IUnitDef[] unitList; //IUnitDef solarcollectordef; public void InitAI(IAICallback aicallback, int team) { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); this.aicallback = aicallback; try{ this.Team = team; logfile = LogFile.GetInstance().Init(team); logfile.WriteLine("C# AI started v" + AIVersion + ", team " + team + " ref " + reference + " map " + aicallback.GetMapName() + " mod " + aicallback.GetModName()); csaiuserinteraction = CSAIUserInteraction.GetInstance(); if (File.Exists("AI/CSAI/debug.flg")) // if this file exists, activate debug mode; saves manually changing this for releases { logfile.WriteLine("Toggling debug on"); DebugOn = true; } if (DebugOn) { new Testing.RunTests().Go(); } InitCache(); PlayStyleManager.GetInstance(); LoadPlayStyles.Go(); metal = Metal.GetInstance(); CommanderController.GetInstance(); BuildTable.GetInstance(); UnitController.GetInstance(); EnemyController.GetInstance(); FriendlyUnitPositionObserver.GetInstance(); EnergyController.GetInstance(); MovementMaps.GetInstance(); BuildMap.GetInstance(); LosMap.GetInstance(); //FactoryController.GetInstance(); //RadarController.GetInstance(); //TankController.GetInstance(); //ScoutController.GetInstance(); //ConstructorController.GetInstance(); metal.Init(); BuildPlanner.GetInstance(); UnitController.GetInstance().LoadExistingUnits(); // need this if we're being reloaded in middle of a game, to get already existing units EnemyController.GetInstance().LoadExistingUnits(); StrategyController.GetInstance(); LoadStrategies.Go(); PlayStyleManager.GetInstance().ChoosePlayStyle("tankrush"); if (aicallback.GetModName().ToLower().IndexOf("aass") == 0 || aicallback.GetModName().ToLower().IndexOf("xtape") == 0) { SendTextMsg("C# AI initialized v" + AIVersion + ", team " + team); SendTextMsg("Please say '.csai help' for available commands"); PlayStyleManager.GetInstance().ListPlayStyles(); //CommanderController.GetInstance().CommanderBuildPower(); } else { SendTextMsg("Warning: CSAI needs AA2.23 or XTA7 to run correctly at this time"); logfile.WriteLine("*********************************************************"); logfile.WriteLine("*********************************************************"); logfile.WriteLine("**** ****"); logfile.WriteLine("**** Warning: CSAI needs AA2.23 or XTA7 to run correctly at this time ****"); logfile.WriteLine("**** ****"); logfile.WriteLine("*********************************************************"); logfile.WriteLine("*********************************************************"); } } catch (Exception e) { logfile.WriteLine("Exception: " + e.ToString()); SendTextMsg("Exception: " + e.ToString()); } }
// 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); }