Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/GridLightningTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Viewscreen/GridLightning.h"
2#include <chrono>
3#include <cstdlib>
4#include <iostream>
5#include <limits>
6#include <stdexcept>
7using namespace GridLightning;
8static int checks=0;
9static void Check(bool ok,const char* why) { ++checks; if(!ok) throw std::runtime_error(why); }
10static Cell C(short u,float x,float y,float precipitation,float area=1)
11{
12 Cell c; c.u=u;c.v=1;c.ground={x,y,0};c.cloud={x,y,1.8f};c.precipitation=precipitation;c.area=area;return c;
13}
14static bool Near(double a,double b,double tolerance=1e-8) { return std::abs(a-b)<tolerance; }
15static void Distribution()
16{
17 Check(Strength(2)==0 && Strength(6)==0 && Strength(6.1f)>0,"lightning threshold changed");
18 Check(Strength(16)==100 && Strength(8)==4,"precipitation weighting changed");
19 Event event;
20 Check(Near(MaximumRate,1.),"peak calibration is not one event per second");
21 for(float p:{2.f,6.f,6.1f,8.f,10.f,16.f}) {
22 System system; system.SetCells({C(1,-20,0,p),C(2,20,0,p)});
23 system.Update(.001,{},1,0,0,event);
24 const double expected=p>6 ? MaximumRate*(p-2)*(p-2)/196 : 0;
25 Check(Near(system.Rate(),expected),"uniform storm no longer follows calibrated precipitation curve");
26 }
27 System small,large;
28 std::vector<Cell> repeated;
29 for(short i=0;i<4096;++i) repeated.push_back(C(i,float(i%16)-8,float(i/16%16)-8,16));
30 small.SetCells({C(1,0,0,16)});large.SetCells(repeated);
31 small.Update(.001,{},1,0,0,event);large.Update(.001,{},1,0,0,event);
32 Check(Near(small.Rate(),large.Rate()) && Near(large.Rate(),MaximumRate),
33 "sector count multiplied the total strike rate");
34 System mixed;
35 mixed.SetCells({C(1,-20,0,16),C(2,20,0,8),C(3,0,20,2)});
36 mixed.Update(.001,{},1,0,0,event);
37 int heavy=0,light=0;
38 for(int i=0;i<26000;++i) {
39 const auto* chosen=mixed.Choose((i+.5)/26000);
40 Check(chosen && chosen->u!=3,"dry plot selected");
41 if(chosen->u==1) ++heavy; else ++light;
42 }
43 Check(heavy==25000 && light==1000,"storm direction/25-to-1 intensity weighting changed");
44 System coverage;
45 coverage.SetCells({C(1,-20,0,16),C(2,20,0,2)});
46 coverage.Update(.001,{},1,0,0,event);
47 Check(Near(coverage.Rate(),MaximumRate*std::sqrt(.5)),"wet coverage is not normalized");
48 coverage.Update(.001,{},2,0,0,event);
49 Check(Near(coverage.Rate(),MaximumRate*std::sqrt(.5)/2),"nearby-player scaling changed");
50 coverage.Update(.001,{},1,1,0,event);
51 Check(Near(coverage.Rate(),MaximumRate*std::sqrt(.5)*.1),"snow attenuation changed");
52 coverage.Update(.001,{},0,0,0,event);
53 Check(Near(coverage.Rate(),MaximumRate*std::sqrt(.5)),"zero player count broke the rate");
54 System area;
55 area.SetCells({C(1,-20,0,16,1),C(2,20,0,16,.25f)});
56 area.Update(.001,{},1,0,0,event);
57 Check(area.Choose(.79)->u==1 && area.Choose(.81)->u==2,"cube projection cell areas ignored");
58 System range;
59 range.SetCells({C(1,0,0,2),C(2,65,0,16)});
60 range.Update(.001,{},1,0,0,event);
61 Check(range.Rate()==0,"out-of-range storm produced strikes");
62 auto invalid=C(3,0,0,16); invalid.ground.x=std::numeric_limits<float>::quiet_NaN();
63 range.SetCells({C(1,0,0,2),C(1,0,0,2),invalid,C(4,0,0,20)});
64 Check(range.CellCount()==1 && range.Find(1,1) && !range.Find(8,1),"invalid/duplicate cells entered distribution");
65}
66static void CooldownAndClock()
67{
68 Event event;
69 System system(2);
70 system.SetCells({C(1,0,0,16)});
71 bool fired=false;
72 for(int i=0;i<1000 && !fired;++i) fired=system.Update(.1,{},1,0,0,event);
73 Check(fired && event.cell.u==1 && event.seed>=0 && event.seed<=32767
74 && event.chains>=0 && event.chains<=2,"valid scheduled event failed");
75 auto moved=C(99,2,0,16); // another cell inside the discharged neighborhood
76 system.SetCells({moved,C(1,0,0,16)});
77 for(int i=0;i<70;++i)
78 Check(!system.Update(.1,{},1,0,0,event) && system.Rate()==0,
79 "full refresh/grid shift forgot geographic discharge cooldown");
80 for(int i=0;i<15;++i) system.Update(.1,{},1,0,0,event);
81 Check(system.Rate()>0,"discharge neighborhood did not recover");
82 Check(!system.Update(600,{},1,0,0,event),"resume replayed a lightning backlog");
83 Check(!system.Update(0,{},1,0,0,event)
84 && !system.Update(-1,{},1,0,0,event)
85 && !system.Update(std::numeric_limits<double>::quiet_NaN(),{},1,0,0,event),"invalid elapsed time accepted");
86 system.Reset();
87 Check(system.CellCount()==0 && !system.Update(.1,{},1,0,0,event),"connection/W0 reset retained cells");
88
89 // Busy bursts must not evict a still-active neighborhood from cooldown.
90 System burst;
91 std::vector<Cell> burstCells;
92 for(short i=0;i<20;++i) burstCells.push_back(C(i,float(i%5)*12-24,float(i/5)*12-18,16));
93 burst.SetCells(burstCells);
94 for(const auto& cell:burstCells) burst.Observe(cell.ground);
95 burst.Update(.1,{},1,0,0,event);
96 Check(burst.Rate()==0 && !burst.Choose(.5),"busy storm forgot active cooldowns after eight strikes");
97 for(int i=0;i<70;++i) burst.Update(.1,{},1,0,0,event);
98 Check(burst.Rate()==0,"burst cooldown expired too early");
99 for(int i=0;i<12;++i) burst.Update(.1,{},1,0,0,event);
100 Check(burst.Rate()>0,"expired burst cooldowns never recovered");
101
102 const auto simulate=[](double dt) {
103 System s(1977); std::vector<Cell> storm;
104 for(short q=0;q<7;++q) for(short r=0;r<7;++r)
105 storm.push_back(C(short(q*7+r),float(q*12-36),float(r*12-36),16));
106 s.SetCells(storm);
107 Event e; int count=0;
108 for(int i=0;i<int(1200/dt);++i) if(s.Update(dt,{},1,0,0,e)) ++count;
109 return count;
110 };
111 const int fast=simulate(1./60),slow=simulate(.1);
112 Check(fast>900 && fast<1150 && std::abs(fast-slow)<fast*.03,"strike rate depends on frame frequency");
113 std::srand(917); const int expectedFirst=std::rand(),expectedSecond=std::rand();
114 std::srand(917); Check(std::rand()==expectedFirst,"RNG fixture failed");
115 simulate(.1);
116 Check(std::rand()==expectedSecond,"lightning scheduler changed shared gameplay RNG");
117 std::cout<<"Fixed-seed 1200 s: "<<fast<<" strikes at 60 Hz, "<<slow<<" at 10 Hz\n";
118}
119static void Benchmark()
120{
121 System system; Event event; std::vector<Cell> cells;
122 for(short q=0;q<64;++q) for(short r=0;r<64;++r) {
123 auto c=C(short(q*64+r),float(q*2-63),float(r*2-63),16); cells.push_back(c);
124 }
125 system.SetCells(cells);
126 const auto start=std::chrono::steady_clock::now();
127 for(int i=0;i<1000;++i) system.Update(1./60,{},1,0,0,event);
128 const double ms=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-start).count();
129 Check(system.RebuildCount()<100,"weather weights were rebuilt every frame");
130 std::cout<<"4096 cells / 1000 frame updates: "<<ms<<" ms total; "<<system.RebuildCount()<<" distribution rebuilds\n";
131}
132int main()
133{
134 try { Distribution(); CooldownAndClock(); Benchmark(); std::cout<<"PASS "<<checks<<" regional lightning checks\n";return 0; }
135 catch(const std::exception& e) { std::cerr<<"FAIL: "<<e.what()<<'\n';return 1; }
136}
SCRIPT_CONTROL SCRIPT_STATE const void int
void SetCells(std::vector< Cell > next)
void Observe(const Vector3 &ground)
const Cell * Choose(double fraction) const
const Cell * Find(short u, short v) const
bool Update(double elapsed, const Vector3 &observer, int players, float snow, float turbulence, Event &event)
size_t CellCount() const
uint64_t RebuildCount() const
char area[3]
Definition globals.cpp:87
constexpr double MaximumRate
double Strength(float precipitation)
bool Near(const Body &b, const Triangle &t, double extra)
Definition RigidProp.h:141