Terraria ModLoader  0.11.7.8
A mod to make and play Terraria mods
MapLoader.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using Terraria.ID;
5 using Terraria.Localization;
6 using Terraria.Map;
7 
8 namespace Terraria.ModLoader
9 {
10  //todo: further documentation
11  internal static class MapLoader
12  {
13  internal static bool initialized = false;
14  internal static readonly IDictionary<ushort, IList<MapEntry>> tileEntries = new Dictionary<ushort, IList<MapEntry>>();
15  internal static readonly IDictionary<ushort, IList<MapEntry>> wallEntries = new Dictionary<ushort, IList<MapEntry>>();
16  internal static readonly IDictionary<ushort, Func<string, int, int, string>> nameFuncs =
17  new Dictionary<ushort, Func<string, int, int, string>>();
18  internal static readonly IDictionary<ushort, ushort> entryToTile = new Dictionary<ushort, ushort>();
19  internal static readonly IDictionary<ushort, ushort> entryToWall = new Dictionary<ushort, ushort>();
20 
21  internal static int modTileOptions(ushort type) {
22  return tileEntries[type].Count;
23  }
24 
25  internal static int modWallOptions(ushort type) {
26  return wallEntries[type].Count;
27  }
28  //make Terraria.Map.MapHelper.colorLookup internal
29  //add internal modPosition field to Terraria.Map.MapHelper
30  //near end of Terraria.Map.MapHelper.Initialize set modPosition to num11 + 1
31  //in Terraria.Map.MapHelper.SaveMap add mod-type-check to darkness check
32  internal static void SetupModMap() {
33  if (Main.dedServ) {
34  return;
35  }
36  Array.Resize(ref MapHelper.tileLookup, TileLoader.TileCount);
37  Array.Resize(ref MapHelper.wallLookup, WallLoader.WallCount);
38  IList<Color> colors = new List<Color>();
39  IList<LocalizedText> names = new List<LocalizedText>();
40  foreach (ushort type in tileEntries.Keys) {
41  MapHelper.tileLookup[type] = (ushort)(MapHelper.modPosition + colors.Count);
42  foreach (MapEntry entry in tileEntries[type]) {
43  ushort mapType = (ushort)(MapHelper.modPosition + colors.Count);
44  entryToTile[mapType] = type;
45  nameFuncs[mapType] = entry.getName;
46  colors.Add(entry.color);
47  if (entry.name != null) {
48  names.Add(entry.name);
49  }
50  else {
51  names.Add(Language.GetText(entry.translation.Key));
52  }
53  }
54  }
55  foreach (ushort type in wallEntries.Keys) {
56  MapHelper.wallLookup[type] = (ushort)(MapHelper.modPosition + colors.Count);
57  foreach (MapEntry entry in wallEntries[type]) {
58  ushort mapType = (ushort)(MapHelper.modPosition + colors.Count);
59  entryToWall[mapType] = type;
60  nameFuncs[mapType] = entry.getName;
61  colors.Add(entry.color);
62  if (entry.name != null) {
63  names.Add(entry.name);
64  }
65  else {
66  names.Add(Language.GetText(entry.translation.Key));
67  }
68  }
69  }
70  Array.Resize(ref MapHelper.colorLookup, MapHelper.modPosition + colors.Count);
71  Lang._mapLegendCache.Resize(MapHelper.modPosition + names.Count);
72  for (int k = 0; k < colors.Count; k++) {
73  MapHelper.colorLookup[MapHelper.modPosition + k] = colors[k];
74  Lang._mapLegendCache[MapHelper.modPosition + k] = names[k];
75  }
76  initialized = true;
77  }
78 
79  internal static void UnloadModMap() {
80  tileEntries.Clear();
81  wallEntries.Clear();
82  if (Main.dedServ) {
83  return;
84  }
85  nameFuncs.Clear();
86  entryToTile.Clear();
87  entryToWall.Clear();
88  Array.Resize(ref MapHelper.tileLookup, TileID.Count);
89  Array.Resize(ref MapHelper.wallLookup, WallID.Count);
90  Array.Resize(ref MapHelper.colorLookup, MapHelper.modPosition);
91  Lang._mapLegendCache.Resize(MapHelper.modPosition);
92  initialized = false;
93  }
94  //at end of Terraria.Map.MapHelper.CreateMapTile before returning call
95  // MapLoader.ModMapOption(ref num16, i, j);
96  internal static void ModMapOption(ref ushort mapType, int i, int j) {
97  if (entryToTile.ContainsKey(mapType)) {
98  ModTile tile = TileLoader.GetTile(entryToTile[mapType]);
99  ushort option = tile.GetMapOption(i, j);
100  if (option < 0 || option >= modTileOptions(tile.Type)) {
101  throw new ArgumentOutOfRangeException("Bad map option for tile " + tile.Name + " from mod " + tile.mod.Name);
102  }
103  mapType += option;
104  }
105  else if (entryToWall.ContainsKey(mapType)) {
106  ModWall wall = WallLoader.GetWall(entryToWall[mapType]);
107  ushort option = wall.GetMapOption(i, j);
108  if (option < 0 || option >= modWallOptions(wall.Type)) {
109  throw new ArgumentOutOfRangeException("Bad map option for wall " + wall.Name + " from mod " + wall.mod.Name);
110  }
111  mapType += option;
112  }
113  }
114  }
115 }