Epsilon

Prototyping a Game - Adding Opposition

Posted on September 25, 2013

Continuing from the last post with prototyping a game based on hacking networks and gaining territory.

Computer Player and Refactoring Trojans

The beginning of different player types

The beginning of different player types

Today was spent refactoring how the trojan code worked. Before I had it so that a new trojan object was created for each cell that it infected. It became immediately apparent that this was a very poor implementation on my part however, as things slowed down to a crawl quickly. So instead, I have the trojan maintain control of where it spreads, and moved drawing for the trojans into that module.

Along with that I also made a trivial implementation of a set made to contain points. This proves very helpful as a quicker way to test if a tile exists at certain points, or if the trojan can move to a new spot.

After this was all working somewhat ok, I started work on differentiating trojans. To start off, I made a computer player type and a human player type. There is no special logic made for when these two trojans interact though, that will be worked on next time.

Implementing a Point set

One of the interesting things about Lua (the language Löve2D uses) is its heavy use of the table (map) data structure. Usually you can easily implement a set using a table, by using the the keys part of the table as where you store your data. This proves difficult in Lua though, due to the fact that if I use a Point table as key for another table, it uses the reference address to determine equality of the tables. This means I couldn’t do this:

set = {}

pt1 = {x=1,y=2}

set[pt1] = true
print(set[pt1]) --true


pt2 = {x=1, y=2} --make a different table
print(set[pt2]) --nil, not the expected true

So for the Point set I had to implement a 2 layer set. The first layer uses the x value of the point as a key, and a set that uses the y value in that slot. Doing this made it so I could use different Point objects and still be able to use them for the Point sets I made.

Do not take this to mean that other languages would not have this problem if I were implementing a set type. It’s mainly due to the fact that Lua leaves a lot of things to be implemented by the user.

comments powered by Disqus