Minecolonies & Fancy Border Rendering

in #utopian-io6 years ago

Hi everyone, until now minecolonies always had a fairly inflexible border rendering, where we would calculate the border of the colony based on the distance given in the config file.

While it looked quite nice it did not consider changes of the config file, manually claiming certain chunks or certain chunks losing the claim due to bugs.

I wanted for a while already, create some chunk based rendering which works better, I did not do that yet mainly due to laziness of having to work with the manual openGL rendering methods.

The new wall now shows the wall accurately chunk per chunk.
And has a small exponential factor the farther up you are the smaller the distances down there seem. (Thanks to @reggaemuffin for the idea)

Preparations:

I started off finding a long term bug where I used the wrong tag to deserialize chunks (my bad).

Then I set up a tuple class which extends the Minecraft tuple but has equals and hashcode implemented (To put it in a hashmap)

Don't ask me why the tuple doesn't have this by default...

Then, I had to make sure the client has all the information about the chunks. So, on entering a chunk I would send the client a message.

Which would check for all chunks around the client in a certain range, check if the chunk is loaded and then append its information to a list.

Then, I needed some code to serialize and deserialize it again.

And finally, on reception, we iterate through the list of colonies and append them to the client-side chunk.

The Rendering:

For the rendering, I had to register a new class which has the render event for the client-side rendering.
But also has a tuple for the last chunk the player was and the map of tuples of the chunk info.

To make this work I had to register this event handler.

Now we get to the main code. First, we retrieve some information as the player, the partial ticks, the world, and colony view and make sure that the player is holding the buildtool to enable the rendering effect.

Then, if the chunk the player is standing in didn't change.
We set the position he is standing in, in the tuple, clear the list of chunks, go through the range of chunks and retrieve the information we need from the capabilities.

We do this to save performance, so we don't have to do this every single partial tick.

Now, we gotta set up the rendering variables.

We need the tesselator and bufferbuilder for the drawing.
Calculate the relative player position to know the relative location from where the player is looking.
We also get the lowest limit from where we want to draw the border to the top. Besides that, we get a relative height to enable the exponential size.
After that, we set some GlStateManager variables I have no idea what they do but I know I need them.

Calculate the current relative chunk coord from where the player currently is and set up the line settings.

Following that, we go through the list of colonies, set the chunk and real position in a variable, make sure the chunk we're drawing in belongs to the current colony we're standing in and set up some variables.

These variables are then used to check if this chunk is connected in a cardinal direction to another chunk owned by this colony. If we don't have the information because of the distance, it's too far away to draw anyway.

Then, we go through the levels (calculating the limit).
And draw the horizontal lines for each direction where there is no colony connected (showing the border).

To draw a line, I need two vertexes, one starting at alpha 0 and one at 1 with their relative starting points.

At the end you can see the code to calculate the relative level of the exponential line distances.

The calculation I used it relatively arbitrary, I could've made it stronger but I wanted to avoid that the player gets seizures while he moved.

After that I gotta draw the horizontal lines at the divisions of each chunk.

I decided to draw one per chunk, always to the left of the lines which would then connect nicely through.

Avoiding to have to draw 2, spending unnecessary CPU.

Finally, we have to wrap this up with some settings I got no idea about, but I'm sure I need them (copied from a Minecraft class which draws some lines).

And lastly, I cleaned up the old border-rendering code which was not needed anymore.

I hope you enjoy this new feature as much as I do, this will enable us in the future to grow colonies more organically and allows the player to detect claim issues easily.

Repository:

https://github.com/ldtteam/minecolonies

Pull Request:

https://github.com/ldtteam/minecolonies/pull/3535

Sort:  

Thank you for your contribution.

  1. You might be able to use javafx.util.Pair instead of creating Tuple
  2. You might want to extract this if (!c.getValue().equals(colonies.get(new Tuple<>(x - 1, z))) && colonies.containsKey(new Tuple<>(x - 1, z))) into a helper function since it contains duplication i.e. same tuple is created twice. Also, if four directions cannot be set at the same time, you might want to add else if instead.
  3. many magik numbers are hard to understand - better to define them as contants.
  4. code duplication could be extracted: bufferbuilder.pos(chunkCoordX + incX, levels, chunkCoordZ + incZ).color(1.0F, 1.0F, 1.0F, 0.0F).endVertex();

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @justyy! Keep up the good work!

This looks awesome and professional, the exp lines really help with depth perception :)

Hi @raycoms!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @raycoms!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Hi, @raycoms!

You just got a 9.27% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.


This post was shared in the Curation Collective Discord community for curators, and upvoted and resteemed by the @c-squared community account after manual review.
@c-squared runs a community witness. Please consider using one of your witness votes on us here