forked from cnlohr/noeuclid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameMap.cpp
280 lines (243 loc) · 8.08 KB
/
GameMap.cpp
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
#include "GameMap.h"
#include <cstring>
#include <functional>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
using namespace std;
void GameMap::die() {
rooms[curroom].reset();
GameAttempt++;
}
void GameMap::collision(CollisionProbe * ddat) {
///XXX TODO: pointers for where we're aimed should come in here.
//printf( "CC %f %f %f (%f)\n", ddat->TargetLocation.r, ddat->TargetLocation.g, ddat->TargetLocation.b, ddat->Normal.a );
}
/**
* read into string until a line containing only '}'
* Used to parse initialization and run scripts from the level file
* TODO: Change syntax or use a real parser
*/
string readScript(ifstream& file, int& lineNum) {
string script = "", line;
int beginning = lineNum;
while(getline(file, line)) {
if(line=="}") return script;
script += line+"\n";
lineNum++;
}
cout<<"Error: script beginning at line "<<beginning<<" not terminated."<<endl;
return "";
}
/** Loads a list of rooms (block positions and scripts) */
void GameMap::loadRooms(string fname) {
rooms.clear();
ifstream file(fname);
printf("Loading %s\n", fname.c_str());
if(!file.is_open()) printf("Could not open rooms.txt");
string line;
int rid = 0;
int lineNum = 0;
while(getline(file, line)) {
lineNum++;
if(line.back()=='\r') line.pop_back();
if(line.length()==0) continue;
if(line[0]=='#') continue;
if(line == "RunScript {") {
if(rooms[rid].runscript) cout <<lineNum<<": Error: Already has run script." << endl;
rooms[rid].runscript = tcc.eval<void(double)>("void fun(double timeInRoom) {"+readScript(file, lineNum)+"}");
continue;
}
if(line == "InitScript {") {
if(rooms[rid].initscript) cout <<lineNum<<": Error: Already has init script." << endl;
rooms[rid].initscript = tcc.eval<void(void)>("void fun() {"+readScript(file, lineNum)+"}");
continue;
}
for(char c: "()") line.erase(remove(line.begin(),line.end(),c),line.end()); // remove parens
istringstream l(line);
string cmd; l >> cmd;
if(cmd == "StartAtRoom") l>>startroom;
else if(cmd == "Room") {
l >> rid;
if(rid>=int(rooms.size())) rooms.resize(rid+1);
}
else if(cmd == "Start") l >> rooms[rid].start;
else if(cmd == "Exit") l >> rooms[rid].exitr1 >> rooms[rid].exitr2;
else if(cmd == "Time") l >> rooms[rid].maxTime;
else if(initfuncs.count(cmd))
rooms[rid].inits.push_back(initfuncs[cmd](l));
else if(runfuncs.count(cmd))
rooms[rid].runs.push_back(runfuncs[cmd](l));
else cout <<lineNum<<": Error: Invalid command " << cmd << endl;
if(l.fail()) cout<<lineNum<<": Error: line formatting error "<<endl;
}
}
void GameMap::update() {
if (gOverallUpdateNo % 30 == 0 && fileChanged("rooms.txt")) {
loadRooms("rooms.txt");
}
if (gOverallUpdateNo == 0) {
curroom = startroom;
gPosition = rooms[curroom].start;
}
if (curroom != lastroom) {
curroom = (curroom%rooms.size()+rooms.size())%rooms.size();
printf("Switching to room %d\n", curroom);
GameTimer = rooms[curroom].maxTime;
rooms[curroom].begin();
lastroom = curroom;
}
gDaytime += worldDeltaTime;
GameTimer -= worldDeltaTime;
if (GameTimer < 0) {
die();
}
rooms[curroom].update();
sprintf(gDialog, "Room: %d\n", curroom);
UpdatePickableBlocks(); //Draw the pickables.
//printf( "%f %f %f %f %f %f\n", targetx, targety, targetz, gDirectionX, gDirectionY, gDirectionZ );
if (gMouseLastClickButton != -1) {
printf("Click on: %f %f %f\n", gTargetHit.x, gTargetHit.y, gTargetHit.z);
if (gMouseLastClickButton == 0) {
//Left-mouse
Vec3f target = gTargetHit + gTargetNormal * .5;
Vec3f dist = (target-gPosition);
PickableClick(true, target, dist.len());
} else if (gMouseLastClickButton == 2) {
//Right-mouse
Vec3f target = gTargetHit - gTargetNormal * .5;
Vec3f dist = (target-gPosition);
PickableClick(false, target, dist.len());
}
gMouseLastClickButton = -1;
}
//We're on the ground. Make sure we don't die.
if (gTimeSinceOnGround < .001) {
Vec3i p = {(int)gPosition.x, (int)gPosition.y, (int)gPosition.z};
//printf( "%d %d %d\n", px, py, pz );
if (IsOnDeathBlock(p)) {
printf("DIE\n");
die();
}
}
if (PlayerInRangeV({2, 40, 63}, {16, 16, 4})) {
int lx = ((int) gPosition.x) - 2;
int ly = ((int) gPosition.y) - 40;
sprintf(gDialog, "OnTile %d\n", lx + ly * 16);
}
}
void GameMap::setRoom(int i, bool reset) {
int s = rooms.size();
curroom = (i%s+s)%s;
if(reset) rooms[curroom].reset();
}
void GameMap::AddDeathBlock(Vec3i p) {
//Don't add multiple.
for (DeathBlock& db:DeathBlocks)
if (db.p == p) return;
for (DeathBlock& db:DeathBlocks) {
if (db.in_use == 0) {
db.in_use = 1;
db.p = p;
break;
}
}
}
bool GameMap::IsOnDeathBlock(Vec3i p) {
for (DeathBlock& db:DeathBlocks) {
if (db.p.x == p.x && db.p.y == p.y && (db.p.z == p.z || db.p.z == p.z - 1))
return 1;
}
return 0;
}
//Returns id if pickable is there.
//Returns -1 if no block.
//Returns -2 if block tween incomplete.
int GameMap::GetPickableAt(Vec3i p) {
int i;
for (i = 0; i < MAX_PICKABLES; i++) {
struct PickableBlock * pb = &PBlocks[i];
if(pb->in_use) printf("%d %d %d\n",pb->p.x,pb->p.y,pb->p.z);
if (pb->in_use && pb->p == p) {
return (pb->density > .99) ? i : -2;
}
}
return -1;
}
void GameMap::DissolvePickable(int pid) {
PBlocks[pid].phasing = -1;
}
//If -1, no pickables left.
//If -2, Pickable already there.
//Otherwise returns which pickable!
//initial_density should be 0 unless you want to shorten (+) or lengthen (-) tween.
int GameMap::PlacePickableAt(Vec3i p, float initial_density) {
int i;
for (i = 0; i < MAX_PICKABLES; i++) {
struct PickableBlock * pb = &PBlocks[i];
if (!pb->in_use) {
break;
}
}
if (i == MAX_PICKABLES)
return -2;
struct PickableBlock * pb = &PBlocks[i];
pb->p = p;
pb->phasing = 1;
pb->in_use = 1;
pb->density = initial_density;
return 0;
}
void GameMap::ClearPickableBlocks() {
for (PickableBlock& pb : PBlocks) {
pb.p = Vec3i{0,0,0};
pb.phasing = 0;
pb.density = 0;
pb.in_use = 0;
}
pickables_in_inventory = 0;
}
//Redraw
void GameMap::UpdatePickableBlocks() {
for (PickableBlock& pb : PBlocks) {
if (!pb.in_use) continue;
pb.density += worldDeltaTime * pb.phasing;
if (pb.density >= 1.0 && pb.phasing > 0) {
pb.density = 1.0;
pb.phasing = 0;
}
if (pb.density <= 0 && pb.phasing < 0) {
pb.density = 0.0;
pb.phasing = 0.0;
pb.in_use = 0;
}
float drawden = (pb.density < 0) ? 0.0 : ((pb.density > 1.0) ? 1.0 : pb.density);
if (drawden > .01) {
PaintRangeV(pb.p, {1, 1, 1}, {1,190,byte(drawden * 200), PICKABLE_CELL});
} else {
ClearCellV(pb.p);
}
}
}
void GameMap::PickableClick(bool left, Vec3f p, float dist) {
//TODO calculate dist some other way (through portals)
//if (dist > 4) return;
if (left) {
if (pickables_in_inventory > 0) {
PlacePickableAt({(int)p.x,(int)p.y,(int)p.z}, 0.0);
pickables_in_inventory--;
} else {
printf("No blocks.\n");
}
} else {
int r = GetPickableAt({(int)p.x,(int)p.y,(int)p.z});
if (r >= 0) {
pickables_in_inventory++;
PBlocks[r].phasing = -1;
} else {
printf("No pickable at (%d,%d,%d)",(int)p.x,(int)p.y,(int)p.z);
}
}
}