void rungame(void) { int ret, role = initrole, race = initrace, gend = initgend, align = initalign; int fd = -1; char plname[BUFSZ]; fnchar filename[1024]; fnchar savedir[BUFSZ]; long t; if (!get_gamedir(SAVE_DIR, savedir)) { curses_raw_print("Could not find where to put the logfile for a new game."); return; } if (!player_selection(&role, &race, &gend, &align, random_player)) return; strncpy(plname, settings.plname, PL_NSIZ); /* The player name is set to "wizard" (again) in nh_start_game, so setting * it here just prevents wizmode player from being asked for a name. */ if (ui_flags.playmode == MODE_WIZARD) strcpy(plname, "wizard"); while (!plname[0]) curses_getline("what is your name?", plname); if (plname[0] == '\033') /* canceled */ return; t = (long)time(NULL); #if defined(WIN32) snwprintf(filename, 1024, L"%ls%ld_%hs.nhgame", savedir, t, plname); #else snprintf(filename, 1024, "%s%ld_%s.nhgame", savedir, t, plname); #endif fd = sys_open(filename, O_TRUNC | O_CREAT | O_RDWR, FILE_OPEN_MASK); if (fd == -1) { curses_raw_print("Could not create the logfile."); return; } create_game_windows(); if (!nh_start_game(fd, plname, role, race, gend, align, ui_flags.playmode)) { destroy_game_windows(); close(fd); return; } load_keymap(); /* need to load the keymap after the game has been started */ ret = commandloop(); free_keymap(); close(fd); destroy_game_windows(); cleanup_messages(); game_ended(ret, filename); }
/* start_game: Start a new game * parameters: name, role, race, gend, align, playmode */ static void ccmd_start_game(json_t * params) { char filename[1024], basename[1024], path[1024]; json_t *j_msg; const char *name; int role, race, gend, align, mode, fd, ret; long t; if (json_unpack (params, "{ss,si,si,si,si,si*}", "name", &name, "role", &role, "race", &race, "gender", &gend, "alignment", &align, "mode", &mode) == -1) exit_client("Bad set of parameters for start_game"); /* reset cached display data from a previous game */ reset_cached_diplaydata(); if (mode == MODE_WIZARD && !user_info.can_debug) mode = MODE_EXPLORE; t = (long)time(NULL); snprintf(path, 1024, "%s/save/%s/", settings.workdir, user_info.username); snprintf(basename, 1024, "%ld_%s.nhgame", t, name); snprintf(filename, 1024, "%s%s", path, basename); mkdir(path, 0755); /* should already exist unless something went wrong while upgrading */ fd = open(filename, O_EXCL | O_CREAT | O_RDWR, 0600); if (fd == -1) exit_client("Could not create the logfile"); ret = nh_start_game(fd, name, role, race, gend, align, mode); if (ret) { struct nh_roles_info *ri = nh_get_roles(); const char *rolename = (gend && ri->rolenames_f[role]) ? ri-> rolenames_f[role] : ri->rolenames_m[role]; gamefd = fd; gameid = db_add_new_game(user_info.uid, basename, rolename, ri->racenames[race], ri->gendnames[gend], ri->alignnames[align], mode, name, player_info.level_desc); log_msg("%s has started a new game (%d) as %s", user_info.username, gameid, name); j_msg = json_pack("{si,si}", "return", ret, "gameid", gameid); } else { close(fd); unlink(filename); j_msg = json_pack("{si,si}", "return", ret, "gameid", -1); } client_msg("start_game", j_msg); }