Sunland Villagers (was: Learning OpenGL/LWJGL)

Thanks :slight_smile:

Iā€™ll refine the looks of the map and the overall display as the project progresses. There is much to do to make it look better, but I think I know a number of ways to make it look good finally.

Right now Iā€™m quite busy to get some code infrastructure in place to handle workers and workplaces. I hope my approach to have workplaces, which may have one or more workers, and workers which have tasks will suffice and be simple enough to allow swift progress. A tricky thing becuase it involves math is to move the workers over the landscape. Well, the only problem is the calculation of the height, and I hope I can refresh my math related memories to get it right.

The carriers on the paths are just placeholder graphics at the moment. I used them to see if my code for creating workers and moving workers works. It does to some extends, but finding pathes and follollowing them is yet to be done.

I was looking at the source, because i was curious how the terrain was lighted (i cant get it to work).
But i dont really get what you are doing:


    GL11.glBegin(4);
    for (int j = 0; j < this.map.getHeight() - 1; j += 2)
    {
      for (int i = left; i < right; i += 2)
      {
        int normalI = i / 2;
        etc....
      }

      for (int i = left; i < right; i += 2)
      {
        int normalI = i / 2;
        etc....
      }
    }

Are you generating an quad for each 2 pixels, and doing this twice?

This code that I produced there must be really confusing for a reader ā€¦ sorry.

The map has a zig-zag border left and right. Iā€™ll try some ASCII art ā€¦ keep in mind each hex is made by 6 triangles, so there is a vertex in the middle of the hex as well (Iā€™ve left that out in the sketch).


 _
/ \_
\_/ \
/ \_/
\_/ \
  \_/

Every second vertex row is indented by half the edge length. This is the one problem.

The other is, how the triangles are drawn in a row.


\/\/\/

Every second triangle is upside down. And, this order changes for each second row. Even rows start like the sketch above, odd rows start with an upside down triangle.


/\/\/\

So I used two row loops, for the even and the odd rows. Iā€™m sure there is a way to get the vertex positions correctly in one calculation, but Iā€™m not that good at math. And my map structure is basically a 2D array, which is used to store triangles, so itā€™s spaced with a factor of two horizontally (2 triangles in a quad).

Very confusing. Normals are stored in the ordinary spacing, thatā€™s why normalI (normal index) is half the triangle index in a row. Itā€™s really quite the voodoo code, sorry. I was and still am way too new to the whole 3D and OpenGL topic to produce good and clean code for it yet.

The lighting purely depends on the normals and the light source position. If you want to try it, just draw flat tris/quads and use random normals. This should already give some random wavy bumps even that the surface is actually flat. Sorry again for the magic code there :-\

Iā€™ve split the game into display-synchroneous actions and asynchroneos actions. Movement and the like is now display-synchroneous, goods creation, good routing and management related taska will be asynchroneous. At the moment there are none of such tasks though, but the infrastructure is in place.

Along with that came some bug fixes to path building and path traversal. Iā€™ve uploaded a new demo version to Sourceforge to show the current state of development:

http://sourceforge.net/projects/sunlands/files/v0.07/

There are no error checks, so input which the game engine doesnā€™t like will just cause problems. The road workers just run up and down the path endlessly - itā€™s really just some demo code at this point.

Next steps will be to add workers to the buildings and add code to create and manage goods.

No its clear what you are doing now.
You dont have to apologise for your code (as long you dont publish it) haha :slight_smile:
But very good work.
Its going to be awsome, im sure about that.
If you want to create an more peacefull version of settlers, have you considered taking some stuff from the Anno series, also loved these games (except the last one) :slight_smile:

Btw check this link about triangle strips: http://dan.lecocq.us/wordpress/2009/12/25/triangle-strip-for-grids-a-construction/.
Maybe it can help you creating the terrain, its very smooth and combined with vbo speedy as fck.

Triangle strips will surely speed terrain rendering up a bit, but I feel too lazy to change the code as long as itā€™s not a bottleneck ;D

Iā€™ve been busy with the workers and the worker management. But I think I have something presentable now. Two workplaces with workers are included, a forester who plants trees, and a woodcutter to cut them down again. Good neighbors for sure :wink: And very tireless workers at the moment.

With this in place, it will be easy to add more workplaces and workers. The worker graphics are all just placeholders at the moment, to see if the code works. Iā€™m much better at making houses and trees than I am at making animals and persons ā€¦ or anything animated.

The latest demo is here:
https://sourceforge.net/projects/sunlands/files/v0.08/

The triangle strip idea was more to fix the messy code :).
It looks great, im really jelous =D

Thanks, itā€™s shaping up nicely indeed. There still is a lot of room for improvements though, particlarly more varied landscape textures and better texture transitions. Lately bug tracking and bug fixing starts to take up a good amount of development time, though - up to odd behavior like, if you build a hut right in front of another workers hut, that one canā€™t leave his hut anymore and will eat up more and more CPU time in tries to find a way out :persecutioncomplex:

This and a number of other bugs have been remedied in v0.09:
http://sourceforge.net/projects/sunlands/files/v0.09/

In the past years I havenā€™t played a lot of games. I often wanted to buy the one or other from the anno series but I nevr did - sometimes they were just too demanding for the computer I had at the time.

Can you help me out with some hints, which features you liked most, and which youā€™d like to see implemented in Sunland Villagers, too?

The main part of the anno series was building up and evolving the economy.
You start with building some of the main resources, like wood and fish.
With wood you can build houses for villagers (they will slowly migrate to your setting) and with fish you can keep the villagers alive.
When your setting has grown a little, these villagers start demanding goods, like wine and wool.
When these demands are satisfied, more people keeps migrating to your settlement until they reach a new civilisation level, with new demands etcā€¦
Soon enough you wont find all of the goods they need on your main island, so you need to claim another island for the other demands.
Also products like bread need an producing system like wheat > flower > bread.

Setting up these chains over multiple islands and turning an ā€œwildā€ island towards an civilasations is really fun.
The main diffrence is that with an game like settlers you build up producing chains for an army.
With anno it all goes around your residents.
You gotta play it sometimes to see what i mean.

Good, that matches quite a lot of my ideas. Thanks for the detailed explanation!

This is looking really cool, wasted many hours on the settler series :wink:
Iā€™m a bit amazed at how fast you progressed with openGL and this game.
Didnā€™t remember it as that easy when I tried!

Art is starting to come together aswell, made all of it yourself?

Good luck with your project :slight_smile:

I started with this tutorial:

http://lwjgl.org/wiki/index.php?title=LWJGL_Basics_3_(The_Quad)

It sets up a 2D display for you and you can move quad around. This was already a good start, but the first days were googling and reading tutorials mostly.

The rock/mountain texture and the ā€œheadqarterā€ big building are borrowed from the Widelands project.

Many graphics I had already before I started. The woodcutter hut, the forester hut and the poplar tree Iā€™ve made during the past week - the photo for the poplar tree I had taken a while ago, expecting that I will need tree images some day soon ;D Itā€™s sometimes quite difficult to find single trees to take photos from in a useful angle for games.

Almost there! But well, you know the last 10% take 90% of the time ā€¦

I think I have the basics in place now. A headqarter which stores goods, a workplace (woodcutter) which produces trunks, another workplace (forester) to plant new trees. Also routing the goods through the path net, and the actual transport with carriers.

Iā€™m quite sure there still is a plethora of bugs and overlooked special cases, but for 2 and half a week, I think this was quite good progress :slight_smile:

http://sourceforge.net/projects/sunlands/files/v0.11/

To test it:

  1. Switch to " Buildings", ā€œLargeā€. Select the headquarter (aka storage house).
  2. Search for a place on the map which you like and place the headquarter there (click spot).
  3. Switch to ā€œPathā€. Select the path building tool.
  4. Build a path to a nearby forest. You can build the path in several segments, each seqment will get a carrier.
  5. Switch to ā€œBuildingsā€, ā€œSmallā€, slect the woodcutter.
  6. place the woodcutter hut (click spot).
  7. Conncet the woodcuuter flag to the path.

It might take a while till the woodcutter made up his mind which tree to cut next. Also he uses to rest between two trees. Also donā€™t worry if the trunks are not carried into the headquarter, but pile up at the flag in front. This is where to current routing code thinks that the destination is. Iā€™ll fix this later.

If you made a path building or house placing mistake, try this:

a) Select the ā€œ?ā€ Button
b) Click the path flag or building flag which needs to be removed.
c) Confirm ā€œremoveā€ in the popup
d) Building, flag and the adjacent paths will be removed.

Now the project needs map saving and loading ā€¦ and more workplaces.

In v0.11a Iā€™ve added a storekeeper to the headquarter who pulls the goods in. Otherwise itā€™s identical to the v0.11

http://sourceforge.net/projects/sunlands/files/v0.11/

Nice work, i only have 3 bugs to rapport:

Terain got messed up when i placed an hut, adding some blue-ish triangle (you really should use more triangles).
Some guy flew over the screen (walked really fast).

Exception (indexoutofbounds):


<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2012-11-29T13:31:18</date>
  <millis>1354192278428</millis>
  <sequence>15</sequence>
  <logger>villagers.Main</logger>
  <level>SEVERE</level>
  <class>villagers.Main</class>
  <method>run</method>
  <thread>1</thread>
  <message>java.lang.IndexOutOfBoundsException: Index: 2, Size: 2</message>
  <exception>
    <message>java.lang.IndexOutOfBoundsException: Index: 2, Size: 2</message>
    <frame>
      <class>java.util.ArrayList</class>
      <method>rangeCheck</method>
    </frame>
    <frame>
      <class>java.util.ArrayList</class>
      <method>get</method>
    </frame>
    <frame>
      <class>villagers.workers.tasks.WalkingTask</class>
      <method>walkAStep</method>
      <line>59</line>
    </frame>
    <frame>
      <class>villagers.workers.Carrier$EnterIdlePosition</class>
      <method>doWork</method>
      <line>166</line>
    </frame>
    <frame>
      <class>villagers.workers.Carrier</class>
      <method>prepareDisplay</method>
      <line>81</line>
    </frame>
    <frame>
      <class>villagers.Map</class>
      <method>prepareDisplay</method>
      <line>330</line>
    </frame>
    <frame>
      <class>villagers.display.GlRenderer</class>
      <method>drawMap</method>
      <line>263</line>
    </frame>
    <frame>
      <class>villagers.display.GlRenderer</class>
      <method>render</method>
      <line>252</line>
    </frame>
    <frame>
      <class>villagers.display.GlRenderer</class>
      <method>run</method>
      <line>755</line>
    </frame>
    <frame>
      <class>villagers.Main</class>
      <method>run</method>
      <line>177</line>
    </frame>
    <frame>
      <class>villagers.Main</class>
      <method>main</method>
      <line>64</line>
    </frame>
  </exception>
</record>
</log>


Thanks for testing :slight_smile:

The flying gui might just have been a new carrier, who tried to reach his new workplace (after building a new path). If it was some other event, please let me know and Iā€™ll look into it.

The problem that buildings sometimes depress the landscape so much that new water appears (blue areas) will be fixed in the release. In future there will be more checks also and bigger buildings need more open/flat space to be built.

The ArrayIndexOutOfBounds exception came from a carrier, who had a fairly short path. I think Iā€™m going to fix this by making the minimum path length two vertex distances, but Iā€™m not sure at the moment if this is alright.

I hear the wish for a better modelled landscape but I donā€™t want to solve it by adding more, smaller triangles, rather by having better transitions between the textures. Also not quite sure about this one, so Iā€™ll wait a bit before doing changes, and try to make up my mind.

Thanks for testing and the reports, this is quite helpful!

You are correct about the flying GIU, it occured after i placed an hut.

The terrain bug seemed like an big triangle splitting up, so thats why i gave the comment about more triangles.
I was testing a little with terrain, and with triangles around 32 / 64 pixels, you get very smooth terrain already (see my thread about normals in newb), the triangles in your game seem much bigger.

No problem, i would love to see this game finished :slight_smile:

Iā€™ll try to get it at least to a playable game :slight_smile:

Iā€™ve uploaded a new version - it contains a number of bug fixes, but also a fairly big change which might make it more difficult for new people. Buildings canā€™t be placed everywhere anymore, but need a certain amount of fairly flat, open space to be built.

The ā€œShow build helperā€ function has been expanded a lot to help there, but the updates are a bit slow. So I donā€™t know if it still works well on older computers. The function is now also turned on by default now.

http://sourceforge.net/projects/sunlands/files/v0.12/

I feel a bit uncertain about the changes, and I might have introduced new bugs with them. I also need to learn how to configure the logging properly, at the moment logs are just written to the debug window.

I think the project is at a point now where saving and loading games will be needed. Saving the map is easy enough, but it wonā€™t be easy to save all the workers with their tasks and states.

Edit: I forgot to say, the default edge length of the triangles (horizontally) is 100 pixels. You can zoom in and out with the mouse wheel.

Itā€™s been a lot of typing work, but I think I have everything in place now for saving and loading games. Maybe I missed some things, but the new release includes a small demo savegame which seems to save and load properly.

http://sourceforge.net/projects/sunlands/files/v0.14/

To save a game, press ctrls-s. To load a game press ctrl-l. To load the included demo game, just start the game, press ctrl-l, wait a second and it should be there.

There also have been some bugfixes for flag and path handling, and the worker/task classes were overhauled a bit. So some things should work better now, but as usual, I might as well have introduced new bugs while ploughing through the code and adding savegame related routines.

And last but not least, the forester now plants small trees which need some time to grow :slight_smile: