User:CloneDeath/Roguelike
From Wikipedia, the free encyclopedia
[edit] Roguelike-like Source (Tentative Title)
#include "stdafx.h" //include this only if you are using windows.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void refreshScreen(int screenx, int screeny);
int getMap(int x, int y);
int getDisc(int x, int y,int mode);
int getView(int sourceX,int sourceY, int pointX, int pointY, int viewDist, int fog);
int movePlayer(int screenx, int screeny);
void makePlayer();
struct PlayerChar
{
char name[20];
int maxhp;
int hp;
int x;
int y;
} player;
int getMap(int x, int y)
{
// _ is Floor
// W is Wall
// S is Solid (cannot walk over, but can see through)
// t will be Torch (working on)
register enum {_ = 0,
W = 1,
S = 2};
static int map1[20][20] = {W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W, //1
W,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,W, //2
W,_,_,_,_,W,_,W,_,_,S,S,S,S,S,S,S,_,_,W, //3
W,_,_,_,_,W,_,W,_,_,S,S,S,S,S,S,S,_,_,W, //4
W,_,_,_,_,W,_,W,_,_,S,S,S,S,S,S,S,_,_,W, //5
W,_,_,_,_,W,_,W,_,_,_,_,_,_,_,_,_,_,_,W, //6
W,W,W,W,W,W,_,W,W,W,W,W,W,W,W,W,W,W,W,W, //7
W,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,W, //8
W,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,W, //9
W,W,W,_,_,W,W,W,W,_,_,W,W,W,W,W,W,_,W,W, //10
W,_,_,_,_,_,W,W,_,_,_,_,_,W,W,_,_,_,_,W, //11
W,_,_,_,_,_,W,W,_,_,_,_,_,W,W,_,_,_,_,W, //12
W,_,_,_,_,_,W,W,_,_,_,_,_,W,W,_,_,_,_,W, //13
W,_,_,_,_,_,W,W,_,_,_,_,_,W,W,_,_,_,_,W, //14
W,_,_,_,_,_,W,W,_,_,_,_,_,W,W,_,_,_,_,W, //15
W,_,W,W,_,_,W,W,_,_,_,_,_,W,W,_,_,_,_,W, //16
W,_,_,W,_,_,W,W,_,_,_,_,_,W,W,_,_,_,_,W, //17
W,_,_,W,_,_,W,W,_,_,_,_,_,_,_,_,_,_,_,W, //18
W,_,_,W,_,_,W,W,_,_,_,_,_,W,W,_,_,_,_,W, //19
W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W};//20
return map1[y][x];
}
int getDisc(int x, int y,int mode)
{
static int Init = 1;
static int disc[20][20];
register int i, n, ret=1;
if (Init == 1)
{
Init = 0;
for (i=0; i<20; i++)
{
for (n=0; n<20; n++)
{
disc[i][n] = 1;
}
}
}
if (mode == -1)
{
ret = disc[x][y];
}
else
{
disc[x][y] = mode;
}
return ret;
}
int getView(int sourceX,int sourceY, int pointX, int pointY, int viewDist, int fog)
{
register enum {viewDiscovered = 177,
viewFogDiscovered = 178,
viewSolidWall = 219,
viewFog = 176,
viewEmpty = ' '};
register int rangeX,rangeY,range,ret,dirY,dirX;
register int i = 0, n = 0;
rangeX = pointX - sourceX;
rangeY = pointY - sourceY;
dirX = rangeX > 0 ? 1 : -1;
dirY = rangeY > 0 ? 1 : -1;
range = (rangeX*dirX) + (rangeY*dirY);
if (getDisc(pointX,pointY,-1) == 0)
{
ret = viewDiscovered;
}
else if (getDisc(pointX,pointY,-1) == 2)
{
ret = viewFogDiscovered;
}
else
{
ret = viewSolidWall;
}
for (n=0;n<=(rangeY*dirY);n++)
{
if (getMap(sourceX,sourceY + (dirY*n))==1)
{
break;
}
for (i=0; i<=(rangeX*dirX); i++)
{
if (getMap(sourceX + (dirX*i),sourceY + (dirY*n))==1)
{
break;
}
else if ((i == (rangeX*dirX)) && (n == (rangeY*dirY)))
{
ret = viewEmpty;
if (range <= viewDist)
{
getDisc(pointX,pointY,0);
}
else if ((range <= viewDist+fog) && getDisc(pointX,pointY,-1) != 0)
{
getDisc(pointX,pointY,2);
}
}
}
}
if (ret != viewEmpty)
{
n = 0;
i = 0;
for (i=0;i<=(rangeX*dirX);i++)
{
if (getMap(sourceX + (dirX*i),sourceY)==1)
{
break;
}
for (n=0; n<=(rangeY*dirY); n++)
{
if (getMap(sourceX + (dirX*i),sourceY + (dirY*n))==1)
{
break;
}
else if ((i == (rangeX*dirX)) && (n == (rangeY*dirY)))
{
ret = viewEmpty;
if (range <= viewDist)
{
getDisc(pointX,pointY,0);
}
else if ((range <= viewDist+fog) && getDisc(pointX,pointY,-1) != 0)
{
getDisc(pointX,pointY,2);
}
}
}
}
}
if (getMap(pointX,pointY))
{
ret = viewSolidWall;
}
else if (range > viewDist)
{
if ((range <= (viewDist+fog)) && (ret != viewSolidWall) && (ret != viewDiscovered) && (ret != viewFogDiscovered))
{
ret = viewFog;
}
else if (getDisc(pointX,pointY,-1) == 0)
{
ret = viewDiscovered;
}
else if (getDisc(pointX,pointY,-1) == 2)
{
ret = viewFogDiscovered;
}
else
{
ret = viewSolidWall;
}
}
return ret;
}
int movePlayer(int screenx, int screeny)
{
register int ret;
char playerKeys[10];
scanf("%10s", &playerKeys);
switch(playerKeys[0])
{
case 'l': ;
case 'L':
if ((player.x > 0) && (getMap(player.x-1,player.y) == 0))
{
player.x -= 1;
}
break;
case 'r': ;
case 'R':
if ((player.x < screenx-1) && (getMap(player.x+1,player.y)==0))
{
player.x += 1;
}
break;
case 'u': ;
case 'U':
if ((player.y > 0) && (getMap(player.x,player.y-1)==0))
{
player.y -= 1;
}
break;
case 'd': ;
case 'D':
if ((player.y < screeny -1) && (getMap(player.x,player.y+1)==0))
{
player.y += 1;
}
break;
case 'e': ;
case 'Q': ;
case 'E': ;
case 'q':
ret = 1;
break;
default:;
}
return ret;
}
void refreshScreen(int screenx, int screeny)
{
register int i = 0;
register int n = 0;
char newScreen[1000] = "Type l/r/u/d to move. Type 'Q' to exit.\n";
//char newScreen[1000] = {"Type l/r/u/d to move. Type 'Q' to exit.\n",'H','P',
// ':',' ',;
register int at = 40;
newScreen[at++] = '\n';
for (n=0;n<screeny;n++)
{
for (i=0; i<screenx; i++)
{
if ((player.x == i) && (player.y == n))
{
//Player
newScreen[at++] = 1; //241 kinda looks like a person.
}
else
{
newScreen[at++] = getView(player.x,player.y,i,n,4,2);
}
}
newScreen[at++] = '\n';
}
newScreen[at++] = '\n';
newScreen[at++] = '\n';
newScreen[at++] = '\0';
printf("%s",newScreen);
}
void makePlayer ()
{
FILE *fchar;
fchar = fopen("Character.txt", "r");
fgets(player.name,20,fchar);
player.hp = fgetc(fchar);
player.maxhp = fgetc(fchar);
printf("Before: \n Name: %s HP: %i / %i \n",player.name,player.hp,player.maxhp);
printf("Hello, please enter your name:\n");
gets(player.name);
printf("I see, %s, what an odd name. It is so stupid,\n",player.name);
printf("that I will throw you into a generic room! Ha ha ha!\n");
player.hp = 10;
player.maxhp = 10;
printf("After: Name: %s\nHP: %i/%i\n",player.name,player.hp,player.maxhp);
fchar = fopen("Character.txt", "w");
fputs(player.name, fchar);
fputc('\n', fchar);
fputc((int) player.hp, fchar);
fputc((int) player.maxhp, fchar);
fclose(fchar);
}
void main()
{
register int GExit = 0;
register int roomx = 20;
register int roomy = 20;
makePlayer();
player.x = 1;
player.y = 1;
refreshScreen ( roomx , roomy );
while (1)
{
GExit = movePlayer ( roomx , roomy );
refreshScreen ( roomx , roomy );
if (GExit != 0)
{
break;
}
}
}