dipl/code/textadv/src/game.c
2020-03-17 17:50:19 +01:00

96 lines
2.2 KiB
C

/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "game.h"
#include "structures.h"
#include "16550.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>
char command_buffer[100];
uint8_t command_buffer_pointer = 0x00;
uint8_t current_room = 0x00;
void routine_game(){
if(command_buffer_pointer >= sizeof(command_buffer)){
command_buffer_pointer = 0x00;
memset(command_buffer, 0, sizeof(command_buffer));
println("\nToo much input!");
return;
}
if(command_buffer[command_buffer_pointer-1] == '\n' ||
command_buffer[command_buffer_pointer-1] == '\r'){
/* A command from the user has been received, we are ready to
* do something!*/
int8_t action_id = -1;
for(size_t i = 0; i < sizeof(action_table)/sizeof(const char*);
i++){
if(strncasecmp(action_table[i], command_buffer,
strlen(action_table[i])) == 0){
action_id = i;
break;
}
}
if(action_id < 0){
println(info_table[1]);
}else{
perform_action(action_id);
}
command_buffer_pointer = 0x00;
memset(command_buffer, 0, sizeof(command_buffer));
}
return;
}
void ingest_user_char(char in){
command_buffer[command_buffer_pointer++] = in;
return;
}
void perform_action(uint8_t action_id){
putchar_16550('\n', NULL);
switch(action_id){
default:
case ACTION_HELP:
println("You can:");
for(size_t i = 0; i < NUM_ACTIONS; i++){
println(" %s",action_table[i]);
}
break;
case ACTION_DESCRIBE:
println(room_table[current_room]);
putchar_16550('\n', NULL);
println(room_description_table[current_room]);
break;
};
return;
}