// mark squares as used // and keep record of where this unit was, and how big, in case it is destroyed public void UnitCreated(int id, IUnitDef unitdef) { if (!buildinginfobyid.Contains(id) && !unitdefhelp.IsMobile(unitdef)) { Float3 pos = aicallback.GetUnitPos(id); int mapposx = (int)(pos.x / 8); int mapposy = (int)(pos.z / 8); //int unitsizex = (int)Math.Ceiling( unitdef.xsize / 8.0 ); //int unitsizey = (int)Math.Ceiling( unitdef.ysize / 8.0 ); int unitsizex = unitdef.xsize; int unitsizey = unitdef.ysize; logfile.WriteLine("Buildmap unitcreated " + unitdef.name + " mappos " + mapposx + " " + mapposy + " unitsize " + unitsizex + " " + unitsizey); buildinginfobyid.Add(id, new BuildingInfo(mapposx, mapposy, unitsizex, unitsizey)); for (int x = 0; x < unitsizex; x++) { for (int y = 0; y < unitsizey; y++) { int thisx = mapposx + x - unitdef.xsize / 2; int thisy = mapposy + y - unitdef.ysize / 2; SquareAvailable[thisx, thisy] = false; //if( csai.DebugOn ) //{ // logfile.WriteLine( "marking " + thisx + " " + thisy + " as used by " + unitdef.name ); // aicallback.DrawUnit( "ARMMINE1", new Float3( thisx * 8, aicallback.GetElevation( thisx * 8, thisy * 8 ), thisy * 8 ), 0.0f, 400, aicallback.GetMyAllyTeam(), true, true); //} } } } }
void AddEnemy(int enemyid) { IUnitDef unitdef = aicallback.GetUnitDef(enemyid); if (!EnemyUnitDefByDeployedId.Contains(enemyid)) { EnemyUnitDefByDeployedId.Add(enemyid, unitdef); logfile.WriteLine("EnemyController New enemy: " + enemyid); if (NewEnemyAddedEvent != null) { NewEnemyAddedEvent(enemyid, unitdef); } } if (unitdef != null) { if (EnemyUnitDefByDeployedId[enemyid] == null) { EnemyUnitDefByDeployedId[enemyid] = unitdef; logfile.WriteLine("enemy " + enemyid + " is " + unitdef.humanName); } if (!unitdefhelp.IsMobile(unitdef)) { if (!EnemyStaticPosByDeployedId.Contains(enemyid)) { EnemyStaticPosByDeployedId.Add(enemyid, aicallback.GetUnitPos(enemyid)); } } } }
public void UnitAdded(int id, IUnitDef unitdef) { if (!PosById.ContainsKey(id)) { PosById.Add(id, aicallback.GetUnitPos(id)); if (unitdefhelp.IsMobile(unitdef)) { MobileUnitIds.Add(id); } else { StaticUnitIds.Add(id); } } }
// 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); }