public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, int2 topLeft, Actor toIgnore) { var res = world.WorldActor.traits.Get<ResourceLayer>(); return !Footprint.Tiles(name, building, topLeft).Any( t => !world.Map.IsInMap(t.X, t.Y) || res.GetResource(t) != null || !world.IsCellBuildable(t, building.WaterBound, toIgnore)); }
public TileTemplate(ushort id, string image, int2 size, int[] tiles) { this.Id = id; this.Image = image; this.Size = size; this.tiles = tiles; }
public bool AnyUnitsAt(int2 a, SubCell sub) { for( var i = influence[ a.X, a.Y ] ; i != null ; i = i.next ) if (i.subCell == sub || i.subCell == SubCell.FullCell) return true; return false; }
public IEnumerable<Actor> GetUnitsAt( int2 a ) { if (!map.IsInMap(a)) yield break; for( var i = influence[ a.X, a.Y ] ; i != null ; i = i.next ) if (!i.actor.Destroyed) yield return i.actor; }
public IEnumerable<Actor> GetUnitsAt( int2 a, SubCell sub ) { if (!map.IsInMap(a)) yield break; for( var i = influence[ a.X, a.Y ] ; i != null ; i = i.next ) if (!i.actor.Destroyed && (i.subCell == sub || i.subCell == SubCell.FullCell)) yield return i.actor; }
public static IEnumerable<int2> FindTilesInCircle(this World world, int2 a, int r) { var min = world.ClampToWorld(a - new int2(r, r)); var max = world.ClampToWorld(a + new int2(r, r)); for (var j = min.Y; j <= max.Y; j++) for (var i = min.X; i <= max.X; i++) if (r * r >= (new int2(i, j) - a).LengthSquared) yield return new int2(i, j); }
public static PathSearch FromPoint( World world, MobileInfo mi, int2 from, int2 target, bool checkForBlocked ) { var search = new PathSearch(world, mi) { heuristic = DefaultEstimator( target ), checkForBlocked = checkForBlocked }; search.AddInitialCell( world, from ); return search; }
public Order(string orderString, Actor subject, Actor targetActor, int2 targetLocation, string targetString) { this.OrderString = orderString; this.Subject = subject; this.TargetActor = targetActor; this.TargetLocation = targetLocation; this.TargetString = targetString; }
public MouseInput(MouseInputEvent ev, MouseButton button, int scrollDelta, int2 location, Modifiers mods, int multiTapCount) { Event = ev; Button = button; ScrollDelta = scrollDelta; Location = location; Modifiers = mods; MultiTapCount = multiTapCount; }
public static PathSearch FromPoint( Actor self, int2 from, int2 target, bool checkForBlocked ) { var search = new PathSearch(self) { heuristic = DefaultEstimator( target ), checkForBlocked = checkForBlocked }; search.AddInitialCell( self.World, from ); return search; }
public static Func<int2, int> DefaultEstimator( int2 destination ) { return here => { int2 d = ( here - destination ).Abs(); int diag = Math.Min( d.X, d.Y ); int straight = Math.Abs( d.X - d.Y ); return (3400 * diag / 24) + (100 * straight); }; }
public static Func<int2, float> DefaultEstimator( int2 destination ) { return here => { int2 d = ( here - destination ).Abs(); int diag = Math.Min( d.X, d.Y ); int straight = Math.Abs( d.X - d.Y ); return 1.5f * diag + straight; }; }
Order(string orderString, Actor subject, Actor targetActor, int2 targetLocation, string targetString, bool queued, int2 extraLocation) { this.OrderString = orderString; this.Subject = subject; this.TargetActor = targetActor; this.TargetLocation = targetLocation; this.TargetString = targetString; this.Queued = queued; this.ExtraLocation = extraLocation; }
public static void AddSmudge(this Map map, int2 targetTile, WarheadInfo warhead) { if (warhead.SmudgeType == SmudgeType.None) return; if (warhead.Size[0] == 0 && warhead.Size[1] == 0) map.AddSmudge(warhead.SmudgeType == SmudgeType.Crater, targetTile.X, targetTile.Y); else foreach (var t in Game.world.FindTilesInCircle(targetTile, warhead.Size[0])) if ((t - targetTile).LengthSquared >= warhead.Size[1] * warhead.Size[1]) if (Rules.TerrainTypes[Game.world.GetTerrainType(t)].AcceptSmudge) map.AddSmudge(warhead.SmudgeType == SmudgeType.Crater, t.X, t.Y); }
public int GetTapCount(int2 xy) { FirstRelease = SecondRelease; SecondRelease = ThirdRelease; ThirdRelease = Pair.New(DateTime.Now, xy); if (!CloseEnough(ThirdRelease, SecondRelease)) return 1; if (!CloseEnough(SecondRelease, FirstRelease)) return 2; return 3; }
public static IEnumerable<int2> FindTilesInCircle(this World world, int2 a, int r) { var min = a - new int2(r, r); var max = a + new int2(r, r); if (min.X < world.Map.XOffset) min.X = world.Map.XOffset; if (min.Y < world.Map.YOffset) min.Y = world.Map.YOffset; if (max.X > world.Map.XOffset + world.Map.Width - 1) max.X = world.Map.XOffset + world.Map.Width - 1; if (max.Y > world.Map.YOffset + world.Map.Height - 1) max.Y = world.Map.YOffset + world.Map.Height - 1; for (var j = min.Y; j <= max.Y; j++) for (var i = min.X; i <= max.X; i++) if (r * r >= (new int2(i, j) - a).LengthSquared) yield return new int2(i, j); }
public static void DoExplosion(Actor attacker, string weapontype, int2 location, int altitude) { var args = new ProjectileArgs { src = location, dest = location, srcAltitude = altitude, destAltitude = altitude, firedBy = attacker, target = null, weapon = Rules.Weapons[ weapontype.ToLowerInvariant() ], facing = 0 }; DoImpacts(args, location); }
public Actor( World world, string name, int2 location, Player owner ) { World = world; ActorID = world.NextAID(); Location = location; CenterLocation = Traits.Util.CenterOfCell(Location); Owner = owner; if (name != null) { Info = Rules.Info[name.ToLowerInvariant()]; Health = this.GetMaxHP(); foreach (var trait in Info.TraitsInConstructOrder()) traits.Add(trait.Create(this)); } }
public static void DoImpact(WarheadInfo warhead, ProjectileArgs args, int2 visualLocation) { var world = args.firedBy.World; var targetTile = ((1f / Game.CellSize) * args.dest.ToFloat2()).ToInt2(); var isWater = world.GetTerrainType(targetTile) == TerrainType.Water; if (warhead.Explosion != 0) world.AddFrameEndTask( w => w.Add(new Explosion(w, visualLocation, warhead.Explosion, isWater))); Sound.Play(GetImpactSound(warhead, isWater)); if (!isWater) world.Map.AddSmudge(targetTile, warhead); if (warhead.Ore) world.WorldActor.traits.Get<ResourceLayer>().Destroy(targetTile); var firepowerModifier = args.firedBy.traits .WithInterface<IFirepowerModifier>() .Select(a => a.GetFirepowerModifier()) .Product(); switch (warhead.DamageModel) { case DamageModel.Normal: { var maxSpread = warhead.Spread * (float)Math.Log(Math.Abs(warhead.Damage), 2); var hitActors = world.FindUnitsInCircle(args.dest, maxSpread); foreach (var victim in hitActors) victim.InflictDamage(args.firedBy, (int)GetDamageToInflict(victim, args, warhead, firepowerModifier), warhead); } break; case DamageModel.PerCell: { foreach (var t in world.FindTilesInCircle(targetTile, warhead.Size[0])) foreach (var unit in world.FindUnits(Game.CellSize * t, Game.CellSize * (t + new float2(1,1)))) unit.InflictDamage(args.firedBy, (int)(warhead.Damage * warhead.EffectivenessAgainst( unit.Info.Traits.Get<OwnedActorInfo>().Armor)), warhead); } break; } }
public BinaryDataHeader(Stream s, int2 expectedSize) { Format = s.ReadUInt8(); var width = s.ReadUInt16(); var height = s.ReadUInt16(); if (width != expectedSize.X || height != expectedSize.Y) throw new InvalidDataException("Invalid tile data"); if (Format == 1) { TilesOffset = 5; HeightsOffset = 0; ResourcesOffset = (uint)(3 * width * height + 5); } else if (Format == 2) { TilesOffset = s.ReadUInt32(); HeightsOffset = s.ReadUInt32(); ResourcesOffset = s.ReadUInt32(); } else throw new InvalidDataException("Unknown binary map format '{0}'".F(Format)); }
public static IEnumerable<int2> GetLineBuildCells(World world, int2 location, string name, BuildingInfo bi) { int range = Rules.Info[name].Traits.Get<LineBuildInfo>().Range; var topLeft = location; // 1x1 assumption! if (world.IsCellBuildable(topLeft, bi.WaterBound)) yield return topLeft; // Start at place location, search outwards // TODO: First make it work, then make it nice var vecs = new[] { new int2(1, 0), new int2(0, 1), new int2(-1, 0), new int2(0, -1) }; int[] dirs = { 0, 0, 0, 0 }; for (int d = 0; d < 4; d++) { for (int i = 1; i < range; i++) { if (dirs[d] != 0) continue; int2 cell = topLeft + i * vecs[d]; if (world.IsCellBuildable(cell, bi.WaterBound)) continue; // Cell is empty; continue search // Cell contains an actor. Is it the type we want? if (Game.world.Queries.WithTrait<LineBuild>().Any(a => (a.Actor.Info.Name == name && a.Actor.Location.X == cell.X && a.Actor.Location.Y == cell.Y))) dirs[d] = i; // Cell contains actor of correct type else dirs[d] = -1; // Cell is blocked by another actor type } // Place intermediate-line sections if (dirs[d] > 0) for (int i = 1; i < dirs[d]; i++) yield return topLeft + i * vecs[d]; } }
// editor magic. public void Resize(int width, int height) { var oldMapTiles = MapTiles.Value; var oldMapResources = MapResources.Value; var oldMapHeight = MapHeight.Value; var newSize = new Size(width, height); MapTiles = Exts.Lazy(() => CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[MPos.Zero])); MapResources = Exts.Lazy(() => CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero])); MapHeight = Exts.Lazy(() => CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero])); MapSize = new int2(newSize); }
// editor magic. public void Resize(int width, int height) { var oldMapTiles = MapTiles.Value; var oldMapResources = MapResources.Value; MapTiles = Lazy.New(() => Exts.ResizeArray(oldMapTiles, oldMapTiles[0, 0], width, height)); MapResources = Lazy.New(() => Exts.ResizeArray(oldMapResources, oldMapResources[0, 0], width, height)); MapSize = new int2(width, height); }
public bool IsExplored(int2 xy) { return IsExplored(xy.X, xy.Y); }
public static int HashInt2(int2 i2) { return(((i2.X * 5) ^ (i2.Y * 3)) / 4); }
public void Resize(int width, int height) // editor magic. { var oldMapTiles = MapTiles.Value; var oldMapResources = MapResources.Value; var newSize = new Size(width, height); MapTiles = Exts.Lazy(() => CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[0, 0])); MapResources = Exts.Lazy(() => CellLayer.Resize(oldMapResources, newSize, oldMapResources[0, 0])); MapSize = new int2(newSize); }
static int WindingDirectionTest(int2 v0, int2 v1, int2 p) { return((v1.X - v0.X) * (p.Y - v0.Y) - (p.X - v0.X) * (v1.Y - v0.Y)); }
public float2(int2 p) { X = p.X; Y = p.Y; }
public static int2 Lerp( int2 a, int2 b, int mul, int div ) { return a + ( b - a ) * mul / div; }
public static int Dot(int2 a, int2 b) { return a.X * b.X + a.Y * b.Y; }
public bool IsVisible(int2 xy) { return IsVisible(xy.X, xy.Y); }
public TerrainTemplateInfo(ushort id, string[] images, int2 size, byte[] tiles) { Id = id; Images = images; Size = size; }
public static bool Contains(this RectangleF r, int2 p) { return(r.Contains(p.ToPointF())); }
public static int2 Min(int2 a, int2 b) { return new int2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); }
public void BeginFrame(int2 scroll, float zoom) { Device.Clear(); SetViewportParams(scroll, zoom); }