View Single Post
Bitz and Bytes , saving with fWRITE
Old
  (#1)
stefanhendriks
RealBot Author
 
stefanhendriks's Avatar
 
Status: Offline
Posts: 3,088
Join Date: Nov 2003
Location: Netherlands
Default Bitz and Bytes , saving with fWRITE - 21-01-2004

I got some code while back ago from cheesemonster about writing my huge leap of data using BITS. So eventually it was not 33MB, but at max only 2 or 3 MB.

I saw at the rb forums the bots cause trouble with big experience files, and i think the function being bad here is 'fwrite'. I probably try to write to much , but there is a problem... i cannot eliminate or reduce the data. I bet i need to chop it in 2 then, but here comes my problem, i don't know how to do that.

I have checked some code first of course, but currently i have no f*cking clue if it works okay when chopping the data in 2. I probably need to create another unassigned char, with half the size of my actual 'big' array (cVisTable), then when saving, i have to assign a first piece of data on the chunk, save it, and then do the same for the other half. In theory this sounds great, but id on't know how to code this yet.

FYI, i have posted here some functions:

Code:
//---------------------------------------------------------
//CODE: CHEESEMONSTER
bool cNodeMachine::GetVisibilityFromTo ( int iFrom, int iTo )	   
{ 
	// -- BY STEFAN --
	if (iVisChecked[iFrom] == 0)
		return NULL; // we have no clue
	// -- END --
	// was int
	// work out the position  
	long iPosition = (iFrom*MAX_NODES)+iTo; 
	
	long iByte = (int)(iPosition/8); 
	unsigned int iBit = iPosition%8; 
	
	if ( iByte < g_iMaxVisibilityByte ) 
	{				   
		// Get the Byte that this is in 
		unsigned char *ToReturn = (cVisTable+iByte); 
		// get the bit in the byte 
		return ( (*ToReturn & (1<<iBit)) > 0 ); 
	} 
	
	return false; 
} 
void cNodeMachine::SetVisibilityFromTo ( int iFrom, int iTo, bool bVisible ) 
{ 
	// -- STEFAN --
	iVisChecked[iFrom] = 1; // WE HAVE CHECKED THIS ONE
	// -- END --
		
	// was int
	long iPosition = (iFrom*MAX_NODES)+iTo; 
	
	long iByte = (int)(iPosition/8); 
	unsigned int iBit = iPosition%8; 
	
	if ( iByte < g_iMaxVisibilityByte ) 
	{ 
		unsigned char *ToChange = (cVisTable+iByte); 
		
		if ( bVisible ) 
			*ToChange |= (1<<iBit); 
		else 
			*ToChange &= ~(1<<iBit); 
	} 
} 
void cNodeMachine::ClearVisibilityTable ( void ) 
{ 
	if ( cVisTable ) 
		memset(cVisTable,0,g_iMaxVisibilityByte);
} 
void cNodeMachine::FreeVisibilityTable( void )
{
	if ( cVisTable )
		free(cVisTable);
}
//---------------------------------------------------------
 
// Save Experience
void cNodeMachine::experience_save()
{
  char dirname[256];
  char filename[256];
  int i;
  // Set Directory name
  strcpy(dirname, "data/cstrike/exp/");
  strcat(dirname, STRING(gpGlobals->mapname));
  strcat(dirname, ".rbx"); // nodes file
  // writes whole path into "filename", Linux compatible
  UTIL_BuildFileNameRB(dirname, filename);
  FILE *rbl;
  // Only save if lock type is < 1
  rbl = fopen(filename, "wb");
  if (rbl != NULL)
  {
	int iVersion = FILE_EXP_VER1;
	fwrite(&iVersion, sizeof(int), 1, rbl);
   
	for (i=0; i < MAX_NODES; i++)
 { 
  fwrite(&InfoNodes[i].fDanger[0], sizeof(Vector), 1, rbl);
		fwrite(&InfoNodes[i].fDanger[1], sizeof(Vector), 1, rbl);
		fwrite(&InfoNodes[i].fContact[0], sizeof(Vector), 1, rbl);
		fwrite(&InfoNodes[i].fContact[1], sizeof(Vector), 1, rbl);
	}
	if (iMaxUsedNodes > MAX_NODES) iMaxUsedNodes=MAX_NODES;
	
	// Here write down the MAX amounts of nodes used from vis table!
	unsigned long iSize = (iMaxUsedNodes*MAX_NODES)/8;
	fwrite(&iMaxUsedNodes, sizeof(int), 1, rbl);
	// Write down the vis-table
	fwrite(cVisTable, iSize, 1, rbl);
	// write down the checked vis
	fwrite(iVisChecked, sizeof(iVisChecked), 1, rbl);
	fclose(rbl);
  }
}
// Load Danger
void cNodeMachine::experience_load()
{
  char dirname[256];
  char filename[256];
  int i;
  // Set Directory name
  strcpy(dirname, "data/cstrike/exp/");
  strcat(dirname, STRING(gpGlobals->mapname));
  strcat(dirname, ".rbx"); // nodes file
  // writes whole path into "filename", Linux compatible
  UTIL_BuildFileNameRB(dirname, filename);
  FILE *rbl;  
  rbl = fopen(filename, "rb");
  if (rbl != NULL)
  {
	int iVersion=FILE_EXP_VER1;
	fread(&iVersion, sizeof(int), 1, rbl);
	if (iVersion == FILE_EXP_VER1)
	{
		for (i=0; i < MAX_NODES; i++)
		{ 
			fread(&InfoNodes[i].fDanger[0], sizeof(Vector), 1, rbl);
			fread(&InfoNodes[i].fDanger[1], sizeof(Vector), 1, rbl);
			fread(&InfoNodes[i].fContact[0], sizeof(Vector), 1, rbl);
			fread(&InfoNodes[i].fContact[1], sizeof(Vector), 1, rbl);
		}
		fread(&iMaxUsedNodes, sizeof(int), 1, rbl);
		// make sure we never exceed the limit
		if (iMaxUsedNodes > MAX_NODES) iMaxUsedNodes=MAX_NODES;
		unsigned long iSize = (iMaxUsedNodes*MAX_NODES)/8;
		// Read table from what we know
		fread(cVisTable, iSize, 1, rbl);
		fread(iVisChecked, sizeof(iVisChecked), 1, rbl);
	}
		fclose(rbl);
  }
}


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me
  
Reply With Quote