Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Anyone happen to know a thing or two about the water in Terraria?

Discussion in '2D' started by Aidenjl, Dec 18, 2015.

  1. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Hello!

    So the story goes, I'm currently trying to replicate the Terraria world generation in Unity2D. So far all has been going fairly well. Here is a nice screenshot of my current progress: upload_2015-12-18_4-0-10.png

    So as it goes, I decided that I would like to program the liquids next as I have the majority of it done apart from liquids and biomes. However, I cam to the realization that I don't know enough about the logic behind how the water works, I tried doing some research online, but there doesn't seem to be much beyond the fact that it works as some form of cellular-automata. So I was just wondering that if on the off-chance someone around here could give me some detail behind how the water operates and decides to flow, I'd greatly appreciate it if you could share your knowledge with me.

    Any help would be much appreciated!

    Thanks! :)
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    I am not familiar with how Terraria water works. Here's a way you can do basic flowing. But if you want more advanced features then this won't be sufficient enough.

    You can have a queue which stores the fluid's level and position in a class or struct.

    You set an update interval. At each update: go through each item in the queue, dequeue it, send it into a function.

    The function is responsible for flowing. It checks adjacent cells around the fluid entering it to determine if flowing is possible. These can be based on whatever rules, examples:

    1. There's a block type that fluid is allowed to flow into such as air.
    2. There's fluid that has a low enough fluid level to be replaced.

    If you determine that flowing is allowed, reduce the fluid level by some amount and place fluid to the adjacent cell.

    I would place it in such a way to where it's registered in the block grid but is then added to a separate list of "pending" blocks. That way you can fully run the update on every item in the queue before adding new items to it.

    Once you've emptied the queue, go through the pending list and add them back to the queue for the next update.

    Stop when the fluid level is too low to spread anymore (return case in the function).
     
  3. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Thanks for the response! These was exactly what I was looking for actually!

    Much appreciated, many thanks!