Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarWeather.cpp
blob: 6fe1a5389896bf6b148c6889cd22a1d67b949422 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "StarWeather.hpp"
#include "StarIterator.hpp"
#include "StarDataStreamExtra.hpp"
#include "StarRoot.hpp"
#include "StarTime.hpp"
#include "StarAssets.hpp"
#include "StarProjectileDatabase.hpp"
#include "StarProjectile.hpp"
#include "StarBiomeDatabase.hpp"

namespace Star {

ServerWeather::ServerWeather() {
  m_undergroundLevel = 0.0f;
  m_currentWeatherIndex = NPos;
  m_currentWeatherIntensity = 0.0f;
  m_currentWind = 0.0f;

  m_currentTime = 0.0;
  m_lastWeatherChangeTime = 0.0;
  m_nextWeatherChangeTime = 0.0;

  m_netGroup.addNetElement(&m_weatherPoolNetState);
  m_netGroup.addNetElement(&m_undergroundLevelNetState);
  m_netGroup.addNetElement(&m_currentWeatherIndexNetState);
  m_netGroup.addNetElement(&m_currentWeatherIntensityNetState);
  m_netGroup.addNetElement(&m_currentWindNetState);
}

void ServerWeather::setup(WeatherPool weatherPool, float undergroundLevel, WorldGeometry worldGeometry,
    WeatherEffectsActiveQuery weatherEffectsActiveQuery) {
  m_weatherPool = weatherPool;
  m_undergroundLevel = undergroundLevel;

  m_worldGeometry = worldGeometry;
  m_weatherEffectsActiveQuery = weatherEffectsActiveQuery;

  m_currentWeatherIndex = NPos;
  m_currentWeatherType = {};

  m_currentTime = 0.0;
  m_lastWeatherChangeTime = 0.0;
  m_nextWeatherChangeTime = 0.0;
}

void ServerWeather::setReferenceClock(ClockConstPtr referenceClock) {
  m_referenceClock = std::move(referenceClock);
  if (m_referenceClock)
    m_clockTrackingTime = m_referenceClock->time();
  else
    m_clockTrackingTime = {};
}

void ServerWeather::setClientVisibleRegions(List<RectI> regions) {
  m_clientVisibleRegions = std::move(regions);
}

pair<ByteArray, uint64_t> ServerWeather::writeUpdate(uint64_t fromVersion, NetCompatibilityRules rules) {
  setNetStates();
  return m_netGroup.writeNetState(fromVersion, rules);
}

void ServerWeather::update(double dt) {
  spawnWeatherProjectiles(dt);

  if (m_referenceClock) {
    double clockTime = m_referenceClock->time();
    if (!m_clockTrackingTime) {
      m_clockTrackingTime = clockTime;
    } else {
      // If our reference clock is set, and we have a valid tracking time, then
      // the dt should be driven by the reference clock.
      dt = clockTime - *m_clockTrackingTime;
      m_clockTrackingTime = clockTime;
    }
  }

  m_currentTime += dt;

  if (!m_weatherPool.empty()) {
    auto assets = Root::singleton().assets();
    double weatherCooldownTime = assets->json("/weather.config:weatherCooldownTime").toDouble();
    double weatherWarmupTime = assets->json("/weather.config:weatherWarmupTime").toDouble();

    if (m_currentTime >= m_nextWeatherChangeTime) {
      m_currentWeatherIndex = m_weatherPool.selectIndex();
      if (m_currentWeatherIndex == NPos)
        m_currentWeatherType = {};
      else
        m_currentWeatherType = Root::singleton().biomeDatabase()->weatherType(m_weatherPool.item(m_currentWeatherIndex));

      m_lastWeatherChangeTime = m_nextWeatherChangeTime;
      m_nextWeatherChangeTime = m_currentTime + Random::randd(m_currentWeatherType->duration[0], m_currentWeatherType->duration[1]);

      // TODO: For now just set the wind at maximum either left or right, nothing exciting.
      m_currentWind = m_currentWeatherType->maximumWind * (Random::randb() ? 1 : -1);
    }

    m_currentWeatherIntensity = min(clamp((m_currentTime - m_lastWeatherChangeTime) / weatherWarmupTime, 0.0, 1.0),
        clamp((m_nextWeatherChangeTime - m_currentTime) / weatherCooldownTime, 0.0, 1.0));

  } else {
    m_currentWeatherIndex = NPos;
    m_currentWeatherType = {};
  }
}

float ServerWeather::wind() const {
  return m_currentWind * m_currentWeatherIntensity;
}

float ServerWeather::weatherIntensity() const {
  return m_currentWeatherIntensity;
}

StringList ServerWeather::statusEffects() const {
  if (m_currentWeatherType && m_currentWeatherIntensity == 1.0)
    return m_currentWeatherType->statusEffects;
  return {};
}

List<ProjectilePtr> ServerWeather::pullNewProjectiles() {
  return take(m_newProjectiles);
}

void ServerWeather::setNetStates() {
  m_weatherPoolNetState.set(DataStreamBuffer::serializeContainer(m_weatherPool.items()));
  m_undergroundLevelNetState.set(m_undergroundLevel);
  m_currentWeatherIndexNetState.set(m_currentWeatherIndex);
  m_currentWeatherIntensityNetState.set(m_currentWeatherIntensity);
  m_currentWindNetState.set(m_currentWind);
}

void ServerWeather::spawnWeatherProjectiles(float dt) {
  if (!m_currentWeatherType || m_clientVisibleRegions.empty())
    return;

  auto projectileDatabase = Root::singleton().projectileDatabase();

  // TODO: The complexity of this method is TERRIBLE, if this becomes a problem
  // for any reason there are large numbers of ways to make this much better,
  // but this was the lazy, simple-ish, and clear (hah) way.

  for (auto const& projectileConfig : m_currentWeatherType->projectiles) {
    // Gather all the tops of the client regions together with the proper
    // padding, splitting at the world wrap boundary.
    List<pair<Vec2I, int>> baseSpawnRegions;
    for (auto const& clientRegion : m_clientVisibleRegions) {
      Vec2I baseRegion = {clientRegion.xMin() - projectileConfig.spawnHorizontalPad, clientRegion.xMax() + projectileConfig.spawnHorizontalPad};
      int height = clientRegion.yMax();
      for (auto const& region : m_worldGeometry.splitXRegion(baseRegion))
        baseSpawnRegions.append({region, height});
    }

    // We are going to have to eliminate vertically redundant sections of
    // spawning regions, so gather up every left and right edge of a spawn
    // region is a "split point"
    List<int> splitPoints;
    for (auto const& baseSpawnRegion : baseSpawnRegions) {
      splitPoints.append(baseSpawnRegion.first[0]);
      splitPoints.append(baseSpawnRegion.first[1]);
    }

    // Split every spawn region on every split point.
    List<pair<Vec2I, int>> splitSpawnRegions;
    for (auto const& baseSpawnRegion : baseSpawnRegions) {
      List<Vec2I> regions = {baseSpawnRegion.first};
      for (auto splitPoint : splitPoints) {
        auto prevRegions = take(regions);
        for (auto const& region : prevRegions) {
          if (splitPoint > region[0] && splitPoint < region[1]) {
            regions.append({region[0], splitPoint});
            regions.append({splitPoint, region[1]});
          } else {
            regions.append(region);
          }
        }
      }
      for (auto const& region : regions)
        splitSpawnRegions.append({region, baseSpawnRegion.second});
    }

    // Sort the split spawn regions by leftmost point then height, preparing to
    // remove the lower overlapping sections.
    sort(splitSpawnRegions,
        [](pair<Vec2I, int> const& lhs, pair<Vec2I, int> rhs) {
          return tie(lhs.first[0], lhs.second) < tie(rhs.first[0], rhs.second);
        });

    // For each region, at this point, if the region to the right shares the
    // same starting X, because we've split up each region on each possible
    // overlapping point, then they totally overlap.  The lower region (which
    // should come before in the list) is totally redundant and should be
    // removed.
    auto sit = makeSMutableIterator(splitSpawnRegions);
    while (sit.hasNext()) {
      auto const& leftRegion = sit.next();
      if (sit.hasNext()) {
        auto const& rightRegion = sit.peekNext();
        if (leftRegion.first[0] == rightRegion.first[0])
          sit.remove();
      }
    }

    for (auto const& spawnRegion : splitSpawnRegions) {
      RectF spawnRect = RectF(spawnRegion.first[0],
          spawnRegion.second,
          spawnRegion.first[1],
          spawnRegion.second + projectileConfig.spawnAboveRegion);

      // Figure out a good target value based on the rate per x tile, making
      // sure to handle very low count values appropriately on average.
      float count = projectileConfig.ratePerX * spawnRect.width() * dt * m_currentWeatherIntensity;
      if (Random::randf() > fpart(count))
        count = floor(count);
      else
        count = ceil(count);

      for (int i = 0; i < count; ++i) {
        Vec2F position = {Random::randf() * spawnRect.width() + spawnRect.xMin(), Random::randf() * spawnRect.height() + spawnRect.yMin()};

        if (position[1] > m_undergroundLevel && (!m_weatherEffectsActiveQuery || m_weatherEffectsActiveQuery(Vec2I::floor(position)))) {
          // Make sure not to spawn projectiles if they intersect any client
          // visible region.
          bool intersectsVisibleRegion = false;
          for (auto const& visibleRegion : m_clientVisibleRegions) {
            if (RectF(visibleRegion).contains(position)) {
              intersectsVisibleRegion = true;
              break;
            }
          }

          if (!intersectsVisibleRegion) {
            auto newProjectile = projectileDatabase->createProjectile(projectileConfig.projectile, projectileConfig.parameters);
            newProjectile->setInitialPosition(position);
            newProjectile->setInitialVelocity(projectileConfig.velocity + Vec2F(projectileConfig.windAffectAmount * wind(), 0));
            newProjectile->setTeam(EntityDamageTeam(TeamType::Environment));
            m_newProjectiles.append(newProjectile);
          }
        }
      }
    }
  }
}

ClientWeather::ClientWeather() {
  m_undergroundLevel = 0.0f;
  m_currentWeatherIndex = NPos;
  m_currentWeatherIntensity = 0.0f;
  m_currentWind = 0.0f;
  m_currentTime = 0.0;

  m_netGroup.addNetElement(&m_weatherPoolNetState);
  m_netGroup.addNetElement(&m_undergroundLevelNetState);
  m_netGroup.addNetElement(&m_currentWeatherIndexNetState);
  m_netGroup.addNetElement(&m_currentWeatherIntensityNetState);
  m_netGroup.addNetElement(&m_currentWindNetState);
}

void ClientWeather::setup(WorldGeometry worldGeometry, WeatherEffectsActiveQuery weatherEffectsActiveQuery) {
  m_worldGeometry = worldGeometry;
  m_weatherEffectsActiveQuery = weatherEffectsActiveQuery;
  m_currentTime = 0.0;
}

void ClientWeather::readUpdate(ByteArray data, NetCompatibilityRules rules) {
  if (!data.empty()) {
    m_netGroup.readNetState(data, 0.0f, rules);
    getNetStates();
  }
}

void ClientWeather::setVisibleRegion(RectI visibleRegion) {
  m_visibleRegion = visibleRegion;
}

void ClientWeather::update(double dt) {
  m_currentTime += dt;

  if (m_currentWeatherIndex == NPos) {
    m_currentWeatherType = {};
  } else {
    if (m_visibleRegion.yMax() > m_undergroundLevel)
      m_currentWeatherType = Root::singleton().biomeDatabase()->weatherType(m_weatherPool.item(m_currentWeatherIndex));
    else
      m_currentWeatherType = {};
  }

  if (m_currentWeatherType)
    spawnWeatherParticles(RectF(m_visibleRegion), dt);
}

float ClientWeather::wind() const {
  return m_currentWind * m_currentWeatherIntensity;
}

float ClientWeather::weatherIntensity() const {
  return m_currentWeatherIntensity;
}

StringList ClientWeather::statusEffects() const {
  if (m_currentWeatherIntensity == 1.0 && m_currentWeatherType)
    return m_currentWeatherType->statusEffects;
  return {};
}

List<Particle> ClientWeather::pullNewParticles() {
  return take(m_particles);
}

StringList ClientWeather::weatherTrackOptions() const {
  if (m_currentWeatherType)
    return m_currentWeatherType->weatherNoises;
  return {};
}

void ClientWeather::getNetStates() {
  if (m_weatherPoolNetState.pullUpdated())
    m_weatherPool = WeatherPool(DataStreamBuffer::deserializeContainer<WeatherPool::ItemsList>(m_weatherPoolNetState.get()));
  m_undergroundLevel = m_undergroundLevelNetState.get();
  m_currentWeatherIndex = m_currentWeatherIndexNetState.get();
  m_currentWeatherIntensity = m_currentWeatherIntensityNetState.get();
  m_currentWind = m_currentWindNetState.get();
}

void ClientWeather::spawnWeatherParticles(RectF newClientRegion, float dt) {
  if (!m_currentWeatherType)
    return;

  for (auto const& particleConfig : m_currentWeatherType->particles) {
    // Move client region to same wrap region as newClientRegion
    RectF visibleRegion(m_worldGeometry.nearestTo(newClientRegion.min(), m_lastParticleVisibleRegion.min()),
        m_worldGeometry.nearestTo(newClientRegion.min(), m_lastParticleVisibleRegion.max()));

    Vec2F targetVelocity = particleConfig.particle.velocity + Vec2F(wind(), 0);
    float angleChange = Vec2F::angleBetween2(Vec2F(0, 1), targetVelocity);
    visibleRegion.translate(targetVelocity * dt);

    for (auto const& renderZone : newClientRegion.subtract(visibleRegion)) {
      float count = particleConfig.density * renderZone.width() * renderZone.height() * m_currentWeatherIntensity;
      if (Random::randf() > fpart(count))
        count = std::floor(count);
      else
        count = std::ceil(count);

      for (int i = 0; i < count; ++i) {
        auto newParticle = particleConfig.particle;
        float x = Random::randf() * renderZone.width() + renderZone.xMin();
        float y = Random::randf() * renderZone.height() + renderZone.yMin();
        newParticle.position += m_worldGeometry.xwrap(Vec2F(x, y));
        newParticle.velocity = targetVelocity;
        if (y > m_undergroundLevel && (!m_weatherEffectsActiveQuery || m_weatherEffectsActiveQuery(Vec2I::floor(newParticle.position)))) {
          if (particleConfig.autoRotate)
            newParticle.rotation += angleChange;
          m_particles.append(std::move(newParticle));
        }
      }
    }
  }

  m_lastParticleVisibleRegion = newClientRegion;
}

}