Advertisement
ZoriaRPG

ZC Fading Torches and Darkness v0.6

Nov 15th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.71 KB | None | 0 0
  1.  
  2. //import "std.zh"
  3.     //Remove the comment before the import directive
  4.     //instruction if you do not elsewhere call this.
  5.  
  6. /////////////////////////////////////
  7. // Scripted FADING Darkness Effect //
  8. //   v0.6 - 26th February, 2016    //
  9. //         By: ZoriaRPG            //
  10. ///////////////////////////////////////////////////////////////////////////////////
  11. // This script set creates a fading darkness effect when a lit torch set by the  //
  12. // constant 'CMB_LIT_TORCH'.                                                     //
  13. // ----------------------------------------------------------------------------- //
  14. // [ 1 ] Set-Up: Make a 'lit torch' combo, and assign its ID to the constant     //
  15. //       CMB_LIT_TORCH.                                                          //
  16. // [ 2 ] Set-Up: Make an 'unlit torch' combo, and assign its ID to the constant     //
  17. //       CMB_UNLIT_TORCH.                                                          //
  18. // [ 3 ] Find a colour swatch in your palette that is ALWAYS BLACK and assign    //
  19. //       its value (in hex, pref.) to the constant 'COLOUR_BALCK'.               //
  20. // [ 4 ] Determine how many 'fading' laywrs of darkness you desire.              //
  21. //       The default is '6', using layers 1 through 6. Enable ordisable what     //
  22. //       Layers you want the darkness effect on, by setting their values to      //
  23. //       '1' in the SETTINGS section (below).                                    //
  24. // [ 5 ] Assign timer values. This comes with defaults, but you may wish to      //
  25. //       adjust them.                                                            //
  26. // [ 6 ] Set up a combo ont he screenwith a secret flag (burn).                  //
  27. //       Set the 'burn' combo in Screen->Secret Combos to match the combo that   //
  28. //       you selected as 'CMB_LIT_TORCH'.                                        //
  29. ///////////////////////////////////////////////////////////////////////////////////
  30.  
  31. //////////////
  32. // SETTINGS //
  33. //////////////
  34.  
  35. //Combo and Colour Constants
  36.  
  37. const int CMB_LIT_TORCH             = 0;    //Set to the combo number of the torch in its lit state.
  38. const int CMB_UNLIT_TORCH           = 0;    //Set tot he combo number of a torch to light.
  39. const int DARK_OPACITY              = 64;   //Set to the opacity for darkness (64 is translucent;
  40.                                             //best to leave alone).
  41. const int COLOUR_BLACK              = 0x91; //Set to the colour for black in your palette in hex.
  42. const int SFX_LIGHTING_TORCH        = 0;    //Set to sound effect to play when lighting a torch.
  43.  
  44. //Timer Values
  45. const int TIME_INIT_DELAY_LIGHTING_ROOM         = 10; //Initial delay before marking the room lit: 20 frames.
  46. const int TIME_INTERPOLERATED_FADING_DARKNESS   = 20; //30 frames per layer
  47.  
  48.  
  49. //Settings: Draw darkness (for fade effect) on these layers (0 no, 1 yes)
  50. const int DARK_LAYER_0 = 0;
  51. const int DARK_LAYER_1 = 1;
  52. const int DARK_LAYER_2 = 1;
  53. const int DARK_LAYER_3 = 1;
  54. const int DARK_LAYER_4 = 1;
  55. const int DARK_LAYER_5 = 1;
  56. const int DARK_LAYER_6 = 1;
  57.  
  58. /////////////////////
  59. // ARRAYS AND VARS //
  60. /////////////////////
  61.  
  62. float GRAM[214727]; //Global Array to hold values.
  63.  
  64. //Global RAM indices.
  65.     //1900 to 1999
  66. const int GR_MAIN                   = 1900; //Dictates if the main loop should run.
  67. const int GR_SCREENSHANGED          = 1901; //Screen, DMap, and Map changes.
  68. const int GR_LASTSCREEN             = 1902;
  69. const int GR_DMAPCHANGED            = 1903;
  70. const int GR_LASTDMAP               = 1904;
  71. const int GR_MAPCHANGED             = 1905;
  72. const int GR_LASTMAP                = 1906;
  73. const int GR_OLDSCREEN              = 1907;
  74. const int GR_OLDDMAP                = 1908;
  75.     // 2000 to 2099
  76. const int TORCH_LIT_THIS_SCREEN     = 2000;
  77. const int TORCH_LIGHT_TIMER         = 2001;
  78. const int TORCH_LIGHT_TIMER_LAYER0  = 2002;
  79. const int TORCH_LIGHT_TIMER_LAYER1  = 2003;
  80. const int TORCH_LIGHT_TIMER_LAYER2  = 2004;
  81. const int TORCH_LIGHT_TIMER_LAYER3  = 2005;
  82. const int TORCH_LIGHT_TIMER_LAYER4  = 2006;
  83. const int TORCH_LIGHT_TIMER_LAYER5  = 2007;
  84. const int TORCH_LIGHT_TIMER_LAYER6  = 2008;
  85. const int ROOM_FULLY_LIT            = 2009;
  86.  
  87.  
  88. /////////////////////////////////////
  89. // DARKNESS AND LIGHTING FUNCTIONS //
  90. /////////////////////////////////////
  91.  
  92. //Resets timers for darkness effects.
  93. //Call before main loop.
  94. void InitTorchTimers(){
  95.     GRAM[TORCH_LIT_THIS_SCREEN] = 0;
  96.     GRAM[TORCH_LIGHT_TIMER] = TIME_INIT_DELAY_LIGHTING_ROOM;
  97.     for ( int q = TIME_INIT_DELAY_LIGHTING_ROOM; q <= TORCH_LIGHT_TIMER_LAYER6; q++ ){
  98.         if ( q == 0 && DARK_LAYER_0 ) GRAM[q] = TIME_INTERPOLERATED_FADING_DARKNESS;
  99.         if ( q == 1 && DARK_LAYER_1 ) GRAM[q] = TIME_INTERPOLERATED_FADING_DARKNESS;
  100.         if ( q == 2 && DARK_LAYER_2 ) GRAM[q] = TIME_INTERPOLERATED_FADING_DARKNESS;
  101.         if ( q == 3 && DARK_LAYER_3 ) GRAM[q] = TIME_INTERPOLERATED_FADING_DARKNESS;
  102.         if ( q == 4 && DARK_LAYER_4 ) GRAM[q] = TIME_INTERPOLERATED_FADING_DARKNESS;
  103.         if ( q == 5 && DARK_LAYER_5 ) GRAM[q] = TIME_INTERPOLERATED_FADING_DARKNESS;
  104.         if ( q == 6 && DARK_LAYER_6 ) GRAM[q] = TIME_INTERPOLERATED_FADING_DARKNESS;
  105.     }
  106. }
  107.  
  108. void LightUnlitTorches(){
  109.     for ( int q = 0; q <= Screen->NumLWeapons(); q++ ){
  110.         lweapon l = Screen->LoadLWeapon(q);
  111.         if ( l->ID == LW_FIRE ) {
  112.             //Layers
  113.             for ( int w = 0; w <= 6; w++ ) {
  114.                 if (Screen->LayerMap(w) != -1) {
  115.                     for ( int e = 0; e < 176; e++ ){
  116.                         if ( Screen->ComboD[e] == CMB_UNLIT_TORCH && Collision(w,l) ) {
  117.                             if ( SFX_LIGHTING_TORCH ) Game->PlaySound(SFX_LIGHTING_TORCH);
  118.                             SetLayerComboD(w,e,CMB_LIT_TORCH);
  119.                         }
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.     }
  125. }
  126.                    
  127.                    
  128.  
  129. //The primary function. Call before Waitdraw.
  130. void Torches(){
  131.     ClearTorchTimersOnScreenChange();
  132.     LightRoom();
  133. }
  134.  
  135. //Called by Torches(), but also stores if the screen changes.
  136. void ClearTorchTimersOnScreenChange(){
  137.     if ( ScreenChanged() || DMapChanged() ) {
  138.         InitTorchTimers();
  139.         RoomLit(0,true); //Clears that the room is lit.
  140.     }
  141. }
  142.  
  143. //The primarycomponent function, called by Torches()
  144. void LightRoom(){
  145.     if ( !RoomLit() ) {
  146.         bool lit;
  147.             if ( !lit ) {
  148.                 for ( int q = 0; q <= 6; q++ ) { //layers
  149.                     if (Screen->LayerMap(q) != -1) {
  150.                         for ( int w = 0; q < 176; w++ ){
  151.                             if ( Screen->ComboD[q] == CMB_LIT_TORCH ) lit = true;
  152.                         }
  153.                     }
  154.                 }
  155.             }
  156.             if ( lit ) {
  157.                 if ( MainTorchTimer() ) MainTorchTimer(true);
  158.                 else if ( !MainTorchTimer() {
  159.                     if ( !(Screen->Lit ) ) Screen->Lit = true;
  160.                     for ( int e = 0; e <= 6; e++ ) {
  161.                         //Layer0
  162.                        
  163.                         if ( LayerTorchTimer(e) && !RoomLit(e) ) {
  164.                             Screen->Rectangle(e,0,0,256,176,COLOUR_BLACK,-1,0,0,0,true,DARK_OPACITY);
  165.                             LayerTorchTimer(e,true);
  166.                             break;
  167.                         }
  168.                         if ( !LayerTorchTimer(e) && RoomLit(e) < e+1 ) RoomLit(e,false);
  169.                     }
  170.                 }
  171.             }
  172.         }
  173.     }
  174. }
  175.    
  176.  
  177. //////////////////////////////////
  178. // GlobalRAM ACCESSOR FUNCTIONS //
  179. //////////////////////////////////
  180.  
  181. //Returns the timer value for system darkness release.
  182. int MainTorchTimer(){
  183.     return GRAM[TORCH_LIGHT_TIMER];
  184. }
  185.  
  186. //Decreases the main torch timer.
  187. void MainTorchTimer(bool dec){
  188.     GRAM[TORCH_LIGHT_TIMER]--;
  189. }
  190.  
  191. //Returns the fading timer for a specific layer.
  192. int LayerTorchTimer(int layer){
  193.     return GRAM[TORCH_LIGHT_TIMER_LAYER0 + layer];
  194. }
  195.  
  196. //Decreases the timer for fading darkness by '1' for a specific layer of scripted drawing.
  197. //Note that you do not need to have these layers set up.
  198. int LayerTorchTimer(int layer, bool dec){
  199.     GRAM[TORCH_LIGHT_TIMER_LAYER0 + layer]--;
  200. }
  201.  
  202. //Returns true if a room is fully lit.
  203. int RoomLit(){
  204.     return GRAM[ROOM_FULLY_LIT] == 7;
  205. }
  206.  
  207. //Returns true if the darkness effect for a given layer is in effect.
  208. bool RoomLit(int layer){
  209.     return RoomLit() == layer + 1;
  210. }
  211.  
  212. //Sets what layer we're up to in removing all layers of darkness.
  213. //Can clear all layers by calling RoomLit(0,true)
  214. void RoomLit(int layer, bool clear){
  215.     if ( clear ) GRAM[ROOM_FULLY_LIT] = 0;
  216.     else GRAM[ROOM_FULLY_LIT] = layer+1;
  217. }
  218.  
  219.  
  220. //! Functions for checking screen and DMap changes.
  221.  
  222. //Returns if the screen has changed, and stores the values as an update at at one time.
  223. bool ScreenChanged(){
  224.     if ( Game->GetCurScreen() != GRAM(GR_LASTSCREEN) ) {
  225.         GRAM(GR_OLDSCREEN,GRAM(GR_LASTSCREEN));
  226.         GRAM(GR_LASTSCREEN, Game->GetCurScreen());
  227.         GRAM(GR_SCREENCHANGED,1);
  228.        
  229.         return true;
  230.     }
  231.     else {
  232.         GRAM(GR_SCREENCHANGED,0);
  233.         return false;
  234.     }
  235. }
  236.  
  237. //Returns if the DMap has changed, and stores the values as an update at at one time.
  238. bool DMapChanged(){
  239.     if ( Game->GetCurDMap() != GRAM(GR_LASTDMAP) ) {
  240.         GRAM(GR_OLDDMAP,GRAM(GR_LASTDMAP);
  241.         GRAM(GR_LASTDMAP, Game->GetCurDMap());
  242.         GRAM(GR_DMAPCHANGED,1);
  243.         return true;
  244.     }
  245.     else {
  246.         GRAM(GR_DMAPCHANGED,0);
  247.         return false;
  248.     }
  249. }
  250.  
  251. //Returns if the Map (NOT DMap!) has changed, and stores the values as an update at at one time.
  252. bool MapChanged(){
  253.     if ( Game->GetCurMap() != GRAM(GR_LASTMAP) ) {
  254.         GRAM(GR_OLDMAP,GRAM(GR_LASTMAP));
  255.         GRAM(GR_LASTMAP, Game->GetCurMap());
  256.         GRAM(GR_MAPCHANGED,1);
  257.         return true;
  258.     }
  259.     else {
  260.         GRAM(GR_MAPCHANGED,0);
  261.         return false;
  262.     }
  263. }
  264.  
  265. ///////////////////
  266. // GLOBAL SCRIPT //
  267. ///////////////////
  268.  
  269. global script active_example_timed_fading_torch_darkness{
  270.     void run() {
  271.         main(true); //Enable the main loop.
  272.         InitTorchTimers(); //Initialise the timers for darkness.
  273.         while( main() ) {       //! I do this, instead of while(true) to allow suspending it,
  274.                                 //! and changing to an alternate global loop, as an option.
  275.             LightUnlitTorches(); //Lights torches on collision with fire lweapon without needing a secret combo.
  276.             Torches(); //The main function for custom darkness.
  277.      
  278.             Waitdraw();
  279.  
  280.             Waitframe();
  281.         }
  282.     }
  283.     int main() { return GRAM(GR_MAIN); }
  284.  
  285.     void main(bool enabled) {
  286.         if ( enabled ) GRAM(GR_MAIN,1);
  287.         else GRAM(GR_MAIN,0);
  288.     }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement