Files
dsrc/sku.0/sys.server/compiled/game/script/library/hue.scriptlib
T

478 lines
11 KiB
Plaintext

/**
* Title: hue.scriptlib
* Description: Base library for object hue system
*/
/***** INCLUDES ********************************************************/
include library.colors;
include java.util.Set;
include java.util.Iterator;
/***** CONSTANTS *******************************************************/
const string INDEX_BASE = "/private/index_color_";
const string INDEX_1 = "/private/index_color_1";
const string INDEX_2 = "/private/index_color_2";
/***** FUNCTIONS ****************************************************/
/*****************************************************************
* @brief overload function: allows passing of color instead
* of just color properties
*
* @param obj_id target
* @param string varPathName
* @param color rgb
*
* @return void
*****************************************************************/
boolean setColor(obj_id target, int varIdx, color c)
{
if ( !isIdValid(target) || (c == null) || (varIdx < 0) )
{
return false;
}
string varPathName = INDEX_BASE + varIdx;
return setColor(target, varPathName, c);
}
boolean setColor(obj_id target, string varPathName, color c)
{
if ( !isIdValid(target) || (varPathName == null) || (c == null) )
{
return false;
}
if ( varPathName.startsWith("/") )
varPathName = varPathName.substring(1);
custom_var cv = getCustomVarByName(target, varPathName);
if ( cv == null )
{
return false;
}
if ( !cv.isPalColor() )
{
return false;
}
palcolor_custom_var pcv = (palcolor_custom_var)cv;
if ( pcv == null )
{
return false;
}
pcv.setToClosestColor(c);
return true;
}
/*****************************************************************
* @brief overload function: allows passing of color instead
* of just color properties
*
* @param obj_id target
* @param string varPathName
* @param color rgb
*
* @return void
*****************************************************************/
boolean setColor(obj_id target, int varIdx, int paletteIdx)
{
if ( !isIdValid(target) || (varIdx < 0) || (paletteIdx < 0) )
{
return false;
}
string varPathName = INDEX_BASE + varIdx;
return setColor(target, varPathName, paletteIdx);
}
boolean setColor(obj_id target, string varPathName, int paletteIdx)
{
if ( !isIdValid(target) || (varPathName == null) || (paletteIdx < 0) )
return false;
if ( varPathName.startsWith("/") )
varPathName = varPathName.substring(1);
custom_var cv = getCustomVarByName(target, varPathName);
if ( cv == null )
return false;
if ( !cv.isPalColor() )
return false;
ranged_int_custom_var ri = (ranged_int_custom_var)cv;
if ( ri == null )
return false;
ri.setValue(paletteIdx);
return true;
}
/*****************************************************************
*****************************************************************/
int getVarColorIndex(obj_id target, string varPathName)
{
if ( !isIdValid(target) || (varPathName == null) )
return -1;
if ( varPathName.startsWith("/") )
varPathName = varPathName.substring(1);
custom_var cv = getCustomVarByName(target, varPathName);
if ( cv == null )
return -1;
if ( !cv.isPalColor() )
return -1;
ranged_int_custom_var ri = (ranged_int_custom_var)cv;
if ( ri == null )
return -1;
return ri.getValue();
}
/*****************************************************************
* @brief overload function: allows passing of color instead
* of just color properties
*
* @param obj_id target
* @param string varPathName
* @param color rgb
*
* @return void
*****************************************************************/
void setPalcolorCustomVarClosestColor(obj_id target, string varPathName, color c)
{
if ( varPathName.startsWith("/") )
varPathName = varPathName.substring(1);
setPalcolorCustomVarClosestColor(target, varPathName, c.getR(), c.getG(), c.getB(), c.getA());
}
/*****************************************************************
* @brief retrives the Palette Color Variables from target
*
* @param obj_id target
*
* @return ranged_int_custom_var[]; null if no PalcolorVars
*****************************************************************/
ranged_int_custom_var[] getPalcolorVars(obj_id target)
{
if ( !isIdValid(target) )
{
return null;
}
PROFILER_START("hue.getPalcolorVars");
custom_var[] allVars = getAllCustomVars(target);
if ( allVars == null )
{
PROFILER_STOP("hue.getPalcolorVars");
return null;
}
//LOG("hue","hue:getPalcolorVars: *************** target = " + target + " **************");
int count = 0;
for ( int i = 0; i < allVars.length; i++ )
if ( allVars[i].isPalColor() )
++count;
ranged_int_custom_var[] ret = new ranged_int_custom_var[count];
int pos = 0;
for ( int i = 0; i < allVars.length; i++ )
{
if ( allVars[i].isPalColor() )
{
//LOG("hue","hue:getPalcolorVars: found palcolor -> " + allVars[i].toString());
ranged_int_custom_var ri = (ranged_int_custom_var)(allVars[i]);
ret[pos++] = ri;
}
}
if ( (ret == null) || (ret.length == 0) )
{
PROFILER_STOP("hue.getPalcolorVars");
return null;
}
PROFILER_STOP("hue.getPalcolorVars");
return ret;
}
/*****************************************************************
* @brief retrives the Palette Color vars and data from target
*
* @param obj_id target
*
* @return dictionary; null on error
*****************************************************************/
dictionary getPalcolorData(obj_id target)
{
if ( !isIdValid(target) )
{
return null;
}
PROFILER_START("hue.getPalcolorData");
ranged_int_custom_var[] palColors = getPalcolorVars(target);
if ( (palColors == null) || (palColors.length == 0) )
{
PROFILER_STOP("hue.getPalcolorData");
return null;
}
dictionary d = new dictionary();
for ( int i = 0; i < palColors.length; i++ )
{
ranged_int_custom_var ri = palColors[i];
d.put(ri.getVarName(), ri.getValue());
}
PROFILER_STOP("hue.getPalcolorData");
return d;
}
void setPalcolorData(obj_id target, dictionary colorData)
{
if(!isIdValid(target))
return;
if(colorData == null)
return;
ranged_int_custom_var[] palColors = getPalcolorVars(target);
if(palColors == null || palColors.length == 0)
return;
for ( int i = 0; i < palColors.length; i++ )
{
String varName = palColors[i].getVarName();
if (colorData.containsKey(varName))
{
int colorValue = colorData.getInt(varName);
palColors[i].setValue(colorValue);
}
}
}
/*****************************************************************
* @brief randomly hues target object
*
* @param obj_id target
*
* @return void
*****************************************************************/
void hueObject(obj_id target)
{
PROFILER_START("hue.hueObject");
ranged_int_custom_var[] c = getPalcolorVars(target);
if ( c != null )
{
for ( int i = 0; i < c.length; i++)
{
int min = c[i].getMinRangeInclusive();
int max = c[i].getMaxRangeInclusive();
int randVal = rand(min, max);
c[i].setValue(randVal);
}
}
PROFILER_STOP("hue.hueObject");
}
/**************************************************************************
* Overload of hueObject written by T.Bailey 11/22/02 to facilitate hueing*
* creatures given an int for their pallette color. *
* *
*************************************************************************/
void hueObject(obj_id target, int color)
{
PROFILER_START("hue.hueObjectColor");
ranged_int_custom_var[] c = getPalcolorVars(target);
if ( c != null )
{
for ( int i = 0; i < c.length; i++)
{
c[i].setValue(color);
}
}
PROFILER_STOP("hue.hueObjectColor");
}
/*****************************************************************
* @brief retrives the Ranged Int Variables from target
*
* @param obj_id target
*
* @return ranged_int_custom_var[]; null if no PalcolorVars
*****************************************************************/
ranged_int_custom_var[] getRangedIntVars(obj_id target)
{
if ( !isIdValid(target) )
{
return null;
}
PROFILER_START("hue.getRangedIntVars.custom");
custom_var[] allVars = getAllCustomVars(target);
PROFILER_STOP("hue.getRangedIntVars.custom");
if ( allVars == null )
{
//debugServerConsoleMsg(target, "hue.getRangedIntVars: no customization vars on target!");
return null;
}
PROFILER_START("hue.getRangedIntVars.alloc");
ranged_int_custom_var[] ri = new ranged_int_custom_var[allVars.length];
PROFILER_STOP("hue.getRangedIntVars.alloc");
if ( ri == null )
{
//debugServerConsoleMsg(target, "hue.getRangedIntVars: no customization vars on target!");
return null;
}
int n = 0;
PROFILER_START("hue.getRangedIntVars.loopA." + allVars.length);
for ( int i = 0; i < allVars.length; i++ )
{
if ( allVars[i].isRangedInt() )
{
ri[n] = (ranged_int_custom_var)allVars[i];
n++;
}
}
PROFILER_STOP("hue.getRangedIntVars.loopA." + allVars.length);
if ( n == 0 )
{
return null;
}
PROFILER_START("hue.getRangedIntVars.alloc2");
ranged_int_custom_var[] ret = new ranged_int_custom_var[n];
PROFILER_STOP("hue.getRangedIntVars.alloc2");
if (ret == null)
{
return null;
}
PROFILER_START("hue.getRangedIntVars.loopB." + n);
for ( int i = 0; i < n; i++ )
{
ret[i] = ri[i];
}
PROFILER_STOP("hue.getRangedIntVars.loopB." + n);
return ret;
}
/*****************************************************************
* @brief randomly reshapes target object according to RI vars
*
* @param obj_id target
*
* @return void
*****************************************************************/
void reshapeObject(obj_id target)
{
PROFILER_START("hue.reshapeObject");
ranged_int_custom_var[] c = getRangedIntVars(target);
if ( c != null )
{
for ( int i = 0; i < c.length; i++)
{
PROFILER_START("hue.reshapeObject.innerLoop");
int min = c[i].getMinRangeInclusive();
int max = c[i].getMaxRangeInclusive();
int randVal = rand(min, max);
c[i].setValue(randVal);
PROFILER_STOP("hue.reshapeObject.innerLoop");
}
}
else
{
//debugServerConsoleMsg(target, "hue.reshapeObject: no palcolors to alter!");
}
PROFILER_STOP("hue.reshapeObject");
}
/*****************************************************************
* @brief randomly reshapes target object according to RI vars
*
* @param obj_id target
*
* @return void
*****************************************************************/
void randomizeObject(obj_id target)
{
reshapeObject(target);
hueObject(target);
//debugSpeakMsg (target, "Being Rehued");
}
/*****************************************************************
* @brief overload function: allows passing of color instead
* of just color properties
*
* @param obj_id target
* @param string varPathName
* @param color rgb
*
* @return void
*****************************************************************/
boolean setRangedIntCustomVar(obj_id target, string cvar_name, int idx)
{
if ( !isIdValid(target) || (cvar_name == null) || cvar_name.equals("") || (idx < 0) )
return false;
if ( cvar_name.startsWith("/") )
cvar_name = cvar_name.substring(1);
custom_var cv = getCustomVarByName(target, cvar_name);
if ( cv == null )
return false;
if ( !cv.isRangedInt() )
return false;
ranged_int_custom_var ri = (ranged_int_custom_var)cv;
if ( ri == null )
return false;
ri.setValue(idx);
return true;
}
boolean setTexture(obj_id target, int idx, int value)
{
if ( !isIdValid(target) || (idx < 1) || (value < 0) )
{
return false;
}
string varPathName = "private/index_texture_" + idx;
return setRangedIntCustomVar(target, varPathName, value);
}