Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Changing Units
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jamonathin




PostPosted: Sun Apr 30, 2006 2:48 pm   Post subject: Changing Units

Hey all, I'm currently making an RTS game and I need to be able to kill off certain units.

Right now what im doing is basically:

pseudo:

for i : deadUnit . . allUnits
       unitStats (i) = unitStats (i + 1)
end for

allUnits -= 1


But since im testing my game i have about 200 units running around on the map. When a unit dies, the game lags like mad.

I actaully put in a lag-o-meter where it goes up to ~7000 milliseconds of lag - horrible, and thats only when a few die; imagine an assult.

Can anyone think of another method that will be much quicker?[/code]
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Apr 30, 2006 2:58 pm   Post subject: (No subject)

well what you're doing is shifting every unit to the right of the destroyed unit one space to the left. So if of 200 units, 1st one dies, you're ending up moving 199 records.

as long as order of units in the array is irrelavent, it would be much simpler to just move the very last record in place of the destroyed record and shrink the array by 1, thus removing the dublicate of that unit.

code:

unitStats (deadUnit) := unitStats (upper(unitStats))
new unitStats, upper(unitStats) - 1


more on flexible arrays[/code]
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Cervantes




PostPosted: Sun Apr 30, 2006 3:14 pm   Post subject: (No subject)

Still, 7 seconds of lag cannot be attributed to moving 199 things. 199 is a very small number to a computer.

Try the change tony suggested, but I can't see it making all that huge (7 seconds) a difference. If there is still considerable lag, try to think what else might be causing it and post that code (along with a little blurb, of course).

Also, wouldn't your for loop have to go from deadUnit .. allUnits - 1 ?
jamonathin




PostPosted: Sun Apr 30, 2006 3:57 pm   Post subject: (No subject)

Method works perfectly Tony - i guess i had that high-score method stuck in my head. There is no difference in the order so swaping it works fine. Good thinkin Smile.
Cervantes wrote:

but I can't see it making all that huge (7 seconds)

It strangely did for some reason.
Cervantes wrote:

Also, wouldn't your for loop have to go from deadUnit .. allUnits - 1 ?

Yes, sorry; that's how it was run.

Here's the code if you're interested. This code has a few different things in it, such as pathfinding (my own way i divised - not perfect), fog of war. You can change it here:
code:

Line: 95
 spotOpen (i, q) := Rand.Int (1, 1)  to .
 spotOpen (i, q) := 0


Line 105 (var c : flexible array 1 .. 15 of): to change unit count

And also the units start randomly - some starting on trees and water, so if a unit starts there he loses life. Also, since the pathfinding isn't perfect - the units will wander in sometimes - losing life then dieing.

I could google up pathfinding and find a way - but I'd rather figure it out. Anyways, have fun Razz.

PS> if you see anywhere i can reduce code/clean it up - lemme know Smile



unnamed.zip
 Description:

Download
 Filename:  unnamed.zip
 Filesize:  29.45 KB
 Downloaded:  80 Time(s)

TokenHerbz




PostPosted: Sun Apr 30, 2006 4:06 pm   Post subject: (No subject)

err that Math.Distance screwed me over yet again.

Anyways, if you want to make a really good, rts game, you should use class's. It'll save you alot of work. Im still learning them myself, but they seem to be usefull.
jamonathin




PostPosted: Sun Apr 30, 2006 4:24 pm   Post subject: (No subject)

Put this in C:\Program Files\Turing\Support\predefs or where ever your Turing is, it mite work Razz.


Math.tu
 Description:

Download
 Filename:  Math.tu
 Filesize:  4.96 KB
 Downloaded:  64 Time(s)

Cervantes




PostPosted: Sun Apr 30, 2006 6:18 pm   Post subject: (No subject)

Yes, TokenHerbz is right. Smile
OOP is a phenominal programming paradigm for large projects because it encapsulates and compartmentalizes and because inheritance and polymorphism help you to not repeat youself (follow the DRY principle).
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: