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
|
/* Copyright (C) 2002 Andrew Apted <ajapted@users.sourceforge.net> */
/* NetHack may be freely redistributed. See license for details. */
/*
* SDL/GL window port for NetHack & Slash'EM.
*
* Character selection code (Role, Race, Gender, Alignment).
*/
#include "hack.h"
#include "dlb.h"
#include "patchlevel.h"
#if defined(GL_GRAPHICS) || defined(SDL_GRAPHICS)
#define WINGL_INTERNAL
#include "winGL.h"
#include <string.h>
#include <ctype.h>
/* value chosen not to conflict with existing ROLE values */
#define ROLE_QUIT (-7)
#define HEIGHT_PICKER 7
#define DEPTH_PICKER 8
/* callback functions used to create menus for player_selection().
* Each one returns: 1 for a valid name, 0 for an invalid name, and -1
* when the list is exhausted. For 1 and 0, the name is copied into
* the buffer provided.
*/
static int query_role_name(int what, char *buf)
{
if (roles[what].name.m == NULL)
return -1;
if (flags.initgend >= 0 && flags.female && roles[what].name.f)
strcpy(buf, roles[what].name.f);
else
strcpy(buf, roles[what].name.m);
return ok_role(what, flags.initrace, flags.initgend,
flags.initalign) ? 1 : 0;
}
static int query_race_name(int what, char *buf)
{
if (races[what].noun == NULL)
return -1;
strcpy(buf, races[what].noun);
return ok_race(flags.initrole, what, flags.initgend,
flags.initalign) ? 1 : 0;
}
static int query_gender_name(int what, char *buf)
{
if (what >= ROLE_GENDERS)
return -1;
strcpy(buf, genders[what].adj);
return ok_gend(flags.initrole, flags.initrace, what,
flags.initalign) ? 1 : 0;
}
static int query_align_name(int what, char *buf)
{
if (what >= ROLE_ALIGNS)
return -1;
strcpy(buf, aligns[what].adj);
return ok_align(flags.initrole, flags.initrace, flags.initgend,
what) ? 1 : 0;
}
/* print a string with line splitting. Based on sdlgl_putstr(), but
* has some significant differences now.
*/
static void raw_puts_split(struct TextWindow *win, const char *str)
{
int len = strlen(str);
while (len > 0)
{
int use_len;
int width = win->show_w;
/* does it fit yet ? */
if (len < width)
{
sdlgl_puts_nolf(win, str);
return;
}
/* choose a good place to break the line */
for (use_len = width-1; use_len > width/2; use_len--)
if (str[use_len] == ' ')
break;
/* no space in latter half of string ? Must be a very long token,
* so we might as well split it at the screen edge.
*/
if (str[use_len] != ' ')
use_len = width-1;
for (; use_len > 0; use_len--, str++, len--)
if (*str != '\n' && *str != '\r')
sdlgl_putc(win, *str);
sdlgl_putc(win, '\n');
/* remove leading spaces */
for (; *str == ' '; str++, len--) { }
}
}
/* may return ROLE_RANDOM, or ROLE_NONE when the list is empty (which
* can only mean that the role/race/gender/alignment combination that
* the user supplied on the command line was not possible), or
* ROLE_QUIT if the user pressed escape.
*/
static int do_player_selection_menu(const char *type,
int (* func)(int what, char *buf))
{
int i, kind;
int valid = 0, total = 0, last = -1;
winid window;
anything any;
int attr, choice;
menu_item *selected = NULL;
int used_accs[52] = { 0, };
char accel;
char name[BUFSZ];
char prompt[BUFSZ];
char randstr[BUFSZ];
/* count valid entries */
for (i=0; ; i++)
{
kind = (* func)(i, name);
if (kind < 0)
break;
total++;
if (kind > 0)
{
valid++;
last = i;
}
}
if (valid == 0)
return ROLE_NONE;
assert(last >= 0);
/* -AJA- this is disabled -- it's better IMHO to show the user the
* single choice, letting them press ESC and pick some other
* role/race/gender if they're not satisfied.
*/
#if 0
if (valid == 1)
return last;
#endif
/* create menu */
sprintf(prompt, "Pick your %s", type);
sprintf(randstr, "Random %s", type);
window = Sdlgl_create_nhwindow(NHW_MENU);
assert(window != WIN_ERR);
Sdlgl_start_menu(window);
for (i=0; i < total; i++)
{
kind = (* func)(i, name);
any.a_void = 0;
attr = ATR_NONE;
/* choose accelerator key */
accel = lowc(name[0]);
if (accel < 'a' || accel > 'z')
accel = 0;
else
{
if (! used_accs[accel - 'a'])
used_accs[accel - 'a'] = 1;
else
{
accel = highc(accel);
if (! used_accs[accel - 'A' + 26])
used_accs[accel - 'A' + 26] = 1;
else
accel = 0;
}
}
if (kind > 0)
{
/* must add one, zero is not allowed */
any.a_int = 1 + i;
}
else
{
/* shift name right (to align), and make dim */
memmove(name + 5, name, strlen(name) + 1);
memcpy(name, " ", 5);
attr = ATR_DIM;
}
Sdlgl_add_menu(window, NO_GLYPH, &any, accel,
0 /* no groupacc */, attr, name, MENU_UNSELECTED);
}
/* add `Random' line (unless there's only one choice) */
if (valid > 1)
{
any.a_int = 1 + total;
Sdlgl_add_menu(window, NO_GLYPH, &any, '*',
0 /* no groupacc */, ATR_NONE, randstr, MENU_UNSELECTED);
}
Sdlgl_end_menu(window, prompt);
/* get result back from menu */
i = Sdlgl_select_menu(window, PICK_ONE, &selected);
Sdlgl_destroy_nhwindow(window);
if (i <= 0)
return ROLE_QUIT;
assert(i == 1 && selected);
choice = selected[0].item.a_int - 1;
free(selected);
assert(0 <= choice && choice <= total);
if (choice == total)
return ROLE_RANDOM;
return choice;
}
/* These return -1 if cancelled, otherwise 0.
*/
static int select_auto_pick(int *pick4u)
{
struct TextWindow *win;
int pixel_h;
int ch;
/* handle the fully random (-@) option */
if (flags.randomall)
{
(* pick4u) = 1;
return 0;
}
/* create text & tile windows */
win = sdlgl_new_textwin(NHW_TEXT); /* type unimportant */
win->show_w = sdlgl_width / sdlgl_font_message->tile_w;
win->show_h = HEIGHT_PICKER;
pixel_h = sdlgl_font_message->tile_h * win->show_h;
win->base = sdlgl_new_tilewin(sdlgl_font_message, win->show_w,
win->show_h, 1,0);
sdlgl_map_tilewin(win->base, 0, sdlgl_height - pixel_h,
sdlgl_width, pixel_h, DEPTH_PICKER);
/* do the query */
{
char pbuf[QBUFSZ];
const char *prompt = "Shall I pick a character for you? [ynq] ";
if (flags.initrole != ROLE_NONE || flags.initrace != ROLE_NONE ||
flags.initgend != ROLE_NONE || flags.initalign != ROLE_NONE)
{
prompt = build_plselection_prompt(pbuf, QBUFSZ, flags.initrole,
flags.initrace, flags.initgend, flags.initalign);
}
sdlgl_home(win);
/* if the copyright wasn't shown on the splash screen (because it
* was disabled), then show it here.
*/
if (! iflags.wc_splash_screen)
{
sdlgl_puts(win, COPYRIGHT_BANNER_A "\n");
sdlgl_puts(win, COPYRIGHT_BANNER_B "\n");
sdlgl_puts(win, COPYRIGHT_BANNER_C "\n");
sdlgl_puts(win, "\n");
}
raw_puts_split(win, prompt);
}
do
{
sdlgl_flush();
ch = sdlgl_get_key(0);
}
while (strchr("ynq\033", ch) == NULL);
sdlgl_unmap_tilewin(win->base);
sdlgl_free_textwin(win);
if (ch == 'q' || ch == '\033')
return -1;
/* should we randomly pick for the player ? */
(* pick4u) = (ch == 'y') ? 1 : 0;
return 0;
}
static int select_a_role(int pick4u)
{
int choice;
while (flags.initrole < 0)
{
if (pick4u || flags.initrole == ROLE_RANDOM || flags.randomall)
{
flags.initrole = pick_role(flags.initrace, flags.initgend,
flags.initalign, PICK_RANDOM);
break;
}
/* select a role */
for (;;)
{
choice = do_player_selection_menu("role", query_role_name);
if (choice != ROLE_NONE)
break;
/* reset */
if (flags.initalign >= 0) flags.initalign = ROLE_NONE;
else if (flags.initgend >= 0) flags.initgend = ROLE_NONE;
else if (flags.initrace >= 0) flags.initrace = ROLE_NONE;
else
return 0;
}
if (choice == ROLE_QUIT)
{
flags.initrole = ROLE_NONE;
return -1;
}
flags.initrole = choice;
}
return 0;
}
static int select_a_race(int pick4u)
{
int choice;
while (!validrace(flags.initrole, flags.initrace))
{
if (pick4u || flags.initrace == ROLE_RANDOM || flags.randomall)
{
flags.initrace = pick_race(flags.initrole, flags.initgend,
flags.initalign, PICK_RANDOM);
break;
}
/* select a race */
for (;;)
{
choice = do_player_selection_menu("race", query_race_name);
if (choice != ROLE_NONE)
break;
/* reset */
if (flags.initalign >= 0) flags.initalign = ROLE_NONE;
else if (flags.initgend >= 0) flags.initgend = ROLE_NONE;
else
return 0;
}
if (choice == ROLE_QUIT)
{
flags.initrole = ROLE_NONE;
flags.initrace = ROLE_NONE;
return -1;
}
flags.initrace = choice;
}
return 0;
}
static int select_a_gender(int pick4u)
{
int choice;
while (!validgend(flags.initrole, flags.initrace, flags.initgend))
{
if (pick4u || flags.initgend == ROLE_RANDOM || flags.randomall)
{
flags.initgend = pick_gend(flags.initrole, flags.initrace,
flags.initalign, PICK_RANDOM);
break;
}
/* select a gender */
for (;;)
{
choice = do_player_selection_menu("gender", query_gender_name);
if (choice != ROLE_NONE)
break;
/* reset */
if (flags.initalign >= 0)
flags.initalign = ROLE_NONE;
else
return 0;
}
if (choice == ROLE_QUIT)
{
flags.initrace = ROLE_NONE;
flags.initgend = ROLE_NONE;
return -1;
}
flags.initgend = choice;
}
return 0;
}
static int select_an_alignment(int pick4u)
{
int choice;
while (!validalign(flags.initrole, flags.initrace, flags.initalign))
{
if (pick4u || flags.initalign == ROLE_RANDOM || flags.randomall)
{
/* pick_align */
flags.initalign = pick_align(flags.initrole, flags.initrace,
flags.initgend, PICK_RANDOM);
break;
}
/* select an alignment */
for (;;)
{
choice = do_player_selection_menu("alignment", query_align_name);
if (choice != ROLE_NONE)
break;
/* nothing to reset ! */
return 0;
}
if (choice == ROLE_QUIT)
{
flags.initgend = ROLE_NONE;
flags.initalign = ROLE_NONE;
return -1;
}
flags.initalign = choice;
}
return 0;
}
#define INIT_IS_RANDOM(val) \
((val) == ROLE_RANDOM || \
(flags.randomall && (val) == ROLE_NONE))
static void do_random_role_checks(void)
{
if (INIT_IS_RANDOM(flags.initrole))
{
flags.initrole = pick_role(flags.initrace, flags.initgend,
flags.initalign, PICK_RANDOM);
}
if (INIT_IS_RANDOM(flags.initrace))
{
flags.initrace = pick_race(flags.initrole, flags.initgend,
flags.initalign, PICK_RANDOM);
}
if (INIT_IS_RANDOM(flags.initalign))
{
flags.initalign = pick_align(flags.initrole, flags.initrace,
flags.initgend, PICK_RANDOM);
}
if (INIT_IS_RANDOM(flags.initgend))
{
flags.initgend = pick_gend(flags.initrole, flags.initrace,
flags.initalign, PICK_RANDOM);
}
}
void Sdlgl_player_selection(void)
{
/* -AJA- Note that the initrole, initrace, initgend and initalign
* fields of the `flag' global have been set to ROLE_NONE in
* the initoptions() routine in src/options.c. Those values
* may then be updated by the system code (sys/unixmain.c)
* depending on command line options.
*/
int pick4u;
/* avoid unnecessary prompts further down */
do_random_role_checks();
rigid_role_checks();
if (select_auto_pick(&pick4u) < 0)
sdlgl_error("Quit from pick-for-you prompt.\n");
for (;;)
{
if (select_a_role(pick4u) < 0)
sdlgl_error("Quit from player selection menu.\n");
if (select_a_race(pick4u) < 0)
continue;
if (select_a_gender(pick4u) < 0)
continue;
if (select_an_alignment(pick4u) < 0)
continue;
break;
}
if (flags.initrole < 0 || flags.initrace < 0 ||
flags.initgend < 0 || flags.initalign < 0)
{
raw_printf("WARNING: failed to select a valid character ! "
"(%d,%d,%d,%d)\n", flags.initrole, flags.initrace,
flags.initgend, flags.initalign);
/* do nothing -- let init_role() deal with it */
}
}
#endif /* GL_GRAPHICS */
/*gl_role.c*/
|