353 lines
12 KiB
C++
353 lines
12 KiB
C++
/*todo:
|
|
- find out why it breaks on resize since it communicates with server
|
|
- take care of tty settings (show input on beginning)
|
|
- enable multiple connections (needs more memory allocation? works from different computers?)
|
|
- "Test" gets ID 9 while it actually has ID 0. all the other ones are therefore actual ID-1.. fix!
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <sys/ioctl.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <chrono>
|
|
#include <thread>
|
|
#include <cmath>
|
|
#include <stdlib.h>
|
|
#include <ncurses.h>
|
|
#include <pthread.h>
|
|
#include <termios.h>
|
|
#include <stdbool.h>
|
|
#include <sys/select.h>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <sys/socket.h>
|
|
#include <string.h>
|
|
#include <arpa/inet.h>
|
|
#define PORT 5210
|
|
|
|
using namespace std;
|
|
using namespace std::chrono;
|
|
|
|
string screen;
|
|
string s_borderLine = "";
|
|
string s_keyPressed;
|
|
|
|
float fPlayerX = 8.0f;
|
|
float fPlayerY = 8.0f;
|
|
float fPlayerA = 0.0f;
|
|
float fFOV = 3.14159 / 4.0;
|
|
float fDepth = 32.0f;
|
|
float fElapsedTime;
|
|
|
|
int i_state = 0; // 0 = server select, 1 = citizen select, 2 = gameloop
|
|
int nScreenWidth;
|
|
int nScreenHeight;
|
|
int nFrameRate = 60;
|
|
int i_miniMapSize = 18;
|
|
|
|
bool bMoveForward;
|
|
bool bMoveBackwards;
|
|
bool bTurnLeft;
|
|
bool bTurnRight;
|
|
bool b_miniMap;
|
|
bool b_debug;
|
|
bool bGameLoop = true;
|
|
|
|
|
|
|
|
void *keyListener(void *tid){
|
|
long id = (long) tid;
|
|
initscr();
|
|
noecho();
|
|
//raw();
|
|
//cbreak();
|
|
//nodelay(stdscr, TRUE);
|
|
//scrollok(stdscr, TRUE);
|
|
int c;
|
|
while ((c = getch()) != ERR){
|
|
s_keyPressed = "";
|
|
s_keyPressed+=to_string(c);
|
|
switch (c) {
|
|
case 100: //d
|
|
b_debug = !b_debug;
|
|
break;
|
|
case 109: //m
|
|
b_miniMap = !b_miniMap;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
void writeStringToScreen(int x, int y, string text){
|
|
int s = (y*nScreenWidth)+x;
|
|
for (int i = s; i < s+text.length(); i++){
|
|
screen[i] = text[i-s];
|
|
}
|
|
}
|
|
|
|
int main(){
|
|
///PREPARE TERMINAL
|
|
/*struct termios ttystate; //linux exclusive
|
|
tcgetattr(STDIN_FILENO, &ttystate);
|
|
ttystate.c_lflag &= (~ICANON & ~ECHO); //not display character || is this in conflict with what ncurses does in keyListener()?
|
|
ttystate.c_cc[VMIN] = 1;
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate); */
|
|
fputs("\e[?25h", stdout); //show cursor agein
|
|
system("kbdrate -d 250 -r 10");
|
|
struct winsize size;
|
|
ioctl(STDOUT_FILENO,TIOCGWINSZ,&size);
|
|
nScreenWidth = size.ws_col;
|
|
nScreenHeight = size.ws_row;
|
|
|
|
//ASK FOR IP ADDRESS
|
|
cout << "Enter Server IP: " << endl;
|
|
string ipAddr;
|
|
getline(cin,ipAddr);
|
|
if(ipAddr == ""){
|
|
ipAddr = "127.0.0.1";
|
|
}
|
|
|
|
//SERVER COMMUNICATION // SETUP
|
|
struct sockaddr_in address;
|
|
int sock = 0;
|
|
struct sockaddr_in serv_addr;
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("\n Socket creation error \n");return -1; }
|
|
memset(&serv_addr, '0', sizeof(serv_addr));
|
|
serv_addr.sin_family = AF_INET;
|
|
serv_addr.sin_port = htons(PORT);
|
|
if(inet_pton(AF_INET, ipAddr.data(), &serv_addr.sin_addr)<=0){ printf("\nInvalid address/ Address not supported \n"); return -1; }
|
|
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n"); return -1; }
|
|
|
|
//SERVER COMMUNICATION
|
|
|
|
//GET CITIZEN
|
|
char *citizenCRequest = (char*)"give citizenC";
|
|
char bufferCitizenC[32] = {0};
|
|
send(sock , citizenCRequest , strlen(citizenCRequest) , 0 );
|
|
int getcitizenC = read( sock , bufferCitizenC, 32);
|
|
string s_citizenC = ("%s\n",bufferCitizenC);
|
|
int i_citizenC = stoi(s_citizenC);
|
|
|
|
vector <string> vs_citizenList;
|
|
vs_citizenList.resize(i_citizenC);
|
|
for (int i = 0; i < i_citizenC; i ++){
|
|
char *citizenRequest = (char*)"give citizen";
|
|
char bufferCitizen[256] = {0};
|
|
send(sock , citizenRequest , strlen(citizenRequest) , 0 );
|
|
int getcitizen = read( sock , bufferCitizen, 256);
|
|
vs_citizenList[i].assign(bufferCitizen);
|
|
cout << to_string(i) << ": " << vs_citizenList[i] << endl;
|
|
}
|
|
cout << "Select citizen (you can always press S to change selection): " << endl;
|
|
string s_citizenID;
|
|
getline(cin,s_citizenID);
|
|
int i_citizenID = stoi(s_citizenID);
|
|
char c_citizenID[32];
|
|
sprintf(c_citizenID, "%d", i_citizenID);
|
|
|
|
//GET MAP
|
|
cout << "Downloading map..." << endl;
|
|
char *mapwRequest = (char*)"give mapw";
|
|
char bufferMapw[32] = {0};
|
|
send(sock , mapwRequest , strlen(mapwRequest) , 0 );
|
|
int getmapw = read( sock , bufferMapw, 32);
|
|
string s_MapWidth = ("%s\n",bufferMapw);
|
|
int nMapWidth = stoi(s_MapWidth);
|
|
char *maphRequest = (char*)"give maph";
|
|
char bufferMaph[32] = {0};
|
|
send(sock , maphRequest , strlen(maphRequest) , 0 );
|
|
int getmaph = read( sock , bufferMaph, 32);
|
|
string s_MapHeight = ("%s\n",bufferMaph);
|
|
int nMapHeight = stoi(s_MapHeight);
|
|
string map = "";
|
|
for (int i = 0; i < nMapHeight; i ++){
|
|
char *mapRequest = (char*)"give map";
|
|
char bufferMap[4096] = {0};
|
|
send(sock , mapRequest , strlen(mapRequest) , 0 );
|
|
int getmap = read( sock , bufferMap, 4096);
|
|
map += ("%s\n",bufferMap );
|
|
map.pop_back();
|
|
}
|
|
|
|
//PREPARE MINIMAP
|
|
vector <string> vs_minimap;
|
|
for(int i = 0; i < i_miniMapSize; i++){
|
|
s_borderLine += ('-');
|
|
}
|
|
|
|
//PREPARE KEYBOARD LISTENER
|
|
pthread_t pThread[1];
|
|
pthread_create(&pThread[0], NULL, keyListener, 0);
|
|
|
|
//PREPARE TICKS
|
|
using clock = steady_clock;
|
|
auto tp1 = clock::now();
|
|
auto tp2 = clock::now();
|
|
|
|
|
|
|
|
|
|
fputs("\e[?25l", stdout); //remove cursor
|
|
///////////////////////////////////////////////////////////
|
|
////////BEGINDRAW/////
|
|
///////////////////////////////////////////////////////////
|
|
while(bGameLoop){
|
|
//UPDATE TICKS
|
|
tp2 = clock::now();
|
|
duration<float> elapsedTime = tp2 - tp1;
|
|
tp1 = tp2;
|
|
fElapsedTime = elapsedTime.count();
|
|
|
|
//COMMUNICATE WITH SERVER (LIVE)
|
|
//stupid fucking C++ fucking shit fuck i hate this overly uselessly complicated shit, fuck, why is there no parser to just do this omg
|
|
//why can't i send a simple string over socket in the first place
|
|
//"it's a char array" YOU DONT SAY
|
|
string s_locxRequest = "give locx:" + s_citizenID;
|
|
int i_locxrLength = s_locxRequest.length();
|
|
char locxRequest[i_locxrLength+1];
|
|
strcpy(locxRequest, s_locxRequest.c_str());
|
|
|
|
string s_locyRequest = "give locy:" + s_citizenID;
|
|
int i_locyrLength = s_locyRequest.length();
|
|
char locyRequest[i_locyrLength+1];
|
|
strcpy(locyRequest, s_locyRequest.c_str());
|
|
|
|
string s_aRequest = "give a:" + s_citizenID;
|
|
int i_arLength = s_aRequest.length();
|
|
char aRequest[i_arLength+1];
|
|
strcpy(aRequest, s_aRequest.c_str());
|
|
|
|
//char *locxRequest = (char*)"give locx";
|
|
//char *locyRequest = (char*)"give locy";
|
|
//char *aRequest = (char*)"give a";
|
|
|
|
|
|
char buffer[32] = {0};
|
|
send(sock , locxRequest , strlen(locxRequest) , 0 );
|
|
int getlocx = read( sock , buffer, 32);
|
|
string s_xPos = ("%s\n",buffer );
|
|
|
|
buffer[32] = {0};
|
|
send(sock , locyRequest , strlen(locyRequest) , 0 );
|
|
int getlocy = read( sock , buffer, 32);
|
|
string s_yPos = ("%s\n",buffer );
|
|
|
|
buffer[32] = {0};
|
|
send(sock , aRequest , strlen(aRequest) , 0 );
|
|
int geta = read( sock , buffer, 32);
|
|
string s_angle = ("%s\n",buffer );
|
|
|
|
fPlayerA = stof(s_angle)+3.14159*0.5;
|
|
fPlayerX = stof(s_xPos);
|
|
fPlayerY = stof(s_yPos);
|
|
|
|
//DYNAMIC TERMINAL SIZE (on Window Resize) FIX FIX FIX
|
|
ioctl(STDOUT_FILENO,TIOCGWINSZ,&size);
|
|
nScreenWidth = size.ws_col;
|
|
nScreenHeight = size.ws_row;
|
|
screen.clear();
|
|
screen.resize(nScreenWidth*nScreenHeight);
|
|
cout << "\x1B[0;0H"; //reset cursor to 0,0
|
|
|
|
//SHADING
|
|
for (int x = 0; x < nScreenWidth; x++){
|
|
float fRayAngle = (fPlayerA - fFOV / 2.0f) + ((float)x / (float)nScreenWidth) * fFOV;
|
|
float fDistanceToWall = 0;
|
|
bool bHitWall = false;
|
|
bool bBoundary = false;
|
|
float fEyeX = -sinf(fRayAngle);
|
|
float fEyeY = cosf(fRayAngle);
|
|
while(!bHitWall && fDistanceToWall < fDepth){
|
|
fDistanceToWall += 0.1f;
|
|
int nTestX = (int)(fPlayerX + fEyeX * fDistanceToWall);
|
|
int nTestY = (int)(fPlayerY + fEyeY * fDistanceToWall);
|
|
if(nTestX < 0 || nTestX >= nMapWidth || nTestY < 0 || nTestY >= nMapHeight){
|
|
bHitWall = true;
|
|
fDistanceToWall = fDepth;
|
|
} else {
|
|
if(map[nTestY * nMapWidth + nTestX] == '#') {
|
|
bHitWall = true;
|
|
vector<pair<float, float>> p; //distance, dot
|
|
for (int tx = 0; tx < 2; tx++)
|
|
for (int ty = 0; ty < 2; ty++) {
|
|
float vy = (float)nTestY + ty - fPlayerY;
|
|
float vx = (float)nTestX + tx - fPlayerX;
|
|
float d = sqrt(vx*vx + vy*vy);
|
|
float dot = (fEyeX * vx / d) + (fEyeY * vy / d);
|
|
p.push_back(make_pair(d,dot));
|
|
}
|
|
sort(p.begin(), p.end(), [](const pair<float, float> &left, const pair<float, float> &right) { return left.first < right.first; });
|
|
float fBound = 0.005;
|
|
if (acos(p.at(0).second) < fBound) bBoundary = true;
|
|
if (acos(p.at(1).second) < fBound) bBoundary = true;
|
|
if (acos(p.at(2).second) < fBound) bBoundary = true;
|
|
}
|
|
}
|
|
}
|
|
int nCeiling = (float)(nScreenHeight / 2.0) - nScreenHeight / ((float)fDistanceToWall);
|
|
int nFloor = nScreenHeight - nCeiling;
|
|
short nShade = 32;
|
|
for(int y = 0; y < nScreenHeight; y++){
|
|
if(y < nCeiling)
|
|
screen[y*nScreenWidth+x] = ' ';
|
|
if(y > nCeiling && y <= nFloor){
|
|
if (fDistanceToWall <= fDepth / 4.0f) nShade = 77; //very close
|
|
else if (fDistanceToWall < fDepth / 3.5f) nShade = 72;
|
|
else if (fDistanceToWall < fDepth / 3.0f) nShade = 84;
|
|
else if (fDistanceToWall < fDepth / 2.5f) nShade = 73;
|
|
else if (fDistanceToWall < fDepth / 2.0f) nShade = 105;
|
|
else if (fDistanceToWall < fDepth / 1.5f) nShade = 58;
|
|
else if (fDistanceToWall < fDepth) nShade = 46;
|
|
else nShade = 32; // too far
|
|
if(bBoundary) nShade = '-';
|
|
screen[y*nScreenWidth+x] = nShade;
|
|
}
|
|
else
|
|
{
|
|
float b = 1.0f - (((float)y - nScreenHeight / 2.0f) / ((float)nScreenHeight / 2.0f));
|
|
if (b < 0.3) nShade = 32; //32 //very close
|
|
else if (b < 0.55) nShade = 96; //96
|
|
else if (b < 0.8) nShade = 94; //94
|
|
else if (b < 0.9) nShade = 42; //42
|
|
else nShade = 32; //64 too far
|
|
screen[y*nScreenWidth+x] = nShade;
|
|
}
|
|
}
|
|
}
|
|
|
|
//USER INTERFACE
|
|
if(b_miniMap){
|
|
vs_minimap.clear();
|
|
vs_minimap.resize(i_miniMapSize);
|
|
int nPlayerPosition = nMapWidth*(int)fPlayerY+(int)fPlayerX;
|
|
char cPlayerPosition = map[nPlayerPosition];
|
|
map[nPlayerPosition] = 'X';
|
|
for (int i = (int)fPlayerY - i_miniMapSize/2; i < (int)fPlayerY + i_miniMapSize/2; i++){
|
|
vs_minimap[i - (int)fPlayerY + i_miniMapSize/2].assign(map.substr( ((int)fPlayerX-i_miniMapSize/2 + i * nMapWidth < map.length() ? (int)fPlayerX-i_miniMapSize/2 + i * nMapWidth : map.size() - i_miniMapSize) , i_miniMapSize));
|
|
writeStringToScreen(0,i - (int)fPlayerY + i_miniMapSize/2, vs_minimap[i - (int)fPlayerY + i_miniMapSize/2] + "|");
|
|
}
|
|
writeStringToScreen(0,i_miniMapSize, s_borderLine + "+");
|
|
map[nPlayerPosition] = cPlayerPosition;
|
|
}
|
|
|
|
//DEBUG-INTERFACE
|
|
if(b_debug){
|
|
writeStringToScreen(0, nScreenHeight-3,"Received from server: " + s_xPos+ " "+s_yPos+ " "+s_angle + " " + to_string(nMapWidth)+ " " + to_string(nMapHeight) + " ");
|
|
writeStringToScreen(0, nScreenHeight-2, "Frame rate: " + to_string(1.0f / fElapsedTime) + " ");
|
|
writeStringToScreen(0, nScreenHeight-1, "Key pressed: " + s_keyPressed + " Angle: " + to_string(fPlayerA) + " X: " + to_string(fPlayerX) + " Y: " + to_string(fPlayerY) + " ");
|
|
}
|
|
|
|
|
|
//RENDER
|
|
cout << screen;
|
|
|
|
//END LOOP
|
|
tp1 += milliseconds(1000 / nFrameRate);
|
|
this_thread::sleep_until(tp1);
|
|
}
|
|
return 0;
|
|
}
|