Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MinecartDiggerHelper.cs
Go to the documentation of this file.
3using Terraria.ID;
4
6
8{
10
11 public void TryDigging(Player player, Vector2 trackWorldPosition, int digDirectionX, int digDirectionY)
12 {
13 digDirectionY = 0;
14 Point point = trackWorldPosition.ToTileCoordinates();
15 if (Framing.GetTileSafely(point).type != 314 || (double)point.Y < Main.worldSurface)
16 {
17 return;
18 }
19 Point point2 = point;
20 point2.X += digDirectionX;
21 point2.Y += digDirectionY;
22 if (AlreadyLeadsIntoWantedTrack(point, point2) || (digDirectionY == 0 && (AlreadyLeadsIntoWantedTrack(point, new Point(point2.X, point2.Y - 1)) || AlreadyLeadsIntoWantedTrack(point, new Point(point2.X, point2.Y + 1)))))
23 {
24 return;
25 }
26 int num = 5;
27 if (digDirectionY != 0)
28 {
29 num = 5;
30 }
31 Point point3 = point2;
32 Point point4 = point3;
33 point4.Y -= num - 1;
34 int x = point4.X;
35 for (int i = point4.Y; i <= point3.Y; i++)
36 {
37 if (!CanGetPastTile(x, i) || !HasPickPower(player, x, i))
38 {
39 return;
40 }
41 }
42 if (CanConsumeATrackItem(player))
43 {
44 int x2 = point4.X;
45 for (int j = point4.Y; j <= point3.Y; j++)
46 {
48 }
49 ConsumeATrackItem(player);
50 PlaceATrack(point2.X, point2.Y);
51 player.velocity.X = MathHelper.Clamp(player.velocity.X, -1f, 1f);
52 if (!DoTheTracksConnectProperly(point, point2))
53 {
54 CorrectTrackConnections(point, point2);
55 }
56 }
57 }
58
59 private bool CanConsumeATrackItem(Player player)
60 {
61 return FindMinecartTrackItem(player) != null;
62 }
63
64 private void ConsumeATrackItem(Player player)
65 {
66 Item item = FindMinecartTrackItem(player);
67 item.stack--;
68 if (item.stack == 0)
69 {
70 item.TurnToAir();
71 }
72 }
73
75 {
76 Item result = null;
77 for (int i = 0; i < 58; i++)
78 {
79 if (player.selectedItem != i || (player.itemAnimation <= 0 && player.reuseDelay <= 0 && player.itemTime <= 0))
80 {
81 Item item = player.inventory[i];
82 if (item.type == 2340 && item.stack > 0)
83 {
84 result = item;
85 break;
86 }
87 }
88 }
89 return result;
90 }
91
92 private void PoundTrack(Point spot)
93 {
94 if (Main.tile[spot.X, spot.Y].type == 314 && Minecart.FrameTrack(spot.X, spot.Y, pound: true) && Main.netMode == 1)
95 {
96 NetMessage.SendData(17, -1, -1, null, 15, spot.X, spot.Y, 1f);
97 }
98 }
99
100 private bool AlreadyLeadsIntoWantedTrack(Point tileCoordsOfFrontWheel, Point tileCoordsWeWantToReach)
101 {
102 Tile tileSafely = Framing.GetTileSafely(tileCoordsOfFrontWheel);
103 Tile tileSafely2 = Framing.GetTileSafely(tileCoordsWeWantToReach);
104 if (!tileSafely.active() || tileSafely.type != 314)
105 {
106 return false;
107 }
108 if (!tileSafely2.active() || tileSafely2.type != 314)
109 {
110 return false;
111 }
112 GetExpectedDirections(tileCoordsOfFrontWheel, tileCoordsWeWantToReach, out var expectedStartLeft, out var expectedStartRight, out var expectedEndLeft, out var expectedEndRight);
113 if (!Minecart.GetAreExpectationsForSidesMet(tileCoordsOfFrontWheel, expectedStartLeft, expectedStartRight))
114 {
115 return false;
116 }
117 if (!Minecart.GetAreExpectationsForSidesMet(tileCoordsWeWantToReach, expectedEndLeft, expectedEndRight))
118 {
119 return false;
120 }
121 return true;
122 }
123
124 private static void GetExpectedDirections(Point startCoords, Point endCoords, out int? expectedStartLeft, out int? expectedStartRight, out int? expectedEndLeft, out int? expectedEndRight)
125 {
126 int num = endCoords.Y - startCoords.Y;
127 int num2 = endCoords.X - startCoords.X;
128 expectedStartLeft = null;
129 expectedStartRight = null;
130 expectedEndLeft = null;
131 expectedEndRight = null;
132 if (num2 == -1)
133 {
134 expectedStartLeft = num;
135 expectedEndRight = -num;
136 }
137 if (num2 == 1)
138 {
139 expectedStartRight = num;
140 expectedEndLeft = -num;
141 }
142 }
143
144 private bool DoTheTracksConnectProperly(Point tileCoordsOfFrontWheel, Point tileCoordsWeWantToReach)
145 {
146 return AlreadyLeadsIntoWantedTrack(tileCoordsOfFrontWheel, tileCoordsWeWantToReach);
147 }
148
149 private void CorrectTrackConnections(Point startCoords, Point endCoords)
150 {
151 GetExpectedDirections(startCoords, endCoords, out var expectedStartLeft, out var expectedStartRight, out var expectedEndLeft, out var expectedEndRight);
152 Tile tileSafely = Framing.GetTileSafely(startCoords);
153 Tile tileSafely2 = Framing.GetTileSafely(endCoords);
154 if (tileSafely.active() && tileSafely.type == 314)
155 {
156 Minecart.TryFittingTileOrientation(startCoords, expectedStartLeft, expectedStartRight);
157 }
158 if (tileSafely2.active() && tileSafely2.type == 314)
159 {
160 Minecart.TryFittingTileOrientation(endCoords, expectedEndLeft, expectedEndRight);
161 }
162 }
163
164 private bool HasPickPower(Player player, int x, int y)
165 {
166 if (player.HasEnoughPickPowerToHurtTile(x, y))
167 {
168 return true;
169 }
170 return false;
171 }
172
173 private bool CanGetPastTile(int x, int y)
174 {
175 if (WorldGen.CheckTileBreakability(x, y) != 0)
176 {
177 return false;
178 }
180 {
181 return false;
182 }
183 Tile tile = Main.tile[x, y];
184 if (tile.active() && (TileID.Sets.Falling[tile.type] || (tile.type == 26 && !Main.hardMode) || !WorldGen.CanKillTile(x, y)))
185 {
186 return false;
187 }
188 return true;
189 }
190
191 private void PlaceATrack(int x, int y)
192 {
193 int num = 314;
194 int num2 = 0;
195 if (WorldGen.PlaceTile(x, y, num, mute: false, forced: false, Main.myPlayer, num2))
196 {
197 NetMessage.SendData(17, -1, -1, null, 1, x, y, num, num2);
198 }
199 }
200
201 private void MineTheTileIfNecessary(int x, int y)
202 {
203 AchievementsHelper.CurrentlyMining = true;
204 if (Main.tile[x, y].active())
205 {
206 WorldGen.KillTile(x, y);
207 NetMessage.SendData(17, -1, -1, null, 0, x, y);
208 }
209 AchievementsHelper.CurrentlyMining = false;
210 }
211}
static float Clamp(float value, float min, float max)
Definition MathHelper.cs:46
Vector2 velocity
Definition Entity.cs:16
static Tile GetTileSafely(Vector2 position)
Definition Framing.cs:419
void TryDigging(Player player, Vector2 trackWorldPosition, int digDirectionX, int digDirectionY)
bool DoTheTracksConnectProperly(Point tileCoordsOfFrontWheel, Point tileCoordsWeWantToReach)
static void GetExpectedDirections(Point startCoords, Point endCoords, out int? expectedStartLeft, out int? expectedStartRight, out int? expectedEndLeft, out int? expectedEndRight)
bool HasPickPower(Player player, int x, int y)
void CorrectTrackConnections(Point startCoords, Point endCoords)
bool AlreadyLeadsIntoWantedTrack(Point tileCoordsOfFrontWheel, Point tileCoordsWeWantToReach)
static bool[] Falling
Definition TileID.cs:289
static double worldSurface
Definition Main.cs:1272
static int myPlayer
Definition Main.cs:1801
static int netMode
Definition Main.cs:2095
static Tile[,] tile
Definition Main.cs:1675
static bool hardMode
Definition Main.cs:1022
static bool FrameTrack(int i, int j, bool pound, bool mute=false)
Definition Minecart.cs:934
static void TryFittingTileOrientation(Point tileCoords, int? expectedYOffsetForLeft, int? expectedYOffsetForRight)
Definition Minecart.cs:1550
static bool GetAreExpectationsForSidesMet(Point tileCoords, int? expectedYOffsetForLeft, int? expectedYOffsetForRight)
Definition Minecart.cs:1526
static void SendData(int msgType, int remoteClient=-1, int ignoreClient=-1, NetworkText text=null, int number=0, float number2=0f, float number3=0f, float number4=0f, int number5=0, int number6=0, int number7=0)
Definition NetMessage.cs:88
bool HasEnoughPickPowerToHurtTile(int x, int y)
Definition Player.cs:50351
Item[] inventory
Definition Player.cs:1257
ushort type
Definition Tile.cs:8
bool active()
Definition Tile.cs:565
static void KillTile(int i, int j, bool fail=false, bool effectOnly=false, bool noItem=false)
static bool PlaceTile(int i, int j, int Type, bool mute=false, bool forced=false, int plr=-1, int style=0)
static bool CanKillTile(int i, int j, SpecialKillTileContext context)
static int CheckTileBreakability(int x, int y)
static bool CheckTileBreakability2_ShouldTileSurvive(int x, int y)