You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bomberclone/src/configuration.c

486 lines
13 KiB

/* configuration */
#include <SDL.h>
#include "basic.h"
#include "bomberclone.h"
#include "network.h"
#include "packets.h"
#include "gfx.h"
#include "chat.h"
#include "sound.h"
void
game_init (int argc, char **argv)
{
int i;
srand (((int) time (NULL))); // initialize randomgenerator
// do some init stuff
for (i = 0; i < MAX_SERVERENTRYS; i++)
bman.serverlist[i].name[0] = 0;
for (i = 0; i < MAX_PLAYERS; i++) {
bman.players[i].gfx = NULL; /* we will select them in the wait_for_players loop */
bman.players[i].gfx_nr = -1; /* and even now in the singleplayer menu */
}
stonelist_del ();
chat.visible = 0;
chat.startline = 0;
keybinput_new (&chat.input);
for (i = 0; i < CHAT_MAX_LINES; i++)
chat.lines[i].text[0] = 0;
bman.maxplayer = MAX_PLAYERS;
bman.net_ai_family = PF_INET;
bman.sock = -1;
bman.gamename[0] = 0;
sprintf (bman.port, "%d", DEFAULT_UDPPORT);
sprintf (bman.gamemaster, DEFAULT_GAMEMASTER);
resend_cache.data = NULL;
resend_cache.fill = -1;
bman.notifygamemaster = 1;
bman.askplayername = 1;
debug = 0;
gfx.res.x = 640;
gfx.res.y = 480;
gfx.bpp = 16;
map.tileset[0] = 0;
map.random_tileset = 1;
map.size.x = 25;
map.size.y = 17;
sprintf (bman.datapath, PACKAGE_DATA_DIR);
printf ("DataPath: %s\n", bman.datapath);
map.map[0] = 0;
map.map_selection = 2;
bman.firewall = 0;
bman.broadcasted_chat = 1;
bman.ai_players = 1;
snd.inited = 0;
snd.audio_rate = 22050;
snd.audio_format = AUDIO_S16;
snd.audio_channels = 2;
snd.playmusic = 1;
snd.playsound = 1;
map.bombs = GAME_SPECIAL_ITEMBOMB;
map.fire = GAME_SPECIAL_ITEMFIRE;
map.shoes = GAME_SPECIAL_ITEMSHOE;
map.mixed = GAME_SPECIAL_ITEMMIXED;
map.death = GAME_SPECIAL_ITEMDEATH;
map.sp_trigger = GAME_SPECIAL_ITEMSTRIGGER;
map.sp_row = GAME_SPECIAL_ITEMSROW;
map.sp_push = GAME_SPECIAL_ITEMSPUSH;
d_printf ("\n\n ***** Bomberclone Version %s \n\n",VERSION);
if (ReadConfig()) {
gfx_init ();
draw_logo ();
menu_get_text ("Your Playername", bman.playername, LEN_PLAYERNAME);
bman.playername[LEN_PLAYERNAME-1] = 0;
} else {
gfx_init ();
draw_logo ();
if (bman.askplayername)
menu_get_text ("Your Playername", bman.playername, LEN_PLAYERNAME);
else menu_clearkeybuff();
}
ReadPrgArgs (argc, argv);
snd_init ();
};
int
ReadConfig ()
{
FILE *config;
char buf[1024],
key2[1024];
char *findit,
*keyword,
*value;
int i;
char filename[512];
#ifdef _WIN32
sprintf (filename, "%sbomberclone.cfg", s_gethomedir ());
#else
sprintf (filename, "%s.bomberclone.cfg", s_gethomedir ());
#endif
config = fopen (filename, "r");
if (config == NULL) {
d_printf ("Error: Config file not found!\n");
return -1;
}
d_printf ("Reading Config-file: %s", filename);
while (fgets (buf, sizeof (buf), config) != NULL) {
findit = strchr (buf, '\n');
if (findit)
findit[0] = '\0';
if (buf[0] == '\0')
continue;
keyword = buf;
while (isspace (*keyword))
keyword++;
value = strchr (buf, '=');
if (value == NULL)
continue;
*value = 0;
value++;
while (*value == ' ')
value++;
while (keyword[strlen (keyword) - 1] == ' ')
keyword[strlen (keyword) - 1] = 0;
while (value[strlen (value) - 1] == ' ')
value[strlen (value) - 1] = 0;
if (strlen (value) == 0)
continue;
for (i = 0; i < (int) strlen (keyword); i++)
keyword[i] = tolower (keyword[i]);
if (!strcmp (keyword, "playername")) {
if (strlen (value) > LEN_PLAYERNAME) {
d_printf
("*** Error - playername too long (maximum size permitted is %d characters)!\n\n",
LEN_PLAYERNAME);
}
value[LEN_PLAYERNAME - 1] = 0;
strcpy (bman.playername, value);
}
if (!strcmp (keyword, "gamename")) {
if (strlen (value) > LEN_GAMENAME) {
d_printf
("*** Error - servername too long (maximum size permitted is %d characters)!\n\n",
LEN_GAMENAME);
}
value[LEN_GAMENAME - 1] = 0;
strcpy (bman.gamename, value);
}
if (!strcmp (keyword, "askplayername")) {
bman.askplayername = atoi (value);
}
if (!strcmp (keyword, "resolutionx")) {
gfx.res.x = atoi (value);
}
if (!strcmp (keyword, "resolutiony")) {
gfx.res.y = atoi (value);
}
if (!strcmp (keyword, "tileset")) {
strcpy (map.tileset, value);
}
if (!strcmp (keyword, "mapname")) {
if (strlen (value) > LEN_PATHFILENAME) {
d_printf
("*** Error - fieldpath too long (maximum size permitted is %d characters)!\n\n",
LEN_PATHFILENAME);
}
value[511] = 0;
strcpy(map.map,value);
}
if (!strcmp (keyword, "firewall")) {
bman.firewall = atoi (value);
}
if (!strcmp (keyword, "broadcasted_chat")) {
bman.broadcasted_chat = atoi (value);
}
if (!strcmp (keyword, "udpport")) {
if (strlen (value) > LEN_PORT) {
d_printf
("*** Error - servername too long (maximum size permitted is %d characters)!\n\n",
LEN_PORT);
}
value[LEN_PORT - 1] = 0;
strcpy (bman.port, value);
}
if (!strcmp (keyword, "ai_players")) {
bman.ai_players = atoi (value);
}
if (!strcmp (keyword, "fieldsizex")) {
map.size.x = atoi (value);
}
if (!strcmp (keyword, "fieldsizey")) {
map.size.y = atoi (value);
}
if (!strcmp (keyword, "fullscreen")) {
gfx.fullscreen = atoi (value);
}
if (!strcmp (keyword, "bitsperpixel")) {
gfx.bpp = atoi (value);
}
if (!strcmp (keyword, "ai_family")) {
bman.net_ai_family = atoi (value);
}
if (!strcmp (keyword, "debug")) {
debug = atoi (value);
}
if (!strcmp (keyword, "notify")) {
bman.notifygamemaster = atoi (value);
}
if (!strcmp (keyword, "masterserver")) {
strcpy (bman.gamemaster, value);
}
if (!strcmp (keyword, "maxplayer")) {
bman.maxplayer = atoi (value);
}
if (!strcmp (keyword, "mapselection")) {
map.map_selection = atoi (value);
}
if (!strcmp (keyword, "randomtileset")) {
map.random_tileset = atoi (value);
}
if(!strcmp (keyword, "sndrate")) {
snd.audio_rate = atoi (value);
}
if (!strcmp (keyword, "sndchannels")) {
snd.audio_channels = atoi (value);
}
if (!strcmp (keyword, "sndformat")) {
snd.audio_format = atoi (value);
}
if (!strcmp (keyword, "sndplaymusic")) {
snd.playmusic = atoi (value);
}
if (!strcmp (keyword, "sndplaysound")) {
snd.playsound = atoi (value);
}
for (i = 0; i < MAX_SERVERENTRYS; i++) {
sprintf (key2, "ip%d", i);
if (!strcmp (keyword, key2)) {
strcpy (bman.serverlist[i].name, value);
}
}
}
fclose (config);
return 0;
}
int
WriteConfig ()
{
FILE *config;
int i;
char filename[512];
#ifdef _WIN32
sprintf (filename, "%sbomberclone.cfg", s_gethomedir ());
#else
sprintf (filename, "%s.bomberclone.cfg", s_gethomedir ());
#endif
if ((config = fopen (filename, "w")) == NULL)
return -1;
fprintf (config, "resolutionx=%d\n", gfx.res.x);
fprintf (config, "resolutiony=%d\n", gfx.res.y);
fprintf (config, "fullscreen=%d\n", gfx.fullscreen);
fprintf (config, "tileset=%s\n", map.tileset);
fprintf (config, "mapname=%s\n", map.map);
fprintf (config, "firewall=%d\n", bman.firewall);
fprintf (config, "broadcasted_chat=%d\n", bman.broadcasted_chat);
fprintf (config, "udpport=%s\n", bman.port);
fprintf (config, "ai_players=%d\n", bman.ai_players);
fprintf (config, "fieldsizex=%d\n", map.size.x);
fprintf (config, "fieldsizey=%d\n", map.size.y);
fprintf (config, "notify=%d\n", bman.notifygamemaster);
fprintf (config, "ai_family=%d\n", bman.net_ai_family);
fprintf (config, "masterserver=%s\n", bman.gamemaster);
fprintf (config, "gamename=%s\n", bman.gamename);
fprintf (config, "maxplayer=%d\n", bman.maxplayer);
for (i = 0; i < MAX_SERVERENTRYS; i++)
fprintf (config, "ip%d=%s\n", i, bman.serverlist[i].name);
fprintf (config, "debug=%d\n", debug);
fprintf (config, "askplayername=%d\n", bman.askplayername);
fprintf (config, "playername=%s\n", bman.playername);
fprintf (config, "bitsperpixel=%d\n", gfx.bpp);
fprintf (config, "randomtileset=%d\n", map.random_tileset);
fprintf (config, "mapselection=%d\n", map.map_selection);
fprintf (config, "sndrate=%d\n", snd.audio_rate);
fprintf (config, "sndchannels=%d\n", snd.audio_channels);
fprintf (config, "sndformat=%d\n", snd.audio_format);
fprintf (config, "sndplaymusic=%d\n", snd.playmusic);
fprintf (config, "sndplaysound=%d\n", snd.playsound);
fclose (config);
return 0;
}
void
change_res ()
{
int menuselect = 0;
_menu menu[] = {
{1, "Full Screen"},
{1, "640x480"},
{2, "800x600"},
{3, "1024x768"},
{4, "1280x1024"},
{5, "BPP"},
{6, "Return To Configuration Menu"},
{-1, ""}
};
while (1) {
if (gfx.fullscreen)
sprintf (menu[0].text, "Disable Fullscreen");
else
sprintf (menu[0].text, "Enable Full Screen");
if (gfx.bpp == 16)
sprintf (menu[5].text, "16 Bit Per Pixel");
else if (gfx.bpp == 24)
sprintf (menu[5].text, "24 Bit Per Pixel");
else
sprintf (menu[5].text, "32 Bit Per Pixel");
menuselect = menu_loop ("Video Options", menu, menuselect);
switch (menuselect) {
case (0): // Fullscreen
if (gfx.fullscreen)
gfx.fullscreen = 0;
else
gfx.fullscreen = 1;
break;
case (1): // 640x480
gfx.res.x = 640;
gfx.res.y = 480;
break;
case (2): // 800x600
gfx.res.x = 800;
gfx.res.y = 600;
break;
case (3): // 1024x768
gfx.res.x = 1024;
gfx.res.y = 768;
break;
case (4): // 1280x1024
gfx.res.x = 1280;
gfx.res.y = 1024;
break;
case (5):
if (gfx.bpp == 16)
gfx.bpp = 24;
else if (gfx.bpp == 24)
gfx.bpp = 32;
else
gfx.bpp = 16;
break;
case (6): // Return
menuselect = -1;
break;
}
if (menuselect != -1) {
gfx_shutdown ();
gfx_init ();
}
else
return;
}
};
void
configuration ()
{
int menuselect = 0;
_menu menu[] = {
{1, "Player Name:"},
{1, "Video Options"},
{2, "Sound Options"},
{3, "Map Options"},
{4, "Customize Keyboard"},
{5, "Prompt For Player Name"},
{6, "Debug"},
{7, "Save Config"},
{8, "Return To Main Manu"},
{-1, ""}
};
while (menuselect != -1) {
sprintf (menu[0].text, "Player Name: %s", bman.playername);
if (bman.askplayername == 1)
sprintf (menu[5].text, "Prompt For Name: YES");
else
sprintf (menu[5].text, "Prompt For Name: NO");
if (debug == 1)
sprintf (menu[6].text, "Debug Messages ON");
else
sprintf (menu[6].text, "Debug Messages OFF");
menuselect = menu_loop ("Configuration", menu, menuselect);
switch (menuselect) {
case (0): // Playername
menu_get_text ("Enter Playername", bman.playername, LEN_PLAYERNAME - 1);
bman.playername[LEN_PLAYERNAME - 1] = 0;
break;
case (1): // Screen Options
change_res ();
break;
case (2): // Sound Options
snd_options ();
break;
case (3): // Map Options
mapmenu();
break;
case (4): // Customize Keyboard
break;
case (5): // Prompt For Player Name
if (bman.askplayername == 1)
bman.askplayername = 0;
else
bman.askplayername = 1;
break;
case (6): // Debugging On / Off
if (debug == 1)
debug = 0;
else {
debug = 1;
d_printf ("BomberClone ver.%s\n", VERSION);
}
break;
case (7): // Save Configuration
WriteConfig ();
break;
case (8): // Return to main menu
menuselect = -1;
break;
}
}
};
void
ReadPrgArgs (int argc, char **argv)
{
int i = 0;
while (argv[++i] != NULL) {
if (!strcmp (argv[i], "-port"))
strncpy (bman.port, argv[++i], LEN_PORT);
}
};