Example #1
0
        protected static CPos?FindSafePlace(SquadCA owner, out Actor detectedEnemyTarget, bool needTarget)
        {
            var map          = owner.World.Map;
            var dangerRadius = owner.SquadManager.Info.DangerScanRadius;

            detectedEnemyTarget = null;

            var columnCount = (map.MapSize.X + dangerRadius - 1) / dangerRadius;
            var rowCount    = (map.MapSize.Y + dangerRadius - 1) / dangerRadius;

            var checkIndices = Exts.MakeArray(columnCount * rowCount, i => i).Shuffle(owner.World.LocalRandom);

            foreach (var i in checkIndices)
            {
                var pos = new MPos((i % columnCount) * dangerRadius + dangerRadius / 2, (i / columnCount) * dangerRadius + dangerRadius / 2).ToCPos(map);

                if (NearToPosSafely(owner, map.CenterOfCell(pos), out detectedEnemyTarget))
                {
                    if (needTarget && detectedEnemyTarget == null)
                    {
                        continue;
                    }

                    return(pos);
                }
            }

            return(null);
        }
Example #2
0
        public void RulesetLoaded(Ruleset rules, WeaponInfo info)
        {
            if (VictimScanRadius == WDist.Zero)
            {
                VictimScanRadius = Util.MinimumRequiredVictimScanRadius(rules);
            }

            if (Range != null)
            {
                if (Range.Length != 1 && Range.Length != Falloff.Length)
                {
                    throw new YamlException("Number of range values must be 1 or equal to the number of Falloff values.");
                }

                for (var i = 0; i < Range.Length - 1; i++)
                {
                    if (Range[i] > Range[i + 1])
                    {
                        throw new YamlException("Range values must be specified in an increasing order.");
                    }
                }
            }
            else
            {
                Range = Exts.MakeArray(Falloff.Length, i => i * Spread);
            }
        }
        public static IEnumerable <string> LinterHotkeyNames(MiniYamlNode widgetNode, Action <string> emitError, Action <string> emitWarning)
        {
            var prefix     = "";
            var prefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "HotkeyPrefix");

            if (prefixNode != null)
            {
                prefix = prefixNode.Value.Value;
            }

            var count     = 0;
            var countNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "HotkeyCount");

            if (countNode != null)
            {
                count = FieldLoader.GetValue <int>("HotkeyCount", countNode.Value.Value);
            }

            if (count == 0)
            {
                return(new string[0]);
            }

            if (string.IsNullOrEmpty(prefix))
            {
                emitError("{0} must define HotkeyPrefix if HotkeyCount > 0.".F(widgetNode.Location));
            }

            return(Exts.MakeArray(count, i => prefix + (i + 1).ToString("D2")));
        }
Example #4
0
        public override void Initialize(WidgetArgs args)
        {
            base.Initialize(args);

            hotkeys = Exts.MakeArray(HotkeyCount,
                                     i => new NamedHotkey(HotkeyPrefix + (i + 1).ToString("D2"), Game.Settings.Keys));
        }
Example #5
0
        public IEnumerable <PipType> GetPips(Actor self)
        {
            var pips = Info.PipCount != 0 ? Info.PipCount : Info.Ammo;

            return(Exts.MakeArray(pips,
                                  i => (ammo * pips) / Info.Ammo > i ? Info.PipType : Info.PipTypeEmpty));
        }
Example #6
0
        public IEnumerable <PipType> GetPips(Actor self)
        {
            var pips = Info.PipCount != 0 ? Info.PipCount : Info.Ammo;

            return(Exts.MakeArray(pips,
                                  i => (ammo * pips) / Info.Ammo > i ? PipType.Green : PipType.Transparent));
        }
Example #7
0
        public void InitPalette(WorldRenderer wr)
        {
            var c = info.Type == ShroudPaletteType.Shroud ? Shroud :
                    info.Type == ShroudPaletteType.Fog ? Fog : Combined;

            wr.AddPalette(info.Name, new Palette(Exts.MakeArray(256, i => (uint)c[i % 8].ToArgb())), false);
        }
        public override void DoImpact(WPos pos, Actor firedBy, IEnumerable <int> damageModifiers)
        {
            var world = firedBy.World;

            if (world.LocalPlayer != null)
            {
                var devMode = world.LocalPlayer.PlayerActor.TraitOrDefault <DebugVisualizations>();
                if (devMode != null && devMode.CombatGeometry)
                {
                    var range = Exts.MakeArray(Range.Length, i => WDist.FromCells(Range[i].Length));
                    world.WorldActor.Trait <WarheadDebugOverlay>().AddImpact(pos, range, DebugOverlayColor);
                }
            }

            var targetTile = world.Map.CellContaining(pos);

            for (var i = 0; i < Range.Length; i++)
            {
                var affectedCells = world.Map.FindTilesInCircle(targetTile, (int)Math.Ceiling((decimal)Range[i].Length / 1024));

                var raLayer = world.WorldActor.TraitsImplementing <TintedCellsLayer>()
                              .First(l => l.Info.Name == LayerName);

                foreach (var cell in affectedCells)
                {
                    int mul = GetIntensityFalloff((pos - world.Map.CenterOfCell(cell)).Length);
                    IncreaseTintedCellLevel(cell, mul, Falloff[i], raLayer);
                }
            }
        }
Example #9
0
        public override void DoImpact(WPos pos, Actor firedBy, IEnumerable <int> damageModifiers)
        {
            var world    = firedBy.World;
            var resLayer = world.WorldActor.Trait <RadioactivityLayer>();

            if (world.LocalPlayer != null)
            {
                var devMode = world.LocalPlayer.PlayerActor.TraitOrDefault <DeveloperMode>();
                if (devMode != null && devMode.ShowCombatGeometry)
                {
                    WDist[] rng = Exts.MakeArray(Range.Length, i => WDist.FromCells(Range[i]));
                    world.WorldActor.Trait <WarheadDebugOverlay>().AddImpact(pos, rng, DebugOverlayColor);
                }
            }

            // Accumulate radiation
            var targetTile = world.Map.CellContaining(pos);

            //for (var i = Range.Length-1; i >=0; i--)
            for (var i = 0; i < Range.Length; i++)
            {
                // Find affected cells, from outer Range down to inner range.
                var affectedCells = world.Map.FindTilesInAnnulus(targetTile, 0, Range[i]);

                var ra_layer = world.WorldActor.Trait <RadioactivityLayer>();

                foreach (var cell in affectedCells)
                {
                    var foff = FalloffDifference[i];
                    IncreaseRALevel(cell, foff, ra_layer);
                }
            }
        }
Example #10
0
        public void RulesetLoaded(Ruleset rules, WeaponInfo info)
        {
            if (Range != null)
            {
                if (Range.Length != 1 && Range.Length != Falloff.Length)
                {
                    throw new YamlException("Number of range values must be 1 or equal to the number of Falloff values.");
                }

                for (var i = 0; i < Range.Length - 1; i++)
                {
                    if (Range[i] > Range[i + 1])
                    {
                        throw new YamlException("Range values must be specified in an increasing order.");
                    }
                }
            }
            else
            {
                Range = Exts.MakeArray(Falloff.Length, i => i * Spread);
            }

            // Compute FalloffDifference LUT.
            FalloffDifference = new int[Falloff.Length];
            for (var i = 0; i < FalloffDifference.Length - 1; i++)
            {
                // with Falloff = { 100, 37, 14, 5, 0 }, you get
                // { 63, 23, 9, 5, 0 }
                FalloffDifference[i] = Falloff[i] - Falloff[i + 1];
            }
            FalloffDifference[FalloffDifference.Length - 1] = 0;
        }
Example #11
0
        public void WorldLoaded(World w, WorldRenderer wr)
        {
            world   = w;
            tiles   = new Dictionary <CPos, Smudge>();
            dirty   = new Dictionary <CPos, Smudge>();
            smudges = new Dictionary <string, Sprite[]>();

            var types = SequenceProvider.Sequences(Info.Sequence);

            foreach (var t in types)
            {
                var seq     = SequenceProvider.GetSequence(Info.Sequence, t);
                var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x));
                smudges.Add(t, sprites);
            }

            // Add map smudges
            foreach (var s in w.Map.Smudges.Value.Where(s => smudges.Keys.Contains(s.Type)))
            {
                var smudge = new Smudge
                {
                    Type   = s.Type,
                    Depth  = s.Depth,
                    Sprite = smudges[s.Type][s.Depth]
                };

                tiles.Add((CPos)s.Location, smudge);
            }
        }
        public override void Initialize(WidgetArgs args)
        {
            base.Initialize(args);

            hotkeys = Exts.MakeArray(HotkeyCount,
                                     i => modData.Hotkeys[HotkeyPrefix + (i + 1).ToString("D2")]);
        }
Example #13
0
        public AttractsWorms(ActorInitializer init, AttractsWormsInfo info)
            : base(info)
        {
            self = init.Self;

            effectiveRange = info.Range ?? Exts.MakeArray(info.Falloff.Length, i => i * info.Spread);
        }
Example #14
0
        public static void RemapShp(string[] args)
        {
            var remap = new Dictionary <int, int>();

            /* the first 4 entries are fixed */
            for (var i = 0; i < 4; i++)
            {
                remap[i] = i;
            }

            var srcMod = args[1].Split(':')[0];

            Game.modData = new ModData(srcMod);
            FileSystem.LoadFromManifest(Game.modData.Manifest);
            Rules.LoadRules(Game.modData.Manifest, new Map());
            var srcPaletteInfo = Rules.Info["player"].Traits.Get <PlayerColorPaletteInfo>();

            int[] srcRemapIndex = srcPaletteInfo.RemapIndex;

            var destMod = args[2].Split(':')[0];

            Game.modData = new ModData(destMod);
            FileSystem.LoadFromManifest(Game.modData.Manifest);
            Rules.LoadRules(Game.modData.Manifest, new Map());
            var destPaletteInfo = Rules.Info["player"].Traits.Get <PlayerColorPaletteInfo>();

            int[] destRemapIndex = destPaletteInfo.RemapIndex;

            int[] ShadowIndex = { };
            // the remap range is always 16 entries, but their location and order changes
            for (var i = 0; i < 16; i++)
            {
                remap[PlayerColorRemap.GetRemapIndex(srcRemapIndex, i)]
                    = PlayerColorRemap.GetRemapIndex(destRemapIndex, i);
            }

            // map everything else to the best match based on channel-wise distance
            var srcPalette  = Palette.Load(args[1].Split(':')[1], ShadowIndex);
            var destPalette = Palette.Load(args[2].Split(':')[1], ShadowIndex);

            var fullIndexRange = Exts.MakeArray <int>(256, x => x);

            for (var i = 0; i < 256; i++)
            {
                if (!remap.ContainsKey(i))
                {
                    remap[i] = fullIndexRange
                               .Where(a => !remap.ContainsValue(a))
                               .OrderBy(a => ColorDistance(destPalette.Values[a], srcPalette.Values[i]))
                               .First();
                }
            }

            var srcImage = ShpReader.Load(args[3]);

            using (var destStream = File.Create(args[4]))
                ShpWriter.Write(destStream, srcImage.Width, srcImage.Height,
                                srcImage.Frames.Select(im => im.Image.Select(px => (byte)remap[px]).ToArray()));
        }
Example #15
0
        public SpatiallyPartitioned(int width, int height, int binSize)
        {
            this.binSize = binSize;

            rows           = Exts.IntegerDivisionRoundingAwayFromZero(height, binSize);
            cols           = Exts.IntegerDivisionRoundingAwayFromZero(width, binSize);
            itemBoundsBins = Exts.MakeArray(rows * cols, _ => new Dictionary <T, Rectangle>());
        }
Example #16
0
        public AttractsWorms(AttractsWormsInfo info)
        {
            Info = info;

            if (info.Range == null)
            {
                info.Range = Exts.MakeArray(info.Falloff.Length, i => i * info.Spread);
            }
        }
Example #17
0
        public override void Initialize(WidgetArgs args)
        {
            base.Initialize(args);

            hotkeys = Exts.MakeArray(HotkeyCount,
                                     i => modData.Hotkeys[HotkeyPrefix + (i + 1).ToString("D2")]);

            overlayFont = Game.Renderer.Fonts[OverlayFont];
        }
Example #18
0
        public AttractsWorms(ActorInitializer init, AttractsWormsInfo info)
            : base(info)
        {
            self = init.Self;

            if (info.Range == null)
            {
                info.Range = Exts.MakeArray(info.Falloff.Length, i => i * info.Spread);
            }
        }
Example #19
0
        public void InitPalette(WorldRenderer wr)
        {
            var remap = new PlayerColorRemap(info.RemapIndex, owner.Color, info.Ramp);

            wr.AddPalette(info.BaseName + owner.InternalName, new Palette(wr.Palette(info.BasePalette).Palette, remap), info.AllowModifiers);

            var argb = (uint)Color.FromArgb(128, owner.Color.RGB).ToArgb();

            wr.AddPalette("highlight" + owner.InternalName, new Palette(Exts.MakeArray(256, i => i == 0 ? 0 : argb)), false);
        }
Example #20
0
 public ResourceType(ResourceTypeInfo info, World world)
 {
     this.Info = info;
     Variants  = new Dictionary <string, Sprite[]>();
     foreach (var v in info.Variants)
     {
         var seq     = world.Map.SequenceProvider.GetSequence("resources", v);
         var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x));
         Variants.Add(v, sprites);
     }
 }
Example #21
0
 public ResourceType(ResourceTypeInfo info, World world)
 {
     Info     = info;
     Variants = new Dictionary <string, Sprite[]>();
     foreach (var v in info.Sequences)
     {
         var seq     = world.Map.Rules.Sequences.GetSequence(Info.Image, v);
         var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x));
         Variants.Add(v, sprites);
     }
 }
Example #22
0
        public override void Initialize(WidgetArgs args)
        {
            base.Initialize(args);

            saveBookmarkHotkeys = Exts.MakeArray(BookmarkKeyCount,
                                                 i => new NamedHotkey(BookmarkSaveKeyPrefix + (i + 1).ToString("D2"), Game.Settings.Keys));

            restoreBookmarkHotkeys = Exts.MakeArray(BookmarkKeyCount,
                                                    i => new NamedHotkey(BookmarkRestoreKeyPrefix + (i + 1).ToString("D2"), Game.Settings.Keys));

            bookmarkPositions = new WPos?[BookmarkKeyCount];
        }
Example #23
0
        public void InitPalette(WorldRenderer wr)
        {
            // Enable palette only for a specific tileset
            if (info.Tileset != null && info.Tileset.ToLowerInvariant() != world.Map.Tileset.ToLowerInvariant())
            {
                return;
            }

            var c = (uint)((info.A << 24) | (info.R << 16) | (info.G << 8) | info.B);

            wr.AddPalette(info.Name, new Palette(Exts.MakeArray(256, i => (i == 0) ? 0 : c)), info.AllowModifiers);
        }
Example #24
0
        public override void Initialize(WidgetArgs args)
        {
            base.Initialize(args);

            saveBookmarkHotkeys = Exts.MakeArray(BookmarkKeyCount,
                                                 i => modData.Hotkeys[BookmarkSaveKeyPrefix + (i + 1).ToString("D2")]);

            restoreBookmarkHotkeys = Exts.MakeArray(BookmarkKeyCount,
                                                    i => modData.Hotkeys[BookmarkRestoreKeyPrefix + (i + 1).ToString("D2")]);

            bookmarkPositions = new WPos?[BookmarkKeyCount];
        }
Example #25
0
        public override void Initialize(WidgetArgs args)
        {
            base.Initialize(args);

            hotkeys = Exts.MakeArray(HotkeyCount,
                                     i => modData.Hotkeys[HotkeyPrefix + (i + 1).ToString("D2")]);

            overlayFont = Game.Renderer.Fonts[OverlayFont];

            iconOffset  = 0.5f * IconSize.ToFloat2() + IconSpriteOffset;
            holdOffset  = iconOffset - overlayFont.Measure(HoldText) / 2;
            readyOffset = iconOffset - overlayFont.Measure(ReadyText) / 2;
        }
Example #26
0
        protected virtual void WorldLoaded(World w, WorldRenderer wr)
        {
            var sequences = w.Map.Rules.Sequences;

            foreach (var kv in Info.ResourceTypes)
            {
                var resourceInfo     = kv.Value;
                var resourceVariants = resourceInfo.Sequences
                                       .ToDictionary(v => v, v => sequences.GetSequence(resourceInfo.Image, v));
                Variants.Add(kv.Key, resourceVariants);

                if (spriteLayer == null)
                {
                    var first       = resourceVariants.First().Value.GetSprite(0);
                    var emptySprite = new Sprite(first.Sheet, Rectangle.Empty, TextureChannel.Alpha);
                    spriteLayer = new TerrainSpriteLayer(w, wr, emptySprite, first.BlendMode, wr.World.Type != WorldType.Editor);
                }

                if (shadowLayer == null)
                {
                    var firstWithShadow = resourceVariants.Values.FirstOrDefault(v => v.ShadowStart > 0);
                    if (firstWithShadow != null)
                    {
                        var first       = firstWithShadow.GetShadow(0, WAngle.Zero);
                        var emptySprite = new Sprite(first.Sheet, Rectangle.Empty, TextureChannel.Alpha);
                        shadowLayer = new TerrainSpriteLayer(w, wr, emptySprite, first.BlendMode, wr.World.Type != WorldType.Editor);
                    }
                }

                // All resources must share a blend mode
                var sprites = resourceVariants.Values.SelectMany(v => Exts.MakeArray(v.Length, x => v.GetSprite(x)));
                if (sprites.Any(s => s.BlendMode != spriteLayer.BlendMode))
                {
                    throw new InvalidDataException("Resource sprites specify different blend modes. "
                                                   + "Try using different ResourceRenderer traits for resource types that use different blend modes.");
                }
            }

            // Initialize the RenderContent with the initial map state so it is visible
            // through the fog with the Explored Map option enabled
            foreach (var cell in w.Map.AllCells)
            {
                var resource             = ResourceLayer.GetResource(cell);
                var rendererCellContents = CreateRenderCellContents(wr, resource, cell);
                if (rendererCellContents.Type != null)
                {
                    RenderContents[cell] = rendererCellContents;
                    UpdateRenderedSprite(cell, rendererCellContents);
                }
            }
        }
Example #27
0
        public ShroudRenderer(World world, ShroudRendererInfo info)
        {
            var map = world.Map;

            bounds           = map.Bounds;
            useExtendedIndex = info.UseExtendedIndex;

            tiles      = new ShroudTile[map.MapSize.X * map.MapSize.Y];
            tileStride = map.MapSize.X;

            // Force update on first render
            shroudHash = -1;

            // Load sprite variants
            sprites       = new Sprite[info.Variants.Length * info.Index.Length];
            variantStride = info.Index.Length;
            for (var j = 0; j < info.Variants.Length; j++)
            {
                var seq = SequenceProvider.GetSequence(info.Sequence, info.Variants[j]);
                for (var i = 0; i < info.Index.Length; i++)
                {
                    sprites[j * variantStride + i] = seq.GetSprite(i);
                }
            }

            // Mapping of shrouded directions -> sprite index
            spriteMap = new int[useExtendedIndex ? 256 : 16];
            for (var i = 0; i < info.Index.Length; i++)
            {
                spriteMap[info.Index[i]] = i;
            }

            // Set individual tile variants to reduce tiling
            for (var i = 0; i < tiles.Length; i++)
            {
                tiles[i].Variant = Game.CosmeticRandom.Next(info.Variants.Length);
            }

            // Synthesize unexplored tile if it isn't defined
            if (!info.Index.Contains(0))
            {
                var size = new Size(Game.modData.Manifest.TileSize, Game.modData.Manifest.TileSize);
                var data = Exts.MakeArray <byte>(size.Width * size.Height, _ => (byte)info.ShroudColor);
                var s    = Game.modData.SheetBuilder.Add(data, size);
                unexploredTile = new Sprite(s.sheet, s.bounds, s.offset, s.channel, info.ShroudBlend);
            }
            else
            {
                unexploredTile = sprites[spriteMap[0]];
            }
        }
Example #28
0
        public SmudgeLayer(Actor self, SmudgeLayerInfo info)
        {
            Info  = info;
            world = self.World;

            var types = world.Map.SequenceProvider.Sequences(Info.Sequence);

            foreach (var t in types)
            {
                var seq     = world.Map.SequenceProvider.GetSequence(Info.Sequence, t);
                var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x));
                smudges.Add(t, sprites);
            }
        }
Example #29
0
        public D2TerrainLayer(Actor self, D2TerrainLayerInfo info)
        {
            Info  = info;
            world = self.World;

            var sequenceProvider = world.Map.Rules.Sequences;
            var types            = sequenceProvider.Sequences(Info.Sequence);

            foreach (var t in types)
            {
                var seq     = sequenceProvider.GetSequence(Info.Sequence, t);
                var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x));
                sideSprites.Add(t, sprites);
            }
        }
Example #30
0
        void IWorldLoaded.WorldLoaded(World w, WorldRenderer wr)
        {
            var resources = w.WorldActor.TraitsImplementing <ResourceType>()
                            .ToDictionary(r => r.Info.ResourceType, r => r);

            foreach (var r in resources)
            {
                if (spriteLayer == null)
                {
                    var first       = r.Value.Variants.First().Value.GetSprite(0);
                    var emptySprite = new Sprite(first.Sheet, Rectangle.Empty, TextureChannel.Alpha);
                    spriteLayer = new TerrainSpriteLayer(w, wr, emptySprite, first.BlendMode, wr.World.Type != WorldType.Editor);
                }

                if (shadowLayer == null)
                {
                    var firstWithShadow = r.Value.Variants.Values.FirstOrDefault(v => v.ShadowStart > 0);
                    if (firstWithShadow != null)
                    {
                        var first       = firstWithShadow.GetShadow(0, WAngle.Zero);
                        var emptySprite = new Sprite(first.Sheet, Rectangle.Empty, TextureChannel.Alpha);
                        shadowLayer = new TerrainSpriteLayer(w, wr, emptySprite, first.BlendMode, wr.World.Type != WorldType.Editor);
                    }
                }

                // All resources must share a blend mode
                var sprites = r.Value.Variants.Values.SelectMany(v => Exts.MakeArray(v.Length, x => v.GetSprite(x)));
                if (sprites.Any(s => s.BlendMode != spriteLayer.BlendMode))
                {
                    throw new InvalidDataException("Resource sprites specify different blend modes. "
                                                   + "Try using different ResourceRenderer traits for resource types that use different blend modes.");
                }
            }

            // Initialize the RenderContent with the initial map state
            // because the shroud may not be enabled.
            foreach (var cell in w.Map.AllCells)
            {
                var type = ResourceLayer.GetResource(cell).Type;
                if (type != null && Info.RenderTypes.Contains(type.Info.Type))
                {
                    var resourceContent      = ResourceLayer.GetResource(cell);
                    var rendererCellContents = new RendererCellContents(ChooseRandomVariant(resourceContent.Type), resourceContent.Type, resourceContent.Density);
                    RenderContent[cell] = rendererCellContents;
                    UpdateRenderedSprite(cell, rendererCellContents);
                }
            }
        }