| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Functions to load & save player's settings (*.plr file) |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "dxxsconf.h" |
| 27 |
#include <stdexcept> |
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
#include <ranges> |
| 31 |
#if !defined(_MSC_VER) && !defined(macintosh) |
| 32 |
#include <unistd.h> |
| 33 |
#endif |
| 34 |
#include <errno.h> |
| 35 |
#include <ctype.h> |
| 36 |
#include <inttypes.h> |
| 37 |
|
| 38 |
#include "dxxerror.h" |
| 39 |
#include "strutil.h" |
| 40 |
#include "game.h" |
| 41 |
#include "player.h" |
| 42 |
#include "playsave.h" |
| 43 |
#include "joy.h" |
| 44 |
#include "digi.h" |
| 45 |
#include "newmenu.h" |
| 46 |
#include "palette.h" |
| 47 |
#include "config.h" |
| 48 |
#include "text.h" |
| 49 |
#include "gauges.h" |
| 50 |
#include "powerup.h" |
| 51 |
#include "makesig.h" |
| 52 |
#include "u_mem.h" |
| 53 |
#include "args.h" |
| 54 |
#include "vers_id.h" |
| 55 |
#include "newdemo.h" |
| 56 |
#include "gauges.h" |
| 57 |
#include "nvparse.h" |
| 58 |
|
| 59 |
#include "compiler-range_for.h" |
| 60 |
#include "d_range.h" |
| 61 |
#include "d_zip.h" |
| 62 |
#include "partial_range.h" |
| 63 |
|
| 64 |
#define GameNameStr "game_name" |
| 65 |
#define GameModeStr "gamemode" |
| 66 |
#define RefusePlayersStr "RefusePlayers" |
| 67 |
#define DifficultyStr "difficulty" |
| 68 |
#define GameFlagsStr "game_flags" |
| 69 |
#define AllowedItemsStr "AllowedItems" |
| 70 |
#define SpawnGrantedItemsStr "SpawnGrantedItems" |
| 71 |
#define DuplicatePrimariesStr "DuplicatePrimaries" |
| 72 |
#define DuplicateSecondariesStr "DuplicateSecondaries" |
| 73 |
#define DuplicateAccessoriesStr "DuplicateAccessories" |
| 74 |
#define ShufflePowerupsStr "ShufflePowerups" |
| 75 |
#define AlwaysLightingStr "AlwaysLighting" |
| 76 |
#define ShowEnemyNamesStr "ShowEnemyNames" |
| 77 |
#define BrightPlayersStr "BrightPlayers" |
| 78 |
#define InvulAppearStr "InvulAppear" |
| 79 |
#define KillGoalStr "KillGoal" |
| 80 |
#define PlayTimeAllowedStr "PlayTimeAllowed" |
| 81 |
#define ControlInvulTimeStr "control_invul_time" |
| 82 |
#define PacketsPerSecStr "PacketsPerSec" |
| 83 |
#define NoFriendlyFireStr "NoFriendlyFire" |
| 84 |
#define MouselookFlagsStr "Mouselook" |
| 85 |
#define PitchLockFlagsStr "PitchLockRelease" |
| 86 |
#define AutosaveIntervalStr "AutosaveInterval" |
| 87 |
#define TrackerStr "Tracker" |
| 88 |
#define TrackerNATHPStr "trackernat" |
| 89 |
#define NGPVersionStr "ngp version" |
| 90 |
|
| 91 |
#if DXX_BUILD_DESCENT == 1 |
| 92 |
#define PLX_OPTION_HEADER_TEXT "[D1X Options]" |
| 93 |
#define WEAPON_REORDER_HEADER_TEXT "[weapon reorder]" |
| 94 |
#define WEAPON_REORDER_PRIMARY_NAME_TEXT "primary" |
| 95 |
#define WEAPON_REORDER_PRIMARY_VALUE_TEXT "0x%x,0x%x,0x%x,0x%x,0x%x,0x%x" |
| 96 |
#define WEAPON_REORDER_SECONDARY_NAME_TEXT "secondary" |
| 97 |
#define WEAPON_REORDER_SECONDARY_VALUE_TEXT "0x%x,0x%x,0x%x,0x%x,0x%x,0x%x" |
| 98 |
//version 5 -> 6: added new highest level information |
| 99 |
//version 6 -> 7: stripped out the old saved_game array. |
| 100 |
//version 7 -> 8: readded the old saved_game array since this is needed |
| 101 |
// for shareware saved games |
| 102 |
//the shareware is level 4 |
| 103 |
|
| 104 |
#define SAVED_GAME_VERSION 8 //increment this every time saved_game struct changes |
| 105 |
#define COMPATIBLE_SAVED_GAME_VERSION 4 |
| 106 |
#define COMPATIBLE_PLAYER_STRUCT_VERSION 16 |
| 107 |
#elif DXX_BUILD_DESCENT == 2 |
| 108 |
#define PLX_OPTION_HEADER_TEXT "[D2X OPTIONS]" |
| 109 |
//version 5 -> 6: added new highest level information |
| 110 |
//version 6 -> 7: stripped out the old saved_game array. |
| 111 |
//version 7 -> 8: added reticle flag, & window size |
| 112 |
//version 8 -> 9: removed player_struct_version |
| 113 |
//version 9 -> 10: added default display mode |
| 114 |
//version 10 -> 11: added all toggles in toggle menu |
| 115 |
//version 11 -> 12: added weapon ordering |
| 116 |
//version 12 -> 13: added more keys |
| 117 |
//version 13 -> 14: took out marker key |
| 118 |
//version 14 -> 15: added guided in big window |
| 119 |
//version 15 -> 16: added small windows in cockpit |
| 120 |
//version 16 -> 17: ?? |
| 121 |
//version 17 -> 18: save guidebot name |
| 122 |
//version 18 -> 19: added automap-highres flag |
| 123 |
//version 19 -> 20: added kconfig data for windows joysticks |
| 124 |
//version 20 -> 21: save seperate config types for DOS & Windows |
| 125 |
//version 21 -> 22: save lifetime netstats |
| 126 |
//version 22 -> 23: ?? |
| 127 |
//version 23 -> 24: add name of joystick for windows version. |
| 128 |
|
| 129 |
#define PLAYER_FILE_VERSION 24 //increment this every time the player file changes |
| 130 |
#define COMPATIBLE_PLAYER_FILE_VERSION 17 |
| 131 |
#define AllowMarkerViewStr "Allow_marker_view" |
| 132 |
#define ThiefAbsenceFlagStr "ThiefAbsent" |
| 133 |
#define ThiefNoEnergyWeaponsFlagStr "ThiefNoEnergyWeapons" |
| 134 |
#define AllowGuidebotStr "AllowGuidebot" |
| 135 |
#endif |
| 136 |
#define KEYBOARD_HEADER_TEXT "[keyboard]" |
| 137 |
#define SENSITIVITY_NAME_TEXT "sensitivity" |
| 138 |
#define SENSITIVITY_VALUE_TEXT "%d" |
| 139 |
#define LINEAR_NAME_TEXT "linearity" |
| 140 |
#define LINEAR_VALUE_TEXT "%d" |
| 141 |
#define SPEED_NAME_TEXT "speed" |
| 142 |
#define SPEED_VALUE_TEXT "%d" |
| 143 |
#define DEADZONE_NAME_TEXT "deadzone" |
| 144 |
#define DEADZONE_VALUE_TEXT "%d" |
| 145 |
#define JOYSTICK_HEADER_TEXT "[joystick]" |
| 146 |
#define MOUSE_HEADER_TEXT "[mouse]" |
| 147 |
#define MOUSE_FLIGHTSIM_NAME_TEXT "flightsim" |
| 148 |
#define MOUSE_FLIGHTSIM_VALUE_TEXT "%d" |
| 149 |
#define MOUSE_FSDEAD_NAME_TEXT "fsdead" |
| 150 |
#define MOUSE_FSDEAD_VALUE_TEXT "%d" |
| 151 |
#define MOUSE_FSINDICATOR_NAME_TEXT "fsindi" |
| 152 |
#define MOUSE_FSINDICATOR_VALUE_TEXT "%d" |
| 153 |
#define MOUSE_OVERRUN_NAME_TEXT "overrun" |
| 154 |
#define WEAPON_KEYv2_HEADER_TEXT "[weapon keys v2]" |
| 155 |
#define WEAPON_KEYv2_VALUE_TEXT "0x%x,0x%x,0x%x" |
| 156 |
#define COCKPIT_HEADER_TEXT "[cockpit]" |
| 157 |
#define COCKPIT_MODE_NAME_TEXT "mode" |
| 158 |
#define COCKPIT_HUD_NAME_TEXT "hud" |
| 159 |
#define COCKPIT_RETICLE_TYPE_NAME_TEXT "rettype" |
| 160 |
#define COCKPIT_RETICLE_COLOR_NAME_TEXT "retrgba" |
| 161 |
#define COCKPIT_RETICLE_SIZE_NAME_TEXT "retsize" |
| 162 |
#define TOGGLES_HEADER_TEXT "[toggles]" |
| 163 |
#define TOGGLES_BOMBGAUGE_NAME_TEXT "bombgauge" |
| 164 |
#define TOGGLES_ESCORTHOTKEYS_NAME_TEXT "escorthotkeys" |
| 165 |
#define TOGGLES_PERSISTENTDEBRIS_NAME_TEXT "persistentdebris" |
| 166 |
#define TOGGLES_PRSHOT_NAME_TEXT "prshot" |
| 167 |
#define TOGGLES_NOREDUNDANCY_NAME_TEXT "noredundancy" |
| 168 |
#define TOGGLES_MULTIMESSAGES_NAME_TEXT "multimessages" |
| 169 |
#define TOGGLES_MULTIPINGHUD_NAME_TEXT "multipinghud" |
| 170 |
#define TOGGLES_NORANKINGS_NAME_TEXT "norankings" |
| 171 |
#define TOGGLES_AUTOMAPFREEFLIGHT_NAME_TEXT "automapfreeflight" |
| 172 |
#define TOGGLES_NOFIREAUTOSELECT_NAME_TEXT "nofireautoselect" |
| 173 |
#define TOGGLES_CYCLEAUTOSELECTONLY_NAME_TEXT "cycleautoselectonly" |
| 174 |
#define TOGGLES_FRIENDMISSILEVIEW_NAME_TEXT "friendmissileview" |
| 175 |
#define TOGGLES_CLOAKINVULTIMER_NAME_TEXT "cloakinvultimer" |
| 176 |
#define TOGGLES_RESPAWN_ANY_KEY "respawnkey" |
| 177 |
#define TOGGLES_MOUSELOOK "mouselook" |
| 178 |
#define TOGGLES_PITCH_LOCK "pitchlock" |
| 179 |
#define TOGGLES_THIEF_ABSENCE_SP "thiefabsent" |
| 180 |
#define TOGGLES_THIEF_NO_ENERGY_WEAPONS_SP "thiefnoenergyweapons" |
| 181 |
#define TOGGLES_AUTOSAVE_INTERVAL_SP "autosaveinterval" |
| 182 |
#define GRAPHICS_HEADER_TEXT "[graphics]" |
| 183 |
#define GRAPHICS_ALPHAEFFECTS_NAME_TEXT "alphaeffects" |
| 184 |
#define GRAPHICS_DYNLIGHTCOLOR_NAME_TEXT "dynlightcolor" |
| 185 |
#define PLX_VERSION_HEADER_TEXT "[plx version]" |
| 186 |
#define END_TEXT "[end]" |
| 187 |
|
| 188 |
#define SAVE_FILE_ID MAKE_SIG('D','P','L','R') |
| 189 |
|
| 190 |
struct player_config PlayerCfg; |
| 191 |
|
| 192 |
namespace dsx { |
| 193 |
|
| 194 |
namespace { |
| 195 |
|
| 196 |
#if DXX_BUILD_DESCENT == 1 |
| 197 |
static constexpr char eff_extension[3]{'e', 'f', 'f'}; |
| 198 |
static void plyr_read_stats(std::span<char>); |
| 199 |
static std::array<saved_game_sw, N_SAVE_SLOTS> saved_games; |
| 200 |
#elif DXX_BUILD_DESCENT == 2 |
| 201 |
static inline void plyr_read_stats(std::span<char>) {} |
| 202 |
static int get_lifetime_checksum (int a,int b); |
| 203 |
#endif |
| 204 |
|
| 205 |
template <typename weapon_type_out, typename weapon_type_from_file, std::size_t N> |
| 206 |
static void check_weapon_reorder(std::array<weapon_type_out, N> &wo, const std::array<weapon_type_from_file, N> &in) |
| 207 |
{ |
| 208 |
/* Examine the input array and update `m` to represent which elements were |
| 209 |
* found. A well-formed input array will have exactly one of each value in |
| 210 |
* the range [0..N-1], and one occurrence of the sentinel `255`. Since |
| 211 |
* only `N` elements are present, any duplicate value means some other |
| 212 |
* value will be missing. If an out of range value is found, the loop |
| 213 |
* breaks early, because that also guarantees that at least one required |
| 214 |
* element will be missing. |
| 215 |
*/ |
| 216 |
constexpr weapon_type_from_file cycle_never_autoselect_below{255}; |
| 217 |
uint_fast32_t m{0}; |
| 218 |
for (auto &&[w, i] : zip(wo, in)) |
| 219 |
{ |
| 220 |
if (i == cycle_never_autoselect_below) |
| 221 |
m |= 1 << N; |
| 222 |
else if (i < N - 1) |
| 223 |
m |= 1 << i; |
| 224 |
else |
| 225 |
break; |
| 226 |
w = weapon_type_out{static_cast<uint8_t>(i)}; |
| 227 |
} |
| 228 |
/* If `m` is equal to the value below, then every desired element appeared |
| 229 |
* in the input sequence. If the input sequence contained duplicates or |
| 230 |
* out of range values, then the test on `m` will fail, and the output |
| 231 |
* array will be reset to a sane default. |
| 232 |
*/ |
| 233 |
if (m != ((1 << N) | ((1 << (N - 1)) - 1))) |
| 234 |
{ |
| 235 |
wo[0] = weapon_type_out{cycle_never_autoselect_below}; |
| 236 |
for (const uint8_t i : xrange(N - 1)) |
| 237 |
wo[i + 1] = weapon_type_out{i}; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
void new_player_config() |
| 243 |
{ |
| 244 |
#if DXX_BUILD_DESCENT == 1 |
| 245 |
range_for (auto &i, saved_games) |
| 246 |
i.name[0] = 0; |
| 247 |
#endif |
| 248 |
InitWeaponOrdering (); //setup default weapon priorities |
| 249 |
PlayerCfg.ControlType=0; // Assume keyboard |
| 250 |
PlayerCfg.RespawnMode = RespawnPress::Any; |
| 251 |
PlayerCfg.MouselookFlags = 0; |
| 252 |
PlayerCfg.PitchLockFlags = 0; |
| 253 |
PlayerCfg.KeySettings = DefaultKeySettings; |
| 254 |
PlayerCfg.KeySettingsRebirth = DefaultKeySettingsRebirth; |
| 255 |
kc_set_controls(); |
| 256 |
|
| 257 |
PlayerCfg.DefaultDifficulty = DEFAULT_DIFFICULTY; |
| 258 |
PlayerCfg.AutoLeveling = 1; |
| 259 |
PlayerCfg.NHighestLevels = 1; |
| 260 |
PlayerCfg.HighestLevels[0].Shortname[0] = 0; //no name for mission 0 |
| 261 |
PlayerCfg.HighestLevels[0].LevelNum = 1; //was highest level in old struct |
| 262 |
{ |
| 263 |
auto &k = PlayerCfg.KeyboardSens; |
| 264 |
k[player_config_keyboard_index::turn_lr] = k[player_config_keyboard_index::pitch_ud] = k[player_config_keyboard_index::slide_lr] = k[player_config_keyboard_index::slide_ud] = k[player_config_keyboard_index::bank_lr] = 16; |
| 265 |
} |
| 266 |
{ |
| 267 |
auto &j = PlayerCfg.JoystickSens; |
| 268 |
j[player_config_joystick_index::turn_lr] = j[player_config_joystick_index::pitch_ud] = j[player_config_joystick_index::slide_lr] = j[player_config_joystick_index::slide_ud] = j[player_config_joystick_index::bank_lr] = j[player_config_joystick_index::throttle] = 8; |
| 269 |
} |
| 270 |
{ |
| 271 |
auto &j = PlayerCfg.JoystickDead; |
| 272 |
j[player_config_joystick_index::turn_lr] = j[player_config_joystick_index::pitch_ud] = j[player_config_joystick_index::slide_lr] = j[player_config_joystick_index::slide_ud] = j[player_config_joystick_index::bank_lr] = j[player_config_joystick_index::throttle] = 0; |
| 273 |
} |
| 274 |
{ |
| 275 |
auto &j = PlayerCfg.JoystickLinear; |
| 276 |
j[player_config_joystick_index::turn_lr] = j[player_config_joystick_index::pitch_ud] = j[player_config_joystick_index::slide_lr] = j[player_config_joystick_index::slide_ud] = j[player_config_joystick_index::bank_lr] = j[player_config_joystick_index::throttle] = 0; |
| 277 |
} |
| 278 |
{ |
| 279 |
auto &j = PlayerCfg.JoystickSpeed; |
| 280 |
j[player_config_joystick_index::turn_lr] = j[player_config_joystick_index::pitch_ud] = j[player_config_joystick_index::slide_lr] = j[player_config_joystick_index::slide_ud] = j[player_config_joystick_index::bank_lr] = j[player_config_joystick_index::throttle] = 16; |
| 281 |
} |
| 282 |
PlayerCfg.MouseFlightSim = 0; |
| 283 |
{ |
| 284 |
auto &m = PlayerCfg.MouseSens; |
| 285 |
m[player_config_mouse_index::turn_lr] = m[player_config_mouse_index::pitch_ud] = m[player_config_mouse_index::slide_lr] = m[player_config_mouse_index::slide_ud] = m[player_config_mouse_index::bank_lr] = m[player_config_mouse_index::throttle] = 8; |
| 286 |
} |
| 287 |
{ |
| 288 |
auto &m = PlayerCfg.MouseOverrun; |
| 289 |
m[player_config_mouse_index::turn_lr] = m[player_config_mouse_index::pitch_ud] = m[player_config_mouse_index::slide_lr] = m[player_config_mouse_index::slide_ud] = m[player_config_mouse_index::bank_lr] = m[player_config_mouse_index::throttle] = 0; |
| 290 |
} |
| 291 |
PlayerCfg.MouseFSDead = 0; |
| 292 |
PlayerCfg.MouseFSIndicator = 1; |
| 293 |
PlayerCfg.CockpitMode[0] = PlayerCfg.CockpitMode[1] = cockpit_mode_t::full_cockpit; |
| 294 |
PlayerCfg.ReticleType = reticle_type::classic; |
| 295 |
PlayerCfg.ReticleRGBA[0] = RET_COLOR_DEFAULT_R; PlayerCfg.ReticleRGBA[1] = RET_COLOR_DEFAULT_G; PlayerCfg.ReticleRGBA[2] = RET_COLOR_DEFAULT_B; PlayerCfg.ReticleRGBA[3] = RET_COLOR_DEFAULT_A; |
| 296 |
PlayerCfg.ReticleSize = 0; |
| 297 |
PlayerCfg.HudMode = HudType::Standard; |
| 298 |
#if DXX_BUILD_DESCENT == 1 |
| 299 |
PlayerCfg.BombGauge = 1; |
| 300 |
#elif DXX_BUILD_DESCENT == 2 |
| 301 |
PlayerCfg.Cockpit3DView = {}; |
| 302 |
PlayerCfg.ThiefModifierFlags = 0; |
| 303 |
PlayerCfg.MissileViewEnabled = MissileViewMode::EnabledSelfOnly; |
| 304 |
PlayerCfg.HeadlightActiveDefault = true; |
| 305 |
PlayerCfg.GuidedInBigWindow = false; |
| 306 |
PlayerCfg.GuidebotName = "GUIDE-BOT"; |
| 307 |
PlayerCfg.GuidebotNameReal = PlayerCfg.GuidebotName; |
| 308 |
PlayerCfg.EscortHotKeys = true; |
| 309 |
#endif |
| 310 |
PlayerCfg.PersistentDebris = false; |
| 311 |
PlayerCfg.PRShot = false; |
| 312 |
PlayerCfg.NoRedundancy = 0; |
| 313 |
PlayerCfg.MultiMessages = 0; |
| 314 |
PlayerCfg.MultiPingHud = 0; |
| 315 |
PlayerCfg.NoRankings = 0; |
| 316 |
PlayerCfg.AutomapFreeFlight = 0; |
| 317 |
PlayerCfg.NoFireAutoselect = FiringAutoselectMode::Immediate; |
| 318 |
PlayerCfg.CycleAutoselectOnly = 0; |
| 319 |
PlayerCfg.CloakInvulTimer = 0; |
| 320 |
PlayerCfg.AlphaEffects = 0; |
| 321 |
PlayerCfg.DynLightColor = 0; |
| 322 |
|
| 323 |
// Default taunt macros |
| 324 |
#if DXX_BUILD_DESCENT == 1 |
| 325 |
PlayerCfg.NetworkMessageMacro[multi_macro_message_index::_0].copy_if(TXT_DEF_MACRO_1); |
| 326 |
PlayerCfg.NetworkMessageMacro[multi_macro_message_index::_1].copy_if(TXT_DEF_MACRO_2); |
| 327 |
PlayerCfg.NetworkMessageMacro[multi_macro_message_index::_2].copy_if(TXT_DEF_MACRO_3); |
| 328 |
PlayerCfg.NetworkMessageMacro[multi_macro_message_index::_3].copy_if(TXT_DEF_MACRO_4); |
| 329 |
#elif DXX_BUILD_DESCENT == 2 |
| 330 |
PlayerCfg.NetworkMessageMacro[multi_macro_message_index::_0] = "Why can't we all just get along?"; |
| 331 |
PlayerCfg.NetworkMessageMacro[multi_macro_message_index::_1] = "Hey, I got a present for ya"; |
| 332 |
PlayerCfg.NetworkMessageMacro[multi_macro_message_index::_2] = "I got a hankerin' for a spankerin'"; |
| 333 |
PlayerCfg.NetworkMessageMacro[multi_macro_message_index::_3] = "This one's headed for Uranus"; |
| 334 |
#endif |
| 335 |
PlayerCfg.NetlifeKills=0; PlayerCfg.NetlifeKilled=0; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
namespace { |
| 340 |
|
| 341 |
static int convert_pattern_array(const std::span<const char> name, const std::span<int> array, const char *word, const char *line) |
| 342 |
{ |
| 343 |
if (memcmp(word, name.data(), name.size() - 1)) |
| 344 |
return 0; |
| 345 |
char *p; |
| 346 |
const unsigned long which = strtoul(word + name.size() - 1, &p, 10); |
| 347 |
if (*p || which >= array.size()) |
| 348 |
return 0; |
| 349 |
array[which] = strtol(line, NULL, 10); |
| 350 |
return 1; |
| 351 |
} |
| 352 |
|
| 353 |
static void print_pattern_array(PHYSFS_File *fout, const char *name, const std::span<const int> array) |
| 354 |
{ |
| 355 |
for (std::size_t i = 0; i < array.size(); ++i) |
| 356 |
PHYSFSX_printf(fout,"%s%" DXX_PRI_size_type "=%d\n", name, i, array[i]); |
| 357 |
} |
| 358 |
|
| 359 |
static const char *splitword(char *line, char c) |
| 360 |
{ |
| 361 |
char *p = strchr(line, c); |
| 362 |
if (p) |
| 363 |
{ |
| 364 |
*p = 0; |
| 365 |
return p + 1; |
| 366 |
} |
| 367 |
return p; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
namespace dsx { |
| 372 |
namespace { |
| 373 |
static void read_player_dxx(const char *filename) |
| 374 |
{ |
| 375 |
auto f = PHYSFSX_openReadBuffered(filename).first; |
| 376 |
if (!f) |
| 377 |
return; |
| 378 |
|
| 379 |
PlayerCfg.SPGameplayOptions.AutosaveInterval = std::chrono::minutes(10); |
| 380 |
for (PHYSFSX_gets_line_t<50> line; PHYSFSX_fgets(line,f);) |
| 381 |
{ |
| 382 |
#if DXX_BUILD_DESCENT == 1 |
| 383 |
if (!strcmp(line, WEAPON_REORDER_HEADER_TEXT)) |
| 384 |
{ |
| 385 |
while(PHYSFSX_fgets(line, f) && strcmp(line, END_TEXT)) |
| 386 |
{ |
| 387 |
const char *value=splitword(line,'='); |
| 388 |
if (!value) |
| 389 |
continue; |
| 390 |
#define CONVERT_WEAPON_REORDER_VALUE(A,F) \ |
| 391 |
if (std::array<unsigned, 6> wo; sscanf(value, F, &wo[0], &wo[1], &wo[2], &wo[3], &wo[4], &wo[5]) == 6) \ |
| 392 |
check_weapon_reorder(A, wo) |
| 393 |
if(!strcmp(line,WEAPON_REORDER_PRIMARY_NAME_TEXT)) |
| 394 |
{ |
| 395 |
CONVERT_WEAPON_REORDER_VALUE(PlayerCfg.PrimaryOrder, WEAPON_REORDER_PRIMARY_VALUE_TEXT); |
| 396 |
} |
| 397 |
else if(!strcmp(line,WEAPON_REORDER_SECONDARY_NAME_TEXT)) |
| 398 |
{ |
| 399 |
CONVERT_WEAPON_REORDER_VALUE(PlayerCfg.SecondaryOrder, WEAPON_REORDER_SECONDARY_VALUE_TEXT); |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
else |
| 404 |
#endif |
| 405 |
if (!strcmp(line,KEYBOARD_HEADER_TEXT)) |
| 406 |
{ |
| 407 |
while(PHYSFSX_fgets(line, f) && strcmp(line, END_TEXT)) |
| 408 |
{ |
| 409 |
const char *value=splitword(line,'='); |
| 410 |
if (!value) |
| 411 |
continue; |
| 412 |
convert_pattern_array(SENSITIVITY_NAME_TEXT, PlayerCfg.KeyboardSens, line, value); |
| 413 |
} |
| 414 |
} |
| 415 |
else if (!strcmp(line,JOYSTICK_HEADER_TEXT)) |
| 416 |
{ |
| 417 |
while(PHYSFSX_fgets(line, f) && strcmp(line, END_TEXT)) |
| 418 |
{ |
| 419 |
const char *value=splitword(line,'='); |
| 420 |
if (!value) |
| 421 |
continue; |
| 422 |
convert_pattern_array(SENSITIVITY_NAME_TEXT, PlayerCfg.JoystickSens, line, value) || |
| 423 |
convert_pattern_array(LINEAR_NAME_TEXT, PlayerCfg.JoystickLinear, line, value) || |
| 424 |
convert_pattern_array(SPEED_NAME_TEXT, PlayerCfg.JoystickSpeed, line, value) || |
| 425 |
convert_pattern_array(DEADZONE_NAME_TEXT, PlayerCfg.JoystickDead, line, value); |
| 426 |
} |
| 427 |
} |
| 428 |
else if (!strcmp(line,MOUSE_HEADER_TEXT)) |
| 429 |
{ |
| 430 |
while(PHYSFSX_fgets(line, f) && strcmp(line, END_TEXT)) |
| 431 |
{ |
| 432 |
const char *value=splitword(line,'='); |
| 433 |
if (!value) |
| 434 |
continue; |
| 435 |
if(!strcmp(line,MOUSE_FLIGHTSIM_NAME_TEXT)) |
| 436 |
PlayerCfg.MouseFlightSim = atoi(value); |
| 437 |
else if (convert_pattern_array(SENSITIVITY_NAME_TEXT, PlayerCfg.MouseSens, line, value)) |
| 438 |
; |
| 439 |
else if (convert_pattern_array(MOUSE_OVERRUN_NAME_TEXT, PlayerCfg.MouseOverrun, line, value)) |
| 440 |
; |
| 441 |
else if(!strcmp(line,MOUSE_FSDEAD_NAME_TEXT)) |
| 442 |
PlayerCfg.MouseFSDead = atoi(value); |
| 443 |
else if(!strcmp(line,MOUSE_FSINDICATOR_NAME_TEXT)) |
| 444 |
PlayerCfg.MouseFSIndicator = atoi(value); |
| 445 |
} |
| 446 |
} |
| 447 |
else if (!strcmp(line,WEAPON_KEYv2_HEADER_TEXT)) |
| 448 |
{ |
| 449 |
while(PHYSFSX_fgets(line,f) && strcmp(line,END_TEXT)) |
| 450 |
{ |
| 451 |
const char *value=splitword(line,'='); |
| 452 |
if (!value) |
| 453 |
continue; |
| 454 |
unsigned int kc1=0,kc2=0,kc3=0; |
| 455 |
int i=atoi(line); |
| 456 |
|
| 457 |
if(i==0) i=10; |
| 458 |
i=(i-1)*3; |
| 459 |
|
| 460 |
if (sscanf(value,WEAPON_KEYv2_VALUE_TEXT,&kc1,&kc2,&kc3) != 3) |
| 461 |
continue; |
| 462 |
PlayerCfg.KeySettingsRebirth[i] = kc1; |
| 463 |
PlayerCfg.KeySettingsRebirth[i+1] = kc2; |
| 464 |
PlayerCfg.KeySettingsRebirth[i+2] = kc3; |
| 465 |
} |
| 466 |
} |
| 467 |
else if (!strcmp(line,COCKPIT_HEADER_TEXT)) |
| 468 |
{ |
| 469 |
while(PHYSFSX_fgets(line,f) && strcmp(line,END_TEXT)) |
| 470 |
{ |
| 471 |
const char *value=splitword(line,'='); |
| 472 |
if (!value) |
| 473 |
continue; |
| 474 |
#if DXX_BUILD_DESCENT == 1 |
| 475 |
if(!strcmp(line,COCKPIT_MODE_NAME_TEXT)) |
| 476 |
PlayerCfg.CockpitMode[0] = PlayerCfg.CockpitMode[1] = static_cast<cockpit_mode_t>(atoi(value)); |
| 477 |
else |
| 478 |
#endif |
| 479 |
if(!strcmp(line,COCKPIT_HUD_NAME_TEXT)) |
| 480 |
PlayerCfg.HudMode = static_cast<HudType>(atoi(value)); |
| 481 |
else if(!strcmp(line,COCKPIT_RETICLE_TYPE_NAME_TEXT)) |
| 482 |
{ |
| 483 |
/* Require that the input be an integer and be a recognized |
| 484 |
* value for the `reticle_type` enum. Integers above the |
| 485 |
* highest `reticle_type` are ignored. |
| 486 |
*/ |
| 487 |
if (const auto r{convert_integer<uint8_t>(value)}; r) |
| 488 |
if (const auto v{*r}; v <= underlying_value(reticle_type::angle)) |
| 489 |
PlayerCfg.ReticleType = reticle_type{v}; |
| 490 |
} |
| 491 |
else if(!strcmp(line,COCKPIT_RETICLE_COLOR_NAME_TEXT)) |
| 492 |
sscanf(value,"%i,%i,%i,%i",&PlayerCfg.ReticleRGBA[0],&PlayerCfg.ReticleRGBA[1],&PlayerCfg.ReticleRGBA[2],&PlayerCfg.ReticleRGBA[3]); |
| 493 |
else if(!strcmp(line,COCKPIT_RETICLE_SIZE_NAME_TEXT)) |
| 494 |
PlayerCfg.ReticleSize = atoi(value); |
| 495 |
} |
| 496 |
} |
| 497 |
else if (!strcmp(line,TOGGLES_HEADER_TEXT)) |
| 498 |
{ |
| 499 |
PlayerCfg.MouselookFlags = 0; |
| 500 |
PlayerCfg.PitchLockFlags = 0; |
| 501 |
#if DXX_BUILD_DESCENT == 2 |
| 502 |
PlayerCfg.ThiefModifierFlags = 0; |
| 503 |
#endif |
| 504 |
while(PHYSFSX_fgets(line,f) && strcmp(line,END_TEXT)) |
| 505 |
{ |
| 506 |
const char *value=splitword(line,'='); |
| 507 |
if (!value) |
| 508 |
continue; |
| 509 |
#if DXX_BUILD_DESCENT == 1 |
| 510 |
if(!strcmp(line,TOGGLES_BOMBGAUGE_NAME_TEXT)) |
| 511 |
PlayerCfg.BombGauge = atoi(value); |
| 512 |
#elif DXX_BUILD_DESCENT == 2 |
| 513 |
if(!strcmp(line,TOGGLES_ESCORTHOTKEYS_NAME_TEXT)) |
| 514 |
{ |
| 515 |
if (const auto r{convert_integer<uint8_t>(value)}; r) |
| 516 |
PlayerCfg.EscortHotKeys = *r; |
| 517 |
} |
| 518 |
else if (!strcmp(line, TOGGLES_THIEF_ABSENCE_SP)) |
| 519 |
{ |
| 520 |
if (strtoul(value, 0, 10)) |
| 521 |
PlayerCfg.ThiefModifierFlags |= ThiefModifier::Absent; |
| 522 |
} |
| 523 |
else if (!strcmp(line, TOGGLES_THIEF_NO_ENERGY_WEAPONS_SP)) |
| 524 |
{ |
| 525 |
if (strtoul(value, 0, 10)) |
| 526 |
PlayerCfg.ThiefModifierFlags |= ThiefModifier::NoEnergyWeapons; |
| 527 |
} |
| 528 |
#endif |
| 529 |
else if (!strcmp(line, TOGGLES_AUTOSAVE_INTERVAL_SP)) |
| 530 |
{ |
| 531 |
const auto l = strtoul(value, 0, 10); |
| 532 |
PlayerCfg.SPGameplayOptions.AutosaveInterval = std::chrono::seconds(l); |
| 533 |
} |
| 534 |
if(!strcmp(line,TOGGLES_PERSISTENTDEBRIS_NAME_TEXT)) |
| 535 |
{ |
| 536 |
if (const auto r{convert_integer<uint8_t>(value)}; r) |
| 537 |
PlayerCfg.PersistentDebris = *r; |
| 538 |
} |
| 539 |
if(!strcmp(line,TOGGLES_PRSHOT_NAME_TEXT)) |
| 540 |
{ |
| 541 |
if (const auto r{convert_integer<uint8_t>(value)}; r) |
| 542 |
PlayerCfg.PRShot = *r; |
| 543 |
} |
| 544 |
if(!strcmp(line,TOGGLES_NOREDUNDANCY_NAME_TEXT)) |
| 545 |
PlayerCfg.NoRedundancy = atoi(value); |
| 546 |
if(!strcmp(line,TOGGLES_MULTIMESSAGES_NAME_TEXT)) |
| 547 |
PlayerCfg.MultiMessages = atoi(value); |
| 548 |
if(!strcmp(line,TOGGLES_MULTIPINGHUD_NAME_TEXT)) |
| 549 |
PlayerCfg.MultiPingHud = atoi(value); |
| 550 |
if(!strcmp(line,TOGGLES_NORANKINGS_NAME_TEXT)) |
| 551 |
PlayerCfg.NoRankings = atoi(value); |
| 552 |
if(!strcmp(line,TOGGLES_AUTOMAPFREEFLIGHT_NAME_TEXT)) |
| 553 |
PlayerCfg.AutomapFreeFlight = atoi(value); |
| 554 |
if(!strcmp(line,TOGGLES_NOFIREAUTOSELECT_NAME_TEXT)) |
| 555 |
PlayerCfg.NoFireAutoselect = static_cast<FiringAutoselectMode>(atoi(value)); |
| 556 |
if(!strcmp(line,TOGGLES_CYCLEAUTOSELECTONLY_NAME_TEXT)) |
| 557 |
PlayerCfg.CycleAutoselectOnly = atoi(value); |
| 558 |
if(!strcmp(line,TOGGLES_CLOAKINVULTIMER_NAME_TEXT)) |
| 559 |
PlayerCfg.CloakInvulTimer = atoi(value); |
| 560 |
else if (!strcmp(line, TOGGLES_RESPAWN_ANY_KEY)) |
| 561 |
PlayerCfg.RespawnMode = static_cast<RespawnPress>(atoi(value)); |
| 562 |
else if (!strcmp(line, TOGGLES_MOUSELOOK)) |
| 563 |
PlayerCfg.MouselookFlags = strtoul(value, 0, 10); |
| 564 |
else if (!strcmp(line, TOGGLES_PITCH_LOCK)) |
| 565 |
PlayerCfg.PitchLockFlags = strtoul(value, 0, 10); |
| 566 |
} |
| 567 |
} |
| 568 |
else if (!strcmp(line,GRAPHICS_HEADER_TEXT)) |
| 569 |
{ |
| 570 |
while(PHYSFSX_fgets(line,f) && strcmp(line,END_TEXT)) |
| 571 |
{ |
| 572 |
const char *value=splitword(line,'='); |
| 573 |
if (!value) |
| 574 |
continue; |
| 575 |
if(!strcmp(line,GRAPHICS_ALPHAEFFECTS_NAME_TEXT)) |
| 576 |
PlayerCfg.AlphaEffects = atoi(value); |
| 577 |
if(!strcmp(line,GRAPHICS_DYNLIGHTCOLOR_NAME_TEXT)) |
| 578 |
PlayerCfg.DynLightColor = atoi(value); |
| 579 |
} |
| 580 |
} |
| 581 |
else if (!strcmp(line,PLX_VERSION_HEADER_TEXT)) // know the version this pilot was used last with - allow modifications |
| 582 |
{ |
| 583 |
#if 0 |
| 584 |
int v1{0},v2=0,v3=0; |
| 585 |
#endif |
| 586 |
while(PHYSFSX_fgets(line,f) && strcmp(line,END_TEXT)) |
| 587 |
{ |
| 588 |
#if 0 |
| 589 |
const char *value=splitword(line,'='); |
| 590 |
if (!value) |
| 591 |
continue; |
| 592 |
sscanf(value,"%i.%i.%i",&v1,&v2,&v3); |
| 593 |
#endif |
| 594 |
} |
| 595 |
} |
| 596 |
else if (!strcmp(line,END_TEXT)) |
| 597 |
{ |
| 598 |
break; |
| 599 |
} |
| 600 |
else if (!strcmp(line,PLX_OPTION_HEADER_TEXT)) |
| 601 |
{ |
| 602 |
// No action needed |
| 603 |
} |
| 604 |
else |
| 605 |
{ |
| 606 |
while(PHYSFSX_fgets(line,f) && strcmp(line,END_TEXT)) |
| 607 |
{ |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
#if DXX_BUILD_DESCENT == 1 |
| 615 |
namespace { |
| 616 |
constexpr char effcode1[]="d1xrocks_SKCORX!D"; |
| 617 |
constexpr char effcode2[]="AObe)7Rn1 -+/zZ'0"; |
| 618 |
constexpr char effcode3[]="aoeuidhtnAOEUIDH6"; |
| 619 |
constexpr char effcode4[]="'/.;]<{=,+?|}->[3"; |
| 620 |
|
| 621 |
static const uint8_t *decode_stat(const uint8_t *p,int *v,const char *effcode) |
| 622 |
{ |
| 623 |
unsigned char c; |
| 624 |
int neg,i; |
| 625 |
|
| 626 |
if (p[0]==0) |
| 627 |
return NULL; |
| 628 |
else if (p[0]>='a'){ |
| 629 |
neg=1;/*I=p[0]-'a';*/ |
| 630 |
}else{ |
| 631 |
neg=0;/*I=p[0]-'A';*/ |
| 632 |
} |
| 633 |
i=0;*v=0; |
| 634 |
p++; |
| 635 |
while (p[i*2] && p[i*2+1] && p[i*2]!=' '){ |
| 636 |
c=(p[i*2]-33)+((p[i*2+1]-33)<<4); |
| 637 |
c^=effcode[i+neg]; |
| 638 |
*v+=c << (i*8); |
| 639 |
i++; |
| 640 |
} |
| 641 |
if (neg) |
| 642 |
*v *= -1; |
| 643 |
if (!p[i*2]) |
| 644 |
return NULL; |
| 645 |
return p+(i*2); |
| 646 |
} |
| 647 |
|
| 648 |
static std::pair<int /* kills */, int /* deaths */> plyr_read_stats_v(const std::span<char> filename) |
| 649 |
{ |
| 650 |
int k1=-1,k2=0,d1=-1,d2=0; |
| 651 |
int k{}, d{}; |
| 652 |
if (auto &&[f, pe] = PHYSFSX_openReadBuffered(filename.data()); f) |
| 653 |
{ |
| 654 |
PHYSFSX_gets_line_t<256> line; |
| 655 |
if (!PHYSFS_eof(f) && PHYSFSX_fgets(line, f)) |
| 656 |
{ |
| 657 |
const char *value=splitword(line,':'); |
| 658 |
if(!strcmp(line,"kills") && value) |
| 659 |
k=atoi(value); |
| 660 |
} |
| 661 |
if (!PHYSFS_eof(f) && PHYSFSX_fgets(line, f)) |
| 662 |
{ |
| 663 |
const char *value=splitword(line,':'); |
| 664 |
if(!strcmp(line,"deaths") && value) |
| 665 |
d=atoi(value); |
| 666 |
} |
| 667 |
if (!PHYSFS_eof(f) && PHYSFSX_fgets(line, f)) |
| 668 |
{ |
| 669 |
const char *value=splitword(line,':'); |
| 670 |
if(value && !strcmp(line,"key") && strlen(value)>10){ |
| 671 |
if (value[0]=='0' && value[1]=='1'){ |
| 672 |
const uint8_t *p; |
| 673 |
if ((p=decode_stat(reinterpret_cast<const unsigned char *>(value + 3), &k1, effcode1))&& |
| 674 |
(p=decode_stat(p+1,&k2,effcode2))&& |
| 675 |
(p=decode_stat(p+1,&d1,effcode3))){ |
| 676 |
decode_stat(p+1,&d2,effcode4); |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
if (k1 != k2 || k1 != k || d1 != d2 || d1 != d) |
| 682 |
k = d = 0; |
| 683 |
} |
| 684 |
else if (pe != PHYSFS_ERR_NOT_FOUND) |
| 685 |
{ |
| 686 |
/* "File not found" is normal on a new install, so do not warn about |
| 687 |
* it. |
| 688 |
*/ |
| 689 |
con_printf(CON_NORMAL, "error: failed to open player effectiveness file \"%s\": %s", filename.data(), PHYSFS_getErrorByCode(pe)); |
| 690 |
} |
| 691 |
return {k, d}; |
| 692 |
} |
| 693 |
|
| 694 |
/* This changes the extension on the filename referenced by `filename`. |
| 695 |
*/ |
| 696 |
static void plyr_read_stats(const std::span<char> filename) |
| 697 |
{ |
| 698 |
std::ranges::copy(eff_extension, std::ranges::begin(filename.last<3>())); |
| 699 |
auto &&[NetlifeKills, NetlifeKilled] = plyr_read_stats_v(filename); |
| 700 |
PlayerCfg.NetlifeKills = NetlifeKills; |
| 701 |
PlayerCfg.NetlifeKilled = NetlifeKilled; |
| 702 |
} |
| 703 |
|
| 704 |
} |
| 705 |
|
| 706 |
void plyr_save_stats(const char *const callsign, int kills, int deaths) |
| 707 |
{ |
| 708 |
struct effectiveness_key |
| 709 |
{ |
| 710 |
enum : std::size_t { buffer_size = 16 }; |
| 711 |
std::array<char, buffer_size> buf1{}, buf2{}; |
| 712 |
char i; |
| 713 |
static effectiveness_key build(int count, const std::span<const char, 18> eff1, const std::span<const char, 18> eff2) |
| 714 |
{ |
| 715 |
effectiveness_key result; |
| 716 |
const unsigned negate{count < 0 ? (count = -count, 1u) : 0u}; |
| 717 |
unsigned i{}; |
| 718 |
for (; count; ++i) |
| 719 |
{ |
| 720 |
const uint8_t truncated_count{static_cast<uint8_t>(count)}; |
| 721 |
{ |
| 722 |
const uint8_t a1 = truncated_count ^ eff1[i + negate]; |
| 723 |
result.buf1[i * 2] = (a1 & 0xF) + 33; |
| 724 |
result.buf1[i * 2 + 1] = (a1 >> 4) + 33; |
| 725 |
} |
| 726 |
{ |
| 727 |
const uint8_t a2 = truncated_count ^ eff2[i + negate]; |
| 728 |
result.buf2[i * 2] = (a2 & 0xF) + 33; |
| 729 |
result.buf2[i * 2 + 1] = (a2 >> 4) + 33; |
| 730 |
} |
| 731 |
count >>= 8; |
| 732 |
} |
| 733 |
result.i = i + (negate ? 'a' : 'A'); |
| 734 |
return result; |
| 735 |
} |
| 736 |
private: |
| 737 |
/* Require use of the helper method `build`. */ |
| 738 |
constexpr effectiveness_key() = default; |
| 739 |
}; |
| 740 |
std::array<char, 26 /* fixed bytes */ + (2 * 12) /* 2 maximum length integers */ + (2 * 2 * effectiveness_key::buffer_size) /* 2 maximum length effectiveness_key buffers */> file_contents; |
| 741 |
const auto ekills{effectiveness_key::build(kills, effcode1, effcode2)}; |
| 742 |
const auto edeaths{effectiveness_key::build(deaths, effcode3, effcode4)}; |
| 743 |
const auto content_length{ |
| 744 |
std::snprintf( |
| 745 |
file_contents.data(), file_contents.size(), |
| 746 |
"kills:%i\n" |
| 747 |
"deaths:%i\n" |
| 748 |
"key:01 " |
| 749 |
"%c%s %c%s " |
| 750 |
"%c%s %c%s\n", |
| 751 |
kills, deaths, |
| 752 |
ekills.i, ekills.buf1.data(), ekills.i, ekills.buf2.data(), |
| 753 |
edeaths.i, edeaths.buf1.data(), edeaths.i, edeaths.buf2.data() |
| 754 |
) |
| 755 |
}; |
| 756 |
if (content_length >= file_contents.size()) |
| 757 |
return; |
| 758 |
std::array<char, sizeof(PLAYER_DIRECTORY_TEXT ".eff") + CALLSIGN_LEN> filename; |
| 759 |
const auto plr_filename_length{std::snprintf(filename.data(), filename.size(), PLAYER_DIRECTORY_STRING("%.8s.eff"), callsign)}; |
| 760 |
if (plr_filename_length >= filename.size()) |
| 761 |
return; |
| 762 |
if (RAIIPHYSFS_File f{PHYSFS_openWrite(filename.data())}) |
| 763 |
PHYSFS_writeBytes(f, file_contents.data(), content_length); |
| 764 |
else |
| 765 |
{ |
| 766 |
const auto pe{PHYSFS_getLastErrorCode()}; |
| 767 |
con_printf(CON_NORMAL, "error: failed to open player effectiveness file for write \"%s\": %s", filename.data(), PHYSFS_getErrorByCode(pe)); |
| 768 |
} |
| 769 |
} |
| 770 |
#endif |
| 771 |
|
| 772 |
namespace { |
| 773 |
static int write_player_dxx(const char *filename) |
| 774 |
{ |
| 775 |
int rc{0}; |
| 776 |
char tempfile[PATH_MAX]; |
| 777 |
|
| 778 |
strcpy(tempfile,filename); |
| 779 |
tempfile[strlen(tempfile)-4]=0; |
| 780 |
strcat(tempfile,".pl$"); |
| 781 |
auto fout = PHYSFSX_openWriteBuffered(tempfile).first; |
| 782 |
if (!fout && CGameArg.SysUsePlayersDir) |
| 783 |
{ |
| 784 |
PHYSFS_mkdir(PLAYER_DIRECTORY_STRING("")); //try making directory |
| 785 |
fout = PHYSFSX_openWriteBuffered(tempfile).first; |
| 786 |
} |
| 787 |
|
| 788 |
if(fout) |
| 789 |
{ |
| 790 |
PHYSFSX_puts_literal(fout, |
| 791 |
PLX_OPTION_HEADER_TEXT "\n" |
| 792 |
#if DXX_BUILD_DESCENT == 1 |
| 793 |
WEAPON_REORDER_HEADER_TEXT "\n" |
| 794 |
#endif |
| 795 |
); |
| 796 |
#if DXX_BUILD_DESCENT == 1 |
| 797 |
PHYSFSX_printf(fout, |
| 798 |
WEAPON_REORDER_PRIMARY_NAME_TEXT "=" WEAPON_REORDER_PRIMARY_VALUE_TEXT "\n" |
| 799 |
WEAPON_REORDER_SECONDARY_NAME_TEXT "=" WEAPON_REORDER_SECONDARY_VALUE_TEXT "\n", |
| 800 |
underlying_value(PlayerCfg.PrimaryOrder[0]), underlying_value(PlayerCfg.PrimaryOrder[1]), underlying_value(PlayerCfg.PrimaryOrder[2]), underlying_value(PlayerCfg.PrimaryOrder[3]), underlying_value(PlayerCfg.PrimaryOrder[4]), underlying_value(PlayerCfg.PrimaryOrder[5]), |
| 801 |
underlying_value(PlayerCfg.SecondaryOrder[0]), underlying_value(PlayerCfg.SecondaryOrder[1]), underlying_value(PlayerCfg.SecondaryOrder[2]), underlying_value(PlayerCfg.SecondaryOrder[3]), underlying_value(PlayerCfg.SecondaryOrder[4]), underlying_value(PlayerCfg.SecondaryOrder[5])); |
| 802 |
#endif |
| 803 |
PHYSFSX_puts_literal(fout, |
| 804 |
#if DXX_BUILD_DESCENT == 1 |
| 805 |
END_TEXT "\n" |
| 806 |
#endif |
| 807 |
KEYBOARD_HEADER_TEXT "\n"); |
| 808 |
print_pattern_array(fout, SENSITIVITY_NAME_TEXT, PlayerCfg.KeyboardSens); |
| 809 |
PHYSFSX_puts_literal(fout, |
| 810 |
END_TEXT "\n" |
| 811 |
JOYSTICK_HEADER_TEXT "\n" |
| 812 |
); |
| 813 |
print_pattern_array(fout, SENSITIVITY_NAME_TEXT, PlayerCfg.JoystickSens); |
| 814 |
print_pattern_array(fout, LINEAR_NAME_TEXT, PlayerCfg.JoystickLinear); |
| 815 |
print_pattern_array(fout, SPEED_NAME_TEXT, PlayerCfg.JoystickSpeed); |
| 816 |
print_pattern_array(fout, DEADZONE_NAME_TEXT, PlayerCfg.JoystickDead); |
| 817 |
PHYSFSX_puts_literal(fout, |
| 818 |
END_TEXT "\n" |
| 819 |
MOUSE_HEADER_TEXT "\n" |
| 820 |
); |
| 821 |
PHYSFSX_printf(fout,MOUSE_FLIGHTSIM_NAME_TEXT "=" MOUSE_FLIGHTSIM_VALUE_TEXT "\n",PlayerCfg.MouseFlightSim); |
| 822 |
print_pattern_array(fout, SENSITIVITY_NAME_TEXT, PlayerCfg.MouseSens); |
| 823 |
print_pattern_array(fout, MOUSE_OVERRUN_NAME_TEXT, PlayerCfg.MouseOverrun); |
| 824 |
PHYSFSX_printf(fout,MOUSE_FSDEAD_NAME_TEXT "=" MOUSE_FSDEAD_VALUE_TEXT "\n",PlayerCfg.MouseFSDead); |
| 825 |
PHYSFSX_printf(fout,MOUSE_FSINDICATOR_NAME_TEXT "=" MOUSE_FSINDICATOR_VALUE_TEXT "\n",PlayerCfg.MouseFSIndicator); |
| 826 |
PHYSFSX_puts_literal(fout, |
| 827 |
END_TEXT "\n" |
| 828 |
WEAPON_KEYv2_HEADER_TEXT "\n" |
| 829 |
); |
| 830 |
PHYSFSX_printf(fout,"1=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[0],PlayerCfg.KeySettingsRebirth[1],PlayerCfg.KeySettingsRebirth[2]); |
| 831 |
PHYSFSX_printf(fout,"2=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[3],PlayerCfg.KeySettingsRebirth[4],PlayerCfg.KeySettingsRebirth[5]); |
| 832 |
PHYSFSX_printf(fout,"3=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[6],PlayerCfg.KeySettingsRebirth[7],PlayerCfg.KeySettingsRebirth[8]); |
| 833 |
PHYSFSX_printf(fout,"4=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[9],PlayerCfg.KeySettingsRebirth[10],PlayerCfg.KeySettingsRebirth[11]); |
| 834 |
PHYSFSX_printf(fout,"5=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[12],PlayerCfg.KeySettingsRebirth[13],PlayerCfg.KeySettingsRebirth[14]); |
| 835 |
PHYSFSX_printf(fout,"6=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[15],PlayerCfg.KeySettingsRebirth[16],PlayerCfg.KeySettingsRebirth[17]); |
| 836 |
PHYSFSX_printf(fout,"7=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[18],PlayerCfg.KeySettingsRebirth[19],PlayerCfg.KeySettingsRebirth[20]); |
| 837 |
PHYSFSX_printf(fout,"8=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[21],PlayerCfg.KeySettingsRebirth[22],PlayerCfg.KeySettingsRebirth[23]); |
| 838 |
PHYSFSX_printf(fout,"9=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[24],PlayerCfg.KeySettingsRebirth[25],PlayerCfg.KeySettingsRebirth[26]); |
| 839 |
PHYSFSX_printf(fout,"0=" WEAPON_KEYv2_VALUE_TEXT "\n",PlayerCfg.KeySettingsRebirth[27],PlayerCfg.KeySettingsRebirth[28],PlayerCfg.KeySettingsRebirth[29]); |
| 840 |
PHYSFSX_puts_literal(fout, |
| 841 |
END_TEXT "\n" |
| 842 |
COCKPIT_HEADER_TEXT "\n" |
| 843 |
); |
| 844 |
#if DXX_BUILD_DESCENT == 1 |
| 845 |
PHYSFSX_printf(fout, COCKPIT_MODE_NAME_TEXT "=%i\n", underlying_value(PlayerCfg.CockpitMode[0])); |
| 846 |
#endif |
| 847 |
PHYSFSX_printf(fout,COCKPIT_HUD_NAME_TEXT "=%u\n", static_cast<unsigned>(PlayerCfg.HudMode)); |
| 848 |
PHYSFSX_printf(fout,COCKPIT_RETICLE_TYPE_NAME_TEXT "=%i\n", underlying_value(PlayerCfg.ReticleType)); |
| 849 |
PHYSFSX_printf(fout,COCKPIT_RETICLE_COLOR_NAME_TEXT "=%i,%i,%i,%i\n",PlayerCfg.ReticleRGBA[0],PlayerCfg.ReticleRGBA[1],PlayerCfg.ReticleRGBA[2],PlayerCfg.ReticleRGBA[3]); |
| 850 |
PHYSFSX_printf(fout,COCKPIT_RETICLE_SIZE_NAME_TEXT "=%i\n",PlayerCfg.ReticleSize); |
| 851 |
PHYSFSX_puts_literal(fout, |
| 852 |
END_TEXT "\n" |
| 853 |
TOGGLES_HEADER_TEXT "\n" |
| 854 |
); |
| 855 |
#if DXX_BUILD_DESCENT == 1 |
| 856 |
PHYSFSX_printf(fout,TOGGLES_BOMBGAUGE_NAME_TEXT "=%i\n",PlayerCfg.BombGauge); |
| 857 |
#elif DXX_BUILD_DESCENT == 2 |
| 858 |
PHYSFSX_printf(fout,TOGGLES_ESCORTHOTKEYS_NAME_TEXT "=%i\n",PlayerCfg.EscortHotKeys); |
| 859 |
PHYSFSX_printf(fout, TOGGLES_THIEF_ABSENCE_SP "=%i\n", PlayerCfg.ThiefModifierFlags & ThiefModifier::Absent); |
| 860 |
PHYSFSX_printf(fout, TOGGLES_THIEF_NO_ENERGY_WEAPONS_SP "=%i\n", PlayerCfg.ThiefModifierFlags & ThiefModifier::NoEnergyWeapons); |
| 861 |
#endif |
| 862 |
PHYSFSX_printf(fout, TOGGLES_AUTOSAVE_INTERVAL_SP "=%i\n", PlayerCfg.SPGameplayOptions.AutosaveInterval.count()); |
| 863 |
PHYSFSX_printf(fout,TOGGLES_PERSISTENTDEBRIS_NAME_TEXT "=%i\n",PlayerCfg.PersistentDebris); |
| 864 |
PHYSFSX_printf(fout,TOGGLES_PRSHOT_NAME_TEXT "=%i\n",PlayerCfg.PRShot); |
| 865 |
PHYSFSX_printf(fout,TOGGLES_NOREDUNDANCY_NAME_TEXT "=%i\n",PlayerCfg.NoRedundancy); |
| 866 |
PHYSFSX_printf(fout,TOGGLES_MULTIMESSAGES_NAME_TEXT "=%i\n",PlayerCfg.MultiMessages); |
| 867 |
PHYSFSX_printf(fout,TOGGLES_MULTIPINGHUD_NAME_TEXT "=%i\n",PlayerCfg.MultiPingHud); |
| 868 |
PHYSFSX_printf(fout,TOGGLES_NORANKINGS_NAME_TEXT "=%i\n",PlayerCfg.NoRankings); |
| 869 |
PHYSFSX_printf(fout,TOGGLES_AUTOMAPFREEFLIGHT_NAME_TEXT "=%i\n",PlayerCfg.AutomapFreeFlight); |
| 870 |
PHYSFSX_printf(fout,TOGGLES_NOFIREAUTOSELECT_NAME_TEXT "=%i\n",static_cast<unsigned>(PlayerCfg.NoFireAutoselect)); |
| 871 |
PHYSFSX_printf(fout,TOGGLES_CYCLEAUTOSELECTONLY_NAME_TEXT "=%i\n",PlayerCfg.CycleAutoselectOnly); |
| 872 |
PHYSFSX_printf(fout,TOGGLES_CLOAKINVULTIMER_NAME_TEXT "=%i\n",PlayerCfg.CloakInvulTimer); |
| 873 |
PHYSFSX_printf(fout,TOGGLES_RESPAWN_ANY_KEY "=%i\n",static_cast<unsigned>(PlayerCfg.RespawnMode)); |
| 874 |
PHYSFSX_printf(fout, TOGGLES_MOUSELOOK "=%i\n", PlayerCfg.MouselookFlags); |
| 875 |
PHYSFSX_printf(fout, TOGGLES_PITCH_LOCK "=%i\n", PlayerCfg.PitchLockFlags); |
| 876 |
PHYSFSX_puts_literal(fout, |
| 877 |
END_TEXT "\n" |
| 878 |
GRAPHICS_HEADER_TEXT "\n" |
| 879 |
); |
| 880 |
PHYSFSX_printf(fout,GRAPHICS_ALPHAEFFECTS_NAME_TEXT "=%i\n",PlayerCfg.AlphaEffects); |
| 881 |
PHYSFSX_printf(fout,GRAPHICS_DYNLIGHTCOLOR_NAME_TEXT "=%i\n",PlayerCfg.DynLightColor); |
| 882 |
PHYSFSX_puts_literal(fout, END_TEXT "\n" |
| 883 |
PLX_VERSION_HEADER_TEXT "\n" |
| 884 |
"plx version=" DXX_VERSION_STR "\n" |
| 885 |
END_TEXT "\n" |
| 886 |
END_TEXT "\n" |
| 887 |
); |
| 888 |
fout.reset(); |
| 889 |
if(rc==0) |
| 890 |
{ |
| 891 |
PHYSFS_delete(filename); |
| 892 |
rc = PHYSFSX_rename(tempfile,filename); |
| 893 |
} |
| 894 |
return rc; |
| 895 |
} |
| 896 |
else |
| 897 |
return errno; |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
//read in the player's saved games. returns errno (0 == no error) |
| 902 |
int read_player_file() |
| 903 |
{ |
| 904 |
#if DXX_BUILD_DESCENT == 1 |
| 905 |
int shareware_file = -1; |
| 906 |
int player_file_size; |
| 907 |
#elif DXX_BUILD_DESCENT == 2 |
| 908 |
int rewrite_it{0}; |
| 909 |
int swap{0}; |
| 910 |
short player_file_version; |
| 911 |
#endif |
| 912 |
|
| 913 |
Assert(Player_num < MAX_PLAYERS); |
| 914 |
|
| 915 |
std::array<char, sizeof(PLAYER_DIRECTORY_TEXT ".plr") + CALLSIGN_LEN> filename; |
| 916 |
const auto plr_filename_length{std::snprintf(filename.data(), filename.size(), PLAYER_DIRECTORY_STRING("%.8s.plr"), static_cast<const char *>(InterfaceUniqueState.PilotName))}; |
| 917 |
if (plr_filename_length >= filename.size()) |
| 918 |
return -1; |
| 919 |
auto &&[file, physfserr]{PHYSFSX_openReadBuffered(filename.data())}; |
| 920 |
if (!file) |
| 921 |
{ |
| 922 |
if (physfserr == PHYSFS_ERR_NOT_FOUND) |
| 923 |
return ENOENT; |
| 924 |
nm_messagebox(menu_title{TXT_ERROR}, {TXT_OK}, "Failed to open PLR file\n%s\n\n%s", filename.data(), PHYSFS_getErrorByCode(physfserr)); |
| 925 |
return -1; |
| 926 |
} |
| 927 |
|
| 928 |
new_player_config(); // Set defaults! |
| 929 |
|
| 930 |
#if DXX_BUILD_DESCENT == 1 |
| 931 |
// Unfortunatly d1x has been writing both shareware and registered |
| 932 |
// player files with a saved_game_version of 7 and 8, whereas the |
| 933 |
// original decent used 4 for shareware games and 7 for registered |
| 934 |
// games. Because of this the player files didn't get properly read |
| 935 |
// when reading d1x shareware player files in d1x registered or |
| 936 |
// vica versa. The problem is that the sizeof of the taunt macros |
| 937 |
// differ between the share and registered versions, causing the |
| 938 |
// reading of the player file to go wrong. Thus we now determine the |
| 939 |
// sizeof the player file to determine what kinda player file we are |
| 940 |
// dealing with so that we can do the right thing |
| 941 |
PHYSFS_seek(file, 0); |
| 942 |
player_file_size = PHYSFS_fileLength(file); |
| 943 |
#endif |
| 944 |
int id; |
| 945 |
PHYSFS_readSLE32(file, &id); |
| 946 |
#if DXX_BUILD_DESCENT == 1 |
| 947 |
short saved_game_version, player_struct_version; |
| 948 |
saved_game_version = PHYSFSX_readSLE16(file); |
| 949 |
player_struct_version = PHYSFSX_readSLE16(file); |
| 950 |
PlayerCfg.NHighestLevels = PHYSFSX_readInt(file); |
| 951 |
{ |
| 952 |
const unsigned u = PHYSFSX_readInt(file); |
| 953 |
PlayerCfg.DefaultDifficulty = cast_clamp_difficulty(u); |
| 954 |
} |
| 955 |
PlayerCfg.AutoLeveling = PHYSFSX_readInt(file); |
| 956 |
#elif DXX_BUILD_DESCENT == 2 |
| 957 |
player_file_version = PHYSFSX_readSLE16(file); |
| 958 |
#endif |
| 959 |
|
| 960 |
if (id!=SAVE_FILE_ID) { |
| 961 |
struct error_invalid_player_file_magic : passive_messagebox |
| 962 |
{ |
| 963 |
error_invalid_player_file_magic() : |
| 964 |
passive_messagebox(menu_title{TXT_ERROR}, menu_subtitle{"Invalid player file"}, TXT_OK, grd_curscreen->sc_canvas) |
| 965 |
{ |
| 966 |
} |
| 967 |
}; |
| 968 |
run_blocking_newmenu<error_invalid_player_file_magic>(); |
| 969 |
return -1; |
| 970 |
} |
| 971 |
|
| 972 |
#if DXX_BUILD_DESCENT == 1 |
| 973 |
if (saved_game_version < COMPATIBLE_SAVED_GAME_VERSION || player_struct_version < COMPATIBLE_PLAYER_STRUCT_VERSION) { |
| 974 |
struct error_invalid_player_file_version : passive_messagebox |
| 975 |
{ |
| 976 |
error_invalid_player_file_version() : |
| 977 |
passive_messagebox(menu_title{TXT_ERROR}, menu_subtitle{TXT_ERROR_PLR_VERSION}, TXT_OK, grd_curscreen->sc_canvas) |
| 978 |
{ |
| 979 |
} |
| 980 |
}; |
| 981 |
run_blocking_newmenu<error_invalid_player_file_version>(); |
| 982 |
return -1; |
| 983 |
} |
| 984 |
|
| 985 |
/* determine if we're dealing with a shareware or registered playerfile */ |
| 986 |
switch (saved_game_version) |
| 987 |
{ |
| 988 |
case 4: |
| 989 |
shareware_file = 1; |
| 990 |
break; |
| 991 |
case 5: |
| 992 |
case 6: |
| 993 |
shareware_file = 0; |
| 994 |
break; |
| 995 |
case 7: |
| 996 |
/* version 7 doesn't have the saved games array */ |
| 997 |
if ((player_file_size - (sizeof(hli)*PlayerCfg.NHighestLevels)) == (2212 - sizeof(saved_games))) |
| 998 |
shareware_file = 1; |
| 999 |
if ((player_file_size - (sizeof(hli)*PlayerCfg.NHighestLevels)) == (2252 - sizeof(saved_games))) |
| 1000 |
shareware_file = 0; |
| 1001 |
break; |
| 1002 |
case 8: |
| 1003 |
if ((player_file_size - (sizeof(hli)*PlayerCfg.NHighestLevels)) == 2212) |
| 1004 |
shareware_file = 1; |
| 1005 |
if ((player_file_size - (sizeof(hli)*PlayerCfg.NHighestLevels)) == 2252) |
| 1006 |
shareware_file = 0; |
| 1007 |
/* d1x-rebirth v0.31 to v0.42 broke things by adding stuff to the |
| 1008 |
player struct without thinking (sigh) */ |
| 1009 |
if ((player_file_size - (sizeof(hli)*PlayerCfg.NHighestLevels)) == (2212 + 2*sizeof(int))) |
| 1010 |
{ |
| 1011 |
|
| 1012 |
shareware_file = 1; |
| 1013 |
/* skip the cruft added to the player_info struct */ |
| 1014 |
PHYSFS_seek(file, PHYSFS_tell(file)+2*sizeof(int)); |
| 1015 |
} |
| 1016 |
if ((player_file_size - (sizeof(hli)*PlayerCfg.NHighestLevels)) == (2252 + 2*sizeof(int))) |
| 1017 |
{ |
| 1018 |
shareware_file = 0; |
| 1019 |
/* skip the cruft added to the player_info struct */ |
| 1020 |
PHYSFS_seek(file, PHYSFS_tell(file)+2*sizeof(int)); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (shareware_file == -1) { |
| 1025 |
struct error_invalid_player_file_size : passive_messagebox |
| 1026 |
{ |
| 1027 |
error_invalid_player_file_size() : |
| 1028 |
passive_messagebox(menu_title{TXT_ERROR}, menu_subtitle{"Error invalid or unknown\nplayerfile-size"}, TXT_OK, grd_curscreen->sc_canvas) |
| 1029 |
{ |
| 1030 |
} |
| 1031 |
}; |
| 1032 |
run_blocking_newmenu<error_invalid_player_file_size>(); |
| 1033 |
return -1; |
| 1034 |
} |
| 1035 |
|
| 1036 |
if (saved_game_version <= 5) { |
| 1037 |
|
| 1038 |
//deal with old-style highest level info |
| 1039 |
|
| 1040 |
PlayerCfg.HighestLevels[0].Shortname[0] = 0; //no name for mission 0 |
| 1041 |
PlayerCfg.HighestLevels[0].LevelNum = PlayerCfg.NHighestLevels; //was highest level in old struct |
| 1042 |
|
| 1043 |
//This hack allows the player to start on level 8 if he's made it to |
| 1044 |
//level 7 on the shareware. We do this because the shareware didn't |
| 1045 |
//save the information that the player finished level 7, so the most |
| 1046 |
//we know is that he made it to level 7. |
| 1047 |
if (PlayerCfg.NHighestLevels==7) |
| 1048 |
PlayerCfg.HighestLevels[0].LevelNum = 8; |
| 1049 |
|
| 1050 |
} |
| 1051 |
else { //read new highest level info |
| 1052 |
if (const std::size_t buffer_size{sizeof(hli) * PlayerCfg.NHighestLevels}; PHYSFSX_readBytes(file, PlayerCfg.HighestLevels, buffer_size) != buffer_size) |
| 1053 |
goto read_player_file_failed; |
| 1054 |
} |
| 1055 |
|
| 1056 |
if ( saved_game_version != 7 ) { // Read old & SW saved games. |
| 1057 |
if (PHYSFSX_readBytes(file, saved_games, sizeof(saved_games)) != sizeof(saved_games)) |
| 1058 |
goto read_player_file_failed; |
| 1059 |
} |
| 1060 |
|
| 1061 |
#elif DXX_BUILD_DESCENT == 2 |
| 1062 |
if (player_file_version > 255) // bigendian file? |
| 1063 |
swap = 1; |
| 1064 |
|
| 1065 |
if (swap) |
| 1066 |
player_file_version = SWAPSHORT(player_file_version); |
| 1067 |
|
| 1068 |
if (player_file_version < COMPATIBLE_PLAYER_FILE_VERSION) { |
| 1069 |
struct error_invalid_player_file_version : passive_messagebox |
| 1070 |
{ |
| 1071 |
error_invalid_player_file_version() : |
| 1072 |
passive_messagebox(menu_title{TXT_ERROR}, menu_subtitle{TXT_ERROR_PLR_VERSION}, TXT_OK, grd_curscreen->sc_canvas) |
| 1073 |
{ |
| 1074 |
} |
| 1075 |
}; |
| 1076 |
run_blocking_newmenu<error_invalid_player_file_version>(); |
| 1077 |
return -1; |
| 1078 |
} |
| 1079 |
|
| 1080 |
PHYSFS_seek(file,PHYSFS_tell(file)+2*sizeof(short)); //skip Game_window_w,Game_window_h |
| 1081 |
PlayerCfg.DefaultDifficulty = cast_clamp_difficulty(PHYSFSX_readByte(file)); |
| 1082 |
PlayerCfg.AutoLeveling = PHYSFSX_readByte(file); |
| 1083 |
PHYSFS_seek(file,PHYSFS_tell(file)+sizeof(sbyte)); // skip ReticleOn |
| 1084 |
PlayerCfg.CockpitMode[0] = PlayerCfg.CockpitMode[1] = static_cast<cockpit_mode_t>(PHYSFSX_readByte(file)); |
| 1085 |
PHYSFS_seek(file,PHYSFS_tell(file)+sizeof(sbyte)); //skip Default_display_mode |
| 1086 |
{ |
| 1087 |
auto i = PHYSFSX_readByte(file); |
| 1088 |
switch (i) |
| 1089 |
{ |
| 1090 |
case static_cast<unsigned>(MissileViewMode::None): |
| 1091 |
case static_cast<unsigned>(MissileViewMode::EnabledSelfOnly): |
| 1092 |
case static_cast<unsigned>(MissileViewMode::EnabledSelfAndAllies): |
| 1093 |
break; |
| 1094 |
default: |
| 1095 |
i = 0; |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
PlayerCfg.MissileViewEnabled = static_cast<MissileViewMode>(i); |
| 1099 |
} |
| 1100 |
PlayerCfg.HeadlightActiveDefault = PHYSFSX_readByte(file); |
| 1101 |
PlayerCfg.GuidedInBigWindow = PHYSFSX_readByte(file); |
| 1102 |
if (player_file_version >= 19) |
| 1103 |
PHYSFS_seek(file,PHYSFS_tell(file)+sizeof(sbyte)); //skip Automap_always_hires |
| 1104 |
|
| 1105 |
//read new highest level info |
| 1106 |
|
| 1107 |
PlayerCfg.NHighestLevels = PHYSFSX_readSLE16(file); |
| 1108 |
if (swap) |
| 1109 |
PlayerCfg.NHighestLevels = SWAPSHORT(PlayerCfg.NHighestLevels); |
| 1110 |
Assert(PlayerCfg.NHighestLevels <= MAX_MISSIONS); |
| 1111 |
|
| 1112 |
if (const std::size_t buffer_size{sizeof(hli) * PlayerCfg.NHighestLevels}; PHYSFSX_readBytes(file, PlayerCfg.HighestLevels, buffer_size) != buffer_size) |
| 1113 |
goto read_player_file_failed; |
| 1114 |
#endif |
| 1115 |
|
| 1116 |
//read taunt macros |
| 1117 |
{ |
| 1118 |
int len; |
| 1119 |
|
| 1120 |
#if DXX_BUILD_DESCENT == 1 |
| 1121 |
len = shareware_file? 25:35; |
| 1122 |
#elif DXX_BUILD_DESCENT == 2 |
| 1123 |
len = MAX_MESSAGE_LEN; |
| 1124 |
#endif |
| 1125 |
|
| 1126 |
for (auto &i : PlayerCfg.NetworkMessageMacro) |
| 1127 |
if (PHYSFSX_readBytes(file, i, len) != len) |
| 1128 |
goto read_player_file_failed; |
| 1129 |
} |
| 1130 |
|
| 1131 |
//read kconfig data |
| 1132 |
{ |
| 1133 |
ubyte dummy_joy_sens; |
| 1134 |
|
| 1135 |
if (PHYSFSX_readBytes(file, &PlayerCfg.KeySettings.Keyboard, sizeof(PlayerCfg.KeySettings.Keyboard)) != sizeof(PlayerCfg.KeySettings.Keyboard)) |
| 1136 |
goto read_player_file_failed; |
| 1137 |
#if DXX_MAX_JOYSTICKS |
| 1138 |
auto &KeySettingsJoystick = PlayerCfg.KeySettings.Joystick; |
| 1139 |
#else |
| 1140 |
std::array<uint8_t, MAX_CONTROLS> KeySettingsJoystick; |
| 1141 |
#endif |
| 1142 |
if (PHYSFSX_readBytes(file, &KeySettingsJoystick, sizeof(KeySettingsJoystick)) != sizeof(KeySettingsJoystick)) |
| 1143 |
goto read_player_file_failed; |
| 1144 |
PHYSFS_seek( file, PHYSFS_tell(file)+(sizeof(ubyte)*MAX_CONTROLS*3) ); // Skip obsolete Flightstick/Thrustmaster/Gravis map fields |
| 1145 |
if (PHYSFSX_readBytes(file, &PlayerCfg.KeySettings.Mouse, sizeof(PlayerCfg.KeySettings.Mouse)) != sizeof(PlayerCfg.KeySettings.Mouse)) |
| 1146 |
goto read_player_file_failed; |
| 1147 |
PHYSFS_seek( file, PHYSFS_tell(file)+(sizeof(ubyte)*MAX_CONTROLS) ); // Skip obsolete Cyberman map field |
| 1148 |
#if DXX_BUILD_DESCENT == 1 |
| 1149 |
if (PHYSFSX_readBytes(file, &PlayerCfg.ControlType, sizeof(uint8_t)) != sizeof(uint8_t)) |
| 1150 |
#elif DXX_BUILD_DESCENT == 2 |
| 1151 |
if (player_file_version>=20) |
| 1152 |
PHYSFS_seek( file, PHYSFS_tell(file)+(sizeof(ubyte)*MAX_CONTROLS) ); // Skip obsolete Winjoy map field |
| 1153 |
uint8_t control_type_dos, control_type_win; |
| 1154 |
if (PHYSFSX_readBytes(file, &control_type_dos, sizeof(uint8_t)) != sizeof(uint8_t)) |
| 1155 |
goto read_player_file_failed; |
| 1156 |
else if (player_file_version >= 21 && PHYSFSX_readBytes(file, &control_type_win, sizeof(uint8_t)) != sizeof(uint8_t)) |
| 1157 |
#endif |
| 1158 |
goto read_player_file_failed; |
| 1159 |
else if (PHYSFSX_readBytes(file, &dummy_joy_sens, sizeof(uint8_t)) != sizeof(uint8_t)) |
| 1160 |
goto read_player_file_failed; |
| 1161 |
|
| 1162 |
#if DXX_BUILD_DESCENT == 2 |
| 1163 |
PlayerCfg.ControlType = control_type_dos; |
| 1164 |
|
| 1165 |
std::array<uint8_t, 22> weapon_file_order{}; |
| 1166 |
std::array<uint8_t, 11> primary_order, secondary_order; |
| 1167 |
PHYSFSX_readBytes(file, weapon_file_order.data(), weapon_file_order.size()); |
| 1168 |
range_for (const unsigned i, xrange(11u)) |
| 1169 |
{ |
| 1170 |
primary_order[i] = weapon_file_order[i * 2]; |
| 1171 |
secondary_order[i] = weapon_file_order[(i * 2) + 1]; |
| 1172 |
} |
| 1173 |
check_weapon_reorder(PlayerCfg.PrimaryOrder, primary_order); |
| 1174 |
check_weapon_reorder(PlayerCfg.SecondaryOrder, secondary_order); |
| 1175 |
|
| 1176 |
if (player_file_version>=16) |
| 1177 |
{ |
| 1178 |
PHYSFS_sint32 view_primary, view_secondary; |
| 1179 |
PHYSFS_readSLE32(file, &view_primary); |
| 1180 |
PHYSFS_readSLE32(file, &view_secondary); |
| 1181 |
if (swap) |
| 1182 |
{ |
| 1183 |
view_primary = SWAPINT(view_primary); |
| 1184 |
view_secondary = SWAPINT(view_secondary); |
| 1185 |
} |
| 1186 |
if (view_primary <= static_cast<unsigned>(cockpit_3d_view::Marker)) |
| 1187 |
PlayerCfg.Cockpit3DView[gauge_inset_window_view::primary] = static_cast<cockpit_3d_view>(view_primary); |
| 1188 |
if (view_secondary <= static_cast<unsigned>(cockpit_3d_view::Marker)) |
| 1189 |
PlayerCfg.Cockpit3DView[gauge_inset_window_view::secondary] = static_cast<cockpit_3d_view>(view_secondary); |
| 1190 |
} |
| 1191 |
#endif |
| 1192 |
} |
| 1193 |
|
| 1194 |
#if DXX_BUILD_DESCENT == 1 |
| 1195 |
if ( saved_game_version != 7 ) { |
| 1196 |
int i, found=0; |
| 1197 |
|
| 1198 |
Assert( N_SAVE_SLOTS == 10 ); |
| 1199 |
|
| 1200 |
for (i=0; i < N_SAVE_SLOTS; i++ ) { |
| 1201 |
if ( saved_games[i].name[0] ) { |
| 1202 |
throw std::runtime_error("old save game not supported"); |
| 1203 |
// make sure we do not do this again, which would possibly overwrite |
| 1204 |
// a new newstyle savegame |
| 1205 |
saved_games[i].name[0] = 0; |
| 1206 |
found++; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
if (found) |
| 1210 |
write_player_file(); |
| 1211 |
} |
| 1212 |
#elif DXX_BUILD_DESCENT == 2 |
| 1213 |
if (player_file_version>=22) |
| 1214 |
{ |
| 1215 |
PHYSFS_readSLE32(file, &PlayerCfg.NetlifeKills); |
| 1216 |
PHYSFS_readSLE32(file, &PlayerCfg.NetlifeKilled); |
| 1217 |
if (swap) { |
| 1218 |
PlayerCfg.NetlifeKills = SWAPINT(PlayerCfg.NetlifeKills); |
| 1219 |
PlayerCfg.NetlifeKilled = SWAPINT(PlayerCfg.NetlifeKilled); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
else |
| 1223 |
{ |
| 1224 |
PlayerCfg.NetlifeKills=0; PlayerCfg.NetlifeKilled=0; |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (player_file_version>=23) |
| 1228 |
{ |
| 1229 |
int i; |
| 1230 |
PHYSFS_readSLE32(file, &i); |
| 1231 |
if (swap) |
| 1232 |
i = SWAPINT(i); |
| 1233 |
if (i!=get_lifetime_checksum (PlayerCfg.NetlifeKills,PlayerCfg.NetlifeKilled)) |
| 1234 |
{ |
| 1235 |
PlayerCfg.NetlifeKills=0; PlayerCfg.NetlifeKilled=0; |
| 1236 |
struct error_invalid_player_file_checksum : passive_messagebox |
| 1237 |
{ |
| 1238 |
error_invalid_player_file_checksum() : |
| 1239 |
passive_messagebox(menu_title{nullptr}, menu_subtitle{"Lifetime kill error"}, TXT_OK, grd_curscreen->sc_canvas) |
| 1240 |
{ |
| 1241 |
} |
| 1242 |
}; |
| 1243 |
run_blocking_newmenu<error_invalid_player_file_checksum>(); |
| 1244 |
rewrite_it=1; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
//read guidebot name |
| 1249 |
if (player_file_version >= 18 && PHYSFSX_fgets(PlayerCfg.GuidebotName, file)) |
| 1250 |
{ |
| 1251 |
} |
| 1252 |
else |
| 1253 |
PlayerCfg.GuidebotName = "GUIDE-BOT"; |
| 1254 |
PlayerCfg.GuidebotNameReal = PlayerCfg.GuidebotName; |
| 1255 |
{ |
| 1256 |
if (player_file_version >= 24) |
| 1257 |
{ |
| 1258 |
PHYSFSX_gets_line_t<128> buf; |
| 1259 |
if (PHYSFSX_fgets(buf, file)) // Just read it in for DPS. |
| 1260 |
{ |
| 1261 |
/* Nothing to do. Buffer contents are ignored. This is only |
| 1262 |
* read for its side effect on the file position. |
| 1263 |
*/ |
| 1264 |
} |
| 1265 |
} |
| 1266 |
} |
| 1267 |
#endif |
| 1268 |
|
| 1269 |
#if DXX_BUILD_DESCENT == 2 |
| 1270 |
if (rewrite_it) |
| 1271 |
write_player_file(); |
| 1272 |
#endif |
| 1273 |
|
| 1274 |
filename[plr_filename_length - 1] = 'x'; |
| 1275 |
read_player_dxx(filename.data()); |
| 1276 |
plyr_read_stats(std::span(filename).first(plr_filename_length)); |
| 1277 |
kc_set_controls(); |
| 1278 |
|
| 1279 |
return EZERO; |
| 1280 |
|
| 1281 |
read_player_file_failed: |
| 1282 |
nm_messagebox(menu_title{TXT_ERROR}, {TXT_OK}, "%s\n\n%s", "Error reading PLR file", PHYSFS_getLastError()); |
| 1283 |
return -1; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
namespace { |
| 1288 |
/* Given a Mission_path, return a pair of pointers. |
| 1289 |
* - If the mission cannot be saved, both pointers are nullptr. |
| 1290 |
* - If the mission name was previously used, return a pointer to that |
| 1291 |
* entry and a pointer to end. |
| 1292 |
* - If the mission name is not recorded, return a pointer to the first |
| 1293 |
* unused element (which may be end() if all elements are used) and a |
| 1294 |
* pointer to end(). The caller must check that the first unused |
| 1295 |
* element is not end(). |
| 1296 |
*/ |
| 1297 |
static std::array<std::array<hli, MAX_MISSIONS>::pointer, 2> find_hli_entry(const std::ranges::subrange<hli *> r, const Mission_path &m) |
| 1298 |
{ |
| 1299 |
const auto mission_filename{m.filename}; |
| 1300 |
const auto mission_length = std::distance(mission_filename, m.path.end()); |
| 1301 |
if (mission_length >= sizeof(hli::Shortname)) |
| 1302 |
{ |
| 1303 |
/* Name is too long to store, so there will never be a match |
| 1304 |
* and it is never stored. |
| 1305 |
*/ |
| 1306 |
con_printf(CON_URGENT, DXX_STRINGIZE_FL(__FILE__, __LINE__, "warning: highest level information cannot be tracked because the mission name is too long to store (should be at most 8 bytes, but is \"%s\")."), &*mission_filename); |
| 1307 |
return {{nullptr, nullptr}}; |
| 1308 |
} |
| 1309 |
const auto &&a = [p = &*mission_filename](const hli &h) { |
| 1310 |
return !d_stricmp(h.Shortname.data(), p); |
| 1311 |
}; |
| 1312 |
const auto &&i{std::ranges::find_if(r, a)}; |
| 1313 |
return {{&*i, r.end()}}; |
| 1314 |
} |
| 1315 |
} |
| 1316 |
|
| 1317 |
//set a new highest level for player for this mission |
| 1318 |
void set_highest_level(const uint8_t best_levelnum_this_game) |
| 1319 |
{ |
| 1320 |
int ret; |
| 1321 |
|
| 1322 |
if ((ret=read_player_file()) != EZERO) |
| 1323 |
if (ret != ENOENT) //if file doesn't exist, that's ok |
| 1324 |
return; |
| 1325 |
|
| 1326 |
const auto &&r = partial_range(PlayerCfg.HighestLevels, PlayerCfg.NHighestLevels); |
| 1327 |
const auto &&i = find_hli_entry(r, *Current_mission); |
| 1328 |
const auto ie = i[1]; |
| 1329 |
if (!ie) |
| 1330 |
return; |
| 1331 |
|
| 1332 |
auto ii = i[0]; |
| 1333 |
const hli *d1_preferred = nullptr; |
| 1334 |
#if DXX_BUILD_DESCENT == 2 |
| 1335 |
const hli *d2_preferred = nullptr, *d2x_preferred = nullptr; |
| 1336 |
#endif |
| 1337 |
range_for (auto &m, r) |
| 1338 |
{ |
| 1339 |
hli *preferred; |
| 1340 |
/* Swapping instead of rotating will perturb the order a bit, |
| 1341 |
* but this is a one-time fix to get builtin missions to |
| 1342 |
* reserved storage. |
| 1343 |
*/ |
| 1344 |
const auto ms = m.Shortname.data(); |
| 1345 |
if (!d1_preferred && !strcmp(D1_MISSION_FILENAME, ms)) |
| 1346 |
d1_preferred = preferred = &PlayerCfg.HighestLevels[0]; |
| 1347 |
#if DXX_BUILD_DESCENT == 2 |
| 1348 |
else if (!d2_preferred && !strcmp(FULL_MISSION_FILENAME, ms)) |
| 1349 |
d2_preferred = preferred = &PlayerCfg.HighestLevels[1]; |
| 1350 |
else if (!d2x_preferred && !strcmp("d2x", ms)) |
| 1351 |
d2x_preferred = preferred = &PlayerCfg.HighestLevels[2]; |
| 1352 |
#endif |
| 1353 |
else |
| 1354 |
continue; |
| 1355 |
if (preferred == &m) |
| 1356 |
continue; |
| 1357 |
std::swap(*preferred, m); |
| 1358 |
} |
| 1359 |
const unsigned reserved_slots = !!d1_preferred |
| 1360 |
#if DXX_BUILD_DESCENT == 2 |
| 1361 |
+ !!d2_preferred + !!d2x_preferred |
| 1362 |
#endif |
| 1363 |
; |
| 1364 |
auto &hl = PlayerCfg.HighestLevels; |
| 1365 |
const auto irs = &hl[reserved_slots]; |
| 1366 |
|
| 1367 |
uint8_t previous_best_levelnum{0}; |
| 1368 |
if (ii == ie) |
| 1369 |
{ |
| 1370 |
/* |
| 1371 |
* If ii == ie, the mission is unknown. If there is no free |
| 1372 |
* space available, move everything, so that the |
| 1373 |
* least-recently-used element (at *irs) is discarded to make |
| 1374 |
* room to add this mission as most recently used. |
| 1375 |
* |
| 1376 |
* Leave previous_best_levelnum set to 0, so that any progress |
| 1377 |
* at all qualifies for a save. |
| 1378 |
*/ |
| 1379 |
if (ie == PlayerCfg.HighestLevels.end()) |
| 1380 |
{ |
| 1381 |
std::move(std::next(irs), ie, irs); |
| 1382 |
--ii; |
| 1383 |
} |
| 1384 |
else |
| 1385 |
PlayerCfg.NHighestLevels++; |
| 1386 |
ii->Shortname.back() = 0; |
| 1387 |
strncpy(ii->Shortname.data(), &*Current_mission->filename, ii->Shortname.size() - 1); |
| 1388 |
} |
| 1389 |
else if (ii != ie - 1) |
| 1390 |
{ |
| 1391 |
/* If this mission is not the most recently used, reorder the |
| 1392 |
* list so that it becomes the most recently used. |
| 1393 |
* |
| 1394 |
* Leave previous_best_levelnum set to 0, so that a save is |
| 1395 |
* required, even if the player has not set a new record. |
| 1396 |
*/ |
| 1397 |
std::rotate(ii, std::next(ii), ie); |
| 1398 |
ii = ie - 1; |
| 1399 |
} |
| 1400 |
else |
| 1401 |
/* Update previous_best_levelnum so that progress is only saved |
| 1402 |
* if this is a new best. |
| 1403 |
*/ |
| 1404 |
previous_best_levelnum = ii->LevelNum; |
| 1405 |
|
| 1406 |
if (previous_best_levelnum < best_levelnum_this_game) |
| 1407 |
{ |
| 1408 |
auto &best_levelnum_recorded = ii->LevelNum; |
| 1409 |
if (best_levelnum_recorded < best_levelnum_this_game) |
| 1410 |
best_levelnum_recorded = best_levelnum_this_game; |
| 1411 |
write_player_file(); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
//gets the player's highest level from the file for this mission |
| 1416 |
int get_highest_level(void) |
| 1417 |
{ |
| 1418 |
read_player_file(); |
| 1419 |
const Mission_path &mission = *Current_mission; |
| 1420 |
// Destination Saturn. |
| 1421 |
const auto &&r = partial_range(PlayerCfg.HighestLevels, PlayerCfg.NHighestLevels); |
| 1422 |
#if DXX_BUILD_DESCENT == 1 |
| 1423 |
unsigned highest_saturn_level{0}; |
| 1424 |
if (!*mission.filename) |
| 1425 |
range_for (auto &m, r) |
| 1426 |
if (!d_stricmp("DESTSAT", m.Shortname.data())) |
| 1427 |
highest_saturn_level = m.LevelNum; |
| 1428 |
#endif |
| 1429 |
const auto &&i = find_hli_entry(r, mission); |
| 1430 |
const auto ie = i[1]; |
| 1431 |
const auto ii = i[0]; |
| 1432 |
if (ii == ie) |
| 1433 |
return 0; |
| 1434 |
auto LevelNum = ii->LevelNum; |
| 1435 |
#if DXX_BUILD_DESCENT == 1 |
| 1436 |
/* Override `LevelNum` of the full campaign with the player's |
| 1437 |
* progress in the OEM mini-campaign, so that users who switch can |
| 1438 |
* keep their progress. |
| 1439 |
* |
| 1440 |
* If the player never played Destination Saturn, or if this is not |
| 1441 |
* the built-in campaign, highest_saturn_level will be 0 and this |
| 1442 |
* will be skipped. |
| 1443 |
*/ |
| 1444 |
if (LevelNum < highest_saturn_level) |
| 1445 |
LevelNum = highest_saturn_level; |
| 1446 |
#endif |
| 1447 |
return LevelNum; |
| 1448 |
} |
| 1449 |
|
| 1450 |
//write out player's saved games. returns errno (0 == no error) |
| 1451 |
namespace dsx { |
| 1452 |
void write_player_file() |
| 1453 |
{ |
| 1454 |
char filename[PATH_MAX]; |
| 1455 |
int errno_ret; |
| 1456 |
|
| 1457 |
if ( Newdemo_state == ND_STATE_PLAYBACK ) |
| 1458 |
return; |
| 1459 |
|
| 1460 |
errno_ret = WriteConfigFile(CGameCfg, GameCfg); |
| 1461 |
|
| 1462 |
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.plx"), static_cast<const char *>(InterfaceUniqueState.PilotName)); |
| 1463 |
write_player_dxx(filename); |
| 1464 |
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.plr"), static_cast<const char *>(InterfaceUniqueState.PilotName)); |
| 1465 |
auto file = PHYSFSX_openWriteBuffered(filename).first; |
| 1466 |
if (!file) |
| 1467 |
return; |
| 1468 |
|
| 1469 |
//Write out player's info |
| 1470 |
PHYSFS_writeULE32(file, SAVE_FILE_ID); |
| 1471 |
#if DXX_BUILD_DESCENT == 1 |
| 1472 |
PHYSFS_writeULE16(file, SAVED_GAME_VERSION); |
| 1473 |
PHYSFS_writeULE16(file, PLAYER_STRUCT_VERSION); |
| 1474 |
PHYSFS_writeSLE32(file, PlayerCfg.NHighestLevels); |
| 1475 |
PHYSFS_writeSLE32(file, underlying_value(PlayerCfg.DefaultDifficulty)); |
| 1476 |
PHYSFS_writeSLE32(file, PlayerCfg.AutoLeveling); |
| 1477 |
errno_ret = EZERO; |
| 1478 |
|
| 1479 |
//write higest level info |
| 1480 |
if (const auto count{PlayerCfg.NHighestLevels * sizeof(hli)}; PHYSFSX_writeBytes(file, PlayerCfg.HighestLevels, count) != count) |
| 1481 |
{ |
| 1482 |
errno_ret = errno; |
| 1483 |
return; |
| 1484 |
} |
| 1485 |
|
| 1486 |
if (PHYSFSX_writeBytes(file, saved_games, sizeof(saved_games)) != sizeof(saved_games)) |
| 1487 |
{ |
| 1488 |
errno_ret = errno; |
| 1489 |
return; |
| 1490 |
} |
| 1491 |
|
| 1492 |
range_for (auto &i, PlayerCfg.NetworkMessageMacro) |
| 1493 |
if (PHYSFSX_writeBytes(file, i.data(), i.size()) != i.size()) |
| 1494 |
{ |
| 1495 |
errno_ret = errno; |
| 1496 |
return; |
| 1497 |
} |
| 1498 |
|
| 1499 |
//write kconfig info |
| 1500 |
{ |
| 1501 |
if (PHYSFSX_writeBytes(file, PlayerCfg.KeySettings.Keyboard, sizeof(PlayerCfg.KeySettings.Keyboard)) != sizeof(PlayerCfg.KeySettings.Keyboard)) |
| 1502 |
errno_ret=errno; |
| 1503 |
#if DXX_MAX_JOYSTICKS |
| 1504 |
auto &KeySettingsJoystick = PlayerCfg.KeySettings.Joystick; |
| 1505 |
#else |
| 1506 |
const std::array<uint8_t, MAX_CONTROLS> KeySettingsJoystick{}; |
| 1507 |
#endif |
| 1508 |
if (PHYSFSX_writeBytes(file, KeySettingsJoystick, sizeof(KeySettingsJoystick)) != sizeof(KeySettingsJoystick)) |
| 1509 |
errno_ret=errno; |
| 1510 |
for (unsigned i = 0; i < MAX_CONTROLS*3; i++) |
| 1511 |
if (PHYSFSX_writeBytes(file, "0", sizeof(ubyte)) != sizeof(ubyte)) // Skip obsolete Flightstick/Thrustmaster/Gravis map fields |
| 1512 |
errno_ret=errno; |
| 1513 |
if (PHYSFSX_writeBytes(file, PlayerCfg.KeySettings.Mouse, sizeof(PlayerCfg.KeySettings.Mouse)) != sizeof(PlayerCfg.KeySettings.Mouse)) |
| 1514 |
errno_ret=errno; |
| 1515 |
{ |
| 1516 |
std::array<uint8_t, MAX_CONTROLS> cyberman{}; |
| 1517 |
if (PHYSFSX_writeBytes(file, cyberman.data(), cyberman.size()) != cyberman.size()) // Skip obsolete Cyberman map field |
| 1518 |
errno_ret=errno; |
| 1519 |
} |
| 1520 |
|
| 1521 |
if(errno_ret == EZERO) |
| 1522 |
{ |
| 1523 |
ubyte old_avg_joy_sensitivity{8}; |
| 1524 |
if (PHYSFSX_writeBytes(file, &PlayerCfg.ControlType, sizeof(ubyte)) != sizeof(ubyte)) |
| 1525 |
errno_ret=errno; |
| 1526 |
else if (PHYSFSX_writeBytes(file, &old_avg_joy_sensitivity, sizeof(ubyte)) != sizeof(ubyte)) |
| 1527 |
errno_ret=errno; |
| 1528 |
} |
| 1529 |
} |
| 1530 |
|
| 1531 |
if (!file.close()) |
| 1532 |
errno_ret = errno; |
| 1533 |
|
| 1534 |
if (errno_ret != EZERO) { |
| 1535 |
PHYSFS_delete(filename); //delete bogus file |
| 1536 |
nm_messagebox(menu_title{TXT_ERROR}, {TXT_OK}, "%s\n\n%s",TXT_ERROR_WRITING_PLR, strerror(errno_ret)); |
| 1537 |
} |
| 1538 |
#elif DXX_BUILD_DESCENT == 2 |
| 1539 |
(void)errno_ret; |
| 1540 |
PHYSFS_writeULE16(file, PLAYER_FILE_VERSION); |
| 1541 |
|
| 1542 |
|
| 1543 |
PHYSFS_seek(file,PHYSFS_tell(file)+2*(sizeof(PHYSFS_uint16))); // skip Game_window_w, Game_window_h |
| 1544 |
PHYSFSX_writeU8(file, underlying_value(PlayerCfg.DefaultDifficulty)); |
| 1545 |
PHYSFSX_writeU8(file, PlayerCfg.AutoLeveling); |
| 1546 |
PHYSFSX_writeU8(file, PlayerCfg.ReticleType == reticle_type::none ? 0 : 1); |
| 1547 |
PHYSFSX_writeU8(file, underlying_value(PlayerCfg.CockpitMode[0])); |
| 1548 |
PHYSFS_seek(file,PHYSFS_tell(file)+sizeof(PHYSFS_uint8)); // skip Default_display_mode |
| 1549 |
PHYSFSX_writeU8(file, static_cast<uint8_t>(PlayerCfg.MissileViewEnabled)); |
| 1550 |
PHYSFSX_writeU8(file, PlayerCfg.HeadlightActiveDefault); |
| 1551 |
PHYSFSX_writeU8(file, PlayerCfg.GuidedInBigWindow); |
| 1552 |
PHYSFS_seek(file,PHYSFS_tell(file)+sizeof(PHYSFS_uint8)); // skip Automap_always_hires |
| 1553 |
|
| 1554 |
//write higest level info |
| 1555 |
PHYSFS_writeULE16(file, PlayerCfg.NHighestLevels); |
| 1556 |
if (const auto count{sizeof(hli) * PlayerCfg.NHighestLevels}; PHYSFSX_writeBytes(file, PlayerCfg.HighestLevels, count) != count) |
| 1557 |
goto write_player_file_failed; |
| 1558 |
|
| 1559 |
range_for (auto &i, PlayerCfg.NetworkMessageMacro) |
| 1560 |
if (PHYSFSX_writeBytes(file, i.data(), i.size()) != i.size()) |
| 1561 |
goto write_player_file_failed; |
| 1562 |
|
| 1563 |
//write kconfig info |
| 1564 |
{ |
| 1565 |
|
| 1566 |
ubyte old_avg_joy_sensitivity{8}; |
| 1567 |
ubyte control_type_dos = PlayerCfg.ControlType; |
| 1568 |
|
| 1569 |
if (PHYSFSX_writeBytes(file, PlayerCfg.KeySettings.Keyboard, sizeof(PlayerCfg.KeySettings.Keyboard)) != sizeof(PlayerCfg.KeySettings.Keyboard)) |
| 1570 |
goto write_player_file_failed; |
| 1571 |
#if DXX_MAX_JOYSTICKS |
| 1572 |
auto &KeySettingsJoystick = PlayerCfg.KeySettings.Joystick; |
| 1573 |
#else |
| 1574 |
const std::array<uint8_t, MAX_CONTROLS> KeySettingsJoystick{}; |
| 1575 |
#endif |
| 1576 |
if (PHYSFSX_writeBytes(file, KeySettingsJoystick, sizeof(KeySettingsJoystick)) != sizeof(KeySettingsJoystick)) |
| 1577 |
goto write_player_file_failed; |
| 1578 |
for (unsigned i = 0; i < MAX_CONTROLS*3; i++) |
| 1579 |
if (PHYSFSX_writeBytes(file, "0", sizeof(ubyte)) != sizeof(ubyte)) // Skip obsolete Flightstick/Thrustmaster/Gravis map fields |
| 1580 |
goto write_player_file_failed; |
| 1581 |
if (PHYSFSX_writeBytes(file, PlayerCfg.KeySettings.Mouse, sizeof(PlayerCfg.KeySettings.Mouse)) != sizeof(PlayerCfg.KeySettings.Mouse)) |
| 1582 |
goto write_player_file_failed; |
| 1583 |
for (unsigned i = 0; i < MAX_CONTROLS*2; i++) |
| 1584 |
if (PHYSFSX_writeBytes(file, "0", sizeof(ubyte)) != sizeof(ubyte)) // Skip obsolete Cyberman/Winjoy map fields |
| 1585 |
goto write_player_file_failed; |
| 1586 |
if (PHYSFSX_writeBytes(file, &control_type_dos, sizeof(ubyte)) != sizeof(ubyte)) |
| 1587 |
goto write_player_file_failed; |
| 1588 |
ubyte control_type_win{0}; |
| 1589 |
if (PHYSFSX_writeBytes(file, &control_type_win, sizeof(ubyte)) != sizeof(ubyte)) |
| 1590 |
goto write_player_file_failed; |
| 1591 |
if (PHYSFSX_writeBytes(file, &old_avg_joy_sensitivity, sizeof(ubyte)) != sizeof(ubyte)) |
| 1592 |
goto write_player_file_failed; |
| 1593 |
|
| 1594 |
range_for (const unsigned i, xrange(11u)) |
| 1595 |
{ |
| 1596 |
PHYSFSX_writeBytes(file, &PlayerCfg.PrimaryOrder[i], sizeof(ubyte)); |
| 1597 |
PHYSFSX_writeBytes(file, &PlayerCfg.SecondaryOrder[i], sizeof(ubyte)); |
| 1598 |
} |
| 1599 |
|
| 1600 |
PHYSFS_writeULE32(file, static_cast<unsigned>(PlayerCfg.Cockpit3DView[gauge_inset_window_view::primary])); |
| 1601 |
PHYSFS_writeULE32(file, static_cast<unsigned>(PlayerCfg.Cockpit3DView[gauge_inset_window_view::secondary])); |
| 1602 |
|
| 1603 |
PHYSFS_writeULE32(file, PlayerCfg.NetlifeKills); |
| 1604 |
PHYSFS_writeULE32(file, PlayerCfg.NetlifeKilled); |
| 1605 |
int i=get_lifetime_checksum (PlayerCfg.NetlifeKills,PlayerCfg.NetlifeKilled); |
| 1606 |
PHYSFS_writeULE32(file, i); |
| 1607 |
} |
| 1608 |
|
| 1609 |
//write guidebot name |
| 1610 |
PHYSFSX_writeString(file, PlayerCfg.GuidebotNameReal); |
| 1611 |
|
| 1612 |
{ |
| 1613 |
char buf[128]; |
| 1614 |
strcpy(buf, "DOS joystick"); |
| 1615 |
PHYSFSX_writeString(file, buf); // Write out current joystick for player. |
| 1616 |
} |
| 1617 |
|
| 1618 |
if (!file.close()) |
| 1619 |
goto write_player_file_failed; |
| 1620 |
|
| 1621 |
return; |
| 1622 |
|
| 1623 |
write_player_file_failed: |
| 1624 |
nm_messagebox(menu_title{TXT_ERROR}, {TXT_OK}, "%s\n\n%s", TXT_ERROR_WRITING_PLR, PHYSFS_getLastError()); |
| 1625 |
if (file) |
| 1626 |
{ |
| 1627 |
file.reset(); |
| 1628 |
PHYSFS_delete(filename); //delete bogus file |
| 1629 |
} |
| 1630 |
#endif |
| 1631 |
} |
| 1632 |
|
| 1633 |
namespace { |
| 1634 |
#if DXX_BUILD_DESCENT == 2 |
| 1635 |
static int get_lifetime_checksum (int a,int b) |
| 1636 |
{ |
| 1637 |
int num; |
| 1638 |
|
| 1639 |
// confusing enough to beat amateur disassemblers? Lets hope so |
| 1640 |
|
| 1641 |
num=(a<<8 ^ b); |
| 1642 |
num^=(a | b); |
| 1643 |
num*=num>>2; |
| 1644 |
return (num); |
| 1645 |
} |
| 1646 |
#endif |
| 1647 |
|
| 1648 |
template <uint_fast32_t shift, uint_fast32_t width> |
| 1649 |
static void convert_duplicate_powerup_integer(packed_netduplicate_items &d, auto &&value) |
| 1650 |
{ |
| 1651 |
if (const auto r{convert_integer<unsigned>(std::move(value))}; !r) |
| 1652 |
return; |
| 1653 |
else if (const auto i = *r; !(i & ~((1 << width) - 1))) |
| 1654 |
d.set_sub_field<shift, width>(i); |
| 1655 |
} |
| 1656 |
} |
| 1657 |
|
| 1658 |
#if DXX_USE_MULTIPLAYER |
| 1659 |
// read stored values from ngp file to netgame_info |
| 1660 |
void read_netgame_profile(netgame_info *ng) |
| 1661 |
{ |
| 1662 |
char filename[PATH_MAX]; |
| 1663 |
#if DXX_USE_TRACKER |
| 1664 |
ng->TrackerNATWarned = TrackerNATHolePunchWarn::Unset; |
| 1665 |
#endif |
| 1666 |
|
| 1667 |
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.ngp"), static_cast<const char *>(InterfaceUniqueState.PilotName)); |
| 1668 |
auto file = PHYSFSX_openReadBuffered(filename).first; |
| 1669 |
if (!file) |
| 1670 |
return; |
| 1671 |
|
| 1672 |
ng->MPGameplayOptions.AutosaveInterval = std::chrono::minutes(10); |
| 1673 |
// NOTE that we do not set any defaults here or even initialize netgame_info. For flexibility, leave that to the function calling this. |
| 1674 |
for (PHYSFSX_gets_line_t<50> line; const auto rc{PHYSFSX_fgets(line, file)};) |
| 1675 |
{ |
| 1676 |
const auto eol{rc.end()}; |
| 1677 |
const auto lb{rc.begin()}; |
| 1678 |
const auto eq{std::ranges::find(lb, eol, '=')}; |
| 1679 |
if (eq == eol) |
| 1680 |
continue; |
| 1681 |
const auto value{std::next(eq)}; |
| 1682 |
if (const std::ranges::subrange name{lb, eq}; compare_nonterminated_name(name, GameNameStr)) |
| 1683 |
convert_string(ng->game_name, value, eol); |
| 1684 |
else if (compare_nonterminated_name(name, GameModeStr)) |
| 1685 |
{ |
| 1686 |
if (auto gamemode = convert_integer<uint8_t>(value)) |
| 1687 |
ng->gamemode = network_game_type{*gamemode}; |
| 1688 |
} |
| 1689 |
else if (compare_nonterminated_name(name, RefusePlayersStr)) |
| 1690 |
convert_integer(ng->RefusePlayers, value); |
| 1691 |
else if (compare_nonterminated_name(name, DifficultyStr)) |
| 1692 |
{ |
| 1693 |
if (auto difficulty = convert_integer<uint8_t>(value)) |
| 1694 |
ng->difficulty = cast_clamp_difficulty(*difficulty); |
| 1695 |
} |
| 1696 |
else if (compare_nonterminated_name(name, GameFlagsStr)) |
| 1697 |
{ |
| 1698 |
if (auto r = convert_integer<uint8_t>(value)) |
| 1699 |
ng->game_flag = netgame_rule_flags{*r}; |
| 1700 |
} |
| 1701 |
else if (compare_nonterminated_name(name, AllowedItemsStr)) |
| 1702 |
{ |
| 1703 |
if (auto r = convert_integer<std::underlying_type<netflag_flag>::type>(value)) |
| 1704 |
ng->AllowedItems = netflag_flag{*r}; |
| 1705 |
} |
| 1706 |
else if (compare_nonterminated_name(name, SpawnGrantedItemsStr)) |
| 1707 |
{ |
| 1708 |
if (auto r = convert_integer<std::underlying_type<netgrant_flag>::type>(value)) |
| 1709 |
ng->SpawnGrantedItems.mask = netgrant_flag{*r}; |
| 1710 |
} |
| 1711 |
else if (compare_nonterminated_name(name, DuplicatePrimariesStr)) |
| 1712 |
convert_duplicate_powerup_integer<packed_netduplicate_items::primary_shift, packed_netduplicate_items::primary_width>(ng->DuplicatePowerups, value); |
| 1713 |
else if (compare_nonterminated_name(name, DuplicateSecondariesStr)) |
| 1714 |
convert_duplicate_powerup_integer<packed_netduplicate_items::secondary_shift, packed_netduplicate_items::secondary_width>(ng->DuplicatePowerups, value); |
| 1715 |
#if DXX_BUILD_DESCENT == 2 |
| 1716 |
else if (compare_nonterminated_name(name, DuplicateAccessoriesStr)) |
| 1717 |
convert_duplicate_powerup_integer<packed_netduplicate_items::accessory_shift, packed_netduplicate_items::accessory_width>(ng->DuplicatePowerups, value); |
| 1718 |
else if (compare_nonterminated_name(name, AllowMarkerViewStr)) |
| 1719 |
convert_integer(ng->Allow_marker_view, value); |
| 1720 |
else if (compare_nonterminated_name(name, AlwaysLightingStr)) |
| 1721 |
convert_integer(ng->AlwaysLighting, value); |
| 1722 |
else if (compare_nonterminated_name(name, ThiefAbsenceFlagStr)) |
| 1723 |
{ |
| 1724 |
if (const auto o{convert_integer<uint8_t>(value)}; o && *o) |
| 1725 |
ng->ThiefModifierFlags |= ThiefModifier::Absent; |
| 1726 |
} |
| 1727 |
else if (compare_nonterminated_name(name, ThiefNoEnergyWeaponsFlagStr)) |
| 1728 |
{ |
| 1729 |
if (const auto o{convert_integer<uint8_t>(value)}; o && *o) |
| 1730 |
ng->ThiefModifierFlags |= ThiefModifier::NoEnergyWeapons; |
| 1731 |
} |
| 1732 |
else if (compare_nonterminated_name(name, AllowGuidebotStr)) |
| 1733 |
convert_integer(ng->AllowGuidebot, value); |
| 1734 |
#endif |
| 1735 |
else if (compare_nonterminated_name(name, ShufflePowerupsStr)) |
| 1736 |
convert_integer(ng->ShufflePowerupSeed, value); |
| 1737 |
else if (compare_nonterminated_name(name, ShowEnemyNamesStr)) |
| 1738 |
convert_integer(ng->ShowEnemyNames, value); |
| 1739 |
else if (compare_nonterminated_name(name, BrightPlayersStr)) |
| 1740 |
convert_integer(ng->BrightPlayers, value); |
| 1741 |
else if (compare_nonterminated_name(name, InvulAppearStr)) |
| 1742 |
convert_integer(ng->InvulAppear, value); |
| 1743 |
else if (compare_nonterminated_name(name, KillGoalStr)) |
| 1744 |
convert_integer(ng->KillGoal, value); |
| 1745 |
else if (compare_nonterminated_name(name, PlayTimeAllowedStr)) |
| 1746 |
{ |
| 1747 |
if (const auto r = convert_integer<int>(value)) |
| 1748 |
ng->PlayTimeAllowed = std::chrono::duration<int, netgame_info::play_time_allowed_abi_ratio>(*r); |
| 1749 |
} |
| 1750 |
else if (compare_nonterminated_name(name, ControlInvulTimeStr)) |
| 1751 |
convert_integer(ng->control_invul_time, value); |
| 1752 |
else if (compare_nonterminated_name(name, PacketsPerSecStr)) |
| 1753 |
convert_integer(ng->PacketsPerSec, value); |
| 1754 |
else if (compare_nonterminated_name(name, NoFriendlyFireStr)) |
| 1755 |
convert_integer(ng->NoFriendlyFire, value); |
| 1756 |
else if (compare_nonterminated_name(name, MouselookFlagsStr)) |
| 1757 |
convert_integer(ng->MouselookFlags, value); |
| 1758 |
else if (compare_nonterminated_name(name, PitchLockFlagsStr)) |
| 1759 |
convert_integer(ng->PitchLockFlags, value); |
| 1760 |
else if (compare_nonterminated_name(name, AutosaveIntervalStr)) |
| 1761 |
{ |
| 1762 |
if (const auto r = convert_integer<uint16_t>(value)) |
| 1763 |
ng->MPGameplayOptions.AutosaveInterval = std::chrono::seconds(*r); |
| 1764 |
} |
| 1765 |
#if DXX_USE_TRACKER |
| 1766 |
else if (compare_nonterminated_name(name, TrackerStr)) |
| 1767 |
convert_integer(ng->Tracker, value); |
| 1768 |
else if (compare_nonterminated_name(name, TrackerNATHPStr)) |
| 1769 |
{ |
| 1770 |
if (const auto r{convert_integer<uint8_t>(value)}) |
| 1771 |
ng->TrackerNATWarned = TrackerNATHolePunchWarn{*r}; |
| 1772 |
} |
| 1773 |
#endif |
| 1774 |
} |
| 1775 |
} |
| 1776 |
|
| 1777 |
// write values from netgame_info to ngp file |
| 1778 |
void write_netgame_profile(const netgame_info *ng) |
| 1779 |
{ |
| 1780 |
char filename[PATH_MAX]; |
| 1781 |
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.ngp"), static_cast<const char *>(InterfaceUniqueState.PilotName)); |
| 1782 |
auto file = PHYSFSX_openWriteBuffered(filename).first; |
| 1783 |
if (!file) |
| 1784 |
return; |
| 1785 |
|
| 1786 |
PHYSFSX_printf(file, GameNameStr "=%s\n", ng->game_name.data()); |
| 1787 |
PHYSFSX_printf(file, GameModeStr "=%i\n", underlying_value(ng->gamemode)); |
| 1788 |
PHYSFSX_printf(file, RefusePlayersStr "=%i\n", ng->RefusePlayers); |
| 1789 |
PHYSFSX_printf(file, DifficultyStr "=%i\n", underlying_value(ng->difficulty)); |
| 1790 |
PHYSFSX_printf(file, GameFlagsStr "=%i\n", underlying_value(ng->game_flag)); |
| 1791 |
PHYSFSX_printf(file, AllowedItemsStr "=%i\n", underlying_value(ng->AllowedItems)); |
| 1792 |
PHYSFSX_printf(file, SpawnGrantedItemsStr "=%i\n", underlying_value(ng->SpawnGrantedItems.mask)); |
| 1793 |
PHYSFSX_printf(file, DuplicatePrimariesStr "=%" PRIuFAST32 "\n", ng->DuplicatePowerups.get_primary_count()); |
| 1794 |
PHYSFSX_printf(file, DuplicateSecondariesStr "=%" PRIuFAST32 "\n", ng->DuplicatePowerups.get_secondary_count()); |
| 1795 |
#if DXX_BUILD_DESCENT == 2 |
| 1796 |
PHYSFSX_printf(file, DuplicateAccessoriesStr "=%" PRIuFAST32 "\n", ng->DuplicatePowerups.get_accessory_count()); |
| 1797 |
PHYSFSX_printf(file, AllowMarkerViewStr "=%i\n", ng->Allow_marker_view); |
| 1798 |
PHYSFSX_printf(file, AlwaysLightingStr "=%i\n", ng->AlwaysLighting); |
| 1799 |
PHYSFSX_printf(file, ThiefAbsenceFlagStr "=%i\n", ng->ThiefModifierFlags & ThiefModifier::Absent); |
| 1800 |
PHYSFSX_printf(file, ThiefNoEnergyWeaponsFlagStr "=%i\n", ng->ThiefModifierFlags & ThiefModifier::NoEnergyWeapons); |
| 1801 |
PHYSFSX_printf(file, AllowGuidebotStr "=%i\n", ng->AllowGuidebot); |
| 1802 |
#endif |
| 1803 |
PHYSFSX_printf(file, ShufflePowerupsStr "=%i\n", !!ng->ShufflePowerupSeed); |
| 1804 |
PHYSFSX_printf(file, ShowEnemyNamesStr "=%i\n", ng->ShowEnemyNames); |
| 1805 |
PHYSFSX_printf(file, BrightPlayersStr "=%i\n", ng->BrightPlayers); |
| 1806 |
PHYSFSX_printf(file, InvulAppearStr "=%i\n", ng->InvulAppear); |
| 1807 |
PHYSFSX_printf(file, KillGoalStr "=%i\n", ng->KillGoal); |
| 1808 |
PHYSFSX_printf(file, PlayTimeAllowedStr "=%i\n", std::chrono::duration_cast<std::chrono::duration<int, netgame_info::play_time_allowed_abi_ratio>>(ng->PlayTimeAllowed).count()); |
| 1809 |
PHYSFSX_printf(file, ControlInvulTimeStr "=%i\n", ng->control_invul_time); |
| 1810 |
PHYSFSX_printf(file, PacketsPerSecStr "=%i\n", ng->PacketsPerSec); |
| 1811 |
PHYSFSX_printf(file, NoFriendlyFireStr "=%i\n", ng->NoFriendlyFire); |
| 1812 |
PHYSFSX_printf(file, MouselookFlagsStr "=%i\n", ng->MouselookFlags); |
| 1813 |
PHYSFSX_printf(file, PitchLockFlagsStr "=%i\n", ng->PitchLockFlags); |
| 1814 |
PHYSFSX_printf(file, AutosaveIntervalStr "=%i\n", ng->MPGameplayOptions.AutosaveInterval.count()); |
| 1815 |
#if DXX_USE_TRACKER |
| 1816 |
PHYSFSX_printf(file, TrackerStr "=%i\n", ng->Tracker); |
| 1817 |
PHYSFSX_printf(file, TrackerNATHPStr "=%i\n", ng->TrackerNATWarned); |
| 1818 |
#else |
| 1819 |
PHYSFSX_puts_literal(file, TrackerStr "=0\n" TrackerNATHPStr "=0\n"); |
| 1820 |
#endif |
| 1821 |
PHYSFSX_puts_literal(file, NGPVersionStr "=" DXX_VERSION_STR "\n"); |
| 1822 |
} |
| 1823 |
#endif |
| 1824 |
|
| 1825 |
} |