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 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
|
/*
* SPDX-FileCopyrightText: Copyright (C) 1987, 1988 Chuck Simmons
* SPDX-License-Identifier: GPL-2.0+
*
* See the file COPYING, distributed with empire, for restriction
* and warranty information.
*/
/*
edit.c -- Routines to handle edit mode commands.
*/
#include "empire.h"
#include "extern.h"
#include <ctype.h>
#include <curses.h>
#include <stdio.h>
#include <string.h>
void e_move(loc_t *path_start, loc_t loc);
extern int get_piece_name(void);
void edit(loc_t edit_cursor) {
char e_cursor(loc_t *);
void e_leave(void), e_print(loc_t *), e_random(loc_t);
void e_stasis(loc_t), e_end(loc_t *, loc_t, int);
void e_wake(loc_t), e_sleep(loc_t);
void e_info(loc_t), e_prod(loc_t), e_help(void), e_explore(loc_t);
void e_fill(loc_t), e_land(loc_t), e_transport(loc_t);
void e_city_func(loc_t *, loc_t, int *);
void e_attack(loc_t), e_repair(loc_t);
loc_t path_start;
int path_type;
path_start = -1; /* not building a path yet */
game.showprod = false;
comment("Edit mode...");
for (;;) { /* until user gives command to leave */
char e;
display_loc_u(edit_cursor); /* position cursor */
e = e_cursor(&edit_cursor); /* handle cursor movement */
switch (e) {
case 'B': /* change city production */
e_prod(edit_cursor);
break;
case 'F': /* fill */
e_fill(edit_cursor);
break;
case 'G': /* explore */
e_explore(edit_cursor);
break;
case 'H': /* help */
e_help();
break;
case 'I': /* directional stasis */
e_stasis(edit_cursor);
break;
case 'J': /* Redraw map showing city production */
game.showprod = true;
sector_change(); // Force redraw */
break;
case 'K': /* wake up anything and everything */
e_wake(edit_cursor);
break;
case 'L': /* land plane */
e_land(edit_cursor);
break;
case 'M': /* start move to location */
path_type = NOPIECE;
e_move(&path_start, edit_cursor);
break;
case 'N': /* end move to location */
e_end(&path_start, edit_cursor, path_type);
break;
case 'O': /* leave edit mode */
if (game.showprod) {
game.showprod = false;
sector_change(); // Force redraw */
}
e_leave();
return;
case 'P': /* print new sector */
e_print(&edit_cursor);
break;
case 'R': /* make piece move randomly */
e_random(edit_cursor);
break;
case 'S': /* sleep */
e_sleep(edit_cursor);
break;
case 'T': /* transport army */
e_transport(edit_cursor);
break;
case 'U': /* repair ship */
e_repair(edit_cursor);
break;
case 'V': /* set city function */
e_city_func(&path_start, edit_cursor, &path_type);
break;
case 'Y': /* set army func to attack */
e_attack(edit_cursor);
break;
case '?': /* request info */
e_info(edit_cursor);
break;
case '\014': /* control-L */
redraw();
break;
default: /* bad command? */
huh();
break;
}
}
}
/*
Get the next command. We handle cursor movement here.
This routine is an attempt to make cursor movement reasonably
fast.
*/
char e_cursor(loc_t *edit_cursor) {
chtype e;
/* set up terminal */
(void)crmode();
(void)refresh();
e = getch();
topini(); /* clear any error messages */
for (;;) {
int p;
p = direction(e);
if (p == -1) {
break;
}
if (!move_cursor(edit_cursor, dir_offset[p])) {
(void)beep();
}
(void)refresh();
e = getch();
}
(void)nocrmode(); /* reset terminal */
return toupper(e);
}
/*
Leave edit mode.
*/
void e_leave(void) { comment("Exiting edit mode."); }
/*
Print new sector.
*/
void e_print(loc_t *edit_cursor) {
int sector;
sector = get_range("New Sector? ", 0, NUM_SECTORS - 1);
/* position cursor at center of sector */
*edit_cursor = sector_loc(sector);
sector_change(); /* allow change of sector */
}
/*
Set the function of a piece.
*/
void e_set_func(loc_t loc, long func) {
piece_info_t *obj;
obj = find_obj_at_loc(loc);
if (obj != NULL && obj->owner == USER) {
obj->func = func;
return;
}
huh(); /* no object here */
}
/* Set the function of a city for some piece. */
void e_set_city_func(city_info_t *cityp, int type, long func) {
ASSERT(cityp != NULL);
// cppcheck-suppress nullPointerRedundantCheck
cityp->func[type] = func;
}
/*
Set a piece to move randomly.
*/
void e_random(loc_t loc) { e_set_func(loc, RANDOM); }
void e_city_random(city_info_t *cityp, int type) {
e_set_city_func(cityp, type, RANDOM);
}
/*
Put a ship in fill mode.
*/
void e_fill(loc_t loc) {
if (game.user_map[loc].contents == 'T' ||
game.user_map[loc].contents == 'C') {
e_set_func(loc, FILL);
} else {
huh();
}
}
void e_city_fill(city_info_t *cityp, int type) {
if (type == TRANSPORT || type == CARRIER) {
e_set_city_func(cityp, type, FILL);
} else {
huh();
}
}
/*
Set a piece to explore.
*/
void e_explore(loc_t loc) { e_set_func(loc, EXPLORE); }
void e_city_explore(city_info_t *cityp, loc_t type) {
e_set_city_func(cityp, type, EXPLORE);
}
/*
Set a fighter to land.
*/
void e_land(loc_t loc) {
if (game.user_map[loc].contents == 'F') {
e_set_func(loc, LAND);
} else {
huh();
}
}
/*
Set an army's function to TRANSPORT.
*/
void e_transport(loc_t loc) {
if (game.user_map[loc].contents == 'A') {
e_set_func(loc, WFTRANSPORT);
} else {
huh();
}
}
/*
Set an army's function to ATTACK.
*/
void e_attack(loc_t loc) {
if (game.user_map[loc].contents == 'A') {
e_set_func(loc, ARMYATTACK);
} else {
huh();
}
}
void e_city_attack(city_info_t *cityp, int type) {
if (type == ARMY) {
e_set_city_func(cityp, type, ARMYATTACK);
} else {
huh();
}
}
/*
Set a ship's function to REPAIR.
*/
void e_repair(loc_t loc) {
if (strchr("PDSTBC", game.user_map[loc].contents)) {
e_set_func(loc, REPAIR);
} else {
huh();
}
}
void e_city_repair(city_info_t *cityp, int type) {
if (type == ARMY || type == FIGHTER || type == SATELLITE) {
huh();
} else {
e_set_city_func(cityp, type, REPAIR);
}
}
/*
Set object to move in a direction.
*/
static char dirs[] = "WEDCXZAQ";
void e_stasis(loc_t loc) {
if (!isupper(game.user_map[loc].contents)) {
huh(); /* no object here */
} else if (game.user_map[loc].contents == 'X') {
huh();
} else {
char e = get_chx(); /* get a direction */
char *p = strchr(dirs, e);
if (p == NULL) {
huh();
} else {
e_set_func(loc, (long)(MOVE_N - (p - dirs)));
}
}
}
void e_city_stasis(city_info_t *cityp, int type) {
char e;
char *p;
e = get_chx(); /* get a direction */
p = strchr(dirs, e);
if (p == NULL) {
huh();
} else {
e_set_city_func(cityp, type, (long)(MOVE_N - (p - dirs)));
}
}
/*
Wake up anything and everything.
*/
void e_wake(loc_t loc) {
city_info_t *cityp;
piece_info_t *obj;
cityp = find_city(loc);
if (cityp != NULL) {
int i;
for (i = 0; i < NUM_OBJECTS; i++) {
cityp->func[i] = NOFUNC;
}
}
for (obj = game.real_map[loc].objp; obj != NULL;
obj = obj->loc_link.next) {
obj->func = NOFUNC;
}
}
void e_city_wake(city_info_t *cityp, int type) {
e_set_city_func(cityp, type, NOFUNC);
}
/*
Set a city's function. We get the piece type to set, then
the function itself.
*/
void e_city_func(loc_t *path_start, loc_t loc, int *path_type) {
int type;
char e;
city_info_t *cityp;
cityp = find_city(loc);
if (!cityp || cityp->owner != USER) {
huh();
return;
}
type = get_piece_name();
if (type == NOPIECE) {
huh();
return;
}
e = get_chx();
switch (e) {
case 'F': /* fill */
e_city_fill(cityp, type);
break;
case 'G': /* explore */
e_city_explore(cityp, type);
break;
case 'I': /* directional stasis */
e_city_stasis(cityp, type);
break;
case 'K': /* turn off function */
e_city_wake(cityp, type);
break;
case 'M': /* start move to location */
*path_type = type;
e_move(path_start, loc);
break;
case 'R': /* make piece move randomly */
e_city_random(cityp, type);
break;
case 'U': /* repair ship */
e_city_repair(cityp, type);
break;
case 'Y': /* set army func to attack */
e_city_attack(cityp, type);
break;
default: /* bad command? */
huh();
break;
}
}
/*
Beginning of move to location.
*/
void e_move(loc_t *path_start, loc_t loc) {
if (!isupper(game.user_map[loc].contents)) {
huh(); /* nothing there? */
} else if (game.user_map[loc].contents == 'X') {
huh(); /* enemy city? */
} else {
*path_start = loc;
}
}
/*
End of move to location.
*/
void e_end(loc_t *path_start, loc_t loc, int path_type) {
if (*path_start == -1) {
huh(); /* no path started? */
} else if (path_type == NOPIECE) {
e_set_func(*path_start, loc);
} else {
city_info_t *cityp = find_city(*path_start);
ASSERT(cityp != NULL);
e_set_city_func(cityp, path_type, loc);
}
*path_start = -1; /* remember no path in progress */
}
/*
Put a piece to sleep.
*/
void e_sleep(loc_t loc) {
if (game.user_map[loc].contents == 'O') {
huh(); /* can't sleep a city */
} else {
e_set_func(loc, SENTRY);
}
}
/*
Print out information about a piece.
*/
void e_info(loc_t edit_cursor) {
void e_city_info(loc_t), e_piece_info(loc_t edit_cursor, char ab);
char ab;
ab = game.user_map[edit_cursor].contents;
if (ab == 'O') {
e_city_info(edit_cursor);
} else if (ab == 'X' && game.debug) {
e_city_info(edit_cursor);
} else if ((ab >= 'A') && (ab <= 'T')) {
e_piece_info(edit_cursor, ab);
} else if ((ab >= 'a') && (ab <= 't') && (game.debug)) {
e_piece_info(edit_cursor, ab);
} else {
huh();
}
}
/*
Print info about a piece.
*/
void e_piece_info(loc_t edit_cursor, char ab) {
piece_info_t *obj;
int type;
char *p;
ab = toupper(ab);
p = strchr(type_chars, ab);
type = p - type_chars;
obj = find_obj(type, edit_cursor);
ASSERT(obj != NULL);
describe_obj(obj);
}
/*
Display info on a city.
*/
void e_city_info(loc_t edit_cursor) {
piece_info_t *obj;
city_info_t *cityp;
int f, s;
char func_buf[STRSIZE];
char temp_buf[STRSIZE];
char junk_buf2[STRSIZE];
error(""); /* clear line */
f = 0; /* no fighters counted yet */
for (obj = game.real_map[edit_cursor].objp; obj != NULL;
obj = obj->loc_link.next) {
if (obj->type == FIGHTER) {
f++;
}
}
s = 0; /* no ships counted yet */
for (obj = game.real_map[edit_cursor].objp; obj != NULL;
obj = obj->loc_link.next) {
if (obj->type >= DESTROYER) {
s++;
}
}
if (f == 1 && s == 1) {
(void)sprintf(game.jnkbuf, "1 fighter landed, 1 ship docked");
} else if (f == 1) {
(void)sprintf(game.jnkbuf, "1 fighter landed, %d ships docked",
s);
} else if (s == 1) {
(void)sprintf(game.jnkbuf, "%d fighters landed, 1 ship docked",
f);
} else {
(void)sprintf(game.jnkbuf,
"%d fighters landed, %d ships docked", f, s);
}
cityp = find_city(edit_cursor);
ASSERT(cityp != NULL);
*func_buf = 0; /* nothing in buffer */
for (s = 0; s < NUM_OBJECTS; s++) { /* for each piece */
// cppcheck-suppress nullPointerRedundantCheck
if (cityp->func[s] < 0) {
(void)sprintf(temp_buf, "%c:%s; ", piece_attr[s].sname,
func_name[FUNCI(cityp->func[s])]);
} else {
(void)sprintf(temp_buf, "%c: %d;", piece_attr[s].sname,
loc_disp(cityp->func[s]));
}
(void)strcat(func_buf, temp_buf);
}
(void)sprintf(
junk_buf2, "City at location %d will complete %s on round %ld",
loc_disp(cityp->loc), piece_attr[(int)cityp->prod].article,
game.date + piece_attr[(int)cityp->prod].build_time - cityp->work);
info(junk_buf2, game.jnkbuf, func_buf);
}
/*
Change city production.
*/
void e_prod(loc_t loc) {
city_info_t *cityp;
cityp = find_city(loc);
if (cityp == NULL) {
huh(); /* no city? */
} else {
set_prod(cityp);
}
}
/*
get help
*/
void e_help(void) {
help(help_edit, edit_lines);
prompt("Press any key to continue: ");
(void)get_chx();
}
/* end */
|