Builder_for_CG/src/main.c

406 lines
11 KiB
C

# include <gint/display.h>
# include <gint/keyboard.h>
# include <stdlib.h>
# include <time.h>
# include <string.h>
#include <gint/clock.h>
# include "msg_fr.h"
# include "itemsizes.h"
# include "gamesettings.h"
# include "worldgen.h"
# include "itoa.h"
extern bopti_image_t title_img;
extern bopti_image_t soil_tile;
extern bopti_image_t grass_tile;
extern bopti_image_t stone_tile;
extern bopti_image_t coal_tile;
extern bopti_image_t steel_tile;
extern bopti_image_t wood_tile;
extern bopti_image_t skin1_player_left1;
extern bopti_image_t skin1_player_left2;
extern bopti_image_t skin1_player_right1;
extern bopti_image_t skin1_player_right2;
extern bopti_image_t select_tool;
extern bopti_image_t invnormal;
extern bopti_image_t invselected;
# include "player.h"
//int x, y, pos, hour, animation, orient, timing, falling, jumping, jumpheight;
int pos, hour;
char tmp_char[2];
void drawselectedgame(int selected) {
dclear(C_WHITE);
// dimage(16, 8, &title_img);
for(int i=0;i!=GAMESNUM;i++){
dtext(1, WORLDSEL_MARGIN + i*(LINEHEIGHT), C_BLACK, WORLDSEL_EMPTY);
}
drect(1, WORLDSEL_MARGIN+selected*(LINEHEIGHT)-(int)(LINEPADDING/2), 128, WORLDSEL_MARGIN+(selected+1)*(LINEHEIGHT)-(int)(LINEPADDING/2), C_INVERT);
dupdate();
}
void addtree(int pos, unsigned short int * terrain, int n){
int i;
srand(clock());
for(i=pos-rand()%11;i!=pos;i++){
terrain[i*WORLD_WIDTH+(n>>3)] = 5;
}
}
void addtrees(unsigned short int * terrain){
int n, i;
for(n=0;n!=WORLD_WIDTH;n++){
if(rand()%11 == 1){
for(i=0;i!=WORLD_HEIGHT;i++){
if(terrain[i*WORLD_WIDTH+(n>>3)] != 0){
pos = i;
break;
}
}
addtree(pos, terrain, n);
}
}
}
void mappartdisplaying(int x, int y, unsigned short int * terrain, int player) {
x = x-60;
y = y-24;
int firsttile_x = x>>3, firsttile_y = y>>3;
int base_x = firsttile_x*8, base_y = firsttile_y*8;
int sx = base_x - x, sy = base_y - y, tx = (SCREEN_WIDTH>>3) + 1, ty = (SCREEN_HEIGHT>>3) + 1;
int cx, cy, px = sx, py = sy;
unsigned short type, type_over;
dclear(C_WHITE);
for(cy = 0;cy != ty;cy++){
for(cx = 0;cx != tx;cx++){
type = terrain[(firsttile_y+cy)*WORLD_WIDTH+(firsttile_x+cx)];
type_over = terrain[(firsttile_y+cy-1)*WORLD_WIDTH+(firsttile_x+cx)];
switch(type){
case 1:
if(type_over == 0){dimage(px, py, &grass_tile);}else{dimage(px, py, &soil_tile);} break;
case 2:
dimage(px, py, &stone_tile); break;
case 3:
dimage(px, py, &coal_tile); break;
case 4:
dimage(px, py, &steel_tile); break;
case 5:
dimage(px, py, &wood_tile); break;
}
px += 8;
}
py += 8;
px = sx;
}
switch(player){
case 2:
dimage(60, 24, &skin1_player_right2); break;
case 3:
dimage(60, 24, &skin1_player_left1); break;
case 4:
dimage(60, 24, &skin1_player_left2); break;
default:
dimage(60, 24, &skin1_player_right1); break;
}
}
int exists(unsigned short int * terrain, short type) {
int i;
for(i=0;i!=WORLD_WIDTH*WORLD_HEIGHT;i++){
if(terrain[i]==type){
return 1;
}
}
return 0;
}
int pointoverrectangle(int rx1, int ry1, int rx2, int ry2, int x, int y){
if ((x>=rx1 && y>=ry1) && (x<=rx2 && y<=ry2)){
return 1;
}
return 0;
}
int collisiononmap(int x, int y, unsigned short int * terrain, int testx, int testy) {
x = x-60;
y = y-24;
int firsttile_x = x>>3, firsttile_y = y>>3;
int base_x = firsttile_x*8, base_y = firsttile_y*8;
int sx = base_x - x, sy = base_y - y, tx = (SCREEN_WIDTH>>3) + 1, ty = (SCREEN_HEIGHT>>3) + 1;
int cx, cy, px = sx, py = sy;
unsigned short type;
dclear(C_WHITE);
for(cy = 0;cy != ty;cy++){
for(cx = 0;cx != tx;cx++){
type = terrain[(firsttile_y+cy)*WORLD_WIDTH+(firsttile_x+cx)];
if(type != 0){
if(pointoverrectangle(px, py, px+8, py+8, testx, testy)){
return 1;
}
}
px += 8;
}
py += 8;
px = sx;
}
return 0;
}
Player player;
void drawinventory(){
int i;
for(i=0;i!=5;i++){
if(i == player.invselect){
dimage(26+i*15, 49, &invselected);
}else{
dimage(26+i*15, 49, &invnormal);
}
switch(player.inventoryitems[i]){
case 1:
dimage(26+i*15+3, 52, &soil_tile); break;
case 2:
dimage(26+i*15+3, 52, &stone_tile); break;
case 3:
dimage(26+i*15+3, 52, &coal_tile); break;
case 4:
dimage(26+i*15+3, 52, &steel_tile); break;
case 5:
dimage(26+i*15+3, 52, &wood_tile); break;
}
}
}
void drawdetailinv(){
int i;
for(i=0;i!=INVENTORY_SIZE;i++){
pos = i*8;
switch(player.inventoryitems[i]){
case 1:
dimage(5, pos, &soil_tile); break;
case 2:
dimage(5, pos, &stone_tile); break;
case 3:
dimage(5, pos, &coal_tile); break;
case 4:
dimage(5, pos, &steel_tile); break;
case 5:
dimage(5, pos, &wood_tile); break;
}
itoa(player.inventorynum[i], tmp_char);
dtext(20, pos, C_BLACK, tmp_char);
if(i == player.invselect){
dtext(60, pos, C_BLACK, SELECTED);
}
}
}
int main(void) {
dclear(C_WHITE);
dimage(16, TITLE_IMAGE_MARGIN, &title_img);
dtext(1, TITLE_MARGIN, C_BLACK, TITLE_START);
dupdate();
int key = 0, game = 0, selected = 0, i;
unsigned short terrain[WORLD_WIDTH*WORLD_HEIGHT];
while(keydown(KEY_EXIT) == 0){
////////// TITLE SCREEN //////////
if(game == 0){
key=getkey().key;
if(key==KEY_EXE){
drawselectedgame(selected);
game = 1;
}
}
////////// GAME CHOOSING SCREEN //////////
else if(game == 1){
key=getkey().key;
if(key==KEY_DOWN){
if(selected<GAMESNUM - 1){
selected++;
}else{
selected = 0;
}
drawselectedgame(selected);
}else if(key==KEY_UP){
if(selected>0){
selected--;
}else{
selected = GAMESNUM - 1;
}
drawselectedgame(selected);
}else if(key==KEY_EXE){
dclear(C_WHITE);
game = 2;
}
}else if(game == 2){
dtext(1, 1, C_BLACK, WORLDGEN_INFO);
dupdate();
for(i=0;i!=WORLD_WIDTH*WORLD_HEIGHT;i++){
terrain[i] = 0;
}
srand(clock());
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 40) - (WORLD_HEIGHT - 50) + 1)) + WORLD_HEIGHT - 50), WORLD_HEIGHT - 50, WORLD_HEIGHT - 40, 0, 1);
srand(clock());
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 30) - (WORLD_HEIGHT - 40) + 1)) + WORLD_HEIGHT - 40), WORLD_HEIGHT - 40, WORLD_HEIGHT - 30, 3, 2);
srand(clock());
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 20) - (WORLD_HEIGHT - 30) + 1)) + WORLD_HEIGHT - 30), WORLD_HEIGHT - 30, WORLD_HEIGHT - 20, 4, 2);
dtext(1, 1, C_BLACK, "Adding trees");
///// Add trees /////
addtrees(terrain);
/////////////////////
player.x = 0;
/*for(i=0;i!=WORLD_HEIGHT;i++){
if(terrain[i*WORLD_WIDTH+(x>>3)] != 0){
y = i*8-8;
break;
}
} */
player.y = 0;
game = 3;
hour = 0;
player.timing = 0;
player.jumping = 0;
player.jumpheight = 0;
player.falling = 0;
for(i=0;i!=INVENTORY_SIZE;i++){
player.inventoryitems[i] = 0;
player.inventorynum[i] = 0;
}
}else if(game == 3){
clearevents();
if(keydown(KEY_RIGHT) && (collisiononmap(player.x, player.y, terrain, 67, 35) == 0 && collisiononmap(player.x, player.y, terrain, 67, 25) == 0)){
player.x++;
if(player.animation == 0 && player.timing == 5){
player.animation = 1;
player.timing = 0;
}else if(player.animation == 1 && player.timing == 5){
player.animation = 0;
player.timing = 0;
}
player.timing++;
player.orient = 1;
}else if(keydown(KEY_LEFT) && (collisiononmap(player.x, player.y, terrain, 61, 35) == 0 && collisiononmap(player.x, player.y, terrain, 61, 25) == 0)){
player.x--;
if(player.animation == 0 && player.timing == 5){
player.animation = 1;
player.timing = 0;
}else if(player.animation == 1 && player.timing == 5){
player.animation = 0;
player.timing = 0;
}
player.timing++;
player.orient = 3;
}
if(collisiononmap(player.x, player.y, terrain, 62, 40) == 0 && collisiononmap(player.x, player.y, terrain, 66, 40) == 0 && collisiononmap(player.x, player.y, terrain, 60, 40) == 0 && player.jumping == 0){
player.y++;
player.falling = 1;
}else{
player.falling = 0;
}
/* clearevents();
if(keydown(KEY_UP)){
y--;
}else if(keydown(KEY_DOWN)){
y++;
} */
clearevents();
if(keydown(KEY_SHIFT) && player.jumping == 0 && player.falling == 0 && player.jumpheight == 0){
player.jumping = 1;
player.jumpheight = 1;
}
if(player.jumping == 1 && player.jumpheight == 12){
player.jumping = 0;
player.jumpheight = 0;
}else if(collisiononmap(player.x, player.y, terrain, 62, 23) || collisiononmap(player.x, player.y, terrain, 66, 23) || collisiononmap(player.x, player.y, terrain, 60, 23)){
player.jumping = 0;
player.jumpheight = 0;
}else if(player.jumping == 1){
player.jumpheight++;
player.y--;
}
if(exists(terrain, 5)==0){
addtrees(terrain);
}
if(player.y>>3>WORLD_HEIGHT){
player.y = 0;
}
clearevents();
if (keydown(KEY_8) && player.sely>-8){
player.sely--;
}else if (keydown(KEY_5) && player.sely<24){
player.sely++;
}
clearevents();
if (keydown(KEY_4) && player.selx>-8){
player.selx--;
}else if (keydown(KEY_6) && player.selx<16){
player.selx++;
}
pos = ((player.y+player.sely)>>3)*WORLD_WIDTH+((player.x+player.selx)>>3);
if(pos<=WORLD_WIDTH*WORLD_HEIGHT && pos>=0){
clearevents();
if (keydown(KEY_EXE) && terrain[pos] == 0 && player.inventorynum[player.invselect] != 0){
terrain[pos] = player.inventoryitems[player.invselect];
player.inventorynum[player.invselect]--;
if(player.inventorynum[player.invselect] == 0) {
player.inventoryitems[player.invselect] = 0;
}
}else if (keydown(KEY_1) && terrain[pos] != 0){
for(i=0;i!=INVENTORY_SIZE;i++){
if((terrain[pos] == player.inventoryitems[i] && player.inventorynum[i] < 64 && terrain[pos] != 0) || (player.inventoryitems[i] == 0 && player.inventorynum[i] == 0 && terrain[pos] != 0)) {
player.inventoryitems[i] = terrain[pos];
terrain[pos] = 0;
player.inventorynum[i]++;
break;
}
}
}
}
clearevents();
if (keydown(KEY_0)){
if(player.invselect<INVENTORY_SIZE-1){
player.invselect++;
}else{
player.invselect = 0;
}
clearevents();
while(keydown(KEY_0)){
clearevents();
}
}
clearevents();
if(keydown(KEY_MENU)){
game = 4;
}
// dtext(1, 1, C_BLACK, "test");
mappartdisplaying(player.x, player.y, terrain, player.orient+player.animation);
dimage(player.selx + 56, player.sely + 20, &select_tool);
drawinventory();
dupdate();
sleep_ms(20);
}else if(game == 4){
dclear(C_WHITE);
drawdetailinv();
dupdate();
clearevents();
if(keydown(KEY_EXE)){
game = 3;
clearevents();
while(keydown(KEY_EXE)){
clearevents();
}
}
clearevents();
if (keydown(KEY_0)){
if(player.invselect<INVENTORY_SIZE-1){
player.invselect++;
}else{
player.invselect = 0;
}
dclear(C_WHITE);
drawdetailinv();
dupdate();
clearevents();
while(keydown(KEY_0)){
clearevents();
}
}
}
clearevents();
}
return 1;
}