include library.utils; include library.ai_lib; include library.space_utils; include library.spawning; include library.create; trigger OnAttach() { CustomerServiceLog("holidaySpawner", "spawner_random.OnAttach: Initialized for: "+self); if (!hasObjVar(self, "registerWithController")) setObjVar(self, "registerWithController", 1); if (hasObjVar(self, "strName")) setName(self, getStringObjVar(self, "strName")); if (canSpawnByConfigSetting()) messageTo(self, "doSpawnEvent", null, 20, false); return SCRIPT_CONTINUE; } trigger OnInitialize() { CustomerServiceLog("holidaySpawner", "spawner_random.OnAttach: Initialized for: "+self); if (!hasObjVar(self, "registerWithController")) setObjVar(self, "registerWithController", 1); if (hasObjVar(self, "strName")) setName(self, getStringObjVar(self, "strName")); if (canSpawnByConfigSetting()) { CustomerServiceLog("holidaySpawner", "spawner_random.OnAttach: CAN SPAWN for: "+self); messageTo(self, "doSpawnEvent", null, 20, false); } else { CustomerServiceLog("holidaySpawner", "spawner_random.OnAttach: CANNOT SPAWN for: "+self); } return SCRIPT_CONTINUE; } // Testing stuff trigger OnHearSpeech(obj_id speaker, string text) { if (!isGod(speaker)) return SCRIPT_CONTINUE; obj_id target = getIntendedTarget(speaker); if (target != self) return SCRIPT_CONTINUE; if (text.equals("reset")) { // LOG("spawning", "ONRESET"); if (!hasObjVar(self, "registerWithController")) setObjVar(self, "registerWithController", 1); if (hasObjVar(self, "strName")) setName(self, getStringObjVar(self, "strName")); if (canSpawnByConfigSetting()) messageTo(self, "doSpawnEvent", null, 20, false); debugSpeakMsg(speaker, "reset"); } return SCRIPT_CONTINUE; } messageHandler doSpawnEvent() { CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: Initialized for: "+self); string datatable = getStringObjVar (self, "strSpawnTable"); if (datatable == null || datatable == "") { debugSpeakMsg(self, "I need a spawn table under objvar strSpawnTable"); LOG("spawning", "No spawn datatable set, necessary for random spawners"); return SCRIPT_CONTINUE; } int numberOfLocations = dataTableGetNumRows (datatable); if(numberOfLocations <= 0) { CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: Spawn table has no rows. Exiting."); return SCRIPT_CONTINUE; } CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: There are "+numberOfLocations+" number of rows for the spawner to select from. Random Spawner: "+self); int numberOfSpawns = getIntObjVar (self, "intSpawnCount"); if(numberOfSpawns <= 0) { CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: Spawner has no spawn count. Exiting."); return SCRIPT_CONTINUE; } CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: There are "+numberOfSpawns+" spawns needed for this spawner to work. Random Spawner: "+ self); if(numberOfSpawns >= numberOfLocations) { CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: There are more or equal the number of spawns to number of locations. Need to be less. Exiting."); return SCRIPT_CONTINUE; } if(!spawning.checkSpawnCount(self)) { CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: spawning.checkSpawnCount(self) reports that it cannot spawn any more. Exiting."); return SCRIPT_CONTINUE; } CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: We have not reached max spawns for event per spawning.checkSpawnCount library. Spawner: "+self); string strSpawnType = getStringObjVar(self, "strSpawns"); if(strSpawnType == null || strSpawnType.equals("")) { CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent:The strSpawns objvar was invalid. Exiting."); return SCRIPT_CONTINUE; } CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: strSpawns: "+strSpawnType); // If strSpawnType is the name of a spawning table, use it. // Otherwise, strSpawnType should be directly from creatures.tab or be an object string strSpawn = strSpawnType; if(strSpawnType.indexOf(".iff") < 0) { string strFileName = "datatables/spawning/ground_spawning/types/"+strSpawnType+".iff"; if ( dataTableOpen(strFileName) ) { CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: strSpawns datatable found and opened"); string[] strSpawns = dataTableGetStringColumnNoDefaults(strFileName, "strItem"); if( strSpawns==null || strSpawns.length==0 ) { CustomerServiceLog("holidaySpawner", "spawner_random.doSpawnEvent: no strItem or corrupt data found for strSpawns datatable."); 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); strSpawn = strSpawns[intRoll]; } } // LOG("spawning", "spawning "+strSpawn); if( strSpawn==null || strSpawn.length() < 1 ) { setName(self, "Mangled spawner. strSpawn is " + strSpawn + "."); return SCRIPT_CONTINUE; } int x = rand(0, numberOfLocations-1); float xCoord = dataTableGetFloat (datatable, x, "loc_x"); float yCoord = dataTableGetFloat (datatable, x, "loc_y"); float zCoord = dataTableGetFloat (datatable, x, "loc_z"); float yaw = dataTableGetFloat (datatable, x, "yaw"); // LOG("spawning", "x is " + xCoord + " y is " + yCoord + " z is " + zCoord + " yaw is " + yaw); // LOG("spawning", "random x is " + x); // LOG("spawning", "number of rows is " + numberOfLocations); location myself = getLocation (self); string planet = myself.area; location locLocation = new location (xCoord, yCoord, zCoord, planet); if (!utils.hasScriptVar(self, "spawnedObjectList.spawnLoc" + locLocation)) { if (utils.hasScriptVar(self, "locationFindTries")) utils.removeScriptVar(self, "locationFindTries"); createMob(strSpawn, null, locLocation, yaw, self); } else { if (utils.hasScriptVar (self, "locationFindTries")) { int nrTries = utils.getIntScriptVar(self, "locationFindTries"); utils.setScriptVar(self, "locationFindTries", nrTries + 1); } else utils.setScriptVar(self, "locationFindTries", 1); if (utils.getIntScriptVar (self, "locationFindTries") < 501) messageTo(self, "doSpawnEvent", null, 3, false); else LOG("spawning", "Random spawner: " + self + " named: " + getStringObjVar(self, "strName") + " has tried to find a good location for a spawn 500 times. Now giving up"); } return SCRIPT_CONTINUE; } void createMob(string strId, obj_id objLocationObject, location locLocation, float yaw, obj_id self) { if(!spawning.checkSpawnCount(self)) { return; } // Begin Spawning // The string is actually what we spawn. int intIndex = strId.indexOf(".iff"); float fltMinSpawnTime = getFloatObjVar(self, "fltMinSpawnTime"); float fltMaxSpawnTime = getFloatObjVar(self, "fltMaxSpawnTime"); float fltRespawnTime = rand(fltMinSpawnTime, fltMaxSpawnTime); float fltLifeTime = 0; // We might set a min and max life time of object. if (hasObjVar(self, "fltMinLifeTime")) { if (hasObjVar(self, "fltMaxLifeTime")) { float fltMinLifeTime = getFloatObjVar(self, "fltMinLifeTime"); float fltMaxLifeTime = getFloatObjVar(self, "fltMaxLifeTime"); if (fltMinLifeTime < fltMaxLifeTime) fltLifeTime = rand(fltMinLifeTime, fltMaxLifeTime); } } if(intIndex>-1) { // Template obj_id objTemplate = createObject(strId, locLocation); if(isIdValid(objLocationObject)) { destroyObject(objLocationObject); } if(!isIdValid(objTemplate)) { return; } spawning.incrementSpawnCount(self); spawning.addToSpawnDebugList(self, objTemplate); setObjVar(objTemplate, "objParent", self); setObjVar(objTemplate, "fltRespawnTime", fltRespawnTime); utils.setScriptVar(self, "spawnedObjectList.spawnLoc" + locLocation, objTemplate); utils.setScriptVar(objTemplate, "deathTracker", locLocation); attachScript(objTemplate, "systems.spawning.spawned_tracker"); if (fltLifeTime != 0) messageTo(objTemplate, "selfDestruct", null, fltLifeTime, false); setYaw(objTemplate, yaw); } 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; } int intBehavior = getIntObjVar(self, "intDefaultBehavior"); ai_lib.setDefaultCalmBehavior(objMob, intBehavior); spawning.incrementSpawnCount(self); spawning.addToSpawnDebugList(self, objMob); setObjVar(objMob, "objParent", self); setObjVar(objMob, "fltRespawnTime", fltRespawnTime); utils.setScriptVar(self, "spawnedObjectList.spawnLoc" + locLocation, objMob); utils.setScriptVar(objMob, "deathTracker", locLocation); attachScript(objMob, "systems.spawning.spawned_tracker"); if (fltLifeTime != 0) messageTo(objMob, "selfDestruct", null, fltLifeTime, false); setYaw(objMob, yaw); } if(!spawning.checkSpawnCount(self)) { // LOG("spawning", "Failed to check spawncount"); return; } // LOG("spawning", "resending doSpawnEvent"); messageTo(self, "doSpawnEvent", null, fltRespawnTime, false); return; } 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); } // Opening up the location the mob was at for spawning. location loc = params.getLocation("loc"); if (utils.hasScriptVar (self, "spawnedObjectList.spawnLoc" + loc)) utils.removeScriptVar (self, "spawnedObjectList.spawnLoc" + loc); messageTo(self, "doSpawnEvent", null, 2, false); return SCRIPT_CONTINUE; } boolean canSpawnByConfigSetting() { string disableSpawners = getConfigSetting("GameServer", "disableAreaSpawners"); 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; }