panthema / 2006 / SDIOS06 / sdios06 / src / sdljump / src / records.c (Download File)
/*
 * SDLjump
 * (C) 2005 Juan Pedro BolĂ­var Puente
 * 
 * This simple but addictive game is based on xjump. Thanks for its author for
 * making such a great game :-)
 * 
 * records.c
 */

/*
    Copyright (C) 2003-2004, Juan Pedro Bolivar Puente

    SDLjump is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    SDLjump is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SDLjump; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "sdljump.h"
#include "records.h"
#include "tools.h"

extern L_gblOptions gblOps;

int loadRecords(char* fname, records_t* rtab)
{
	FILE* fh = NULL;
	int i;
	
#if 0
	if ((fh = fopen(fname,"r"))==NULL) {
        printf("\n Error while opening records file (%s) \n",fname);
        return FALSE;
	}
	
	for (i = 0; i < MAX_RECORDS; i++) {
		rtab[i].pname = getValue_charp(fh,"player_name");
		rtab[i].floor = getValue_int(fh,"floor");
		rtab[i].mode = getValue_charp(fh,"mode");
		rtab[i].time = getValue_int(fh,"time");
	}
	
	fclose(fh);
#endif
	return TRUE;
}

int writeRecords(char* fname, records_t* rtab)
{
	FILE* fh;
	int i;

#if 0	
	if ((fh = fopen(fname,"w"))==NULL) {
        printf("\n Error while opening records file (%s) \n",fname);
        return FALSE;
	}
	
	putComment(fh,"This file has been automatically generated by SDLjump");
	putLine(fh);
	
	for (i = 0; i< MAX_RECORDS; i++) {
		putValue_str(fh, "player_name", rtab[i].pname);
		putValue_int(fh, "floor", rtab[i].floor);
		putValue_str(fh, "mode", rtab[i].mode);
		putValue_int(fh, "time", rtab[i].time);
		putLine(fh);
	}
	
	fclose (fh);
#endif
	return TRUE;
}

int addRecord(records_t* rtab, records_t* rec, int pos)
{
	int j;
	
	pos--;
	free(rtab[MAX_RECORDS-1].pname);
	free(rtab[MAX_RECORDS-1].mode);
	for (j = MAX_RECORDS-1; j >= pos; j--) {
		rtab[j] = rtab[j-1];
	}
	
	rtab[pos] = *rec;

	return FALSE;
}

int checkRecord (records_t* rtab, int floor)
{
	int i;
	for (i=0; i<MAX_RECORDS; i++)
	{
		if (rtab[i].floor <= floor) {
			return i+1;
		}
	}
    return FALSE;	
}

void makeRecord(records_t* rec, char* name, int floor, int time)
{
	rec->pname = NULL;
	rec->pname = malloc(sizeof(char)*(strlen(name)+1));
	strcpy(rec->pname, name);
	rec->floor = floor;
	
	rec->mode = NULL;
	rec->mode = malloc(sizeof(char)*4);
	if (gblOps.fps == 40) {
		rec->mode[0] = 'x';
	} else
		rec->mode[0] = 's';
	
	switch (gblOps.rotMode) {
		case ROTNONE:
			rec->mode[1] = 'n';
			break;
		case ROTORIG:
			rec->mode[1] = 'x';
			break;
		case ROTFULL:
			rec->mode[1] = 's';
			break;
		default: break;
	}
	
	switch (gblOps.scrollMode) {
		case HARDSCROLL:
			rec->mode[2] = 'x';
			break;
		case SOFTSCROLL:
			rec->mode[2] = 's';
		default:
			break;
	}
	rec->mode[3] = '\0';
	
	rec->time = time;
}

void defaultRecords(records_t* rtab)
{
	int i;
	for (i = 0; i < MAX_RECORDS; i++) {
		makeRecord(&rtab[i], "RasKolniKoV", (MAX_RECORDS-i)*3, rnd(100));
	}
}

void freeRecords(records_t* rtab)
{
	int i;
	for (i = 0; i < MAX_RECORDS; i++) {
		free (rtab[i].pname);
		free (rtab[i].mode);
	}
}