Author Topic: Op2 Distance Graph  (Read 2239 times)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Op2 Distance Graph
« on: January 01, 2008, 08:32:23 PM »
Ever wondered just how exactly OP2 determines if two units are within weapons range of each other? The actual calculation isn't the usual distance formula since that involves a square root, which is a bit of a slow algorithm. Instead, it uses a somewhat faster approximation, which results in non circular distance pattern.


I decided to make a quick graph to see what it really looks like. The image just uses the distance from the current pixel to the center (as calculated by OP2) as the color of that pixel. It results in shades of red, with 0 being close, and bright red being far, and wraps back around to black once it gets far enough away. Basically rings of the same color are considered the same distance.



Here's the distance calculation:
distX = Abs(x1 - x2)
distY = Abs(y1 - y2)
Dist = (min(distX, distY) / 2) + max(distX, distY)
 

Offline BlackBox

  • Administrator
  • Hero Member
  • *****
  • Posts: 3093
Op2 Distance Graph
« Reply #1 on: January 01, 2008, 10:09:27 PM »
Interesting. I suppose this is sort of irrelevant now since modern CPUs can probably do sqrt operations a lot faster than the ones back when OP2 was created (wasn't OP2 able to run on a 486 anyway, which would necessitate code that doesn't use the FPU (since the FPU is not present on the 486SX)?)

Even if you were doing sqrt several times a second on a modern CPU I wouldn't think there would be much of a performance penalty.

Offline Freeza-CII

  • Administrator
  • Hero Member
  • *****
  • Posts: 2308
Op2 Distance Graph
« Reply #2 on: January 01, 2008, 10:20:32 PM »
Im glad hoomans has only one hobby. :P

Thats pretty interesting.

Offline Savant 231-A

  • Sr. Member
  • ****
  • Posts: 486
    • https://www.outpost2.net
Op2 Distance Graph
« Reply #3 on: January 02, 2008, 02:49:16 AM »
Quote
Ever wondered just how exactly OP2 determines if two units are within weapons range of each other? The actual calculation isn't the usual distance formula since that involves a square root, which is a bit of a slow algorithm. Instead, it uses a somewhat faster approximation, which results in non circular distance pattern.


I decided to make a quick graph to see what it really looks like. The image just uses the distance from the current pixel to the center (as calculated by OP2) as the color of that pixel. It results in shades of red, with 0 being close, and bright red being far, and wraps back around to black once it gets far enough away. Basically rings of the same color are considered the same distance.



Here's the distance calculation:
distX = Abs(x1 - x2)
distY = Abs(y1 - y2)
Dist = (min(distX, distY) / 2) + max(distX, distY)
It is interesting how much often someone finds some kind of bugs and other stuff (remember when Hooman found a bug, when you research a tech in which sticky foam should stay longer on enemy vehicles, it actually stays longer on your own vehicles!)

However, still, I don't understand even one word that Hooman wrote.
Prolly thats why I am not a programmer :P
Gordon Freeman, and mr. Crowbar would own Master Chief in any part of the day.
"Come here citizen."

"From the ashes of the collapse we seek to build a better world for all."

Offline Drakmar

  • Sr. Member
  • ****
  • Posts: 251
Op2 Distance Graph
« Reply #4 on: January 02, 2008, 03:05:46 AM »
Quote
Interesting. I suppose this is sort of irrelevant now since modern CPUs can probably do sqrt operations a lot faster than the ones back when OP2 was created (wasn't OP2 able to run on a 486 anyway, which would necessitate code that doesn't use the FPU (since the FPU is not present on the 486SX)?)

Even if you were doing sqrt several times a second on a modern CPU I wouldn't think there would be much of a performance penalty.
Actually, while I'm not 100% sure whether or not OP2 requires a FPU, the Operating System that it ran on does (Win95), so it was only able to run on CPUs with a FPU at that time. Now, if you could get it to run on WFW3.11 with Win32 APIs, then we could find out if it needed one or not. Just my 2 cents worth.

Offline BlackBox

  • Administrator
  • Hero Member
  • *****
  • Posts: 3093
Op2 Distance Graph
« Reply #5 on: January 02, 2008, 05:38:31 PM »
Quote
Actually, while I'm not 100% sure whether or not OP2 requires a FPU, the Operating System that it ran on does (Win95), so it was only able to run on CPUs with a FPU at that time. Now, if you could get it to run on WFW3.11 with Win32 APIs, then we could find out if it needed one or not. Just my 2 cents worth.
Untrue. Officially, Win95 required a 386DX (which does not include an FPU. You needed to install a 387 math coprocessor to get an FPU). Win95 could run on a 386SX I hear, but very poorly.
If an FPU was really "needed," the OS probably had (slower) functions to emulate the floating point unit.

As far as running it on Win32s (32 bit subsystem for Win3.1), this wouldn't be possible since thread creation doesn't work. (Well, I suppose the game -might- run in single player mode but multiplayer would not work -- the game uses a second thread for the network receive loop).

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Op2 Distance Graph
« Reply #6 on: January 02, 2008, 06:18:03 PM »
Of course, since OP2's units are all locked to a grid, it probably doesn't matter that the range graph is 100% circular.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Leviathan

  • Hero Member
  • *****
  • Posts: 4055
Op2 Distance Graph
« Reply #7 on: January 03, 2008, 12:50:54 PM »
Cool beans :)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Op2 Distance Graph
« Reply #8 on: January 03, 2008, 02:47:05 PM »
They can just use the invalid opcode exception to emulate FPU instructions that are missing. The newer CPUs have the floating point built in though, instead of using a seperate chip as a coprocessor. I don't see how it'd be a big deal for an OS. Just that the OS needs to know what registers to save during a task switch.


As for the graph, it does matter a little, since units are only locked to the grid when they stop. When they move between tiles, it'll affect when they fire as they are moving. Many things done internally are pixel based. There's just a lot of interface points where it forces tile measurements on you, and the occasional data structure that's meant to work with tiles. Certainly understandable for buildings though, especially with the tube connections.



Edit: Since you seem to be interested in tiles, I thought I'd make a few more images.  :)

Tile Granularity


Matching Pixel Granularity


Contour Lines

 
« Last Edit: January 03, 2008, 03:20:04 PM by Hooman »

Offline Arklon

  • Administrator
  • Hero Member
  • *****
  • Posts: 1269
Op2 Distance Graph
« Reply #9 on: January 03, 2008, 03:28:27 PM »
That last one looks trippy.

Offline Drakmar

  • Sr. Member
  • ****
  • Posts: 251
Op2 Distance Graph
« Reply #10 on: January 03, 2008, 05:00:32 PM »
Quote
Quote
Actually, while I'm not 100% sure whether or not OP2 requires a FPU, the Operating System that it ran on does (Win95), so it was only able to run on CPUs with a FPU at that time. Now, if you could get it to run on WFW3.11 with Win32 APIs, then we could find out if it needed one or not. Just my 2 cents worth.
Untrue. Officially, Win95 required a 386DX (which does not include an FPU. You needed to install a 387 math coprocessor to get an FPU). Win95 could run on a 386SX I hear, but very poorly.
If an FPU was really "needed," the OS probably had (slower) functions to emulate the floating point unit.

As far as running it on Win32s (32 bit subsystem for Win3.1), this wouldn't be possible since thread creation doesn't work. (Well, I suppose the game -might- run in single player mode but multiplayer would not work -- the game uses a second thread for the network receive loop).
Oh ok. I guess I was mistaken then. I haven't read up on Windows 95 in a long time, so it's possible that I confused it with one of the many other OS I've used over the years.

Offline Freeza-CII

  • Administrator
  • Hero Member
  • *****
  • Posts: 2308
Op2 Distance Graph
« Reply #11 on: January 03, 2008, 09:29:42 PM »
That last one would look great on a passive matrix screen.

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Op2 Distance Graph
« Reply #12 on: January 03, 2008, 09:40:02 PM »
That last one makes me see the universe.
True though, I forgot about when units were moving...
That explains how you gain that extra tile of range when you're running away.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials