Repository navigation
Expand file tree
/
Copy pathevalbuildingscript.cpp
More file actions
executable file
·223 lines (185 loc) · 5.83 KB
/
Copy pathevalbuildingscript.cpp
File metadata and controls
executable file
·223 lines (185 loc) · 5.83 KB
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
#include "evalbuildingscript.h"
#include "plantscript.h"
#include "corescript.h"
#include "../game/lumoschitbag.h"
#include "../game/gameitem.h"
#include "../game/mapspatialcomponent.h"
#include "../game/worldmap.h"
#include "../game/fluidsim.h"
#include "../game/worldinfo.h"
#include "../game/physicssims.h"
#include "../grinliz/glstringutil.h"
#include "../xegame/istringconst.h"
#include "../xegame/spatialcomponent.h"
#include "../xegame/chitcontext.h"
using namespace grinliz;
void EvalBuildingScript::Serialize(XStream* xs)
{
BeginSerialize(xs, Name());
EndSerialize(xs);
}
void EvalBuildingScript::OnAdd(Chit* chit, bool init)
{
super::OnAdd(chit, init);
timer.SetPeriod(2000 + parentChit->random.Rand(1000));
}
double EvalBuildingScript::EvalIndustrial( bool debugLog )
{
Chit* building = ParentChit();
int hitB = 0, hitIBuilding = 0, hitNBuilding = 0, hitWater = 0, hitPlant = 0, hitRock = 0, hitWaterfall = 0;
int hitShrub = 0; // doesn't terminate a ray.
if (lastEval == 0 || (Context()->chitBag->AbsTime() - lastEval) > 2000) {
lastEval = Context()->chitBag->AbsTime();
GameItem* item = building->GetItem();
GLASSERT(item);
reachable = true;
IString consume = item->keyValues.GetIString(ISC::zone);
if (consume.empty()) {
eval = 0;
return eval;
}
MapSpatialComponent* msc = GET_SUB_COMPONENT(building, SpatialComponent, MapSpatialComponent);
GLASSERT(msc);
if (!msc) {
eval = 0;
return eval;
}
Rectangle2I porch = msc->PorchPos();
static const int RAD = 4;
Rectangle2I bounds = porch;
if (porch.min.x == 0) { // shouldn't happen, but be sure.
GLASSERT(0);
eval = 0;
return eval;
}
bounds.Outset(RAD);
Vector2I sector = ToSector(building->Position());
WorldMap* worldMap = Context()->worldMap;
Rectangle2I mapBounds = worldMap->Bounds();
if (!mapBounds.Contains(bounds)) {
eval = 0;
return eval; // not worth dealing with edge of world
}
// Check if we can go from the core to the porch.
// And make sure the core is inUse!
CoreScript* cs = CoreScript::GetCore(ToSector(porch.min));
if (!cs || !cs->InUse())
reachable = false;
if (reachable) {
const SectorData& sd = worldMap->GetSectorData(ToSector(porch.min));
reachable = worldMap->CalcPath(ToWorld2F(sd.core), ToWorld2F(porch.min), 0, 0, false);
}
CChitArray arr;
BuildingFilter buildingFilter;
const FluidSim* fluidSim = Context()->physicsSims->GetFluidSim(sector);
bool hasWaterfalls = fluidSim->NumWaterfalls() > 0;
LumosChitBag* chitBag = Context()->chitBag;
Rectangle2IEdgeIterator it(bounds);
while (!it.Done()) {
Vector2I pos = { it.Pos().x >= porch.max.x ? porch.max.x : porch.min.x,
it.Pos().y >= porch.max.y ? porch.max.y : porch.min.y };
LineWalk walk(pos.x, pos.y, it.Pos().x, it.Pos().y);
walk.Step(); // ignore where we are standing.
while ( !walk.Done() ) { // non-intuitive iterator. See linewalk docs.
// - building
// - plant
// - ice
// - rock
// - waterfall
// - water
// Buildings. Can be 2x2. Extend out beyond current check.
bool hitBuilding = false;
Vector2I p = walk.P();
// Don't count self as a hit, but stops the ray cast.
// Also, use a larger radius because buildings can be 2x2
chitBag->QuerySpatialHash(&arr, ToWorld2F(p), 0.8f, 0, &buildingFilter);
for (int i = 0; i < arr.Size(); ++i) {
if (arr[i] != building) {
MapSpatialComponent* buildingMSC = GET_SUB_COMPONENT(arr[i], SpatialComponent, MapSpatialComponent);
GLASSERT(buildingMSC);
if (buildingMSC->Bounds().Contains(p)) {
hitBuilding = true;
double thisSys = arr[i]->GetItem()->GetBuildingIndustrial();
hitB++;
if (thisSys <= -0.5) hitNBuilding++;
if (thisSys >= 0.5) hitIBuilding++;
break;
}
}
}
if (hitBuilding) break;
const WorldGrid& wg = worldMap->GetWorldGrid(p.x, p.y);
if (wg.Plant()) {
int stage = wg.PlantStage();
if (stage >= PLANT_BLOCKING_STAGE) {
++hitPlant;
break;
}
else {
hitShrub++;
}
}
if (wg.RockHeight()) {
++hitRock;
break;
}
if (wg.IsWater()) {
++hitWater;
break;
}
Rectangle2I wb;
wb.min = wb.max = p;
if (hasWaterfalls && fluidSim->ContainsWaterfalls(wb)) {
++hitWaterfall;
break;
}
walk.Step();
}
it.Next();
}
// Note rock/ice isn't counted either way.
int natural = hitNBuilding
+ hitWater
+ hitPlant
+ 10 * hitWaterfall
+ hitShrub / 4; // small plants don't add to rRays, so divide is okay.
int industrial = hitIBuilding;
int nRays = hitNBuilding + hitWater + hitPlant + hitWaterfall + hitIBuilding;
eval = 0;
if (nRays) {
// With this system, that one ray (say from a distillery to plant) can be
// hugely impactful. This may need tweaking:
if (nRays < 2)
nRays = 2;
eval = double(industrial - natural) / double(nRays);
}
eval = Clamp(eval, -1.0, 1.0);
if (debugLog) {
Vector2I pos = ToWorld2I(building->Position());
GLOUTPUT(("Building %s at %d,%d eval=%.2f nRays=%d \n hit: Build=%d (I=%d N=%d) water=%d plant=%d rock=%d\n",
building->GetItem()->Name(),
pos.x, pos.y,
eval,
nRays,
hitB, hitIBuilding, hitNBuilding, hitWater, hitPlant, hitRock));
(void)pos;
}
}
return eval;
}
double EvalBuildingScript::Efficiency()
{
double eff = 0.65 + 0.35 * EvalIndustrial(false);
GLASSERT(eff > 0);
if (Reachable())
return eff;
return 0;
}
int EvalBuildingScript::DoTick(U32 delta)
{
if (timer.Delta(delta)) {
// Regular update to change the porch.
parentChit->SendMessage(ChitMsg(ChitMsg::CHIT_POS_CHANGE));
}
return timer.Next();
}