20150903

Units between tiles and attacking

Having the requirement that units travel between tiles for one or two turns is nice, but it poses a problem: when a unit is between two tiles, what happens when someone would arrive to one of those tiles?

Moreover, there is another problem: what if two units of the same owner want to move to the same tile at once? I made it a clear rule that one tile could be occupied by only one unit at a time.

Ok, let's tackle the first problem: say there is a unit (blue circle) trying to go from Tile A to Tile B. It's the start of the turn. Now let's imagine there is another unit (red circle), that is trying to move to Tile A.





Let's say their speed are 0.5. (I should've painted mountains instead of grasses. Whatever.) At the end of the turn, their positions are like this:






I quickly discarded the idea that we should use the border of tiles as places, too, because that would really complicate things - like calculating visibility, calculating speed, and so on.

But which tile should we take into account when a unit is between tiles? The target tile? The starting tile? Both?

If wou would take both into accounts, then the red unit would kill the blue. This is because the target of the red Infantry unit is the same as the origin of the blue Archer. But at the beginning of the turn, there were no overlapping tiles. So this idea is not consistent - it is possible to kill a unit when both of the attacker and defender are moving. Idea scrapped.

Okay, then let's look at the possibility when we use the target tile. Here, blue tries to escape by going to the mountains.




We know that going to the mountain halves a unit's speed. So going by common sense, red should capture blue. Let's see, what happens if we take the target tile into account:




Seems kind of counter-intuitive. Okay, red could capture blue in the next turn, but still, this is plain bad for a perfectionist like me. If we would take into account the original tile the unit was on, everything would work as they should.

The result is that we will take the original tile into account. But what about multiple units going to the same tile?

Since I don't allow more than one unit on any single tile, I would have to kill some units. But which one? It seems I would have to kill all of them. Which is kind of unfortunate if the user only misclicked, or was tired, and didn't think through where they want to place their units. One possibility would be to have the regular "move and attack" command, and there a simple "attack" command as well. The "move and attack" command would tell the unit to attack anything that is on the target tile, and also start moving towards it. The "attack" command would tell the unit to attack the target, but stay on the place.

But in this case, the unit's attacking tile would be one tile forward, but the tile it can be attacked on would be one tile backward (when the unit is between tiles). This is really uncomfortable if an infantry attacks an archer, since I also want every attack to result in exactly one winner. Would that mean that the defender could kill the attacker on a tile it can't even attack?

Also, it would be possible to set up a chain of attacks, where the outcome is not so clear at first glance:





The unit marked with read would die because it is attacked with a unit of the same type, but would also kill the blue unit. Resolving this in game logic would require a two-step system: in the first step, we check all encounters, and mark the dead units, then in the second run, we delete all those marked units.

If the units attaking tile would be the same that they could be attacked on, this wouldn't happen. But this would eliminate the possibility of a command that tells the unit to attack, but not move. (Although I don't really like the idea of attacking without moving, this is a medieval game, not a sniperfest.)

The other solution would be to just simply disable this possibility from the client. This is not a good idea, as I can't be sure that players use the official client, and they can issue commands that would make the gamestate illegal with respect to these rules.

The most likely scenario is that I will just resolve units of the same owner going to the same tile like I would do it if the units were of different teams. I don't want to disable this possibility in the client, because it could be useful in some very rare cases.

There is also the possibility that two units of different teams want to swap places. I don't want them to just swap places, because it would make it really hard to capture a single unit. But by these rules, this is what should happen.

It really needs some serious thoughts.

20150822

WHY U NO BLOG????

Well. This is my shot at looking like a normal indie developer - keeping track of things on blog, on twitter, on github, on yout...wait, not youtube. At least not yet.

I had a lot of things to write about until recently - design decisions, progressions and hiccups in the development process, things like that. Well, maybe it isn't considered much by indie standards, but I usually have very few things to talk about to random strangers of the internet. But for a few weeks I haven't written anything. It is because, for a few weeks, I haven't progressed anything. I wrote maybe 20-50 lines of code. What happened?

Work. More work. Heatwave. Heatwave while working. More work. Not enough sleep. Heatwave. Work.

The thing is, I'm a very low-energy person, and it can be depleted pretty fast. This month, there was always a colleague who was on vacation. (not me, mind you.) This left me with a workload bigger than I usually had to bear with. When I got home, I was dead tired. All I could do is to fire up some MOBA and anger myself because of the noob teammates.

And then there were two heatwaves. My room was a sauna. If the air is 30 °C then I can't think. (We don't have air conditioner.) So in addition of being tired, I could barely get enough cool. I couldn't do anything productive. But now the heat is gone, and all of my colleagues are finally back.

Next week, I won't be home, so probably I won't code. After that... well, that depends. Since I am in a different department in my work starting from September (it doesn't ahve anything to do with this month, just a coincidence), I'll have to learn a lot of new things. (I'm learning SAP, ABAP, and things like that.). If it doesn't sap (hehe, get it?) too much energy from me, then I will be able to progress. Otherwise, not so much.

I also angered myself so much on Heroes of the Storm, that I deleted all games from my PC. I considered myself pretty good, I had a nearly 50% win rate. And then it broke. For example, the last 60 matches I played, I lost 40 of them. (I lost all of my 10 most recent matches.) I don't know if I suddenly became lame, or just RNG thought I should play with noobs, but I had enough. Team games suck, because doesn't matter how good you are, you won't be able to carry a whole team.

Fuck them.

20150714

PaaS and Compiler bugbashing

In the past few days, I thought I should start to have my server code running on some online server, so I decided to give OpenShift a shot - this is a Platform-as-a-Service (PaaS) solution, which - in layman's terms - means I can host my server without paying anything. Of course, the free plan can't handle big traffic, and there are other limitations, too. But it's fine for testing.

Since I wrote my server in Haskell, and OpenShift supports custom cartridges (Haskell, for example), OpenShift was a logical choice. However, it requires that my code is buildable in Cabal (Haskell's "package management" system), which shouldn't be a problem - I just write a .cabal file defining the name of the project, the author, files that are used, etc. Well, it did cause problems.

If I compile my server using `ghc main`, everything works as it should. However, if I use cabal to build it, it doesn't work quite as it should - it hangs at some point. I couldn't uncover the causes, so I wrote a post to reddit (2nd time I ask the internet's help in something), where a commenter gave me some pointers on how I can continue uncovering the secrets of this case. Finally, I was able to find the cause of the problem: cabal has optimization level one on by default. (Cabal invokes GHC with a ton of flags. I took and replicated this command in the comand line, and then tried to delete some flags, trying to figure out which of them causes this problem. Finally I could isolate the -O flag.) No problem, I could simply turn it off in the .cabal file, so the problem is essentially solved. However, there was something still bothering me.

I suspected a bug in the haskell compiler (GHC 7.10.1), so I tried to reproduce the problem with a minimal and complete code. My method was deleting more and more code, furiously recompiling to check if the problem is still there, until there was only one file left, with a code that couldn't be simplified further. I checked a few times more, to be sure that if I made any changes to this code, the problem would go away, and then filed a ticket on GHC's trac.

Since this is my first bugreport, I'm a little nervous, and also I have a feeling I could have done more to help their problem (provided that this actually *is* a compiler bug, and not a result of some silly mistake I made), but we'll see what happens next.

All this was done while I was sleep-deprived and finished a 7-hour shift. It took me about 6 hours to do all this stuff. And I don't have much time to sleep, so I probably should go to sleep now, so I maybe have another 4 hours of sleep. But I think I'll pat myself on the back.

20150710

Towns

Map editor now supports towns. A couple of points:
  • Had to change Physics2D.Raycast to Physics2D.RaycastAll in the code responsible for changing tiles, due to towns being over terrain tiles - probably would have been more elegant to use layers, instead of just checking for the name (either "HexTile" or "TownTile"), but I'm following the 3-step development process (I'm currently at step 1):
    1. Make it work.
    2. Make it beautiful.
    3. Make it fast.
  •  This GUI is like my backpack: there is always more room for things. But it starts to look cluttered. I probably should do a redesign at some point in the future.
  • Original plan was to add the ability to place units, but then I would needed another GUI feature to change the amount of food each unit had. So I scrapped it. Along with the GUI redesign, I'll need to find a convenient way to add this feature.
  • You don't need undo and redo... *waves hand like a jedi* You really don't... Okay, you do, and I'm a lazy sloth. Someday... someday...

20150709

Saving and loading maps

I am lazy. I wanted to figure out a file structure for storing maps, but then I thought it would be easier to just serialize the whole Map object to a file, and then load it back. It isn't programmer-friendly, meaning it is really hard to write a map file parser, if you don't use C# and you don't know the layout of the Map object. I promise, if the need ever arises, I will rewrite the whole map saving/loading part.

So currently, I'm using System.Serializable for saving and loading maps. There was a little problem, though (other than figuring out how serializing actually works): Unity's Vector2 is not Serializable. I am in a fortunate situation, as I don't use Unity's types extensively in my Map object, so it was easy and fast to write an ad-hoc replacement for Vector2, but if you ever want to use C#'s internal serializer, know about this problem: Unity's types are not serializable by the default C# Serializer.

Anyway, a gif from the Save / New / Load dialog:


20150707

Minimap

Today, I finished up some code from yesterday, simplified the painting algorithm, and added a minimap.

The painting algorithm was a little bit pathetic: at first, I only supported 3 brush sizes. Writing the painting algorith wsas kind of trivial for these three sizes: I told Unity which tiles should we overwrite. Yes, by hand. I know, I should kill myself. But today I re-discovered some older code (it was from the map generator I wrote for a tech-demo), which was a method to create hexagon rings (a hexagon ring, in this sense, consists of every tile that has the same distance from some origin). Now I just tell how many rings (of what sizes) do I want to make, and voilá, I can have as many brush sizes I want (I made buttons up until 6, I think that's more than enough).

For the minimap, I originally thought I would have a Texture2D, and update it at every painting event, but then I did a quick google search to see how others do it. Well, it turned out, creating a second camera is way more easier, and why-the-f***-didn't-I-think-of-it-OMG... So I made a second camera, and I'm dynamically changing it's viewport size as the map gets bigger. For this, I used a bounding rectangle calculator method, which I wrote earlier today - it simply calculates every added hex's coordinates, and updates the min and max variables accordingly. It doesn't update when erasing, though - that would be a little more resource-heavy. But if you completely erase the map, it will reset to default. Hmm....probably I should have a Clear button for that...

Also, I have noticed that the tile outlines are kind of visible in the minimap. This gave me the idea to make a layer for a rectangle encompassing the area the user can see, and it would be only visible to the minimap, and then place the outlines to a different layer, which would be only visible to the main camera. Also, clicking in the minimap should reposition the main camera - I think I can read the coordinates from the minimap's camera, and set the main camera to that position. But this is not a priority right now.


20150706

Map painting

The map editor has a few things it should be able to do:
  • Create a new (empty) map
  • Save a map
  • Load a map
  • Show a minimap of the whole map
  • Set brush tile type 
  • Set brush size
  • Set tiles
  • Erase tiles
Today, I did all painting-related things: setting tiles, changing brush tile types / brush size, and erasing tiles. Basically, you have a brush that is either 1, 3 or 5 cells wide. There are eight terrain types, plus an "empty" terrain type, which is effectively the eraser.

How did I make this work?

I made a HexTile prefab, which is a GameObject with a SpriteRenderer component (and also a child that would be the outline, but this will be only used in Game mode). This is what gets put here and there. At each frame, I check if the mouse button is down. If yes, I calculate which hex is the mouse pointing at, and then check if there is already a tile at that hex's center (with Physics2D.RayCast) - if yes, I delete it. Then I check which terrain type is selected (the buttons on the sidebar are a toolbar) and if it is not the empty terrain, I put a tile of said type to that hex. If the brush size is bigger, I do the same for the other hexes.

The result is here: