One of my first programming projects. I tried to learn C and use curses...it is quite embarrassing looking back at it now :D
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

84 lines
1.5 KiB

#include <screen.h>
#include <world.h>
#include <getPos.h>
int getOpenPos(){
int chooseAnother=1;
while(chooseAnother){
spaceExists:
openPos.y=(rand()%(viewportWinRows-1)+1);
openPos.x=(rand()%(viewportWinCols-1)+1);
if(mvwinch(viewportWin,openPos.y,openPos.x)==' '){
int i;
for(i=1;i<MAX_ALIVE;i++){
if(allEntities[i].isAlive && allEntities[i].curYpos==openPos.y && allEntities[i].curXpos==openPos.x){
chooseAnother=1;
}
else{
chooseAnother=0;
}
}
}
else{
int x,y;
for(y=0;y<viewportWinRows;y++){
for(x=0;x<viewportWinCols;x++){
if(mvwinch(viewportWin,y,x)==' '){
chooseAnother=1;
goto spaceExists;
}
}
}
return 0;
}
}
return 1;
}
void getNextPos(int y, int x, int y2, int x2){
int w = x2 - x;
int h = y2 - y;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 =0;
if (w<0){
dx1 = -1;
}
else if(w>0){
dx1 = 1;
}
if (h<0){
dy1 = -1;
}
else if(h>0){
dy1 = 1;
}
if (w<0){
dx2 = -1;
}
else if(w>0){
dx2 = 1;
}
int longest = abs(w);
int shortest = abs(h);
if (!(longest>shortest)) {
longest = abs(h);
shortest = abs(w);
if (h<0){
dy2 = -1;
}
else if (h>0){
dy2 = 1;
}
dx2 = 0;
}
int numerator = longest >> 1;
numerator += shortest;
if (!(numerator<longest)) {
numerator -= longest;
x += dx1;
y += dy1;
} else {
x += dx2;
y += dy2;
}
nextPos.x = x;
nextPos.y = y;
}