Subversion Repositories eduke32

Rev

Rev 8386 | Rev 8536 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 Plagman 1
//-------------------------------------------------------------------------
2
/*
5612 terminx 3
Copyright (C) 2016 EDuke32 developers and contributors
5 Plagman 4
 
1652 terminx 5
This file is part of EDuke32.
5 Plagman 6
 
7
EDuke32 is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License version 2
9
as published by the Free Software Foundation.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 
15
See the GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
4541 hendricks2 19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
5 Plagman 20
*/
21
//-------------------------------------------------------------------------
22
 
23
#include "duke3d.h"
24
#include "scriplib.h"
820 terminx 25
#include "osdcmds.h"
5621 terminx 26
#include "renderlayer.h"
6064 hendricks2 27
#include "cmdline.h"
5 Plagman 28
 
7359 hendricks2 29
#include "vfs.h"
30
 
4440 terminx 31
#ifdef __ANDROID__
6926 terminx 32
# include "android.h"
4440 terminx 33
#endif
34
 
6926 terminx 35
#if defined RENDERTYPESDL && defined SDL_TARGET && SDL_TARGET > 1
36
# include "sdl_inc.h"
37
#endif
38
 
5 Plagman 39
// we load this in to get default button and key assignments
40
// as well as setting up function mappings
41
 
42
#define __SETUP__   // JBF 20031211
43
#include "_functio.h"
44
 
1625 terminx 45
hashtable_t h_gamefuncs    = { NUMGAMEFUNCTIONS<<1, NULL };
1026 terminx 46
 
2796 helixhorne 47
int32_t CONFIG_FunctionNameToNum(const char *func)
5 Plagman 48
{
4827 hendricks2 49
    if (!func)
50
        return -1;
51
 
7366 terminx 52
    return hash_findcase(&h_gamefuncs, func);
5 Plagman 53
}
54
 
55
 
7943 hendricks2 56
static char const * CONFIG_FunctionNumToName(int32_t func)
5 Plagman 57
{
58
    if ((unsigned)func >= (unsigned)NUMGAMEFUNCTIONS)
7943 hendricks2 59
        return "";
437 terminx 60
    return gamefunctions[func];
5 Plagman 61
}
62
 
63
 
2796 helixhorne 64
int32_t CONFIG_AnalogNameToNum(const char *func)
5 Plagman 65
{
4827 hendricks2 66
    if (!func)
67
        return -1;
7367 terminx 68
    else if (!Bstrcasecmp(func, "analog_turning"))
5 Plagman 69
        return analog_turning;
7367 terminx 70
    else if (!Bstrcasecmp(func, "analog_strafing"))
5 Plagman 71
        return analog_strafing;
7367 terminx 72
    else if (!Bstrcasecmp(func, "analog_moving"))
5 Plagman 73
        return analog_moving;
7367 terminx 74
    else if (!Bstrcasecmp(func, "analog_lookingupanddown"))
5 Plagman 75
        return analog_lookingupanddown;
7367 terminx 76
    else
77
        return -1;
5 Plagman 78
}
79
 
80
 
7943 hendricks2 81
static char const * CONFIG_AnalogNumToName(int32_t func)
5 Plagman 82
{
335 terminx 83
    switch (func)
84
    {
337 terminx 85
    case analog_turning:
86
        return "analog_turning";
87
    case analog_strafing:
88
        return "analog_strafing";
89
    case analog_moving:
90
        return "analog_moving";
91
    case analog_lookingupanddown:
92
        return "analog_lookingupanddown";
5 Plagman 93
    }
94
 
7943 hendricks2 95
    return "";
5 Plagman 96
}
97
 
98
 
7968 hendricks2 99
static void CONFIG_SetJoystickButtonFunction(int i, int j, int function)
100
{
101
    ud.config.JoystickFunctions[i][j] = function;
102
    CONTROL_MapButton(function, i, j, controldevice_joystick);
103
}
104
static void CONFIG_SetJoystickAnalogAxisScale(int i, int scale)
105
{
106
    ud.config.JoystickAnalogueScale[i] = scale;
107
    CONTROL_SetAnalogAxisScale(i, scale, controldevice_joystick);
108
}
7986 hendricks2 109
static void CONFIG_SetJoystickAnalogAxisInvert(int i, int invert)
110
{
111
    ud.config.JoystickAnalogueInvert[i] = invert;
112
    CONTROL_SetAnalogAxisInvert(i, invert, controldevice_joystick);
113
}
7968 hendricks2 114
static void CONFIG_SetJoystickAnalogAxisDeadSaturate(int i, int dead, int saturate)
115
{
116
    ud.config.JoystickAnalogueDead[i] = dead;
117
    ud.config.JoystickAnalogueSaturate[i] = saturate;
118
    joySetDeadZone(i, dead, saturate);
119
}
120
static void CONFIG_SetJoystickDigitalAxisFunction(int i, int j, int function)
121
{
122
    ud.config.JoystickDigitalFunctions[i][j] = function;
123
    CONTROL_MapDigitalAxis(i, function, j, controldevice_joystick);
124
}
125
static void CONFIG_SetJoystickAnalogAxisFunction(int i, int function)
126
{
127
    ud.config.JoystickAnalogueAxes[i] = function;
128
    CONTROL_MapAnalogAxis(i, function, controldevice_joystick);
129
}
130
 
131
 
7212 terminx 132
void CONFIG_SetDefaultKeys(const char (*keyptr)[MAXGAMEFUNCLEN], bool lazy/*=false*/)
495 terminx 133
{
7317 terminx 134
    static char const s_gamefunc_[] = "gamefunc_";
135
    int constexpr strlen_gamefunc_  = ARRAY_SIZE(s_gamefunc_) - 1;
136
 
7212 terminx 137
    if (!lazy)
138
    {
139
        Bmemset(ud.config.KeyboardKeys, 0xff, sizeof(ud.config.KeyboardKeys));
140
        CONTROL_ClearAllBinds();
141
    }
3127 helixhorne 142
 
7212 terminx 143
    for (int i=0; i < ARRAY_SSIZE(gamefunctions); ++i)
495 terminx 144
    {
6185 hendricks2 145
        if (gamefunctions[i][0] == '\0')
146
            continue;
147
 
7212 terminx 148
        auto &key = ud.config.KeyboardKeys[i];
495 terminx 149
 
7212 terminx 150
        int const default0 = KB_StringToScanCode(keyptr[i<<1]);
151
        int const default1 = KB_StringToScanCode(keyptr[(i<<1)+1]);
152
 
153
        // skip the function if the default key is already used
154
        // or the function is assigned to another key
7317 terminx 155
        if (lazy && (key[0] != 0xff || (CONTROL_KeyIsBound(default0) && Bstrlen(CONTROL_KeyBinds[default0].cmdstr) > strlen_gamefunc_
156
                        && CONFIG_FunctionNameToNum(CONTROL_KeyBinds[default0].cmdstr + strlen_gamefunc_) >= 0)))
157
        {
158
#if 0 // defined(DEBUGGINGAIDS)
159
            if (key[0] != 0xff)
160
                initprintf("Skipping %s bound to %s\n", keyptr[i<<1], CONTROL_KeyBinds[default0].cmdstr);
161
#endif
7212 terminx 162
            continue;
7317 terminx 163
        }
7212 terminx 164
 
165
        key[0] = default0;
166
        key[1] = default1;
167
 
168
        if (key[0])
169
            CONTROL_FreeKeyBind(key[0]);
170
 
171
        if (key[1])
172
            CONTROL_FreeKeyBind(key[1]);
173
 
6191 hendricks2 174
        if (i == gamefunc_Show_Console)
7212 terminx 175
            OSD_CaptureKey(key[0]);
6191 hendricks2 176
        else
7212 terminx 177
            CONFIG_MapKey(i, key[0], 0, key[1], 0);
495 terminx 178
    }
179
}
180
 
6704 hendricks2 181
 
333 terminx 182
void CONFIG_SetDefaults(void)
5 Plagman 183
{
7181 terminx 184
    ud.config.scripthandle = -1;
5 Plagman 185
 
4386 terminx 186
#ifdef __ANDROID__
4993 terminx 187
    droidinput.forward_sens = 5.f;
7180 terminx 188
    droidinput.gameControlsAlpha = 0.5;
189
    droidinput.hideStick = 0;
190
    droidinput.pitch_sens = 5.f;
191
    droidinput.quickSelectWeapon = 1;
4993 terminx 192
    droidinput.strafe_sens = 5.f;
7180 terminx 193
    droidinput.toggleCrouch = 1;
4993 terminx 194
    droidinput.yaw_sens = 5.f;
195
 
7219 terminx 196
    ud.setup.xdim = droidinfo.screen_width;
197
    ud.setup.ydim = droidinfo.screen_height;
4386 terminx 198
#else
6923 terminx 199
# if defined RENDERTYPESDL && SDL_MAJOR_VERSION > 1
6704 hendricks2 200
    uint32_t inited = SDL_WasInit(SDL_INIT_VIDEO);
201
    if (inited == 0)
202
        SDL_Init(SDL_INIT_VIDEO);
203
    else if (!(inited & SDL_INIT_VIDEO))
204
        SDL_InitSubSystem(SDL_INIT_VIDEO);
205
 
206
    SDL_DisplayMode dm;
207
    if (SDL_GetDesktopDisplayMode(0, &dm) == 0)
208
    {
7219 terminx 209
        ud.setup.xdim = dm.w;
210
        ud.setup.ydim = dm.h;
6704 hendricks2 211
    }
212
    else
213
# endif
214
    {
7219 terminx 215
        ud.setup.xdim = 1024;
216
        ud.setup.ydim = 768;
6704 hendricks2 217
    }
4386 terminx 218
#endif
219
 
1820 terminx 220
#ifdef USE_OPENGL
7219 terminx 221
    ud.setup.bpp = 32;
622 terminx 222
#else
7219 terminx 223
    ud.setup.bpp = 8;
622 terminx 224
#endif
7180 terminx 225
 
821 terminx 226
#if defined(_WIN32)
563 terminx 227
    ud.config.MixRate = 44100;
4436 terminx 228
#elif defined __ANDROID__
229
    ud.config.MixRate = droidinfo.audio_sample_rate;
821 terminx 230
#else
231
    ud.config.MixRate = 48000;
232
#endif
7180 terminx 233
 
6380 terminx 234
#if defined GEKKO || defined __OPENDINGUX__
563 terminx 235
    ud.config.NumVoices = 32;
7181 terminx 236
    ud.camera_time = 11;
6380 terminx 237
#else
6934 terminx 238
    ud.config.NumVoices = 64;
7180 terminx 239
    ud.camera_time    = 4;
4445 terminx 240
#endif
241
 
7942 hendricks2 242
    // currently settings.cfg is only read after the startup window launches the game,
243
    // and rereading binds might be fickle so just enable this
7219 terminx 244
    ud.setup.usejoystick = 1;
131 terminx 245
 
7180 terminx 246
    g_myAimMode = 1;
247
    g_player[0].ps->aim_mode = 1;
248
 
7219 terminx 249
    ud.setup.forcesetup       = 1;
250
    ud.setup.noautoload       = 1;
251
    ud.setup.fullscreen       = 1;
252
    ud.setup.usemouse         = 1;
253
 
7180 terminx 254
    ud.althud                 = 1;
255
    ud.angleinterpolation     = 0;
256
    ud.auto_run               = 1;
257
    ud.automsg                = 0;
258
    ud.autosave               = 1;
259
    ud.autosavedeletion       = 1;
260
    ud.autovote               = 0;
261
    ud.brightness             = 8;
262
    ud.camerasprite           = -1;
263
    ud.color                  = 0;
264
    ud.config.AmbienceToggle  = 1;
265
    ud.config.AutoAim         = 1;
1245 terminx 266
    ud.config.CheckForUpdates = 1;
7180 terminx 267
    ud.config.FXVolume        = 255;
268
    ud.config.MouseBias       = 0;
269
    ud.config.MouseDeadZone   = 0;
8216 terminx 270
    ud.config.MusicDevice     = ASS_AutoDetect;
7180 terminx 271
    ud.config.MusicToggle     = 1;
272
    ud.config.MusicVolume     = 195;
273
    ud.config.NumBits         = 16;
274
    ud.config.NumChannels     = 2;
8480 hendricks2 275
#ifdef ASS_REVERSESTEREO
276
    ud.config.ReverseStereo   = 0;
277
#endif
7180 terminx 278
    ud.config.ShowWeapons     = 0;
279
    ud.config.SmoothInput     = 1;
280
    ud.config.SoundToggle     = 1;
281
    ud.config.VoiceToggle     = 5;  // bitfield, 1 = local, 2 = dummy, 4 = other players in DM
282
    ud.config.useprecache     = 1;
283
    ud.configversion          = 0;
284
    ud.crosshair              = 1;
285
    ud.crosshairscale         = 50;
286
    ud.default_skill          = 1;
287
    ud.democams               = 1;
288
    ud.detail                 = 0;
289
    ud.display_bonus_screen   = 1;
290
    ud.drawweapon             = 1;
7329 terminx 291
    ud.fov                    = 90;
7180 terminx 292
    ud.hudontop               = 0;
293
    ud.idplayers              = 1;
294
    ud.levelstats             = 0;
295
    ud.lockout                = 0;
296
    ud.m_marker               = 1;
297
    ud.maxautosaves           = 5;
298
    ud.menu_scrollbartilenum  = -1;
299
    ud.menu_scrollbarz        = 65536;
300
    ud.menu_scrollcursorz     = 65536;
301
    ud.menu_slidebarmargin    = 65536;
302
    ud.menu_slidebarz         = 65536;
303
    ud.menu_slidecursorz      = 65536;
304
    ud.menubackground         = 1;
305
    ud.mouseaiming            = 0;
306
    ud.mouseflip              = 1;
307
    ud.msgdisptime            = 120;
308
    ud.obituaries             = 1;
309
    ud.pwlockout[0]           = '\0';
310
    ud.runkey_mode            = 0;
311
    ud.screen_size            = 4;
312
    ud.screen_tilting         = 1;
313
    ud.screenfade             = 1;
314
    ud.shadow_pal             = 4;
315
    ud.shadows                = 1;
316
    ud.show_level_text        = 1;
317
    ud.slidebar_paldisabled   = 1;
318
    ud.statusbarflags         = STATUSBAR_NOSHRINK;
319
    ud.statusbarmode          = 1;
320
    ud.statusbarscale         = 100;
321
    ud.team                   = 0;
322
    ud.textscale              = 200;
323
    ud.viewbob                = 1;
324
    ud.weaponscale            = 100;
325
    ud.weaponsway             = 1;
326
    ud.weaponswitch           = 3;  // new+empty
1245 terminx 327
 
6668 hendricks2 328
    Bstrcpy(ud.rtsname, G_DefaultRtsFile());
5 Plagman 329
 
6273 terminx 330
    Bstrcpy(szPlayerName, "Player");
331
 
332
#ifndef EDUKE32_STANDALONE
5 Plagman 333
    Bstrcpy(ud.ridecule[0], "An inspiration for birth control.");
334
    Bstrcpy(ud.ridecule[1], "You're gonna die for that!");
335
    Bstrcpy(ud.ridecule[2], "It hurts to be you.");
4472 hendricks2 336
    Bstrcpy(ud.ridecule[3], "Lucky son of a bitch.");
337
    Bstrcpy(ud.ridecule[4], "Hmmm... payback time.");
5 Plagman 338
    Bstrcpy(ud.ridecule[5], "You bottom dwelling scum sucker.");
339
    Bstrcpy(ud.ridecule[6], "Damn, you're ugly.");
4472 hendricks2 340
    Bstrcpy(ud.ridecule[7], "Ha ha ha... wasted!");
5 Plagman 341
    Bstrcpy(ud.ridecule[8], "You suck!");
342
    Bstrcpy(ud.ridecule[9], "AARRRGHHHHH!!!");
6273 terminx 343
#endif
5 Plagman 344
 
6184 hendricks2 345
    CONFIG_SetDefaultKeys(keydefaults);
506 terminx 346
 
563 terminx 347
    memset(ud.config.MouseFunctions, -1, sizeof(ud.config.MouseFunctions));
7180 terminx 348
    memset(ud.config.MouseDigitalFunctions, -1, sizeof(ud.config.MouseDigitalFunctions));
349
    memset(ud.config.JoystickFunctions, -1, sizeof(ud.config.JoystickFunctions));
350
    memset(ud.config.JoystickDigitalFunctions, -1, sizeof(ud.config.JoystickDigitalFunctions));
351
 
352
    CONTROL_MouseSensitivity = DEFAULTMOUSESENSITIVITY;
353
 
7181 terminx 354
    for (int i=0; i<MAXMOUSEBUTTONS; i++)
335 terminx 355
    {
563 terminx 356
        ud.config.MouseFunctions[i][0] = CONFIG_FunctionNameToNum(mousedefaults[i]);
357
        CONTROL_MapButton(ud.config.MouseFunctions[i][0], i, 0, controldevice_mouse);
114 terminx 358
        if (i>=4) continue;
563 terminx 359
        ud.config.MouseFunctions[i][1] = CONFIG_FunctionNameToNum(mouseclickeddefaults[i]);
360
        CONTROL_MapButton(ud.config.MouseFunctions[i][1], i, 1, controldevice_mouse);
113 terminx 361
    }
5 Plagman 362
 
7181 terminx 363
    for (int i=0; i<MAXMOUSEAXES; i++)
335 terminx 364
    {
2728 hendricks2 365
        ud.config.MouseAnalogueScale[i] = DEFAULTMOUSEANALOGUESCALE;
563 terminx 366
        CONTROL_SetAnalogAxisScale(i, ud.config.MouseAnalogueScale[i], controldevice_mouse);
5 Plagman 367
 
563 terminx 368
        ud.config.MouseDigitalFunctions[i][0] = CONFIG_FunctionNameToNum(mousedigitaldefaults[i*2]);
369
        ud.config.MouseDigitalFunctions[i][1] = CONFIG_FunctionNameToNum(mousedigitaldefaults[i*2+1]);
370
        CONTROL_MapDigitalAxis(i, ud.config.MouseDigitalFunctions[i][0], 0, controldevice_mouse);
371
        CONTROL_MapDigitalAxis(i, ud.config.MouseDigitalFunctions[i][1], 1, controldevice_mouse);
5 Plagman 372
 
563 terminx 373
        ud.config.MouseAnalogueAxes[i] = CONFIG_AnalogNameToNum(mouseanalogdefaults[i]);
374
        CONTROL_MapAnalogAxis(i, ud.config.MouseAnalogueAxes[i], controldevice_mouse);
113 terminx 375
    }
5 Plagman 376
 
7968 hendricks2 377
#if !defined GEKKO
378
    CONFIG_SetGameControllerDefaultsStandard();
379
#else
7181 terminx 380
    for (int i=0; i<MAXJOYBUTTONSANDHATS; i++)
335 terminx 381
    {
563 terminx 382
        ud.config.JoystickFunctions[i][0] = CONFIG_FunctionNameToNum(joystickdefaults[i]);
383
        ud.config.JoystickFunctions[i][1] = CONFIG_FunctionNameToNum(joystickclickeddefaults[i]);
384
        CONTROL_MapButton(ud.config.JoystickFunctions[i][0], i, 0, controldevice_joystick);
385
        CONTROL_MapButton(ud.config.JoystickFunctions[i][1], i, 1, controldevice_joystick);
113 terminx 386
    }
5 Plagman 387
 
7181 terminx 388
    for (int i=0; i<MAXJOYAXES; i++)
335 terminx 389
    {
2728 hendricks2 390
        ud.config.JoystickAnalogueScale[i] = DEFAULTJOYSTICKANALOGUESCALE;
7986 hendricks2 391
        ud.config.JoystickAnalogueInvert[i] = 0;
2728 hendricks2 392
        ud.config.JoystickAnalogueDead[i] = DEFAULTJOYSTICKANALOGUEDEAD;
393
        ud.config.JoystickAnalogueSaturate[i] = DEFAULTJOYSTICKANALOGUESATURATE;
563 terminx 394
        CONTROL_SetAnalogAxisScale(i, ud.config.JoystickAnalogueScale[i], controldevice_joystick);
7986 hendricks2 395
        CONTROL_SetAnalogAxisInvert(i, 0, controldevice_joystick);
5 Plagman 396
 
563 terminx 397
        ud.config.JoystickDigitalFunctions[i][0] = CONFIG_FunctionNameToNum(joystickdigitaldefaults[i*2]);
398
        ud.config.JoystickDigitalFunctions[i][1] = CONFIG_FunctionNameToNum(joystickdigitaldefaults[i*2+1]);
399
        CONTROL_MapDigitalAxis(i, ud.config.JoystickDigitalFunctions[i][0], 0, controldevice_joystick);
400
        CONTROL_MapDigitalAxis(i, ud.config.JoystickDigitalFunctions[i][1], 1, controldevice_joystick);
112 terminx 401
 
563 terminx 402
        ud.config.JoystickAnalogueAxes[i] = CONFIG_AnalogNameToNum(joystickanalogdefaults[i]);
403
        CONTROL_MapAnalogAxis(i, ud.config.JoystickAnalogueAxes[i], controldevice_joystick);
113 terminx 404
    }
7968 hendricks2 405
#endif
6554 hendricks2 406
 
407
    VM_OnEvent(EVENT_SETDEFAULTS, g_player[myconnectindex].ps->i, myconnectindex);
5 Plagman 408
}
409
 
410
 
8303 hendricks2 411
// generates key bindings to reflect changes to keyboard setup
7180 terminx 412
void CONFIG_MapKey(int which, kb_scancode key1, kb_scancode oldkey1, kb_scancode key2, kb_scancode oldkey2)
822 terminx 413
{
7180 terminx 414
    int const keys[] = { key1, key2, oldkey1, oldkey2 };
3209 helixhorne 415
    char buf[2*MAXGAMEFUNCLEN];
5 Plagman 416
 
3128 terminx 417
    if (which == gamefunc_Show_Console)
418
        OSD_CaptureKey(key1);
419
 
7180 terminx 420
    for (int k = 0; (unsigned)k < ARRAY_SIZE(keys); k++)
822 terminx 421
    {
7180 terminx 422
        if (keys[k] == 0xff || !keys[k])
3209 helixhorne 423
            continue;
836 terminx 424
 
7180 terminx 425
        int match = 0;
426
 
8041 hendricks2 427
        for (; match < ARRAY_SSIZE(sctokeylut); ++match)
7180 terminx 428
        {
429
            if (keys[k] == sctokeylut[match].sc)
822 terminx 430
                break;
7180 terminx 431
        }
824 terminx 432
 
836 terminx 433
        tempbuf[0] = 0;
434
 
7180 terminx 435
        for (int i=NUMGAMEFUNCTIONS-1; i>=0; i--)
824 terminx 436
        {
7180 terminx 437
            if (ud.config.KeyboardKeys[i][0] == keys[k] || ud.config.KeyboardKeys[i][1] == keys[k])
836 terminx 438
            {
7180 terminx 439
                Bsprintf(buf, "gamefunc_%s; ", CONFIG_FunctionNumToName(i));
836 terminx 440
                Bstrcat(tempbuf,buf);
441
            }
824 terminx 442
        }
443
 
7180 terminx 444
        int const len = Bstrlen(tempbuf);
445
 
446
        if (len >= 2)
3209 helixhorne 447
        {
7180 terminx 448
            tempbuf[len-2] = 0;  // cut off the trailing "; "
449
            CONTROL_BindKey(keys[k], tempbuf, 1, sctokeylut[match].key ? sctokeylut[match].key : "<?>");
3209 helixhorne 450
        }
451
        else
452
        {
7180 terminx 453
            CONTROL_FreeKeyBind(keys[k]);
3209 helixhorne 454
        }
822 terminx 455
    }
456
}
457
 
5 Plagman 458
 
333 terminx 459
void CONFIG_SetupMouse(void)
5 Plagman 460
{
7180 terminx 461
    if (ud.config.scripthandle < 0)
462
        return;
463
 
91 terminx 464
    char str[80];
5 Plagman 465
    char temp[80];
466
 
7180 terminx 467
    for (int i=0; i<MAXMOUSEBUTTONS; i++)
5 Plagman 468
    {
584 terminx 469
        Bsprintf(str,"MouseButton%d",i);
335 terminx 470
        temp[0] = 0;
563 terminx 471
        if (!SCRIPT_GetString(ud.config.scripthandle,"Controls", str,temp))
753 terminx 472
            ud.config.MouseFunctions[i][0] = CONFIG_FunctionNameToNum(temp);
5 Plagman 473
 
584 terminx 474
        Bsprintf(str,"MouseButtonClicked%d",i);
335 terminx 475
        temp[0] = 0;
563 terminx 476
        if (!SCRIPT_GetString(ud.config.scripthandle,"Controls", str,temp))
753 terminx 477
            ud.config.MouseFunctions[i][1] = CONFIG_FunctionNameToNum(temp);
5 Plagman 478
    }
479
 
480
    // map over the axes
7180 terminx 481
    for (int i=0; i<MAXMOUSEAXES; i++)
5 Plagman 482
    {
584 terminx 483
        Bsprintf(str,"MouseAnalogAxes%d",i);
335 terminx 484
        temp[0] = 0;
563 terminx 485
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 486
            ud.config.MouseAnalogueAxes[i] = CONFIG_AnalogNameToNum(temp);
5 Plagman 487
 
584 terminx 488
        Bsprintf(str,"MouseDigitalAxes%d_0",i);
335 terminx 489
        temp[0] = 0;
563 terminx 490
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 491
            ud.config.MouseDigitalFunctions[i][0] = CONFIG_FunctionNameToNum(temp);
5 Plagman 492
 
584 terminx 493
        Bsprintf(str,"MouseDigitalAxes%d_1",i);
335 terminx 494
        temp[0] = 0;
563 terminx 495
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 496
            ud.config.MouseDigitalFunctions[i][1] = CONFIG_FunctionNameToNum(temp);
5 Plagman 497
 
584 terminx 498
        Bsprintf(str,"MouseAnalogScale%d",i);
7180 terminx 499
        int32_t scale = ud.config.MouseAnalogueScale[i];
500
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str, &scale);
563 terminx 501
        ud.config.MouseAnalogueScale[i] = scale;
5 Plagman 502
    }
503
 
7180 terminx 504
    for (int i=0; i<MAXMOUSEBUTTONS; i++)
5 Plagman 505
    {
563 terminx 506
        CONTROL_MapButton(ud.config.MouseFunctions[i][0], i, 0, controldevice_mouse);
507
        CONTROL_MapButton(ud.config.MouseFunctions[i][1], i, 1,  controldevice_mouse);
5 Plagman 508
    }
7180 terminx 509
    for (int i=0; i<MAXMOUSEAXES; i++)
5 Plagman 510
    {
563 terminx 511
        CONTROL_MapAnalogAxis(i, ud.config.MouseAnalogueAxes[i], controldevice_mouse);
512
        CONTROL_MapDigitalAxis(i, ud.config.MouseDigitalFunctions[i][0], 0,controldevice_mouse);
513
        CONTROL_MapDigitalAxis(i, ud.config.MouseDigitalFunctions[i][1], 1,controldevice_mouse);
514
        CONTROL_SetAnalogAxisScale(i, ud.config.MouseAnalogueScale[i], controldevice_mouse);
5 Plagman 515
    }
516
}
517
 
518
 
333 terminx 519
void CONFIG_SetupJoystick(void)
5 Plagman 520
{
1346 terminx 521
    int32_t i;
91 terminx 522
    char str[80];
5 Plagman 523
    char temp[80];
1346 terminx 524
    int32_t scale;
5 Plagman 525
 
563 terminx 526
    if (ud.config.scripthandle < 0) return;
5 Plagman 527
 
4826 hendricks2 528
    for (i=0; i<MAXJOYBUTTONSANDHATS; i++)
5 Plagman 529
    {
7969 hendricks2 530
        Bsprintf(str,"ControllerButton%d",i);
335 terminx 531
        temp[0] = 0;
563 terminx 532
        if (!SCRIPT_GetString(ud.config.scripthandle,"Controls", str,temp))
753 terminx 533
            ud.config.JoystickFunctions[i][0] = CONFIG_FunctionNameToNum(temp);
5 Plagman 534
 
7969 hendricks2 535
        Bsprintf(str,"ControllerButtonClicked%d",i);
335 terminx 536
        temp[0] = 0;
563 terminx 537
        if (!SCRIPT_GetString(ud.config.scripthandle,"Controls", str,temp))
753 terminx 538
            ud.config.JoystickFunctions[i][1] = CONFIG_FunctionNameToNum(temp);
5 Plagman 539
    }
540
 
541
    // map over the axes
1229 terminx 542
    for (i=0; i<MAXJOYAXES; i++)
5 Plagman 543
    {
7969 hendricks2 544
        Bsprintf(str,"ControllerAnalogAxes%d",i);
335 terminx 545
        temp[0] = 0;
563 terminx 546
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 547
            ud.config.JoystickAnalogueAxes[i] = CONFIG_AnalogNameToNum(temp);
5 Plagman 548
 
7969 hendricks2 549
        Bsprintf(str,"ControllerDigitalAxes%d_0",i);
335 terminx 550
        temp[0] = 0;
563 terminx 551
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 552
            ud.config.JoystickDigitalFunctions[i][0] = CONFIG_FunctionNameToNum(temp);
5 Plagman 553
 
7969 hendricks2 554
        Bsprintf(str,"ControllerDigitalAxes%d_1",i);
335 terminx 555
        temp[0] = 0;
563 terminx 556
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 557
            ud.config.JoystickDigitalFunctions[i][1] = CONFIG_FunctionNameToNum(temp);
5 Plagman 558
 
7969 hendricks2 559
        Bsprintf(str,"ControllerAnalogScale%d",i);
563 terminx 560
        scale = ud.config.JoystickAnalogueScale[i];
561
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str,&scale);
562
        ud.config.JoystickAnalogueScale[i] = scale;
5 Plagman 563
 
7986 hendricks2 564
        Bsprintf(str,"ControllerAnalogInvert%d",i);
565
        scale = ud.config.JoystickAnalogueInvert[i];
566
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str,&scale);
567
        ud.config.JoystickAnalogueInvert[i] = scale;
568
 
7969 hendricks2 569
        Bsprintf(str,"ControllerAnalogDead%d",i);
563 terminx 570
        scale = ud.config.JoystickAnalogueDead[i];
571
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str,&scale);
572
        ud.config.JoystickAnalogueDead[i] = scale;
5 Plagman 573
 
7969 hendricks2 574
        Bsprintf(str,"ControllerAnalogSaturate%d",i);
563 terminx 575
        scale = ud.config.JoystickAnalogueSaturate[i];
576
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str,&scale);
577
        ud.config.JoystickAnalogueSaturate[i] = scale;
5 Plagman 578
    }
579
 
4826 hendricks2 580
    for (i=0; i<MAXJOYBUTTONSANDHATS; i++)
5 Plagman 581
    {
563 terminx 582
        CONTROL_MapButton(ud.config.JoystickFunctions[i][0], i, 0, controldevice_joystick);
583
        CONTROL_MapButton(ud.config.JoystickFunctions[i][1], i, 1,  controldevice_joystick);
5 Plagman 584
    }
1229 terminx 585
    for (i=0; i<MAXJOYAXES; i++)
5 Plagman 586
    {
563 terminx 587
        CONTROL_MapAnalogAxis(i, ud.config.JoystickAnalogueAxes[i], controldevice_joystick);
588
        CONTROL_MapDigitalAxis(i, ud.config.JoystickDigitalFunctions[i][0], 0, controldevice_joystick);
589
        CONTROL_MapDigitalAxis(i, ud.config.JoystickDigitalFunctions[i][1], 1, controldevice_joystick);
590
        CONTROL_SetAnalogAxisScale(i, ud.config.JoystickAnalogueScale[i], controldevice_joystick);
7986 hendricks2 591
        CONTROL_SetAnalogAxisInvert(i, ud.config.JoystickAnalogueInvert[i], controldevice_joystick);
5 Plagman 592
    }
593
}
594
 
7968 hendricks2 595
struct GameControllerButtonSetting
596
{
597
    GameControllerButton button;
598
    int function;
293 terminx 599
 
7968 hendricks2 600
    void apply() const
601
    {
602
        CONFIG_SetJoystickButtonFunction(button, 0, function);
603
    }
604
};
605
struct GameControllerAnalogAxisSetting
606
{
607
    GameControllerAxis axis;
608
    int function;
609
 
610
    void apply() const
611
    {
612
        CONFIG_SetJoystickAnalogAxisFunction(axis, function);
613
    }
614
};
615
struct GameControllerDigitalAxisSetting
616
{
617
    GameControllerAxis axis;
618
    int polarity;
619
    int function;
620
 
621
    void apply() const
622
    {
623
        CONFIG_SetJoystickDigitalAxisFunction(axis, polarity, function);
624
    }
625
};
626
 
627
static void CONFIG_SetGameControllerAxesModern()
628
{
629
    static GameControllerAnalogAxisSetting const analogAxes[] =
630
    {
631
        { GAMECONTROLLER_AXIS_LEFTX, analog_strafing },
632
        { GAMECONTROLLER_AXIS_LEFTY, analog_moving },
633
        { GAMECONTROLLER_AXIS_RIGHTX, analog_turning },
634
        { GAMECONTROLLER_AXIS_RIGHTY, analog_lookingupanddown },
635
    };
636
 
7981 hendricks2 637
    CONFIG_SetJoystickAnalogAxisScale(GAMECONTROLLER_AXIS_RIGHTX, 32768+16384);
638
    CONFIG_SetJoystickAnalogAxisScale(GAMECONTROLLER_AXIS_RIGHTY, 32768+16384);
639
 
7968 hendricks2 640
    for (auto const & analogAxis : analogAxes)
641
        analogAxis.apply();
642
}
643
 
644
void CONFIG_SetGameControllerDefaultsStandard()
645
{
646
    CONFIG_SetGameControllerDefaultsClear();
647
    CONFIG_SetGameControllerAxesModern();
648
 
649
    static GameControllerButtonSetting const buttons[] =
650
    {
651
        { GAMECONTROLLER_BUTTON_A, gamefunc_Jump },
8059 terminx 652
        { GAMECONTROLLER_BUTTON_B, gamefunc_Toggle_Crouch },
7968 hendricks2 653
        { GAMECONTROLLER_BUTTON_BACK, gamefunc_Map },
654
        { GAMECONTROLLER_BUTTON_LEFTSTICK, gamefunc_Run },
7981 hendricks2 655
        { GAMECONTROLLER_BUTTON_RIGHTSTICK, gamefunc_Quick_Kick },
7968 hendricks2 656
        { GAMECONTROLLER_BUTTON_LEFTSHOULDER, gamefunc_Crouch },
657
        { GAMECONTROLLER_BUTTON_RIGHTSHOULDER, gamefunc_Jump },
658
        { GAMECONTROLLER_BUTTON_DPAD_UP, gamefunc_Previous_Weapon },
659
        { GAMECONTROLLER_BUTTON_DPAD_DOWN, gamefunc_Next_Weapon },
660
    };
661
 
7981 hendricks2 662
    static GameControllerButtonSetting const buttonsDuke[] =
7968 hendricks2 663
    {
7981 hendricks2 664
        { GAMECONTROLLER_BUTTON_X, gamefunc_Open },
665
        { GAMECONTROLLER_BUTTON_Y, gamefunc_Inventory },
7968 hendricks2 666
        { GAMECONTROLLER_BUTTON_DPAD_LEFT, gamefunc_Inventory_Left },
667
        { GAMECONTROLLER_BUTTON_DPAD_RIGHT, gamefunc_Inventory_Right },
668
    };
669
 
7981 hendricks2 670
    static GameControllerButtonSetting const buttonsFury[] =
7968 hendricks2 671
    {
7981 hendricks2 672
        { GAMECONTROLLER_BUTTON_X, gamefunc_Steroids }, // Reload
673
        { GAMECONTROLLER_BUTTON_Y, gamefunc_Open },
7968 hendricks2 674
        { GAMECONTROLLER_BUTTON_DPAD_LEFT, gamefunc_MedKit },
7981 hendricks2 675
        { GAMECONTROLLER_BUTTON_DPAD_RIGHT, gamefunc_NightVision }, // Radar
7968 hendricks2 676
    };
677
 
7981 hendricks2 678
    static GameControllerDigitalAxisSetting const digitalAxes[] =
679
    {
680
        { GAMECONTROLLER_AXIS_TRIGGERLEFT, 1, gamefunc_Alt_Fire },
681
        { GAMECONTROLLER_AXIS_TRIGGERRIGHT, 1, gamefunc_Fire },
682
    };
683
 
7968 hendricks2 684
    for (auto const & button : buttons)
685
        button.apply();
686
 
687
    if (FURY)
688
    {
7981 hendricks2 689
        for (auto const & button : buttonsFury)
7968 hendricks2 690
            button.apply();
691
    }
692
    else
693
    {
7981 hendricks2 694
        for (auto const & button : buttonsDuke)
7968 hendricks2 695
            button.apply();
696
    }
697
 
698
    for (auto const & digitalAxis : digitalAxes)
699
        digitalAxis.apply();
700
}
701
 
702
void CONFIG_SetGameControllerDefaultsPro()
703
{
704
    CONFIG_SetGameControllerDefaultsClear();
705
    CONFIG_SetGameControllerAxesModern();
706
 
707
    static GameControllerButtonSetting const buttons[] =
708
    {
709
        { GAMECONTROLLER_BUTTON_A, gamefunc_Open },
7981 hendricks2 710
        { GAMECONTROLLER_BUTTON_B, gamefunc_Third_Person_View },
711
        { GAMECONTROLLER_BUTTON_Y, gamefunc_Quick_Kick },
7968 hendricks2 712
        { GAMECONTROLLER_BUTTON_BACK, gamefunc_Map },
713
        { GAMECONTROLLER_BUTTON_LEFTSTICK, gamefunc_Run },
714
        { GAMECONTROLLER_BUTTON_RIGHTSTICK, gamefunc_Crouch },
715
        { GAMECONTROLLER_BUTTON_DPAD_UP, gamefunc_Previous_Weapon },
716
        { GAMECONTROLLER_BUTTON_DPAD_DOWN, gamefunc_Next_Weapon },
717
    };
718
 
7981 hendricks2 719
    static GameControllerButtonSetting const buttonsDuke[] =
7968 hendricks2 720
    {
721
        { GAMECONTROLLER_BUTTON_X, gamefunc_Inventory },
722
        { GAMECONTROLLER_BUTTON_LEFTSHOULDER, gamefunc_Previous_Weapon },
723
        { GAMECONTROLLER_BUTTON_RIGHTSHOULDER, gamefunc_Next_Weapon },
724
        { GAMECONTROLLER_BUTTON_DPAD_LEFT, gamefunc_Inventory_Left },
725
        { GAMECONTROLLER_BUTTON_DPAD_RIGHT, gamefunc_Inventory_Right },
726
    };
727
 
7981 hendricks2 728
    static GameControllerButtonSetting const buttonsFury[] =
7968 hendricks2 729
    {
7981 hendricks2 730
        { GAMECONTROLLER_BUTTON_X, gamefunc_Steroids }, // Reload
7968 hendricks2 731
        { GAMECONTROLLER_BUTTON_LEFTSHOULDER, gamefunc_Crouch },
732
        { GAMECONTROLLER_BUTTON_RIGHTSHOULDER, gamefunc_Alt_Fire },
733
        { GAMECONTROLLER_BUTTON_DPAD_LEFT, gamefunc_MedKit },
7981 hendricks2 734
        { GAMECONTROLLER_BUTTON_DPAD_RIGHT, gamefunc_NightVision }, // Radar
7968 hendricks2 735
    };
736
 
7981 hendricks2 737
    static GameControllerDigitalAxisSetting const digitalAxes[] =
738
    {
739
        { GAMECONTROLLER_AXIS_TRIGGERLEFT, 1, gamefunc_Jump },
740
        { GAMECONTROLLER_AXIS_TRIGGERRIGHT, 1, gamefunc_Fire },
741
    };
742
 
7968 hendricks2 743
    for (auto const & button : buttons)
744
        button.apply();
745
 
746
    if (FURY)
747
    {
7981 hendricks2 748
        for (auto const & button : buttonsFury)
7968 hendricks2 749
            button.apply();
750
    }
751
    else
752
    {
7981 hendricks2 753
        for (auto const & button : buttonsDuke)
7968 hendricks2 754
            button.apply();
755
    }
756
 
757
    for (auto const & digitalAxis : digitalAxes)
758
        digitalAxis.apply();
759
}
760
 
761
void CONFIG_SetGameControllerDefaultsClear()
762
{
763
    for (int i=0; i<MAXJOYBUTTONSANDHATS; i++)
764
    {
765
        CONFIG_SetJoystickButtonFunction(i, 0, -1);
766
        CONFIG_SetJoystickButtonFunction(i, 1, -1);
767
    }
768
 
769
    for (int i=0; i<MAXJOYAXES; i++)
770
    {
771
        CONFIG_SetJoystickAnalogAxisScale(i, DEFAULTJOYSTICKANALOGUESCALE);
7986 hendricks2 772
        CONFIG_SetJoystickAnalogAxisInvert(i, 0);
7968 hendricks2 773
        CONFIG_SetJoystickAnalogAxisDeadSaturate(i, DEFAULTJOYSTICKANALOGUEDEAD, DEFAULTJOYSTICKANALOGUESATURATE);
774
 
775
        CONFIG_SetJoystickDigitalAxisFunction(i, 0, -1);
776
        CONFIG_SetJoystickDigitalAxisFunction(i, 1, -1);
777
 
778
        CONFIG_SetJoystickAnalogAxisFunction(i, -1);
779
    }
780
}
781
 
7180 terminx 782
int CONFIG_ReadSetup(void)
5 Plagman 783
{
1029 terminx 784
    char tempbuf[1024];
5 Plagman 785
 
786
    CONTROL_ClearAssignments();
787
    CONFIG_SetDefaults();
788
 
563 terminx 789
    ud.config.setupread = 1;
6339 terminx 790
    pathsearchmode = 1;
112 terminx 791
 
6339 terminx 792
    if (ud.config.scripthandle < 0)
793
    {
7359 hendricks2 794
        if (buildvfs_exists(g_setupFileName))  // JBF 20031211
6339 terminx 795
            ud.config.scripthandle = SCRIPT_Load(g_setupFileName);
6311 terminx 796
#if !defined(EDUKE32_TOUCH_DEVICES) && !defined(EDUKE32_STANDALONE)
7359 hendricks2 797
        else if (buildvfs_exists(SETUPFILENAME))
6339 terminx 798
        {
7180 terminx 799
            int const i = wm_ynbox("Import Configuration Settings",
800
                                   "The configuration file \"%s\" was not found. "
801
                                   "Import configuration data from \"%s\"?",
802
                                   g_setupFileName, SETUPFILENAME);
803
            if (i)
804
                ud.config.scripthandle = SCRIPT_Load(SETUPFILENAME);
6339 terminx 805
        }
806
#endif
1178 terminx 807
    }
4440 terminx 808
 
466 terminx 809
    pathsearchmode = 0;
5 Plagman 810
 
6339 terminx 811
    if (ud.config.scripthandle < 0)
812
        return -1;
194 terminx 813
 
7180 terminx 814
    char commmacro[] = "CommbatMacro# ";
815
 
816
    for (int i = 0; i < MAXRIDECULE; i++)
5 Plagman 817
    {
7180 terminx 818
        commmacro[13] = i+'0';
819
        SCRIPT_GetString(ud.config.scripthandle, "Comm Setup",commmacro,&ud.ridecule[i][0]);
6339 terminx 820
    }
1100 terminx 821
 
6339 terminx 822
    Bmemset(tempbuf, 0, sizeof(tempbuf));
823
    SCRIPT_GetString(ud.config.scripthandle, "Comm Setup","PlayerName",&tempbuf[0]);
5 Plagman 824
 
6339 terminx 825
    char nameBuf[64];
5 Plagman 826
 
6339 terminx 827
    while (Bstrlen(OSD_StripColors(nameBuf, tempbuf)) > 10)
828
        tempbuf[Bstrlen(tempbuf) - 1] = '\0';
135 terminx 829
 
6339 terminx 830
    Bstrncpyz(szPlayerName, tempbuf, sizeof(szPlayerName));
159 terminx 831
 
6668 hendricks2 832
    SCRIPT_GetString(ud.config.scripthandle, "Comm Setup","RTSName",&ud.rtsname[0]);
5 Plagman 833
 
6339 terminx 834
    SCRIPT_GetNumber(ud.config.scripthandle, "Setup", "ConfigVersion", &ud.configversion);
7219 terminx 835
    SCRIPT_GetNumber(ud.config.scripthandle, "Setup", "ForceSetup", &ud.setup.forcesetup);
836
    SCRIPT_GetNumber(ud.config.scripthandle, "Setup", "NoAutoLoad", &ud.setup.noautoload);
1143 terminx 837
 
7180 terminx 838
    int32_t cachesize;
839
    SCRIPT_GetNumber(ud.config.scripthandle, "Setup", "CacheSize", &cachesize);
6878 terminx 840
 
7180 terminx 841
    if (cachesize > MAXCACHE1DSIZE)
842
        MAXCACHE1DSIZE = cachesize;
843
 
6339 terminx 844
    if (g_noSetup == 0 && g_modDir[0] == '/')
845
    {
846
        SCRIPT_GetString(ud.config.scripthandle, "Setup","ModDir",&g_modDir[0]);
847
 
7359 hendricks2 848
        if (!buildvfs_isdir(g_modDir))
1187 terminx 849
        {
7359 hendricks2 850
            initprintf("Invalid mod dir in cfg!\n");
851
            Bsprintf(g_modDir,"/");
1187 terminx 852
        }
6339 terminx 853
    }
1143 terminx 854
 
6339 terminx 855
    if (g_grpNamePtr == NULL && g_addonNum == 0)
856
    {
7180 terminx 857
        SCRIPT_GetStringPtr(ud.config.scripthandle, "Setup", "SelectedGRP", &g_grpNamePtr);
6339 terminx 858
        if (g_grpNamePtr && !Bstrlen(g_grpNamePtr))
859
            g_grpNamePtr = dup_filename(G_DefaultGrpFile());
860
    }
374 terminx 861
 
6339 terminx 862
    if (!NAM_WW2GI)
863
    {
7180 terminx 864
        SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "Out", &ud.lockout);
865
        SCRIPT_GetString(ud.config.scripthandle, "Screen Setup", "Password", &ud.pwlockout[0]);
6339 terminx 866
    }
245 terminx 867
 
6339 terminx 868
    windowx = -1;
869
    windowy = -1;
5 Plagman 870
 
6339 terminx 871
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "MaxRefreshFreq", (int32_t *)&maxrefreshfreq);
7219 terminx 872
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenBPP", &ud.setup.bpp);
8063 terminx 873
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenDisplay", &r_displayindex);
7219 terminx 874
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenHeight", &ud.setup.ydim);
875
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenMode", &ud.setup.fullscreen);
876
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenWidth", &ud.setup.xdim);
7180 terminx 877
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "WindowPosX", (int32_t *)&windowx);
878
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "WindowPosY", (int32_t *)&windowy);
879
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "WindowPositioning", (int32_t *)&windowpos);
1143 terminx 880
 
7219 terminx 881
    if (ud.setup.bpp < 8) ud.setup.bpp = 32;
6339 terminx 882
 
1677 terminx 883
#ifdef POLYMER
7180 terminx 884
    int32_t rendmode = 0;
885
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "Polymer", &rendmode);
886
    glrendmode = (rendmode > 0) ? REND_POLYMER : REND_POLYMOST;
1660 terminx 887
#endif
163 terminx 888
 
7180 terminx 889
    SCRIPT_GetNumber(ud.config.scripthandle, "Misc", "Executions", &ud.executions);
148 terminx 890
 
428 terminx 891
#ifdef _WIN32
6339 terminx 892
    SCRIPT_GetNumber(ud.config.scripthandle, "Updates", "CheckForUpdates", &ud.config.CheckForUpdates);
893
    SCRIPT_GetNumber(ud.config.scripthandle, "Updates", "LastUpdateCheck", &ud.config.LastUpdateCheck);
416 terminx 894
#endif
942 terminx 895
 
563 terminx 896
    ud.config.setupread = 1;
194 terminx 897
    return 0;
5 Plagman 898
}
899
 
900
 
5387 terminx 901
void CONFIG_WriteSettings(void) // save binds and aliases to <cfgname>_settings.cfg
813 terminx 902
{
7180 terminx 903
    char filename[BMAX_PATH];
813 terminx 904
 
5835 terminx 905
    if (!Bstrcmp(g_setupFileName, SETUPFILENAME))
7180 terminx 906
        Bsprintf(filename, "settings.cfg");
907
    else
7359 hendricks2 908
        Bsprintf(filename, "%s_settings.cfg", strtok(g_setupFileName, "."));
1625 terminx 909
 
7359 hendricks2 910
    buildvfs_FILE fp = buildvfs_fopen_write(filename);
972 terminx 911
 
813 terminx 912
    if (fp)
913
    {
7359 hendricks2 914
        buildvfs_fputstr(fp, "// this file is automatically generated by ");
915
        buildvfs_fputstrptr(fp, AppProperName);
916
        buildvfs_fputstr(fp,"\nunbindall\n");
1632 terminx 917
 
7180 terminx 918
        for (int i=0; i<MAXBOUNDKEYS+MAXMOUSEBUTTONS; i++)
919
        {
3209 helixhorne 920
            if (CONTROL_KeyIsBound(i))
7180 terminx 921
            {
7359 hendricks2 922
                buildvfs_fputstr(fp, "bind \"");
923
                buildvfs_fputstrptr(fp, CONTROL_KeyBinds[i].key);
924
                if (CONTROL_KeyBinds[i].repeat)
925
                    buildvfs_fputstr(fp, "\" \"");
926
                else
927
                    buildvfs_fputstr(fp, "\" norepeat \"");
928
                buildvfs_fputstrptr(fp, CONTROL_KeyBinds[i].cmdstr);
929
                buildvfs_fputstr(fp, "\"\n");
7180 terminx 930
            }
931
        }
818 terminx 932
 
7442 terminx 933
        for (int i=0; i<NUMGAMEFUNCTIONS; ++i)
934
        {
7943 hendricks2 935
            char const * name = CONFIG_FunctionNumToName(i);
936
            if (name && name[0] != '\0' && (ud.config.KeyboardKeys[i][0] == 0xff || !ud.config.KeyboardKeys[i][0]))
7442 terminx 937
            {
938
                buildvfs_fputstr(fp, "unbound ");
7943 hendricks2 939
                buildvfs_fputstrptr(fp, name);
7442 terminx 940
                buildvfs_fputstr(fp, "\n");
941
            }
942
        }
943
 
4213 helixhorne 944
        OSD_WriteAliases(fp);
5387 terminx 945
 
5950 hendricks2 946
        if (g_crosshairSum != -1 && g_crosshairSum != DefaultCrosshairColors.r+(DefaultCrosshairColors.g<<8)+(DefaultCrosshairColors.b<<16))
7359 hendricks2 947
        {
948
            buildvfs_fputstr(fp, "crosshaircolor ");
949
            char buf[64];
950
            snprintf(buf, sizeof(buf), "%d %d %d\n", CrosshairColors.r, CrosshairColors.g, CrosshairColors.b);
951
            buildvfs_fputstrptr(fp, buf);
952
        }
5387 terminx 953
 
4213 helixhorne 954
        OSD_WriteCvars(fp);
818 terminx 955
 
7359 hendricks2 956
        buildvfs_fclose(fp);
4213 helixhorne 957
 
7180 terminx 958
        OSD_Printf("Wrote %s\n", filename);
1740 helixhorne 959
 
845 terminx 960
        return;
813 terminx 961
    }
1625 terminx 962
 
7180 terminx 963
    OSD_Printf("Error writing %s: %s\n", filename, strerror(errno));
813 terminx 964
}
965
 
1820 terminx 966
void CONFIG_WriteSetup(uint32_t flags)
5 Plagman 967
{
563 terminx 968
    if (!ud.config.setupread) return;
112 terminx 969
 
563 terminx 970
    if (ud.config.scripthandle < 0)
5835 terminx 971
        ud.config.scripthandle = SCRIPT_Init(g_setupFileName);
5 Plagman 972
 
7180 terminx 973
    SCRIPT_PutNumber(ud.config.scripthandle, "Misc", "Executions", ud.executions, FALSE, FALSE);
150 terminx 974
 
6791 terminx 975
    SCRIPT_PutNumber(ud.config.scripthandle, "Setup", "CacheSize", MAXCACHE1DSIZE, FALSE, FALSE);
7180 terminx 976
    SCRIPT_PutNumber(ud.config.scripthandle, "Setup", "ConfigVersion", BYTEVERSION_EDUKE32, FALSE, FALSE);
7219 terminx 977
    SCRIPT_PutNumber(ud.config.scripthandle, "Setup", "ForceSetup", ud.setup.forcesetup, FALSE, FALSE);
978
    SCRIPT_PutNumber(ud.config.scripthandle, "Setup", "NoAutoLoad", ud.setup.noautoload, FALSE, FALSE);
1143 terminx 979
 
1660 terminx 980
#ifdef POLYMER
7293 terminx 981
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "Polymer", glrendmode == REND_POLYMER, FALSE, FALSE);
1660 terminx 982
#endif
1143 terminx 983
 
7219 terminx 984
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenBPP", ud.setup.bpp, FALSE, FALSE);
8063 terminx 985
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenDisplay", r_displayindex, FALSE, FALSE);
7219 terminx 986
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenHeight", ud.setup.ydim, FALSE, FALSE);
987
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenMode", ud.setup.fullscreen, FALSE, FALSE);
988
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenWidth", ud.setup.xdim, FALSE, FALSE);
1143 terminx 989
 
5830 terminx 990
    if (g_grpNamePtr && !g_addonNum)
7180 terminx 991
        SCRIPT_PutString(ud.config.scripthandle, "Setup", "SelectedGRP", g_grpNamePtr);
1820 terminx 992
 
6273 terminx 993
#ifdef STARTUP_SETUP_WINDOW
1820 terminx 994
    if (g_noSetup == 0)
7180 terminx 995
        SCRIPT_PutString(ud.config.scripthandle, "Setup", "ModDir", &g_modDir[0]);
2962 helixhorne 996
#endif
1820 terminx 997
    // exit early after only updating the values that can be changed from the startup window
998
    if (flags & 1)
999
    {
5835 terminx 1000
        SCRIPT_Save(ud.config.scripthandle, g_setupFileName);
1820 terminx 1001
        SCRIPT_Free(ud.config.scripthandle);
1002
        return;
1003
    }
1004
 
7180 terminx 1005
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "MaxRefreshFreq", maxrefreshfreq, FALSE, FALSE);
1346 terminx 1006
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "WindowPosX", windowx, FALSE, FALSE);
1007
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "WindowPosY", windowy, FALSE, FALSE);
7180 terminx 1008
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "WindowPositioning", windowpos, FALSE, FALSE);
1143 terminx 1009
 
5450 hendricks2 1010
    if (!NAM_WW2GI)
1820 terminx 1011
    {
1012
        SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "Out",ud.lockout,FALSE,FALSE);
1013
        SCRIPT_PutString(ud.config.scripthandle, "Screen Setup", "Password",ud.pwlockout);
1014
    }
1015
 
416 terminx 1016
#ifdef _WIN32
1346 terminx 1017
    SCRIPT_PutNumber(ud.config.scripthandle, "Updates", "CheckForUpdates", ud.config.CheckForUpdates, FALSE, FALSE);
1018
    SCRIPT_PutNumber(ud.config.scripthandle, "Updates", "LastUpdateCheck", ud.config.LastUpdateCheck, FALSE, FALSE);
416 terminx 1019
#endif
1020
 
7219 terminx 1021
    if (ud.setup.usemouse)
335 terminx 1022
    {
7180 terminx 1023
        for (int i=0; i<MAXMOUSEBUTTONS; i++)
1625 terminx 1024
        {
7180 terminx 1025
            if (CONFIG_FunctionNumToName(ud.config.MouseFunctions[i][0]))
5065 terminx 1026
            {
7180 terminx 1027
                Bsprintf(buf, "MouseButton%d", i);
1028
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.MouseFunctions[i][0]));
5065 terminx 1029
            }
5 Plagman 1030
 
7180 terminx 1031
            if (i >= (MAXMOUSEBUTTONS-2)) continue;
5 Plagman 1032
 
7180 terminx 1033
            if (CONFIG_FunctionNumToName(ud.config.MouseFunctions[i][1]))
5065 terminx 1034
            {
7180 terminx 1035
                Bsprintf(buf, "MouseButtonClicked%d", i);
1036
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.MouseFunctions[i][1]));
5065 terminx 1037
            }
1625 terminx 1038
        }
606 terminx 1039
 
7180 terminx 1040
        for (int i=0; i<MAXMOUSEAXES; i++)
1625 terminx 1041
        {
7180 terminx 1042
            if (CONFIG_AnalogNumToName(ud.config.MouseAnalogueAxes[i]))
5065 terminx 1043
            {
7180 terminx 1044
                Bsprintf(buf, "MouseAnalogAxes%d", i);
1045
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_AnalogNumToName(ud.config.MouseAnalogueAxes[i]));
5065 terminx 1046
            }
5 Plagman 1047
 
7180 terminx 1048
            if (CONFIG_FunctionNumToName(ud.config.MouseDigitalFunctions[i][0]))
5065 terminx 1049
            {
7180 terminx 1050
                Bsprintf(buf, "MouseDigitalAxes%d_0", i);
1051
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.MouseDigitalFunctions[i][0]));
5065 terminx 1052
            }
5 Plagman 1053
 
7180 terminx 1054
            if (CONFIG_FunctionNumToName(ud.config.MouseDigitalFunctions[i][1]))
5065 terminx 1055
            {
7180 terminx 1056
                Bsprintf(buf, "MouseDigitalAxes%d_1", i);
1057
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.MouseDigitalFunctions[i][1]));
5065 terminx 1058
            }
5 Plagman 1059
 
7341 terminx 1060
            Bsprintf(buf, "MouseAnalogScale%d", i);
1061
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.MouseAnalogueScale[i], FALSE, FALSE);
1625 terminx 1062
        }
5 Plagman 1063
    }
1064
 
7219 terminx 1065
    if (ud.setup.usejoystick)
335 terminx 1066
    {
7180 terminx 1067
        for (int dummy=0; dummy<MAXJOYBUTTONSANDHATS; dummy++)
1625 terminx 1068
        {
5065 terminx 1069
            if (CONFIG_FunctionNumToName(ud.config.JoystickFunctions[dummy][0]))
1070
            {
7969 hendricks2 1071
                Bsprintf(buf, "ControllerButton%d", dummy);
5065 terminx 1072
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.JoystickFunctions[dummy][0]));
1073
            }
5 Plagman 1074
 
5065 terminx 1075
            if (CONFIG_FunctionNumToName(ud.config.JoystickFunctions[dummy][1]))
1076
            {
7969 hendricks2 1077
                Bsprintf(buf, "ControllerButtonClicked%d", dummy);
5065 terminx 1078
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.JoystickFunctions[dummy][1]));
1079
            }
1625 terminx 1080
        }
7180 terminx 1081
        for (int dummy=0; dummy<MAXJOYAXES; dummy++)
1625 terminx 1082
        {
5065 terminx 1083
            if (CONFIG_AnalogNumToName(ud.config.JoystickAnalogueAxes[dummy]))
1084
            {
7969 hendricks2 1085
                Bsprintf(buf, "ControllerAnalogAxes%d", dummy);
5065 terminx 1086
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_AnalogNumToName(ud.config.JoystickAnalogueAxes[dummy]));
1087
            }
5 Plagman 1088
 
5065 terminx 1089
            if (CONFIG_FunctionNumToName(ud.config.JoystickDigitalFunctions[dummy][0]))
1090
            {
7969 hendricks2 1091
                Bsprintf(buf, "ControllerDigitalAxes%d_0", dummy);
5065 terminx 1092
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.JoystickDigitalFunctions[dummy][0]));
1093
            }
5 Plagman 1094
 
5065 terminx 1095
            if (CONFIG_FunctionNumToName(ud.config.JoystickDigitalFunctions[dummy][1]))
1096
            {
7969 hendricks2 1097
                Bsprintf(buf, "ControllerDigitalAxes%d_1", dummy);
5065 terminx 1098
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.JoystickDigitalFunctions[dummy][1]));
1099
            }
5 Plagman 1100
 
7969 hendricks2 1101
            Bsprintf(buf, "ControllerAnalogScale%d", dummy);
7341 terminx 1102
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.JoystickAnalogueScale[dummy], FALSE, FALSE);
5 Plagman 1103
 
7986 hendricks2 1104
            Bsprintf(buf, "ControllerAnalogInvert%d", dummy);
1105
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.JoystickAnalogueInvert[dummy], FALSE, FALSE);
1106
 
7969 hendricks2 1107
            Bsprintf(buf, "ControllerAnalogDead%d", dummy);
7341 terminx 1108
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.JoystickAnalogueDead[dummy], FALSE, FALSE);
5 Plagman 1109
 
7969 hendricks2 1110
            Bsprintf(buf, "ControllerAnalogSaturate%d", dummy);
7341 terminx 1111
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.JoystickAnalogueSaturate[dummy], FALSE, FALSE);
1625 terminx 1112
        }
5 Plagman 1113
    }
1114
 
1143 terminx 1115
    SCRIPT_PutString(ud.config.scripthandle, "Comm Setup","PlayerName",&szPlayerName[0]);
5065 terminx 1116
 
6668 hendricks2 1117
    SCRIPT_PutString(ud.config.scripthandle, "Comm Setup","RTSName",&ud.rtsname[0]);
495 terminx 1118
 
5065 terminx 1119
    char commmacro[] = "CommbatMacro# ";
1120
 
7180 terminx 1121
    for (int dummy = 0; dummy < MAXRIDECULE; dummy++)
88 terminx 1122
    {
5065 terminx 1123
        commmacro[13] = dummy+'0';
1124
        SCRIPT_PutString(ud.config.scripthandle, "Comm Setup",commmacro,&ud.ridecule[dummy][0]);
88 terminx 1125
    }
1126
 
5835 terminx 1127
    SCRIPT_Save(ud.config.scripthandle, g_setupFileName);
3636 terminx 1128
 
1129
    if ((flags & 2) == 0)
1130
        SCRIPT_Free(ud.config.scripthandle);
1131
 
5835 terminx 1132
    OSD_Printf("Wrote %s\n",g_setupFileName);
5387 terminx 1133
    CONFIG_WriteSettings();
2725 hendricks2 1134
    Bfflush(NULL);
5 Plagman 1135
}
1136
 
7993 hendricks2 1137
char const * CONFIG_GetGameFuncOnKeyboard(int gameFunc)
1138
{
7996 hendricks2 1139
    const char * string0 = KB_ScanCodeToString(ud.config.KeyboardKeys[gameFunc][0]);
1140
    return string0[0] == '\0' ? KB_ScanCodeToString(ud.config.KeyboardKeys[gameFunc][1]) : string0;
7993 hendricks2 1141
}
1142
 
1143
char const * CONFIG_GetGameFuncOnMouse(int gameFunc)
1144
{
1145
    for (int j = 0; j < 2; ++j)
1146
        for (int i = 0; i < joystick.numButtons; ++i)
1147
            if (ud.config.JoystickFunctions[i][j] == gameFunc)
1148
                return joyGetName(1, i);
1149
 
1150
    for (int i = 0; i < joystick.numAxes; ++i)
1151
        for (int j = 0; j < 2; ++j)
1152
            if (ud.config.JoystickDigitalFunctions[i][j] == gameFunc)
1153
                return joyGetName(0, i);
1154
 
1155
    return "";
1156
}
1157
 
1158
char const * CONFIG_GetGameFuncOnJoystick(int gameFunc)
1159
{
1160
    for (int j = 0; j < 2; ++j)
1161
        for (int i = 0; i < joystick.numButtons; ++i)
1162
            if (ud.config.JoystickFunctions[i][j] == gameFunc)
1163
                return joyGetName(1, i);
1164
 
1165
    for (int i = 0; i < joystick.numAxes; ++i)
1166
        for (int j = 0; j < 2; ++j)
1167
            if (ud.config.JoystickDigitalFunctions[i][j] == gameFunc)
1168
                return joyGetName(0, i);
1169
 
1170
    return "";
1171
}
1172
 
5470 hendricks2 1173
static const char *CONFIG_GetMapEntryName(char m[], char const * const mapname)
322 terminx 1174
{
5193 helixhorne 1175
    strcpy(m, mapname);
322 terminx 1176
 
5193 helixhorne 1177
    char *p = strrchr(m, '/');
322 terminx 1178
    if (!p) p = strrchr(m, '\\');
5193 helixhorne 1179
    if (p) Bmemmove(m, p, Bstrlen(p)+1);
1229 terminx 1180
    for (p=m; *p; p++) *p = tolower(*p);
322 terminx 1181
 
1076 terminx 1182
    // cheap hack because SCRIPT_GetNumber doesn't like the slashes
1183
    p = m;
1184
    while (*p == '/') p++;
1185
 
5193 helixhorne 1186
    return p;
1187
}
1188
 
5470 hendricks2 1189
static void CONFIG_GetMD4EntryName(char m[], uint8_t const * const md4)
5193 helixhorne 1190
{
5470 hendricks2 1191
    sprintf(m, "MD4_%08x%08x%08x%08x",
1192
            B_BIG32(B_UNBUF32(&md4[0])), B_BIG32(B_UNBUF32(&md4[4])),
1193
            B_BIG32(B_UNBUF32(&md4[8])), B_BIG32(B_UNBUF32(&md4[12])));
1194
}
1195
 
1196
int32_t CONFIG_GetMapBestTime(char const * const mapname, uint8_t const * const mapmd4)
1197
{
6273 terminx 1198
    if (!ud.config.setupread || ud.config.scripthandle < 0)
1199
        return -1;
322 terminx 1200
 
5470 hendricks2 1201
    char m[37];
7180 terminx 1202
 
5470 hendricks2 1203
    CONFIG_GetMD4EntryName(m, mapmd4);
5193 helixhorne 1204
 
1205
    int32_t t = -1;
5470 hendricks2 1206
    if (SCRIPT_GetNumber(ud.config.scripthandle, "MapTimes", m, &t))
1207
    {
1208
        // fall back to map filenames
1209
        char m2[BMAX_PATH];
7180 terminx 1210
        auto p = CONFIG_GetMapEntryName(m2, mapname);
1211
 
5470 hendricks2 1212
        SCRIPT_GetNumber(ud.config.scripthandle, "MapTimes", p, &t);
1213
    }
7180 terminx 1214
 
322 terminx 1215
    return t;
1216
}
1217
 
7180 terminx 1218
int CONFIG_SetMapBestTime(uint8_t const * const mapmd4, int32_t tm)
322 terminx 1219
{
7180 terminx 1220
    if (ud.config.scripthandle < 0 && (ud.config.scripthandle = SCRIPT_Init(g_setupFileName)) < 0)
1221
        return -1;
322 terminx 1222
 
5470 hendricks2 1223
    char m[37];
7180 terminx 1224
 
5470 hendricks2 1225
    CONFIG_GetMD4EntryName(m, mapmd4);
7180 terminx 1226
    SCRIPT_PutNumber(ud.config.scripthandle, "MapTimes", m, tm, FALSE, FALSE);
5193 helixhorne 1227
 
322 terminx 1228
    return 0;
1229
}
1230