Subversion Repositories eduke32

Rev

Rev 8557 | 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;
8216 terminx 269
    ud.config.MusicDevice     = ASS_AutoDetect;
7180 terminx 270
    ud.config.MusicToggle     = 1;
271
    ud.config.MusicVolume     = 195;
272
    ud.config.NumBits         = 16;
273
    ud.config.NumChannels     = 2;
8480 hendricks2 274
#ifdef ASS_REVERSESTEREO
275
    ud.config.ReverseStereo   = 0;
276
#endif
7180 terminx 277
    ud.config.ShowWeapons     = 0;
278
    ud.config.SoundToggle     = 1;
279
    ud.config.VoiceToggle     = 5;  // bitfield, 1 = local, 2 = dummy, 4 = other players in DM
280
    ud.config.useprecache     = 1;
281
    ud.configversion          = 0;
282
    ud.crosshair              = 1;
283
    ud.crosshairscale         = 50;
284
    ud.default_skill          = 1;
285
    ud.democams               = 1;
286
    ud.detail                 = 0;
287
    ud.display_bonus_screen   = 1;
288
    ud.drawweapon             = 1;
7329 terminx 289
    ud.fov                    = 90;
7180 terminx 290
    ud.hudontop               = 0;
291
    ud.idplayers              = 1;
292
    ud.levelstats             = 0;
293
    ud.lockout                = 0;
294
    ud.m_marker               = 1;
295
    ud.maxautosaves           = 5;
296
    ud.menu_scrollbartilenum  = -1;
297
    ud.menu_scrollbarz        = 65536;
298
    ud.menu_scrollcursorz     = 65536;
299
    ud.menu_slidebarmargin    = 65536;
300
    ud.menu_slidebarz         = 65536;
301
    ud.menu_slidecursorz      = 65536;
302
    ud.menubackground         = 1;
303
    ud.mouseaiming            = 0;
304
    ud.mouseflip              = 1;
305
    ud.msgdisptime            = 120;
306
    ud.obituaries             = 1;
307
    ud.pwlockout[0]           = '\0';
308
    ud.runkey_mode            = 0;
309
    ud.screen_size            = 4;
310
    ud.screen_tilting         = 1;
311
    ud.screenfade             = 1;
312
    ud.shadow_pal             = 4;
313
    ud.shadows                = 1;
314
    ud.show_level_text        = 1;
315
    ud.slidebar_paldisabled   = 1;
316
    ud.statusbarflags         = STATUSBAR_NOSHRINK;
317
    ud.statusbarmode          = 1;
318
    ud.statusbarscale         = 100;
319
    ud.team                   = 0;
320
    ud.textscale              = 200;
321
    ud.viewbob                = 1;
322
    ud.weaponscale            = 100;
323
    ud.weaponsway             = 1;
324
    ud.weaponswitch           = 3;  // new+empty
1245 terminx 325
 
6668 hendricks2 326
    Bstrcpy(ud.rtsname, G_DefaultRtsFile());
5 Plagman 327
 
6273 terminx 328
    Bstrcpy(szPlayerName, "Player");
329
 
330
#ifndef EDUKE32_STANDALONE
5 Plagman 331
    Bstrcpy(ud.ridecule[0], "An inspiration for birth control.");
332
    Bstrcpy(ud.ridecule[1], "You're gonna die for that!");
333
    Bstrcpy(ud.ridecule[2], "It hurts to be you.");
4472 hendricks2 334
    Bstrcpy(ud.ridecule[3], "Lucky son of a bitch.");
335
    Bstrcpy(ud.ridecule[4], "Hmmm... payback time.");
5 Plagman 336
    Bstrcpy(ud.ridecule[5], "You bottom dwelling scum sucker.");
337
    Bstrcpy(ud.ridecule[6], "Damn, you're ugly.");
4472 hendricks2 338
    Bstrcpy(ud.ridecule[7], "Ha ha ha... wasted!");
5 Plagman 339
    Bstrcpy(ud.ridecule[8], "You suck!");
340
    Bstrcpy(ud.ridecule[9], "AARRRGHHHHH!!!");
6273 terminx 341
#endif
5 Plagman 342
 
6184 hendricks2 343
    CONFIG_SetDefaultKeys(keydefaults);
506 terminx 344
 
563 terminx 345
    memset(ud.config.MouseFunctions, -1, sizeof(ud.config.MouseFunctions));
7180 terminx 346
    memset(ud.config.JoystickFunctions, -1, sizeof(ud.config.JoystickFunctions));
347
    memset(ud.config.JoystickDigitalFunctions, -1, sizeof(ud.config.JoystickDigitalFunctions));
348
 
349
    CONTROL_MouseSensitivity = DEFAULTMOUSESENSITIVITY;
350
 
7181 terminx 351
    for (int i=0; i<MAXMOUSEBUTTONS; i++)
335 terminx 352
    {
563 terminx 353
        ud.config.MouseFunctions[i][0] = CONFIG_FunctionNameToNum(mousedefaults[i]);
354
        CONTROL_MapButton(ud.config.MouseFunctions[i][0], i, 0, controldevice_mouse);
114 terminx 355
        if (i>=4) continue;
563 terminx 356
        ud.config.MouseFunctions[i][1] = CONFIG_FunctionNameToNum(mouseclickeddefaults[i]);
357
        CONTROL_MapButton(ud.config.MouseFunctions[i][1], i, 1, controldevice_mouse);
113 terminx 358
    }
5 Plagman 359
 
7181 terminx 360
    for (int i=0; i<MAXMOUSEAXES; i++)
335 terminx 361
    {
8777 terminx 362
        CONTROL_SetAnalogAxisScale(i, DEFAULTMOUSEANALOGUESCALE, controldevice_mouse);
5 Plagman 363
 
563 terminx 364
        ud.config.MouseAnalogueAxes[i] = CONFIG_AnalogNameToNum(mouseanalogdefaults[i]);
365
        CONTROL_MapAnalogAxis(i, ud.config.MouseAnalogueAxes[i], controldevice_mouse);
113 terminx 366
    }
5 Plagman 367
 
7968 hendricks2 368
#if !defined GEKKO
369
    CONFIG_SetGameControllerDefaultsStandard();
370
#else
7181 terminx 371
    for (int i=0; i<MAXJOYBUTTONSANDHATS; i++)
335 terminx 372
    {
563 terminx 373
        ud.config.JoystickFunctions[i][0] = CONFIG_FunctionNameToNum(joystickdefaults[i]);
374
        ud.config.JoystickFunctions[i][1] = CONFIG_FunctionNameToNum(joystickclickeddefaults[i]);
375
        CONTROL_MapButton(ud.config.JoystickFunctions[i][0], i, 0, controldevice_joystick);
376
        CONTROL_MapButton(ud.config.JoystickFunctions[i][1], i, 1, controldevice_joystick);
113 terminx 377
    }
5 Plagman 378
 
7181 terminx 379
    for (int i=0; i<MAXJOYAXES; i++)
335 terminx 380
    {
2728 hendricks2 381
        ud.config.JoystickAnalogueScale[i] = DEFAULTJOYSTICKANALOGUESCALE;
7986 hendricks2 382
        ud.config.JoystickAnalogueInvert[i] = 0;
2728 hendricks2 383
        ud.config.JoystickAnalogueDead[i] = DEFAULTJOYSTICKANALOGUEDEAD;
384
        ud.config.JoystickAnalogueSaturate[i] = DEFAULTJOYSTICKANALOGUESATURATE;
563 terminx 385
        CONTROL_SetAnalogAxisScale(i, ud.config.JoystickAnalogueScale[i], controldevice_joystick);
7986 hendricks2 386
        CONTROL_SetAnalogAxisInvert(i, 0, controldevice_joystick);
5 Plagman 387
 
563 terminx 388
        ud.config.JoystickDigitalFunctions[i][0] = CONFIG_FunctionNameToNum(joystickdigitaldefaults[i*2]);
389
        ud.config.JoystickDigitalFunctions[i][1] = CONFIG_FunctionNameToNum(joystickdigitaldefaults[i*2+1]);
390
        CONTROL_MapDigitalAxis(i, ud.config.JoystickDigitalFunctions[i][0], 0, controldevice_joystick);
391
        CONTROL_MapDigitalAxis(i, ud.config.JoystickDigitalFunctions[i][1], 1, controldevice_joystick);
112 terminx 392
 
563 terminx 393
        ud.config.JoystickAnalogueAxes[i] = CONFIG_AnalogNameToNum(joystickanalogdefaults[i]);
394
        CONTROL_MapAnalogAxis(i, ud.config.JoystickAnalogueAxes[i], controldevice_joystick);
113 terminx 395
    }
7968 hendricks2 396
#endif
6554 hendricks2 397
 
398
    VM_OnEvent(EVENT_SETDEFAULTS, g_player[myconnectindex].ps->i, myconnectindex);
5 Plagman 399
}
400
 
401
 
8303 hendricks2 402
// generates key bindings to reflect changes to keyboard setup
7180 terminx 403
void CONFIG_MapKey(int which, kb_scancode key1, kb_scancode oldkey1, kb_scancode key2, kb_scancode oldkey2)
822 terminx 404
{
7180 terminx 405
    int const keys[] = { key1, key2, oldkey1, oldkey2 };
3209 helixhorne 406
    char buf[2*MAXGAMEFUNCLEN];
5 Plagman 407
 
3128 terminx 408
    if (which == gamefunc_Show_Console)
409
        OSD_CaptureKey(key1);
410
 
7180 terminx 411
    for (int k = 0; (unsigned)k < ARRAY_SIZE(keys); k++)
822 terminx 412
    {
7180 terminx 413
        if (keys[k] == 0xff || !keys[k])
3209 helixhorne 414
            continue;
836 terminx 415
 
7180 terminx 416
        int match = 0;
417
 
8041 hendricks2 418
        for (; match < ARRAY_SSIZE(sctokeylut); ++match)
7180 terminx 419
        {
420
            if (keys[k] == sctokeylut[match].sc)
822 terminx 421
                break;
7180 terminx 422
        }
824 terminx 423
 
836 terminx 424
        tempbuf[0] = 0;
425
 
7180 terminx 426
        for (int i=NUMGAMEFUNCTIONS-1; i>=0; i--)
824 terminx 427
        {
7180 terminx 428
            if (ud.config.KeyboardKeys[i][0] == keys[k] || ud.config.KeyboardKeys[i][1] == keys[k])
836 terminx 429
            {
7180 terminx 430
                Bsprintf(buf, "gamefunc_%s; ", CONFIG_FunctionNumToName(i));
836 terminx 431
                Bstrcat(tempbuf,buf);
432
            }
824 terminx 433
        }
434
 
7180 terminx 435
        int const len = Bstrlen(tempbuf);
436
 
437
        if (len >= 2)
3209 helixhorne 438
        {
7180 terminx 439
            tempbuf[len-2] = 0;  // cut off the trailing "; "
440
            CONTROL_BindKey(keys[k], tempbuf, 1, sctokeylut[match].key ? sctokeylut[match].key : "<?>");
3209 helixhorne 441
        }
442
        else
443
        {
7180 terminx 444
            CONTROL_FreeKeyBind(keys[k]);
3209 helixhorne 445
        }
822 terminx 446
    }
447
}
448
 
5 Plagman 449
 
333 terminx 450
void CONFIG_SetupMouse(void)
5 Plagman 451
{
7180 terminx 452
    if (ud.config.scripthandle < 0)
453
        return;
454
 
91 terminx 455
    char str[80];
5 Plagman 456
    char temp[80];
457
 
7180 terminx 458
    for (int i=0; i<MAXMOUSEBUTTONS; i++)
5 Plagman 459
    {
584 terminx 460
        Bsprintf(str,"MouseButton%d",i);
335 terminx 461
        temp[0] = 0;
563 terminx 462
        if (!SCRIPT_GetString(ud.config.scripthandle,"Controls", str,temp))
753 terminx 463
            ud.config.MouseFunctions[i][0] = CONFIG_FunctionNameToNum(temp);
5 Plagman 464
 
584 terminx 465
        Bsprintf(str,"MouseButtonClicked%d",i);
335 terminx 466
        temp[0] = 0;
563 terminx 467
        if (!SCRIPT_GetString(ud.config.scripthandle,"Controls", str,temp))
753 terminx 468
            ud.config.MouseFunctions[i][1] = CONFIG_FunctionNameToNum(temp);
5 Plagman 469
    }
470
 
471
    // map over the axes
7180 terminx 472
    for (int i=0; i<MAXMOUSEAXES; i++)
5 Plagman 473
    {
584 terminx 474
        Bsprintf(str,"MouseAnalogAxes%d",i);
335 terminx 475
        temp[0] = 0;
563 terminx 476
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 477
            ud.config.MouseAnalogueAxes[i] = CONFIG_AnalogNameToNum(temp);
5 Plagman 478
    }
479
 
7180 terminx 480
    for (int i=0; i<MAXMOUSEBUTTONS; i++)
5 Plagman 481
    {
563 terminx 482
        CONTROL_MapButton(ud.config.MouseFunctions[i][0], i, 0, controldevice_mouse);
483
        CONTROL_MapButton(ud.config.MouseFunctions[i][1], i, 1,  controldevice_mouse);
5 Plagman 484
    }
7180 terminx 485
    for (int i=0; i<MAXMOUSEAXES; i++)
563 terminx 486
        CONTROL_MapAnalogAxis(i, ud.config.MouseAnalogueAxes[i], controldevice_mouse);
5 Plagman 487
}
488
 
489
 
333 terminx 490
void CONFIG_SetupJoystick(void)
5 Plagman 491
{
1346 terminx 492
    int32_t i;
91 terminx 493
    char str[80];
5 Plagman 494
    char temp[80];
1346 terminx 495
    int32_t scale;
5 Plagman 496
 
563 terminx 497
    if (ud.config.scripthandle < 0) return;
5 Plagman 498
 
4826 hendricks2 499
    for (i=0; i<MAXJOYBUTTONSANDHATS; i++)
5 Plagman 500
    {
7969 hendricks2 501
        Bsprintf(str,"ControllerButton%d",i);
335 terminx 502
        temp[0] = 0;
563 terminx 503
        if (!SCRIPT_GetString(ud.config.scripthandle,"Controls", str,temp))
753 terminx 504
            ud.config.JoystickFunctions[i][0] = CONFIG_FunctionNameToNum(temp);
5 Plagman 505
 
7969 hendricks2 506
        Bsprintf(str,"ControllerButtonClicked%d",i);
335 terminx 507
        temp[0] = 0;
563 terminx 508
        if (!SCRIPT_GetString(ud.config.scripthandle,"Controls", str,temp))
753 terminx 509
            ud.config.JoystickFunctions[i][1] = CONFIG_FunctionNameToNum(temp);
5 Plagman 510
    }
511
 
512
    // map over the axes
1229 terminx 513
    for (i=0; i<MAXJOYAXES; i++)
5 Plagman 514
    {
7969 hendricks2 515
        Bsprintf(str,"ControllerAnalogAxes%d",i);
335 terminx 516
        temp[0] = 0;
563 terminx 517
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 518
            ud.config.JoystickAnalogueAxes[i] = CONFIG_AnalogNameToNum(temp);
5 Plagman 519
 
7969 hendricks2 520
        Bsprintf(str,"ControllerDigitalAxes%d_0",i);
335 terminx 521
        temp[0] = 0;
563 terminx 522
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 523
            ud.config.JoystickDigitalFunctions[i][0] = CONFIG_FunctionNameToNum(temp);
5 Plagman 524
 
7969 hendricks2 525
        Bsprintf(str,"ControllerDigitalAxes%d_1",i);
335 terminx 526
        temp[0] = 0;
563 terminx 527
        if (!SCRIPT_GetString(ud.config.scripthandle, "Controls", str,temp))
7943 hendricks2 528
            ud.config.JoystickDigitalFunctions[i][1] = CONFIG_FunctionNameToNum(temp);
5 Plagman 529
 
7969 hendricks2 530
        Bsprintf(str,"ControllerAnalogScale%d",i);
563 terminx 531
        scale = ud.config.JoystickAnalogueScale[i];
532
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str,&scale);
533
        ud.config.JoystickAnalogueScale[i] = scale;
5 Plagman 534
 
7986 hendricks2 535
        Bsprintf(str,"ControllerAnalogInvert%d",i);
536
        scale = ud.config.JoystickAnalogueInvert[i];
537
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str,&scale);
538
        ud.config.JoystickAnalogueInvert[i] = scale;
539
 
7969 hendricks2 540
        Bsprintf(str,"ControllerAnalogDead%d",i);
563 terminx 541
        scale = ud.config.JoystickAnalogueDead[i];
542
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str,&scale);
543
        ud.config.JoystickAnalogueDead[i] = scale;
5 Plagman 544
 
7969 hendricks2 545
        Bsprintf(str,"ControllerAnalogSaturate%d",i);
563 terminx 546
        scale = ud.config.JoystickAnalogueSaturate[i];
547
        SCRIPT_GetNumber(ud.config.scripthandle, "Controls", str,&scale);
548
        ud.config.JoystickAnalogueSaturate[i] = scale;
5 Plagman 549
    }
550
 
4826 hendricks2 551
    for (i=0; i<MAXJOYBUTTONSANDHATS; i++)
5 Plagman 552
    {
563 terminx 553
        CONTROL_MapButton(ud.config.JoystickFunctions[i][0], i, 0, controldevice_joystick);
554
        CONTROL_MapButton(ud.config.JoystickFunctions[i][1], i, 1,  controldevice_joystick);
5 Plagman 555
    }
1229 terminx 556
    for (i=0; i<MAXJOYAXES; i++)
5 Plagman 557
    {
563 terminx 558
        CONTROL_MapAnalogAxis(i, ud.config.JoystickAnalogueAxes[i], controldevice_joystick);
559
        CONTROL_MapDigitalAxis(i, ud.config.JoystickDigitalFunctions[i][0], 0, controldevice_joystick);
560
        CONTROL_MapDigitalAxis(i, ud.config.JoystickDigitalFunctions[i][1], 1, controldevice_joystick);
561
        CONTROL_SetAnalogAxisScale(i, ud.config.JoystickAnalogueScale[i], controldevice_joystick);
7986 hendricks2 562
        CONTROL_SetAnalogAxisInvert(i, ud.config.JoystickAnalogueInvert[i], controldevice_joystick);
5 Plagman 563
    }
564
}
565
 
7968 hendricks2 566
struct GameControllerButtonSetting
567
{
568
    GameControllerButton button;
569
    int function;
293 terminx 570
 
7968 hendricks2 571
    void apply() const
572
    {
573
        CONFIG_SetJoystickButtonFunction(button, 0, function);
574
    }
575
};
576
struct GameControllerAnalogAxisSetting
577
{
578
    GameControllerAxis axis;
579
    int function;
580
 
581
    void apply() const
582
    {
583
        CONFIG_SetJoystickAnalogAxisFunction(axis, function);
584
    }
585
};
586
struct GameControllerDigitalAxisSetting
587
{
588
    GameControllerAxis axis;
589
    int polarity;
590
    int function;
591
 
592
    void apply() const
593
    {
594
        CONFIG_SetJoystickDigitalAxisFunction(axis, polarity, function);
595
    }
596
};
597
 
598
static void CONFIG_SetGameControllerAxesModern()
599
{
600
    static GameControllerAnalogAxisSetting const analogAxes[] =
601
    {
602
        { GAMECONTROLLER_AXIS_LEFTX, analog_strafing },
603
        { GAMECONTROLLER_AXIS_LEFTY, analog_moving },
604
        { GAMECONTROLLER_AXIS_RIGHTX, analog_turning },
605
        { GAMECONTROLLER_AXIS_RIGHTY, analog_lookingupanddown },
606
    };
607
 
7981 hendricks2 608
    CONFIG_SetJoystickAnalogAxisScale(GAMECONTROLLER_AXIS_RIGHTX, 32768+16384);
609
    CONFIG_SetJoystickAnalogAxisScale(GAMECONTROLLER_AXIS_RIGHTY, 32768+16384);
610
 
7968 hendricks2 611
    for (auto const & analogAxis : analogAxes)
612
        analogAxis.apply();
613
}
614
 
615
void CONFIG_SetGameControllerDefaultsStandard()
616
{
617
    CONFIG_SetGameControllerDefaultsClear();
618
    CONFIG_SetGameControllerAxesModern();
619
 
620
    static GameControllerButtonSetting const buttons[] =
621
    {
622
        { GAMECONTROLLER_BUTTON_A, gamefunc_Jump },
8059 terminx 623
        { GAMECONTROLLER_BUTTON_B, gamefunc_Toggle_Crouch },
7968 hendricks2 624
        { GAMECONTROLLER_BUTTON_BACK, gamefunc_Map },
625
        { GAMECONTROLLER_BUTTON_LEFTSTICK, gamefunc_Run },
7981 hendricks2 626
        { GAMECONTROLLER_BUTTON_RIGHTSTICK, gamefunc_Quick_Kick },
7968 hendricks2 627
        { GAMECONTROLLER_BUTTON_LEFTSHOULDER, gamefunc_Crouch },
628
        { GAMECONTROLLER_BUTTON_RIGHTSHOULDER, gamefunc_Jump },
629
        { GAMECONTROLLER_BUTTON_DPAD_UP, gamefunc_Previous_Weapon },
630
        { GAMECONTROLLER_BUTTON_DPAD_DOWN, gamefunc_Next_Weapon },
631
    };
632
 
7981 hendricks2 633
    static GameControllerButtonSetting const buttonsDuke[] =
7968 hendricks2 634
    {
7981 hendricks2 635
        { GAMECONTROLLER_BUTTON_X, gamefunc_Open },
636
        { GAMECONTROLLER_BUTTON_Y, gamefunc_Inventory },
7968 hendricks2 637
        { GAMECONTROLLER_BUTTON_DPAD_LEFT, gamefunc_Inventory_Left },
638
        { GAMECONTROLLER_BUTTON_DPAD_RIGHT, gamefunc_Inventory_Right },
639
    };
640
 
7981 hendricks2 641
    static GameControllerButtonSetting const buttonsFury[] =
7968 hendricks2 642
    {
7981 hendricks2 643
        { GAMECONTROLLER_BUTTON_X, gamefunc_Steroids }, // Reload
644
        { GAMECONTROLLER_BUTTON_Y, gamefunc_Open },
7968 hendricks2 645
        { GAMECONTROLLER_BUTTON_DPAD_LEFT, gamefunc_MedKit },
7981 hendricks2 646
        { GAMECONTROLLER_BUTTON_DPAD_RIGHT, gamefunc_NightVision }, // Radar
7968 hendricks2 647
    };
648
 
7981 hendricks2 649
    static GameControllerDigitalAxisSetting const digitalAxes[] =
650
    {
651
        { GAMECONTROLLER_AXIS_TRIGGERLEFT, 1, gamefunc_Alt_Fire },
652
        { GAMECONTROLLER_AXIS_TRIGGERRIGHT, 1, gamefunc_Fire },
653
    };
654
 
7968 hendricks2 655
    for (auto const & button : buttons)
656
        button.apply();
657
 
658
    if (FURY)
659
    {
7981 hendricks2 660
        for (auto const & button : buttonsFury)
7968 hendricks2 661
            button.apply();
662
    }
663
    else
664
    {
7981 hendricks2 665
        for (auto const & button : buttonsDuke)
7968 hendricks2 666
            button.apply();
667
    }
668
 
669
    for (auto const & digitalAxis : digitalAxes)
670
        digitalAxis.apply();
671
}
672
 
673
void CONFIG_SetGameControllerDefaultsPro()
674
{
675
    CONFIG_SetGameControllerDefaultsClear();
676
    CONFIG_SetGameControllerAxesModern();
677
 
678
    static GameControllerButtonSetting const buttons[] =
679
    {
680
        { GAMECONTROLLER_BUTTON_A, gamefunc_Open },
7981 hendricks2 681
        { GAMECONTROLLER_BUTTON_B, gamefunc_Third_Person_View },
682
        { GAMECONTROLLER_BUTTON_Y, gamefunc_Quick_Kick },
7968 hendricks2 683
        { GAMECONTROLLER_BUTTON_BACK, gamefunc_Map },
684
        { GAMECONTROLLER_BUTTON_LEFTSTICK, gamefunc_Run },
685
        { GAMECONTROLLER_BUTTON_RIGHTSTICK, gamefunc_Crouch },
686
        { GAMECONTROLLER_BUTTON_DPAD_UP, gamefunc_Previous_Weapon },
687
        { GAMECONTROLLER_BUTTON_DPAD_DOWN, gamefunc_Next_Weapon },
688
    };
689
 
7981 hendricks2 690
    static GameControllerButtonSetting const buttonsDuke[] =
7968 hendricks2 691
    {
692
        { GAMECONTROLLER_BUTTON_X, gamefunc_Inventory },
693
        { GAMECONTROLLER_BUTTON_LEFTSHOULDER, gamefunc_Previous_Weapon },
694
        { GAMECONTROLLER_BUTTON_RIGHTSHOULDER, gamefunc_Next_Weapon },
695
        { GAMECONTROLLER_BUTTON_DPAD_LEFT, gamefunc_Inventory_Left },
696
        { GAMECONTROLLER_BUTTON_DPAD_RIGHT, gamefunc_Inventory_Right },
697
    };
698
 
7981 hendricks2 699
    static GameControllerButtonSetting const buttonsFury[] =
7968 hendricks2 700
    {
7981 hendricks2 701
        { GAMECONTROLLER_BUTTON_X, gamefunc_Steroids }, // Reload
7968 hendricks2 702
        { GAMECONTROLLER_BUTTON_LEFTSHOULDER, gamefunc_Crouch },
703
        { GAMECONTROLLER_BUTTON_RIGHTSHOULDER, gamefunc_Alt_Fire },
704
        { GAMECONTROLLER_BUTTON_DPAD_LEFT, gamefunc_MedKit },
7981 hendricks2 705
        { GAMECONTROLLER_BUTTON_DPAD_RIGHT, gamefunc_NightVision }, // Radar
7968 hendricks2 706
    };
707
 
7981 hendricks2 708
    static GameControllerDigitalAxisSetting const digitalAxes[] =
709
    {
710
        { GAMECONTROLLER_AXIS_TRIGGERLEFT, 1, gamefunc_Jump },
711
        { GAMECONTROLLER_AXIS_TRIGGERRIGHT, 1, gamefunc_Fire },
712
    };
713
 
7968 hendricks2 714
    for (auto const & button : buttons)
715
        button.apply();
716
 
717
    if (FURY)
718
    {
7981 hendricks2 719
        for (auto const & button : buttonsFury)
7968 hendricks2 720
            button.apply();
721
    }
722
    else
723
    {
7981 hendricks2 724
        for (auto const & button : buttonsDuke)
7968 hendricks2 725
            button.apply();
726
    }
727
 
728
    for (auto const & digitalAxis : digitalAxes)
729
        digitalAxis.apply();
730
}
731
 
732
void CONFIG_SetGameControllerDefaultsClear()
733
{
734
    for (int i=0; i<MAXJOYBUTTONSANDHATS; i++)
735
    {
736
        CONFIG_SetJoystickButtonFunction(i, 0, -1);
737
        CONFIG_SetJoystickButtonFunction(i, 1, -1);
738
    }
739
 
740
    for (int i=0; i<MAXJOYAXES; i++)
741
    {
742
        CONFIG_SetJoystickAnalogAxisScale(i, DEFAULTJOYSTICKANALOGUESCALE);
7986 hendricks2 743
        CONFIG_SetJoystickAnalogAxisInvert(i, 0);
7968 hendricks2 744
        CONFIG_SetJoystickAnalogAxisDeadSaturate(i, DEFAULTJOYSTICKANALOGUEDEAD, DEFAULTJOYSTICKANALOGUESATURATE);
745
 
746
        CONFIG_SetJoystickDigitalAxisFunction(i, 0, -1);
747
        CONFIG_SetJoystickDigitalAxisFunction(i, 1, -1);
748
 
749
        CONFIG_SetJoystickAnalogAxisFunction(i, -1);
750
    }
751
}
752
 
7180 terminx 753
int CONFIG_ReadSetup(void)
5 Plagman 754
{
1029 terminx 755
    char tempbuf[1024];
5 Plagman 756
 
757
    CONTROL_ClearAssignments();
758
    CONFIG_SetDefaults();
759
 
563 terminx 760
    ud.config.setupread = 1;
6339 terminx 761
    pathsearchmode = 1;
112 terminx 762
 
6339 terminx 763
    if (ud.config.scripthandle < 0)
764
    {
7359 hendricks2 765
        if (buildvfs_exists(g_setupFileName))  // JBF 20031211
6339 terminx 766
            ud.config.scripthandle = SCRIPT_Load(g_setupFileName);
6311 terminx 767
#if !defined(EDUKE32_TOUCH_DEVICES) && !defined(EDUKE32_STANDALONE)
7359 hendricks2 768
        else if (buildvfs_exists(SETUPFILENAME))
6339 terminx 769
        {
7180 terminx 770
            int const i = wm_ynbox("Import Configuration Settings",
771
                                   "The configuration file \"%s\" was not found. "
772
                                   "Import configuration data from \"%s\"?",
773
                                   g_setupFileName, SETUPFILENAME);
774
            if (i)
775
                ud.config.scripthandle = SCRIPT_Load(SETUPFILENAME);
6339 terminx 776
        }
777
#endif
1178 terminx 778
    }
4440 terminx 779
 
466 terminx 780
    pathsearchmode = 0;
5 Plagman 781
 
6339 terminx 782
    if (ud.config.scripthandle < 0)
783
        return -1;
194 terminx 784
 
7180 terminx 785
    char commmacro[] = "CommbatMacro# ";
786
 
787
    for (int i = 0; i < MAXRIDECULE; i++)
5 Plagman 788
    {
7180 terminx 789
        commmacro[13] = i+'0';
790
        SCRIPT_GetString(ud.config.scripthandle, "Comm Setup",commmacro,&ud.ridecule[i][0]);
6339 terminx 791
    }
1100 terminx 792
 
6339 terminx 793
    Bmemset(tempbuf, 0, sizeof(tempbuf));
794
    SCRIPT_GetString(ud.config.scripthandle, "Comm Setup","PlayerName",&tempbuf[0]);
5 Plagman 795
 
6339 terminx 796
    char nameBuf[64];
5 Plagman 797
 
6339 terminx 798
    while (Bstrlen(OSD_StripColors(nameBuf, tempbuf)) > 10)
799
        tempbuf[Bstrlen(tempbuf) - 1] = '\0';
135 terminx 800
 
6339 terminx 801
    Bstrncpyz(szPlayerName, tempbuf, sizeof(szPlayerName));
159 terminx 802
 
6668 hendricks2 803
    SCRIPT_GetString(ud.config.scripthandle, "Comm Setup","RTSName",&ud.rtsname[0]);
5 Plagman 804
 
6339 terminx 805
    SCRIPT_GetNumber(ud.config.scripthandle, "Setup", "ConfigVersion", &ud.configversion);
7219 terminx 806
    SCRIPT_GetNumber(ud.config.scripthandle, "Setup", "ForceSetup", &ud.setup.forcesetup);
807
    SCRIPT_GetNumber(ud.config.scripthandle, "Setup", "NoAutoLoad", &ud.setup.noautoload);
1143 terminx 808
 
7180 terminx 809
    int32_t cachesize;
810
    SCRIPT_GetNumber(ud.config.scripthandle, "Setup", "CacheSize", &cachesize);
6878 terminx 811
 
7180 terminx 812
    if (cachesize > MAXCACHE1DSIZE)
813
        MAXCACHE1DSIZE = cachesize;
814
 
6339 terminx 815
    if (g_noSetup == 0 && g_modDir[0] == '/')
816
    {
817
        SCRIPT_GetString(ud.config.scripthandle, "Setup","ModDir",&g_modDir[0]);
818
 
7359 hendricks2 819
        if (!buildvfs_isdir(g_modDir))
1187 terminx 820
        {
7359 hendricks2 821
            initprintf("Invalid mod dir in cfg!\n");
822
            Bsprintf(g_modDir,"/");
1187 terminx 823
        }
6339 terminx 824
    }
1143 terminx 825
 
6339 terminx 826
    if (g_grpNamePtr == NULL && g_addonNum == 0)
827
    {
7180 terminx 828
        SCRIPT_GetStringPtr(ud.config.scripthandle, "Setup", "SelectedGRP", &g_grpNamePtr);
6339 terminx 829
        if (g_grpNamePtr && !Bstrlen(g_grpNamePtr))
830
            g_grpNamePtr = dup_filename(G_DefaultGrpFile());
831
    }
374 terminx 832
 
6339 terminx 833
    if (!NAM_WW2GI)
834
    {
7180 terminx 835
        SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "Out", &ud.lockout);
836
        SCRIPT_GetString(ud.config.scripthandle, "Screen Setup", "Password", &ud.pwlockout[0]);
6339 terminx 837
    }
245 terminx 838
 
6339 terminx 839
    windowx = -1;
840
    windowy = -1;
5 Plagman 841
 
6339 terminx 842
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "MaxRefreshFreq", (int32_t *)&maxrefreshfreq);
7219 terminx 843
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenBPP", &ud.setup.bpp);
8063 terminx 844
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenDisplay", &r_displayindex);
7219 terminx 845
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenHeight", &ud.setup.ydim);
846
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenMode", &ud.setup.fullscreen);
847
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "ScreenWidth", &ud.setup.xdim);
7180 terminx 848
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "WindowPosX", (int32_t *)&windowx);
849
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "WindowPosY", (int32_t *)&windowy);
850
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "WindowPositioning", (int32_t *)&windowpos);
1143 terminx 851
 
7219 terminx 852
    if (ud.setup.bpp < 8) ud.setup.bpp = 32;
6339 terminx 853
 
1677 terminx 854
#ifdef POLYMER
7180 terminx 855
    int32_t rendmode = 0;
856
    SCRIPT_GetNumber(ud.config.scripthandle, "Screen Setup", "Polymer", &rendmode);
857
    glrendmode = (rendmode > 0) ? REND_POLYMER : REND_POLYMOST;
1660 terminx 858
#endif
163 terminx 859
 
7180 terminx 860
    SCRIPT_GetNumber(ud.config.scripthandle, "Misc", "Executions", &ud.executions);
148 terminx 861
 
428 terminx 862
#ifdef _WIN32
6339 terminx 863
    SCRIPT_GetNumber(ud.config.scripthandle, "Updates", "CheckForUpdates", &ud.config.CheckForUpdates);
864
    SCRIPT_GetNumber(ud.config.scripthandle, "Updates", "LastUpdateCheck", &ud.config.LastUpdateCheck);
416 terminx 865
#endif
942 terminx 866
 
563 terminx 867
    ud.config.setupread = 1;
194 terminx 868
    return 0;
5 Plagman 869
}
870
 
871
 
5387 terminx 872
void CONFIG_WriteSettings(void) // save binds and aliases to <cfgname>_settings.cfg
813 terminx 873
{
7180 terminx 874
    char filename[BMAX_PATH];
813 terminx 875
 
5835 terminx 876
    if (!Bstrcmp(g_setupFileName, SETUPFILENAME))
7180 terminx 877
        Bsprintf(filename, "settings.cfg");
878
    else
7359 hendricks2 879
        Bsprintf(filename, "%s_settings.cfg", strtok(g_setupFileName, "."));
1625 terminx 880
 
7359 hendricks2 881
    buildvfs_FILE fp = buildvfs_fopen_write(filename);
972 terminx 882
 
813 terminx 883
    if (fp)
884
    {
7359 hendricks2 885
        buildvfs_fputstr(fp, "// this file is automatically generated by ");
886
        buildvfs_fputstrptr(fp, AppProperName);
887
        buildvfs_fputstr(fp,"\nunbindall\n");
1632 terminx 888
 
7180 terminx 889
        for (int i=0; i<MAXBOUNDKEYS+MAXMOUSEBUTTONS; i++)
890
        {
3209 helixhorne 891
            if (CONTROL_KeyIsBound(i))
7180 terminx 892
            {
7359 hendricks2 893
                buildvfs_fputstr(fp, "bind \"");
894
                buildvfs_fputstrptr(fp, CONTROL_KeyBinds[i].key);
895
                if (CONTROL_KeyBinds[i].repeat)
896
                    buildvfs_fputstr(fp, "\" \"");
897
                else
898
                    buildvfs_fputstr(fp, "\" norepeat \"");
899
                buildvfs_fputstrptr(fp, CONTROL_KeyBinds[i].cmdstr);
900
                buildvfs_fputstr(fp, "\"\n");
7180 terminx 901
            }
902
        }
818 terminx 903
 
7442 terminx 904
        for (int i=0; i<NUMGAMEFUNCTIONS; ++i)
905
        {
7943 hendricks2 906
            char const * name = CONFIG_FunctionNumToName(i);
907
            if (name && name[0] != '\0' && (ud.config.KeyboardKeys[i][0] == 0xff || !ud.config.KeyboardKeys[i][0]))
7442 terminx 908
            {
909
                buildvfs_fputstr(fp, "unbound ");
7943 hendricks2 910
                buildvfs_fputstrptr(fp, name);
7442 terminx 911
                buildvfs_fputstr(fp, "\n");
912
            }
913
        }
914
 
4213 helixhorne 915
        OSD_WriteAliases(fp);
5387 terminx 916
 
5950 hendricks2 917
        if (g_crosshairSum != -1 && g_crosshairSum != DefaultCrosshairColors.r+(DefaultCrosshairColors.g<<8)+(DefaultCrosshairColors.b<<16))
7359 hendricks2 918
        {
919
            buildvfs_fputstr(fp, "crosshaircolor ");
920
            char buf[64];
921
            snprintf(buf, sizeof(buf), "%d %d %d\n", CrosshairColors.r, CrosshairColors.g, CrosshairColors.b);
922
            buildvfs_fputstrptr(fp, buf);
923
        }
5387 terminx 924
 
4213 helixhorne 925
        OSD_WriteCvars(fp);
818 terminx 926
 
7359 hendricks2 927
        buildvfs_fclose(fp);
4213 helixhorne 928
 
7180 terminx 929
        OSD_Printf("Wrote %s\n", filename);
1740 helixhorne 930
 
845 terminx 931
        return;
813 terminx 932
    }
1625 terminx 933
 
7180 terminx 934
    OSD_Printf("Error writing %s: %s\n", filename, strerror(errno));
813 terminx 935
}
936
 
1820 terminx 937
void CONFIG_WriteSetup(uint32_t flags)
5 Plagman 938
{
563 terminx 939
    if (!ud.config.setupread) return;
112 terminx 940
 
563 terminx 941
    if (ud.config.scripthandle < 0)
5835 terminx 942
        ud.config.scripthandle = SCRIPT_Init(g_setupFileName);
5 Plagman 943
 
7180 terminx 944
    SCRIPT_PutNumber(ud.config.scripthandle, "Misc", "Executions", ud.executions, FALSE, FALSE);
150 terminx 945
 
6791 terminx 946
    SCRIPT_PutNumber(ud.config.scripthandle, "Setup", "CacheSize", MAXCACHE1DSIZE, FALSE, FALSE);
7180 terminx 947
    SCRIPT_PutNumber(ud.config.scripthandle, "Setup", "ConfigVersion", BYTEVERSION_EDUKE32, FALSE, FALSE);
7219 terminx 948
    SCRIPT_PutNumber(ud.config.scripthandle, "Setup", "ForceSetup", ud.setup.forcesetup, FALSE, FALSE);
949
    SCRIPT_PutNumber(ud.config.scripthandle, "Setup", "NoAutoLoad", ud.setup.noautoload, FALSE, FALSE);
1143 terminx 950
 
1660 terminx 951
#ifdef POLYMER
7293 terminx 952
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "Polymer", glrendmode == REND_POLYMER, FALSE, FALSE);
1660 terminx 953
#endif
1143 terminx 954
 
7219 terminx 955
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenBPP", ud.setup.bpp, FALSE, FALSE);
8063 terminx 956
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenDisplay", r_displayindex, FALSE, FALSE);
7219 terminx 957
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenHeight", ud.setup.ydim, FALSE, FALSE);
958
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenMode", ud.setup.fullscreen, FALSE, FALSE);
959
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "ScreenWidth", ud.setup.xdim, FALSE, FALSE);
1143 terminx 960
 
5830 terminx 961
    if (g_grpNamePtr && !g_addonNum)
7180 terminx 962
        SCRIPT_PutString(ud.config.scripthandle, "Setup", "SelectedGRP", g_grpNamePtr);
1820 terminx 963
 
6273 terminx 964
#ifdef STARTUP_SETUP_WINDOW
1820 terminx 965
    if (g_noSetup == 0)
7180 terminx 966
        SCRIPT_PutString(ud.config.scripthandle, "Setup", "ModDir", &g_modDir[0]);
2962 helixhorne 967
#endif
1820 terminx 968
    // exit early after only updating the values that can be changed from the startup window
969
    if (flags & 1)
970
    {
5835 terminx 971
        SCRIPT_Save(ud.config.scripthandle, g_setupFileName);
1820 terminx 972
        SCRIPT_Free(ud.config.scripthandle);
973
        return;
974
    }
975
 
7180 terminx 976
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "MaxRefreshFreq", maxrefreshfreq, FALSE, FALSE);
1346 terminx 977
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "WindowPosX", windowx, FALSE, FALSE);
978
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "WindowPosY", windowy, FALSE, FALSE);
7180 terminx 979
    SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "WindowPositioning", windowpos, FALSE, FALSE);
1143 terminx 980
 
5450 hendricks2 981
    if (!NAM_WW2GI)
1820 terminx 982
    {
983
        SCRIPT_PutNumber(ud.config.scripthandle, "Screen Setup", "Out",ud.lockout,FALSE,FALSE);
984
        SCRIPT_PutString(ud.config.scripthandle, "Screen Setup", "Password",ud.pwlockout);
985
    }
986
 
416 terminx 987
#ifdef _WIN32
1346 terminx 988
    SCRIPT_PutNumber(ud.config.scripthandle, "Updates", "CheckForUpdates", ud.config.CheckForUpdates, FALSE, FALSE);
989
    SCRIPT_PutNumber(ud.config.scripthandle, "Updates", "LastUpdateCheck", ud.config.LastUpdateCheck, FALSE, FALSE);
416 terminx 990
#endif
991
 
7219 terminx 992
    if (ud.setup.usemouse)
335 terminx 993
    {
7180 terminx 994
        for (int i=0; i<MAXMOUSEBUTTONS; i++)
1625 terminx 995
        {
7180 terminx 996
            if (CONFIG_FunctionNumToName(ud.config.MouseFunctions[i][0]))
5065 terminx 997
            {
7180 terminx 998
                Bsprintf(buf, "MouseButton%d", i);
999
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.MouseFunctions[i][0]));
5065 terminx 1000
            }
5 Plagman 1001
 
7180 terminx 1002
            if (i >= (MAXMOUSEBUTTONS-2)) continue;
5 Plagman 1003
 
7180 terminx 1004
            if (CONFIG_FunctionNumToName(ud.config.MouseFunctions[i][1]))
5065 terminx 1005
            {
7180 terminx 1006
                Bsprintf(buf, "MouseButtonClicked%d", i);
1007
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.MouseFunctions[i][1]));
5065 terminx 1008
            }
1625 terminx 1009
        }
606 terminx 1010
 
7180 terminx 1011
        for (int i=0; i<MAXMOUSEAXES; i++)
1625 terminx 1012
        {
7180 terminx 1013
            if (CONFIG_AnalogNumToName(ud.config.MouseAnalogueAxes[i]))
5065 terminx 1014
            {
7180 terminx 1015
                Bsprintf(buf, "MouseAnalogAxes%d", i);
1016
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_AnalogNumToName(ud.config.MouseAnalogueAxes[i]));
5065 terminx 1017
            }
1625 terminx 1018
        }
5 Plagman 1019
    }
1020
 
7219 terminx 1021
    if (ud.setup.usejoystick)
335 terminx 1022
    {
7180 terminx 1023
        for (int dummy=0; dummy<MAXJOYBUTTONSANDHATS; dummy++)
1625 terminx 1024
        {
5065 terminx 1025
            if (CONFIG_FunctionNumToName(ud.config.JoystickFunctions[dummy][0]))
1026
            {
7969 hendricks2 1027
                Bsprintf(buf, "ControllerButton%d", dummy);
5065 terminx 1028
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.JoystickFunctions[dummy][0]));
1029
            }
5 Plagman 1030
 
5065 terminx 1031
            if (CONFIG_FunctionNumToName(ud.config.JoystickFunctions[dummy][1]))
1032
            {
7969 hendricks2 1033
                Bsprintf(buf, "ControllerButtonClicked%d", dummy);
5065 terminx 1034
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.JoystickFunctions[dummy][1]));
1035
            }
1625 terminx 1036
        }
7180 terminx 1037
        for (int dummy=0; dummy<MAXJOYAXES; dummy++)
1625 terminx 1038
        {
5065 terminx 1039
            if (CONFIG_AnalogNumToName(ud.config.JoystickAnalogueAxes[dummy]))
1040
            {
7969 hendricks2 1041
                Bsprintf(buf, "ControllerAnalogAxes%d", dummy);
5065 terminx 1042
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_AnalogNumToName(ud.config.JoystickAnalogueAxes[dummy]));
1043
            }
5 Plagman 1044
 
5065 terminx 1045
            if (CONFIG_FunctionNumToName(ud.config.JoystickDigitalFunctions[dummy][0]))
1046
            {
7969 hendricks2 1047
                Bsprintf(buf, "ControllerDigitalAxes%d_0", dummy);
5065 terminx 1048
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.JoystickDigitalFunctions[dummy][0]));
1049
            }
5 Plagman 1050
 
5065 terminx 1051
            if (CONFIG_FunctionNumToName(ud.config.JoystickDigitalFunctions[dummy][1]))
1052
            {
7969 hendricks2 1053
                Bsprintf(buf, "ControllerDigitalAxes%d_1", dummy);
5065 terminx 1054
                SCRIPT_PutString(ud.config.scripthandle, "Controls", buf, CONFIG_FunctionNumToName(ud.config.JoystickDigitalFunctions[dummy][1]));
1055
            }
5 Plagman 1056
 
7969 hendricks2 1057
            Bsprintf(buf, "ControllerAnalogScale%d", dummy);
7341 terminx 1058
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.JoystickAnalogueScale[dummy], FALSE, FALSE);
5 Plagman 1059
 
7986 hendricks2 1060
            Bsprintf(buf, "ControllerAnalogInvert%d", dummy);
1061
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.JoystickAnalogueInvert[dummy], FALSE, FALSE);
1062
 
7969 hendricks2 1063
            Bsprintf(buf, "ControllerAnalogDead%d", dummy);
7341 terminx 1064
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.JoystickAnalogueDead[dummy], FALSE, FALSE);
5 Plagman 1065
 
7969 hendricks2 1066
            Bsprintf(buf, "ControllerAnalogSaturate%d", dummy);
7341 terminx 1067
            SCRIPT_PutNumber(ud.config.scripthandle, "Controls", buf, ud.config.JoystickAnalogueSaturate[dummy], FALSE, FALSE);
1625 terminx 1068
        }
5 Plagman 1069
    }
1070
 
1143 terminx 1071
    SCRIPT_PutString(ud.config.scripthandle, "Comm Setup","PlayerName",&szPlayerName[0]);
5065 terminx 1072
 
6668 hendricks2 1073
    SCRIPT_PutString(ud.config.scripthandle, "Comm Setup","RTSName",&ud.rtsname[0]);
495 terminx 1074
 
5065 terminx 1075
    char commmacro[] = "CommbatMacro# ";
1076
 
7180 terminx 1077
    for (int dummy = 0; dummy < MAXRIDECULE; dummy++)
88 terminx 1078
    {
5065 terminx 1079
        commmacro[13] = dummy+'0';
1080
        SCRIPT_PutString(ud.config.scripthandle, "Comm Setup",commmacro,&ud.ridecule[dummy][0]);
88 terminx 1081
    }
1082
 
5835 terminx 1083
    SCRIPT_Save(ud.config.scripthandle, g_setupFileName);
3636 terminx 1084
 
1085
    if ((flags & 2) == 0)
1086
        SCRIPT_Free(ud.config.scripthandle);
1087
 
5835 terminx 1088
    OSD_Printf("Wrote %s\n",g_setupFileName);
5387 terminx 1089
    CONFIG_WriteSettings();
2725 hendricks2 1090
    Bfflush(NULL);
5 Plagman 1091
}
1092
 
7993 hendricks2 1093
char const * CONFIG_GetGameFuncOnKeyboard(int gameFunc)
1094
{
7996 hendricks2 1095
    const char * string0 = KB_ScanCodeToString(ud.config.KeyboardKeys[gameFunc][0]);
1096
    return string0[0] == '\0' ? KB_ScanCodeToString(ud.config.KeyboardKeys[gameFunc][1]) : string0;
7993 hendricks2 1097
}
1098
 
1099
char const * CONFIG_GetGameFuncOnMouse(int gameFunc)
1100
{
1101
    for (int j = 0; j < 2; ++j)
1102
        for (int i = 0; i < joystick.numButtons; ++i)
1103
            if (ud.config.JoystickFunctions[i][j] == gameFunc)
1104
                return joyGetName(1, i);
1105
 
1106
    for (int i = 0; i < joystick.numAxes; ++i)
1107
        for (int j = 0; j < 2; ++j)
1108
            if (ud.config.JoystickDigitalFunctions[i][j] == gameFunc)
1109
                return joyGetName(0, i);
1110
 
1111
    return "";
1112
}
1113
 
1114
char const * CONFIG_GetGameFuncOnJoystick(int gameFunc)
1115
{
1116
    for (int j = 0; j < 2; ++j)
1117
        for (int i = 0; i < joystick.numButtons; ++i)
1118
            if (ud.config.JoystickFunctions[i][j] == gameFunc)
1119
                return joyGetName(1, i);
1120
 
1121
    for (int i = 0; i < joystick.numAxes; ++i)
1122
        for (int j = 0; j < 2; ++j)
1123
            if (ud.config.JoystickDigitalFunctions[i][j] == gameFunc)
1124
                return joyGetName(0, i);
1125
 
1126
    return "";
1127
}
1128
 
5470 hendricks2 1129
static const char *CONFIG_GetMapEntryName(char m[], char const * const mapname)
322 terminx 1130
{
5193 helixhorne 1131
    strcpy(m, mapname);
322 terminx 1132
 
5193 helixhorne 1133
    char *p = strrchr(m, '/');
322 terminx 1134
    if (!p) p = strrchr(m, '\\');
5193 helixhorne 1135
    if (p) Bmemmove(m, p, Bstrlen(p)+1);
1229 terminx 1136
    for (p=m; *p; p++) *p = tolower(*p);
322 terminx 1137
 
1076 terminx 1138
    // cheap hack because SCRIPT_GetNumber doesn't like the slashes
1139
    p = m;
1140
    while (*p == '/') p++;
1141
 
5193 helixhorne 1142
    return p;
1143
}
1144
 
5470 hendricks2 1145
static void CONFIG_GetMD4EntryName(char m[], uint8_t const * const md4)
5193 helixhorne 1146
{
5470 hendricks2 1147
    sprintf(m, "MD4_%08x%08x%08x%08x",
1148
            B_BIG32(B_UNBUF32(&md4[0])), B_BIG32(B_UNBUF32(&md4[4])),
1149
            B_BIG32(B_UNBUF32(&md4[8])), B_BIG32(B_UNBUF32(&md4[12])));
1150
}
1151
 
1152
int32_t CONFIG_GetMapBestTime(char const * const mapname, uint8_t const * const mapmd4)
1153
{
6273 terminx 1154
    if (!ud.config.setupread || ud.config.scripthandle < 0)
1155
        return -1;
322 terminx 1156
 
5470 hendricks2 1157
    char m[37];
7180 terminx 1158
 
5470 hendricks2 1159
    CONFIG_GetMD4EntryName(m, mapmd4);
5193 helixhorne 1160
 
1161
    int32_t t = -1;
5470 hendricks2 1162
    if (SCRIPT_GetNumber(ud.config.scripthandle, "MapTimes", m, &t))
1163
    {
1164
        // fall back to map filenames
1165
        char m2[BMAX_PATH];
7180 terminx 1166
        auto p = CONFIG_GetMapEntryName(m2, mapname);
1167
 
5470 hendricks2 1168
        SCRIPT_GetNumber(ud.config.scripthandle, "MapTimes", p, &t);
1169
    }
7180 terminx 1170
 
322 terminx 1171
    return t;
1172
}
1173
 
7180 terminx 1174
int CONFIG_SetMapBestTime(uint8_t const * const mapmd4, int32_t tm)
322 terminx 1175
{
7180 terminx 1176
    if (ud.config.scripthandle < 0 && (ud.config.scripthandle = SCRIPT_Init(g_setupFileName)) < 0)
1177
        return -1;
322 terminx 1178
 
5470 hendricks2 1179
    char m[37];
7180 terminx 1180
 
5470 hendricks2 1181
    CONFIG_GetMD4EntryName(m, mapmd4);
7180 terminx 1182
    SCRIPT_PutNumber(ud.config.scripthandle, "MapTimes", m, tm, FALSE, FALSE);
5193 helixhorne 1183
 
322 terminx 1184
    return 0;
1185
}
1186