/** * This function cleans up output files. */ static void core_cleanup_files(void) { core_debug(""); core_debug(" +----------------------+"); core_debug(" | TURBO BOMBER STOPPED |"); core_debug(" +----------------------+"); core_debug(""); if(core_file_debug != NULL) { fclose(core_file_debug); } if(core_file_error != NULL) { fclose(core_file_error); } }
/** * This function initializes output files. */ static void core_init_files(void) { core_file_debug = fopen("debug.log", "a"); if(core_file_debug == NULL) { fprintf(stderr, "Failed to open debug log fvoid core_init(void)ile.\n"); exit(1); return; } core_file_error = fopen("error.log", "a"); if(core_file_error == NULL) { fprintf(stderr, "Failed to open error log file.\n"); fclose(core_file_debug); exit(1); return; } core_debug(""); core_debug(" +----------------------+"); core_debug(" | TURBO BOMBER STARTED |"); core_debug(" +----------------------+"); core_debug(""); core_debug("Log system initialized."); }
static rstatus_t core_process_messages(void) { //loga("Leng of C2G_OutQ ::: %d", CBUF_Len( C2G_OutQ )); while (!CBUF_IsEmpty(C2G_OutQ)) { struct ring_msg *msg = (struct ring_msg *) CBUF_Pop(C2G_OutQ); if (msg != NULL && msg->cb != NULL) { msg->cb(msg); core_debug(msg->sp->ctx); ring_msg_deinit(msg); } } return DN_OK; }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; srand((unsigned) time(NULL)); ctx = dn_alloc(sizeof(*ctx)); if (ctx == NULL) { return NULL; } ctx->id = ++ctx_id; ctx->cf = NULL; ctx->stats = NULL; ctx->evb = NULL; array_null(&ctx->pool); ctx->max_timeout = nci->stats_interval; ctx->timeout = ctx->max_timeout; ctx->dyn_state = INIT; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { loga("Failed to create context!!!"); dn_free(ctx); return NULL; } /* initialize server pool from configuration */ status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != DN_OK) { loga("Failed to initialize server pool!!!"); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* crypto init */ status = crypto_init(ctx); if (status != DN_OK) { loga("Failed to initialize crypto!!!"); dn_free(ctx); return NULL; } /* create stats per server pool */ ctx->stats = stats_create(nci->stats_port, nci->stats_addr, nci->stats_interval, nci->hostname, &ctx->pool, ctx); if (ctx->stats == NULL) { loga("Failed to create stats!!!"); crypto_deinit(); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize event handling for client, proxy and server */ ctx->evb = event_base_create(EVENT_SIZE, &core_core); if (ctx->evb == NULL) { loga("Failed to create socket event handling!!!"); crypto_deinit(); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* preconnect? servers in server pool */ status = server_pool_preconnect(ctx); if (status != DN_OK) { loga("Failed to preconnect for server pool!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize proxy per server pool */ status = proxy_init(ctx); if (status != DN_OK) { loga("Failed to initialize proxy!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize dnode listener per server pool */ status = dnode_init(ctx); if (status != DN_OK) { loga("Failed to initialize dnode!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } ctx->dyn_state = JOINING; //TODOS: change this to JOINING /* initialize peers */ status = dnode_peer_init(&ctx->pool, ctx); if (status != DN_OK) { loga("Failed to initialize dnode peers!!!"); crypto_deinit(); dnode_deinit(ctx); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } core_debug(ctx); /* preconntect peers - probably start gossip here */ status = dnode_peer_pool_preconnect(ctx); if (status != DN_OK) { loga("Failed to preconnect dnode peers!!!"); crypto_deinit(); dnode_peer_deinit(&ctx->pool); dnode_deinit(ctx); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } //init ring msg queue CBUF_Init(C2G_InQ); CBUF_Init(C2G_OutQ); //init stats msg queue CBUF_Init(C2S_InQ); CBUF_Init(C2S_OutQ); gossip_pool_init(ctx); log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }
/** * This function starts the main game loop. This function runs periodically * all neccessary functions for rendering. */ void core_main(void) { int character = 0; core_state_t saved_state = CORE_RUNNING; gameplay_players_player_t *player = NULL; char screenshot_request = 0; while(core_state != CORE_SHUTDOWN) { clear(); if(core_state == CORE_RUNNING) { gameplay_update(); } while((character = getch()) != ERR) { switch(character) { // <Q> or <Esc>: Quit game loop // <C>: Show QR code // <V>: Take screenshot // <P>: Pause or resume game loop updating // <W>: Move user player up // <A>: Move user player left // <S>: Move user player down // <D>: Move user player right // <E>: Pick up item (user player) // <SPACE>: Place bomb (user player), skip start screen, return to menu // <T>: TURBO-MODE // <U>: TURBO-MODE // <R>: TURBO-MODE // <B>: TURBO-MODE // <O>: TURBO-MODE // <M>: Return to menu case 'q': case 27: // 27 = <Esc> { core_debug("Invoked quit event."); core_state = CORE_SHUTDOWN; break; } case 'v': { // only request screenshot, take it at the end of drawing screenshot_request = 1; break; } case 'c': { if(core_state != CORE_QR_CODE) { saved_state = core_state; core_state = CORE_QR_CODE; } else { core_state = saved_state; } break; } case 'p': { if(core_state == CORE_RUNNING) { core_state = CORE_PAUSED; core_debug(""); core_debug(" +----------------------+"); core_debug(" | TURBO BOMBER PAUSED |"); core_debug(" +----------------------+"); core_debug(""); core_debug("Press <P> to resume."); break; } if(core_state == CORE_PAUSED) { core_state = CORE_RUNNING; core_debug(""); core_debug(" +----------------------+"); core_debug(" | TURBO BOMBER RESUMED |"); core_debug(" +----------------------+"); core_debug(""); break; } } // case 'w': case 'a': case 's': case 'd': case ' ': case 'e': case 't': case 'u': case 'r': case 'b': case 'o': case 'm': { // skip start screen if(character == ' ' && core_state == CORE_START_SCREEN) { core_state = CORE_MENU; break; } // start game if(character == ' ' && core_state == CORE_MENU) { core_state = CORE_RUNNING; gameplay_init(); break; } // return to menu from end screen if(character == ' ' && (core_state == CORE_WIN || core_state == CORE_GAME_OVER)) { core_state = CORE_MENU; break; } // return to menu from everywhere if(character == 'm') { if(core_state == CORE_RUNNING || core_state == CORE_PAUSED) { gameplay_cleanup(); } core_state = CORE_MENU; break; } // only process keyboard input when game is unpaused if(core_state != CORE_PAUSED) { gameplay_key(character); } break; } default: { core_debug("Pressed unhandled key. Keycode: %i", character); break; } } } switch(core_state) { case CORE_START_SCREEN: { if(graphics_render_start_screen() == 0) { core_state = CORE_MENU; } break; } case CORE_MENU: { graphics_render_menu(); break; } case CORE_RUNNING: case CORE_PAUSED: { graphics_render_field(); graphics_render_players(); #ifdef DEBUG_INFO graphics_render_debug(); #else graphics_render_information(); #endif /* DEBUG_INFO */ player = gameplay_players_get_user(); if(player->health_points == 0) { core_cutscene_reset = 1; core_state = CORE_GAME_OVER; gameplay_cleanup(); } else if(gameplay_players_ai_amount() == 0) { core_state = CORE_WIN; gameplay_cleanup(); } break; } case CORE_QR_CODE: { graphics_render_qr_code(); break; } case CORE_WIN: { graphics_render_win_screen(core_cutscene_reset); core_cutscene_reset = 0; break; } case CORE_GAME_OVER: { graphics_render_game_over_screen(core_cutscene_reset); core_cutscene_reset = 0; break; } default: { break; } } if(screenshot_request == 1) { graphics_sprites_screenshot(); screenshot_request = 0; } move(0, 0); refresh(); if(core_state != CORE_SHUTDOWN) { usleep(CORE_FRAME_TIME); } } }