mirror of
https://github.com/SWG-Source/dsrc.git
synced 2026-08-01 01:15:59 -04:00
252 lines
5.6 KiB
Plaintext
252 lines
5.6 KiB
Plaintext
include library.create;
|
|
include library.utils;
|
|
include library.player_structure;
|
|
include library.sui;
|
|
|
|
const int DECAY_LOOP_TIME = 30*60;
|
|
|
|
trigger OnInitialize()
|
|
{
|
|
//clean-up deprecated objvars:
|
|
removeObjVar (self, "timeStamp");
|
|
removeObjVar (self, "decayTime");
|
|
|
|
string templateName = getTemplateName (self);
|
|
if ( isLit(self) || templateName.indexOf("candlestick") >= 0) // candles dont have lit version
|
|
return SCRIPT_CONTINUE;//this is lit, so don't do anything.
|
|
|
|
//switch ub-lit object to lit object
|
|
templateName = getTemplateName (self);
|
|
int idx = templateName.indexOf("frn_all_");
|
|
if (idx!=-1)
|
|
{
|
|
string newTemplateName = templateName.substring(0, idx) + "frn_all_light_" + templateName.substring (idx+8);
|
|
obj_id newLight = self;
|
|
if ( isInHouse(self))
|
|
{
|
|
newLight = createObjectAt(newTemplateName, self);
|
|
if ( isIdValid(newLight) )
|
|
setYaw(newLight, getYaw (self));
|
|
}
|
|
else
|
|
{
|
|
obj_id container = getContainedBy(self);
|
|
if ( isIdValid(container) )
|
|
{
|
|
//must be a volume container:
|
|
newLight = createObject(newTemplateName, container, "");
|
|
}
|
|
else
|
|
{
|
|
//must be outside
|
|
newLight = createObjectAt(newTemplateName, self);
|
|
if ( isIdValid(newLight))
|
|
setYaw(newLight, getYaw (self));
|
|
}
|
|
}
|
|
if (!isIdValid(newLight) || newLight == self)
|
|
{
|
|
detachScript(self,"item.candle.candle_decay");
|
|
return SCRIPT_CONTINUE;//don't do anything else nor this ever again, because creation failed.
|
|
}
|
|
else
|
|
{
|
|
attachScript( newLight, "item.candle.candle_decay");
|
|
destroyObject( self );
|
|
}
|
|
}
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
boolean isLit( obj_id light )
|
|
{
|
|
string templateName = getTemplateName(light);
|
|
int idx = templateName.indexOf("frn_all_light_");
|
|
return ( idx != -1 );
|
|
}
|
|
|
|
boolean isInHouse(obj_id light)
|
|
{
|
|
obj_id container = getContainedBy(light);
|
|
if ( !isIdValid(container) )
|
|
{
|
|
return false;//this item is outside.
|
|
}
|
|
|
|
int got = getGameObjectType(container);
|
|
//LOG("CANDLE_DECAY", "container type is" + got);
|
|
|
|
if(isGameObjectTypeOf(got, GOT_misc_container) ||
|
|
isGameObjectTypeOf(got, GOT_misc_container_wearable) ||
|
|
isGameObjectTypeOf(got, GOT_tool) ||
|
|
isGameObjectTypeOf(got, GOT_installation))
|
|
{
|
|
// Stuff to do if the item is not placed in a building
|
|
//LOG("CANDLE_DECAY", "returned false");
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
// Stuff to do if the item is placed in a building
|
|
//LOG("CANDLE_DECAY", "returned true");
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/*
|
|
trigger OnAttach()
|
|
{
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
trigger OnInitialize()
|
|
|
|
{
|
|
//if in a building, start decay loop.
|
|
if (isInHouse(self))
|
|
{
|
|
applyDecay (self);
|
|
messageTo (self, "decayLoop", null, DECAY_LOOP_TIME, false);
|
|
}
|
|
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
|
|
trigger OnTransferred(obj_id sourceContainer, obj_id destContainer, obj_id transferer)
|
|
{
|
|
//Check to see what type of container object is transferred to.
|
|
if (isInHouse(self))
|
|
{
|
|
if (!hasObjVar (self, "timeStamp") && hasObjVar (self, "decayTime"))
|
|
{
|
|
setObjVar (self, "timeStamp", getGameTime());
|
|
messageTo (self, "decayLoop", null, DECAY_LOOP_TIME, false);
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
applyDecay (self);
|
|
removeObjVar (self, "timeStamp");
|
|
}
|
|
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
messageHandler decayLoop()
|
|
|
|
{
|
|
//Check to see what type of container candle is in every hour.
|
|
if (!isInHouse(self))
|
|
return SCRIPT_CONTINUE;
|
|
|
|
//If still in a building, call decay function
|
|
applyDecay (self);
|
|
messageTo (self, "decayLoop", null, DECAY_LOOP_TIME, false);
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
void applyDecay(obj_id light)
|
|
|
|
{
|
|
//get time from last decay
|
|
int timeStamp = getIntObjVar (light, "timeStamp");
|
|
|
|
if (timeStamp == 0)
|
|
return;
|
|
|
|
//difference in time from current to last decay.
|
|
|
|
int currentTime = getGameTime();
|
|
|
|
int decayTime = currentTime - timeStamp;
|
|
|
|
//apply the decay
|
|
|
|
int timeLeft = getIntObjVar (light, "decayTime");
|
|
|
|
timeLeft = timeLeft - decayTime;
|
|
|
|
//if obj var goes below 0, send message to destroyObject
|
|
if (timeLeft <= 0)
|
|
{
|
|
messageTo (light, "destroyOldTemplate", null, 1, false);
|
|
return;
|
|
}
|
|
|
|
setObjVar (light, "decayTime", timeLeft);
|
|
|
|
//update timestamp
|
|
setObjVar (light, "timeStamp", currentTime);
|
|
|
|
}
|
|
|
|
messageHandler destroyOldTemplate()
|
|
|
|
{
|
|
//switch object to non lit type.
|
|
string templateName = getTemplateName (self);
|
|
int idx = templateName.indexOf("frn_all_light_");
|
|
string newTemplateName = templateName.substring(0, idx) + "frn_all_" + templateName.substring (idx+14);
|
|
|
|
obj_id decayedLight = null;
|
|
if (isInHouse(self))
|
|
{
|
|
decayedLight = createObjectAt (newTemplateName, self);
|
|
setYaw (decayedLight, getYaw (self));
|
|
}
|
|
else
|
|
{
|
|
obj_id container = getContainedBy(self);
|
|
decayedLight = createObject(newTemplateName, container, "");
|
|
}
|
|
|
|
string assignedName = getAssignedName (self);
|
|
|
|
if (assignedName !=null)
|
|
{
|
|
setName (decayedLight, assignedName);
|
|
}
|
|
|
|
attachScript(decayedLight, "item.candle.candle_decay");
|
|
|
|
destroyObject (self);
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
trigger OnGetAttributes(obj_id player, string[] names, string[] attribs)
|
|
|
|
{
|
|
int idx = utils.getValidAttributeIndex(names);
|
|
if (idx == -1)
|
|
return SCRIPT_CONTINUE;
|
|
|
|
names[idx] = "lifespan";
|
|
if (!hasObjVar (self, "decayTime"))
|
|
{
|
|
attribs[idx] = "Burnt out";
|
|
}
|
|
|
|
else
|
|
{
|
|
int timeRemaining = getIntObjVar (self, "decayTime");
|
|
int timeDecayed = 0;
|
|
|
|
if (hasObjVar (self, "timeStamp"))
|
|
{
|
|
int timeStamp = getIntObjVar (self, "timeStamp");
|
|
timeDecayed = getGameTime() - timeStamp;
|
|
}
|
|
|
|
timeRemaining -= timeDecayed;
|
|
if (timeRemaining <= 0)
|
|
attribs[idx] = "0";
|
|
else
|
|
attribs[idx] = player_structure.assembleTimeRemaining(player_structure.convertSecondsTime(timeRemaining));
|
|
}
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
*/
|