/// <summary> /// This routine loads a script (if it has not already been loaded), /// and sets up globals for the script. The script needs to be safe to /// have its entrypoint called from OBJECT_INVALID for this function to /// work properly. /// </summary> /// <param name="ScriptName">Supplies the name of the script to load.</param> /// <param name="ForceLoadScript">If true, forcibly attempt to load /// the script.</param> /// <param name="CallerScript">Supplies the script object owned by the /// caller.</param> /// <returns>The script object for the newly loaded script. On failure /// an exception is raised.</returns> public static IGeneratedScriptProgram LoadScript(string ScriptName, bool ForceLoadScript, CLRScriptBase CallerScript) { string AssemblyName = String.Format("NWScriptAsm_{0}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", ScriptName); string TypeName = String.Format("NWScript.JITCode.{0}.ScriptProgram", ScriptName); Assembly ScriptAssembly; IGeneratedScriptProgram ScriptObject; Type ScriptObjectType; MethodInfo NWScriptInitGlobals; // // Check if the script is already loaded. If so, use the existing // script assembly. // ScriptAssembly = (from LoadedAsm in AppDomain.CurrentDomain.GetAssemblies() where LoadedAsm.FullName == AssemblyName select LoadedAsm).FirstOrDefault(); if (ScriptAssembly == null && ForceLoadScript) { // // The assembly is not yet loaded. Force it to be loaded via // a dummy ExecuteScript call. // CallerScript.ExecuteScript(ScriptName, CLRScriptBase.OBJECT_INVALID); ScriptAssembly = (from LoadedAsm in AppDomain.CurrentDomain.GetAssemblies() where LoadedAsm.FullName == AssemblyName select LoadedAsm).FirstOrDefault(); } if (ScriptAssembly == null) { throw new ApplicationException("Could not load script " + ScriptName); } // // Now that the script assembly is loaded, we can create a script // object out of the script for our own use. The script object // encapsulates a private set of global variables for the target // script. // ScriptObject = (IGeneratedScriptProgram)ScriptAssembly.CreateInstance( TypeName, false, BindingFlags.CreateInstance, null, new object[] { CallerScript.ScriptHost.Intrinsics, CallerScript.ScriptHost.Host }, null, null); ScriptObjectType = ScriptObject.GetType(); // // If the script had global variables, call the global variable // initializer on the script now. This will set the script object // up into a state as though we created all the global variables, // but stopped just before main() or StartingConditional(). // NWScriptInitGlobals = ScriptObjectType.GetMethod("NWScriptInitGlobals"); if (NWScriptInitGlobals != null) { NWScriptInitGlobals.Invoke(ScriptObject, null); } // // The private script object is ready to use. Return the instance // of the script that we had created. // return(ScriptObject); }
public static uint _SpawnObject(string sResRef, int nObjectType, uint oWP, NWLocation lLoc, int nFlags, int nAlternate, CLRScriptBase s) { // if the object is not being spawned at it's waypoint location, we need to make sure // the actual spawn-in location isn't violating the "in PC sight" guidelines. if ((nAlternate != CLRScriptBase.FALSE) && ((nFlags & _SPAWN_FLAGS_IN_PC_SIGHT) == 0)) { uint oNeighbor = s.GetNearestCreatureToLocation(CLRScriptBase.CREATURE_TYPE_PLAYER_CHAR, CLRScriptBase.PLAYER_CHAR_IS_PC, lLoc, 1, -1, -1, -1, -1); if (s.GetIsObjectValid(oNeighbor) == CLRScriptBase.FALSE && (s.GetDistanceBetweenLocations(lLoc, s.GetLocation(oNeighbor)) <= PC_PERCEPTION_RANGE)) { // ACR_GetPCVisualRange() )) { return(CLRScriptBase.OBJECT_INVALID); } if (GetPrespawnPrediction(s) == CLRScriptBase.FALSE) { uint oTestWP = s.GetNearestObjectToLocation(CLRScriptBase.OBJECT_TYPE_WAYPOINT, lLoc, 1); int nWP_Index = 1; while ((oTestWP != CLRScriptBase.OBJECT_INVALID) && (s.GetDistanceBetweenLocations(lLoc, s.GetLocation(oTestWP)) <= PC_PERCEPTION_RANGE)) { // ACR_GetPC_VisualRange() )) { if (s.GetTag(oTestWP) == "ACR_SA_WP") { return(CLRScriptBase.OBJECT_INVALID); } else { nWP_Index = nWP_Index + 1; oTestWP = s.GetNearestObjectToLocation(CLRScriptBase.OBJECT_TYPE_WAYPOINT, lLoc, nWP_Index); } } } } uint oSpawned = s.CreateObject(nObjectType, sResRef, lLoc, nFlags & _SPAWN_FLAGS_WITH_ANIMATION, ""); // Check to make sure it spawned ok, print an error and exit if not. if (s.GetIsObjectValid(oSpawned) == CLRScriptBase.FALSE) { return(CLRScriptBase.OBJECT_INVALID); } // If it should be in stealth mode, place it there. if ((nFlags & _SPAWN_IN_STEALTH) == _SPAWN_IN_STEALTH) { s.SetActionMode(oSpawned, CLRScriptBase.ACTION_MODE_STEALTH, 1); } // If it should be in detect mode, place it there. if ((nFlags & _SPAWN_IN_DETECT) == _SPAWN_IN_DETECT) { s.SetActionMode(oSpawned, CLRScriptBase.ACTION_MODE_DETECT, 1); } // If this creature should buff himself, do it. if ((nFlags & _SPAWN_BUFFED) == _SPAWN_BUFFED) { ActivateLongTermBuffs(oSpawned, s); } // Play the spawn animation. s.PlayAnimation(s.GetLocalInt(oWP, _WP_SPAWN_ANIMATION), 1.0f, 0.0f); // Play the spawn in VFX. s.ApplyEffectAtLocation(CLRScriptBase.DURATION_TYPE_INSTANT, s.EffectVisualEffect(s.GetLocalInt(oWP, _WP_SPAWN_IN_VFX), CLRScriptBase.FALSE), s.GetLocation(oSpawned), 0.0f); // Play the spawn in SFX. s.AssignCommand(oSpawned, delegate { s.PlaySound(s.GetLocalString(oWP, _WP_SPAWN_IN_SFX), CLRScriptBase.FALSE); }); // Determine facing. if ((nFlags & _SPAWN_FLAGS_RANDOM_FACING) == _SPAWN_FLAGS_RANDOM_FACING) { // Spawn facing is random. s.AssignCommand(oSpawned, delegate { s.SetFacing(new Random().Next() * 360.0f, CLRScriptBase.FALSE); }); } // Colorize name if needed. if (s.GetLocalString(oSpawned, ACR_COLOR_NAME) != "") { s.SetFirstName(oSpawned, "<C='" + s.GetLocalString(oSpawned, ACR_COLOR_NAME) + "'>" + s.GetName(oSpawned) + "</C>"); s.SetLastName(oSpawned, ""); } // Run the spawn-in scripts, if any. int i = 1; while (true) { string sScript = s.GetLocalString(oWP, _WP_SPAWN_IN_SCRIPT_ARRAY + s.IntToString(i)); if (sScript == "") { break; } s.ExecuteScript(sScript, oSpawned); i++; } _AddObjectToSpawnPoint(oWP, oSpawned, s); return(oSpawned); }