2015-12-27 14:23:04 +00:00
# include <iostream>
2015-12-27 15:34:17 +00:00
# include <random>
2015-12-28 16:10:11 +00:00
# include <conio.h>
2015-12-29 18:06:47 +00:00
# include <stdlib.h>
2016-01-12 17:51:35 +00:00
# include <time.h>
2016-01-12 15:43:51 +00:00
using namespace std ;
//Costanti di sistema
2015-12-27 14:23:04 +00:00
# define X_MAX 80
# define Y_MAX 23
2015-12-27 15:34:17 +00:00
# define ROOMS 8
# define ROOM_SIZE 7
2016-01-12 16:08:48 +00:00
# define MAX_ENEMIES 50
2016-02-03 06:30:23 +00:00
# define HP_MAX 100
2016-01-05 16:39:53 +00:00
# define STARTING_ATK 5
2016-01-12 15:15:42 +00:00
# define STARTING_DEF 5
2016-01-03 15:48:29 +00:00
# define MAX_POTIONS_PER_FLOOR 5
2016-01-12 17:21:11 +00:00
# define ENEMY_HP 10
2015-12-27 14:23:04 +00:00
2016-01-12 15:43:51 +00:00
//Costanti globali per il rendering della mappa
2016-01-12 17:51:35 +00:00
unsigned char WALL = 0xB2 ;
2016-01-04 16:53:30 +00:00
const unsigned char EMPTY = ' ' ;
const unsigned char PLAYER = 0x02 ;
const unsigned char ENEMY = ' X ' ;
const unsigned char EXIT = ' > ' ;
const unsigned char DOUBLELINE = 0xCD ;
const unsigned char ITEM_SMALL_POTION = ' p ' ;
const unsigned char ITEM_MEDIUM_POTION = ' n ' ;
const unsigned char ITEM_BIG_POTION = ' m ' ;
2016-01-12 15:43:51 +00:00
//Prototipiamo tutte le classi
class Entity ;
class Player ;
class Enemy ;
2015-12-29 18:51:19 +00:00
2016-01-12 15:43:51 +00:00
//Variabili globali
//Lista di tutti i nemici nel livello
2016-01-12 16:08:48 +00:00
Enemy * list [ MAX_ENEMIES ] ;
2015-12-30 15:25:23 +00:00
//Numero del piano raggiunto dal giocatore
int depth = 1 ;
2016-01-12 15:43:51 +00:00
//Mappa del gioco
unsigned char map [ X_MAX ] [ Y_MAX ] ;
2015-12-30 15:25:23 +00:00
2016-01-12 15:43:51 +00:00
//Prototipiamo tutte le funzioni, per semplificarci un po' la vita
2016-01-03 16:43:02 +00:00
void draw ( ) ;
void inventory ( ) ;
2016-01-12 15:43:51 +00:00
void init ( ) ;
void room ( int start_x , int start_y , int end_x , int end_y ) ;
void corridor ( int start_x , int start_y , int end_x , int end_y , bool verticale ) ;
void generate ( ) ;
void tick ( ) ;
2016-01-12 16:38:33 +00:00
void attack ( int x , int y ) ;
2016-01-12 15:43:51 +00:00
Enemy * find ( int x , int y ) ;
2016-01-03 15:12:29 +00:00
2016-01-12 16:38:33 +00:00
//Classe entit<69> generica, sia nemico sia giocatore
2016-01-03 16:19:40 +00:00
class Entity
2015-12-28 16:10:11 +00:00
{
2016-01-03 16:19:40 +00:00
public :
2016-01-10 15:50:57 +00:00
bool alive = true ;
2016-01-03 16:19:40 +00:00
int x ;
int y ;
2016-01-12 17:21:11 +00:00
int hp = HP_MAX ;
int hp_max = HP_MAX ;
2016-01-03 16:19:40 +00:00
int move ( ) ;
2016-01-12 16:38:33 +00:00
//Cura di x l'entit<69>
2016-01-03 16:45:52 +00:00
void heal ( int x )
{
2016-01-03 18:02:19 +00:00
if ( hp + x > hp_max )
2016-01-03 16:45:52 +00:00
{
2016-01-03 18:02:19 +00:00
hp = hp_max ;
2016-01-03 16:45:52 +00:00
}
else
{
hp + = x ;
}
}
2016-01-12 16:38:33 +00:00
//Danneggia di x l'entit<69>
2016-01-03 18:12:55 +00:00
void damage ( int x )
{
2016-01-12 16:36:41 +00:00
if ( hp - x < = 0 )
2016-01-03 18:12:55 +00:00
{
2016-01-10 15:50:57 +00:00
kill ( ) ;
2016-01-03 18:12:55 +00:00
}
else
{
hp - = x ;
}
}
2016-01-12 16:38:33 +00:00
//Uccide ed elimina l'entit<69>
2016-01-10 15:50:57 +00:00
void kill ( )
{
hp = 0 ;
alive = false ;
map [ x ] [ y ] = EMPTY ;
}
2016-01-12 15:20:09 +00:00
//Visualizza la vita
int gethp ( )
{
return hp ;
}
2016-01-03 16:19:40 +00:00
} ;
//Classe del giocatore
class Player : public Entity
{
2016-01-05 16:39:53 +00:00
private :
2016-01-12 15:15:42 +00:00
int base_atk = STARTING_ATK ; //Attacco di base
int base_def = STARTING_DEF ; //Difesa di base
2016-01-05 16:39:53 +00:00
int equipment_atk = 0 ; //Attacco ottenuto dall'equipaggiamento
2016-01-12 15:15:42 +00:00
int equipment_def = 0 ; //Difesa ottenuta dall'equipaggiamento
2016-01-03 16:19:40 +00:00
public :
2016-01-05 16:39:53 +00:00
int atk ( )
{
int result = equipment_atk + base_atk ;
return result ;
}
2016-01-12 16:56:21 +00:00
int def ( )
{
int result = equipment_def + base_def ;
return result ;
}
2016-01-03 18:02:19 +00:00
int pozioni_vita_piccole = 3 ;
int pozioni_vita_medie = 2 ;
int pozioni_vita_grandi = 1 ;
2016-01-03 16:19:40 +00:00
int move ( )
2015-12-28 16:10:11 +00:00
{
2016-01-10 15:50:57 +00:00
if ( alive )
2015-12-28 16:10:11 +00:00
{
2016-01-10 15:50:57 +00:00
bool waiting = true ;
unsigned char * starting = & map [ x ] [ y ] ; //Casella attuale
unsigned char * target ; //Bersaglio del movimento
//Rileva i tasti freccia
while ( waiting )
2016-01-03 16:19:40 +00:00
{
2016-01-10 15:50:57 +00:00
unsigned char input = getch ( ) ;
short int dir = 0 ; //Direzione del movimento, per modificare x e y.
if ( input = = 224 )
2016-01-03 17:20:13 +00:00
{
2016-01-10 15:50:57 +00:00
switch ( getch ( ) )
{
case 72 : //Freccia su, 1
target = & map [ x ] [ y - 1 ] ;
dir = 1 ;
break ;
2016-01-12 16:38:33 +00:00
case 80 : //Freccia gi<67> , 2
2016-01-10 15:50:57 +00:00
target = & map [ x ] [ y + 1 ] ;
dir = 2 ;
break ;
case 75 : //Freccia sinistra, 3
target = & map [ x - 1 ] [ y ] ;
dir = 3 ;
break ;
case 77 : //Freccia destra, 4
target = & map [ x + 1 ] [ y ] ;
dir = 4 ;
break ;
2016-01-12 16:38:33 +00:00
default : //Pag su, pag gi<67> , skippa
2016-01-10 15:50:57 +00:00
target = & map [ x ] [ y ] ; //Un po' hackerino, ma...
break ;
//Aggiungere gestione del caso che non sia una delle quattro frecce
}
//Muoviti e agisci!
if ( * target = = EMPTY )
{
* starting = EMPTY ;
* target = PLAYER ;
waiting = false ;
}
else if ( * target = = ITEM_SMALL_POTION )
{
* starting = EMPTY ;
* target = PLAYER ;
pozioni_vita_piccole + + ;
waiting = false ;
}
else if ( * target = = ITEM_MEDIUM_POTION )
{
* starting = EMPTY ;
* target = PLAYER ;
pozioni_vita_medie + + ;
waiting = false ;
}
else if ( * target = = ITEM_BIG_POTION )
{
* starting = EMPTY ;
* target = PLAYER ;
pozioni_vita_grandi + + ;
waiting = false ;
}
else if ( * target = = EXIT )
{
return 1 ;
}
2016-01-03 17:20:13 +00:00
}
2016-01-10 15:50:57 +00:00
else if ( input = = ' s ' ) //S
2016-01-03 17:20:13 +00:00
{
2016-01-10 15:50:57 +00:00
//Salta un turno
2016-01-03 17:20:13 +00:00
waiting = false ;
}
2016-01-10 15:50:57 +00:00
else if ( input = = ' i ' ) //I
2016-01-03 17:20:13 +00:00
{
2016-01-10 15:50:57 +00:00
//Apri l'inventario
inventory ( ) ;
//Torna alla mappa
draw ( ) ;
2016-01-03 17:20:13 +00:00
}
2016-01-12 16:38:33 +00:00
else if ( input = = ' a ' ) //A
{
2016-01-12 16:50:52 +00:00
printf ( " ATTACCO selezionato " ) ;
2016-01-12 16:38:33 +00:00
if ( getch ( ) = = 224 )
{
char atk = getch ( ) ;
switch ( atk )
{
2016-01-12 16:50:52 +00:00
case 72 : //ATK SU
if ( map [ x ] [ y - 1 ] = = ENEMY )
{
attack ( x , y - 1 ) ;
}
break ;
case 80 : //ATK GIU
if ( map [ x ] [ y + 1 ] = = ENEMY )
{
attack ( x , y + 1 ) ;
}
break ;
case 75 : //ATK SX
if ( map [ x - 1 ] [ y ] = = ENEMY )
{
attack ( x - 1 , y ) ;
}
break ;
case 77 : //ATK DX
if ( map [ x + 1 ] [ y ] = = ENEMY )
{
attack ( x + 1 , y ) ;
}
break ;
2016-01-12 16:38:33 +00:00
}
2016-01-12 16:50:52 +00:00
waiting = false ;
2016-01-12 16:38:33 +00:00
}
}
2016-01-10 15:50:57 +00:00
//Se ti sei mosso, controlla in che direzione e aggiorna correttamente la x e la y.
//Non mi veniva in mente un modo per farlo meglio.
if ( ! waiting )
2016-01-03 17:20:13 +00:00
{
2016-01-10 15:50:57 +00:00
if ( dir = = 1 )
{
y - - ;
}
else if ( dir = = 2 )
{
y + + ;
}
else if ( dir = = 3 )
{
x - - ;
}
else if ( dir = = 4 )
{
x + + ;
}
2016-01-03 17:20:13 +00:00
}
}
2015-12-28 16:10:11 +00:00
}
2016-01-03 16:19:40 +00:00
return 0 ;
2015-12-28 16:10:11 +00:00
}
2016-01-03 16:34:20 +00:00
} player ;
2016-01-03 16:19:40 +00:00
//Classe dei nemici
class Enemy : public Entity
{
public :
int move ( )
2016-01-03 00:43:00 +00:00
{
2016-01-10 15:50:57 +00:00
if ( alive )
2016-01-03 16:19:40 +00:00
{
2016-01-12 16:38:33 +00:00
//Se intorno c'<27> il giocatore
2016-01-10 15:50:57 +00:00
if ( map [ x - 1 ] [ y ] = = PLAYER | | map [ x + 1 ] [ y ] = = PLAYER | | map [ x ] [ y - 1 ] = = PLAYER | | map [ x ] [ y + 1 ] = = PLAYER )
2016-01-07 17:34:43 +00:00
{
2016-01-10 15:50:57 +00:00
//Forse sarebbe meglio fare una funzione per togliere vita che controlla anche se va a 0...
player . damage ( rand ( ) % 5 + 1 ) ;
2016-01-07 17:34:43 +00:00
}
2016-01-10 15:50:57 +00:00
else
2016-01-07 17:46:02 +00:00
{
2016-01-12 16:38:33 +00:00
//Se il giocatore <20> vicino, muoviti verso di lui
2016-01-10 15:50:57 +00:00
if ( map [ x - 2 ] [ y ] = = PLAYER & & map [ x - 1 ] [ y ] = = EMPTY ) //Due a sinistra
2016-01-07 17:46:02 +00:00
{
map [ x ] [ y ] = EMPTY ;
map [ x - 1 ] [ y ] = ENEMY ;
x - - ;
}
2016-01-10 15:50:57 +00:00
else if ( map [ x + 2 ] [ y ] = = PLAYER & & map [ x + 1 ] [ y ] = = EMPTY ) //Due a destra
2016-01-07 17:46:02 +00:00
{
map [ x ] [ y ] = EMPTY ;
2016-01-10 15:50:57 +00:00
map [ x + 1 ] [ y ] = ENEMY ;
x + + ;
2016-01-07 17:46:02 +00:00
}
2016-01-10 15:50:57 +00:00
else if ( map [ x ] [ y - 2 ] = = PLAYER & & map [ x ] [ y - 1 ] = = EMPTY ) //Due in su
2016-01-07 17:46:02 +00:00
{
map [ x ] [ y ] = EMPTY ;
map [ x ] [ y - 1 ] = ENEMY ;
y - - ;
}
2016-01-12 16:38:33 +00:00
else if ( map [ x ] [ y + 2 ] = = PLAYER & & map [ x ] [ y + 1 ] = = EMPTY ) //Due in gi<67>
2016-01-07 17:46:02 +00:00
{
map [ x ] [ y ] = EMPTY ;
map [ x ] [ y + 1 ] = ENEMY ;
y + + ;
}
2016-01-10 15:50:57 +00:00
else if ( map [ x - 1 ] [ y - 1 ] = = PLAYER ) //In alto a sinistra
2016-01-07 17:46:02 +00:00
{
2016-01-10 15:50:57 +00:00
if ( map [ x ] [ y - 1 ] = = EMPTY ) //Vai in alto
{
map [ x ] [ y ] = EMPTY ;
map [ x ] [ y - 1 ] = ENEMY ;
y - - ;
}
else if ( map [ x - 1 ] [ y ] = = EMPTY ) //Vai a sinistra
{
map [ x ] [ y ] = EMPTY ;
map [ x - 1 ] [ y ] = ENEMY ;
x - - ;
}
2016-01-07 17:46:02 +00:00
}
2016-01-10 15:50:57 +00:00
else if ( map [ x - 1 ] [ y + 1 ] = = PLAYER ) //In basso a sinistra
{
if ( map [ x ] [ y + 1 ] = = EMPTY ) //Vai in basso
{
map [ x ] [ y ] = EMPTY ;
map [ x ] [ y + 1 ] = ENEMY ;
y + + ;
}
else if ( map [ x - 1 ] [ y ] = = EMPTY ) //Vai a sinistra
{
map [ x ] [ y ] = EMPTY ;
map [ x - 1 ] [ y ] = ENEMY ;
x - - ;
}
}
else if ( map [ x + 1 ] [ y - 1 ] = = PLAYER ) //In alto a destra
{
if ( map [ x ] [ y - 1 ] = = EMPTY ) //Vai in alto
{
map [ x ] [ y ] = EMPTY ;
map [ x ] [ y - 1 ] = ENEMY ;
y - - ;
}
else if ( map [ x + 1 ] [ y ] = = EMPTY ) //Vai a destra
{
map [ x ] [ y ] = EMPTY ;
map [ x + 1 ] [ y ] = ENEMY ;
x + + ;
}
}
else if ( map [ x + 1 ] [ y + 1 ] = = PLAYER ) //In basso a destra
{
if ( map [ x ] [ y + 1 ] = = EMPTY ) //Vai in basso
{
map [ x ] [ y ] = EMPTY ;
map [ x ] [ y + 1 ] = ENEMY ;
y + + ;
}
else if ( map [ x + 1 ] [ y ] = = EMPTY ) //Vai a destra
{
map [ x ] [ y ] = EMPTY ;
map [ x + 1 ] [ y ] = ENEMY ;
x + + ;
}
}
2016-01-12 16:38:33 +00:00
//Il giocatore non <20> vicino
2016-01-10 15:50:57 +00:00
else
2016-01-03 16:19:40 +00:00
{
2016-01-10 15:50:57 +00:00
if ( map [ x - 1 ] [ y ] = = EMPTY | | map [ x + 1 ] [ y ] = = EMPTY | | map [ x ] [ y - 1 ] = = EMPTY | | map [ x ] [ y + 1 ] = = EMPTY )
2016-01-07 17:34:43 +00:00
{
2016-01-10 15:50:57 +00:00
//Muoviti in una direzione casuale
bool moving = true ;
while ( moving )
2016-01-08 13:54:08 +00:00
{
2016-01-10 15:50:57 +00:00
int direction = rand ( ) % 4 ;
switch ( direction )
{
case 0 : //Sinistra
if ( map [ x - 1 ] [ y ] = = EMPTY )
{
map [ x ] [ y ] = EMPTY ;
map [ x - 1 ] [ y ] = ENEMY ;
x - - ;
moving = false ;
}
break ;
case 1 : //Destra
if ( map [ x + 1 ] [ y ] = = EMPTY )
{
map [ x ] [ y ] = EMPTY ;
map [ x + 1 ] [ y ] = ENEMY ;
x + + ;
moving = false ;
}
break ;
case 2 : //Su
if ( map [ x ] [ y - 1 ] = = EMPTY )
{
map [ x ] [ y ] = EMPTY ;
map [ x ] [ y - 1 ] = ENEMY ;
y - - ;
moving = false ;
}
break ;
2016-01-12 16:38:33 +00:00
case 3 : //Gi<47>
2016-01-10 15:50:57 +00:00
if ( map [ x ] [ y + 1 ] = = EMPTY )
{
map [ x ] [ y ] = EMPTY ;
map [ x ] [ y + 1 ] = ENEMY ;
y + + ;
moving = false ;
}
break ;
}
2016-01-08 13:54:08 +00:00
}
2016-01-07 17:34:43 +00:00
}
2016-01-03 16:19:40 +00:00
}
}
}
2016-01-03 00:43:00 +00:00
}
2016-01-03 16:19:40 +00:00
} ;
2016-01-03 00:43:00 +00:00
2016-01-03 16:43:02 +00:00
//Aggiorna la console con la situazione corrente del gioco.
void draw ( )
{
//Svuota lo schermo della console. Sono sicuro che ci sia un modo molto migliore per farlo, ma non mi viene in mente...
system ( " cls " ) ;
for ( int y = 0 ; y < Y_MAX ; y + + )
{
for ( int x = 0 ; x < X_MAX ; x + + )
{
2016-01-04 16:40:04 +00:00
printf ( " %c " , map [ x ] [ y ] ) ;
2016-01-03 16:43:02 +00:00
}
}
2016-01-12 17:21:11 +00:00
printf ( " Piano: %d | Vita: %d/%d | x:%d y:%d \n " , depth , player . gethp ( ) , HP_MAX , player . x , player . y ) ;
2016-01-03 16:43:02 +00:00
}
//Visualizza l'inventario
void inventory ( )
{
system ( " cls " ) ;
2016-01-12 17:21:11 +00:00
printf ( " Piano: %d | Vita: %d/%d | x:%d y:%d \n " , depth , player . gethp ( ) , HP_MAX , player . x , player . y ) ;
2016-01-03 16:43:02 +00:00
for ( int i = 0 ; i < X_MAX ; i + + )
{
2016-01-12 15:20:09 +00:00
//TODO: Cambiare qui. Rallenta.
2016-01-04 16:46:51 +00:00
printf ( " %c " , ( char ) DOUBLELINE ) ;
2016-01-03 16:43:02 +00:00
}
//Anche qui, credo si possa migliorare qualcosa...
2016-01-03 18:02:19 +00:00
if ( player . pozioni_vita_piccole > 0 )
2016-01-03 16:43:02 +00:00
{
2016-01-04 16:46:51 +00:00
printf ( " %dx Pozione di Vita (p)iccola \t Ripristina 10 Vita \n " , player . pozioni_vita_piccole ) ;
2016-01-03 16:43:02 +00:00
}
else
{
2016-01-04 16:46:51 +00:00
printf ( " \n " ) ;
2016-01-03 16:43:02 +00:00
}
2016-01-03 18:02:19 +00:00
if ( player . pozioni_vita_medie > 0 )
2016-01-03 16:43:02 +00:00
{
2016-01-04 16:46:51 +00:00
printf ( " %dx Pozione di Vita (n)ormale \t Ripristina 20 Vita \n " , player . pozioni_vita_medie ) ;
2016-01-03 16:43:02 +00:00
}
else
{
2016-01-04 16:46:51 +00:00
printf ( " \n " ) ;
2016-01-03 16:43:02 +00:00
}
2016-01-03 18:02:19 +00:00
if ( player . pozioni_vita_grandi > 0 )
2016-01-03 16:43:02 +00:00
{
2016-01-04 16:46:51 +00:00
printf ( " %dx Pozione di Vita (m)aggiore \t Ripristina 50 Vita \n " , player . pozioni_vita_grandi ) ;
2016-01-03 16:43:02 +00:00
}
else
{
2016-01-04 16:46:51 +00:00
printf ( " \n " ) ;
2016-01-03 16:43:02 +00:00
}
2016-01-04 16:20:46 +00:00
for ( int i = 0 ; i < X_MAX ; i + + )
{
2016-01-04 16:46:51 +00:00
printf ( " %c " , ( char ) DOUBLELINE ) ;
2016-01-04 16:20:46 +00:00
}
2016-01-03 16:43:02 +00:00
//Selezione dell'oggetto da usare.
2016-01-04 16:46:51 +00:00
printf ( " Scrivi la lettera corrispondente all'oggetto che vuoi usare. \n Esci con Esc. \n " ) ;
2016-01-03 16:43:02 +00:00
while ( true )
{
//Effetto degli oggetti
unsigned char selezione = getch ( ) ;
if ( selezione = = 112 ) //p
{
2016-01-03 18:02:19 +00:00
if ( player . pozioni_vita_piccole > 0 )
2016-01-03 16:43:02 +00:00
{
2016-01-03 18:02:19 +00:00
player . pozioni_vita_piccole - - ;
2016-01-03 16:45:52 +00:00
player . heal ( 10 ) ;
2016-01-03 16:43:02 +00:00
break ;
}
}
else if ( selezione = = 110 ) //n
{
2016-01-03 18:02:19 +00:00
if ( player . pozioni_vita_medie > 0 )
2016-01-03 16:43:02 +00:00
{
2016-01-03 18:02:19 +00:00
player . pozioni_vita_medie - - ;
2016-01-03 16:45:52 +00:00
player . heal ( 20 ) ;
2016-01-03 16:43:02 +00:00
break ;
}
}
else if ( selezione = = 109 ) //m
{
2016-01-03 18:02:19 +00:00
if ( player . pozioni_vita_grandi > 0 )
2016-01-03 16:43:02 +00:00
{
2016-01-03 18:02:19 +00:00
player . pozioni_vita_grandi - - ;
2016-01-03 16:45:52 +00:00
player . heal ( 50 ) ;
2016-01-03 16:43:02 +00:00
break ;
}
}
2016-01-04 16:19:07 +00:00
else if ( selezione = = 27 ) //esc
{
break ;
}
2016-01-03 16:43:02 +00:00
}
}
2015-12-27 14:31:40 +00:00
//Funzioni per la generazione della mappa
2015-12-27 14:23:04 +00:00
//Inizializza la mappa con spazi vuoti
2015-12-29 18:51:19 +00:00
void init ( )
2015-12-27 14:23:04 +00:00
{
for ( int y = 0 ; y < Y_MAX ; y + + )
{
for ( int x = 0 ; x < X_MAX ; x + + )
{
2015-12-27 15:46:41 +00:00
map [ x ] [ y ] = WALL ;
2015-12-27 14:23:04 +00:00
}
}
}
2015-12-27 14:31:40 +00:00
//Crea una stanza quadrata
2015-12-29 18:51:19 +00:00
void room ( int start_x , int start_y , int end_x , int end_y )
2015-12-27 14:31:40 +00:00
{
for ( int y = start_y ; y < = end_y ; y + + )
{
for ( int x = start_x ; x < = end_x ; x + + )
{
2015-12-27 15:46:41 +00:00
map [ x ] [ y ] = EMPTY ;
2015-12-27 14:31:40 +00:00
}
}
}
2015-12-27 14:52:42 +00:00
//Crea un corridoio che connetta due punti
2015-12-29 18:51:19 +00:00
void corridor ( int start_x , int start_y , int end_x , int end_y , bool verticale )
2015-12-27 14:52:42 +00:00
{
if ( verticale )
{
if ( start_y > end_y )
{
for ( int y = end_y ; y < = start_y ; y + + )
{
2015-12-27 15:46:41 +00:00
map [ start_x ] [ y ] = EMPTY ;
2015-12-27 14:52:42 +00:00
}
}
else
{
for ( int y = start_y ; y < = end_y ; y + + )
{
2015-12-27 15:46:41 +00:00
map [ start_x ] [ y ] = EMPTY ;
2015-12-27 14:52:42 +00:00
}
}
if ( start_x > end_x )
{
for ( int x = end_x ; x < = start_x ; x + + )
{
2015-12-27 15:46:41 +00:00
map [ x ] [ end_y ] = EMPTY ;
2015-12-27 14:52:42 +00:00
}
}
else
{
for ( int x = start_x ; x < = end_x ; x + + )
{
2015-12-27 15:46:41 +00:00
map [ x ] [ end_y ] = EMPTY ;
2015-12-27 14:52:42 +00:00
}
}
}
else
{
if ( start_x > end_x )
{
for ( int x = end_x ; x < = start_x ; x + + )
{
2015-12-27 15:46:41 +00:00
map [ x ] [ start_y ] = EMPTY ;
2015-12-27 14:52:42 +00:00
}
}
else
{
for ( int x = start_x ; x < = end_x ; x + + )
{
2015-12-27 15:46:41 +00:00
map [ x ] [ start_y ] = EMPTY ;
2015-12-27 14:52:42 +00:00
}
}
if ( start_y > end_y )
{
for ( int y = end_y ; y < = start_y ; y + + )
{
2015-12-27 15:46:41 +00:00
map [ end_x ] [ y ] = EMPTY ;
2015-12-27 14:52:42 +00:00
}
}
else
{
for ( int y = start_y ; y < = end_y ; y + + )
{
2015-12-27 15:46:41 +00:00
map [ end_x ] [ y ] = EMPTY ;
2015-12-27 14:52:42 +00:00
}
}
}
}
2015-12-27 14:31:40 +00:00
2016-01-03 15:48:29 +00:00
//Genera il livello
2016-01-12 16:08:48 +00:00
void generate ( int enemies_to_place )
2015-12-27 15:34:17 +00:00
{
int corridor_x ;
int corridor_y ;
2015-12-29 18:12:15 +00:00
//Creazione delle stanze
2015-12-27 15:34:17 +00:00
for ( int r = 0 ; r < ROOMS ; r + + )
{
int size_x = rand ( ) % ROOM_SIZE + 1 ;
int size_y = rand ( ) % ROOM_SIZE + 1 ;
2015-12-27 20:40:59 +00:00
int start_x = rand ( ) % ( X_MAX - size_x - 2 ) + 1 ;
int start_y = rand ( ) % ( Y_MAX - size_y - 2 ) + 1 ;
2015-12-29 18:51:19 +00:00
room ( start_x , start_y , start_x + size_x , start_y + size_y ) ;
2016-01-12 16:38:33 +00:00
//Se non <20> la prima stanza, crea un corridoio che connetta quella appena generata con quella precedente
2015-12-27 15:34:17 +00:00
if ( r > 0 )
{
int link_x = rand ( ) % size_x + 1 + start_x ;
int link_y = rand ( ) % size_y + 1 + start_y ;
2015-12-29 18:51:19 +00:00
corridor ( link_x , link_y , corridor_x , corridor_y , rand ( ) % 2 ) ;
2015-12-27 15:34:17 +00:00
}
corridor_x = rand ( ) % size_x + start_x ;
corridor_y = rand ( ) % size_y + start_y ;
2016-01-12 16:38:33 +00:00
//Posiziona il giocatore se <20> l'ultima stanza
2015-12-29 18:12:15 +00:00
if ( r = = ROOMS - 1 )
{
map [ corridor_x ] [ corridor_y ] = PLAYER ;
2016-01-03 16:34:20 +00:00
player . x = corridor_x ;
player . y = corridor_y ;
2015-12-29 18:12:15 +00:00
}
2015-12-27 15:34:17 +00:00
}
2015-12-30 15:25:23 +00:00
//Posizionamento nemici
2016-01-12 16:08:48 +00:00
for ( int e = 0 ; e < enemies_to_place ; e + + )
2015-12-29 18:43:23 +00:00
{
while ( true )
{
int x = rand ( ) % ( X_MAX - 1 ) + 1 ;
int y = rand ( ) % ( Y_MAX - 1 ) + 1 ;
if ( map [ x ] [ y ] = = EMPTY )
{
map [ x ] [ y ] = ENEMY ;
Enemy * created = new Enemy ( ) ;
created - > x = x ;
created - > y = y ;
2016-01-12 17:21:11 +00:00
created - > hp = ENEMY_HP ;
created - > hp_max = ENEMY_HP ;
2015-12-29 18:43:23 +00:00
list [ e ] = created ;
break ;
}
}
}
2015-12-30 15:25:23 +00:00
//Posizionamento uscita
while ( true )
{
int x = rand ( ) % ( X_MAX - 1 ) + 1 ;
int y = rand ( ) % ( Y_MAX - 1 ) + 1 ;
if ( map [ x ] [ y ] = = EMPTY )
{
map [ x ] [ y ] = EXIT ;
break ;
}
}
2016-01-03 15:48:29 +00:00
//Posizionamento pozioni di vita
int placed_potions = 0 ;
2016-01-04 16:12:18 +00:00
int potions_in_floor = rand ( ) % MAX_POTIONS_PER_FLOOR ;
2016-01-03 15:48:29 +00:00
while ( placed_potions < potions_in_floor )
{
int x = rand ( ) % ( X_MAX - 1 ) + 1 ;
int y = rand ( ) % ( Y_MAX - 1 ) + 1 ;
int size = rand ( ) % 100 ;
if ( map [ x ] [ y ] = = EMPTY )
{
if ( size < 60 )
{
map [ x ] [ y ] = ITEM_SMALL_POTION ;
}
else if ( size > 90 )
{
map [ x ] [ y ] = ITEM_BIG_POTION ;
}
else
{
map [ x ] [ y ] = ITEM_MEDIUM_POTION ;
}
placed_potions + + ;
}
}
2015-12-27 15:34:17 +00:00
}
2015-12-29 18:51:19 +00:00
//Processa il resto di un turno, dopo il movimento del giocatore.
2016-01-12 16:08:48 +00:00
void tick ( int enemies_to_tick )
2015-12-29 18:51:19 +00:00
{
2016-01-12 16:08:48 +00:00
for ( int e = 0 ; e < enemies_to_tick ; e + + )
2015-12-29 18:51:19 +00:00
{
list [ e ] - > move ( ) ;
}
}
2016-01-10 15:57:18 +00:00
//Trova il puntatore al nemico in una certa posizione
2016-01-12 15:21:59 +00:00
Enemy * find ( int x , int y )
2016-01-10 15:57:18 +00:00
{
2016-01-12 16:08:48 +00:00
for ( int e = 0 ; e < MAX_ENEMIES ; e + + )
2016-01-10 15:57:18 +00:00
{
2016-01-12 16:38:33 +00:00
//Se c'<27> un nemico in quella posizione ED E' VIVO
2016-01-10 15:57:18 +00:00
if ( list [ e ] - > x = = x & & list [ e ] - > y = = y & & list [ e ] - > alive )
{
return list [ e ] ;
}
}
return NULL ;
}
2016-01-12 16:56:21 +00:00
2016-01-12 16:38:33 +00:00
void attack ( int x , int y )
{
2016-01-12 17:21:11 +00:00
find ( x , y ) - > damage ( player . atk ( ) ) ;
2016-01-12 16:38:33 +00:00
}
2016-01-12 16:56:21 +00:00
2015-12-27 14:23:04 +00:00
int main ( )
{
2016-01-04 16:19:07 +00:00
int seed ; //Seed casuale per generare il livello
2016-01-12 17:51:35 +00:00
srand ( time ( NULL ) ) ;
int titolo = 0 , cursorpos = 1 ;
string splash [ 10 ] { " Ora con ben 2 colori! " , " E' come skyrim, ma con una bella grafica! " , " Quasi senza bug " , " Potrebbe contenere canditi " , " Ora in grado di generare fame " , " I colori hanno ben 1 sfumatura " , " I SEE YOU " , " WOLOLO " , " AUTOMAAAAAH " , " Stai davvero giocando a sta roba? WOW! " } ;
2016-01-21 20:02:17 +00:00
cout < < ' \n ' ;
cout < < " 000000 \n " ;
cout < < " 000000 0000 0000 0000 0000 0000 000 000000 0000 000000 000000 000000 \n " ;
cout < < " 00 000 00 000000 000000 000000 \n " ;
cout < < " 00 0000 0000 0000 0000 0000 000 00 00 0000 00 00 000 00 00 \n " ;
cout < < " 00 0000 0000 0000 0000 0000 000 00 00 0000 00 00 000 00 00 \n " ;
cout < < " 000000 0000 0000 0000 0000 0000 000000 000000 0000 000000 000000 000000 \n " ;
cout < < " 000000 0000 0000 0000 0000 0000 000000 000000 0000 000000 000000 000000 \n " ;
2016-01-12 17:51:35 +00:00
titolo = rand ( ) % 10 ;
2016-01-21 20:02:17 +00:00
cout < < " \n \n Citazione all'avvio: " < < splash [ titolo ] < < ' \n ' ;
cout < < " \n \n \n \n Premi INVIO per entrare nel menu! " < < ' \n ' ;
2016-01-21 19:58:02 +00:00
getch ( ) ;
while ( true )
2016-01-12 17:51:35 +00:00
{
2016-01-21 19:58:02 +00:00
char selection = getch ( ) ;
system ( " cls " ) ;
if ( cursorpos = = 1 )
{
2016-01-21 20:02:17 +00:00
cout < < ' \n ' ;
cout < < " 000000 \n " ;
cout < < " 000000 0000 0000 0000 0000 0000 000 000000 0000 000000 000000 000000 \n " ;
cout < < " 00 000 00 000000 000000 000000 \n " ;
cout < < " 00 0000 0000 0000 0000 0000 000 00 00 0000 00 00 000 00 00 \n " ;
cout < < " 00 0000 0000 0000 0000 0000 000 00 00 0000 00 00 000 00 00 \n " ;
cout < < " 000000 0000 0000 0000 0000 0000 000000 000000 0000 000000 000000 000000 \n " ;
cout < < " 000000 0000 0000 0000 0000 0000 000000 000000 0000 000000 000000 000000 \n " ;
cout < < " \n \n > NUOVA PARTITA < \n " ;
cout < < " \n OPZIONI \n " ;
2016-01-21 19:58:02 +00:00
}
else if ( cursorpos = = 2 )
{
cout < < " " < < endl ;
cout < < " 000000 " < < endl ;
2016-01-21 20:02:17 +00:00
cout < < " 000000 0000 0000 0000 0000 0000 000 000000 0000 000000 000000 000000 \n " ;
cout < < " 00 000 00 000000 000000 000000 \n " ;
cout < < " 00 0000 0000 0000 0000 0000 000 00 00 0000 00 00 000 00 00 \n " ;
cout < < " 00 0000 0000 0000 0000 0000 000 00 00 0000 00 00 000 00 00 \n " ;
cout < < " 000000 0000 0000 0000 0000 0000 000000 000000 0000 000000 000000 000000 \n " ;
cout < < " 000000 0000 0000 0000 0000 0000 000000 000000 0000 000000 000000 000000 \n " ;
cout < < " \n \n NUOVA PARTITA \n " ;
cout < < " \n > OPZIONI < \n " ;
2016-01-21 19:58:02 +00:00
}
selection = getch ( ) ;
if ( selection = = 72 )
{
cursorpos - - ;
}
else if ( selection = = 80 )
{
cursorpos + + ;
}
else if ( selection = = 13 ) //PRESSIONE ENTER SU ELEMENTI MENU
{
if ( cursorpos = = 1 ) //AVVIO GIOCO
2016-01-12 17:51:35 +00:00
{
break ;
}
2016-01-21 19:58:02 +00:00
else if ( cursorpos = = 2 )
2016-01-12 17:51:35 +00:00
{
system ( " cls " ) ;
cout < < " Modificare tipo blocco? (S/N) " < < endl ;
char scelta ;
cin > > scelta ;
if ( scelta = = ' S ' | | scelta = = ' s ' )
{
2016-01-21 19:58:02 +00:00
WALL = 0xDB ;
2016-01-12 17:51:35 +00:00
}
}
}
}
printf ( " \n \n Seleziona un seed per la partita: " ) ;
2016-01-04 16:19:07 +00:00
cin > > seed ;
srand ( seed ) ;
2015-12-30 15:25:23 +00:00
//Ciclo del gioco
2015-12-28 16:10:11 +00:00
while ( true )
{
2016-01-12 16:12:24 +00:00
int enemies_in_level ;
if ( depth < MAX_ENEMIES )
{
enemies_in_level = depth ;
}
else
{
enemies_in_level = MAX_ENEMIES ;
}
2015-12-30 15:25:23 +00:00
init ( ) ;
2016-01-12 16:08:48 +00:00
generate ( enemies_in_level ) ;
2016-01-03 16:35:26 +00:00
draw ( ) ;
2015-12-30 15:25:23 +00:00
//Ciclo di un livello
while ( true )
{
2016-01-03 16:34:20 +00:00
int trigger = player . move ( ) ;
2015-12-30 15:25:23 +00:00
if ( trigger = = 1 ) //Uscita toccata
{
break ;
}
2016-01-12 16:08:48 +00:00
tick ( enemies_in_level ) ;
2015-12-30 15:25:23 +00:00
draw ( ) ;
}
depth + + ;
2015-12-28 16:10:11 +00:00
}
2015-12-27 14:23:04 +00:00
return 0 ;
}