Rataime's Shadows and Reflections

There is a demo for these scripts in the Download Manager, but I wanted to post them here anyways so you don't have to download the demo if you don't want. All credit goes to Rataime, I did not make any of these scripts.:-/

Rataime's Shadow Script
For this you need both of the scripts I have provided. Instructions on usage are below the scripts.
Dynamic Shadows by Rataime Show
#==============================================================================
#  –  Sprite_Shadow (Sprite_Ombre )
# Based on Genzai Kawakami's shadows, dynamisme&features by Rataime, extra features Boushy
#==============================================================================

CATERPILLAR_COMPATIBLE = true
SHADOW_WARN = true

class Game_Party
attr_reader :characters
end

class Sprite_Shadow < RPG::Sprite

attr_accessor :character

def initialize(viewport, character = nil,id=0)
   super(viewport)
   params=$shadow_spriteset.shadows[id]
   @source=params[0]
   @anglemin=0
   @anglemin=params[1] if params.size>1
   @anglemax=0
   @anglemax=params[2] if params.size>2
   @self_opacity=100
   @self_opacity=params[4] if params.size>4
   @distancemax=350
   @distancemax=params[3] if params.size>3
   @character = character
   update
end

def update

   if !in_range?(@character, @source, @distancemax)
     self.opacity=0
     return
   end
   super
   
   if @tile_id != @character.tile_id or
      @character_name != @character.character_name or
      @character_hue != @character.character_hue
     @tile_id = @character.tile_id
     @character_name = @character.character_name
     @character_hue = @character.character_hue
     if @tile_id >= 384
       self.bitmap = RPG::Cache.tile($game_map.tileset_name,
         @tile_id, @character.character_hue)
       self.src_rect.set(0, 0, 32, 32)
       self.ox = 16
       self.oy = 32
     else
       self.bitmap = RPG::Cache.character(@character.character_name,
         @character.character_hue)
       @cw = bitmap.width / 4
       @ch = bitmap.height / 4
       self.ox = @cw / 2
       self.oy = @ch
     end
   end
   self.visible = (not @character.transparent)
   if @tile_id == 0
     sx = @character.pattern * @cw
     sy = (@character.direction - 2) / 2 * @ch
     if self.angle>90 or angle<-90
       if @character.direction== 6
              sy = ( 4- 2) / 2 * @ch
       end
       if @character.direction== 4
              sy = ( 6- 2) / 2 * @ch
       end
       if @character.direction== 2
              sy = ( 8- 2) / 2 * @ch
       end
       if @character.direction== 8
              sy = ( 2- 2) / 2 * @ch
       end
     end
     self.src_rect.set(sx, sy, @cw, @ch)
   end
   self.x = @character.screen_x
   self.y = @character.screen_y-5
   self.z = @character.screen_z(@ch)-1
   self.blend_type = @character.blend_type
   self.bush_depth = @character.bush_depth
   if @character.animation_id != 0
     animation = $data_animations[@character.animation_id]
     animation(animation, true)
     @character.animation_id = 0
   end
   @[email protected]_x-self.x
   @deltay= @source.screen_y-self.y
   self.color = Color.new(0, 0, 0)
   @distance = ((@deltax ** 2) + (@deltay ** 2))
   self.opacity = @self_opacity*13000/((@distance*370/@distancemax)+6000)
   self.angle = 57.3*Math.atan2(@deltax, @deltay )
   @angle_trigo=self.angle+90
   if @angle_trigo<0
       @angle_trigo=360+@angle_trigo
    end
   if @anglemin !=0 or @anglemax !=0
      if (@angle_trigo<@anglemin or @angle_trigo>@anglemax) and @anglemin<@anglemax
        self.opacity=0
        return
      end
      if (@angle_trigo<@anglemin and @angle_trigo>@anglemax) and @anglemin>@anglemax
        self.opacity=0
        return
      end     
    end
end
 
def in_range?(element, object, range)# From Near's Anti Lag Script, edited
   x = (element.screen_x - object.screen_x) * (element.screen_x - object.screen_x)
   y = (element.screen_y - object.screen_y) * (element.screen_y - object.screen_y)
   r = x + y
   if r <= (range * range)
      return true
   else
     return false
   end
end

end

#===================================================
#  –¼ CLASS Sprite_Character edit
#===================================================

class Sprite_Character < RPG::Sprite
alias shadow_initialize initialize

def initialize(viewport, character = nil)
   @character = character
   super(viewport)
   @ombrelist=[]
   if character.is_a?(Game_Event) and $shadow_spriteset.shadows!=[]
     params = XPML_read("Shadow",@character.id,4)
     if params!=nil
       for i in 0...$shadow_spriteset.shadows.size
         @ombrelist.push(Sprite_Shadow.new(viewport, @character,i))
       end
     end
   end
   if character.is_a?(Game_Player) and $shadow_spriteset.shadows!=[]
     for i in 0...$shadow_spriteset.shadows.size
       @ombrelist.push(Sprite_Shadow.new(viewport, $game_player,i))
     end
     #===================================================
     #  — Compatibility with fukuyama's caterpillar script
     #===================================================
     if CATERPILLAR_COMPATIBLE and $game_party.characters!=nil
       for member in $game_party.characters
         for i in 0...$shadow_spriteset.shadows.size
           @ombrelist.push(Sprite_Shadow.new(viewport, member,i))
         end
       end
     end
     #===================================================
     #  — End of the compatibility
     #===================================================
   end
   shadow_initialize(viewport, @character)
end

alias shadow_update update

def update
   shadow_update
   if @ombrelist!=[]
     for i in [email hidden]
       @ombrelist[i].update
     end
   end
end 

end

#===================================================
#  –¼ CLASS Game_Event edit
#===================================================
class Game_Event
attr_accessor :id
end

#===================================================
#  –¼ CLASS Spriteset_Map edit
#===================================================
class Spriteset_Map
attr_accessor :shadows
alias shadow_initialize initialize

def initialize
   $shadow_spriteset=self
   @shadows=[]
   warn=false
   for k in $game_map.events.keys.sort
     warn=true if ($game_map.events[k].list!=nil and $game_map.events[k].list[0].code == 108 and ($game_map.events[k].list[0].parameters == ["s"] or $game_map.events[k].list[0].parameters == ["o"]))
     params = XPML_read("Shadow Source",k,4)
     $shadow_spriteset.shadows.push([$game_map.events[k]]+params) if params!=nil
   end
   p "Warning : At least one event on this map uses the obsolete way to add shadows" if warn == true and SHADOW_WARN
   shadow_initialize
end 
end



#===================================================
#  –¼ XPML Definition, by Rataime, using ideas from Near Fantastica
#
#   Returns nil if the markup wasn't present at all,
#   returns [] if there wasn't any parameters, else
#   returns a parameters list with "int" converted as int
#   eg :
#   begin first
#   begin second
#   param1 1
#   param2 two
#   begin third
#   anything 3
#
#   p XPML_read("first", event_id) -> []
#   p XPML_read("second", event_id) -> [1,"two"]
#   p XPML_read("third", event_id) -> [3]
#   p XPML_read("forth", event_id) -> nil
#===================================================

def XPML_read(markup,event_id,max_param_number=0)
   parameter_list = nil
   event=$game_map.events[event_id]
   return if event.list==nil
     for i in 0...event.list.size
       if event.list[i].code == 108 and event.list[i].parameters[0].downcase == "begin "+markup.downcase
         parameter_list = [] if parameter_list == nil
         for j in i+1...event.list.size
           if event.list[j].code == 108
             parts = event.list[j].parameters[0].split
             if parts.size!=1 and parts[0].downcase!=begin"
               if parts[1].to_i!=0 or parts[1]=="0"
                 parameter_list.push(parts[1].to_i)
               else
                 parameter_list.push(parts[1])
               end
             else
               return parameter_list
             end
           else
             return parameter_list
           end
           return parameter_list if max_param_number!=0 and j==i+max_param_number
         end
       end
     end
   return parameter_list
end
Expand to see the code.

Sun Shadows by Rataime Show
#==============================================================================
#  –  Sprite_Sun
# # Based on Sprite_Shadow, modified by Rataime
#==============================================================================

CATERPILLAR_COMPATIBLE = true
SUN_WARN =true

class Game_Party
attr_reader :characters
end

class Sprite_Sun < RPG::Sprite
attr_accessor :character           

def initialize(viewport, character = nil,id=0)
   super(viewport)
   @character = character
   params=$sun_spriteset.sun[id]
   self_angle=45
   self_angle=params[0] if params.size>0
   self_opacity=128
   self_opacity=params[1] if params.size>1
   @self_angle=self_angle
   @self_opacity=self_opacity
   update
end

def update
   super
   if @tile_id != @character.tile_id or
      @character_name != @character.character_name or
      @character_hue != @character.character_hue
     @tile_id = @character.tile_id
     @character_name = @character.character_name
     @character_hue = @character.character_hue
     if @tile_id >= 384
       self.bitmap = RPG::Cache.tile($game_map.tileset_name,
         @tile_id, @character.character_hue)
       self.src_rect.set(0, 0, 32, 32)
       self.ox = 16
       self.oy = 32
     else
       self.bitmap = RPG::Cache.character(@character.character_name,
         @character.character_hue)
       @cw = bitmap.width / 4
       @ch = bitmap.height / 4
       self.ox = @cw / 2
       self.oy = @ch
     end
   end
   
   self.visible = (not @character.transparent)

   if @tile_id == 0
    sx = @character.pattern * @cw
    @[email protected]
   
     if self.angle>90 or angle<-90
 
       if @direct== 6
              sy = ( 4- 2) / 2 * @ch
     end
     if @direct== 4
              sy = ( 6- 2) / 2 * @ch
       end
       if @direct != 4 and @direct !=6
       sy = (@character.direction - 2) / 2 * @ch
         end
       else
    sy = (@character.direction - 2) / 2 * @ch
    end
      self.src_rect.set(sx, sy, @cw, @ch)
   end
   self.x = @character.screen_x
   self.y = @character.screen_y-5
   self.z = @character.screen_z(@ch)-1
   self.opacity = @self_opacity
   self.blend_type = @character.blend_type
   self.bush_depth = @character.bush_depth
   if @character.animation_id != 0
     animation = $data_animations[@character.animation_id]
     animation(animation, true)
     @character.animation_id = 0
   end
   self.angle = @self_angle.to_i-90
   self.color = Color.new(0, 0, 0)
end
end

#===================================================
#  –¼ CLASS Sprite_Character edit
#===================================================

class Sprite_Character < RPG::Sprite
alias sun_initialize initialize

def initialize(viewport, character = nil)
   @character = character
   super(viewport)
   @sunlist=[]
   if character.is_a?(Game_Event) and $sun_spriteset.sun!=[]
     params = XPML_read("Shadow",@character.id,2)
     if params!=nil
       for i in 0...$sun_spriteset.sun.size
         @sunlist.push(Sprite_Sun.new(viewport, @character,i))
       end
     end
   end
  if character.is_a?(Game_Player) and $sun_spriteset.sun!=[]
     for i in 0...$sun_spriteset.sun.size
       @sunlist.push(Sprite_Sun.new(viewport, $game_player,i))
     end
     #===================================================
     #  — Compatibility with fukuyama's caterpillar script
     #===================================================
     if CATERPILLAR_COMPATIBLE and $game_party.characters!=nil
       for member in $game_party.characters
         for i in 0...$sun_spriteset.sun.size
           @sunlist.push(Sprite_Sun.new(viewport, member,i))
         end
       end
     end
     #===================================================
     #  — End of the compatibility
     #===================================================
   end
   sun_initialize(viewport, @character)
end

alias sun_update update

def update
   sun_update
   if @sunlist!=[]
     for i in [email hidden]
       @sunlist[i].update
     end
   end
end 

end

#===================================================
#  –¼ CLASS Game_Event edit
#===================================================
class Game_Event
attr_accessor :id
end

#===================================================
#  –¼ CLASS Spriteset_Map edit
#===================================================
class Spriteset_Map
attr_accessor :sun
alias sun_initialize initialize

def initialize
   @sun=[]
   $sun_spriteset=self
   warn=false
   for k in $game_map.events.keys.sort
     warn=true if ($game_map.events[k].list!=nil and $game_map.events[k].list[0].code == 108 and ($game_map.events[k].list[0].parameters == ["sun] or $game_map.events[k].list[0].parameters == ["o"]))
     params = XPML_read("Sun",k,2)
     $sun_spriteset.sun.push(params) if params!=nil
   end
   p "Warning : At least one event on this map uses the obsolete way to add a sun effect" if warn == true and SUN_WARN
   sun_initialize
end 
end



#===================================================
#  –¼ XPML Definition, by Rataime, using ideas from Near Fantastica
#
#   Returns nil if the markup wasn't present at all,
#   returns [] if there wasn't any parameters, else
#   returns a parameters list with "int" converted as int
#   eg :
#   begin first
#   begin second
#   param1 1
#   param2 two
#   begin third
#   anything 3
#
#   p XPML_read("first", event_id) -> []
#   p XPML_read("second", event_id) -> [1,"two"]
#   p XPML_read("third", event_id) -> [3]
#   p XPML_read("forth", event_id) -> nil
#===================================================

def XPML_read(markup,event_id,max_param_number=0)
   parameter_list = nil
   event=$game_map.events[event_id]
   return if event.list==nil
     for i in 0...event.list.size
       if event.list[i].code == 108 and event.list[i].parameters[0].downcase == "begin "+markup.downcase
         parameter_list = [] if parameter_list == nil
         for j in i+1...event.list.size
           if event.list[j].code == 108
             parts = event.list[j].parameters[0].split
             if parts.size!=1 and parts[0].downcase!=begin"
               if parts[1].to_i!=0 or parts[1]=="0"
                 parameter_list.push(parts[1].to_i)
               else
                 parameter_list.push(parts[1])
               end
             else
               return parameter_list
             end
           else
             return parameter_list
           end
           return parameter_list if max_param_number!=0 and j==i+max_param_number
         end
       end
     end
   return parameter_list
end
Expand to see the code.

Shadow Script Instructions Show
The script is easily adapted with simple event comments.

To create a shadow source:
Create an event over an object that gives light. Then type in the following comment:begin shadow source.

Now you can use the following comments to perfect the shadow source:
anglemin n and anglemax n: Replace n with a number of an angle. The angles follow the Polar Coordinate Grid. If you are unsure about how to work the angles according to the Polar Coordinate Grid, please Google it.
distancemax n: Replace n with the a number to justify the radius of the shadows effect. The larger the number, the father you can be from the source and still achieve a shadow.
opacity n: Replace in with an opacity value. 255 being jet black, and 0 being completely transparent. The value you put is what the opacity is directly against the light source.

When a source is created it automatically applied to your actor.

Adding shadows to events:
Here is how to add shadows to events. Warning this only works the best with biped chatacters, not well with animals and objects.

Just place a comment saying:begin shadow.

Setting up the sun shadow effect:
This allows the character to have a static shadow created by the sun. To do this just make a comment in an event saying:begin sun. This will automatically applied to your actor, and any events with the begin shadow comment.

There you go! :p


Rataime's Reflection Script
The only script is the one below. The instructions are below that.
Reflections by Rataime Show
#==============================================================================
#  –  Sprite_Reflection
# Based on Sprite_Mirror, Modified By: JmsPlDnl, rewritten entirely by Rataime
#==============================================================================
CATERPILLAR_COMPATIBLE = true

class Game_Party
attr_reader :characters
end

class Sprite_Reflection < RPG::Sprite

attr_accessor :character

def initialize(viewport=nil, character=nil,self_angle = 180)
  super(viewport)
  @character = character
  @self_angle=self_angle
  self.opacity=0
  @reflected=false
  @former=false
  @moving=false
  if $game_map.terrain_tag(@character.real_x/128,@character.real_y/128+1)==7
    @reflected=true
    @former=true
  end
  update
end

def update
  super
  if @tile_id != @character.tile_id or
     @character_name != @character.character_name or
     @character_hue != @character.character_hue
    @tile_id = @character.tile_id
    @character_name = @character.character_name
    @character_hue = @character.character_hue
    if @tile_id >= 384
      self.bitmap = RPG::Cache.tile($game_map.tileset_name,
        @tile_id, @character.character_hue)
      self.src_rect.set(0, 0, 32, 32)
      self.ox = 16
      self.oy = 32
    else
      self.bitmap = RPG::Cache.character(@character.character_name,
        @character.character_hue)
      @cw = bitmap.width / 4
      @ch = bitmap.height / 4
      self.ox = @cw / 2
      self.oy = @ch
    end
  end
 
  self.visible = (not @character.transparent)
  if @tile_id == 0
    sx = (@character.pattern) * @cw
    sy = (@character.direction - 2) / 2 * @ch
    if @character.direction== 6
          sy = ( 4- 2) / 2 * @ch
    end
    if @character.direction== 4
          sy = ( 6- 2) / 2 * @ch
   end
   if @character.direction != 4 and @character.direction != 6
         sy = (@character.direction - 2) / 2 * @ch
   end
  end
 
  self.x = @character.screen_x
  self.y = @character.screen_y-5
  @moving=!(@character.real_x%128==0 and @character.real_y%128==0)
  @[email protected]
  @rect=[sx, sy, @cw, @ch]
  if !(@moving)
    if $game_map.terrain_tag(@character.real_x/128,@character.real_y/128+1)==7
      @reflected=true
      @former=true
    else
      @reflected=false 
      @former=false
    end
   
  else
      case @d
     
      when 2
      if $game_map.terrain_tag(@character.real_x/128,@character.real_y/128+2)==7
        @reflected=true
        if @former==false
          @offset = (@character.screen_y%32)*@ch/32
          @rect=[sx, sy, @cw, @offset]
          [email protected]_y-5
        end
      else
        @reflected=false 
      end
     
      when 4
      if $game_map.terrain_tag(@character.real_x/128,@character.real_y/128+1)!=7
        @offset = ((@character.screen_x-@cw/2)%32)*@cw/32
        @rect=[sx, sy, @offset, @ch]
        [email protected]_x
      else
        @reflected=true
        if @former==false
          @offset = ((@character.screen_x-@cw/2)%32)*@cw/32
          @rect=[sx+@offset, sy, @cw-@offset, @ch]
          [email protected]_x-@offset
        end
      end
     
      when 6
      if $game_map.terrain_tag(@character.real_x/128+1,@character.real_y/128+1)!=7
        @offset = ((@character.screen_x-@cw/2)%32)*@cw/32
        @rect=[sx+@offset, sy, @cw-@offset, @ch]
        [email protected]_x-@offset
      else
        @reflected=true
        if @former==false
          @offset = ((@character.screen_x-@cw/2)%32)*@cw/32
          @rect=[sx, sy, @offset, @ch]
          [email protected]_x
        end
      end
     
      when 8
      if $game_map.terrain_tag(@character.real_x/128,@character.real_y/128+2)==7
        @reflected=true
        if $game_map.terrain_tag(@character.real_x/128,@character.real_y/128+1)!=7
          @offset = (@character.screen_y%32)*@ch/32
          @rect=[sx, sy, @cw, @offset]
          [email protected]_y-5
        end
      else
        @reflected=false 
      end
     
      end

  end
  if @reflected
    self.opacity=128
  else
   @rect=[sx, sy, @cw, @ch]
   self.opacity=0
  end
 
  if $game_map.terrain_tag((@character.real_x+64)/128,@character.real_y/128+2)!=7
    if $game_map.terrain_tag((@character.real_x+64)/128,@character.real_y/128+2)!=7
      @rect[1]= @rect[1]+@ch/2
      @rect[3]= @rect[3]/2
      self.y = self.y - @ch/2
    else
      @reflected=false
    end
  end
   
  self.src_rect.set(@rect[0],@rect[1],@rect[2],@rect[3])
 
  @character.is_a?(Game_Player) ? self.z = 9 : self.z = 5
  self.blend_type = @character.blend_type
  self.bush_depth = @character.bush_depth
  if @character.animation_id != 0
    animation = $data_animations[@character.animation_id]
    animation(animation, true)
    @character.animation_id = 0
  end

  self.angle = @self_angle
end
end

#===================================================
#  –¼ CLASS Sprite_Character edit
#===================================================

class Sprite_Character < RPG::Sprite
alias reflect_initialize initialize

def initialize(viewport, character = nil)
   @character = character
   @reflection = []
   super(viewport)
   if (character.is_a?(Game_Event) and character.list!=nil and character.list[0].code == 108 and character.list[0].parameters == ["r])
    @reflection.push(Sprite_Reflection.new(viewport,@character))
   end
   if (character.is_a?(Game_Event) and character.list!=nil and character.list[0].code == 108 and character.list[0].parameters == ["hero_r"])
    @reflection.push(Sprite_Reflection.new(viewport,$game_player))
#===================================================
#  — Compatibility with fukuyama's caterpillar script
#===================================================
if CATERPILLAR_COMPATIBLE and $game_party.characters!=nil

for member in $game_party.characters
   @reflection.push(Sprite_Reflection.new(viewport,member))
end

end
#===================================================
#  — End of the compatibility
#===================================================
   end
   reflect_initialize(viewport, @character)
end

alias reflect_update update

def update
  reflect_update
  if @reflection!=nil
    for reflect in @reflection
      reflect.update
    end
  end
end

end
Expand to see the code.

Reflections Instructions Show
I will tell you how to apply reflections to both actor and events.

To get reflections going you need to have an event with a comment saying:hero_r. The '_' must be included. Now you have to go into your Databse, then click on the Tilesets tab. Find the tileset you want to use it on, and then click on the big 'Terrain Tags' button at the right of the box. Find any water you want to apply the effect to and click it's tile until the number is '7'. The tile will only cause a reflection if it has that terrain tag! To apply reflections to an event, make a comment saying:r. This must be inside the event with their graphic.

There you go!


These are both great scripts, but they have some flaws, and are not completely compatible with all scripts. If you find some incompatabilities with some scripts, tell me here and I will start a list.

Hope you enjoy, credit goes to Rataime!:yes:

Peace,
EiB
Thanks but I perfer the demo. lol. Its easier to use.
This rocks.
I find very little things that could be fixed, like when you have the water reflect the actor and when your standing on something like a bridge you can see the reflection even on the bridge itself.
Another thing is that when you have a light source like a window, and you move behind the building you can still see the shadow from the actor when behind the window.
I think this is a great script that I will probably use in my game.
I really hope someone makes this SDK compatible
I think they are, look at the SDK Test Bed.
@Chaos_Prime: That is what anglemin and anglemax are for. Use the Polar Coordinate grid to get the angles. I think that for that it would be anglemin 180 and anglemax 360. I am not profecient in trigonometry because I am only a freshman in high school, but I have done some looking at Polar Coordinates.

If Polar Coordinates and the angling becomes a major problem, I can try and set up a little lesson. If those angles don't work, then I need to brush up on some stuff before I write a tutorial.

EDIT:
The angles I provided are correct for putting a light against an interior wall. I am going to get a tutorial working on how to use this for those who are not mathmatecians. :p

Peace,
EiB
Check this out, might help;
http://www.ccd.rpi.edu/Eglash/csdt/subcult/grafitti/math/Polar_Coordinate_Grid.htm
Wow, this is cool, I'll definitely be using it in my game, and I'll credit you :)
[QUOTE=Myonosken;114814]I'm still pretty damn amazed no-one has done any updated versions and/or SDKed this yet- its so popular with the newer members, you'd be guaranteed idolisation.[/QUOTE]

Maybe because it is not true.

I have SDK'ed all three scripts (I simply had to for a game). Also, I merged it with an time system so the shadows date up their angle and opacity every minut (in game-minut).

I just did not posted it. No-one seems to care :P
And what about the destruction engine of his?
[QUOTE=Meââ ‚¬Å¾Ã‚ ¢;155520]Maybe because it is not true.

I have SDK'ed all three scripts (I simply had to for a game). Also, I merged it with an time system so the shadows date up their angle and opacity every minut (in game-minut).

I just did not posted it. No-one seems to care :P[/QUOTE]

You're crazy, homie :yes: . I Want it.
I'm really delightful looking those realistic shadows in my game..
but just 1 thing.... when I give comment:"begin shadow" to an event
in its page1, every other pages takes shadows too..:-/

How can I do so that shadow isn't attached anymore to an events another
page?
It shouldn't be so complex..
What comment do you need to use to call the 'sun'?
very nice script, but is it compatible w/ f0tz!baerchen's pixelmovement script?
I don't see see a point in this topic being open as rataime is back and has posted an updated version of this script in the forums go make a post in that topic

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