Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// read from fildes til read "size" bytes or read "split" byte
read_until(int fildes, char* buf, int size, int split);
read_name(struct gun *gun) {
puts("Length of name...");
int n = read_int();
char* buf = malloc(n+1);
assert(buf != NULL);
puts("Input name:");
read_until( stdin, buf, n, '\n');
if( gun->name ) {
free(arg2[1]);
}
gun->name = buf;
puts("succeed.");
}
struct gun {
struct gun_vtable *vtable;
char *name;
int max_bullets;
int bullets;
}
struct gun_vtable {
void (*op_shoot)(struct gun*);
void (*op_reload)(struct gun*);
void (*op_info)(struct gun*);
}
const
struct gun_vtable gun1_vtable@0x1d30{
op_shoot = gun1_op_shoot@0x1590;
op_reload = gun1_op_reload@0x15d4;
op_info = gun1_op_info@0x15fa;
};
const
struct gun_vtable gun2_vtable@0x1d1c{
op_shoot = gun2_op_shoot@0x1654;
op_reload = gun2_op_reload@0x1698;
op_info = gun2_op_info@0x16be;
}
init_gun1(struct gun*) {
gun->vtable = &gun1_vtable;
gun->bullets = 15;
gun->max_bullets = 15;
gun->name = NULL;
}
init_gun2(struct gun*) {
gun->vtable = &gun2_vtable;
gun->bullets = 0x1e;
gun->max_bulletx = 0x1e;
gun->name = NULL;
}
// global variables
int guns[4];
struct gun* gun_data[4];
int selected_gun;
buy_a_gun() {
puts("You can only have up to 4 guns.");
puts("Choose a gun to add:");
puts("1. QSZ92");
puts("2. QBZ95");
int n = read_int();
for(int i = 0; i <= 3; i++ ) {
if( !guns[i] ) {
struct gun *gun;
if( n == 1 ) {
gun = malloc(0x10);
init_gun1(gun);
}else if( n == 2 ) {
gun = malloc(0x10);
init_gun2(gun);
}else {
puts("Wrong input.");
return;
}
read_name(gun)
guns[i] = 1;
gun_data[i] = gun;
return;
}
}
puts("You cant have more guns");
return;
}
select_a_gun() {
puts("Select a gun");
int n = read_int();
if( 0 <= n && n <= 3 && guns[i] ) {
selected_gun = n;
printf("Select No. %d.", n);
}
}
list_guns() {
puts("List of guns:");
for( i = 0; i <= 3; i++ ) {
if( guns[i] )
printf("No. %d, %s, %d/%d.",
i,
gun_data[i]->name,
gun_data[i]->bullets,
gun_data[i]->max_bullets);
}
}
rename_a_gun() {
puts("Choose a gun to rename:");
int n = read_int();
if( 0 <= n && n <= 3 && guns[i] ) {
read_name(gun_data[i]);
}
}
use_a_gun() {
struct gun *gun = gun_data[selected_gun];
printf("Select gun %s", gun->name);
puts("1. Shoot");
puts("2. Reload");
puts("3. Info");
puts("4. Main menu");
char buf[0x20];
while(true){
read_until(stdin, buf, 0x20, '\n');
int n = atoi(buf);
switch(n) {
case 1:
gun->vtable->op_shoot(gun);
break;
case 2:
gun->vtable->op_reload(gun);
break;
case 3:
gun->vtable->op_info(gun);
break;
case 4:
return;
default:
puts("Wrong input");
}
}
}
drop_a_gun() {
puts("Choose a gun to delete:");
int n = read_int();
if( 0 <= n && n <= 3 && guns[n] ) {
struct gun *gun = gun_data[n];
free(gun->name);
free(gun);
guns[n] = 0;
puts("Deleted");
}else {
puts("Wrong input");
}
}
main() {
setup();
put_logo();
while(true) {
put_menu();
int n = read_int();
if( n > 7 ) {
puts("Wrong input");
}
switch(n) {
case 1:
buy_a_gun();
break;
case 2:
select_a_gun();
break;
case 3:
list_guns();
break;
case 4:
rename_a_gun();
break;
case 5:
use_a_gun();
break;
case 6:
drop_a_gun();
break;
default:
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment