Sheeps4k

Here is my 2nd submision to the contest:

http://flx.proyectoanonimo.com/proyectos/webstart/sheeps4k/preview.jpg

In this game, you are a shepherd dog in charge of a flock of sheeps and you have to guide them to the stable door before the time runs while you mantain the wolves away from them.

In fact, the game more a prototype than a full game yet, but it’s still playable ::slight_smile: . I tried to implements some steering behaviors patterns, but i had to cut out many things to fit in :-\

however, there are some bytes left, and is litle optimized, so any feature request / bug report is apreciated.

size 3853 bytes. 3 levels. 3825B. unlimited levels. tested on linux and winxp.

play the game :slight_smile: (server)

Nice game!

The wolves are no match really, you just hunt 'em down and you’re safe. Wolves should be faster than the player, and to compensate they should keep a larger distance from the player.

You can save 1 byte by changing “enought” to “enough” :stuck_out_tongue:

ok, updated

now the wolves are quite a challenge, they are imortal, but they will flee you more effectively if you pursuit them, and they can go out of the screen bounds, so they might be there even if you can’t see them ::slight_smile:

added generated levels, not as fun, but it saved some bits.

Very cool game! I got through the first two levels, but the 3rd level I tried multiple times and never even got close hahaha. There was always one wolf I couldn’t get near. If I was near one, or maybe 2, the 3rd one was free to play. So essentially I had a constant decrease in sheep and if I dealt with wolves, I was wasting my time and sheep ended up scattered all over the screen. If I tried to herd and minimally scare off wolves, I wasn’t fast enough to get the % of sheep I needed by a long shot. Maybe I just stink, but between levels 2 and 3 that’s quite a difficult learning curve :slight_smile:

Neat game.

Yeah, neat!

Though with 400 sheep it really slowed down. I suspect you haven’t done anything to try to optimize the number of sheep-to-sheep “attraction” calculations. Not that you have a lot of options with 4k.

I ran it on my computer at home and the game is slow like hell, which is weird because my home pc is more powerful than my work pc.

P4 2.6ghz, 1 gig ram
ATI 9600xt 256mb ram
windows xp

i noticed that on some windows boxes, it becomes slow win more than 400 sheeps. In linux ran fine with >750 sheeps

i’ll trade drawing the actors with something more ellaborated than ovals for some optimization :slight_smile:

great game i enjoyed it alot, as for performance you may want to try

<property name="sun.java2d.noddraw" value="true"/>

flag, usually helps improve the performance by a little.

ok, updated the jnlp with the noddraw property and the -server vm argument, see if it makes it playable with >400 sheeps

i’ll upload now a easier version (less sheeps required to pass)

The game requires the “server” JVM, but I only have the “client” installed here (i.e. the JRE only). You might want to change the jnlp to start in client mode.

I just get an error message! Cannot run the game. :frowning:

ok, that was a random shot uploaded another jnlp. However, the one with the -server argument is here

the -server arguement isn’t really a good idea to use with games as it won’t really give a performance enhancement instead it usually does the opposite for games. since it takes a few minutes for the optimisations to kick in.

I’m having problems to optimize the sheep to sheep loop due the data structures i chosen to use.
the loop looks like this:


for(int i = 0; i < sheep.length; i += 4){
	if(sheep[i] == -1)
		continue;
	desired_vx = desired_vy = avg_x = avg_y = num_on_range = 0;
	// fetch data from the nearby sheep
	for (int o = 0; o < sheep.length; o += 4) {
		dist = (float) Point.distance(sheep[o], sheep[o + 1], sheep[i], sheep[i + 1]) + 0.1f;
		if (dist < SHEEP_VIEW_RANGE && (o != i) && sheep[o] != -1) {
			num_on_range++;
			//cumulate the repeling force for this sheep
			desired_vx -= ((sheep[o] - sheep[i]) / dist);
			desired_vy -= ((sheep[o + 1] - sheep[i + 1]) / dist);
			//cumulate the average center of the nearby crowd
			avg_x += sheep[o];
			avg_y += sheep[o + 1];
		}
	}
	[...]
}

I tried to loop only for some sheeps before and after the ‘i’ sheep, but without ordering the array it doesn’t work, and ordering each frame kills the game.

I also tried to check if the distance on the x or y was too much but it seems that any checking kills the performance if there are more than 1000 sheeps :frowning:

any idea

Restructuring the data would be necessary to get any big benefit… though there are some minor opimization that can be done with your existing loop.

Computing the distances is one of the more expensive operations. There are ways to speed it up:


dist = (float) Point.distance(sheep[o], sheep[o + 1], sheep[i], sheep[i + 1]) + 0.1f;
if (dist < SHEEP_VIEW_RANGE && (o != i) && sheep[o] != -1) {

First check o!=i and sheep[o] != -1 BEFORE computing the distance.

Instead of using Point.distance do your own computation… but leave off the sqrt just compute (dx^2 + dy^2). Then compare to squared SHEEP_VIEW_RANGE^2 instead.

Then only inside the if block do you need the sqrt, AND you can save the dx & dy values in local vars so they don’t need to be recalculated when computing desired_vx & desired_vy.

Hmm nice game, but very ambitous with the (very cool) gravitational pull aspect. The giant “sheep balls” are interesting to watch. Too bad it begins running slowly around 500 sheep, as you’re already aware.

Sorry I can’t really think of anything else to help out besides what swpalmer already said.

Only thing that crosses my mind, though you’re probably already doing this, is to make sure you’re not drawing two circles for each sheep, and you’re just pasting a pregenerated image where needed. Java circles are pretty slow.

Runs fine on my machine. I only made it to the 4th level. 750 sheep I think. Too hard with 3 wolves. Maybe you could have the wolves fight each other a little bit as a distraction for them. Overall well done. Especially like the flocking.

AMD Athlon64 3000+
512MB Dual channel
Radeon 9600PRO

  1. you don’t need to sort the entire array every frame, only about 1x a second I’d guess
  2. you could use another array with indexes into the sheep array to implement a quad tree or something like that…

The game is pretty cool as is, do you really need more sheep?

;D

Very nice and fun game! Ran fine on my 2Ghz 512mb RAM Windows XP Pro. box.

No wonder if is slow, you are performing sheep.length*sheep.length square roots!!!

How about you change this line :-


dist = (float) Point.distance(sheep[o], sheep[o + 1], sheep[i], sheep[i + 1]) + 0.1f;
if (dist < SHEEP_VIEW_RANGE && (o != i) && sheep[o] != -1) {

into this :-


dist = (float) Point.distanceSq(sheep[o], sheep[o + 1], sheep[i], sheep[i + 1]);
if (dist < (SHEEP_VIEW_RANGE-0.1f)*(SHEEP_VIEW_RANGE-0.1f) && (o != i) && sheep[o] != -1) {
   dist = Math.sqrt(dist) + 0.1f

It will remove a considerable number of sqrt’s, with only a small size cost.

It’d be even better if you can change the desired_vx stuff inside the if, so it can use the square of the distance.