Example #1
0
 public void Update()
 {
     System.DateTime start = DateTime.Now;
     try
     {
         if (!aicallback.IsGamePaused())
         {
             if (TickEvent != null)
             {
                 TickEvent();
             }
             if (DebugOn)
             {
                 //logfile.WriteLine("frame: " + aicallback.GetCurrentFrame());
                 if (aicallback.GetCurrentFrame() - lastdebugframe > 30)
                 {
                     DumpTimings();
                     lastdebugframe = aicallback.GetCurrentFrame();
                 }
             }
         }
     }
     catch (Exception e)
     {
         logfile.WriteLine("Exception: " + e.ToString());
         SendTextMsg("Exception: " + e.ToString());
     }
     ticktime += DateTime.Now.Subtract(start).TotalMilliseconds;
 }
Example #2
0
        void UpdateLosForUnit(int unitid, IUnitDef unitdef)
        {
            int    thisframecount = aicallback.GetCurrentFrame();
            Float3 pos            = friendlyunitpositionobserver.PosById[unitid] as Float3;
            int    seenmapx       = (int)(pos.x / 16);
            int    seenmapy       = (int)(pos.z / 16);
            // int radius = (int)( unitdef.losRadius / 8 / 2 );
            int radius = (int)unitdef.losRadius;

            if (csai.DebugOn)
            {
                //aicallback.SendTextMsg( "Updating los for " + unitid + " " + unitdef.humanName + " los radius " + radius, 0 );
                DrawingUtils.DrawCircle(pos, radius * 16);
            }
            logfile.WriteLine("Updating los for " + unitid + " " + unitdef.humanName + " los radius " + radius + " pos " + pos.ToString());
            // go line by line, determine positive and negative extent of line, mark lostime
            for (int deltay = -radius; deltay <= radius; deltay++)
            {
                int xextent = (int)Math.Sqrt(radius * radius - deltay * deltay);
                for (int deltax = -xextent; deltax <= xextent; deltax++)
                {
                    int thisx = seenmapx + deltax;
                    int thisy = seenmapy + deltay;
                    if (thisx >= 0 && thisx < mapwidth / 2 && thisy >= 0 && thisy < mapheight / 2)
                    {
                        LastSeenFrameCount[thisx, thisy] = thisframecount;
                    }
                }
                // in progress
            }
            LastLosRefreshFrameCountByUnitId[unitid] = thisframecount;
            PosAtLastRefreshByUnitId[unitid]         = pos;
            logfile.WriteLine("...done");
        }
Example #3
0
 void csai_TickEvent()
 {
     if (aicallback.GetCurrentFrame() - lastcheckidleframe > 30)
     {
         CheckIdleUnits();
         lastcheckidleframe = aicallback.GetCurrentFrame();
     }
 }
Example #4
0
 void csai_TickEvent()
 {
     if (aicallback.GetCurrentFrame() - lastcommandssentresetframe >= 30)
     {
         lastcommandssentresetframe = aicallback.GetCurrentFrame();
         if (csai.DebugOn)
         {
             //  DumpCommandsSentStats();
         }
     }
 }
Example #5
0
 public void DumpLosMap(string cmd, string[] split, int player)
 {
     bool[,] losmap = new bool[mapwidth / 2, mapheight / 2];
     for (int y = 0; y < mapheight / 2; y++)
     {
         for (int x = 0; x < mapwidth / 2; x++)
         {
             losmap[x, y] = (aicallback.GetCurrentFrame() - LastSeenFrameCount[x, y] < 6000);
         }
     }
     DrawingUtils.DrawMap(losmap);
 }
 public static string GetGameTimeString()
 {
     IAICallback aicallback = CSAI.GetInstance().aicallback;
     int frames = aicallback.GetCurrentFrame();
     TimeSpan gametime = TimeSpan.FromSeconds((double)frames / 30);
     string secondfractionstring = ( gametime.Milliseconds / 10 ).ToString().PadLeft( 2, '0' );
     return gametime.Hours + ":" + gametime.Minutes + ":" + gametime.Seconds + "." + secondfractionstring;
 }
Example #7
0
        void UnitCreatedEvent(int deployedunitid, IUnitDef unitdef)
        {
            IAICallback aicallback = CSAI.GetInstance().aicallback;

            if (aicallback.GetCurrentFrame() <= 1)
            {
                if (unitdef.isCommander)
                {
                    startposition = aicallback.GetUnitPos(deployedunitid);
                    //aicallback.get
                }
            }
        }
Example #8
0
        void DumpLosMap(string cmd, string[] split, int player)
        {
            bool[,] losmapfordisplay = new bool[aicallback.GetMapWidth() / 8, aicallback.GetMapHeight() / 8];
            for (int y = 0; y < aicallback.GetMapHeight() / 2; y += 4)
            {
                for (int x = 0; x < aicallback.GetMapWidth() / 2; x += 4)
                {
                    losmapfordisplay[x / 4, y / 4] = (aicallback.GetCurrentFrame() - LosMap.GetInstance().LastSeenFrameCount[x, y]) < 6000;
                }
            }
            int figuregroup = DrawingUtils.DrawMap(losmapfordisplay);

            aicallback.SetFigureColor(figuregroup, 0, 1, 1, 1);
        }
        // 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);
        }
 public static TimeSpan GetGameTime()
 {
     IAICallback aicallback = CSAI.GetInstance().aicallback;
     int frames = aicallback.GetCurrentFrame();
     return TimeSpan.FromSeconds((double)frames / 30);
 }
Example #11
0
        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);
        }