Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WorldMap.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using Terraria.IO;
6
7namespace Terraria.Map;
8
9public class WorldMap
10{
11 public readonly int MaxWidth;
12
13 public readonly int MaxHeight;
14
15 public const int BlackEdgeWidth = 40;
16
17 private MapTile[,] _tiles;
18
19 public MapTile this[int x, int y] => _tiles[x, y];
20
21 public WorldMap(int maxWidth, int maxHeight)
22 {
23 MaxWidth = maxWidth;
24 MaxHeight = maxHeight;
26 }
27
28 public void ConsumeUpdate(int x, int y)
29 {
30 _tiles[x, y].IsChanged = false;
31 }
32
33 public void Update(int x, int y, byte light)
34 {
35 _tiles[x, y] = MapHelper.CreateMapTile(x, y, light);
36 }
37
38 public void SetTile(int x, int y, ref MapTile tile)
39 {
40 _tiles[x, y] = tile;
41 }
42
43 public bool IsRevealed(int x, int y)
44 {
45 return _tiles[x, y].Light > 0;
46 }
47
48 public bool UpdateLighting(int x, int y, byte light)
49 {
50 MapTile other = _tiles[x, y];
51 if (light == 0 && other.Light == 0)
52 {
53 return false;
54 }
55 MapTile mapTile = MapHelper.CreateMapTile(x, y, Math.Max(other.Light, light));
56 if (mapTile.Equals(ref other))
57 {
58 return false;
59 }
60 _tiles[x, y] = mapTile;
61 return true;
62 }
63
64 public bool UpdateType(int x, int y)
65 {
66 MapTile mapTile = MapHelper.CreateMapTile(x, y, _tiles[x, y].Light);
67 if (mapTile.Equals(ref _tiles[x, y]))
68 {
69 return false;
70 }
71 _tiles[x, y] = mapTile;
72 return true;
73 }
74
75 public void UnlockMapSection(int sectionX, int sectionY)
76 {
77 }
78
79 public void Load()
80 {
82 bool isCloudSave = Main.ActivePlayerFileData.IsCloudSave;
83 if ((isCloudSave && SocialAPI.Cloud == null) || !Main.mapEnabled)
84 {
85 return;
86 }
87 string text = Main.playerPathName.Substring(0, Main.playerPathName.Length - 4) + Path.DirectorySeparatorChar;
88 if (Main.ActiveWorldFileData.UseGuidAsMapName)
89 {
90 string text2 = text;
91 text = string.Concat(text, Main.ActiveWorldFileData.UniqueId, ".map");
92 if (!FileUtilities.Exists(text, isCloudSave))
93 {
94 text = text2 + Main.worldID + ".map";
95 }
96 }
97 else
98 {
99 text = text + Main.worldID + ".map";
100 }
101 if (!FileUtilities.Exists(text, isCloudSave))
102 {
103 Main.MapFileMetadata = FileMetadata.FromCurrentSettings(FileType.Map);
104 return;
105 }
107 using BinaryReader binaryReader = new BinaryReader(input);
108 try
109 {
110 int num = binaryReader.ReadInt32();
111 if (num <= 279)
112 {
113 if (num <= 91)
114 {
115 MapHelper.LoadMapVersion1(binaryReader, num);
116 }
117 else
118 {
119 MapHelper.LoadMapVersion2(binaryReader, num);
120 }
121 ClearEdges();
122 Main.clearMap = true;
123 Main.loadMap = true;
124 Main.loadMapLock = true;
125 Main.refreshMap = false;
126 }
127 }
128 catch (Exception value)
129 {
130 using (StreamWriter streamWriter = new StreamWriter("client-crashlog.txt", append: true))
131 {
132 streamWriter.WriteLine(DateTime.Now);
133 streamWriter.WriteLine(value);
134 streamWriter.WriteLine("");
135 }
136 if (!isCloudSave)
137 {
138 File.Copy(text, text + ".bad", overwrite: true);
139 }
140 Clear();
141 }
142 }
143
144 public void Save()
145 {
147 }
148
149 public void Clear()
150 {
151 for (int i = 0; i < MaxWidth; i++)
152 {
153 for (int j = 0; j < MaxHeight; j++)
154 {
155 _tiles[i, j].Clear();
156 }
157 }
158 }
159
160 public void ClearEdges()
161 {
162 for (int i = 0; i < MaxWidth; i++)
163 {
164 for (int j = 0; j < 40; j++)
165 {
166 _tiles[i, j].Clear();
167 }
168 }
169 for (int k = 0; k < MaxWidth; k++)
170 {
171 for (int l = MaxHeight - 40; l < MaxHeight; l++)
172 {
173 _tiles[k, l].Clear();
174 }
175 }
176 for (int m = 0; m < 40; m++)
177 {
178 for (int n = 40; n < MaxHeight - 40; n++)
179 {
180 _tiles[m, n].Clear();
181 }
182 }
183 for (int num = MaxWidth - 40; num < MaxWidth; num++)
184 {
185 for (int num2 = 40; num2 < MaxHeight - 40; num2++)
186 {
187 _tiles[num, num2].Clear();
188 }
189 }
190 }
191}
static void Copy(string sourceFileName, string destFileName)
Definition File.cs:47
static readonly char DirectorySeparatorChar
Definition Path.cs:71
override void WriteLine(string? value)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static FileMetadata FromCurrentSettings(FileType type)
static void Clear()
Definition Lighting.cs:177
static WorldFileData ActiveWorldFileData
Definition Main.cs:1946
static string playerPathName
Definition Main.cs:2835
static PlayerFileData ActivePlayerFileData
Definition Main.cs:1940
static bool mapEnabled
Definition Main.cs:906
static void LoadMapVersion2(BinaryReader fileIO, int release)
static void LoadMapVersion1(BinaryReader fileIO, int release)
static MapTile CreateMapTile(int i, int j, byte Light)
static void SaveMap()
readonly int MaxHeight
Definition WorldMap.cs:13
WorldMap(int maxWidth, int maxHeight)
Definition WorldMap.cs:21
readonly int MaxWidth
Definition WorldMap.cs:11
const int BlackEdgeWidth
Definition WorldMap.cs:15
void ConsumeUpdate(int x, int y)
Definition WorldMap.cs:28
bool UpdateType(int x, int y)
Definition WorldMap.cs:64
bool UpdateLighting(int x, int y, byte light)
Definition WorldMap.cs:48
bool IsRevealed(int x, int y)
Definition WorldMap.cs:43
MapTile[,] _tiles
Definition WorldMap.cs:17
void Update(int x, int y, byte light)
Definition WorldMap.cs:33
void UnlockMapSection(int sectionX, int sectionY)
Definition WorldMap.cs:75
void SetTile(int x, int y, ref MapTile tile)
Definition WorldMap.cs:38
static Terraria.Social.Base.CloudSocialModule Cloud
Definition SocialAPI.cs:18
static bool Exists(string path, bool cloud)
static byte[] ReadAllBytes(string path, bool cloud)
static DateTime Now
Definition DateTime.cs:103
bool Equals(ref MapTile other)
Definition MapTile.cs:49