Subversion Repositories eduke32

Rev

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