forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
3,714 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,295 @@ | ||
#include "tileray.h" | ||
#include <math.h> | ||
#include <stdlib.h> | ||
|
||
static const int sx[4] = { 1, -1, -1, 1 }; | ||
static const int sy[4] = { 1, 1, -1, -1 }; | ||
|
||
tileray::tileray (): deltax(0), deltay(0), direction(0), leftover (0), | ||
last_dx(0), last_dy(0), infinite (false), steps(0) | ||
{ | ||
} | ||
|
||
tileray::tileray (int adx, int ady) | ||
{ | ||
init (adx, ady); | ||
} | ||
|
||
tileray::tileray (int adir): direction (adir) | ||
{ | ||
init (adir); | ||
} | ||
|
||
void tileray::init (int adx, int ady) | ||
{ | ||
leftover = 0; | ||
last_dx = 0; | ||
last_dy = 0; | ||
deltax = adx; | ||
deltay = ady; | ||
if (!adx && !ady) | ||
direction = 0; | ||
else | ||
direction = (int) (atan2 (deltay, deltax) * 180.0 / M_PI); | ||
infinite = false; | ||
steps = 0; | ||
} | ||
|
||
void tileray::init (int adir) | ||
{ | ||
leftover = 0; | ||
direction = adir; | ||
if (direction -0) | ||
direction += 360; | ||
if (direction >= 360) | ||
direction -= 360; | ||
last_dx = 0; | ||
last_dy = 0; | ||
deltax = abs((int) (cos ((float) direction * M_PI / 180.0) * 100)); | ||
deltay = abs((int) (sin ((float) direction * M_PI / 180.0) * 100)); | ||
infinite = true; | ||
steps = 0; | ||
} | ||
|
||
int tileray::dx () | ||
{ | ||
return last_dx; | ||
} | ||
|
||
int tileray::dy () | ||
{ | ||
return last_dy; | ||
} | ||
|
||
int tileray::dir () | ||
{ | ||
return direction; | ||
} | ||
|
||
int tileray::dir4 () | ||
{ | ||
if (direction >= 45 && direction <= 135) | ||
return 1; | ||
else | ||
if (direction > 135 && direction < 225) | ||
return 2; | ||
else | ||
if (direction >= 225 && direction <= 315) | ||
return 3; | ||
else | ||
return 0; | ||
} | ||
|
||
char tileray::dir_symbol (char sym) | ||
{ | ||
switch (sym) | ||
{ | ||
case 'j': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return 'h'; | ||
case 1: return 'j'; | ||
case 2: return 'h'; | ||
case 3: return 'j'; | ||
} | ||
case 'h': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return 'j'; | ||
case 1: return 'h'; | ||
case 2: return 'j'; | ||
case 3: return 'h'; | ||
} | ||
case 'y': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return 'u'; | ||
case 1: return 'n'; | ||
case 2: return 'b'; | ||
case 3: return 'y'; | ||
} | ||
case 'u': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return 'n'; | ||
case 1: return 'b'; | ||
case 2: return 'y'; | ||
case 3: return 'u'; | ||
} | ||
case 'n': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return 'b'; | ||
case 1: return 'y'; | ||
case 2: return 'u'; | ||
case 3: return 'n'; | ||
} | ||
case 'b': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return 'y'; | ||
case 1: return 'u'; | ||
case 2: return 'n'; | ||
case 3: return 'b'; | ||
} | ||
case '^': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return '>'; | ||
case 1: return 'v'; | ||
case 2: return '<'; | ||
case 3: return '^'; | ||
} | ||
case '[': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return '-'; | ||
case 1: return '['; | ||
case 2: return '-'; | ||
case 3: return '['; | ||
} | ||
case ']': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return '-'; | ||
case 1: return ']'; | ||
case 2: return '-'; | ||
case 3: return ']'; | ||
} | ||
case '|': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return '-'; | ||
case 1: return '|'; | ||
case 2: return '-'; | ||
case 3: return '|'; | ||
} | ||
case '-': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return '|'; | ||
case 1: return '-'; | ||
case 2: return '|'; | ||
case 3: return '-'; | ||
} | ||
case '=': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return 'H'; | ||
case 1: return '='; | ||
case 2: return 'H'; | ||
case 3: return '='; | ||
} | ||
case 'H': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return '='; | ||
case 1: return 'H'; | ||
case 2: return '='; | ||
case 3: return 'H'; | ||
} | ||
case '\\': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return '/'; | ||
case 1: return '\\'; | ||
case 2: return '/'; | ||
case 3: return '\\'; | ||
} | ||
case '/': | ||
switch (dir4()) | ||
{ | ||
default: | ||
case 0: return '\\'; | ||
case 1: return '/'; | ||
case 2: return '\\'; | ||
case 3: return '/'; | ||
} | ||
default:; | ||
} | ||
return sym; | ||
} | ||
|
||
int tileray::ortho_dx (int od) | ||
{ | ||
int quadr = (direction / 90) % 4; | ||
od *= -sy[quadr]; | ||
return mostly_vertical()? od : 0; | ||
} | ||
|
||
int tileray::ortho_dy (int od) | ||
{ | ||
int quadr = (direction / 90) % 4; | ||
od *= sx[quadr]; | ||
return mostly_vertical()? 0 : od; | ||
} | ||
|
||
bool tileray::mostly_vertical () | ||
{ | ||
return abs(deltax) <= abs(deltay); | ||
} | ||
|
||
void tileray::advance (int num) | ||
{ | ||
last_dx = last_dy = 0; | ||
int ax = abs(deltax); | ||
int ay = abs(deltay); | ||
int anum = abs (num); | ||
for (int i = 0; i < anum; i++) | ||
{ | ||
if (mostly_vertical ()) | ||
{ // mostly vertical line | ||
leftover += ax; | ||
if (leftover >= ay) | ||
{ | ||
last_dx++; | ||
leftover -= ay; | ||
} | ||
last_dy++; | ||
} | ||
else | ||
{ // mostly horizontal line | ||
leftover += ay; | ||
if (leftover >= ax) | ||
{ | ||
last_dy++; | ||
leftover -= ax; | ||
} | ||
last_dx++; | ||
} | ||
steps++; | ||
} | ||
|
||
// offset calculated for 0-90 deg quadrant, we need to adjust if direction is other | ||
int quadr = (direction / 90) % 4; | ||
last_dx *= sx[quadr]; | ||
last_dy *= sy[quadr]; | ||
if (num < 0) | ||
{ | ||
last_dx = -last_dx; | ||
last_dy = -last_dy; | ||
} | ||
} | ||
|
||
bool tileray::end () | ||
{ | ||
if (infinite) | ||
return true; | ||
return mostly_vertical()? steps >= abs(deltay) - 1 : steps >= abs(deltax) - 1; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef _TILERAY_H_ | ||
#define _TILERAY_H_ | ||
|
||
// Class for calculating tile coords | ||
// of a point that moves along the ray with given | ||
// direction (dir) or delta tile coords (dx, dy). | ||
// Advance method will move to the next tile | ||
// along ray | ||
// Direction is angle in degrees from positive x-axis to positive y-axis: | ||
// | ||
// | 270 orthogonal dir left (-) | ||
// 180 | 0 ^ | ||
// ----+----> X -------> X (forward dir, 0 degrees) | ||
// | v | ||
// V 90 orthogonal dir right (+) | ||
// Y | ||
class tileray | ||
{ | ||
private: | ||
int deltax; // ray delta x | ||
int deltay; // ray delta y | ||
int leftover; // counter to shift coords | ||
int direction; // ray direction | ||
int last_dx; // dx of last advance | ||
int last_dy; // dy of last advance | ||
int steps; // how many steps we advanced so far | ||
bool infinite; // ray is infinite (end will always return true) | ||
public: | ||
tileray (); | ||
tileray (int adx, int ady); | ||
tileray (int adir); | ||
|
||
void init (int adx, int ady); // init ray with dx,dy | ||
void init (int adir); // init ray with direction | ||
|
||
int dx (); // return dx of last advance (-1 to 1) | ||
int dy (); // return dy of last advance (-1 to 1) | ||
int dir (); // return direction of ray (degrees) | ||
int dir4 (); // return 4-sided direction (0 = east, 1 = south, 2 = west, 3 = north) | ||
char dir_symbol (char sym); // convert certain symbols from north-facing variant into current dir facing | ||
int ortho_dx (int od); // return dx for point at "od" distance in orthogonal direction | ||
int ortho_dy (int od); // return dy for point at "od" distance in orthogonal direction | ||
bool mostly_vertical (); // return if ray is mostly vertical | ||
|
||
void advance (int num = 1); // move to the next tile (calculate last dx, dy) | ||
bool end (); // do we reach the end of (dx,dy) defined ray? | ||
}; | ||
|
||
#endif |
Oops, something went wrong.