mirror of
https://github.com/SWG-Source/dsrc.git
synced 2026-07-31 00:15:54 -04:00
473 lines
12 KiB
Plaintext
473 lines
12 KiB
Plaintext
include library.utils;
|
|
include library.ai_lib;
|
|
include library.space_utils;
|
|
include library.spawning;
|
|
include library.create;
|
|
include java.util.Vector;
|
|
|
|
const int LOCATION_SEARCH_RADIUS = 1000;
|
|
|
|
trigger OnAttach()
|
|
{
|
|
start( self );
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
messageHandler startTheaterFromBuildout()
|
|
{
|
|
start( self );
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
void start( obj_id self )
|
|
{
|
|
messageTo(self, "setLocationArray", null, 5, false);
|
|
|
|
if ( ! hasObjVar(self, "registerWithController") )
|
|
{
|
|
setObjVar(self, "registerWithController", 1);
|
|
}
|
|
|
|
if ( canSpawnByConfigSetting() )
|
|
{
|
|
dictionary dict = new dictionary();
|
|
dict.put("spawnNum", 0);
|
|
messageTo( self, "doInitialSpawn", dict, 10, false );
|
|
}
|
|
}
|
|
|
|
messageHandler doInitialSpawn()
|
|
{
|
|
if(!spawning.checkSpawnCount(self))
|
|
return SCRIPT_CONTINUE;
|
|
|
|
int intGoodLocationSpawner = getIntObjVar(self, "intGoodLocationSpawner");
|
|
string strSpawnType = getStringObjVar(self, "strSpawns");
|
|
|
|
// if strSpawnType is the name of a spawning table, use it.
|
|
// Otherwise, strSpawnType should be directly from creatures.tab
|
|
string[] strSpawns = {strSpawnType};
|
|
float[] fltSizes = {8.0f};
|
|
|
|
string strFileName = "datatables/spawning/ground_spawning/types/"+strSpawnType+".iff";
|
|
if ( dataTableOpen(strFileName) )
|
|
{
|
|
strSpawns = dataTableGetStringColumnNoDefaults(strFileName, "strItem");
|
|
fltSizes = dataTableGetFloatColumn(strFileName, "fltSize");
|
|
if((strSpawns==null)||(strSpawns.length==0))
|
|
{
|
|
LOG("spawning", "NO SPAWNS IN FILE "+strFileName);
|
|
setName(self, "Mangled spawner. strFileName is "+strFileName+" I couldnt find any spawns in that file.");
|
|
}
|
|
|
|
if(fltSizes.length==0)
|
|
{
|
|
LOG("spawning", "BAD NUMBER OF SIZES !@!@@ IN FILE "+strFileName);
|
|
setName(self, "BAD NUMBER OF SIZES !@!@@ IN FILE "+strFileName);
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
if(fltSizes.length!=strSpawns.length)
|
|
{
|
|
LOG("spawning", "Missing either spawns or sizes in "+strFileName);
|
|
setName(self, "Missing either spawns or sizes in "+strFileName);
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
}
|
|
|
|
location[] patrolPoints = utils.getLocationArrayScriptVar(self, "patrolArray");
|
|
if (patrolPoints == null)
|
|
{
|
|
LOG("spawning", "ERROR: patrolArray script var not defined.");
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
int spawnNum = params.getInt("spawnNum");
|
|
|
|
// get maximum number of spawns
|
|
int maxSpawns = getIntObjVar(self, "intSpawnCount");
|
|
if (maxSpawns >= patrolPoints.length)
|
|
{
|
|
LOG("spawning", "ERROR: Attempting to spawn more creatures than patrol points.");
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
// create a dynamic list of spawnable entries.
|
|
// we will remove entries from this list if they are reported
|
|
// as unspawnable.
|
|
|
|
resizeable int[] goodSpawns = new Vector();
|
|
for(int i = 0; i < fltSizes.length; ++i)
|
|
{
|
|
goodSpawns.addElement(new Integer(i));
|
|
}
|
|
|
|
int spawnCount = 0;
|
|
while(spawnCount < maxSpawns)
|
|
{
|
|
int roll = rand(0, goodSpawns.size() - 1);
|
|
int rowIndex = goodSpawns[roll];
|
|
|
|
float size = fltSizes[rowIndex];
|
|
string spawn = strSpawns[rowIndex];
|
|
|
|
size = getClosestSize(size);
|
|
location locTest = patrolPoints[spawnCount];
|
|
//LOG("spawning", "fltSize is " + size);
|
|
if (createMob(spawn, null, locTest, 0.0f, self, false, spawnCount))
|
|
{
|
|
++spawnCount;
|
|
}
|
|
else
|
|
{
|
|
LOG("spawning", "Unable to create creature " + spawn + ". Check data.");
|
|
|
|
// this row is bad, so remove it from our list of potential spawns.
|
|
utils.removeElementAt(goodSpawns, roll);
|
|
if (goodSpawns.size() == 0)
|
|
{
|
|
// we've removed all of our entries. The entire table is bad,
|
|
// so warn and exit.
|
|
LOG("spawning", "All entries in table " + strFileName + " are bad!!!");
|
|
setName(self, "No valid spawners!");
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
messageHandler doSpawnEvent()
|
|
{
|
|
|
|
if(!spawning.checkSpawnCount(self))
|
|
{
|
|
LOG("spawning", "No count 1");
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
|
|
int intGoodLocationSpawner = getIntObjVar(self, "intGoodLocationSpawner");
|
|
float fltRadius = getFloatObjVar(self, "fltRadius");
|
|
string strSpawnType = getStringObjVar(self, "strSpawns");
|
|
|
|
// if strSpawnType is the name of a spawning table, use it.
|
|
// Otherwise, strSpawnType should be directly from creatures.tab
|
|
float fltSize = 8.0f;
|
|
string strSpawn = strSpawnType;
|
|
|
|
string strFileName = "datatables/spawning/ground_spawning/types/"+strSpawnType+".iff";
|
|
if ( dataTableOpen(strFileName) )
|
|
{
|
|
string[] strSpawns = dataTableGetStringColumnNoDefaults(strFileName, "strItem");
|
|
float[] fltSizes= dataTableGetFloatColumn(strFileName, "fltSize");
|
|
if((strSpawns==null)||(strSpawns.length==0))
|
|
{
|
|
//LOG("spawning", "NO SPAWNS IN FILE "+strFileName);
|
|
setName(self, "Mangled spawner. strFileName is "+strFileName+" I couldnt find any spawns in that file.");
|
|
}
|
|
int intRoll = rand(0, strSpawns.length-1);
|
|
//LOG("spawning", "looking for "+strFileName);
|
|
|
|
if(fltSizes.length==0)
|
|
{
|
|
//LOG("spawning", "BAD NUMBER OF SIZES !@!@@ IN FILE "+strFileName);
|
|
setName(self, "BAD NUMBER OF SIZES !@!@@ IN FILE "+strFileName);
|
|
}
|
|
if(fltSizes.length!=strSpawns.length)
|
|
{
|
|
//LOG("spawning", "Missing either spawns or sizes in "+strFileName);
|
|
setName(self, "Missing either spawns or sizes in "+strFileName);
|
|
}
|
|
fltSize = fltSizes[intRoll];
|
|
strSpawn = strSpawns[intRoll];
|
|
}
|
|
//LOG("spawning", "spawning "+strSpawn);
|
|
|
|
if( strSpawn==null || strSpawn.length() < 1 )
|
|
{
|
|
setName(self, "Mangled spawner. strSpawn is " + strSpawn + ".");
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
|
|
location locTest =spawning.getRandomLocationInCircle(getLocation(self), fltRadius);
|
|
fltSize = getClosestSize(fltSize);
|
|
//LOG("spawning", "fltSize is "+fltSize);
|
|
if(intGoodLocationSpawner>0)
|
|
{
|
|
//LOG("spawning", "getting a good location around "+locTest+" with size "+fltSize);
|
|
requestLocation(self, strSpawn, locTest, rand(100, 200), fltSize, true, true);
|
|
}
|
|
else
|
|
{
|
|
createMob(strSpawn, null, locTest, fltRadius, self);
|
|
}
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
|
|
float getClosestSize(float fltOriginalSize)
|
|
{
|
|
|
|
if(fltOriginalSize<=4.0f)
|
|
{
|
|
return 4.0f;
|
|
}
|
|
if(fltOriginalSize<=8.0f)
|
|
{
|
|
return 8.0f;
|
|
}
|
|
if(fltOriginalSize<=12.0f)
|
|
{
|
|
return 12.0f;
|
|
}
|
|
if(fltOriginalSize<=16.0f)
|
|
{
|
|
return 16.0f;
|
|
}
|
|
|
|
if(fltOriginalSize<=32.0f)
|
|
{
|
|
return 32.0f;
|
|
}
|
|
else if(fltOriginalSize <= 48.0f)
|
|
{
|
|
return 48.0f;
|
|
}
|
|
else if(fltOriginalSize <= 64.0f)
|
|
{
|
|
return 64.0f;
|
|
}
|
|
else if(fltOriginalSize <= 80.0f)
|
|
{
|
|
return 80f;
|
|
}
|
|
else if(fltOriginalSize <= 96.0f)
|
|
{
|
|
return 96f;
|
|
}
|
|
return 32f;
|
|
|
|
}
|
|
|
|
boolean createMob(string creature, obj_id locationObject, location spawnLoc, float spawnRadius, obj_id self)
|
|
{
|
|
return createMob(creature, locationObject, spawnLoc, spawnRadius, self, true, 0);
|
|
}
|
|
|
|
boolean createMob(string strId, obj_id objLocationObject, location locLocation, float fltRadius, obj_id self, boolean doCallBack, int startingPoint)
|
|
{
|
|
|
|
if(!spawning.checkSpawnCount(self))
|
|
{
|
|
return false;
|
|
}
|
|
// SPAWN@!@!@!@!@!
|
|
|
|
// the string is actuallywaht we spawn.
|
|
int intIndex = strId.indexOf(".iff");
|
|
float fltMinSpawnTime = getFloatObjVar(self, "fltMinSpawnTime");
|
|
float fltMaxSpawnTime = getFloatObjVar(self, "fltMaxSpawnTime");
|
|
float fltRespawnTime = rand(fltMinSpawnTime, fltMaxSpawnTime);
|
|
if(intIndex>-1)
|
|
{
|
|
// template
|
|
obj_id objTemplate = createObject(strId, locLocation);
|
|
if(isIdValid(objLocationObject))
|
|
{
|
|
destroyObject(objLocationObject);
|
|
}
|
|
|
|
if(!isIdValid(objTemplate))
|
|
{
|
|
//LOG("spawning", "Can't create mob of type " + strId);
|
|
return false;
|
|
}
|
|
spawning.incrementSpawnCount(self);
|
|
utils.setScriptVar(objTemplate, "parent", self);
|
|
utils.setScriptVar(objTemplate, "fltRespawnTime", fltRespawnTime);
|
|
utils.setScriptVar(objTemplate, "patrolPoints", utils.getLocationArrayScriptVar(self, "patrolArray"));
|
|
utils.setScriptVar(objTemplate, "patrolPathType", getStringObjVar(self, "patrolPathType"));
|
|
utils.setScriptVar(objTemplate, "startingPoint", startingPoint);
|
|
attachScript(objTemplate, "systems.spawning.patrol_spawned_tracker");
|
|
|
|
|
|
}
|
|
else
|
|
{
|
|
// monster
|
|
//LOG("spawning", "I am going to make a "+strId);
|
|
if(isIdValid(objLocationObject))
|
|
{
|
|
destroyObject(objLocationObject);
|
|
}
|
|
obj_id objMob = create.object(strId, locLocation);
|
|
if(!isIdValid(objMob))
|
|
{
|
|
|
|
setName(self , "BAD MOB OF TYPE "+strId);
|
|
return false;
|
|
}
|
|
int intBehavior = getIntObjVar(self, "intDefaultBehavior");
|
|
ai_lib.setDefaultCalmBehavior(objMob, intBehavior);
|
|
|
|
spawning.incrementSpawnCount(self);
|
|
utils.setScriptVar(objMob, "parent", self);
|
|
utils.setScriptVar(objMob, "fltRespawnTime", fltRespawnTime);
|
|
utils.setScriptVar(objMob, "patrolPoints", utils.getLocationArrayScriptVar(self, "patrolArray"));
|
|
utils.setScriptVar(objMob, "patrolPathType", getStringObjVar(self, "patrolPathType"));
|
|
utils.setScriptVar(objMob, "startingPoint", startingPoint);
|
|
attachScript(objMob, "systems.spawning.patrol_spawned_tracker");
|
|
|
|
}
|
|
|
|
|
|
//LOG("spawn", "resending doSpawnEvent");
|
|
if (doCallBack)
|
|
{
|
|
if( spawning.checkSpawnCount(self) )
|
|
{
|
|
messageTo(self, "doSpawnEvent", null, fltRespawnTime, false);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
messageHandler setLocationArray()
|
|
{
|
|
string[] patrolPointString = utils.getStringArrayObjVar(self, "strPatrolPointNames");
|
|
|
|
if (patrolPointString == null || patrolPointString.length == 0)
|
|
return SCRIPT_CONTINUE;
|
|
|
|
resizeable location[] patrolPointLocation = new location[0];
|
|
obj_id[] objects = getObjectsInRange(self, 700);
|
|
|
|
if (objects == null || objects.length == 0)
|
|
return SCRIPT_CONTINUE;
|
|
|
|
patrolPointLocation = utils.addElement(patrolPointLocation, getLocation(getSelf())); // The spawner is patrol point #1
|
|
boolean isInInstance = false;
|
|
obj_id dungeonController = obj_id.NULL_ID;
|
|
|
|
if (hasObjVar(self, "dungeonController"))
|
|
isInInstance = true;
|
|
|
|
if (isInInstance)
|
|
dungeonController = getObjIdObjVar(self, "dungeonController");
|
|
|
|
setName(self, getStringObjVar(self, "strName"));
|
|
|
|
for (int i=0;i<patrolPointString.length;i++)
|
|
{
|
|
for (int k=0;k<objects.length;k++)
|
|
{
|
|
if(!isIdValid(objects[k]))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (hasObjVar(objects[k], "pointName"))
|
|
{
|
|
if (getStringObjVar(objects[k], "pointName").equals(patrolPointString[i]))
|
|
{
|
|
if (isInInstance)
|
|
{
|
|
if (getObjIdObjVar(objects[k], "dungeonController") == dungeonController)
|
|
{
|
|
setName(objects[k], getStringObjVar(objects[k], "pointName"));
|
|
patrolPointLocation = utils.addElement(patrolPointLocation, getLocation(objects[k]));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
setName(objects[k], getStringObjVar(objects[k], "pointName"));
|
|
patrolPointLocation = utils.addElement(patrolPointLocation, getLocation(objects[k]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
location[] patrolArray = patrolPointLocation;
|
|
utils.setScriptVar(self, "patrolArray", patrolArray);
|
|
return SCRIPT_CONTINUE;
|
|
}
|
|
|
|
trigger OnLocationReceived(string strId, obj_id objLocationObject, location locLocation, float fltRadius)
|
|
{
|
|
|
|
//LOG("spawning", "strId is "+strId+" and objLocationObject is "+objLocationObject+" and location os "+locLocation);
|
|
if(isIdValid(objLocationObject))
|
|
{
|
|
//LOG("spawning", "got goodlocation 2");
|
|
// we make our thang. Otherwise
|
|
createMob(strId, objLocationObject, locLocation, fltRadius, self);
|
|
}
|
|
else
|
|
{
|
|
float fltMinSpawnTime = getFloatObjVar(self, "fltMinSpawnTime");
|
|
float fltMaxSpawnTime = getFloatObjVar(self, "fltMaxSpawnTime");
|
|
float fltRespawnTime = rand(fltMinSpawnTime, fltMaxSpawnTime);
|
|
// keep trying
|
|
messageTo(self, "doSpawnEvent", null, fltRespawnTime, false);
|
|
}
|
|
return SCRIPT_CONTINUE;
|
|
|
|
|
|
}
|
|
|
|
messageHandler spawnDestroyed()
|
|
{
|
|
int intCurrentSpawnCount = utils.getIntScriptVar(self, "intCurrentSpawnCount");
|
|
intCurrentSpawnCount = intCurrentSpawnCount - 1;
|
|
if(intCurrentSpawnCount>-1)
|
|
{
|
|
utils.setScriptVar(self, "intCurrentSpawnCount", intCurrentSpawnCount);
|
|
|
|
}
|
|
else
|
|
{
|
|
utils.setScriptVar(self, "intCurrentSpawnCount", 0);
|
|
}
|
|
messageTo(self, "doSpawnEvent", null, 2, false);
|
|
return SCRIPT_CONTINUE;
|
|
|
|
}
|
|
|
|
boolean canSpawnByConfigSetting()
|
|
{
|
|
string disableSpawners = getConfigSetting("GameServer", "disablePatrolSpawners");
|
|
if (disableSpawners == null)
|
|
return true;
|
|
|
|
if (disableSpawners.equals("true") || disableSpawners.equals("1"))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
//Destroy spawned objects when spawner is destroyed.
|
|
trigger OnDestroy()
|
|
{
|
|
if (utils.hasScriptVar(self, "debugSpawnList"))
|
|
{
|
|
//LOG("spawning", "Reached debugSpawnList");
|
|
obj_id[] spawns = utils.getObjIdArrayScriptVar(self, "debugSpawnList");
|
|
|
|
if(spawns == null || spawns.length == 0)
|
|
return SCRIPT_CONTINUE;
|
|
|
|
for(int i = 0; i < spawns.length; ++i)
|
|
{
|
|
//LOG("spawning", "Reached loop");
|
|
if(isIdValid(spawns[i]) && exists(spawns[i]))
|
|
messageTo(spawns[i], "selfDestruct", null, 5, false);
|
|
}
|
|
}
|
|
return SCRIPT_CONTINUE;
|
|
} |