MODULE: Underwater v1.1

Started by Kweepa, Wed 19/08/2009 06:13:55

Previous topic - Next topic

Kweepa

Quote from: GarageGothic on Sat 22/08/2009 18:46:52
Have you tried calling SetBackgroundFrame(0) in the player_enters_room event (unless the module already does this)?

It does. :=
See the repeatedly_execute function below:
Code: ags

// Underwater module script

__Underwater Underwater;
export Underwater;

bool gEnabled = false;

float tt = 0.0;
float t = 0.0;

float a[5], b[5], c[5];

function __Underwater::Enable()
{
  float scalar = 2000.0;
  int i = 0;
  while (i < 5)
  {
    a[i] = IntToFloat(Random(2000)*(2*Random(1)-1))/scalar;
    b[i] = IntToFloat(Random(2000)*(2*Random(1)-1))/scalar;
    c[i] = IntToFloat(Random(2000)*(2*Random(1)-1))/2000.0;
    i++;
  }
  gEnabled = true;
}

function __Underwater::Disable()
{
  gEnabled = false;
}

float get_gradient_x(int xi, int yi)
{
  float x = IntToFloat(xi)/160.0 - 1.0;
  float y = IntToFloat(yi)/160.0 - 1.0;
  
  float dx = 0.0;
  int i = 0;
  while (i < 5)
  {
    dx += a[i]*t*Maths.Cos(a[i]*t*x + b[i]*t*y + c[i]);
    i++;
  }

  return dx;
}

float get_gradient_y(int xi, int yi)
{
  float x = IntToFloat(xi)/160.0 - 1.0;
  float y = IntToFloat(yi)/160.0 - 1.0;
  
  float dy = 0.0;
  int i = 0;
  while (i < 5)
  {
    dy += b[i]*t*Maths.Cos(a[i]*t*x + b[i]*t*y + c[i]);
    i++;
  }

  return dy;
}

function offset_a_block(DrawingSurface *surf, int x, int y, int w)
{
  int xoff = FloatToInt(1.0*get_gradient_x(x + w/2, y + w/2));
  int yoff = FloatToInt(1.0*get_gradient_y(x + w/2, y + w/2));
  DynamicSprite *ds = DynamicSprite.CreateFromBackground(1, x, y, w, w);
  #ifver 3.00
  surf.DrawImage(x - xoff, y - xoff, ds.Graphic);
  #endif
  #ifnver 3.00
  RawDrawImage(x - xoff, y - yoff, ds.Graphic);
  #endif
}

function repeatedly_execute()
{
  if (gEnabled)
  {
    tt = tt + 0.01;
    t = 2.0*Maths.Pi*Maths.Sin(tt);

    SetBackgroundFrame(0);
    // repeatedly grab a random chunk from bg 1 and paste it to bg 0, slightly offset
    #ifver 3.00
    DrawingSurface *surf = Room.GetDrawingSurfaceForBackground(0);
    #endif
    int i = 0;
    while (i < 128)
    {
      i++;
      int w = Room.Width/32;
      int x = Random(Room.Width - 1 - w);
      int y = Random(Room.Height - 1 - w);

      offset_a_block(surf, x, y, w);
    }

    // draw some more around the player
    ViewFrame *frame = Game.GetViewFrame(player.View, player.Loop, player.Frame);
    int graphic = frame.Graphic;
    int height = FloatToInt(IntToFloat(Game.SpriteHeight[graphic]*player.Scaling)/100.0);
    int width  = FloatToInt(IntToFloat( Game.SpriteWidth[graphic]*player.Scaling)/100.0);
    int left = player.x - width/2;
    int top = player.y - height - player.z;

    i = 0;
    while (i < 32)
    {
      i++;
      int w = Room.Width/64;
      int x = left - w/2 + Random(width - 1);
      int y = top - w/2 + Random(height - 1);
      if (x < 0) x = 0;
      else if (x >= Room.Width - w) x = Room.Width - 1 - w;
      if (y < 0) y = 0;
      else if (y >= Room.Height - w) y = Room.Height - 1 - w;
      
      offset_a_block(surf, x, y, w);
    }
    #ifver 3.00
    surf.Release();
    #endif
  }
}
Still waiting for Purity of the Surf II

TerranRich

My God, Steve, you have a ton of modules, all of them very useful and amazingly well-done! I gotta see which ones I can use. ;D
Status: Trying to come up with some ideas...

Kweepa

I've got a way to go to catch up with SSH or monkey_05_06!
Still waiting for Purity of the Surf II

monkey0506

To be fair though Steve your modules tend to be way cooler than mine. Most of mine are based on other people's scripts (like AGSMuse and QueuedSpeech) or a generic concept that's not really that difficult (like GUIAnimation for example where I just change the GUI.BackgroundGraphic property).

Yours implement 3D effects and things which I frankly don't understand what's happening at all! ::)

Igor Hardy

#24
For some reason everything stops moving during cutscenes. Any ideas?

EDIT: AH! I added "always" to "repeatedly_execute" and it did the trick.

Kweepa

Ah yes, that'll do it.
It's probably worth putting out a new version with that change.
Still waiting for Purity of the Surf II

abstauber

Wow, this is really a great module. Is there by any chance a source of all those algorithms, you're using?

Kweepa

The source code is right up there ^^^. :=
The "magic" is in get_gradient_x/y, which just sum up some sine waves with different magnitude and phase, and are used to offset a block of pixels.
I don't have a specific reference. I just pulled the approach out of my arse. However, a quick web search for "2d fractal noise sum of sine waves" turned up this which seems similar:
http://developer.download.nvidia.com/SDK/9.5/Samples/samples.html#glsl_vertex_water
Still waiting for Purity of the Surf II

abstauber

Hehe... that makes the module even more impressive :D

I once had a book about megademo-coding for Turbo Pascal, but unfortunately I've lost it :(
It was full of algorithms and 3d gimmicks... *sigh*

PuNKKoMmANDO77

thank Steve , this module is simply amazing

Kweepa

Quote from: PuNKKoMmANDO77 on Thu 10/12/2009 11:35:57
thank Steve , this module is simply amazing

Glad you like it.
Let me know if you need any help integrating it into a game (that goes for everyone!). And also let me know if you release a game using it...
Still waiting for Purity of the Surf II

Lufia

Hi. I want to implement something like that in a completely different game engine and I thought this module could help me out. ... But I've trouble understanding how it works. ^^'

Starting here :
Code: ags
a[i] = IntToFloat(Random(2000)*(2*Random(1)-1))/scalar;

Get a random number between 0 and 2000, multiply by... either 0 or 1 ? then divide by 2000. I think I'm reading something wrong.

Then in get_gradient
Code: ags
float x = IntToFloat(xi)/160.0 - 1.0;

What's that 160 doing here ? Why -1 ?
I'll just trust you on the way to calculate dx and dy.

offset_a_block
Code: ags
int xoff = FloatToInt(1.0*get_gradient_x(x + w/2, y + w/2));

We're using the center of the chunk of background to displace as the argument for get_gradient, right ?

repeatedly_execute
Are you displacing 128 random chunks of background ?
I understand that first loop (I think), the rest of the function lost me.

I hope I'm not too annoying. >.>

Kweepa

Quote from: Lufia on Tue 26/01/2010 15:30:04
Code: ags
a[i] = IntToFloat(Random(2000)*(2*Random(1)-1))/scalar;

Get a random number between 0 and 2000, multiply by... either 0 or 1 ? then divide by 2000. I think I'm reading something wrong.
Yup; multiply by either -1 or 1; yup.
It's just a random float between -1 and 1. More complicated than it needs to be, sorry!

Quote
Code: ags
float x = IntToFloat(xi)/160.0 - 1.0;

What's that 160 doing here ? Why -1 ?
The 160 is the screen width divided by two. The -1 then makes x between -1 and 1.

Quote
Code: ags
int xoff = FloatToInt(1.0*get_gradient_x(x + w/2, y + w/2));

We're using the center of the chunk of background to displace as the argument for get_gradient, right ?
Yup, that's right.

Quote
repeatedly_execute
Are you displacing 128 random chunks of background ?
I understand that first loop (I think), the rest of the function lost me.
Yes, that's right.
The rest of the function displaces some more blocks near the player.

Good luck!
Still waiting for Purity of the Surf II

ZapZap

 First of all, I suck at scripting when it gets the tiniest bit complicated. So I was trying to use your module for a game (MAGS Underwater) and when using the fakescreen I don't really understand what are the parameters I need to put... one int for objects another one for characters and the baseline? I don't really get it (the numbers I 'intuitively' try result in error)
"Loose ends have a way of strangling you"

eri0o

Hey, does someone has a backup of this code somewhere? I am really interested in the code :). Also the FakeScreen module, I couldn't find it's topic. :~(

arj0n


eri0o

Thanks arj0n ! Just downloaded both modules! :]

m0ds

thanks arjon + eri0o! i was also going to ask for underwater a couple days ago :)

Danvzare

I never knew I needed this module until now. :shocked:

Mehrdad

#39

Maybe I'm wrong to use this module. But I get an error in AGS 3.4.1. I only add underwater.Enabled(); in one of my rooms. Is it enough or I need other codes too?

Edit: Problem solved. I forgot duplicate frame. It works perfectly on 3.4.1 too.
My official site: http://www.pershaland.com/

SMF spam blocked by CleanTalk