Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
AmbienceServer.cs
Go to the documentation of this file.
1using System;
3using System.Linq;
5using Terraria.Net;
6
8
9public class AmbienceServer
10{
11 public struct AmbienceSpawnInfo
12 {
14
15 public int targetPlayer;
16 }
17
18 private const int MINIMUM_SECONDS_BETWEEN_SPAWNS = 10;
19
20 private const int MAXIMUM_SECONDS_BETWEEN_SPAWNS = 120;
21
23
25
27
29
30 private static bool IsSunnyDay()
31 {
33 {
34 return !Main.eclipse;
35 }
36 return false;
37 }
38
39 private static bool IsSunset()
40 {
41 if (Main.dayTime)
42 {
43 return Main.time > 40500.0;
44 }
45 return false;
46 }
47
48 private static bool IsCalmNight()
49 {
51 {
52 return !Main.snowMoon;
53 }
54 return false;
55 }
56
58 {
65 _spawnConditions[SkyEntityType.Butterflies] = () => IsSunnyDay() && !Main.IsItAHappyWindyDay && !NPC.TooWindyForButterflies && NPC.butterflyChance < 6;
67 _spawnConditions[SkyEntityType.Vulture] = () => IsSunnyDay();
69 _spawnConditions[SkyEntityType.PixiePosse] = () => IsSunnyDay() || IsCalmNight();
70 _spawnConditions[SkyEntityType.Seagulls] = () => IsSunnyDay();
72 _spawnConditions[SkyEntityType.Gastropods] = () => IsCalmNight();
73 _spawnConditions[SkyEntityType.Pegasus] = () => IsSunnyDay();
74 _spawnConditions[SkyEntityType.EaterOfSouls] = () => IsSunnyDay() || IsCalmNight();
76 _spawnConditions[SkyEntityType.Hellbats] = () => true;
77 _secondarySpawnConditionsPerPlayer[SkyEntityType.Vulture] = (Player player) => player.ZoneDesert;
78 _secondarySpawnConditionsPerPlayer[SkyEntityType.PixiePosse] = (Player player) => player.ZoneHallow;
79 _secondarySpawnConditionsPerPlayer[SkyEntityType.Seagulls] = (Player player) => player.ZoneBeach;
80 _secondarySpawnConditionsPerPlayer[SkyEntityType.Gastropods] = (Player player) => player.ZoneHallow;
81 _secondarySpawnConditionsPerPlayer[SkyEntityType.Pegasus] = (Player player) => player.ZoneHallow;
82 _secondarySpawnConditionsPerPlayer[SkyEntityType.EaterOfSouls] = (Player player) => player.ZoneCorrupt;
83 _secondarySpawnConditionsPerPlayer[SkyEntityType.Crimera] = (Player player) => player.ZoneCrimson;
84 _secondarySpawnConditionsPerPlayer[SkyEntityType.Bats] = (Player player) => player.ZoneJungle;
85 }
86
95
96 public void Update()
97 {
100 {
102 return;
103 }
106 where pair.Value()
107 select pair.Key;
108 if (source.Count((SkyEntityType type) => true) == 0)
109 {
110 return;
111 }
113 if (player == null)
114 {
115 return;
116 }
118 int num = source2.Count((SkyEntityType type) => true);
119 if (num == 0 || Main.rand.Next(5) < 3)
120 {
122 num = source2.Count((SkyEntityType type) => true);
123 }
124 if (num != 0)
125 {
126 SkyEntityType type2 = source2.ElementAt(Main.rand.Next(num));
127 SpawnForPlayer(player, type2);
128 }
129 }
130
131 public void ResetSpawnTime()
132 {
133 _updatesUntilNextAttempt = Main.rand.Next(600, 7200);
135 {
137 }
138 }
139
144
145 private void SpawnForcedEntities()
146 {
147 if (_forcedSpawns.Count == 0)
148 {
149 return;
150 }
151 for (int num = _forcedSpawns.Count - 1; num >= 0; num--)
152 {
154 Player player;
155 if (ambienceSpawnInfo.targetPlayer == -1)
156 {
158 }
159 else
160 {
161 player = Main.player[ambienceSpawnInfo.targetPlayer];
162 }
163 if (player != null && IsPlayerAtRightHeightForType(ambienceSpawnInfo.skyEntityType, player))
164 {
165 SpawnForPlayer(player, ambienceSpawnInfo.skyEntityType);
166 }
167 _forcedSpawns.RemoveAt(num);
168 }
169 }
170
172 {
173 player = null;
174 int num = Main.player.Count((Player plr) => plr.active && IsPlayerInAPlaceWhereTheyCanSeeAmbience(plr));
175 if (num != 0)
176 {
177 player = Main.player.Where((Player plr) => plr.active && IsPlayerInAPlaceWhereTheyCanSeeAmbience(plr)).ElementAt(Main.rand.Next(num));
178 }
179 }
180
189
191 {
192 return (double)plr.position.Y <= Main.worldSurface * 16.0 + 1600.0;
193 }
194
196 {
197 return plr.position.Y >= (float)((Main.UnderworldLayer - 100) * 16);
198 }
199
201 {
203 }
204}
void Add(TKey key, TValue value)
static void FindPlayerThatCanSeeBackgroundAmbience(out Player player)
static bool IsPlayerInAPlaceWhereTheyCanSeeAmbienceSky(Player plr)
bool IsPlayerAtRightHeightForType(SkyEntityType type, Player plr)
static bool IsPlayerInAPlaceWhereTheyCanSeeAmbienceHell(Player plr)
readonly Dictionary< SkyEntityType, Func< Player, bool > > _secondarySpawnConditionsPerPlayer
void ForceEntitySpawn(AmbienceSpawnInfo info)
static bool IsPlayerInAPlaceWhereTheyCanSeeAmbience(Player plr)
readonly Dictionary< SkyEntityType, Func< bool > > _spawnConditions
void SpawnForPlayer(Player player, SkyEntityType type)
static NetPacket SerializeSkyEntitySpawn(Player player, SkyEntityType type)
static bool IsItAHappyWindyDay
Definition Main.cs:2869
static bool IsItRaining
Definition Main.cs:2664
static bool dayTime
Definition Main.cs:1282
static bool bloodMoon
Definition Main.cs:1296
static bool eclipse
Definition Main.cs:1312
static UnifiedRandom rand
Definition Main.cs:1387
static bool tenthAnniversaryWorld
Definition Main.cs:343
static int dayRate
Definition Main.cs:746
static int UnderworldLayer
Definition Main.cs:2825
static bool snowMoon
Definition Main.cs:1300
static bool pumpkinMoon
Definition Main.cs:1298
static bool hardMode
Definition Main.cs:1022
static Player[] player
Definition Main.cs:1803
static readonly NetManager Instance
Definition NetManager.cs:18