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.
 
 

88 lines
1.9 KiB

#include <world.h>
#include <getPos.h>
#include <stdlib.h>
struct OpenPos getOpenPos(){
int chooseAnother=1;
struct OpenPos openPos;
while(chooseAnother){
spaceExists:
openPos.y=(rand()%(viewportWinRows-2)+1);
openPos.x=(rand()%(viewportWinCols-2)+1);
if(mvwinch(viewportWin,openPos.y,openPos.x)==' ' && ((openPos.y > allEntities[0].curYpos+10 || openPos.y < allEntities[0].curYpos-10) || (openPos.x > allEntities[0].curXpos+10 || openPos.x < allEntities[0].curXpos-10))){
int i;
for(i=1;i<MAX_ENTITIES;i++){
if(allEntities[i].isAlive && allEntities[i].curYpos==openPos.y && allEntities[i].curXpos==openPos.x){
chooseAnother=1;
}
else{
chooseAnother=0;
return openPos;
}
}
}
else{
int x,y;
for(y=0;y<viewportWinRows;y++){
for(x=0;x<viewportWinCols;x++){
if(mvwinch(viewportWin,y,x)==' ' && ((y > allEntities[0].curYpos+5 || y < allEntities[0].curYpos-5) || (x > allEntities[0].curXpos+5 || x < allEntities[0].curXpos-5))){
chooseAnother=1;
goto spaceExists;
}
}
}
openPos.y=0;
openPos.x=0;
return openPos;
}
}
}
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;
}