Guard Events

Guard Events
Version: 2.0
Author: Death10​
*Description: A view range script.

-Create guards.

-Can't see through wall.

-Custom range.

-Can work with Region.

*Intructions:

Spoiler
1. Create guard:
-Create a event with:
*Comment: <guard ss A range x B y C>
-A: Self Switch:
-B: Range's width.

-C: Range's height.
range.PNG

2. Create guard position: (Watch tower,...)
-Create events with:
*Options: Through = On (For the Pathfinding(cozziekuns) script in demo)
*Comment: <guard set D>
-D: Guard Regions (Read below for more informations)
-Create a autorun event with:
*Trigger = Autorun
*Script:

$game_system.guard_info = {
1 => [E, F, G, H]
}

-1: Guard Position (While standing).
-E: Region while guards look down.
-F: Region while guards look left.
-G: Region while guards look right.
-H: Region while guards look up.

-Set Region in Map Editor:

region.PNG
*Script: 
Spoiler
#==============================================================================

# ** Guard Events v2.0

#------------------------------------------------------------------------------

# * A view range script

#------------------------------------------------------------------------------

# * Author: Death

# * http://taotrochoi.com

#==============================================================================

class Guard_Events

def initialize

@data = []

end

def [](pos)

@data[pos] || 0

end

def []=(pos, value)

@data[pos] = value

on_change

end

def on_change

$game_map.need_refresh = true

end

end

class Scene_Title < Scene_Base

alias command_new_game_gv command_new_game

def command_new_game

$guard_events = Guard_Events.new

command_new_game_gv

end

end

class Game_System

attr_accessor :guard_info

alias initialize_gv initialize

def initialize

initialize_gv

@guard_info = {}

end

end

class Game_Event < Game_Character

attr_reader   :guard

attr_accessor :viewrangex

attr_accessor :viewrangey

attr_accessor :guard_switch

attr_accessor :guard_region_down

attr_accessor :guard_region_left

attr_accessor :guard_region_right

attr_accessor :guard_region_up

alias init_public_members_gv init_public_members

def init_public_members

init_public_members_gv

@guard = false

@viewrangex = 0

@viewrangey = 0

@guard_switch = nil

@guard_region_down = nil

@guard_region_left = nil

@guard_region_right = nil

@guard_region_up = nil

end

alias setup_page_guard setup_page

def setup_page(new_page)

setup_page_guard(new_page)

return if @page.nil?

@page.list.each { |cmd|

break unless [108, 408].include?(cmd.code)

case cmd.parameters[0]

when /<guard ss (.) range x (.) y (.)>/i

@guard = true

@guard_switch = $1

@viewrangex = $2.to_i

@viewrangey = $3.to_i

when /<guard set (.)>/i

$game_map.guard_set(@x, @y, $1.to_i)

end

}

end

end

class Scene_Map < Scene_Base

alias update_gv update

def update

update_gv

list = []

for x in 0...$game_map.width

for y in 0...$game_map.height

list += [[x,y,y*$game_map.width+x+1]]

end

end

$game_map.events.each_value do |event|

ev = $game_map.events[event.id]

key = [$game_map.map_id, ev.id, ev.guard_switch]

next if ev.guard == false

pos = []

pos = [pos]*(ev.viewrangex/2*2+1)

list2 = pos.clone

p = pos.clone

rpos = pos.clone

pcount = pos.clone

plist = pos.clone

case ev.direction

when 2

for i2 in -(ev.viewrangex/2)...(ev.viewrangex/2+1)

pos[i2+ev.viewrangex/2] += [i2]

end

for i in 1...ev.viewrangey+1

for i2 in -(ev.viewrangex/2)...(ev.viewrangex/2+1)

pos[i2+ev.viewrangex/2] += [+$game_map.width*i+i2]

end

end

when 4

for i2 in -(ev.viewrangex/2)...(ev.viewrangex/2+1)

pos[i2+ev.viewrangex/2] += [$game_map.width*i2]

end

for i in 1...ev.viewrangey+1

for i2 in -(ev.viewrangex/2)...(ev.viewrangex/2+1)

pos[i2+ev.viewrangex/2] += [-$game_map.width*i2-i]

end

end

when 6

for i2 in -(ev.viewrangex/2)...(ev.viewrangex/2+1)

pos[i2+ev.viewrangex/2] += [$game_map.width*i2]

end

for i in 1...ev.viewrangey+1

for i2 in -(ev.viewrangex/2)...(ev.viewrangex/2+1)

pos[i2+ev.viewrangex/2] += [-$game_map.width*i2+i]

end

end

when 8

for i2 in -(ev.viewrangex/2)...(ev.viewrangex/2+1)

pos[i2+ev.viewrangex/2] += [i2]

end

for i in 1...ev.viewrangey+1

for i2 in -(ev.viewrangex/2)...(ev.viewrangex/2+1)

pos[i2+ev.viewrangex/2] += [-$game_map.width*i+i2]

end

end

end 

pos.each do |a|

for i in 0...pos.size

if pos == a

r = i

end

end

a.each do |b|

for i in 0...a.size

list2[r] += [[i,ev.y*$game_map.width+ev.x+1+b]]

end

end

end

for i2 in 0...list2.size

for i in 0...ev.viewrangey

i = i*(ev.viewrangey+2)

p[i2] += [list2[i2]]

end

end

pos.each do |a|

for i in 0...pos.size

if pos == a

r = i

end

end

for pp in a

e = ev.y*$game_map.width+ev.x+pp+1

list.each do |b|

next if b[2] != e

next if $game_map.check_passage(b[0], b[1], 0x0f)

plist[r] = b

end

end

p[r].each do |c|

next if c[1] != plist[r][2]

pcount[r] = c[0]

end

if pcount[r][0] != nil

for i in 0...pcount[r]

rpos[r] += [a]

end

else

rpos[r] = a

end

end

pos = []

for i in 0...rpos.size

pos += rpos

end

for c in pos

e = ev.y*$game_map.width+ev.x+c+1

p2 = $game_player.y*$game_map.width+$game_player.x+1

if p2 == e

ev.turn_toward_player

$game_self_switches[key] = true

end

end

end

$game_map.events.each_value do |event|

ev = $game_map.events[event.id]

key = [$game_map.map_id, ev.id, ev.guard_switch]

next if ev.guard == false    

next if $game_map.guard_pos(ev.x, ev.y) < 1

ev.guard_region_down = $game_system.guard_info[$game_map.guard_pos(ev.x, ev.y)][0]

ev.guard_region_left = $game_system.guard_info[$game_map.guard_pos(ev.x, ev.y)][1]

ev.guard_region_right = $game_system.guard_info[$game_map.guard_pos(ev.x, ev.y)][2]

ev.guard_region_up = $game_system.guard_info[$game_map.guard_pos(ev.x, ev.y)][3]

case ev.direction

when 2

if $game_player.region_id == ev.guard_region_down

$game_self_switches[key] = true

end

when 4

if $game_player.region_id == ev.guard_region_left

$game_self_switches[key] = true

end

when 6

if $game_player.region_id == ev.guard_region_right

$game_self_switches[key] = true

end

when 8

if $game_player.region_id == ev.guard_region_up

$game_self_switches[key] = true

end

end

end

end

end

class Game_Map

def guard_pos(x, y)

return $guard_events[y*$game_map.width+x+1]

end

def guard_set(x, y, region)

$guard_events[y*$game_map.width+x+1] = region

end

end
*Demo: http://www.mediafire.com/?a2ppxu6gjbu2uh8
*Credit:
- cozziekuns (for the Pathfinding script)
OH YES. This is exactly what I needed for the portion of my game I'm working on this very second. THE TIMING OF IT ALL.

Let's just hope it works out. xD

EDIT: Err, actually got a random error in the demo itself. I got it after trying out the demo for the 6th time (after intentionally being caught in different places). I didn't actually do anything, the queen took one step and the error occured. Something about line 256. I'll see if I can replicate it again.

EDIT2: Yeah, it just happened again without me actually doing anything. Line 256, undefined method '[]' nil:NilClass

EDIT3: Lol, all these edits. Yeah, I just found out it occurs when the girl reaches the first "Guard Set" position before I close the message box since the girl begins moving as the message box at the beginning shows up.
OH YES. This is exactly what I needed for the portion of my game I'm working on this very second. THE TIMING OF IT ALL.

Let's just hope it works out. xD

EDIT: Err, actually got a random error in the demo itself. I got it after trying out the demo for the 6th time (after intentionally being caught in different places). I didn't actually do anything, the queen took one step and the error occured. Something about line 256. I'll see if I can replicate it again.

EDIT2: Yeah, it just happened again without me actually doing anything. Line 256, undefined method '[]' nil:NilClass

EDIT3: Lol, all these edits. Yeah, I just found out it occurs when the girl reaches the first "Guard Set" position before I close the message box since the girl begins moving as the message box at the beginning shows up.
Thanks, I will try to fix that 🙂
Updated a lot in #1 if someone care  🤣

Original thread: https://forums.rpgmakerweb.com/threads/guard-events.3319/