Advertisement
khanhdu

Khas Ultra Graphics

Jul 17th, 2017
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 49.14 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # * [ACE] Khas Ultra Graphics
  3. #-------------------------------------------------------------------------------
  4. # * By Nilo K. (Khas)
  5. # * Version: 1.00
  6. # * Released on: 08/01/2015
  7. #
  8. # Blog: arcthunder.blogspot.com
  9. # Facebook: facebook.com/khasarc
  10. # Twitter: twitter.com/arcthunder
  11. # Youtube: youtube.com/c/khasarc
  12. #
  13. #-------------------------------------------------------------------------------
  14. # * Terms of Use
  15. #-------------------------------------------------------------------------------
  16. # Please read the terms of use included with this script.
  17. # The Khas Ultra Lighting requires the purchase of a license to be used.
  18. #
  19. #-------------------------------------------------------------------------------
  20. # * Requirements
  21. #-------------------------------------------------------------------------------
  22. # 1. Khas Core Library
  23. #    Place the "Khas Core Library" script before KUL.
  24. #
  25. # 2. Khas Graphics Library
  26. #    Place the "Khas Graphics Library" script before KUL.
  27. #
  28. #-------------------------------------------------------------------------------
  29. # * Instructions
  30. #-------------------------------------------------------------------------------
  31. # Please read the Ultra Lighting guide! It's a pdf file inside the demo folder.
  32. #
  33. #-------------------------------------------------------------------------------
  34. # * Light Core (configuration)
  35. #-------------------------------------------------------------------------------
  36. module Light_Core
  37.  
  38.   Lights = { #  <= DO NOT change this!
  39. #-------------------------------------------------------------------------------
  40. # * Lights
  41. #-------------------------------------------------------------------------------
  42. # To create a light, use the following model and paste the code below.
  43. #
  44. # "name" => {
  45. #    :light => "file" / {2 => "file2", 4 => "file4", 6 => "file6", 8 => "file8"},
  46. #    :opacity => a, :variation => b,
  47. #    :static => true/false, :shadows => true/false,
  48. #    :group => "group name",
  49. #    :offset => [ox, oy] / {2=>[ox2,oy2], 4=>[ox4,oy4], 6=>[ox6,oy6], 8=>[[ox8,oy8]}
  50. #  },
  51. #
  52. #
  53. # You can omit the following to use the defaults:
  54. # :opacity -> 255
  55. # :static -> true
  56. # :shadows -> false
  57. # :group -> no group
  58. # :offset -> [0,0]
  59. #
  60. #
  61. #
  62. #
  63. # "name" -> Used for identification. Must be between quotation marks, without
  64. #           blank spaces. Can include characters from a to z, 0 to 9 and
  65. #           underlines. Examples:
  66. #           "torch"
  67. #           "1"
  68. #           "light88"
  69. #
  70. # :light -> Light file on Graphics/Lights folder. May be a single file, or
  71. #           a hash containing the file for each direction. Examples:
  72. #
  73. #           :light => "single file",
  74. #
  75. #           :light => {2 => "file2", 4 => "file4", 6 => "file6", 8 => "file8"},
  76. #
  77. #           Where:
  78. #             "file2" -> looking down
  79. #             "file4" -> looking left
  80. #             "file6" -> looking right
  81. #             "file8" -> looking up
  82. #
  83. # :opacity -> Main opacity "a".
  84. # :variation -> "rand(b)" will be added to opacity "a".
  85. #             On each frame, the opacity of a light is:
  86. #             opacity = a + rand(b)
  87. #
  88. # :shadows -> Set to true if the light will cast shadows, false otherwise.
  89. # :static -> Set to true if the light won't move, false otherwise.
  90. #            This option is highly recommended for lights that won't move.
  91. #            It prevents the light to refresh and cast shadows every frame, so
  92. #            the performance will increase.
  93. #            A good example is fixed wall lights.
  94. #
  95. # :group ->  The group's name that the light pertains. The light's opacity will
  96. #            be modified by the group's opacity.
  97. #
  98. # :offset -> Offset for x/y directions. May be a single array, or
  99. #            a hash containing an array for each direction.
  100. #            OFFSET VALUES MUST BE between -15 and 15 (values out of this range
  101. #            may cast wrong shadows - use at your own risk). Examples:
  102. #
  103. #           :offset => [ox, oy],
  104. #
  105. #           :offset => {2=>[ox2,oy2], 4=>[ox4,oy4], 6=>[ox6,oy6], 8=>[[ox8,oy8]},
  106. #
  107. #           Where:
  108. #             [ox2, oy2] -> looking down
  109. #             [ox4, oy4] -> looking left
  110. #             [ox6, oy6] -> looking right
  111. #             [ox8, oy8] -> looking up
  112. #
  113. #
  114. #-------------------------------------------------------------------------------
  115. #       ATTENTION! DO NOT FORGET THE COMMAS! - PUT YOUR LIGHTS HERE!
  116. #-------------------------------------------------------------------------------
  117.  
  118.   "simple" => {
  119.     :light => "light",
  120.     :opacity => 255, :variation => 0,
  121.     :static => false, :shadows => true,
  122.     :offset => [0,0],
  123.   },
  124.  
  125.   "simple2" => {
  126.     :light => "light",
  127.     :opacity => 255, :variation => 0,
  128.     :static => true, :shadows => false,
  129.     :offset => [0,0],
  130.   },
  131.  
  132.   "small" => {
  133.     :light => "small",
  134.     :opacity => 255, :variation => 0,
  135.     :static => true, :shadows => true,
  136.     :offset => [0,0],
  137.   },
  138.  
  139.  
  140.   "big" => {
  141.     :light => "big",
  142.     :opacity => 255, :variation => 0,
  143.     :static => false, :shadows => true,
  144.     :offset => [0,0]
  145.   },
  146.  
  147.   "broken" => {
  148.     :light => "light",
  149.     :opacity => 150, :variation => 100,
  150.     :static => false, :shadows => true,
  151.     :offset => [0,0]
  152.   },
  153.  
  154.   "torch" => {
  155.     :light => "torch",
  156.     :opacity => 215, :variation => 40,
  157.     :static => false, :shadows => true,
  158.     :offset => [0,0]
  159.   },
  160.  
  161.   "blue_torch" => {
  162.     :light => "blue",
  163.     :opacity => 215, :variation => 40,
  164.     :static => false, :shadows => true,
  165.     :offset => [0,0]
  166.   },
  167.  
  168.   "green_torch" => {
  169.     :light => "green",
  170.     :opacity => 215, :variation => 40,
  171.     :static => false, :shadows => true,
  172.     :offset => [0,0]
  173.   },
  174.  
  175.  
  176.   "big_torch" => {
  177.     :light => "big_torch",
  178.     :opacity => 180, :variation => 20,
  179.     :static => false, :shadows => true,
  180.     :offset => [0,0]
  181.   },
  182.  
  183.   "white" => {
  184.     :light => "white",
  185.     :opacity => 255, :variation => 0,
  186.     :static => false, :shadows => true,
  187.     :offset => [0,0]
  188.   },
  189.  
  190.   "red" => {
  191.     :light => "red",
  192.     :opacity => 255, :variation => 0,
  193.     :static => false, :shadows => true,
  194.     :offset => [0,0]
  195.   },
  196.  
  197.   "green" => {
  198.     :light => "green",
  199.     :opacity => 255, :variation => 0,
  200.     :static => false, :shadows => true,
  201.     :offset => [0,0]
  202.   },
  203.  
  204.   "blue" => {
  205.     :light => "blue",
  206.     :opacity => 255, :variation => 0,
  207.     :static => false, :shadows => true,
  208.     :offset => [0,0]
  209.   },
  210.  
  211.   "blue2" => {
  212.     :light => "blue",
  213.     :opacity => 255, :variation => 0,
  214.     :static => true, :shadows => false,
  215.     :offset => [0,0]
  216.   },
  217.  
  218.   "violet" => {
  219.     :light => "violet",
  220.     :opacity => 255, :variation => 0,
  221.     :static => false, :shadows => true,
  222.     :offset => [0,0]
  223.   },
  224.  
  225.   "cyan" => {
  226.     :light => "cyan",
  227.     :opacity => 255, :variation => 0,
  228.     :static => false, :shadows => true,
  229.     :offset => [0,0]
  230.   },
  231.  
  232.   "yellow" => {
  233.     :light => "yellow",
  234.     :opacity => 255, :variation => 0,
  235.     :static => false, :shadows => true,
  236.     :offset => [0,0]
  237.   },
  238.  
  239.   "flashlight" => {
  240.     :light => {2 => "lantern_2", 4 => "lantern_4", 6 => "lantern_6", 8 => "lantern_8"},
  241.     :opacity => 200, :variation => 10,
  242.     :static => false, :shadows => true,
  243.     :offset => {2=>[-6,15], 4=>[-15,-6], 6=>[15,6], 8=>[6,-15]}
  244.   },
  245.  
  246.   "window1" => {
  247.     :light => "window1",
  248.     :opacity => 255, :variation => 0,
  249.     :static => true, :shadows => true,
  250.     :offset => [0,-17]
  251.   },
  252.  
  253.   "window2" => {
  254.     :light => "window2",
  255.     :opacity => 255, :variation => 0,
  256.     :static => true, :shadows => true,
  257.     :offset => [0,-17]
  258.   },
  259.  
  260.   "window3" => {
  261.     :light => "window3",
  262.     :opacity => 255, :variation => 0,
  263.     :static => true, :shadows => true,
  264.     :offset => [0,-17]
  265.   },
  266.  
  267.  
  268.   "window4" => {
  269.     :light => "window4",
  270.     :opacity => 255, :variation => 0,
  271.     :static => true, :shadows => true,
  272.     :offset => [0,0]
  273.   },
  274.  
  275.  
  276.  
  277. #-------------------------------------------------------------------------------
  278. # End of light configuration
  279. #-------------------------------------------------------------------------------
  280.   } #  <= DO NOT change this!
  281.  
  282.   # LIGHT FOLDER
  283.   # You can change the default light folder here (root/Graphics/folder name/).
  284.   Light_Folder = "Lights"
  285.  
  286.   # DISABLE LIGHTS SWITCH
  287.   # Set an switch ID below to temporarily disable light rendering.
  288.   # If you turn ON this switch, lights won't be rendered.
  289.   Light_Sw = 17
  290.  
  291.   # DISABLE LIGHTS SWITCH
  292.   # Set an switch ID below to temporarily disable the entire system.
  293.   # If you turn ON this switch, Ultra Lighting will be disabled.
  294.   UlSys_Sw = 18
  295.  
  296.   # ENABLE SHADOWS
  297.   # Please set this as false if you don't want shadows. By disabling shadows
  298.   # the performance is increased.
  299.   Enable_Shadows = true
  300.  
  301.   # DISABLE STANDARD SHADOWS
  302.   # You may want to disable the standard/automatic shadows from RMVXA.
  303.   # Notice that they will still appear on the editor.
  304.   Disable_STD_Shadows = false
  305.  
  306.   # SHADOW Z COORDINATE
  307.   # You may decrease this if the shadows are overlaying something that they  
  308.   # shouldn't, or increase if they are supposed to overlay something.
  309.   Shadow_Overlay_Z = 50
  310.  
  311.   # GRAPHICS SETTINGS FILE
  312.   # You can change the default graphics settings file here:
  313.   GS_File = "Graphics Settings.cfg"
  314.  
  315.   # GRAPHICS MENU FONT
  316.   # Font name
  317.   GM_Font = "Trajan Pro"
  318.   # Font size (title)
  319.   GM_TSize = 32
  320.   # Font size (settings)
  321.   GM_FSize = 18
  322.   # Bold?
  323.   GM_Bold = true
  324.  
  325. end
  326.  
  327. #-------------------------------------------------------------------------------
  328. # * Requirements
  329. #-------------------------------------------------------------------------------
  330.  
  331. unless $khas && $khas[:core] >= 1.0
  332.   warning = Sprite.new
  333.   warning.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  334.   warning.bitmap.fill_rect(0,0,Graphics.width,Graphics.height,Color.new(255,255,255))
  335.   warning.bitmap.font = Font.new("Verdana", 32)
  336.   warning.bitmap.font.bold = true
  337.   warning.bitmap.font.outline = false
  338.   warning.bitmap.font.color.set(255,144,9)
  339.   warning.bitmap.draw_text(0,Graphics.height/2-32,Graphics.width,32,"PLEASE INSTALL",1)
  340.   warning.bitmap.draw_text(0,Graphics.height/2,Graphics.width,32,"KHAS CORE LIBRARY 1.0",1)
  341.   while !Input.trigger?(:C)
  342.     Input.update
  343.     Graphics.update
  344.   end
  345.   warning.bitmap.dispose
  346.   warning.dispose
  347.   exit
  348. end
  349.  
  350. unless $khas_graphics[:core] && $khas_graphics[:core] >= 1.0
  351.   warning = Sprite.new
  352.   warning.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  353.   warning.bitmap.fill_rect(0,0,Graphics.width,Graphics.height,Color.new(255,255,255))
  354.   warning.bitmap.font = Font.new("Verdana", 32)
  355.   warning.bitmap.font.bold = true
  356.   warning.bitmap.font.outline = false
  357.   warning.bitmap.font.color.set(255,144,9)
  358.   warning.bitmap.draw_text(0,Graphics.height/2-32,Graphics.width,32,"PLEASE INSTALL",1)
  359.   warning.bitmap.draw_text(0,Graphics.height/2,Graphics.width,32,"KHAS GRAPHICS LIBRARY 1.0",1)
  360.   while !Input.trigger?(:C)
  361.     Input.update
  362.     Graphics.update
  363.   end
  364.   warning.bitmap.dispose
  365.   warning.dispose
  366.   exit
  367. end
  368.  
  369. unless $khas_graphics[:kgl] && $khas_graphics[:kgl] >= 1.08
  370.   warning = Sprite.new
  371.   warning.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  372.   warning.bitmap.fill_rect(0,0,Graphics.width,Graphics.height,Color.new(255,255,255))
  373.   warning.bitmap.font = Font.new("Verdana", 32)
  374.   warning.bitmap.font.bold = true
  375.   warning.bitmap.font.outline = false
  376.   warning.bitmap.font.color.set(255,144,9)
  377.   warning.bitmap.draw_text(0,Graphics.height/2-32,Graphics.width,32,"PLEASE INSTALL",1)
  378.   warning.bitmap.draw_text(0,Graphics.height/2,Graphics.width,32,"KHAS GRAPHICS LIBRARY.dll 1.08",1)
  379.   while !Input.trigger?(:C)
  380.     Input.update
  381.     Graphics.update
  382.   end
  383.   warning.bitmap.dispose
  384.   warning.dispose
  385.   exit
  386. end
  387.  
  388. #-------------------------------------------------------------------------------
  389. # * Version
  390. #-------------------------------------------------------------------------------
  391.  
  392. $khas_graphics[:ultra_lighting] = 1.0
  393.  
  394. #-------------------------------------------------------------------------------
  395. # * Light Core
  396. #-------------------------------------------------------------------------------
  397.  
  398. module Light_Core
  399.  
  400.   SCShift = 0x02
  401.   SCSize = 0x01 << SCShift
  402.  
  403. end
  404.  
  405. #-------------------------------------------------------------------------------
  406. # * Graphics Settings
  407. #-------------------------------------------------------------------------------
  408.  
  409. class Graphics_Settings
  410.  
  411.   include Light_Core
  412.  
  413.   attr_reader :names
  414.   attr_reader :options
  415.   attr_accessor :settings
  416.  
  417.   def initialize
  418.     @names = {}
  419.     @options = {}
  420.     @settings = {}
  421.   end
  422.  
  423.   def add(key, name, default, options)
  424.     @names[key] = name
  425.     @settings[key] = default
  426.     @options[key] = options
  427.     refresh_settings
  428.   end
  429.  
  430.   def refresh_settings
  431.     @settings.each { |s, v| set(s, v) }
  432.   end
  433.    
  434.   def set(setting, value)
  435.   end
  436.  
  437.   def save
  438.     File.open(GS_File, 'w') do |config|
  439.       @settings.each do |setting, value|
  440.         config.write("[#{setting} #{value}]\n")
  441.       end
  442.     end
  443.   end
  444.  
  445.   def load
  446.     return unless File.file?(GS_File)
  447.     File.readlines(GS_File).each do |line|
  448.       @settings[line.khas_command.to_sym] = (line.khas_value.is_int? ? line.khas_value.to_i : line.khas_value) if @settings.include?(line.khas_command.to_sym)
  449.     end
  450.     refresh_settings
  451.   end
  452.  
  453. end
  454.  
  455. $kgs = Graphics_Settings.new
  456.  
  457. #-------------------------------------------------------------------------------
  458. # * Graphics Settings
  459. #-------------------------------------------------------------------------------
  460.  
  461. class Graphics_Settings
  462.  
  463.   attr_reader :static
  464.   attr_reader :dynamic
  465.   attr_reader :soft_shadows
  466.   attr_reader :light_size
  467.   attr_reader :light_alpha
  468.  
  469.   alias kul_set set
  470.  
  471.   def set_static_light(s)
  472.     return if @static == s
  473.     @static = s
  474.     unless $game_map.nil?
  475.       unless $game_map.lights.nil?
  476.         $game_map.lights.each { |source| source.light.restore if source.light.change_setting? }
  477.       end
  478.     end
  479.   end
  480.  
  481.   def set_dynamic_light(d)
  482.     return if @dynamic == d
  483.     @dynamic = d
  484.     unless $game_map.nil?
  485.       unless $game_map.lights.nil?
  486.         $game_map.lights.each { |source| source.light.restore if source.light.change_setting? }
  487.       end
  488.     end
  489.   end
  490.  
  491.   def set_soft_shadows(s)
  492.     return if @soft_shadows == s
  493.     @soft_shadows = s
  494.     KGL.softShadows(@soft_shadows)
  495.   end
  496.  
  497.   def set_light_size(s)
  498.     return if @light_size == (s.to_f / 100)
  499.     s = 50 if s < 50
  500.     s = 100 if s > 100
  501.     @light_size = s.to_f / 100
  502.     if defined?(Light_Cache)
  503.       Light_Cache.clear
  504.       Light_Cache.build
  505.     end
  506.     unless $game_map.nil?
  507.       unless $game_map.lights.nil?
  508.         $game_map.lights.each { |source| source.light.restore if source.light.change_setting? }
  509.       end
  510.     end
  511.   end
  512.  
  513.   def set_light_alpha(a)
  514.     return if @light_alpha == a
  515.     @light_alpha = a
  516.     KGL.lightBlending(a)
  517.   end
  518.  
  519.   def set(setting, value)
  520.     kul_set(setting, value)
  521.     case setting
  522.       when :static_shadows; set_static_light(value == "ON")
  523.       when :dynamic_shadows; set_dynamic_light(value == "ON")
  524.       when :soft_shadows; set_soft_shadows(value == "ON")
  525.       when :light_size; set_light_size(value)
  526.       when :light_opacity; set_light_alpha(value == "ON")
  527.     end
  528.   end
  529.  
  530. end
  531.  
  532. if Light_Core::Enable_Shadows
  533.   $kgs.add(:static_shadows, "Static Shadows", "ON", ["OFF", "ON"])
  534.   $kgs.add(:dynamic_shadows, "Dynamic Shadows", "ON", ["OFF", "ON"])
  535.   $kgs.add(:soft_shadows, "Soft Shadows", "ON", ["OFF", "ON"])
  536. end
  537. $kgs.add(:light_size, "Light Size", 100, [70,80,90,100])
  538. $kgs.add(:light_opacity, "Light Opacity", "ON", ["OFF", "ON"])
  539. $kgs.load
  540.  
  541. #-------------------------------------------------------------------------------
  542. # * Graphics Menu
  543. #-------------------------------------------------------------------------------
  544.  
  545. class Graphics_Menu < Sprite
  546.  
  547.   include Graphics_Core
  548.   include Light_Core
  549.  
  550.   def initialize
  551.     super(nil)
  552.     self.bitmap = Bitmap.new(544,416)
  553.     self.x = Screen_Width/2
  554.     self.y = Screen_Height/2
  555.     self.ox = 272
  556.     self.oy = 208
  557.     self.bitmap.font.name = GM_Font
  558.     self.bitmap.font.outline = false
  559.     self.bitmap.font.bold = GM_Bold
  560.     draw_title
  561.     draw_items
  562.   end
  563.  
  564.   def update
  565.     refresh_index(@index + 1) if Input.trigger?(:DOWN)
  566.     refresh_index(@index - 1) if Input.trigger?(:UP)
  567.     next_option if Input.trigger?(:RIGHT)
  568.     previous_option if Input.trigger?(:LEFT)
  569.   end
  570.  
  571.   def dispose
  572.     self.bitmap.dispose
  573.     super
  574.   end
  575.  
  576.   def refresh_index(i)
  577.     Sound.play_cursor
  578.     clear_item(@index)
  579.     draw_item(@index)
  580.     @index = i % @items.size
  581.     clear_item(@index)
  582.     draw_item(@index, true)
  583.   end
  584.  
  585.   def draw_title
  586.     self.bitmap.font.size = GM_TSize
  587.     self.bitmap.draw_text(48,32,544,32,"Graphics")
  588.     self.bitmap.font.size = GM_FSize
  589.   end
  590.  
  591.   def draw_items
  592.     @index = 0
  593.     @items = []
  594.     $kgs.names.keys.each { |k| @items << k }
  595.      draw_item(0,true)
  596.     for i in 1...@items.size
  597.       draw_item(i)
  598.     end
  599.   end
  600.  
  601.   def draw_item(i, active = false)
  602.     c = (active ? 255 : 192)
  603.     self.bitmap.font.color.set(c,c,c)
  604.     self.bitmap.draw_text(64,70+i*32,416,32,$kgs.names[@items[i]],0)
  605.     if active
  606.       self.bitmap.draw_text(356,70+i*32,96,32,"< #{$kgs.settings[@items[i]]} >",1)
  607.     else
  608.       self.bitmap.draw_text(356,70+i*32,96,32,$kgs.settings[@items[i]].to_s,1)
  609.     end
  610.   end
  611.  
  612.   def next_option
  613.     Sound.play_cursor
  614.     options = $kgs.options[@items[@index]]
  615.     current = $kgs.settings[@items[@index]]
  616.     oi = 0
  617.     for i in 0...options.size
  618.       oi = i if options[i] == current
  619.     end
  620.     oi = (oi + 1) % options.size
  621.     $kgs.settings[@items[@index]] = options[oi]
  622.     clear_item(@index)
  623.     draw_item(@index, true)
  624.   end
  625.  
  626.   def previous_option
  627.     Sound.play_cursor
  628.     options = $kgs.options[@items[@index]]
  629.     current = $kgs.settings[@items[@index]]
  630.     oi = 0
  631.     for i in 0...options.size
  632.       oi = i if options[i] == current
  633.     end
  634.     oi = (oi - 1) % options.size
  635.     $kgs.settings[@items[@index]] = options[oi]
  636.     clear_item(@index)
  637.     draw_item(@index, true)
  638.   end
  639.  
  640.   def clear_item(i)
  641.     self.bitmap.clear_rect(64,70+i*32,416,32)
  642.   end
  643.  
  644. end
  645.  
  646. #-------------------------------------------------------------------------------
  647. # * Scene Graphics
  648. #-------------------------------------------------------------------------------
  649.  
  650. class Scene_Graphics < Scene_Base
  651.  
  652.   def start
  653.     super
  654.     create_background
  655.     @menu = Graphics_Menu.new
  656.   end
  657.  
  658.   def update
  659.     super
  660.     refresh_menu
  661.     refresh_background
  662.   end
  663.  
  664.   def terminate
  665.     super
  666.     dispose_background
  667.     @menu.dispose
  668.   end
  669.  
  670.   def create_background
  671.     @sub = 0
  672.     @background_sprite = Sprite.new
  673.     @background_sprite.bitmap = SceneManager.background_bitmap
  674.     @background_sprite.color.set(16, 16, 16, 128)
  675.   end
  676.  
  677.   def refresh_background
  678.     @sub -= 4 if @sub > -128
  679.     @background_sprite.tone.set(@sub, @sub, @sub, @sub)
  680.   end
  681.  
  682.   def dispose_background
  683.     @background_sprite.dispose
  684.   end
  685.  
  686.   def refresh_menu
  687.     @menu.update
  688.     if Input.trigger?(:B)
  689.       Sound.play_cancel
  690.       while (@sub < 0)
  691.         @background_sprite.tone.set(@sub, @sub, @sub, @sub)
  692.         Graphics.update
  693.         @sub += 10
  694.       end
  695.       $kgs.save
  696.       $kgs.refresh_settings
  697.       return_scene
  698.     end
  699.   end
  700.  
  701. end
  702.  
  703. #-------------------------------------------------------------------------------
  704. # * Cache
  705. #-------------------------------------------------------------------------------
  706.  
  707. module Cache
  708.  
  709.   def self.light(filename)
  710.     load_bitmap("Graphics/#{Light_Core::Light_Folder}/", filename)
  711.   end
  712.  
  713. end
  714.  
  715. #-------------------------------------------------------------------------------
  716. # * Light Cache
  717. #-------------------------------------------------------------------------------
  718.  
  719. module Light_Cache
  720.  
  721.   include Light_Core
  722.  
  723.   @@buffer = {}
  724.  
  725.   def self::[](key)
  726.     return @@buffer[key]
  727.   end
  728.  
  729.   def self.build
  730.     for effect in Lights.values
  731.       if effect[:light].is_a?(Hash)
  732.         effect[:light].values.each { |e| Light_Cache.store(e)}
  733.       else
  734.         Light_Cache.store(effect[:light])
  735.       end
  736.     end
  737.   end
  738.  
  739.   def self.clear
  740.     @@buffer.values.each { |b| b.dispose }
  741.     @@buffer.clear
  742.   end
  743.  
  744.   def self.store(key)
  745.     return if @@buffer.keys.include?(key)
  746.     if $kgs.light_size >= 1.0
  747.       @@buffer[key] = Cache.light(key).clone
  748.     else
  749.       source = Cache.light(key)
  750.       sr = Rect.new(0,0,source.width,source.height)
  751.       tr = Rect.new(0,0,source.width*$kgs.light_size,source.height*$kgs.light_size)
  752.       @@buffer[key] = Bitmap.new(tr.width, tr.height)
  753.       @@buffer[key].stretch_blt(tr, source, sr)
  754.       source.dispose
  755.     end
  756.     KGL.compressAlpha(@@buffer[key])
  757.   end
  758.  
  759. end
  760.  
  761. Light_Cache.build
  762.  
  763. #-------------------------------------------------------------------------------
  764. # * Virtual Char
  765. #-------------------------------------------------------------------------------
  766.  
  767. class Virtual_Char
  768.  
  769.   attr_accessor :real_x
  770.   attr_accessor :real_y
  771.   attr_accessor :direction
  772.  
  773. end
  774.  
  775. #-------------------------------------------------------------------------------
  776. # * Light Source
  777. #-------------------------------------------------------------------------------
  778.  
  779. class Light_Source
  780.  
  781.   include Graphics_Core
  782.   include Light_Core
  783.  
  784.   attr_reader :id
  785.   attr_reader :x
  786.   attr_reader :y
  787.   attr_reader :cx
  788.   attr_reader :cy
  789.   attr_reader :h
  790.   attr_reader :screen_x
  791.   attr_reader :screen_y
  792.   attr_reader :char
  793.   attr_reader :static
  794.   attr_reader :shadows
  795.  
  796.   def initialize
  797.     @bitmap = nil
  798.   end
  799.  
  800.   def dispose
  801.     return if @bitmap.nil?
  802.     @bitmap.dispose
  803.     @bitmap = nil
  804.   end
  805.  
  806.   def restore
  807.     dispose
  808.     refresh_size
  809.     refresh_xyh
  810.     @bitmap = Light_Cache[@key].clone
  811.     if @shadows && @static && $kgs.static
  812.       KGL.bindShadowbuffer(@bitmap)
  813.       draw_shadow
  814.     end
  815.   end
  816.  
  817.   def change_setting?
  818.     @bitmap != nil
  819.   end
  820.  
  821.   def set_light(id, key)
  822.     @id = id
  823.     @key = key
  824.   end
  825.  
  826.   def set_group(group)
  827.   end
  828.  
  829.   def set_mode(static, shadows)
  830.     @static = (static.nil? ? true : static)
  831.     @shadows = (shadows.nil? ? false : shadows)
  832.     restore
  833.   end
  834.  
  835.   def set_opacity(op, var)
  836.     if op != nil
  837.       @opacity = op
  838.       @variation = (var.nil? ? 0 : var)
  839.     else
  840.       @opacity = 255
  841.       @variation = 0
  842.     end
  843.   end
  844.  
  845.   def set_pos(rx, ry, d)
  846.     @char = Virtual_Char.new
  847.     @char.real_x = rx
  848.     @char.real_y = ry
  849.     @char.direction = d
  850.   end
  851.    
  852.   def follow_char(char)
  853.     @char = char
  854.   end
  855.  
  856.   def set_offset(offset)
  857.     offset = 0 if offset.nil?
  858.     @offset = (offset.is_a?(Hash) ? offset : {2=>offset, 4=>offset, 6=>offset, 8=>offset})
  859.   end
  860.  
  861.   def opacity
  862.     @variation == 0 ? @opacity : (@opacity + rand(@variation))
  863.   end
  864.  
  865.   def visible?
  866.     @screen_x < Screen_Width && @screen_y < Screen_Height && (@screen_x + @width) > 0 && (@screen_y + @height) > 0
  867.   end
  868.  
  869.   def refresh_size
  870.     @width = Light_Cache[@key].width
  871.     @height = Light_Cache[@key].height
  872.     @half_width = @width/2
  873.     @half_height = @height/2
  874.     @width_offset = @half_width - 16
  875.     @height_offset = @half_height - 16
  876.     @tile_hw = @half_width/32.0 + 0.5
  877.     @tile_hh = @half_height/32.0 + 0.5
  878.   end
  879.  
  880.   def refresh_xyh
  881.     @x = (@char.real_x*32 - @width_offset).to_f + @offset[@char.direction][0]
  882.     @y = (@char.real_y*32 - @height_offset).to_f + @offset[@char.direction][1]
  883.     @cx = @x + @half_width
  884.     @cy = @y + @half_height
  885.     @screen_x = $game_map.adjust_x(@char.real_x)*32-@width_offset + @offset[@char.direction][0] - $game_map.screen.shake
  886.     @screen_y = $game_map.adjust_y(@char.real_y)*32-@height_offset + @offset[@char.direction][1]
  887.     @h = ($game_map.local_height(@char.real_x + 0.4, @char.real_y + 0.4) > $game_map.local_height(@char.real_x + 0.5, @char.real_y + 0.5) ? $game_map.local_height(@char.real_x + 0.4, @char.real_y + 0.4) : $game_map.local_height(@char.real_x + 0.5, @char.real_y + 0.5))
  888.   end
  889.  
  890.   def sbmp
  891.     @bitmap
  892.   end
  893.    
  894.   def dbmp
  895.     if !@static && @shadows
  896.       KGL.clone(@bitmap, Light_Cache[@key])
  897.       KGL.bindShadowbuffer(@bitmap)
  898.       draw_shadow
  899.     end
  900.     @bitmap
  901.   end
  902.    
  903.   def draw_shadow
  904.     $game_map.casters.shadow(self)
  905.   end
  906.  
  907. end
  908.  
  909. unless Light_Core::Enable_Shadows
  910.  
  911.   class Light_Source
  912.    
  913.     def restore
  914.       dispose
  915.       refresh_size
  916.       refresh_xyh
  917.       @bitmap = Light_Cache[@key].clone
  918.     end
  919.    
  920.     def set_mode(static, shadows)
  921.       @static = false
  922.       @shadows = false
  923.       restore
  924.     end
  925.    
  926.     def refresh_xyh
  927.       @screen_x = $game_map.adjust_x(@char.real_x)*32-@width_offset + @offset[@char.direction][0] - $game_map.screen.shake
  928.       @screen_y = $game_map.adjust_y(@char.real_y)*32-@height_offset + @offset[@char.direction][1]
  929.     end
  930.    
  931.     def dbmp
  932.       @bitmap
  933.     end
  934.    
  935.     def draw_shadow
  936.     end
  937.    
  938.   end
  939.  
  940. end
  941.  
  942. #-------------------------------------------------------------------------------
  943. # * Light SKSource
  944. #-------------------------------------------------------------------------------
  945.  
  946. class Light_SKSource < Light_Source
  947.  
  948.   attr_reader :tile_hw
  949.   attr_reader :tile_hh
  950.  
  951. end
  952.  
  953. #-------------------------------------------------------------------------------
  954. # * Light MKSource
  955. #-------------------------------------------------------------------------------
  956. class Light_MKSource < Light_Source
  957.  
  958.   def initialize
  959.     super
  960.     @width = {}
  961.     @height = {}
  962.     @half_width = {}
  963.     @half_height = {}
  964.     @width_offset = {}
  965.     @height_offset = {}
  966.     @tile_hw = {}
  967.     @tile_hh = {}
  968.   end
  969.  
  970.   def tile_hw
  971.     @tile_hw[@char.direction]
  972.   end
  973.  
  974.   def tile_hh
  975.     @tile_hh[@char.direction]
  976.   end
  977.  
  978.   def dispose
  979.     return if @bitmap.nil?
  980.     @bitmap.values.each { |b| b.dispose }
  981.     @bitmap = nil
  982.   end
  983.  
  984.   def restore
  985.     dispose
  986.     refresh_size
  987.     refresh_xyh
  988.     @bitmap = {2 => Light_Cache[@key[2]].clone, 4 => Light_Cache[@key[4]].clone,6 => Light_Cache[@key[6]].clone,8 => Light_Cache[@key[8]].clone}
  989.     @bitmap.values.each {|b| KGL.bindShadowbuffer(b); draw_shadow} if @shadows && @static && $kgs.static
  990.   end
  991.  
  992.   def refresh_size
  993.     for d in [2,4,6,8]
  994.       @width[d] = Light_Cache[@key[d]].width
  995.       @height[d] = Light_Cache[@key[d]].height
  996.       @half_width[d] = @width[d]/2
  997.       @half_height[d] = @height[d]/2
  998.       @width_offset[d] = @half_width[d] - 16
  999.       @height_offset[d] = @half_height[d] - 16
  1000.       @tile_hw[d] = @half_width[d]/32.0 + 0.5
  1001.       @tile_hh[d] = @half_height[d]/32.0 + 0.5
  1002.     end
  1003.   end
  1004.  
  1005.   def visible?
  1006.     @screen_x < Screen_Width && @screen_y < Screen_Height && (@screen_x + @width[@char.direction]) > 0 && (@screen_y + @height[@char.direction]) > 0
  1007.   end
  1008.  
  1009.   def refresh_xyh
  1010.     d = @char.direction
  1011.     @x = (@char.real_x*32 - @width_offset[d]).to_f + @offset[d][0]
  1012.     @y = (@char.real_y*32 - @height_offset[d]).to_f + @offset[d][1]
  1013.     @cx = @x + @half_width[d]
  1014.     @cy = @y + @half_height[d]
  1015.     @screen_x = $game_map.adjust_x(@char.real_x)*32-@width_offset[d] + @offset[d][0] - $game_map.screen.shake
  1016.     @screen_y = $game_map.adjust_y(@char.real_y)*32-@height_offset[d] + @offset[d][1]
  1017.     @h = ($game_map.local_height(@char.real_x + 0.4, @char.real_y + 0.4) > $game_map.local_height(@char.real_x + 0.5, @char.real_y + 0.5) ? $game_map.local_height(@char.real_x + 0.4, @char.real_y + 0.4) : $game_map.local_height(@char.real_x + 0.5, @char.real_y + 0.5))
  1018.   end
  1019.  
  1020.   def sbmp
  1021.     @bitmap[@char.direction]
  1022.   end
  1023.    
  1024.   def dbmp
  1025.     if !@static && @shadows
  1026.       KGL.clone(@bitmap[@char.direction], Light_Cache[@key[@char.direction]])
  1027.       KGL.bindShadowbuffer(@bitmap[@char.direction])
  1028.       draw_shadow
  1029.     end
  1030.     @bitmap[@char.direction]
  1031.   end
  1032.  
  1033. end
  1034.  
  1035. unless Light_Core::Enable_Shadows
  1036.  
  1037.   class Light_MKSource < Light_Source
  1038.    
  1039.     def restore
  1040.       dispose
  1041.       refresh_size
  1042.       refresh_xyh
  1043.       @bitmap = {2 => Light_Cache[@key[2]].clone, 4 => Light_Cache[@key[4]].clone,6 => Light_Cache[@key[6]].clone,8 => Light_Cache[@key[8]].clone}
  1044.     end
  1045.    
  1046.     def refresh_xyh
  1047.       d = @char.direction
  1048.       @screen_x = $game_map.adjust_x(@char.real_x)*32-@width_offset[d] + @offset[d][0] - $game_map.screen.shake
  1049.       @screen_y = $game_map.adjust_y(@char.real_y)*32-@height_offset[d] + @offset[d][1]
  1050.     end
  1051.    
  1052.     def dbmp
  1053.       @bitmap[@char.direction]
  1054.     end
  1055.    
  1056.   end
  1057.  
  1058. end
  1059.  
  1060. #-------------------------------------------------------------------------------
  1061. # * Shadow
  1062. #-------------------------------------------------------------------------------
  1063.  
  1064. class Shadow
  1065.  
  1066.   include Graphics_Core
  1067.   include Light_Core
  1068.  
  1069.   attr_accessor :bitmap
  1070.  
  1071.   def initialize
  1072.     @sprite = nil
  1073.     @bitmap = nil
  1074.     @color = Color.new(255,255,255,255)
  1075.     @r, @g, @b, @a = 255, 255, 255, 0
  1076.     @tr, @tg, @tb, @ta = 255, 255, 255, 0
  1077.     @br, @bg, @bb, @ba = 0, 0, 0, 0
  1078.     @dr, @dg, @db, @da = 0, 0, 0, 0
  1079.     @color_phase, @alpha_phase = 0, 0
  1080.     @color_dp, @alpha_dp = 0, 0
  1081.     @color_timer, @alpha_timer = nil, nil
  1082.     @lighting = false
  1083.   end
  1084.  
  1085.   def restore
  1086.     @bitmap = Bitmap.new(Screen_Width, Screen_Height)
  1087.     clear
  1088.   end
  1089.  
  1090.   def dispose
  1091.     return if @bitmap.nil?
  1092.     @bitmap.dispose
  1093.     @bitmap = nil
  1094.   end
  1095.  
  1096.   def give_sprite(s)
  1097.     @sprite = s
  1098.     @sprite.visible = @lighting
  1099.   end
  1100.  
  1101.   def release_sprite
  1102.     @sprite = nil
  1103.   end
  1104.  
  1105.   def clear
  1106.     KGL.clear(@bitmap, @color)
  1107.   end
  1108.  
  1109.   def draw
  1110.     return if @a <= 0 || !@lighting
  1111.     clear
  1112.     draw_lights unless $game_switches[Light_Sw]
  1113.   end
  1114.  
  1115.   def draw_lights
  1116.     KGL.bindFramebuffer(@bitmap)
  1117.     $game_map.lights.each { |source| source.draw_light }
  1118.   end
  1119.  
  1120.   def refresh
  1121.     refresh_ul if $game_switches[UlSys_Sw] == @lighting
  1122.     if @color_timer
  1123.       if @color_timer > 0
  1124.         @color_phase -= @color_dp
  1125.         drgb = Math.cos(@color_phase)+1
  1126.         @r = (@br + @dr*drgb).to_i
  1127.         @g = (@bg + @dg*drgb).to_i
  1128.         @b = (@bb + @db*drgb).to_i
  1129.         @color.set(@r, @g, @b)
  1130.         @color_timer -= 1
  1131.       else
  1132.         @r = @tr
  1133.         @g = @tg
  1134.         @b = @tb
  1135.         @color_timer = nil
  1136.       end
  1137.     end
  1138.     if @alpha_timer
  1139.       if @alpha_timer > 0
  1140.         @alpha_phase -= @alpha_dp
  1141.         @a = (@ba + @da*(Math.cos(@alpha_phase)+1)).to_i
  1142.         @sprite.opacity = @a
  1143.         @alpha_timer -= 1
  1144.       else
  1145.         @a = @ta
  1146.         @alpha_timer = nil
  1147.       end
  1148.     end
  1149.   end
  1150.  
  1151.   def refresh_ul
  1152.     @lighting = !$game_switches[UlSys_Sw]
  1153.     @lighting ? show : hide
  1154.   end
  1155.  
  1156.   def set_color(r, g, b, time = nil)
  1157.     r = 0 if r < 0; r = 255 if r > 255
  1158.     g = 0 if g < 0; g = 255 if g > 255
  1159.     b = 0 if b < 0; b = 255 if b > 255
  1160.     if time.nil?
  1161.       @tr = @r = 255 - r
  1162.       @tg = @g = 255 - g
  1163.       @tb = @b = 255 - b
  1164.       @color.set(@r, @g, @b)
  1165.       @color_timer = nil
  1166.     else
  1167.       @color_phase = Math::PI
  1168.       @color_dp = @color_phase/time
  1169.       @tr, @tg, @tb = 255 - r, 255 - g, 255 - b
  1170.       @br, @bg, @bb = @r, @g, @b
  1171.       @dr = (@tr - @br)/2
  1172.       @dg = (@tg - @bg)/2
  1173.       @db = (@tb - @bb)/2
  1174.       @color_timer = time - 1
  1175.     end
  1176.   end
  1177.  
  1178.   def set_opacity(a, time = nil)
  1179.     a = 0 if a < 0
  1180.     a = 255 if a > 255
  1181.     if time.nil?
  1182.       @ta = @a = a
  1183.       @sprite.opacity = @a unless @sprite.nil?
  1184.       @alpha_timer = nil
  1185.     else
  1186.       @alpha_phase = Math::PI
  1187.       @alpha_dp = @alpha_phase/time
  1188.       @ta = a
  1189.       @ba = @a
  1190.       @da = (@ta - @ba)/2
  1191.       @alpha_timer = time - 1
  1192.     end
  1193.   end
  1194.  
  1195.   def show
  1196.     @lighting = true
  1197.     @sprite.visible = @lighting if @sprite
  1198.   end
  1199.  
  1200.   def hide
  1201.     @lighting = false
  1202.     @sprite.visible = @lighting if @sprite
  1203.   end
  1204.  
  1205.   def opacity
  1206.     @a
  1207.   end
  1208.  
  1209. end
  1210.  
  1211. #-------------------------------------------------------------------------------
  1212. # * Sprite GlobalShadow
  1213. #-------------------------------------------------------------------------------
  1214.  
  1215. class Shadow_Overlay < Sprite
  1216.  
  1217.   include Light_Core
  1218.  
  1219.   def initialize
  1220.     super(nil)
  1221.     $game_map.shadows.restore
  1222.     self.bitmap = $game_map.shadows.bitmap
  1223.     self.blend_type = 2
  1224.     self.opacity = $game_map.shadows.opacity
  1225.     self.z = Shadow_Overlay_Z
  1226.     $game_map.shadows.give_sprite(self)
  1227.   end
  1228.  
  1229.   def update
  1230.     $game_map.shadows.refresh
  1231.     $game_map.shadows.draw
  1232.   end
  1233.  
  1234.   def dispose
  1235.     $game_map.shadows.release_sprite
  1236.     $game_map.shadows.dispose
  1237.     self.bitmap = nil
  1238.     super
  1239.   end
  1240.  
  1241. end
  1242.  
  1243. #-------------------------------------------------------------------------------
  1244. # * Game CharacterBase
  1245. #-------------------------------------------------------------------------------
  1246.  
  1247. class Game_CharacterBase
  1248.  
  1249.   include Light_Core
  1250.  
  1251.   attr_accessor :light
  1252.  
  1253.   alias kul_initialize initialize
  1254.  
  1255.   def initialize
  1256.     kul_initialize
  1257.     @light = nil
  1258.   end
  1259.  
  1260.   def give_light(i)
  1261.     drop_light
  1262.     create_light(i)
  1263.   end
  1264.  
  1265.   def create_light(id)
  1266.     effect = Lights[id.to_s]
  1267.     if effect.nil?
  1268.       print "Warning! Light #{id.to_s} not found!\n"
  1269.       return
  1270.     end
  1271.     @light = (effect[:light].is_a?(Hash) ? Light_MKSource.new : Light_SKSource.new)
  1272.     @light.set_light(id.to_s, effect[:light])
  1273.     @light.follow_char(self)
  1274.     @light.set_group(effect[:group])
  1275.     @light.set_offset(effect[:offset])
  1276.     @light.set_opacity(effect[:opacity], effect[:variation])
  1277.     @light.set_mode(effect[:static], effect[:shadows])
  1278.     $game_map.lights << self
  1279.   end
  1280.  
  1281.   def drop_light
  1282.     if @light
  1283.       $game_map.lights.delete(self)
  1284.       @light.dispose
  1285.       @light = nil
  1286.     end
  1287.   end
  1288.  
  1289.   def carrying_light?
  1290.     @light != nil
  1291.   end
  1292.  
  1293.   def restore_light
  1294.     @light.restore
  1295.   end
  1296.  
  1297.   def dispose_light
  1298.     @light.dispose
  1299.   end
  1300.  
  1301.   def draw_light
  1302.     @light.refresh_xyh
  1303.     KGL.lightShader($kgs.dynamic ? @light.dbmp : @light.sbmp, @light.screen_x, @light.screen_y, @light.opacity) if @light.visible?
  1304.   end
  1305.  
  1306. end
  1307.  
  1308. #-------------------------------------------------------------------------------
  1309. # * Game Event
  1310. #-------------------------------------------------------------------------------
  1311.  
  1312. class Game_Event < Game_Character
  1313.  
  1314.   alias kul_extend_setup khas_extend_setup
  1315.   alias kul_call_khas_tag call_khas_tag
  1316.   alias kul_call_khas_command call_khas_command
  1317.  
  1318.   def khas_extend_setup
  1319.     kul_extend_setup
  1320.     drop_light
  1321.   end
  1322.  
  1323.   def call_khas_tag(t)
  1324.     kul_call_khas_tag(t)
  1325.     case t
  1326.       when "static"; @light.set_mode(true, @light.shadows) if @light
  1327.       when "dynamic"; @light.set_mode(false, @light.shadows) if @light
  1328.       when "shadows"; @light.set_mode(@light.static, true) if @light
  1329.       when "no_shadows"; @light.set_mode(@light.static, false) if @light
  1330.     end
  1331.   end
  1332.  
  1333.   def call_khas_command(c,v)
  1334.     kul_call_khas_command(c,v)
  1335.     case c
  1336.       when "light"; create_light(v) if @light.nil?
  1337.       when "group"; @light.set_group(v.to_sym) if @light
  1338.     end
  1339.   end
  1340.  
  1341. end
  1342.  
  1343. #-------------------------------------------------------------------------------
  1344. # * Scene Map
  1345. #-------------------------------------------------------------------------------
  1346.  
  1347. class Scene_Map < Scene_Base
  1348.  
  1349.   alias kul_pre_battle_scene pre_battle_scene
  1350.  
  1351.   def pre_battle_scene
  1352.     kul_pre_battle_scene
  1353.     @spriteset.dispose_ultra_graphics
  1354.   end
  1355.  
  1356. end
  1357.  
  1358. #-------------------------------------------------------------------------------
  1359. # * Shadow CFF
  1360. #-------------------------------------------------------------------------------
  1361.  
  1362. class Shadow_CFF
  1363.  
  1364.   def initialize(x1,x2,y,h)
  1365.     @x1 = x1
  1366.     @x2 = x2
  1367.     @y = y
  1368.     @h = h
  1369.   end
  1370.  
  1371.   def cast(light)
  1372.     KGL.shadowShaderH(@x1-light.x, @x2-light.x, @y-light.y) if light.cy > @y || light.h >= @h
  1373.   end
  1374.  
  1375.   def cast_offset(light, ox, oy)
  1376.     KGL.shadowShaderH(@x1+ox-light.x, @x2+ox-light.x, @y+oy-light.y) if light.cy > (@y+oy) || light.h >= @h
  1377.   end
  1378.  
  1379. end
  1380.  
  1381. #-------------------------------------------------------------------------------
  1382. # * Shadow CBF
  1383. #-------------------------------------------------------------------------------
  1384.  
  1385. class Shadow_CBF
  1386.  
  1387.   def initialize(x1,x2,y,h)
  1388.     @x1 = x1
  1389.     @x2 = x2
  1390.     @y = y
  1391.     @h = h
  1392.   end
  1393.  
  1394.   def cast(light)
  1395.     KGL.shadowShaderH(@x1-light.x, @x2-light.x, @y-light.y) if light.cy < @y || light.h >= @h
  1396.   end
  1397.  
  1398.   def cast_offset(light, ox, oy)
  1399.     KGL.shadowShaderH(@x1+ox-light.x, @x2+ox-light.x, @y+oy-light.y) if light.cy < (@y+oy) || light.h >= @h
  1400.   end
  1401.  
  1402. end
  1403.  
  1404. #-------------------------------------------------------------------------------
  1405. # * Shadow CLF
  1406. #-------------------------------------------------------------------------------
  1407.  
  1408. class Shadow_CLF
  1409.  
  1410.   def initialize(y1,y2,x,h)
  1411.     @x = x
  1412.     @y1 = y1
  1413.     @y2 = y2
  1414.     @h = h
  1415.   end
  1416.  
  1417.   def cast(light)
  1418.     KGL.shadowShaderV(@y1-light.y, @y2-light.y, @x-light.x) if light.cx < @x || light.h >= @h
  1419.   end
  1420.  
  1421.   def cast_offset(light, ox, oy)
  1422.     KGL.shadowShaderV(@y1+oy-light.y, @y2+oy-light.y, @x+ox-light.x) if light.cx < (@x+ox) || light.h >= @h
  1423.   end
  1424.  
  1425. end
  1426.  
  1427. #-------------------------------------------------------------------------------
  1428. # * Shadow CRF
  1429. #-------------------------------------------------------------------------------
  1430.  
  1431. class Shadow_CRF
  1432.  
  1433.   def initialize(y1,y2,x,h)
  1434.     @x = x
  1435.     @y1 = y1
  1436.     @y2 = y2
  1437.     @h = h
  1438.   end
  1439.  
  1440.   def cast(light)
  1441.     KGL.shadowShaderV(@y1-light.y, @y2-light.y, @x-light.x) if light.cx > @x || light.h >= @h
  1442.   end
  1443.  
  1444.   def cast_offset(light, ox, oy)
  1445.     KGL.shadowShaderV(@y1+oy-light.y, @y2+oy-light.y, @x+ox-light.x) if light.cx > (@x+ox) || light.h >= @h
  1446.   end
  1447.  
  1448. end
  1449.  
  1450. #-------------------------------------------------------------------------------
  1451. # * Shadow CLW
  1452. #-------------------------------------------------------------------------------
  1453.  
  1454. class Shadow_CLW
  1455.  
  1456.   def initialize(y1,y2,y0,y3,x)
  1457.     @x = x
  1458.     @y1 = y1
  1459.     @y2 = y2
  1460.     @y0 = y0
  1461.     @y3 = y3
  1462.   end
  1463.  
  1464.   def cast(light)
  1465.     KGL.shadowShaderV(@y1-light.y, @y2-light.y, @x-light.x) if light.cx < @x && light.cy < @y3
  1466.     KGL.shadowShaderW(@y1-light.y, @y2-light.y, @x-light.x) if light.cx > @x && light.cy >= @y0
  1467.   end
  1468.  
  1469.   def cast_offset(light, ox, oy)
  1470.     KGL.shadowShaderV(@y1+oy-light.y, @y2+oy-light.y, @x+ox-light.x) if light.cx < (@x+ox) && light.cy < (@y3+oy)
  1471.     KGL.shadowShaderW(@y1+oy-light.y, @y2+oy-light.y, @x+ox-light.x) if light.cx > (@x+ox) && light.cy >= (@y0+oy)
  1472.   end
  1473.  
  1474. end
  1475.  
  1476. #-------------------------------------------------------------------------------
  1477. # * Shadow CRW
  1478. #-------------------------------------------------------------------------------
  1479.  
  1480. class Shadow_CRW
  1481.  
  1482.   def initialize(y1,y2,y0,y3,x)
  1483.     @x = x
  1484.     @y1 = y1
  1485.     @y2 = y2
  1486.     @y0 = y0
  1487.     @y3 = y3
  1488.   end
  1489.  
  1490.   def cast(light)
  1491.     KGL.shadowShaderV(@y1-light.y, @y2-light.y, @x-light.x) if light.cx > @x && light.cy < @y3
  1492.     KGL.shadowShaderW(@y1-light.y, @y2-light.y, @x-light.x) if light.cx < @x && light.cy >= @y0
  1493.   end
  1494.  
  1495.   def cast_offset(light, ox, oy)
  1496.     KGL.shadowShaderV(@y1+oy-light.y, @y2+oy-light.y, @x+ox-light.x) if light.cx > (@x+ox) && light.cy < (@y3+oy)
  1497.     KGL.shadowShaderW(@y1+oy-light.y, @y2+oy-light.y, @x+ox-light.x) if light.cx < (@x+ox) && light.cy >= (@y0+oy)
  1498.   end
  1499.  
  1500. end
  1501.  
  1502. #-------------------------------------------------------------------------------
  1503. # * Table Casters
  1504. #-------------------------------------------------------------------------------
  1505.  
  1506. class Table_Casters < Table
  1507.  
  1508.   def n?(x,y,b)
  1509.     (self[x,y] & b) == 0
  1510.   end
  1511.  
  1512. end
  1513.  
  1514. #-------------------------------------------------------------------------------
  1515. # * Shadow Casters
  1516. #-------------------------------------------------------------------------------
  1517.  
  1518. class Shadow_Casters
  1519.  
  1520.   include Light_Core
  1521.  
  1522.   attr_reader :width
  1523.   attr_reader :height
  1524.  
  1525.   def initialize(map_width, map_height, loop_x, loop_y)
  1526.     @map_width = map_width
  1527.     @map_height = map_height
  1528.     @loop_x = loop_x
  1529.     @loop_y = loop_y
  1530.     @ox = @map_width*32
  1531.     @oy = @map_height*32
  1532.     build_data
  1533.   end
  1534.  
  1535.   def build_data
  1536.     @width = ((@map_width - 1) >> SCShift) + 1
  1537.     @height = ((@map_height - 1) >> SCShift) + 1
  1538.     @data = Array.new(@width) { Array.new(@height) { [] } }
  1539.   end
  1540.  
  1541.   def same_chunk?(x1, y1, x2, y2)
  1542.     return false if x2 >= @map_width || y2 >= @map_height
  1543.     (x1 >> SCShift == x2 >> SCShift) && (y1 >> SCShift == y2 >> SCShift)
  1544.   end
  1545.  
  1546.   def push(x, y, caster)
  1547.     @data[x >> SCShift][y >> SCShift] << caster
  1548.   end
  1549.  
  1550.   def shadow(light)
  1551.     if @loop_x || @loop_y
  1552.       x1 = (light.char.real_x - light.tile_hw).to_i / SCSize
  1553.       x2 = (light.char.real_x + light.tile_hw).to_i / SCSize + 1
  1554.       y1 = (light.char.real_y - light.tile_hh).to_i / SCSize
  1555.       y2 = (light.char.real_y + light.tile_hh).to_i / SCSize + 1
  1556.       if @loop_x
  1557.         x1 -= 1 if x1 < 0
  1558.         x2 += 1 if x2 >= @width
  1559.       else
  1560.         x1 = 0 if x1 < 0
  1561.         x2 = @width - 1 if x2 >= @width
  1562.       end
  1563.       if @loop_y
  1564.         y1 -= 1 if y1 < 0
  1565.         y2 += 1 if y2 >= @height
  1566.       else
  1567.         y1 = 0 if y1 < 0
  1568.         y2 = @height - 1 if y2 >= @height
  1569.       end
  1570.       for x in x1..x2
  1571.         for y in y1..y2
  1572.           ox = (x < 0 ? -@ox : 0)
  1573.           oy = (y < 0 ? -@oy : 0)
  1574.           ox = @ox if x >= @width
  1575.           oy = @oy if y >= @height
  1576.           if ox == 0 && oy == 0
  1577.             for caster in @data[x][y]
  1578.               caster.cast(light)
  1579.             end
  1580.           else
  1581.             for caster in @data[x % @width][y % @height]
  1582.               caster.cast_offset(light,ox,oy)
  1583.             end
  1584.           end
  1585.         end
  1586.       end
  1587.     else
  1588.       x1 = (light.char.real_x - light.tile_hw).to_i
  1589.       x2 = (light.char.real_x + light.tile_hw).to_i
  1590.       y1 = (light.char.real_y - light.tile_hh).to_i
  1591.       y2 = (light.char.real_y + light.tile_hh).to_i
  1592.       x1 = 0 if x1 < 0
  1593.       x2 = @map_width - 1 if x2 >= @map_width
  1594.       y1 = 0 if y1 < 0
  1595.       y2 = @map_height - 1 if y2 >= @map_height
  1596.       x1 >>= SCShift
  1597.       x2 >>= SCShift
  1598.       y1 >>= SCShift
  1599.       y2 >>= SCShift
  1600.       for x in x1..x2
  1601.         for y in y1..y2
  1602.           for caster in @data[x][y]
  1603.             caster.cast(light)
  1604.           end
  1605.         end
  1606.       end
  1607.     end
  1608.   end
  1609.  
  1610. end
  1611.  
  1612. #-------------------------------------------------------------------------------
  1613. # * Game Map
  1614. #-------------------------------------------------------------------------------
  1615.  
  1616. class Game_Map
  1617.  
  1618.   include Light_Core
  1619.  
  1620.   attr_accessor :lights
  1621.   attr_accessor :shadows
  1622.   attr_accessor :casters
  1623.  
  1624.   alias kul_setup_map khas_setup_map
  1625.   alias kul_call_khas_command call_khas_command
  1626.  
  1627.   def khas_setup_map
  1628.     kul_setup_map
  1629.     setup_ultra_lighting
  1630.   end
  1631.  
  1632.   def call_khas_command(c,v)
  1633.     kul_call_khas_command(c,v)
  1634.     case c
  1635.       when "lighting"; (v == "on" ? @shadows.show : @shadows.hide)
  1636.       when "opacity"; @shadows.set_opacity(v.to_i)
  1637.       when "color"; @shadows.set_color(v[0,3].to_i,v[3,3].to_i,v[6,3].to_i)
  1638.     end
  1639.   end
  1640.  
  1641.   alias kul_initialize initialize
  1642.  
  1643.   def initialize
  1644.     initialize_ultra_graphics
  1645.     kul_initialize
  1646.   end
  1647.  
  1648.   def initialize_ultra_graphics
  1649.     @lights = []
  1650.     @shadows = Shadow.new
  1651.   end
  1652.  
  1653.   def setup_ultra_lighting
  1654.     @lights.clear
  1655.     @lights << $game_player if $game_player.carrying_light?
  1656.     @casters = Khas_Core.shadow_casters(@map_id, @map, @height_map) if Enable_Shadows
  1657.     disable_std_shadows if Disable_STD_Shadows
  1658.   end
  1659.  
  1660.   def disable_std_shadows
  1661.     data.xsize.times do |x|
  1662.       data.ysize.times do |y|
  1663.         data[x, y, 0x03] &= 0x0fff0
  1664.       end
  1665.     end
  1666.   end
  1667.  
  1668. end
  1669.  
  1670. #-------------------------------------------------------------------------------
  1671. # * Spriteset Map
  1672. #-------------------------------------------------------------------------------
  1673.  
  1674. class Spriteset_Map
  1675.  
  1676.   include Light_Core
  1677.  
  1678.   alias kul_initialize initialize
  1679.   alias kul_update update
  1680.   alias kul_dispose dispose
  1681.  
  1682.   def initialize
  1683.     initialize_ultra_graphics
  1684.     kul_initialize
  1685.   end
  1686.  
  1687.   def update
  1688.     kul_update
  1689.     update_ultra_graphics
  1690.   end
  1691.  
  1692.   def dispose
  1693.     kul_dispose
  1694.     dispose_ultra_graphics
  1695.   end
  1696.  
  1697.   def initialize_ultra_graphics
  1698.     @shadow_overlay = Shadow_Overlay.new
  1699.     $game_map.lights.each { |source| source.restore_light }
  1700.     initialize_fog
  1701.   end
  1702.  
  1703.   def update_ultra_graphics
  1704.     @shadow_overlay.update
  1705.     update_fog
  1706.   end
  1707.  
  1708.   def dispose_ultra_graphics
  1709.     $game_map.lights.each { |source| source.dispose_light }
  1710.     dispose_fog
  1711.     if @shadow_overlay
  1712.       @shadow_overlay.dispose
  1713.       @shadow_overlay = nil
  1714.     end
  1715.   end
  1716.  
  1717.   def initialize_fog
  1718.   end
  1719.  
  1720.   def update_fog
  1721.   end
  1722.  
  1723.   def dispose_fog
  1724.   end
  1725.  
  1726. end
  1727.  
  1728. #-------------------------------------------------------------------------------
  1729. # * Khas Core
  1730. #-------------------------------------------------------------------------------
  1731.  
  1732. module Khas_Core
  1733.  
  1734.   @@sc_cache = {}
  1735.  
  1736.   def self.gen_shadow_casters(map, height_map)
  1737.     i = 0; j = 0; k = 0
  1738.     added = Table_Casters.new(map.width, map.height)
  1739.     casters = Shadow_Casters.new(map.width, map.height, map.scroll_type == 2 || map.scroll_type == 3, map.scroll_type == 1 || map.scroll_type == 3)
  1740.     for x in 0...map.width
  1741.       for y in 0...map.height
  1742.         next unless height_map[x,y] > 0
  1743.         if height_map.floor?(x,y)
  1744.           if added.n?(x,y,0b1) && height_map[x,y] > height_map.lxy(x,y+1)
  1745.             added[x,y] |= 0b1
  1746.             i = 1
  1747.             while(height_map.floor?(x+i,y) && casters.same_chunk?(x,y,x+i,y) && height_map.lxy(x+i,y) > height_map.lxy(x+i,y+1))
  1748.               added[x+i,y] |= 0b1
  1749.               i += 1
  1750.             end
  1751.             casters.push(x,y,Shadow_CFF.new(x*32, (x+i)*32, (y+1)*32, height_map[x,y]))
  1752.           end
  1753.           if added.n?(x,y,0b10) && height_map[x,y] > height_map.lxy(x,y-1)
  1754.             added[x,y] |= 0b10
  1755.             i = 1
  1756.             while(height_map.floor?(x+i,y) && casters.same_chunk?(x,y,x+i,y) && height_map.lxy(x+i,y) > height_map.lxy(x+i,y-1))
  1757.               added[x+i,y] |= 0b10
  1758.               i += 1
  1759.             end
  1760.             casters.push(x,y,Shadow_CBF.new(x*32, (x+i)*32, y*32, height_map[x,y]))
  1761.           end
  1762.           if added.n?(x,y,0b100) && height_map[x,y] > height_map.lxy(x-1,y)
  1763.             added[x,y] |= 0b100
  1764.             i = 1
  1765.             while(height_map.floor?(x,y+i) && casters.same_chunk?(x,y,x,y+i) && height_map.lxy(x,y+i) > height_map.lxy(x-1,y+i))
  1766.               added[x,y+i] |= 0b100
  1767.               i += 1
  1768.             end
  1769.             casters.push(x,y,Shadow_CLF.new(y*32, (y+i)*32, x*32, height_map[x,y]))
  1770.           end
  1771.           if added.n?(x,y,0b1000) && height_map[x,y] > height_map.lxy(x+1,y)
  1772.             added[x,y] |= 0b1000
  1773.             i = 1
  1774.             while(height_map.floor?(x,y+i) && casters.same_chunk?(x,y,x,y+i) && height_map.lxy(x,y+i) > height_map.lxy(x+1,y+i))
  1775.               added[x,y+i] |= 0b1000
  1776.               i += 1
  1777.             end
  1778.             casters.push(x,y,Shadow_CRF.new(y*32, (y+i)*32, (x+1)*32, height_map[x,y]))
  1779.           end
  1780.         end
  1781.         if height_map.wall?(x,y)
  1782.           if added.n?(x,y,0b10000) && height_map[x,y] > height_map.lxy(x-1,y)
  1783.             added[x,y] |= 0b10000
  1784.             i = 1
  1785.             while(height_map.wall?(x,y+i) && casters.same_chunk?(x,y,x,y+i) && height_map[x,y+i] > height_map.lxy(x-1,y+i))
  1786.               added[x,y+i] |= 0b10000
  1787.               i += 1
  1788.             end
  1789.             k = 1
  1790.             while(height_map.wall?(x,y-k) && (y-k) > 0 && height_map[x,y-k] > height_map.lxy(x-1,y-k))
  1791.               k += 1
  1792.             end
  1793.             k -= 1
  1794.             j = i
  1795.             while(height_map.wall?(x,y+j) && (y+j) < map.height && height_map[x,y+j] > height_map.lxy(x-1,y+j))
  1796.               j += 1
  1797.             end
  1798.             casters.push(x,y,Shadow_CLW.new(y*32, (y+i)*32, (y-k)*32, (y+j)*32, x*32))
  1799.           end
  1800.           if added.n?(x,y,0b100000) && height_map[x,y] > height_map.lxy(x+1,y)
  1801.             added[x,y] |= 0b100000
  1802.             i = 1
  1803.             while(height_map.wall?(x,y+i) && casters.same_chunk?(x,y,x,y+i) && height_map[x,y+i] > height_map.lxy(x+1,y+i))
  1804.               added[x,y+i] |= 0b100000
  1805.               i += 1
  1806.             end
  1807.             k = 1
  1808.             while(height_map.wall?(x,y-k) && (y-k) > 0 && height_map[x,y-k] > height_map.lxy(x+1,y-k))
  1809.               k += 1
  1810.             end
  1811.             k -= 1
  1812.             j = i
  1813.             while(height_map.wall?(x,y+j) && (y+j) < map.height && height_map[x,y+j] > height_map.lxy(x+1,y+j))
  1814.               j += 1
  1815.             end
  1816.             casters.push(x,y,Shadow_CRW.new(y*32,(y+i)*32, (y-k)*32, (y+j)*32, (x+1)*32))
  1817.           end
  1818.         end
  1819.       end
  1820.     end
  1821.     casters
  1822.   end
  1823.  
  1824.   def self.shadow_casters(map_id, map, height_map)
  1825.     @@sc_cache.include?(map_id) ? @@sc_cache[map_id] : gen_shadow_casters(map, height_map)
  1826.   end
  1827.  
  1828.   def self.load_sc_data
  1829.     for id in Load_Map_Data
  1830.       map = load_data(sprintf("Data/Map%03d.rvdata2", id))
  1831.       @@sc_cache[id] = gen_shadow_casters(map, @@height_cache[id])
  1832.     end
  1833.   end
  1834.  
  1835. end
  1836.  
  1837. Khas_Core.load_sc_data if Light_Core::Enable_Shadows
  1838.  
  1839. #-------------------------------------------------------------------------------
  1840. # * End
  1841. #-------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement