Difference between revisions of "LOS by Odd"

From RogueBasin
Jump to navigation Jump to search
 
m (I don't think category + subcategory are both needed at the same time.)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<pre>
<pre>
#include &ltiostream.h>
#include <iostream.h>
#include &ltstdio.h>
#include <stdio.h>
#include &ltconio.h>
#include <conio.h>
#include &ltmath.h>
#include <math.h>
#include &ltwindows.h>
#include <windows.h>


/* by odd lobster@takas.lt
/* by odd lobster@takas.lt
Line 11: Line 11:
*/
*/
int myMap[20][20]=  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
int myMap[20][20]=  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
        1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,
1,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
                    1,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
                    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};


void drawline_mod(int map[20][20], int x, int y, int x2, int y2) {
void drawline_mod(int map[20][20], int x, int y, int x2, int y2) {
     int dx = abs(x - x2);
     int dx = abs(x - x2);
     int dy = abs(y - y2);
     int dy = abs(y - y2);
     double s = double(.99/(dx&gtdy?dx:dy));
     double s = double(.99/(dx>dy?dx:dy));
     double t = 0.0;
     double t = 0.0;
     while(t < 1.0) {
     while(t < 1.0) {
Line 58: Line 58:


void draw_map(int map[20][20]) {
void draw_map(int map[20][20]) {
for (int y=0; y&lt20; y++) {
for (int y=0; y<20; y++) {
for (int x=0; x&lt20; x++) {
for (int x=0; x<20; x++) {
if (map[y][x] == 0) {
if (map[y][x] == 0) {
printf(" ");
printf(" ");
Line 90: Line 90:
By Odd [[lobster@takas.lt]]
By Odd [[lobster@takas.lt]]
</pre>
</pre>
[[category:LOS]][[category:FOV]]

Latest revision as of 06:48, 22 March 2009

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <windows.h>

/* by odd lobster@takas.lt
** map: 0-passable terrain(los can see), 1-block, 9-player start position(actualy this one isnt used), 5 - los can see, 3-player position
** ;)
*/
int myMap[20][20]=  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,
                     1,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

void drawline_mod(int map[20][20], int x, int y, int x2, int y2) {
    int dx = abs(x - x2);
    int dy = abs(y - y2);
    double s = double(.99/(dx>dy?dx:dy));
    double t = 0.0;
    while(t < 1.0) {
        dx = int((1.0 - t)*x + t*x2);
        dy = int((1.0 - t)*y + t*y2);
        if (map[dy][dx] != 1) {
			map[dy][dx] = 5;
		} else {
			return;
		}
        t += s; 
    }
}

void los(int map[20][20], int range, int plx, int ply) {
	int x, y;
	for (double f = 0; f < 3.14*2; f += 0.05) {
		x = int(range*cos(f)) + plx;
		y = int(range*sin(f)) + ply;
		drawline_mod(map,plx,ply,x,y);
	}
}

void draw_map(int map[20][20]) {
	for (int y=0; y<20; y++) {
		for (int x=0; x<20; x++) {
			if (map[y][x] == 0) {
				printf(" ");
			} else if (map[y][x] == 1) {
				printf("X");
			} else if (map[y][x] == 5) {
				printf(".");
			} else if (map[y][x] == 3) {
				printf("@");
			}
		}
		printf("\n");
	}
}

void main() {
	LARGE_INTEGER i1, i2, f;
	QueryPerformanceFrequency(&f);
	QueryPerformanceCounter(&i1); // start test
	los(myMap,99,8,6); // <- call los
	QueryPerformanceCounter(&i2); // end test
	int iii = 1000000*(i2.QuadPart - i1.QuadPart)/f.QuadPart;
	printf("Code executed in: %i/%i ms/ns\n", iii/1000, iii);
	myMap[6][8] = 3; // put player
	draw_map(myMap); // draw map ;)
	getch();
}


By Odd [[lobster@takas.lt]]