Dynamic Lights & Shadows

Dynamic Lights & Shadows
Version: 1.5

Introduction

Based on the Rataime's Shadow script, I've rewrote it, using a clearer code in my opinion, and adding new features to make it a bit more realistics.

Features
Basically the same as Rataime's Shadow script. But!
  • Shadows overlay, the system know when to display the shadow or not
  • Possibility to control the source: turn the light on/off, change its direction, make it move
  • Height of objects is handled, no more 1 event per tile for the shadow of a pillar
  • Auto-Screen tint according to the number of lights off
videos
http://rmxp.trebor777.net/rmxp/dynamic_lights_and_shadows.avi
10Mb, H.264, Tell me if you can't read it.
http://rmxp.trebor777.net/rmxp/Dynamic%20Light%20and%20Shadow.wmv - 6.7Mb
Demo

Link

Script

script Spoiler
  1. =begin
  2. ==============================================================================
  3.  ** Dynamic Light & Shadows
  4. ------------------------------------------------------------------------------
  5.  Trebor777
  6.  Version 1.5
  7.  16/11/2007
  8.  Version 1.5 is based on Rataime's shadows script,Rewrote the code structure
  9.  and Added extra features
  10.  
  11. ==============================================================================
  12. Instructions
  13. ==============================================================================
  14. To create a source light:
  15. Write a comment on 2 lines
  16.   Light
  17.   arg1|arg2|arg3|arg4|arg5
  18.  
  19. arg1, is the minimum angle.
  20. arg2, is the maximum angle
  21. arg3, the distance in pixel covered by the light
  22. arg4, the height, in tiles where the light is.
  23. arg5, the direction of the light, use the same numbers as RMXP is using:
  24. up: 8, right: 6, down: 2, left: 4
  25. It will turn the event automatically to this direction and
  26. draw the light according to it, using its min and max angle.
  27.  
  28. The 0 is always on the left of the direction.
  29. example,
  30. the light "look at" the right, so its direction is 6.
  31. the min angle is 0, so it'll start from the tile above to whatever
  32.  the max angle is, in a clockwise way.
  33.  
  34. So if you need to create a light, who covers only a cone of 60?, facing to the
  35. right, at a height of 1 tile, covering a radius of 150pixels:
  36.     Light
  37.     60|120|150|1|6
  38.    
  39.    
  40. I might do in the future, a simpler version for that.
  41. _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
  42. To have an event with a shadow:
  43. Write a comment on 2 lines
  44.   Shadow
  45.   arg1
  46.  
  47. arg1, is the maximum height of the object, so if you have a pillar on your map
  48. covering several tiles in height, just create a shadow event at its base,
  49. and give the height of that pillar as arg1.
  50.  
  51. For characters, just use a height of 0.
  52.  
  53. To turn off/on a light:
  54. Use a script call:
  55. a simple "self.off=true" (to turn off) or "self.off=false" to turn on is needed.
  56. What is important is where you use this script call:
  57.  
  58. If the event switching the light is not the same as the source, you need use the
  59. "call script" command inside the "set move route" command
  60.   (of course, don't forget to say on which event it applies)
  61. instead of the default "call script" command found on page3.
  62.  
  63. ==============================================================================
  64. Configuration
  65. ==============================================================================
  66. ==============================================================================
  67.  You probably won't need to touch this : it's the 'map' of how to display the
  68.  shadow depending on the event's direction and his relative position to the
  69.  source. a minus means the shadow is mirrored. It seems complex, and it is.
  70.  Complain to Enterbrain (why didn't they use something clockwise or counter-
  71.  clockwise ? I suspect it's because of the rm2k legacy. More explanations
  72.  below.
  73. =============================================================================
  74. =end
  75. SDK.log('DL&S', "trebor777", 1, "16.11.07")
  76. if SDK.state('DL&S')
  77.  
  78. SHADOWS_DIRECTION_ARRAY = Array.new
  79. SHADOWS_DIRECTION_ARRAY[2] = [ -3, 4, -2, 1 ]
  80. SHADOWS_DIRECTION_ARRAY[4] = [ 4, -2, 1, -3 ]
  81. SHADOWS_DIRECTION_ARRAY[6] = [ 1, -3, 4, -2 ]
  82. SHADOWS_DIRECTION_ARRAY[8] = [ -2, 1, -3, 4 ]
  83.  
  84. #==============================================================================
  85. # An important option : if you set it to true, the shadows will get longer if
  86. # you are far from the source. Nice, but induces lag : it will eat your CPU,
  87. # and quite possibly your first born if you try that on a big map.
  88. #==============================================================================
  89. SHADOW_GETS_LONGER = true
  90.  
  91. #==============================================================================
  92. # Misc options
  93. # If an event has its opacity below SHADOWS_OPACITY_THRESHOLD, no shadow will
  94. # be displayed.
  95. # Set SHADOWS_CATERPILLAR_COMPATIBLE to true if you uses the caterpillar script
  96. #==============================================================================
  97. SHADOWS_OPACITY_THRESHOLD = 254
  98. SHADOWS_CATERPILLAR_COMPATIBLE = true
  99.  
  100. #==============================================================================
  101. # Class Light
  102. #==============================================================================
  103. class Light
  104.   @@lights=[]
  105.   attr_reader :character, :parameters
  106.   #--------------------------------------------------------------------------
  107.   def initialize(viewport,character, param)
  108.     @character = character
  109.     @viewport = viewport
  110.     unless param.nil?
  111.       @parameters = param[0].split('|')
  112.       @anglemin =  @parameters.first.to_i
  113.       @anglemax = @parameters[1].to_i
  114.       @distancemax = @parameters[2].to_i
  115.       @light_height = @parameters[3].to_i
  116.       @base_face_at = @parameters.last.to_i
  117.       @character.direction=@base_face_at
  118.       test = @@lights.find_all{|l| l.character==@character and l.parameters==@parameters}
  119.       if test.size==0
  120.         draw_lights
  121.         update
  122.         @@lights.push(self)
  123.       end
  124.     end
  125.   end
  126.   #--------------------------------------------------------------------------
  127.   def Light.set
  128.     return @@lights
  129.   end
  130.   #--------------------------------------------------------------------------
  131.   def Light.off_size
  132.     result=0
  133.     @@lights.each do |light|
  134.       result+=1 if light.character.off
  135.     end
  136.     return result
  137.   end
  138.   #--------------------------------------------------------------------------
  139.   def dispose
  140.     @s_light.dispose
  141.     @@lights.delete(self)
  142.   end
  143.   #--------------------------------------------------------------------------
  144.   def update
  145.     unless @s_light.nil?
  146.       @[email protected]
  147.       @s_light.angle=0 if @character.direction==2
  148.       @s_light.angle=-90 if @character.direction==4
  149.       @s_light.angle=180 if @character.direction==8
  150.       @s_light.angle=90 if @character.direction==6
  151.       @[email protected]_x
  152.       @[email protected]_y+32*@light_height
  153.       @[email protected]_z
  154.     end
  155.   end
  156.   #--------------------------------------------------------------------------
  157.   def draw_lights
  158.  
  159.     return if @@lights.include?(self) or (@@lights.find_all{|l| l.character==@character and l.parameters==@parameters}).size>0
  160.    
  161.     radius = @distancemax
  162.     @s_light=Sprite.new(@viewport)
  163.     @[email protected]_x
  164.     @[email protected]_y+32*@light_height
  165.     @[email protected]_z
  166.     @s_light.bitmap=Bitmap.new(radius*2,radius*2)
  167.     @s_light.opacity=90
  168.     @s_light.ox+=radius
  169.     @s_light.oy+=radius
  170.     @s_light.angle=0 if @base_face_at==2
  171.     @s_light.angle=270 if @base_face_at==4
  172.     @s_light.angle=180 if @base_face_at==8
  173.     @s_light.angle=90 if @base_face_at==6
  174.     @s_light.bitmap.draw_pie(radius,radius,radius,Color.new(255,255,100,90),@anglemin, @anglemax)
  175.   end
  176. end
  177. #==============================================================================
  178. # Class shadow
  179. #==============================================================================
  180. class Shadow
  181.   attr_accessor :character
  182.   attr_reader :distance, :distancemax, :overlayed
  183.   @@shadows=[]
  184.   #--------------------------------------------------------------------------
  185.   def initialize(viewport,character, param,light)
  186.     @character = character
  187.     @viewport = viewport
  188.     unless param.nil?
  189.       @parameters = param[0].split('|')
  190.       @shadow_max_height = @parameters[1].to_i
  191.       @anglemin=light.parameters[0].to_f
  192.       @anglemax=light.parameters[1].to_f
  193.       @distancemax=light.parameters[2].to_f
  194.       @light_height= light.parameters[3].to_i
  195.       @source = light
  196.       @s_shadow = RPG::Sprite.new(@viewport)
  197.       @s_shadow.color = Color.new(0, 0, 0)
  198.       update
  199.     end
  200.     @@shadows.push(self)
  201.   end
  202.   #--------------------------------------------------------------------------
  203.   def Shadow.set
  204.     return @@shadows
  205.   end
  206.   #--------------------------------------------------------------------------
  207.   def sprite
  208.     return @s_shadow
  209.   end
  210.   #--------------------------------------------------------------------------
  211.   def dispose
  212.     @s_shadow.dispose
  213.     @@shadows.delete(self)
  214.   end
  215.   #--------------------------------------------------------------------------
  216.   def in_light_range?
  217.     return (@distance<=@distancemax)
  218.   end
  219.   #--------------------------------------------------------------------------
  220.   def overlayed?
  221.     @overlayed =  false
  222.     @@shadows.each do |i|
  223.       s = i.sprite
  224.       next if s.nil? or i == self or @character.tile_id!=0 or s.disposed?
  225.       if (@s_shadow.z)>s.z and @s_shadow.angle.between?(s.angle-1.5,s.angle+1.5)\
  226.        and ((@character.x-i.character.x)**2 + (@character.y-i.character.y)**2)**0.5<=s.zoom_y \
  227.        and s.z>=0
  228.         @s_shadow.visible=false
  229.         @overlayed = true
  230.       end
  231.       return if !@s_shadow.visible
  232.     end
  233.    
  234.     return @overlayed
  235.   end
  236.   #--------------------------------------------------------------------------
  237.   def update
  238.     # set shadow visibility according to the light state
  239.     @s_shadow.visible = [email protected]
  240.     if @character.transparent or @character.opacity <= SHADOWS_OPACITY_THRESHOLD
  241.       @s_shadow.visible = false
  242.       return
  243.     end
  244.     if @old_amin.nil? and @old_amax.nil? and @old_dir.nil?
  245.       @old_amin= @anglemin
  246.       @old_amax= @anglemax
  247.       @old_dir = @source.character.direction
  248.     end
  249.     # adapt the angle according to the source direction
  250.     case @source.character.direction
  251.       when 2
  252.         @anglemin= @old_amin+180
  253.         @anglemax= @old_amax+180
  254.       when 4
  255.         @anglemin= @old_amin+90
  256.         @anglemax= @old_amax+90
  257.       when 8
  258.         @anglemin= @old_amin
  259.         @anglemax= @old_amax
  260.       when 6
  261.         @anglemin= @old_amin+270
  262.         @anglemax= @old_amax+270
  263.       end
  264.     if @[email protected]
  265.       @old_dir = @source.character.direction
  266.     end
  267.     # simplify the angles if more than 360 or less than 0
  268.     @anglemin%=360 ;@anglemax%=360
  269.    
  270.     tile_height= @shadow_max_height
  271.     @deltax=(@[email hidden]_x)/4
  272.     @deltay= ((@source.character.real_y+@light_height*128)-(@character.real_y+tile_height*128))/4
  273.     @distance = (((@deltax ** 2) + (@deltay ** 2))** 0.5)
  274.     if @distancemax !=0 and @distance>@distancemax
  275.       @s_shadow.visible = false
  276.       return
  277.     end
  278.    
  279.     @s_shadow.angle = 57.3*Math.atan2(@deltax, @deltay )
  280.     @angle_trigo= (@s_shadow.angle+90) % 360
  281.    
  282.     #test if there is a shadow above it, from something close,and then
  283.     # don't display its shadow
  284.     return if overlayed?
  285.    
  286.     if @anglemin !=0 or @anglemax !=0
  287.       if (@angle_trigo<@anglemin or @angle_trigo>@anglemax) and \
  288.           @anglemin<@anglemax
  289.         @s_shadow.visible = false
  290.         return
  291.       elsif (@angle_trigo<@anglemin and @angle_trigo>@anglemax) and \
  292.              @anglemin>@anglemax
  293.         @s_shadow.visible = false
  294.         return
  295.       end    
  296.     end
  297.     @s_shadow.update
  298.     if @tile_id != @character.tile_id or
  299.        @character_name != @character.character_name or
  300.        @character_hue != @character.character_hue
  301.        @tile_id = @character.tile_id
  302.       @character_name = @character.character_name
  303.       @character_hue = @character.character_hue
  304.       if @tile_id >= 384
  305.         @s_shadow.bitmap = RPG::Cache.tile($game_map.tileset_name,
  306.         @tile_id, @character.character_hue)
  307.         @s_shadow.src_rect.set(0, 0, 32, 32)
  308.         @s_shadow.ox = 16
  309.         @s_shadow.oy = 32
  310.       else
  311.         @s_shadow.bitmap = RPG::Cache.character(@character.character_name,
  312.           @character.character_hue)
  313.         @cw = @s_shadow.bitmap.width / 4
  314.         @ch = @s_shadow.bitmap.height / 4
  315.         @s_shadow.ox = @cw / 2
  316.         @s_shadow.oy = @ch
  317.       end
  318.     end
  319.     @s_shadow.x = @character.screen_x
  320.     @s_shadow.y = @character.screen_y-8
  321.     @s_shadow.z = @character.screen_z(@ch)-1
  322.    
  323.     if @tile_id!=0 # if the sprite graphic is from the tileset
  324.       # set the Z-Index using the tileset priority settings
  325.       @s_shadow.z = ($game_map.priorities[@tile_id])*32
  326.       # convert the shadow angle, into 8-directions (0-7)
  327.       direction_shadow=((@s_shadow.angle/45+45/2.0+90).round)%8
  328.       # for the middle-Top and top layers,
  329.       for i in [1, 2]
  330.         # get the tile from the converted angle
  331.         tile_around=[$game_map.data[@character.x,@character.y-1,i],
  332.         $game_map.data[@character.x-1,@character.y-1,i],
  333.         $game_map.data[@character.x-1,@character.y,i],
  334.         $game_map.data[@character.x-1,@character.y+1,i],  
  335.         $game_map.data[@character.x,@character.y+1,i],
  336.         $game_map.data[@character.x+1,@character.y+1,i],
  337.         $game_map.data[@character.x+1,@character.y,i],
  338.         $game_map.data[@character.x+1,@character.y-1,i]]
  339.         # if the tile is the empty one go to the next layer
  340.         next if tile_around[direction_shadow]==0
  341.         # else, lower the Z-index if the tile around is "above" or at the same
  342.         # priority of our tile
  343.         @s_shadow.z-=32 if $game_map.priorities[tile_around[direction_shadow]]>=$game_map.priorities[@tile_id]
  344.       end
  345.     end
  346.     if @tile_id == 0
  347.       sx = @character.pattern * @cw
  348.       quarter = ((@angle_trigo/90+0.5).floor)%4
  349.      
  350.       # The quarter is the position of the event relative to the source.
  351.       # Imagine the source is the o point (0,0). Trace the 2 lines
  352.       # y=x and y=-x : you get something like a big X
  353.       # On the right, quarter=0. Up, quarter = 1, and so on
  354.       # Take the @character.direction row (2,4,6,8), and the quarter
  355.       # column (0,1,2,3) (remember, it starts at 0), and you'll get
  356.       # a number between 1 and 4. It correspond to the row of the charset
  357.       # the shadow will be, and mirrored if negative.
  358.       # Yes, it isn't obvious, but I didn't find any simple operation to
  359.       # get those.
  360.       magic = SHADOWS_DIRECTION_ARRAY[@character.direction][quarter]
  361.       magic = -magic
  362.       if magic < 0
  363.         @s_shadow.mirror = true
  364.         magic = -magic
  365.       else
  366.         @s_shadow.mirror = false
  367.       end
  368.       sy = (magic-1)*@ch
  369.       @s_shadow.src_rect.set(sx, sy, @cw, @ch)
  370.     end
  371.     # This is the formula of the opacity in function of the distance
  372.     # ** 2 means square
  373.     @s_shadow.opacity = 1200/((@distance ** 2)/ 1000 + 6)
  374.     # This is the formula of the size in function of the distance
  375.     # The 0.75 is here so you have a size of 1:1 when next to the source.
  376.     @s_shadow.zoom_y=0.75*(@shadow_max_height+1) + (@distance) / 256 if SHADOW_GETS_LONGER
  377.   end
  378. end
  379. #==============================================================================
  380. # ** Zlib    
  381. #==============================================================================
  382.  
  383. module Zlib
  384.   #============================================================================
  385.   # ** Png_File    
  386.   #============================================================================
  387.  
  388.   class Png_File < GzipWriter
  389.     #--------------------------------------------------------------------------
  390.     # * Make PNG
  391.     #--------------------------------------------------------------------------
  392.     def make_png(bitmap, mode = 0)
  393.       # Save Bitmap & Mode
  394.       @bitmap, @mode = bitmap, mode
  395.       # Create & Save PNG
  396.       self.write(make_header)
  397.       self.write(make_ihdr)
  398.       self.write(make_idat)
  399.       self.write(make_iend)
  400.     end
  401.     #--------------------------------------------------------------------------
  402.     # * Make Header
  403.     #--------------------------------------------------------------------------
  404.     def make_header
  405.       return [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a].pack('C*')
  406.     end
  407.     #--------------------------------------------------------------------------
  408.     # * Make IHDR
  409.     #--------------------------------------------------------------------------
  410.     def make_ihdr
  411.       ih_size               = [13].pack("N")
  412.       ih_sign               = 'IHDR'
  413.       ih_width              = [@bitmap.width].pack('N')
  414.       ih_height             = [@bitmap.height].pack('N')
  415.       ih_bit_depth          = [8].pack('C')
  416.       ih_color_type         = [6].pack('C')
  417.       ih_compression_method = [0].pack('C')
  418.       ih_filter_method      = [0].pack('C')
  419.       ih_interlace_method   = [0].pack('C')
  420.       string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
  421.                ih_compression_method + ih_filter_method + ih_interlace_method
  422.       ih_crc = [Zlib.crc32(string)].pack('N')
  423.       return ih_size + string + ih_crc
  424.     end
  425.     #--------------------------------------------------------------------------
  426.     # * Make IDAT
  427.     #--------------------------------------------------------------------------
  428.     def make_idat
  429.       header  = "\x49\x44\x41\x54"
  430.       data    = @mode == 0 ? make_bitmap_data0 : make_bitmap_data1
  431.       data    = Zlib::Deflate.deflate(data, 8)
  432.       crc     = [Zlib.crc32(header + data)].pack('N')
  433.       size    = [data.length].pack('N')
  434.       return size + header + data + crc
  435.     end
  436.     #--------------------------------------------------------------------------
  437.     # * Make Bitmap Data 0
  438.     #--------------------------------------------------------------------------
  439.     def make_bitmap_data0
  440.       gz = Zlib::GzipWriter.open('hoge.gz')
  441.       t_Fx = 0
  442.       w = @bitmap.width
  443.       h = @bitmap.height
  444.       data = []
  445.       for y in 0...h
  446.         data.push(0)
  447.         for x in 0...w
  448.           t_Fx += 1
  449.           if t_Fx % 10000 == 0
  450.             Graphics.update
  451.           end
  452.           if t_Fx % 100000 == 0
  453.             s = data.pack("C*")
  454.             gz.write(s)
  455.             data.clear
  456.           end
  457.           color = @bitmap.get_pixel(x, y)
  458.           red = color.red
  459.           green = color.green
  460.           blue = color.blue
  461.           alpha = color.alpha
  462.           data.push(red)
  463.           data.push(green)
  464.           data.push(blue)
  465.           data.push(alpha)
  466.         end
  467.       end
  468.       s = data.pack("C*")
  469.       gz.write(s)
  470.       gz.close  
  471.       data.clear
  472.       gz = Zlib::GzipReader.open('hoge.gz')
  473.       data = gz.read
  474.       gz.close
  475.       File.delete('hoge.gz')
  476.       return data
  477.     end
  478.     #--------------------------------------------------------------------------
  479.     # * Make Bitmap Data Mode 1
  480.     #--------------------------------------------------------------------------
  481.     def make_bitmap_data1
  482.       w = @bitmap.width
  483.       h = @bitmap.height
  484.       data = []
  485.       for y in 0...h
  486.         data.push(0)
  487.         for x in 0...w
  488.           color = @bitmap.get_pixel(x, y)
  489.           red = color.red
  490.           green = color.green
  491.           blue = color.blue
  492.           alpha = color.alpha
  493.           data.push(red)
  494.           data.push(green)
  495.           data.push(blue)
  496.           data.push(alpha)
  497.         end
  498.       end
  499.       return data.pack("C*")
  500.     end
  501.     #--------------------------------------------------------------------------
  502.     # * Make IEND
  503.     #--------------------------------------------------------------------------
  504.     def make_iend
  505.       ie_size = [0].pack('N')
  506.       ie_sign = 'IEND'
  507.       ie_crc  = [Zlib.crc32(ie_sign)].pack('N')
  508.       return ie_size + ie_sign + ie_crc
  509.     end
  510.   end
  511. end
  512. #==============================================================================
  513. # Class Game Event
  514. #==============================================================================
  515. class Game_Event
  516.   alias treb_mobile_source_game_event_initialize initialize
  517.   attr_accessor :off, :direction
  518.   def initialize(*args)
  519.     # create the off attribute, only used for the lights
  520.     @off = false
  521.     treb_mobile_source_game_event_initialize(*args)
  522.   end
  523. end
  524. #==============================================================================
  525. # class Spriteset Map
  526. #==============================================================================
  527. class Spriteset_Map
  528.   alias trebor777_DLS_spriteset_map_init_characters init_characters
  529.   alias trebor777_DLS_spriteset_map_update_character_sprites update_character_sprites
  530.   alias trebor777_DLS_spriteset_map_dispose dispose
  531.   #--------------------------------------------------------------------------
  532.   def init_characters
  533.     trebor777_DLS_spriteset_map_init_characters
  534.     # create the lights and shadows arrays
  535.     @lights = []
  536.     @shadows = []
  537.     @viewport1.visible=false
  538.     @viewport2.visible=false
  539.     @viewport3.visible=false
  540.     for i in $game_map.events.keys.sort
  541.       # Search for light trigger
  542.       light_param = SDK.event_comment_input($game_map.events[i], 1, "Light")
  543.       # if not found go to the next iteration
  544.       next if light_param.nil?
  545.       # else add it to the Lights Hash, and create a new light
  546.       @lights.push( Light.new(@viewport1,$game_map.events[i],light_param) )
  547.       # create the shadow for the player.
  548.       @shadows.push( Shadow.new(@viewport1,$game_player, ['0'], @lights.last) )
  549.     end
  550.     for i in $game_map.events.keys.sort
  551.       for light in @lights
  552.         # Search for shadow trigger
  553.         shadow_param = SDK.event_comment_input($game_map.events[i], 1, "Shadow")
  554.         next if shadow_param.nil?
  555.         @shadows.push( Shadow.new(@viewport1,$game_map.events[i], shadow_param, light) )
  556.       end
  557.     end
  558.     @viewport1.visible=true
  559.     @viewport2.visible=true
  560.     @viewport3.visible=true
  561.   end
  562.   #--------------------------------------------------------------------------
  563.   def update_character_sprites
  564.     trebor777_DLS_spriteset_map_update_character_sprites
  565.     lights_off = 0
  566.     # Update the lights
  567.     for light in @lights
  568.       light.update
  569.       # count the number of lights off
  570.       lights_off+=1 if light.character.off
  571.     end
  572.     # Update the Shadows
  573.     for shade in @shadows
  574.       shade.update
  575.     end
  576.     if @lights.size>0
  577.       # updating screen tinting according to the number of lights on
  578.       value=((lights_off.to_f/@lights.size)*(-180)).round
  579.       $game_screen.start_tone_change(Tone.new(value,value,value,value), 1)
  580.     else
  581.       $game_screen.start_tone_change(Tone.new(0,0,0,0), 1)
  582.     end
  583.   end
  584.  #--------------------------------------------------------------------------
  585.   def dispose
  586.     for light in @lights
  587.       light.dispose
  588.     end
  589.     # Update the Shadows
  590.     for shade in @shadows
  591.       shade.dispose
  592.     end
  593.     @lights = []
  594.     @shadows = []
  595.   end
  596. end
  597. #==============================================================================
  598. # Class Bitmap
  599. #==============================================================================
  600. class Bitmap
  601.   #--------------------------------------------------------------------------
  602.   def draw_line(x1, y1, x2, y2, width = 1, color = Color.new(255, 255, 255))
  603.     # Return if width is less than or 0
  604.     return if width <= 0
  605.     # Reverse all parameters sent if 2 x is less than the first x
  606.     x1, x2, y1, y2 = x2, x1, y2, y1 if x2 < x1    
  607.     # Get S (1/2 width)
  608.     s = width / 2.0
  609.     # If X Coordinates are equal
  610.     if x1 == x2
  611.       # Draw Vertical line
  612.       fill_rect(x1 - s, [y1, y2].min, width, (y2 - y1).abs, color)
  613.     # If Y Coordinates are equal
  614.     elsif y1 == y2
  615.       # Draw Horizontal line
  616.       fill_rect(x1, y1 - s, x2 - x1, width, color)
  617.     end
  618.     # Get Length
  619.     length = x2 - x1 < (y2 - y1).abs ? (y2 - y1).abs : x2 - x1
  620.     # Get Increments
  621.     x_increment, y_increment = (x2 - x1) / length.to_f, (y2 - y1) / length.to_f
  622.     # Get Current X and Y
  623.     x, y = x1, y1
  624.     # While Current X is less than end X
  625.     while x < x2
  626.       # Draw Box of width width and width height
  627.       fill_rect(x-s, y-s, width, width, color)
  628.       # Increment X and Y
  629.       x += x_increment
  630.       y += y_increment
  631.     end
  632.   end
  633.   #--------------------------------------------------------------------------
  634.   # Draw pie, take quite a long time as it draws each line.
  635.   #--------------------------------------------------------------------------
  636.   def draw_pie(x,y,radius,color = Color.new(255, 255, 255, 255),start_angle=0,end_angle=360)
  637.     end_angle+=360 if end_angle<start_angle
  638.     name_string="#{radius}_#{start_angle}_#{end_angle}"
  639.     #filename="Graphics/Pictures/#{name_string}.png"
  640.     if FileTest.exist?("Graphics/Pictures/#{name_string}.png")
  641.       temp = RPG::Cache.picture(name_string)
  642.       blt(0,0,temp,temp.rect)
  643.     else
  644.       Graphics.transition
  645.       t = Progress_Bar.new(160,240,320,20,end_angle-start_angle+2 )
  646.       for i in start_angle...end_angle
  647.         t.current_step+=1
  648.         t.update
  649.         Graphics.update
  650.         for j in 0..2
  651.           x_=(Math::cos((i+j/2.0)*Math::PI/180)*radius).round+x
  652.           y_=(Math::sin((i+j/2.0)*Math::PI/180)*radius).round+y
  653.           draw_line(x, y, x_, y_, 2,color)
  654.         end
  655.       end
  656.       t.current_step+=1
  657.       t.update
  658.       Graphics.update
  659.       make_png(name_string, 'Graphics/Pictures/')
  660.       t.current_step+=1
  661.       t.update
  662.       Graphics.update
  663.       t.dispose
  664.     end
  665.   end
  666.   def make_png(name = 'like', path = '', mode = 0)
  667.     Dir.make_dir(path) if path != '' and !FileTest.directory?(path)
  668.     Zlib::Png_File.open('temp.gz')   { |gz| gz.make_png(self, mode) }
  669.     Zlib::GzipReader.open('temp.gz') { |gz| $read = gz.read }
  670.     f = File.open(path + name + '.png', 'wb')
  671.     f.write($read)
  672.     f.close
  673.     File.delete('temp.gz')
  674.   end
  675. end
  676.  
  677. class Progress_Bar < Sprite
  678.   #--------------------------------------------------------------------------
  679.   # * Public Instance Variables
  680.   #--------------------------------------------------------------------------
  681.   attr_accessor :current_step   # The Current Step
  682.   attr_accessor :steps          # The max amount of steps
  683.   #----------------------------------------------------------------------------
  684.   # * Initialize Object
  685.   #----------------------------------------------------------------------------
  686.   def initialize(x,y,width,height=16,steps=100,start=0)
  687.     super()
  688.     @steps = steps
  689.     self.x = x
  690.     self.y = y
  691.     @current_step= start
  692.     @width = width
  693.     @height = height
  694.     @w = 9
  695.     @nb_bars = @width/@w
  696.     @c1 = Color.new(46,211,49,255)
  697.     @c2 = Color.new(46,211,49,227)
  698.     @c3 = Color.new(46,211,49,202)
  699.     @c4 = Color.new(46,211,49,177)
  700.     @c5 = Color.new(46,211,49,152)
  701.     @c6 = Color.new(46,211,49,127)
  702.     @stretch_c = (@height-4)/16.0
  703.     self.bitmap = Bitmap.new(@width, @height)
  704.     self.bitmap.clear
  705.     self.bitmap.fill_rect(0, 0, @width, @height, Color.new(59,59,59,167))
  706.     draw
  707.   end
  708.   #----------------------------------------------------------------------------
  709.   # * Draw Bar
  710.   #----------------------------------------------------------------------------
  711.   def draw
  712.     self.bitmap.fill_rect(1, 1, @width-2, @height-2, Color.new(59,59,59,0))
  713.     for i in 0...((@current_step/@steps.to_f)*@nb_bars).round
  714.       self.bitmap.fill_rect(i*@w+1,2,@w-2,@height-4,@c6)
  715.       self.bitmap.fill_rect(i*@w+1,3*@stretch_c,@w-2,@height-6*@stretch_c,@c5)
  716.       self.bitmap.fill_rect(i*@w+1,4*@stretch_c,@w-2,@height-8*@stretch_c,@c4)
  717.       self.bitmap.fill_rect(i*@w+1,5*@stretch_c,@w-2,@height-10*@stretch_c,@c3)
  718.       self.bitmap.fill_rect(i*@w+1,6*@stretch_c,@w-2,@height-12*@stretch_c,@c2)
  719.       self.bitmap.fill_rect(i*@w+1,7*@stretch_c,@w-2,@height-14*@stretch_c,@c1)
  720.     end
  721.     @old = @current_step
  722.   end
  723.   #----------------------------------------------------------------------------
  724.   # * Update
  725.   #----------------------------------------------------------------------------
  726.   def update
  727.     draw if @current_step != @old
  728.   end
  729. end
  730. #SDK test
  731. end
  732.  
Expand to see the code.


Instructions
Everything is explained in the comments.

FAQ

Why is it so long when i'm starting to play, or going to another map?
- That's because of the time needed to draw the lights. I'm sorry about that...I need a real fast method to draw lines... So don't abuse of lights lol
and it's not to annoying as it's only a loading time, instead of slow down during gameplay.

I might make a Loading screen for that! XD

Compatibility

Require The SDK, whatever the version.

Credits and Thanks

Rataime's for his script, Learned a lot^^.
MACL stuff,but I only use one method, so I didn't make it required.

Author's Notes
I'm thinking of doing an easier version for the light settings. Remind me if I forgot!




Terms and Conditions

Use it as you want!
Not bad. The video is very impressive. I see a little metal gear action going on eh? Or maybe splinter cell. I don't know. But I'm very interested to see your other work. I gotta try this out and see how it actually works. But great job from what I've seen!
Hum yeah the video is made from a project request from a customer, named Badmonur.
That's why I need to do a proper Demo for this script!^^
But will this work with the sdk 1.5? Because I'm using Caldron's ABS and yeah I don't know how to switch it up from 1.5 to 2.3.

edit: well it works but for some odd reason the screen flashes like crazy. I mean its nice and easy to use but is there a way to keep it from flickering like that? Just curious if so I will be using this for sure.
arf, I was wrong lol actually

It should work with all versions of the SDK lol
I thought that the aliased methods I used, were only in the v2.xx
Well thats good to know but is there any way to fix the flickering problem I'm encountering?
Flickering? what do you mean?
Well I can't really explain it but if you get the Evergore demo in Caldrons ABS and insert your script in there you'll see the screen flicker. Its really odd because it doesn't happen in Mr.Mo's ABS but it does in that one so I don't know whats the deal there.
I must say it looks fine on my computer...
I just tested the caldaron's Evergore demo, and no problem.

But it poped up a bug lol, when i tried without any light lol, it crash I need to fix that lol.
EDIT, fixed! Script updated.
Seems like a really good lighting script, but I have one question, is it possible to set it so that the hero is the one casting the light? I mean, for example the hero has a lantern or flashlight on them is this possible with the current script or could it even be done?
Very nice effects, but it seems to lag pretty much ;)
oh, lol, I didn't think to that one, lol even if it's a quite obvious thing.
Hum, nope, there is no way for the moment to make the game_player as a source.

But It's a usefull feature to include in the next version!
And, hum, I think... it could be done quite easily.

About the video, lol it's seems laggy because of the capture software... it does the same thing on other video i created... when moving the mouse for example, it dropped the frame rate, and if it's just recording it remove only like 5 or 6 fps.
else the script only creates long loading time between maps.

it would be perfect to create the same kind of effects of your static lighting script...( like having a gradient light "halo" thing)

XD I must that I never had so much replies so quickly after posting a script XD

May I remind you of the wonderful "Edit" button? ;p - arevulopapo
I can't get the video to play for me for some reason =[
I can only hear the sound.
try this one:
http://rmxp.trebor777.net/rmxp/Dynamic%20Light%20and%20Shadow.wmv


EDIT: Big News!
I just found a nice solution to the "slow loading times" problem...
Basically, it only draw the bitmap one time, so it doesn't have to create it each time.
Instead a png file is created if not found. If found, the file is loaded. So loading times only happen the first time you load the map.

Moreover, it means, that, if the game creator is nice, he will play his game one time to create all the lights file, and then create the game archive. Like this, the players won't be affected by this problem.
mmm...
Did you know that the light created by this script follows me all the time?
O_o??
is your event is supposed to follow the player?? Check Please...

Big News!
I just found a nice solution to the "slow loading times" problem...
Basically, it only draw the bitmap one time, so it doesn't have to create it each time.
Instead a png file is created if not found. If found, the file is loaded. So loading times only happen the first time you load the map.

Moreover, it means, that, if the game creator is nice, he will play his game one time to create all the lights file, and then create the game archive. Like this, the players won't be affected by this problem.

EDIT: Solution Done, I've also included a small loading bar,
The script on the first page will be updated soon.
You finally finished it :D Good job man.
Yeap! ^^
Thx!

Hey here comes the demo!
Check the first post!

I removed on purpose the generated light files. So you can test it.
No trebor777, the event does not even move at all, only the light effect follows the character anywhere...
O_o??
hum, explain me everything you did,
script used etc;.;
a demo (would be easier)

Have you tried with the updated script I just posted with the Demo?
Well I copied the example included in the script and pasted it as a comment. I used both version 1.0 and 1.5...
maybe a screenshot could help me to explain what happened today.

http://img206.imageshack.us/img206/790/light01fh2.png
http://img411.imageshack.us/img411/7895/light02sr5.png
http://img267.imageshack.us/img267/8594/light03dy8.png
Seriously I don't know what's wrong... as you did configure your event as it should be.

Is the demo working fine with you?

Are you using other scripts?
You, sir, are a vile fiend. :p
I started working on this a while ago but I had to put it down for a while for my current projects. Although I have to admit I was only doing the Dynamic Shadows to go with a DNS. You went for the lighting too which I have not started, but I have been planning becuase I'll need it for a future project.

Nice work with this script! To be honest with you, I thought I was the only one who ever wanted Dynamic Lighting and Shadows in RMXP ^^ I was wrong!
h?h? thanks and sorry.
That's the same with a script for Stealth based gameplay... can't find a script for that...
So I'm working on it...
It seems it is incompatible with a version of Near Fantastica's Anti-Lag script... Only when I removed it, the light did not move at all.
But now that I know this, I noticed that the light is not correctly placed on the map, I mean, it does not look like it comes from the light source but from the grass...
http://img128.imageshack.us/img128/7642/light04np0.png
Yeah that's because you need to read the instructions lol.. ^^
no, it's because that the represented light is what you can see on the floor not where it exactly comes from.

If you don't like it, just use set the height argument to 0;( look at the instruction for that)

But I should try to improve that, it does look right if the light angle is really large and facing to one side like in my demo; but not in your case.
say looks nice.
In the video, are those lockers with the shadow charsets?
or am i able to make shadows from the chipset?
Thanks for the advice...
Is there a way I could use this script and the anti-lag script in the same project?
Well I know that the angle was wrong, I just tried to make sure I did not enter a wrong (event) comment, he, he.
hum use the other antilag script which is better than near one,
made by fl0tz.... something

(Can't people have simple username?)
Well it seems that the only anti lag script that does not make the lights move is f0tz!baerchen's... But there was actually a lag I never experienced while not using any anti lag script...

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=32271