mirror of
https://github.com/SWG-Source/dsrc.git
synced 2026-07-31 00:15:54 -04:00
206 lines
5.1 KiB
Plaintext
206 lines
5.1 KiB
Plaintext
/**
|
|
* Title: datatable.scriptlib
|
|
* Description: library for generic datatable lookup functions
|
|
*/
|
|
|
|
// TABULATION IN BELOW FILE IS MESSED UP.
|
|
// NEW FUNCTIONS LIVE AT THE BOTTOM
|
|
|
|
/***** INCLUDES ********************************************************/
|
|
|
|
|
|
include datatable_writer;
|
|
|
|
/***** FUNCTIONS *******************************************************/
|
|
|
|
/*****************************************************************
|
|
* @brief select a random object template from a datatable
|
|
*
|
|
* @param string datatable
|
|
*
|
|
* @return string the path & name of the selected template;
|
|
* null if unable to find datatable or
|
|
* datatable is not properly formatted
|
|
*****************************************************************/
|
|
|
|
|
|
string getRandomTemplate(string datatable)
|
|
{
|
|
|
|
string[] templateFilenames = dataTableGetStringColumn(datatable, "templateFilename");
|
|
|
|
if ( templateFilenames == null )
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
int numItems = templateFilenames.length;
|
|
int row = rand(0, numItems-1);
|
|
|
|
return templateFilenames[row];
|
|
}
|
|
}
|
|
|
|
|
|
/*****************************************************************
|
|
* @brief instantiate a random object template from a datatable lookup
|
|
* in the world
|
|
*
|
|
* @param string datatable
|
|
*
|
|
* @return obj_id the obj_id of the created object
|
|
*****************************************************************/
|
|
|
|
obj_id createRandomTemplateInWorld(string datatable)
|
|
{
|
|
string templateFilename = getRandomTemplate(datatable);
|
|
|
|
obj_id myObjects = createObject(templateFilename, getLocation( getSelf() ));
|
|
|
|
if ( myObjects == null )
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
return myObjects;
|
|
}
|
|
}
|
|
|
|
|
|
/*****************************************************************
|
|
* @brief instantiate a random object template from a datatable lookup
|
|
* at the target
|
|
*
|
|
* @param string datatable
|
|
* @param obj_id target
|
|
*
|
|
* @return obj_id the obj_id of the created object
|
|
*****************************************************************/
|
|
|
|
obj_id createRandomTemplateAtTarget(string datatable, obj_id target)
|
|
{
|
|
string templateFilename = getRandomTemplate(datatable);
|
|
|
|
if ( (target == null) || (templateFilename == null) )
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
obj_id myObject = createObjectAt(templateFilename, target);
|
|
if ( myObject == null )
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
return myObject;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*****************************************************************
|
|
* @brief instantiate a random object template from a datatable lookup
|
|
* at a specified location
|
|
*
|
|
* @param string datatable
|
|
* @param location loc
|
|
*
|
|
* @return obj_id the obj_id of the created object
|
|
*****************************************************************/
|
|
|
|
obj_id createRandomTemplateAtTarget(string datatable, location loc)
|
|
{
|
|
string templateFilename = getRandomTemplate(datatable);
|
|
|
|
if ( (loc == null) || (templateFilename == null) )
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
obj_id myObject = createObject(templateFilename, loc);
|
|
if ( myObject == null )
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
return myObject;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// THE TABULATION IS FUCKED UP AND ITS NOT MY FAULT.
|
|
|
|
boolean createDataTable(string strFileName, string[] strHeaders, string[] strHeaderTypes)
|
|
{
|
|
if(strFileName==null)
|
|
{
|
|
LOG("ERROR", "Null string passed into createDataTable)");
|
|
return false;
|
|
}
|
|
int intIndex = strFileName.indexOf(".tab");
|
|
if(intIndex<0)
|
|
{
|
|
LOG("ERROR", "Datatbles need a .tab extension");
|
|
return false;
|
|
}
|
|
if(strHeaders.length!=strHeaderTypes.length)
|
|
{
|
|
LOG("ERROR", "You need to use the same length header/headertypes arrays");
|
|
return false;
|
|
}
|
|
|
|
string strHeaderString = "";
|
|
string strHeaderTypeString = "";
|
|
strFileName = "../../dsrc/sku.0/sys.server/compiled/game/"+strFileName;
|
|
for(int intI = 0; intI<strHeaders.length; intI++)
|
|
{
|
|
string strTest = strHeaderTypes[intI]; // preproc bug
|
|
|
|
if((strTest!="i")&&(strTest!="s")&&(strTest!="f")&&(strTest!="h")&&(strTest!="c")&&(strTest!="e")&&(strTest!="b")&&(strTest!="p"))
|
|
{
|
|
LOG("ERROR", "You passed in a bad type for your header types at entry "+intI+" value was "+strTest);
|
|
return false;
|
|
|
|
}
|
|
if(intI<strHeaders.length-1)
|
|
{
|
|
strHeaderString = strHeaderString + strHeaders[intI]+"\t"; // we want tabs until we hit the end of teh line, then no tabs
|
|
strHeaderTypeString = strHeaderTypeString + strHeaderTypes[intI]+"\t"; // tab
|
|
}
|
|
else
|
|
{
|
|
strHeaderString = strHeaderString + strHeaders[intI];
|
|
strHeaderTypeString = strHeaderTypeString + strHeaderTypes[intI]; // tab
|
|
|
|
|
|
}
|
|
|
|
}
|
|
strHeaderString = strHeaderString + "\n";
|
|
strHeaderTypeString = strHeaderTypeString +"\n";
|
|
string strTest = datatable_writer.makeDataTable(strFileName, strHeaderString, strHeaderTypeString);
|
|
LOG("NOT_ERROR", "Datatable filename is "+strFileName);
|
|
if(strTest!=null)
|
|
{
|
|
return true;
|
|
|
|
}
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
void serverDataTableAddRow(string strFileName, dictionary dctParams) // wrapper for addDatatableRow to avoid worrying about what directory everything lives in
|
|
{
|
|
strFileName = "../../dsrc/sku.0/sys.server/compiled/game/"+strFileName;
|
|
dataTableAddRow(strFileName, dctParams);
|
|
|
|
|
|
}
|