Tutorial located in CoD:UO Mapping section:
http://www.modsonline.com/Tutorials-read-292.html
Code:
/* ======================================================================
Light control script by WHC_Grassy
Created: September 07, 2005
Version 2
Added either On or Off inital light states (global to all lights in map)
Added sound and light switch model usage
(see comments and notes below)
What is this? :
It's a script I wrote to allow players to be able to turn on/off
lights in mp maps. It has been tested in UO but should also work in COD.
I have tried to make it universal so anyone can use it in their own map.
How does it work? :
By simple illusion, it actually moves the light radiating model
out of the play area, and hides the light_on xmodel, and shows
the light_off xmodel. And of course the reverse when turned on again.
This creates the illusion of the light being switched
on and off :)
Some advice :
The light radiated from the script_models is a workaround due to
the fact that the game dosn't support control of real lights.
Unfortunatly the script_models we have to use dont radiate light
as well as a normal light entity would, it's advisable to keep the light
values fairly low otherwise it will radiate right through walls, floors
and roofs and look nasty.
Only use them for effect in enclosed areas like tunnels, cellars
underground, and have other lighting not too far away,
say around a corner of a tunnel, or in an adjacent roon of a cellar.
What you need to do in your map :
You can have any number of lights and models but don't go overboard here!
-------------------------------------------------------------------------------------------------------
Example for a room with two lights and two triggers (one each end of the room near doors for eg)
Place your triggers (trigger_use) where you want them and give them both
key: targetname value: lighttrigger
key: script_noteworthy value: room1
If you want a model to represent the light switch you can make one out brushes
and define it as a script_brushmodel or use an existing xmodel and define it
as a script_model. You can have any number of models coresponding to the triggers
and just follow the same naming convention. The script will still work ok if no models
are found, but you wont get a sound played when triggered.
key: targetname value: lightswitch
key: script_noteworthy value:room1
Now place your xmodels of the light_on type and give them both
key: targetname value: lightonmodel
key: script_noteworthy value: room1
And change them into script_models, dont leave them as misc_models!
Next place in the same position as the on models your xmodel light_off types and give them these keys,
key: targetname value: lightoffmodel
key: script_noteworthy value: room1
And change them into script_models, dont leave them as misc_models!
Now for the actual lights, create two script_models and position them on the light xmodels, then select
each in turn and give them these keys and values.
Key: targetname value: light
Key: script_noteworthy value: room1
Key: light value: 50
Note* I recomend you stay below 100 for light value or it will show through everything
Get the picture here?
All the above are related to each other by the script_noteworthy keys they contain.
Only the targetnames need to be different.
-------------------------------------------------------------------------------------------------------
To start this script use;
level thread maps\mp\light_control::main();
from your maps .gcs file
Of course make sure this script is in the same place as your maps .bsp and .gsc :-)
NOTE: the default distance this script moves the light entitys below the map is 1000 units,
if your lights are located high in your map and you have areas below the map and you
can still see them when they have beem moved by this script then just increase the
value of level.dist from 1000 to whatever you need to stop them being seen.
If you have problems you can usually get me in the forums of modsonline.
Enjoy! Grassy
==============================================================*/
main() {
level.dist = 1000; //increase this value if needed
// thread init_lights(); // to start your map with lights ON use this line
thread start_lights_off(); // to start your map with lights OFF use this line
}
init_lights() {
lightoff = getentarray ("lightoffmodel","targetname");
if ( isdefined(lightoff)) {
for (i = 0; i < lightoff.size; i++) {
lightoff[i] hide(); //hide the off models
}
}
lightswitch = getentarray ("lighttrigger","targetname"); //get all trigger_use brushes
if ( isdefined(lightswitch)) {
for (i = 0; i < lightswitch.size; i++) {
if(lightswitch[i].script_noteworthy == "room1")
lightswitch[i] thread light_handler(false); //monitor triggers
if(lightswitch[i].script_noteworthy == "bunker") //Not used - example only
lightswitch[i] thread light_handler(false); //maybe for lights in a bunker somewhere? :)
// Just add more lines like the examples above to include more lights starting in the ON state
}
}
}
start_lights_off() {
lighton = getentarray ("lightonmodel","targetname");
if ( isdefined(lighton)) {
for (i = 0; i < lighton.size; i++) {
lighton[i] hide(); //hide the on models
}
}
lightswitch = getentarray ("lighttrigger","targetname");
if ( isdefined(lightswitch)) {
for (i = 0; i < lightswitch.size; i++) {
if(lightswitch[i].script_noteworthy == "room1")
{
lightswitch[i] thread hide_lights(true); //hide the light entity
lightswitch[i] thread light_handler2(true); //monitor triggers
}
if(lightswitch[i].script_noteworthy == "bunker") //
{ // Not used
lightswitch[i] thread hide_lights(true); // This is an example of another
lightswitch[i] thread light_handler2(true); // entry for another location
} //
// Just add more lines exactly like the example lines above to include more lights starting in the OFF state.
}
}
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//-----------------------No need to touch these threads below----------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
hide_lights(state) {
self.id = self.script_noteworthy;
if(state == true)
{
state = false;
lights = getentarray ("light","targetname");
if(isdefined(lights)) {
for (i = 0; i < lights.size; i++) {
lights[i].id = lights[i].script_noteworthy;
if (lights[i].id == self.id) {
distdown = 0 - level.dist;
temp1 = spawn ("script_origin", lights[i].origin + (0, 0, distdown));
lights[i] moveto (temp1.origin,0.1,0.1,0);
temp1 delete();
}
}
}
lightoff = getentarray ("lightoffmodel","targetname");
if(isdefined(lightoff)) {
for (i = 0; i < lightoff.size; i++) {
lightoff[i].id = lightoff[i].script_noteworthy;
if (lightoff[i].id == self.id)
lightoff[i] show();
}
}
lighton = getentarray ("lightonmodel","targetname");
if(isdefined(lighton)) {
for (i = 0; i < lighton.size; i++) {
lighton[i].id = lighton[i].script_noteworthy;
if (lighton[i].id == self.id)
lighton[i] hide();
}
}
}
else if(state == false)
{
state = true;
lights = getentarray ("light","targetname");
if(isdefined(lights)) {
for (i = 0; i < lights.size; i++) {
lights[i].id = lights[i].script_noteworthy;
if(lights[i].id == self.id) {
distup = 0 + level.dist;
temp2 = spawn ("script_origin", lights[i].origin + (0, 0, distup));
lights[i] moveto (temp2.origin,0.1,0.1,0);
temp2 delete();
}
}
}
lighton = getentarray ("lightonmodel","targetname");
if(isdefined(lighton)) {
for (i = 0; i < lighton.size; i++) {
lighton[i].id = lighton[i].script_noteworthy;
if (lighton[i].id == self.id)
lighton[i] show();
}
}
lightoff = getentarray ("lightoffmodel","targetname");
if(isdefined(lightoff)) {
for (i = 0; i < lightoff.size; i++) {
lightoff[i].id = lightoff[i].script_noteworthy;
if (lightoff[i].id == self.id)
lightoff[i] hide();
}
}
}
}
//end of hide_lights thread
light_handler2(state)
{
while(1) {
self waittill ("trigger");
self.id = self.script_noteworthy;
//if you have switch models in the map with this targetname and script_noteworthy values this will play a sound on it.
ent = getentarray("lightswitch", "targetname");
if(isDefined(ent)) {
for (a = 0; a < ent.size; a++) {
ent[a].id = ent[a].script_noteworthy;
if (ent[a].id == self.id)
ent[a] playsound("switch");
}
}
if(state == false)
{
state = true;
lights = getentarray ("light","targetname");
if(isdefined(lights)) {
for (i = 0; i < lights.size; i++) {
lights[i].id = lights[i].script_noteworthy;
if (lights[i].id == self.id) {
distdown = 0 - level.dist;
temp1 = spawn ("script_origin", lights[i].origin + (0, 0, distdown));
lights[i] moveto (temp1.origin,0.1,0.1,0);
temp1 delete();
}
}
}
lightoff = getentarray ("lightoffmodel","targetname");
if(isdefined(lightoff)) {
for (i = 0; i < lightoff.size; i++) {
lightoff[i].id = lightoff[i].script_noteworthy;
if (lightoff[i].id == self.id)
lightoff[i] show();
}
}
lighton = getentarray ("lightonmodel","targetname");
if(isdefined(lighton)) {
for (i = 0; i < lighton.size; i++) {
lighton[i].id = lighton[i].script_noteworthy;
if (lighton[i].id == self.id)
lighton[i] hide();
}
}
}
else if(state == true)
{
state = false;
lights = getentarray ("light","targetname");
if(isdefined(lights)) {
for (i = 0; i < lights.size; i++) {
lights[i].id = lights[i].script_noteworthy;
if(lights[i].id == self.id) {
distup = 0 + level.dist;
temp2 = spawn ("script_origin", lights[i].origin + (0, 0, distup));
lights[i] moveto (temp2.origin,0.1,0.1,0);
temp2 delete();
}
}
}
lighton = getentarray ("lightonmodel","targetname");
if(isdefined(lighton)) {
for (i = 0; i < lighton.size; i++) {
lighton[i].id = lighton[i].script_noteworthy;
if (lighton[i].id == self.id)
lighton[i] show();
}
}
lightoff = getentarray ("lightoffmodel","targetname");
if(isdefined(lightoff)) {
for (i = 0; i < lightoff.size; i++) {
lightoff[i].id = lightoff[i].script_noteworthy;
if (lightoff[i].id == self.id)
lightoff[i] hide();
}
}
}
}
}
//end of start_lights_off thread
light_handler(state)
{
while(1) {
self waittill ("trigger");
self.id = self.script_noteworthy;
//if you have switch models in the map with this targetname and a
//script_noteworthy value that matches the triggers this will play a sound on it.
ent = getentarray("lightswitch", "targetname");
if(isDefined(ent)) {
for (a = 0; a < ent.size; a++) {
ent[a].id = ent[a].script_noteworthy;
if (ent[a].id == self.id)
ent[a] playsound("switch");
}
}
if(state == false)
{
state = true;
lights = getentarray ("light","targetname");
if(isdefined(lights)) {
for (i = 0; i < lights.size; i++) {
lights[i].id = lights[i].script_noteworthy;
if (lights[i].id == self.id) {
distdown = 0 - level.dist;
temp1 = spawn ("script_origin", lights[i].origin + (0, 0, distdown));
lights[i] moveto (temp1.origin,0.1,0.1,0);
temp1 delete();
}
}
}
lightoff = getentarray ("lightoffmodel","targetname");
if(isdefined(lightoff)) {
for (i = 0; i < lightoff.size; i++) {
lightoff[i].id = lightoff[i].script_noteworthy;
if (lightoff[i].id == self.id)
lightoff[i] show();
}
}
lighton = getentarray ("lightonmodel","targetname");
if(isdefined(lighton)) {
for (i = 0; i < lighton.size; i++) {
lighton[i].id = lighton[i].script_noteworthy;
if (lighton[i].id == self.id)
lighton[i] hide();
}
}
}
else if(state == true)
{
state = false;
lights = getentarray ("light","targetname");
if(isdefined(lights)) {
for (i = 0; i < lights.size; i++) {
lights[i].id = lights[i].script_noteworthy;
if(lights[i].id == self.id) {
distup = 0 + level.dist;
temp2 = spawn ("script_origin", lights[i].origin + (0, 0, distup));
lights[i] moveto (temp2.origin,0.1,0.1,0);
temp2 delete();
}
}
}
lighton = getentarray ("lightonmodel","targetname");
if(isdefined(lighton)) {
for (i = 0; i < lighton.size; i++) {
lighton[i].id = lighton[i].script_noteworthy;
if (lighton[i].id == self.id)
lighton[i] show();
}
}
lightoff = getentarray ("lightoffmodel","targetname");
if(isdefined(lightoff)) {
for (i = 0; i < lightoff.size; i++) {
lightoff[i].id = lightoff[i].script_noteworthy;
if (lightoff[i].id == self.id)
lightoff[i] hide();
}
}
}
}
}
//end of light_handler thread
basic idea: move the light out of the visible map, 'cause you can't change it's color or anything else in realtime (at least CoD-CoD2)
http://www.modsonline.com/Forums-top-72729.html