Advertisement
Velvetisis

Velvet Isis' Vision Script

Oct 18th, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.16 KB | None | 0 0
  1. # Velvet Isis' Vision Script
  2. #-------------------------------------------------------------------------------
  3. # This is not entirely my own work. Parts are based on the following scripts:
  4. #   Tracer stealth system - Prof. Meow Meow Converted to VXA by LiTTleDRAgo
  5. #     Jet10985's stealth script (which was inspired by Touchfuzzy)
  6. #     The Infamous Bon Bon's Direction Based Line of Sight Events
  7. # This script takes bits from each of these and merges them into a simple but
  8. # functional Line of Sight Script
  9. #-------------------------------------------------------------------------------
  10.  
  11. #-------------------------------------------------------------------------------
  12. # Directions:  To make an event able to 'see' use the event command 'Script...'
  13. #   under tab 3 of the Event Commands and type in
  14. #
  15. #   sentinel
  16. #
  17. #   That's it, no ' no " just the word sentinel, all lower case.  They must also
  18. #   be set to parallel process.
  19. #  
  20. #   Then for what they do once they see the Player, have a new page with the
  21. #   Condition: self-switch A is on.  The default is A, but you can change it
  22. #   below. If you want the event to return to normal if the Player exits it's
  23. #   field of vision, make sure to put the sentinel script on this page as well.
  24. #
  25. #-------------------------------------------------------------------------------
  26.  
  27.  
  28. #-------------------------------------------------------------------------------
  29. #  Things you can edit
  30. #-------------------------------------------------------------------------------
  31.  
  32. module VIS
  33.     CAMO_THRESHOLD = 50
  34.       #A character needs this much Camo to NOT be seen.
  35.             #You can either do this with terrain tags, a variable, or sneaking.
  36.  
  37.     CAMO_VARIABLE = 7  
  38.       #This is which game variable controls Camo.  If, for instance, you wanted
  39.       #an event to add or take away Camo you would adjust the variable listed
  40.       #here.
  41.      
  42.     SNEAK_CAMO = 50
  43.       #This is how much Camo sneaking provides.  Sneaking is done by holding
  44.       #down the 'A' button, which is actually the 'SHIFT' button.
  45.  
  46.     FOV_WIDTH = 2  
  47.       #This is how many squares to the left AND right an event can 'see'.
  48.       #This does not take into account the path in front of the event
  49.             #itself, so for instance, using 2 gives a total of 5 squares
  50.             #left to right. (2 1 0 1 2)
  51.     FOV_LENGTH = 5 
  52.       #This is how many squares in front of itself an event can 'see'.
  53.       #Nothing weird here, just the straight number.
  54.    
  55.     FOUND_SELFSWITCH = "A"
  56.       #This is which self switch becomes true when the Player is spotted.
  57.       #Be sure to keep the " " around the letter.
  58.  
  59.     TERRAIN_CAMOS = { 1 => 60}
  60.       #These are how much Camo a Terrain Tag gives when standing on it.
  61.       #These go across all maps.  Use the format: terrain_id => camo
  62.       #So right now terrain tag 1 has a Camo value of 60.
  63.    
  64.     TERRAIN_CAMOS.default = 0
  65.       #This is the default camo of any undefined terrain above.    
  66. end
  67.  
  68. #-------------------------------------------------------------------------------
  69. #   You probably shouldn't edit things below here, but I'm not gonna judge.
  70. #   I mean, that's how I learned to do things, was by putzin' around with
  71. #   other people's scripts.  I mean if you change stuff the script might not
  72. #   work, but you live and you learn, and that's how you get better
  73. #-------------------------------------------------------------------------------
  74.  
  75.  
  76. #-------------------------------------------------------------------------------
  77. #   Terrain Tag Camoflage
  78. #-------------------------------------------------------------------------------
  79. class Game_Map  
  80.   def xy_camo(x, y)
  81.     ter = terrain_tag(x, y)
  82.     ter_c = terrain_camos(ter)
  83.     ter_c
  84.   end
  85.  
  86.   def terrain_camos(ter)
  87.     (@map_terrain_camos ||= {})[@map_id] ||= VIS::TERRAIN_CAMOS
  88.     @map_terrain_camos[@map_id][ter]
  89.   end
  90. end
  91.  
  92.  
  93. #-------------------------------------------------------------------------------
  94. #  Player Camoflage
  95. #-------------------------------------------------------------------------------
  96. class Game_Player
  97.  
  98.   alias altered_speed real_move_speed
  99.     def real_move_speed
  100.       altered_speed
  101.       @move_speed - (dash? ? 1 : 0)
  102.     end
  103.  
  104.   def camouflage
  105.     snk = 0
  106.       if Input.press?(:A)
  107.         snk = VIS::SNEAK_CAMO
  108.       end
  109.     camo = 0
  110.     camo += snk
  111.     camo += $game_map.xy_camo(self.x, self.y)
  112.     camo += $game_variables[VIS::CAMO_VARIABLE]
  113.   end
  114. end
  115.  
  116. #-------------------------------------------------------------------------------
  117. #  Actual Vision Scripting
  118. #-------------------------------------------------------------------------------
  119. class Game_Character
  120.  
  121.   #-------------------------#
  122.   # Local Variables         #
  123.   #-------------------------#
  124.     attr_accessor :thresh
  125.    
  126.   #-------------------------#
  127.   # Initialize              #
  128.   #-------------------------#
  129.     alias vision_initialize initialize unless $@
  130.       def initialize
  131.         vision_initialize
  132.         @thresh = VIS::CAMO_THRESHOLD
  133.       end
  134.  
  135.   #-------------------------------#
  136.   # Define Field of Vision        #
  137.   #-------------------------------#
  138.   def fov? # This is the static Field of Vision
  139.       w = VIS::FOV_WIDTH
  140.       l = VIS::FOV_LENGTH
  141.       ex = self.x
  142.       ey = self.y
  143.       px = $game_player.x      
  144.       py = $game_player.y
  145.       if @direction == 2 && ey < py ||
  146.         @direction == 8 && ey > py
  147.         if (ey - py).abs <= l && (ex - px).abs <= w
  148.           return true
  149.         end
  150.       end
  151.       if @direction == 4 && ex > px ||
  152.         @direction == 6 && ex < px
  153.         if (ey - py).abs <= w && (ex - px).abs <= l
  154.           return true
  155.         end
  156.       end
  157.     end
  158.  
  159.   #-------------------------------#
  160.   # Camo Threshold                #
  161.   #-------------------------------#
  162.     def camo_threshold(x)
  163.       for event in $game_map.events.values
  164.       @thresh = x
  165.       end
  166.     end
  167. #------------------------------------------------------------------------------  
  168.  
  169.  
  170.   #-------------------------------#
  171.   # Determine if Player is seen   #
  172.   #-------------------------------#
  173.   def viewable?
  174.     viewable = true
  175.     ex = self.x
  176.     ey = self.y
  177.     px = $game_player.x
  178.     py = $game_player.y
  179.     dx = (ex - px).abs
  180.     dy = (ey - py).abs
  181.     m = ((ey - py).to_f / (ex - px).to_f)
  182.        
  183.   #----------------------------------#
  184.   # Player in the Field of Vision?   #
  185.   #----------------------------------#
  186.     if fov?
  187.   #-----------------------------#
  188.   # Check Camouflage            #
  189.   #-----------------------------#
  190.       camo = $game_player.camouflage
  191.       if camo < @thresh
  192.   #-----------------------------#
  193.   # Check for any Obstructions  #
  194.   #-----------------------------#
  195.         if (dx > dy)
  196.           dx.times { |x|
  197.             case
  198.             when (ex > px)
  199.               viewable = false if !$game_map.passable?(
  200.                 (ex - x),
  201.                 ((-m * (x)) + ey - 0.1).round,
  202.                 @direction) ||
  203.               $game_map.events_xy_nt(
  204.                 (ex - x - 1),
  205.                 ((-m * (x + 1)) + ey - 0.1).round).any?
  206.             when (ex < px)
  207.               viewable = false if !$game_map.passable?(
  208.                 (ex + x),
  209.                 ((m * (x)) + ey - 0.1).round,
  210.                 @direction) ||
  211.               $game_map.events_xy_nt(
  212.                 (ex + x + 1),
  213.                 ((m * (x + 1)) + ey - 0.1).round).any?
  214.             end}
  215.         elsif (dx < dy)
  216.           (dy).times { |y|
  217.             case
  218.             when (ey > py)
  219.               viewable = false if !$game_map.passable?(
  220.                 (((y) / -m) + ex - 0.1).round,
  221.                 (ey - y),
  222.                 @direction) ||
  223.               $game_map.events_xy_nt(
  224.                 (((y + 1) / -m) + ex - 0.1).round,
  225.                 (ey - y - 1)).any?
  226.             when (ey < py)
  227.               viewable = false if !$game_map.passable?(
  228.                 (((y) / m) + ex - 0.1).round,
  229.                 (ey + y),
  230.                 @direction) ||
  231.               $game_map.events_xy_nt(
  232.                 (((y + 1) / m) + ex - 0.1).round,
  233.                 (ey + y + 1)).any?
  234.             end}
  235.         elsif (dx = dy)
  236.           dx.times { |x|
  237.             case
  238.             when (ex > px)
  239.               viewable = false if !$game_map.passable?(
  240.                 (ex - x),
  241.                 ((-m * (x)) + ey - 0.1).round,
  242.                 @direction) ||
  243.               $game_map.events_xy_nt(
  244.                 (ex - x - 1),
  245.                 ((-m * (x + 1)) + ey - 0.1).round).any?
  246.             when (ex < px)
  247.               viewable = false if !$game_map.passable?(
  248.                 (ex + x),
  249.                 ((m * (x)) + ey - 0.1).round,
  250.                 @direction) ||
  251.               $game_map.events_xy_nt(
  252.                 (ex + x + 1),
  253.                 ((m * (x + 1)) + ey - 0.1).round).any?
  254.             end}
  255.         end
  256.   #---------------------------------------#
  257.   # Check for any Obstructions if dy == 0 #
  258.   #---------------------------------------#
  259.         if dy == 0
  260.           dx.times { |x|
  261.             case
  262.             when (ex < px)
  263.               viewable = false if !$game_map.passable?((ex + x), ey, @direction) ||
  264.               $game_map.events_xy_nt((ex + x + 1), ey).any?
  265.             when (ex > px)
  266.               viewable = false if !$game_map.passable?((ex - x), ey, @direction) ||
  267.               $game_map.events_xy_nt((ex - x - 1), ey).any?
  268.             end}
  269.         end
  270.   #---------------------------------------#
  271.   # Check for any Obstructions if dx == 0 #
  272.   #---------------------------------------#
  273.         if dx == 0
  274.           dy.times { |y|
  275.             case
  276.             when (ey < py)
  277.               viewable = false if !$game_map.passable?(ex, (ey + y), @direction) ||
  278.               $game_map.events_xy_nt(ex, (ey + y + 1)).any?
  279.             when (ey > py)
  280.               viewable = false if !$game_map.passable?(ex, (ey - y), @direction) ||
  281.               $game_map.events_xy_nt(ex, (ey - y - 1)).any?
  282.             end}
  283.         end
  284.   #--------------------------#
  285.   # * Returns false if !fov? #
  286.   #--------------------------#
  287.       else
  288.       viewable = false
  289.       end
  290.     else
  291.       viewable = false
  292.     end
  293.     return viewable
  294.   end  
  295.      
  296.   def sentinel
  297.     if self.viewable?
  298.       set_self_switch(VIS::FOUND_SELFSWITCH, true)
  299.     else
  300.       set_self_switch(VIS::FOUND_SELFSWITCH, false)
  301.     end
  302.   end
  303.  
  304.   #-----------------------------#
  305.   # Get Self Switch             #
  306.   #-----------------------------#
  307.     def get_self_switch(switch)
  308.       key = [@map_id, @id, switch]
  309.       return $game_self_switches[key]
  310.     end
  311.   #-----------------------------#
  312.   # Set Self Switch             #
  313.   #-----------------------------#
  314.     def set_self_switch(switch,true_false)
  315.       key = [@map_id, @id, switch]
  316.       $game_self_switches[key] = true_false
  317.       $game_map.need_refresh = true
  318.     end  
  319.  
  320. end
  321.  
  322. class Game_Interpreter
  323.  
  324.   def camouflage
  325.     get_character(-1).camouflage
  326.   end
  327.  
  328.   def fov?
  329.     get_character(0).fov?
  330.   end
  331.  
  332.   def camo_threshold(x)
  333.     get_character(0).camo_threshold(x)
  334.   end
  335.  
  336.   def viewable?
  337.     get_character(0).viewable?
  338.   end
  339.  
  340.   def sentinel
  341.     get_character(0).sentinel
  342.   end
  343.  
  344. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement