forked from headswe/Cataclysm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer.cpp
More file actions
542 lines (493 loc) · 13.7 KB
/
Copy pathcomputer.cpp
File metadata and controls
542 lines (493 loc) · 13.7 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include "computer.h"
#include "game.h"
#include "monster.h"
#include "overmap.h"
#include "output.h"
#include <fstream>
#include <string>
#include <sstream>
computer::computer()
{
security = 0;
name = DEFAULT_COMPUTER_NAME;
w_terminal = NULL;
}
computer::computer(std::string Name, int Security)
{
security = Security;
name = Name;
w_terminal = NULL;
}
computer::~computer()
{
if (w_terminal != NULL)
delwin(w_terminal);
}
computer& computer::operator=(const computer &rhs)
{
security = rhs.security;
name = rhs.name;
options.clear();
for (int i = 0; i < rhs.options.size(); i++)
options.push_back(rhs.options[i]);
failures.clear();
for (int i = 0; i < rhs.failures.size(); i++)
failures.push_back(rhs.failures[i]);
w_terminal = NULL;
return *this;
}
void computer::set_security(int Security)
{
security = Security;
}
void computer::add_option(std::string opt_name, computer_action action,
int Security)
{
options.push_back(computer_option(opt_name, action, Security));
}
void computer::add_failure(computer_failure failure)
{
failures.push_back(failure);
}
void computer::shutdown_terminal()
{
werase(w_terminal);
delwin(w_terminal);
w_terminal = NULL;
}
void computer::use(game *g)
{
if (w_terminal == NULL)
w_terminal = newwin(25, 80, 0, 0);
wborder(w_terminal, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
print_line("Logging into %s...", name.c_str());
if (security > 0) {
print_error("ERROR! Access denied!");
switch (query_ynq("Bypass security?")) {
case 'q':
case 'Q':
shutdown_terminal();
return;
case 'n':
case 'N':
print_line("Shutting down... press any key.");
getch();
shutdown_terminal();
return;
case 'y':
case 'Y':
if (!hack_attempt(&(g->u))) {
if (failures.size() == 0) {
print_line("Maximum login attempts exceeded. Press any key...");
getch();
shutdown_terminal();
return;
}
activate_random_failure(g);
shutdown_terminal();
return;
} else { // Successful hack attempt
security = 0;
print_line("Login successful. Press any key...");
getch();
reset_terminal();
}
}
} else { // No security
print_line("Login successful. Press any key...");
getch();
reset_terminal();
}
// Main computer loop
bool done = false; // Are we done using the computer?
do {
reset_terminal();
print_line("%s - Root Menu", name.c_str());
for (int i = 0; i < options.size(); i++)
print_line("%d - %s", i + 1, options[i].name.c_str());
print_line("Q - Quit and shut down");
char ch;
do
ch = getch();
while (ch != 'q' && ch != 'Q' && (ch < '1' || ch - '1' > options.size()));
if (ch == 'q' || ch == 'Q')
done = true;
else { // We selected an option other than quit.
ch -= '1'; // So '1' -> 0; index in options.size()
computer_option current = options[ch];
if (current.security > 0) {
print_error("Password required.");
if (query_ynq("Hack into system?")) {
if (!hack_attempt(&(g->u), current.security)) {
activate_random_failure(g);
shutdown_terminal();
return;
} else {
activate_function(g, current.action);
done = true;
reset_terminal();
}
}
} else // No need to hack, just activate
activate_function(g, current.action);
} // Done processing a selected option.
} while (!done); // Done with main terminal loop
shutdown_terminal(); // This should have been done by now, but just in case.
}
bool computer::hack_attempt(player *p, int Security)
{
if (Security == -1)
Security = security; // Set to main system security if no value passed
p->practice(sk_computer, Security * 2);
int player_roll = p->sklevel[sk_computer];
if (p->int_cur < 8 && one_in(2))
player_roll -= rng(0, 8 - p->int_cur);
else if (p->int_cur > 8 && one_in(3))
player_roll += rng(0, p->int_cur - 8);
return (rng(player_roll, 6) >= rng(Security, 6));
}
std::string computer::save_data()
{
std::stringstream data;
std::string savename = name; // Replace " " with "_"
size_t found = savename.find(" ");
while (found != std::string::npos) {
savename.replace(found, 1, "_");
found = savename.find(" ");
}
data << savename << " " << security << " " << options.size() << " ";
for (int i = 0; i < options.size(); i++) {
savename = options[i].name;
found = savename.find(" ");
while (found != std::string::npos) {
savename.replace(found, 1, "_");
found = savename.find(" ");
}
data << savename << " " << int(options[i].action) << " " <<
options[i].security << " ";
}
data << failures.size() << " ";
for (int i = 0; i < failures.size(); i++)
data << int(failures[i]) << " ";
return data.str();
}
void computer::load_data(std::string data)
{
options.clear();
failures.clear();
std::stringstream dump;
std::string buffer;
dump << data;
// Pull in name and security
dump >> name >> security;
size_t found = name.find("_");
while (found != std::string::npos) {
name.replace(found, 1, " ");
found = name.find("_");
}
// Pull in options
int optsize;
dump >> optsize;
for (int n = 0; n < optsize; n++) {
std::string tmpname;
int tmpaction, tmpsec;
dump >> tmpname >> tmpaction >> tmpsec;
size_t found = tmpname.find("_");
while (found != std::string::npos) {
tmpname.replace(found, 1, " ");
found = tmpname.find("_");
}
add_option(tmpname, computer_action(tmpaction), tmpsec);
}
// Pull in failures
int failsize, tmpfail;
dump >> failsize;
for (int n = 0; n < failsize; n++) {
dump >> tmpfail;
failures.push_back(computer_failure(tmpfail));
}
}
void computer::activate_function(game *g, computer_action action)
{
switch (action) {
case COMPACT_NULL:
break; // Why would this be called?
case COMPACT_OPEN:
g->m.translate(t_door_metal_locked, t_floor);
print_line("Doors opened.");
break;
case COMPACT_RELEASE:
g->sound(g->u.posx, g->u.posy, 40, "An alarm sounds!");
g->m.translate(t_reinforced_glass_h, t_floor);
g->m.translate(t_reinforced_glass_v, t_floor);
print_line("Containment shields opened.");
break;
case COMPACT_TERMINATE:
for (int x = 0; x < SEEX * 3; x++) {
for (int y = 0; y < SEEY * 3; y++) {
int mondex = g->mon_at(x, y);
if (mondex != -1 &&
((g->m.ter(x, y - 1) == t_reinforced_glass_h &&
g->m.ter(x, y + 1) == t_wall_h) ||
(g->m.ter(x, y + 1) == t_reinforced_glass_h &&
g->m.ter(x, y - 1) == t_wall_h)))
g->kill_mon(mondex);
}
}
print_line("Subjects terminated.");
break;
case COMPACT_PORTAL:
for (int i = 0; i < SEEX * 3; i++) {
for (int j = 0; j < SEEY * 3; j++) {
int numtowers = 0;
for (int xt = i - 2; xt <= i + 2; xt++) {
for (int yt = j - 2; yt <= j + 2; yt++) {
if (g->m.ter(xt, yt) == t_radio_tower)
numtowers++;
}
}
if (numtowers == 4) {
if (g->m.tr_at(i, j) == tr_portal)
g->m.tr_at(i, j) = tr_null;
else
g->m.add_trap(i, j, tr_portal);
}
}
}
break;
case COMPACT_CASCADE: {
if (!query_yn("WARNING: Resonance cascade carries severe risk! Continue?"))
return;
std::vector<point> cascade_points;
for (int i = g->u.posx - 10; i <= g->u.posx + 10; i++) {
for (int j = g->u.posy - 10; j <= g->u.posy + 10; j++) {
if (g->m.ter(i, j) == t_radio_tower)
cascade_points.push_back(point(i, j));
}
}
if (cascade_points.size() == 0)
g->resonance_cascade(g->u.posx, g->u.posy);
else {
point p = cascade_points[rng(0, cascade_points.size() - 1)];
g->resonance_cascade(p.x, p.y);
}
} break;
case COMPACT_RESEARCH: {
int lines = 0, notes = 0;
std::string log, tmp;
int ch;
std::ifstream fin;
fin.open("data/LAB_NOTES");
if (!fin.is_open()) {
debugmsg("Couldn't open ./data/LAB_NOTES for reading");
return;
}
while (fin.good()) {
ch = fin.get();
if (ch == '%')
notes++;
}
while (lines < 10) {
fin.clear();
fin.seekg(0, std::ios::beg);
fin.clear();
int choice = rng(1, notes);
while (choice > 0) {
getline(fin, tmp);
if (tmp.find_first_of('%') == 0)
choice--;
}
bool get_okay;
getline(fin, tmp);
do {
lines++;
if (lines < 15 && tmp.find_first_of('%') != 0) {
log.append(tmp);
log.append("\n");
}
} while(tmp.find_first_of('%') != 0 && getline(fin, tmp));
}
print_line(log.c_str());
print_line("Press any key...");
getch();
} break;
case COMPACT_MAPS: {
int minx = int(g->levx / 2) - 40;
int maxx = int(g->levx / 2) + 40;
int miny = int(g->levy / 2) - 40;
int maxy = int(g->levy / 2) + 40;
if (minx < 0) minx = 0;
if (maxx >= OMAPX) maxx = OMAPX - 1;
if (miny < 0) miny = 0;
if (maxy >= OMAPY) maxy = OMAPY - 1;
overmap tmp(g, g->cur_om.posx, g->cur_om.posy, 0);
for (int i = minx; i <= maxx; i++) {
for (int j = miny; j <= maxy; j++)
tmp.seen(i, j) = true;
}
tmp.save(g->u.name, g->cur_om.posx, g->cur_om.posy, 0);
print_line("Surface map data downloaded.");
} break;
case COMPACT_MISS_LAUNCH: {
overmap tmp_om(g, g->cur_om.posx, g->cur_om.posy, 0);
// Target Acquisition.
point target = tmp_om.choose_point(g);
if (target.x == -1) {
print_line("Launch canceled.");
return;
}
// Figure out where the glass wall is...
int wall_spot = 0;
for (int i = 0; i < SEEX * 3 && wall_spot == 0; i++) {
if (g->m.ter(i, 10) == t_wall_glass_v)
wall_spot = i;
}
// ...and put radioactive to the right of it
for (int i = wall_spot + 1; i < SEEX * 2 - 1; i++) {
for (int j = 1; j < SEEY * 2 - 1; j++) {
if (one_in(3))
g->m.add_field(NULL, i, j, fd_nuke_gas, 3);
}
}
// For each level between here and the surface, remove the missile
for (int level = g->cur_om.posz; level < 0; level++) {
tmp_om = g->cur_om;
g->cur_om = overmap(g, tmp_om.posx, tmp_om.posy, level);
map tmpmap(&g->itypes, &g->mapitems, &g->traps);
tmpmap.load(g, g->levx, g->levy);
tmpmap.translate(t_missile, t_hole);
tmpmap.save(&tmp_om, g->turn, g->levx, g->levy);
}
g->cur_om = tmp_om;
for (int x = target.x - 2; x <= target.x + 2; x++) {
for (int y = target.y - 2; y <= target.y + 2; y++)
g->nuke(x, y);
}
} break;
case COMPACT_MISS_DISARM: // TODO: This!
break;
} // switch (action)
}
void computer::activate_random_failure(game *g)
{
computer_failure fail = (failures.empty() ? COMPFAIL_SHUTDOWN :
failures[rng(0, failures.size() - 1)]);
activate_failure(g, fail);
}
void computer::activate_failure(game *g, computer_failure fail)
{
switch (fail) {
case COMPFAIL_NULL:
break; // Do nothing. Why was this even called >:|
case COMPFAIL_SHUTDOWN:
for (int x = 0; x < SEEX * 3; x++) {
for (int y = 0; y < SEEY * 3; y++) {
if (g->m.has_flag(console, x, y))
g->m.ter(x, y) = t_console_broken;
}
}
break;
case COMPFAIL_ALARM:
g->sound(g->u.posx, g->u.posy, 60, "An alarm sounds!");
if (g->levz > 0 && !g->event_queued(EVENT_WANTED))
g->add_event(EVENT_WANTED, g->turn + 300, 0, g->levx, g->levy);
break;
case COMPFAIL_MANHACKS: {
int num_robots = rng(4, 8);
for (int i = 0; i < num_robots; i++) {
int mx, my, tries = 0;
do {
mx = rng(g->u.posx - 3, g->u.posx + 3);
my = rng(g->u.posy - 3, g->u.posy + 3);
tries++;
} while (!g->is_empty(mx, my) && tries < 10);
if (tries != 10) {
g->add_msg("Manhacks drop from compartments in the ceiling.");
monster robot(g->mtypes[mon_manhack]);
robot.spawn(mx, my);
g->z.push_back(robot);
}
}
} break;
case COMPFAIL_SECUBOTS: {
int num_robots = rng(4, 8);
for (int i = 0; i < num_robots; i++) {
int mx, my, tries = 0;
do {
mx = rng(g->u.posx - 3, g->u.posx + 3);
my = rng(g->u.posy - 3, g->u.posy + 3);
tries++;
} while (!g->is_empty(mx, my) && tries < 10);
if (tries != 10) {
g->add_msg("Secubots emerge from compartments in the floor.");
monster robot(g->mtypes[mon_secubot]);
robot.spawn(mx, my);
g->z.push_back(robot);
}
}
} break;
case COMPFAIL_DAMAGE:
g->add_msg("The console electrocutes you!");
g->u.hurtall(rng(1, 10));
break;
}// switch (fail)
}
char computer::query_ynq(const char *mes, ...)
{
// Translate the printf flags
va_list ap;
va_start(ap, mes);
char buff[6000];
vsprintf(buff, mes, ap);
va_end(ap);
// Append with (Y/N/Q)
std::string full_line = buff;
full_line += " (Y/N/Q)";
// Print the resulting text
print_line(full_line.c_str());
char ret;
do
ret = getch();
while (ret != 'y' && ret != 'Y' && ret != 'n' && ret != 'N' && ret != 'q' &&
ret != 'Q');
return ret;
}
void computer::print_line(const char *mes, ...)
{
// Translate the printf flags
va_list ap;
va_start(ap, mes);
char buff[6000];
vsprintf(buff, mes, ap);
va_end(ap);
// Print the line.
wprintz(w_terminal, c_green, " %s%s", buff, "\n");
// Reprint the border, in case we pushed a line over it
wborder(w_terminal, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
wrefresh(w_terminal);
}
void computer::print_error(const char *mes, ...)
{
// Translate the printf flags
va_list ap;
va_start(ap, mes);
char buff[6000];
vsprintf(buff, mes, ap);
va_end(ap);
// Print the line.
wprintz(w_terminal, c_red, " %s%s", buff, "\n");
// Reprint the border, in case we pushed a line over it
wborder(w_terminal, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
wrefresh(w_terminal);
}
void computer::reset_terminal()
{
werase(w_terminal);
wborder(w_terminal, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
wmove(w_terminal, 1, 1);
wrefresh(w_terminal);
}