City4k

I love city building games and wanted to see if I could fit one in 4k. So all of you simcity fans have at it. Let me know about any balancing issues you might have and I’ll try to make some quick updates.

V2:
Roads are cheaper
Time till next payday is displayed as a white bar
3 Icons per row!

V3:
Minimaps!
Sped up traffic and power algorithms!
Better color coordination!

http://www.russellspitzer.com/wp-content/uploads/2012/02/City4kLarge.jpg

For an easy start make sure that your first residential units are at least ~7 squares away from the power-plant. My basic start is Check out your land value overlay for more info.

http://www.russellspitzer.com/wp-content/uploads/2012/02/City4kStart.jpg

http://www.java4k.com/index.php?action=games&method=view&gid=397

Several Comments from me:

It’s difficult to gauge the time step in the game. Having some sort of indicator would be useful so that you can understand when things are going to happen, especially the removing of money due to maintenance. It’s sort of difficult to figure out what’s going on. I’m not sure what I’m doing wrong, but it doesn’t seem like time is passing while I’m trying to play.

A reset button of some sort would also be nice. Having to reload the page when I just realized that I completely botched things (By building too many roads too early) is a PITA since my desktop’s Java drivers are out of date and I have to do the whole “Run this time” thing each time.

A Bulldozer feature would be nice as well. Just something to remove what’s placed there, like a road that you realized you didn’t need.

Not sure how much more you space you have, or whether you can even implement these, but…

People refuse to move into my houses even though I tried to build my city precisely like in your screenshot above :frowning:

Cute! SimCity in 4kb, I never knew it is possible :smiley:

Interface:

  • make 3 buttons in a row instead of 2, also use 2 letters, this will reduce confusion. The first row is zones “RZ” “CZ”, “IZ”. The second is special “PP” “PL” “R” (road is one letter).

Balance:

  • too difficult, can’t reach even 1 population
  • the maintenance cost for roads seems too high

Bugs:

  • an empty zone does not relay power

Took me a few tries to get people to move in, but I got it now.

However, after I got a reasonable village (about 180 people, 4 industrial areas and 3 commercial areas) the game started to get extremely slow and the window turned white for some time. It seems like there is some loop that’s taking up a lot of time.

Other than that I think a bulldoze function would be nice (and if it fits maybe a minimap, I keep losing my city when I go to other windows :P)

Thanks for all the feedback!
I’ve changed the game around a bit so it should be a bit easier and run a little faster.

Unfortunately i’m pushing up against 4k really hard now so there isn’t really room for a Minimap. I also had bulldoze in the game before but again 4k is maybe just a little too small for a full featured Simcity. Imagine that there is a very vocal group of activists in the town who insist on preserving everything.

I think the traffic detection loop may be causing slowdowns, if someone can post a screenshot with a really slow game that would be a big help.

I didn’t really want power to flow through unbuilt zones but if that would be useful I can add it.

Thanks again and try V2!

Either I’m missing something, or there are quite a few bugs.

When I connect powerlines to the residential zone, which have roads connecting them to the Industry zone, they might start building houses. After restarting the game 3 times, I got them to build houses only once.

Initially, when you have a connected R-zone, you see the houses are built near the roads. Fair enough, so expand the road network to connect more houses to the industry… nothing happens, no new houses are built.

:emo:

@Riven,
Are you sure you have a road reaching enough industry tiles? If a road borders an industry tile it only has access to jobs two deep. A road needs to be run on both sides of the zone to allow access to all of the tiles.


OOO
OOO
XXX

Can you detail the problem with a picture?

Oh I see. That Residential zone is in a poor land value zone due to the proximity to the industrial region and the Power Plant. People won’t move in when the land value starts getting orange~red. If you click on the overlay you’ll see that the residential zone you placed is almost in pure red. Too move people in that close to industry you have to start by building a little further away and then inch you way closer and closer as the newly built houses will increase nearby land value.

Thanks, I built a nice town, then this happened:

Exception in thread "Thread-11" java.lang.ArrayIndexOutOfBoundsException: 4130
	at a.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

I was building at the edge of the map.

At a population of ~200 and ~100 roads, the game grinds to a halt. No exception, just 100% CPU usage and failing to respond to input. :-\

Blargh. You found me out, to save some bytes I was a little fast and loose on the edge of map calculations. I’ll see if i can put in some failsafes that don’t cost to much memory. As for the other slowdown … ??? I have 4 days left right :slight_smile:

I made a quick image of a scenario where the game slows down a lot:

http://imageshack.us/photo/my-images/823/city4k.png/

btw, is the placing of items bound to the mouseUp event? when I click somewhere and move the mouse during a slowdown the tile get’s placed at the position where my mouse ends up (hence the odd road and powerline in the screenshot :))

If the slowdowns are indeed because of traffic calculations, you don’t have to update them each step, only when something on the map changes. It could be solved with a simple boolean check, so it shouldn’t take up too much space.

I just jury rigged the game to skip traffic, so Now i’m sure its the power calculations. Its all a relic from when I had these grand plans of trying to limit the amount of power one plant could give, but that just ended up taking way too much space. I have to go to real work now but I’ll try to fix this tonight.

Thanks everyone for your support!

Ah, you need to build power lines on roads but you cannot build roads on power lines, that explains it all :slight_smile:

Got to 550 people but by then the game was so slow it was unplayable (I made more money than I could spend because scrolling or clicking buttons took forever).

Nicely done :slight_smile:

Mike

Minimap - instead make the map smaller (it will prevent players from “losing” the city). And/or map coordinates display.

Edge of the map failsafe - instead put 3 tiles of water around the map. Should be cheaper and it will make the game look better (you are on an island).

Speed - traffic and other calculations don’t have to be done each cycle. Make a counter and update traffic like every 10 cycles. Or even better make the traffic update cycle based on number of population (the more population the rarer the traffic updates are, while interface behaves fast). Also, if you have other calculations (like energy) do these in other cycles that traffic.

Colours - you should make the colour of the button and building identical (it’s confusing right now).

Any chance of releasing the source code by the way?

I’m going to release the source for all of my games once I clean it up so it won’t be so embarrassing. We are allowed to display code post contest right?

I’ll give you the gist of how the game runs as is:

The game is basically a cellular automata where every cell has a bunch of things that get computed. Since there are way too many cell to iterate over every frame the game tries to do as many rows of the map as it can prior to the nextFrameTime.

Power is handled by doing a BFS every 60 or so frames but I realize this is killing the program because of how I have it built. Long story short, a really long array list is built and I use arrayList.in() which is causing the slow down. I built it this way originally so that I could have integer values for the amount of power in the power lines and there wasn’t a clear way of knowing if the BFS had visited a node before. This was a mistake. I’m going to fix this by switching power to being a binary thing that way I can keep the search from backtracking by simply not executing on nodes that are already on.

Traffic is calculated when someone moves in. A DFS is called along the roadway which stops if it hits max traffic at any square. All Squares visited are incremented. There is another array list here but it doesn’t seem to be slowing much down. I know this isn’t perfect since it can add traffic to non-driven roads but it seems to work pretty well.

Landvalue is calculated by averaging the land-value of all of the neighboring tiles with a certain bonus depending on the built structure. Powerplants give off a huge negative bonus which is why residential units won’t move in there.

I wanted to program in move outs and derelict buildings and fire and a whole bunch of other stuff but I hit 4k much faster than I thought I would.

One of my real downfalls in making this was that I didn’t really think out how the graphics were going to be done when I started construction. Currently every cell has a representative short in an array which tell the program which sprite to draw for that cell. When a cell needs to be changed the graphic array is sent to gNeedsUpdate which then sorts out which image to draw. This wouldn’t be so bad if I had a 1 : 1 correspondence between buildingCode and graphicCodes but I don’t because I failed to plan. I’m pretty sure this code could have been condensed had I thought about it a bit more.

TODO

Tonight my plans are to remove the MouseUp and Down logic that was noted by Mickelukas. I was trying to get an approximation of a click event by keeping track of when the program recieved both a mouseup and mousedown in the same frame. By changing this to operate only on mousedown I should save some bytes that might let me get the minimap in.

I’m also going to change the BFS for the power-plant so that it doesn’t require the vistednode array-list.

@Archibald thanks for the Water Idea, that will be really small to implement compared to me fixing the array bounds checking since it actually happens in lots of places in the code.

Also I’ll change the colors :slight_smile:

Yes, you can make your code public anytime.

“Traffic is calculated when someone moves in.” - that explains the weird traffic behaviour (I first built all residentials and then built additioanl roads, but no one used it)

“Power is handled by doing a BFS every 60 or so frames” - why do you check power based on time? Shouldn’t it be event based (when something is built it set all neighbouring tiles as powered)?

What’s the point of landvalue? Does residential upgrade?

Pathfinding - I think the use of graph algorithms for such mini game is a waste. The map is rather small plus you have basicly unlimited memory, it would be more logical to use some fixed table algorithm like flood fill (since size is irrelevant). Plus it probably would let you save some bytes.

Power was checked based on time because I wasn’t thinking straight when I implemented it :slight_smile:

Land Value dictates whether or not a zone tile will be considered for buildup. If the land value is low, like near a power plant, no homes or comm businesses will move in. I wanted to make there be different classes of house but I’ll need to find a lot more space for that.

I’m not sure I understand your point about graph algorithms. My implementation of BFS is the iterative approach which is basically identical to a 4way Flood Fill.

while (!searchList.isEmpty()) {
                        Integer currCell = searchList.get(0);
                        power[currCell] = true;
                        searchList.remove(0);
                        for (i = 0; i < 4; i++) {
                            offset = upDownLeftRight[i];
                            if (currCell + offset > 0 && buildType[currCell + offset] > TRANSMITSPOWER) {
                                if (!power[currCell+offset])) { // This used to check a list of visited cells hence the slowdown 
                                    searchList.add(currCell + offset);
                                }
                            }
                        }

                    }

The traffic path-finding is basically the same as the above code but it instead of appending to the searchList we push. And it has to keep track of nodes visited.
If you can think if a better traffic approach I’d love to hear it. Especially if I can implement it quickly ;D