8 Replies - 456 Views - Last Post: 31 January 2012 - 07:27 AM Rate Topic: -----

Topic Sponsor:

#1 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Tower Defence tower efficiency

Posted 31 January 2012 - 06:29 AM

When I had created a simple TD game in java, when I had multiple towers and monsters the game started to lag ( maybe with at least 10-15 of each tower and monster.

I think this would have to do with iterating through each tower then iterating through all monsters to decide which one it should attack - it also needs to calculate the angle towards the monster from the towers position. (lots of iterations and calculations)

Is there any algorithms or alternate methods that are used to decide which tower should attack which monster?

This post has been edited by Mylo: 31 January 2012 - 06:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Tower Defence tower efficiency

#2 Tayacan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 33
  • View blog
  • Posts: 116
  • Joined: 18-January 11

Re: Tower Defence tower efficiency

Posted 31 January 2012 - 06:40 AM

Well... The easy way would be to just always let the towers attack the foremost monster in reach, if that makes sense. That way you just let each tower attack the front monster when it gets in range, and when it gets out of range, you switch to the next monster. That's a suggestion, at any rate.
Was This Post Helpful? 1
  • +
  • -

#3 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: Tower Defence tower efficiency

Posted 31 January 2012 - 06:44 AM

That would be an improvement, I may try it

but maybe not so much, I'd have to check each monster if it's not been destroyed, then check if its in range. At the moment it attacking anywhere on the map

This post has been edited by Mylo: 31 January 2012 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 Serapth  Icon User is offline

  • D.I.C Head


Reputation: 49
  • View blog
  • Posts: 145
  • Joined: 17-August 11

Re: Tower Defence tower efficiency

Posted 31 January 2012 - 06:46 AM

Even with that many calculations, unless you had some bug in your code, that should not have caused your game to slow to a halt. Your computer is literally capable of millions of such calculations per second. The most likely culprit is you were copy a rather large piece of data, or something similar, with each calculation.

Even with 15 towers checking against 15 monsters, that's only 225 iterations. Now if you were doing a factorial test ( 15x15 + 15x14 + 15x13... ), well yeah, thats going to bring your computer to it's knees.

Can you post the code in question ( the compare/evaluate code ), or is it too long?
Was This Post Helpful? 1
  • +
  • -

#5 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: Tower Defence tower efficiency

Posted 31 January 2012 - 06:59 AM

The code is like 30 classes. I'm not sure how to export the jar with image files in it either.

But maybe you are right in that it could very well be the design of the application. I did this back a while ago when I was just beginning java and made my data static, the monsters are a static array of Monster classes which according to my own tests gave slower access, though I could be wrong.

this is the paint method where it is calculated (I may be able to put this in another thread, it could be holding up the paint event)

Data.map is the tiles
Data.monsters are monsters
Data.mx/my is location of mouse

	@Override
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 15; j++)
			{
				if (Data.map[i][j] != null)
				{
					Data.map[i][j].paint(g);

				}
			}

		}

		for (int i = 0; i < Data.monster.length; i++)
		{
			if (Data.monster[i] != null)
			{
				Data.monster[i].paint(g);
			}
		}

		// Shows if the a red line should be drawn to the monster from tower
		if (Data.lasers == true)
		{
			for (int i = 0; i < 10; i++)
			{
				for (int j = 0; j < 15; j++)
				{
					// false = face monster
					if (Data.face == false)
					{
						g.setColor(Color.red);
						for (int k = 0; k < Data.monster.length; k++)
						{
							if (Data.monster[k] != null)
							{

								g.drawLine(Data.map[i][j].xloc + Data.tilewidth / 2, Data.map[i][j].yloc + Data.tileheight / 2, Data.monster[k].xloc, Data.monster[k].yloc);
							}
						}
					}
					// true = face mouse
					else
					{

						g.setColor(Color.red);
						g.drawLine(Data.map[i][j].xloc + Data.tilewidth / 2, Data.map[i][j].yloc + Data.tileheight / 2, Data.mx, Data.my);

					}

				}

			}
		}

	}



When I get time I will approach it in OOP style, I expect I can make it perform better than.

This post has been edited by Mylo: 31 January 2012 - 07:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 Serapth  Icon User is offline

  • D.I.C Head


Reputation: 49
  • View blog
  • Posts: 145
  • Joined: 17-August 11

Re: Tower Defence tower efficiency

Posted 31 January 2012 - 07:12 AM

Traditionally um... traditional drawing code ( standard Java calls like Graphics.Drawline or .Paint() ) are BRUTALLY slow. This itself could be a huge part of your slowdown.

I wouldn't be surprised if you switched to an accelerated graphics library such as Slick2D or even Java2D, you would see a MASSIVE improvement in your games performance.
Was This Post Helpful? 1
  • +
  • -

#7 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: Tower Defence tower efficiency

Posted 31 January 2012 - 07:16 AM

That is something I've been meaning to try actually, I have tried before, but I always seem to have trouble adding additional libraries to projects. Even if it's not eclipse, and Visual Studio or something.

But it's worth a shot anyway. I think I'll take the OOP and Slick2D approach first before complaining things are slow.

Thanks a lot =)

This post has been edited by Mylo: 31 January 2012 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

#8 Serapth  Icon User is offline

  • D.I.C Head


Reputation: 49
  • View blog
  • Posts: 145
  • Joined: 17-August 11

Re: Tower Defence tower efficiency

Posted 31 January 2012 - 07:21 AM

Unlike many, I am actually an OOP advocate, but let me just say...


Never look to object oriented programming for performance reasons.... ;)


( Not saying not to do it, I very much think thats a good next step. It just wont make your code any faster. It should however make it more maintainable )
Was This Post Helpful? 2
  • +
  • -

#9 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: Tower Defence tower efficiency

Posted 31 January 2012 - 07:27 AM

I have heard that before, thanks for reminding me. But the maintainance will definately be good since it's a big static gui/logic mixed code atm.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1