
/* Generated by Interface Builder */

#import "PreferencesBrain.h"
#import "InfoController.h"
#import "GameView.h"
#import <stdio.h>
#import <string.h>
#import <appkit/Button.h>

@implementation PreferencesBrain

- init		// designated initializer sets up game variables
{		// to sensible values.    
    defaultPlayerName = malloc(64);
    version = malloc(8);
    return self;
}

/* methods to get at important variables */
- (int)speed { return speed; }
- (BOOL)effects { return effects; }
- (BOOL)music { return music; }
- (BOOL)alert { return alert; }
- (BOOL)autoUnPause { return autoUnPause; }
- (BOOL)autoStart { return autoStart; }
- (BOOL)demoSound {return demoSound; }
- setAlert:(BOOL)al { alert = al; return self; }
- setAutoUnPause:(BOOL)al { autoUnPause = al; return self; }
- setAutoStart:(BOOL)al { autoStart = al; return self; }

- readDefaults:sender		// get preferences from defaults database
{
// I would be smarter to use a defaults structure to grab all the defaults
// at once.  Why haven't I?  This object has been through tons of changes,
// and that's one of the lower priorities; it would speed up launch a little
// bit, but not any significant amount....
    const char *tmpstr;
    int tempInt;
    
    tmpstr = NXGetDefaultValue ([NXApp appName], "SoundOn");
    if (!tmpstr) tempInt = NO;
    else sscanf(tmpstr, "%d", &tempInt);
    if (tempInt) effects = YES; else effects = NO;
    
    tmpstr = NXGetDefaultValue ([NXApp appName], "Alerts");
    if (!tmpstr) alert = YES;
    else {
	sscanf(tmpstr, "%d", &tempInt);
	if (tempInt) alert = YES; else alert = NO;
    }

    tmpstr = NXGetDefaultValue ([NXApp appName], "AutoUnPause");
    if (!tmpstr) autoUnPause = YES;
    else {
	sscanf(tmpstr, "%d", &tempInt);
	if (tempInt) autoUnPause = YES; else autoUnPause = NO;
    }

    tmpstr = NXGetDefaultValue ([NXApp appName], "AutoStart");
    if (!tmpstr) autoStart = YES;
    else {
	sscanf(tmpstr, "%d", &tempInt);
	if (tempInt) autoStart = YES; else autoStart = NO;
    }

    tmpstr = NXGetDefaultValue ([NXApp appName], "DemoSound");
    if (!tmpstr) demoSound = YES;
    else {
	sscanf(tmpstr, "%d", &tempInt);
	if (tempInt) demoSound = YES; else demoSound = NO;
    }

    tmpstr = NXGetDefaultValue ([NXApp appName], "MusicOn");
    if (!tmpstr) tempInt = NO;
    else sscanf(tmpstr, "%d", &tempInt);
    if (tempInt) music = YES; else music = NO;
    
    tmpstr = NXGetDefaultValue ([NXApp appName], "GameSpeed");
    if (!tmpstr) speed = 1;
    else sscanf(tmpstr, "%d", &speed);
    if ((speed < 0) || (speed > 3)) speed = 1;

    tmpstr = NXGetDefaultValue ([NXApp appName], "PlayerName");
    if (tmpstr == NULL) strcpy(defaultPlayerName,"MysteryPlayer");
    else strcpy(defaultPlayerName,tmpstr); 
    
    tmpstr = NXGetDefaultValue ([NXApp appName], "Version");
    if (tmpstr == NULL) strcpy(version,"0.0");
    else strcpy(version,tmpstr); 
    
    [gameScreen getPreferences];
    return self;
}

- writeDefaults:sender		// save preferences in defaults database
{
	// This is a dumb way to do it, as above, since a defaults structure
	// is much more efficient.  See note for above method.
    char str[32];
    int tempInt;
    
    tempInt = 0; if (alert) tempInt = 1;
    sprintf(str, "%d\0", tempInt);
    NXWriteDefault ([NXApp appName], "Alerts", str);
    tempInt = 0; if (autoStart) tempInt = 1;
    sprintf(str, "%d\0", tempInt);
    NXWriteDefault ([NXApp appName], "AutoStart", str);
    tempInt = 0; if (autoUnPause) tempInt = 1;
    sprintf(str, "%d\0", tempInt);
    NXWriteDefault ([NXApp appName], "AutoUnPause", str);
    tempInt = 0; if (demoSound) tempInt = 1;
    sprintf(str, "%d\0", tempInt);
    NXWriteDefault ([NXApp appName], "DemoSound", str);
    tempInt = 0; if (music) tempInt = 1;
    sprintf(str, "%d\0", tempInt);
    NXWriteDefault ([NXApp appName], "MusicOn", str);
    tempInt = 0; if (effects) tempInt = 1;
    sprintf(str, "%d\0", tempInt);
    NXWriteDefault ([NXApp appName], "SoundOn", str);
    sprintf(str, "%d\0", speed);  
    NXWriteDefault ([NXApp appName], "GameSpeed", str);
    NXWriteDefault ([NXApp appName], "PlayerName", defaultPlayerName);
    NXWriteDefault ([NXApp appName], "Version", version);
    
    return self;
}

- (BOOL)firstTimeCheck // first time running this version?
{
	char *str;
	
	
	if (strcmp(version, versionString)) { // first time!
		str = malloc(128);		// Put up a welcome Panel
		sprintf(str, "Welcome to PacMan %s!  I hope you like it!",
			versionString);
		strcpy(version, versionString);
		NXRunAlertPanel("Welcome", str,
			"Let's Play", NULL, NULL);
		free(str);
		if (speed == 2) speed = 3; // compatability with 1.0;
			// this will be removed in 2.0...it keeps the original "fast"
			// Preference playing the same (moves it over to "hyper")
		return YES;
	}
	return NO;
}

- revert:sender	// return to default values
{
	effects = NO;
	alert = YES;
	autoUnPause = YES;
	autoStart = YES;
	demoSound = NO;
	music = NO;
	[gameScreen revertBackground:sender];
	speed = 1;
	// I'm not reverting "defaultPlayerName"; doesn't seem necessary
    
	[gameScreen getPreferences];   // make sure that the gameScreen is aware
	return [self preferences:self]; // update the panel
}

- speedChange:sender			// accept changes in speed
{
    speed = [sender selectedTag];
    return self;
}

- preferences:sender		// sets controls on pref panel & brings it up
{
    [self preferencesPanel]; // be sure panel is loaded; throw away return val.
	[speedButtons selectCellWithTag:speed];
    [musicButtons selectCellWithTag:music];
    [effectButtons selectCellWithTag:effects];
    [alertSwitch setState:alert];
    [autoStartSwitch setState:autoStart];
    [autoUnPauseSwitch setState:autoUnPause];
    [demoSwitch setState:demoSound];
    [[self preferencesPanel] orderFront:self];
    return self;
}

- preferencesPanel	// return the preferencesPanel, load it if needed.
{
  if( !preferencesPanel)
    [NXApp loadNibSection:"PreferencesPanel.nib" owner:self withNames:NO];
  return preferencesPanel;
}

- musicChange:sender		// turn music on/off
{
    music = [sender selectedTag];
    if (music) [gameScreen musicOn];
    else [gameScreen musicOff];
    return self;
}

- effectsChange:sender		// turn sound effects on/off
{
    effects = [sender selectedTag];
    if (effects) [gameScreen soundOn];
    else [gameScreen soundOff];
    return self;
}

- setDefaultPlayerName:(char *)aString	// change default player name
{
    defaultPlayerName = aString;
    return self;
}

- (const char *)defaultPlayerName  // return ptr to defaultPlayerName string
{
    return defaultPlayerName;
}

- alertChange:sender
{
    alert = NO;
    if ([sender state]) alert = YES;
    return self;
}

- unPauseChange:sender
{
    autoUnPause = NO;
    if ([sender state]) autoUnPause = YES;
    return self;
}

- autoStartChange:sender
{
    autoStart = NO;
    if ([sender state]) autoStart = YES;
    return self;
}

- demoSoundChange:sender	// turn off sound only in demo
{
    demoSound = NO;
    if ([sender state]) demoSound = YES;
    return self;
}
 
- setBack:sender		// set the background image
{ // forward the message to the game screen
    return [gameScreen changeBackground:sender];
}

- revertBack:sender		// revert to default background image
{ // forward the message to the game screen
    return [gameScreen revertBackground:sender];
}

- back1:sender		// set black background - forward to gamescreen
{
    return [gameScreen back1:sender];
}

- back2:sender		// set solid.tiff background
{ // - forward to gamescreen
    return [gameScreen back2:sender];
}

- back3:sender		// set Lyapunov.tiff background
{ // - forward to gamescreen
    return [gameScreen back3:sender];
}

@end
