The RPG Maker Resource Kit

RMRK RPG Maker Creation => VX => VX Scripts Database => Topic started by: modern algebra on July 06, 2010, 07:59:34 PM

Title: Fog of War
Post by: modern algebra on July 06, 2010, 07:59:34 PM
Fog of War
Version: 1.0
Author: modern algebra
Date: July 6, 2010

Version History



Description


This script allows you to create "fog of war" on specified maps. This is a concept familiar to RTS games as it is used to hide the movement of enemy units when they are not in range of your own units. This script brings that concept to RMVX, and allows for two layers: a permanent one that is cleared forever once a unit explores the area, and a temporary one that is clear only for as long as it is within range of a unit. You can use either layer or both for any given map.

This script also allows for other events (not just the player) to have vision, and will hide other events not within direct vision of a unit unless you specify that event to show anyway.

Features


Screenshots

(http://img716.imageshack.us/img716/5136/fowscreenshot.png)

Instructions

Please see the header of the script for instructions.

Script


Script is very long. Please retrieve it either from the text document (http://rmrk.net/index.php?action=dlattach;topic=39271.0;attach=20868) or demo (http://rmrk.net/index.php?action=dlattach;topic=39271.0;attach=20867).

Bug Fixes

Spoiler for Exit Site Uncleared:
If you re-enter a fog of war map at a different place than you exited, the sight radius will still be cleared at the site you exited. This patch fixes that:

Code: [Select]
#==============================================================================
#  Clear Temporary Layer Patch
#    Bug fix for Fog of War 1.0
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Description:
#
#    There was a bug in the previous version where if you re-entered a map with
#   fog of war at a different location than you exited, the sight radius from
#   when the player exited would still be there. This patch fixes that bug.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Instructions:
#
#    Paste this into its own slot below Fog of War but still above Main.
#==============================================================================

class Tilemap_FogofWar
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Object Initialization
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias ma_hos_initz_fow_6yh2 initialize
  def initialize (*args)
    # Eliminate the top layer
    $game_system.paint_fog ($game_map.map_id, 0, 2816)
    $game_player.update_fow
    ma_hos_initz_fow_6yh2 (*args) # Run original method
  end
end

Credit



Thanks


Support


Please post in this topic at RMRK if you require support, no matter when this topic was last posted in. Do not PM me.

Known Compatibility Issues

No currently known compatibility issues, but it may interact strangely with some fog scripts.

Demo


See attached (http://rmrk.net/index.php?action=dlattach;topic=39271.0;attach=20867)

Graphics

I have attached a few fog of war autotiles as samples and free for use. "FogOfWar" (http://rmrk.net/index.php?action=dlattach;topic=39271.0;attach=20869) and "FogOfWar2" (http://rmrk.net/index.php?action=dlattach;topic=39271.0;attach=20870) I made myself, while "FogOfWar Wachunga" (http://rmrk.net/index.php?action=dlattach;topic=39271.0;attach=20871) is a resized version of the sample autotile Wachunga used in his RMXP script. All FogOfWar autotiles must be saved in System.

Also, the script has "FogOfWar2" built into it, and it will use that autotile if you have no fog of war autotiles in the System folder.

Author's Notes


I remember using Wachunga's Fog of War script when I first started using RMXP. It's a nice idea, so I figured I'd write my own RMVX one.
Title: Re: Fog of War
Post by: cozziekuns on July 06, 2010, 08:27:35 PM
Holy cow... You even made your own default auto-tile through script...

I would never go so far for my scripts :P Great job. Could someone be working on an RTS Script?
Title: Re: Fog of War
Post by: Mishka on July 06, 2010, 11:20:35 PM
It's also useful for TBS games in the vein of Fire Emblem, etc.

Awe-inspiring step forward yet again, Modern.
Title: Re: Fog of War
Post by: cozziekuns on July 07, 2010, 12:05:12 AM
It's also useful for TBS games in the vein of Fire Emblem, etc.

Awe-inspiring step forward yet again, Modern.

Speaking of which, do your forsee any compatability problems with any Tactical Battle System?
Title: Re: Fog of War
Post by: Mishka on July 07, 2010, 12:29:07 AM
Speaking of which, do your forsee any compatability problems with any Tactical Battle System?
Not with GTBS, but I'll have to take a look-see.
Title: Re: Fog of War
Post by: modern algebra on July 07, 2010, 01:07:27 AM
I've never used any TBS, so I couldn't say for sure. It would depend on if it uses or inherits from Spriteset_Map, and you could only set vision for player units if it uses events to denote them. Assuming those two factors are present, it would probably work.

And, definitely not working on an RTS script. That could only have terrible application to RM :P
Title: Re: Fog of War
Post by: Mishka on July 07, 2010, 01:21:53 AM
You are a genius if there ever was one. You could probably make a cleaner version than the GTBS (since GubiD used a large portion the XP version of his script, which made customisation kind of tedious in some ways).


I was wondering if you were interested in taking on a dynamic random dungeon generation script, if anything (there is one out already, but it has some problems). A Roguelike script, if you will ;)
Title: Re: Fog of War
Post by: 104251 on July 15, 2010, 07:51:47 PM
Hmm i got an interesting idea. Don't know if something like this has already been made... (sorry if such thing already exists)
Is it hard to make two temporary 'fog of war' layers with possibility to set two independent sight radiuses? And i guess there must be a way to check if the player is in the 'darkness' or not.
I'm just still dreaming of realtime lighting&stealth system)
I've already seen a stealth script, but it felt incomplete without hiding-in-the-shadows routine)
I also tried to make stealth levels with parallax shadows. But the amount of eventing there was horrifying - to check all tiles on the map, if player is lit enough, whether or not the guards see him etc etc... And the main problem was that no matter how pretty hand-drawn shadows are, they were static.

So now its possible to make light sources, walking guards with torches and stuff like that)
Title: Re: Fog of War
Post by: Mr_Wiggles on July 15, 2010, 11:56:26 PM
That does sound like a really neat idea and a great idea that could probably use this script to the fullest of its capabilities. This does sound like it would be very hard to do with out any scripts.

oh and modern this is a really cool script you never seize to amazing any one with your scripts, and im sure like me every one is always looking foreword to that next one.
Title: Re: Fog of War
Post by: 104251 on July 17, 2010, 12:34:44 PM
So what do you say, MA?
How much of the script needs to be changed?  Two temporary layers, two radius, transparency and circular/diamond shape settings.


--edit--
like that)
Spoiler for:
(http://img594.imageshack.us/img594/9530/exampleo.jpg)
Title: Re: Fog of War
Post by: modern algebra on July 18, 2010, 02:32:56 PM
It wouldn't be hard to do; if I get some time in the future I might do it, but I don't think it would look like that map - it looks like the parts with the fire are tinted. Are you actually wanting the fire layer to use a tinted autotile over the parts that it lights?
Title: Re: Fog of War
Post by: 104251 on July 18, 2010, 03:21:47 PM
No-no, i just photoshop'd it like that) The glow effect around the light sources can be done separately with appropriate charsets or pictures, no need to do that with script.
Actually it can be enough with only one shadow layer, but with two layers shading will be smooth and not so blocky. Also i think that it can be used to determine the intensity of character lighting - is he fully, partially lit or in total darkness.

Gosh, if you'll manage to achieve that, it will be real masterpiece!
Title: Re: Fog of War
Post by: cippy12486 on April 14, 2011, 11:05:30 PM
I really hope I don't get crunched into a little ball for necro-posting......

Anyway......I love this Fog of War system.  I just can't get it to work with GTBS v1.5.1.4 (The most current)

If there's anyway you can find it in you heart to make a patch, or at least point me in the right direction, I would be most grateful......Thank You!





Title: Re: Fog of War
Post by: modern algebra on April 14, 2011, 11:10:53 PM
I am in the middle of exams right now so cannot do anything, but remind me after April and I will see if I'm not busy then.
Title: Re: Fog of War
Post by: cippy12486 on April 14, 2011, 11:44:11 PM
Roger that.....

I'm getting married in a couple of weeks, so I'll probably bug you sometime in late May.....


Thank You!
Title: Re: Fog of War
Post by: modern algebra on April 14, 2011, 11:51:38 PM
Congratulations! Three cheers for marriage!

Just three though :P I'll cheer more for it when I'm not bitter and single.
Title: Re: Fog of War
Post by: cippy12486 on April 15, 2011, 12:39:01 AM
HAHAHAHA.....My NOOB skills rock......

I copied the method used to make Wortona's Light effects, and multiple fogs Patches for GTBS.....


This patch will allow the Fog of War to work with GTBS......All I need now is a way to make the player view effect work......because GTBS is NOT run by events, but by sprite_sets....and it's really confusing... :(


At least this cuts down on future work.....and I'll try and find a way around this in the meantime.....

The power of NOOB runs strong within me!!!



Code: [Select]
#===============================================================================
# This Script is made specfically for usage with GTBS version 1.5.1.4 by GubiD.
#===============================================================================

#--------------------------------------------------------------
# Fog of War (modern_algebra) Patch
#--------------------------------------------------------------


if defined? (Tilemap_FogofWar)
class Spriteset_Battle_GTBS
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Create Tilemap
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias modrnb_fow_crttilmp_5tf2 create_tilemap
  def create_tilemap (*args)
    modrnb_fow_crttilmp_5tf2 (*args) # Run Original Method
    # Create viewport between tile and picture layers
    @fow_viewport = Viewport.new (0, 0, Graphics.width, Graphics.height)
    @fow_viewport.z = 40
    data = $game_system.fow_data
    $game_system.fow_reset_data ($game_map.map_id) if @tilemap.map_data.xsize != data.xsize || @tilemap.map_data.ysize != data.ysize
    @fow_tilemap = Tilemap_FogofWar.new(@fow_viewport)
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Dispose of Tilemap
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias malgbr_fow_dispmap_7yh2 dispose_tilemap
  def dispose_tilemap (*args)
    malgbr_fow_dispmap_7yh2 (*args) # Run Original Method
    for i in 0...9 do @fow_tilemap.bitmaps[i].dispose end
    @fow_tilemap.dispose
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Update Tilemap
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias monaba_fogwar_tilmup_2fu8 update_tilemap
  def update_tilemap (*args)
    monaba_fogwar_tilmup_2fu8 (*args) # Run Original Map
    @fow_tilemap.ox = @tilemap.ox
    @fow_tilemap.oy = @tilemap.oy
    @fow_viewport.visible = $game_map.fow_active
    @fow_tilemap.update
  end
end
end
Title: Re: Fog of War
Post by: 104251 on April 15, 2011, 10:13:37 AM
Oh, its not dead? Nice!
Title: Re: Fog of War
Post by: cippy12486 on June 08, 2011, 12:29:34 AM
I have come back from the awesomeness that is the Bahamas!!!!!!   8)

Anyway....back to reality.......

Ok....so I figured out how to show the Fog of War (FOW) on GTBS battles with the little scriplet I conjured up.  The problem now becomes integrating FOW with the players on the GTBS map.  Actors are spritesets instead of events......which makes the problem harder (at least for me it does).....

I don't think we have to worry about the enemies too much, because you probably don't want them to show if the players haven't found them.....I tried fiddling around with both your script and GTBS, and failed miserably....


I humbly ask for your help modern_algebra.....your status in the rpgmaker world is legendary.......any help would be most appreciated!




Title: Re: Fog of War
Post by: twist3d nova on August 02, 2011, 07:14:51 AM
So, if the fog creates lag on the map, how do i fix this?
Title: Re: Fog of War
Post by: modern algebra on August 02, 2011, 12:23:38 PM
Well, does the fog create lag on the map? You could grab an anti-lag script to help prevent lag from other sources, but to reduce lag from the FOW I would need to take a look; there probably are ways I could make it more efficient than it is. But why the hypothetical? Is it causing lag or not?

Sorry cippy, I forgot all about you. If you still want it, send the demo you have to me and I will take a look
Title: Re: Fog of War
Post by: twist3d nova on August 02, 2011, 07:06:04 PM
EDIT:  If you experience lag using show_fow (0) change it to show_fow (1) and the script settings for opacity should be Permanant layer 255 temp layer 55 or whatever desired.
Title: Re: Fog of War
Post by: cippy12486 on August 11, 2011, 07:58:33 AM
No worries modern.....I figured out how to implement it a few weeks ago......

I actually have taught myself ruby(With GubiD's awesome tutorial series) and have been scripting for a little bit....I usually go by Nelderson, not sure why I chose cippy for this site.....

Anyway......here it is:

Crap it exceeds the maximum characters.......just download this demo, it has it in there......

 http://www.mediafire.com/?vqpas1a9uxruf2y

BTW: Your script does cause some lag, especially when dashing..... ::)
Title: Re: Fog of War
Post by: Sophist on August 11, 2011, 08:06:11 AM
Have you thought of a way to create complete encapsulating darkness until explored, and in the wake leaves the 'post-shadow' that is the current default?
Title: Re: Fog of War
Post by: modern algebra on August 11, 2011, 12:04:54 PM
I'll look into ways of reducing the lag.

@Anski - I might be misunderstanding the question, but I think that's how it is now.
Title: Re: Fog of War
Post by: Sophist on August 11, 2011, 07:38:33 PM
I meant like, that you can't see what's ahead at all. No tile set, no events, nothing, and when you explore it and leave it, it's left with that gray area with no events visible.
Title: Re: Fog of War
Post by: cozziekuns on August 11, 2011, 08:54:02 PM
You can do that. Just fill the Fog of War tile with black and set the sight to 1. Or am I missing something?
Title: Re: Fog of War
Post by: modern algebra on August 11, 2011, 10:13:27 PM
Yeah, that's in the script I think - the screenshot might be a little confusing since I had cleared all the walkable tiles, but the white stuff is everything within visibility, the grey stuff is where you have explored but is no longer visible (hides events too) and the black stuff along the borders would cover the whole map and only go away once you've brought it into visibility.
Title: Re: Fog of War
Post by: Heartofshadow on August 21, 2011, 07:01:45 PM
Hey modern algebra, I just started using this script and I love it.

I did run into what appears to be a slight bug, however, when transferring maps. Using the scripted auto-tile, and the temporary vision type (where you have a sight radius and nothing is "permanently" revealed), if you hit a transfer player event and then return to that same map at any given time, the spot where you left is still revealed.

So, example, exit a map on the right side via a Transfer Player event and then come back to that map. The right side where you hit the event will still be revealed as if the player were standing there.

Did I explain that well enough? If not, I can add a screenshot for you. Keep up the awesome work!
Title: Re: Fog of War
Post by: Mushu on August 21, 2011, 07:36:11 PM
I'm using it as a torch type thing for a maze, is there any way I can set the bright area to 3x3 square(non-diamond) and then switch back and forth between that and basic 2-square-dist diamond at random?
                     X
XXX             XXX
XXX  <-->  XXXXX 
XXX             XXX
                     X
btw heart, I copied and pasted script and am only initiating with event and I don't have that prob :P
it only does it for me for like a split second then moves back to me
Title: Re: Fog of War
Post by: modern algebra on August 21, 2011, 08:04:41 PM
@Scalinger - yes, you can use the following code in a script call:

Code: [Select]
$game_player.create_vision_field (sight, circular?)

Ie. the following:

Code: [Select]
$game_player.create_vision_field (3, true)
would create the 3 radius circle while:
Code: [Select]
$game_player.create_vision_field (2, false)
would change it to 2 radius diamond.

I don't know what you mean by at random - you will need to event it so it's not totally random, but you could add some randomness through eventing.

@HoS - I see what you mean. I will whip up a fix.
Title: Re: Fog of War
Post by: Mushu on August 21, 2011, 08:25:56 PM
sry, it's all good now lol
Title: Re: Fog of War
Post by: modern algebra on August 21, 2011, 08:28:40 PM
@HoS:

The fix:

Code: [Select]
#==============================================================================
#  Clear Temporary Layer Patch
#    Bug fix for Fog of War 1.0
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Description:
#
#    There was a bug in the previous version where if you re-entered a map with
#   fog of war at a different location than you exited, the sight radius from
#   when the player exited would still be there. This patch fixes that bug.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Instructions:
#
#    Paste this into its own slot below Fog of War but still above Main.
#==============================================================================

class Tilemap_FogofWar
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Object Initialization
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias ma_hos_initz_fow_6yh2 initialize
  def initialize (*args)
    # Eliminate the top layer
    $game_system.paint_fog ($game_map.map_id, 0, 2816)
    $game_player.update_fow
    ma_hos_initz_fow_6yh2 (*args) # Run original method
  end
end
Title: Re: Fog of War
Post by: Mushu on August 21, 2011, 09:13:47 PM
I spoke too soon.
This is what I tried..
var 16 and 17 are random, when they are equal it makes size1 diamond, when not equal, a circle
when your in the dark room it's suposed to active the circle and daimond pattern switch.

On the map I posted, the left side is like a lit room and the right side you use lantern but the problem is
when your in right side it stays to one shadow type then splotches it instead of doing a
random shadow pattern switch between circle and diamond.
http://dl.dropbox.com/u/33252047/Project2.zip
Title: Re: Fog of War
Post by: Ahnez67 on August 21, 2011, 10:11:06 PM
About the lag.

The script runs 60 times in a second, right?

Isn't there a way to make it run less times in a second? Maybe 40~50 times per second... ?

Title: Re: Fog of War
Post by: Mushu on August 21, 2011, 10:44:03 PM
download this kgc library
the 'maplightening' decreases the number of events being processed on a map therefor decreasing lag
I have mine set to 65 events
http://www.mediafire.com/?jxouo3iebco
Title: Re: Fog of War
Post by: Heartofshadow on August 22, 2011, 12:15:40 AM
Thanks modern algebra!

Works like a charm now.
Title: Re: Fog of War
Post by: teknoarcanist on October 09, 2011, 01:17:27 AM
Been fiddling with the script.  Great work.  Being able to swap your own fog tileset in is a godsend.

Is there a quick way to adjust the player's sight range in-game?  Say I have a player touch a magical rock, and it increases the sight range to 5, from the default 3?

Edit: if not, there's a very easy workaround by having various events with fog sight above the character at all times.  So if the player's "fog sight" skill is 10, just turn the "10 fog sight" event on and have it follow above the player at all times.
Title: Re: Fog of War
Post by: modern algebra on October 09, 2011, 01:35:38 AM
THat would work, but there is a way to change it in game too:

Quote from: Instruction
Code: [Select]
#      2) Use the following code in a script command:
#        set_unit_vision (event_id, radius, circular?)
#          event_id  : ID of the event you want to change. If 0, sets the
#            vision of the current event; if -1, sets the player vision.
#          radius    : same as at line 71
#          circular? : same as at line 73

Sothe following in a script call should do it:

Code: [Select]
set_unit_vision (-1, 5)
Title: Re: Fog of War
Post by: teknoarcanist on October 09, 2011, 01:49:57 AM
Ah, see, this is why they should have taught reading when I was in school :P

Thanks lol
Title: Re: Fog of War
Post by: amerk on April 06, 2012, 03:50:18 PM
Hey MA, not sure if you are still working with this or not, but a couple of questions. Is there a way to keep both the permanent and temporary layers on, but still make all current graphic events visible without having to code it with

\fow_vision[3, true]

Basically, I want to use the temporary fog layer more for decorative, as in, I can still see my event moving, and when it's in my line of vision it's clear, but when it's in the temporary layer it's a bit obscure but still visible, and of course when it's in a place I haven't explored yet it's hidden.

I imagine it's a minor adjustment in how this script reveals events, but not sure how that would be changed.

My second question was if you had plans porting this over to VX-Ace, unless you already had and I'm missing it? Seeing as it's been a benefit with the last two makers, I think this would be great in Ace.

Thanks!
Title: Re: Fog of War
Post by: modern algebra on April 06, 2012, 04:10:07 PM
Well, you can put a comment on the first line of an event page which has the following code:
Code: [Select]
\ignore_fow

If you really wanted to do it for every event, then you could also go to the following line in the script:

Code: [Select]
      @ignore_fow = !comment[/\\IGNORE_FOW/i].nil?

And change it to:

Code: [Select]
      @ignore_fow = true
Title: Re: Fog of War
Post by: Shadowyugi on May 18, 2012, 06:16:17 AM
You, my friend, are a genius!
Title: Re: Fog of War
Post by: le noob on December 09, 2012, 10:41:49 PM
can someone help i keep getting this error

Script 'Fog of War' line 175: SyntaxError occurred.

unexpected ',', expecting ')'
         ma_fow_systm_2fk9 (filename, *args)
Title: Re: Fog of War
Post by: modern algebra on December 10, 2012, 09:18:55 PM
This script is for VX. You are trying to insert it in to RMVX Ace.
Title: Re: Fog of War
Post by: Strak on January 16, 2013, 06:52:53 PM
I'm probably just blind, but is there a way to register whether or not another event is within your sight range? If so, I'm not seeing it. I would very much like to use this as a stealth system. It's perfect for simulating dim lighting combined with distance to your target. I realized recently that a game based around assassination should probably include at least one stealth feature.

EDIT: Also, is it possible to simply change the opacity of other events that are out of your sight range, or do they have to turn completely invisible?
Title: Re: Fog of War
Post by: modern algebra on January 16, 2013, 10:57:15 PM
If you have the x and y coordinates of the events, then you can check:

Code: [Select]
$game_system.square_visible?(x, y)

As for whether you can just reduce the opacity instead of making it fully transparent, the answer is no, that's not a feature built into this script. You can, however, have events ignore the fog of war altogether by placing the \ignore_fow code into the first comment of the event. That won't reduce the opacity, but it will make them visible at least under the temporary fog.
Title: Re: Fog of War
Post by: Strak on January 17, 2013, 12:29:55 AM
Oh well. Thanks anyway, this does help a lot.