Test(Player, create) { player_t *pl = player_create(); cr_assert(pl); player_delete(pl); }
Test(Player, inventory_list_gfx) { player_t *pl = player_create_at((vector2d_t){4, 7}); player_inventory_add(pl, FOOD); dynbuf_t *buf = player_inventory_list(pl); cr_log_info(buf->b_data); cr_expect_neq(strstr(buf->b_data, "food 1"), 0); dynbuf_delete(buf); player_inventory_add(pl, FOOD); buf = player_inventory_list(pl); cr_log_info(buf->b_data); cr_expect_neq(strstr(buf->b_data, "food 2"), 0); dynbuf_delete(buf); player_inventory_add(pl, SIBUR); player_inventory_add(pl, SIBUR); player_inventory_add(pl, SIBUR); player_inventory_add(pl, SIBUR); buf = player_inventory_list_gfx(pl); cr_log_info(buf->b_data); cr_assert_str_eq(buf->b_data, "2 0 0 4 0 0 0"); dynbuf_delete(buf); player_delete(pl); }
Test(Player, inv_add) { player_t *pl = player_create(); cr_assert(pl); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); player_inventory_add(pl, FOOD); cr_expect_eq(player_inventory_get(pl, FOOD), 10); player_inventory_add(pl, SIBUR); player_inventory_add(pl, THYSTAME); player_inventory_add(pl, THYSTAME); player_inventory_add(pl, THYSTAME); player_inventory_add(pl, THYSTAME); player_inventory_add(pl, DERAUMERE); player_inventory_add(pl, DERAUMERE); cr_expect_eq(player_inventory_get(pl, FOOD), 10); cr_expect_eq(player_inventory_get(pl, SIBUR), 1); cr_expect_eq(player_inventory_get(pl, THYSTAME), 4); cr_expect_eq(player_inventory_get(pl, DERAUMERE), 2); player_delete(pl); }
static sexp sexp_player_delete_stub (sexp ctx, sexp self, sexp_sint_t n, sexp arg0) { sexp res; if (! (sexp_pointerp(arg0) && (sexp_pointer_tag(arg0) == sexp_unbox_fixnum(sexp_opcode_arg1_type(self))))) return sexp_type_exception(ctx, self, sexp_unbox_fixnum(sexp_opcode_arg1_type(self)), arg0); res = ((player_delete((struct player**)sexp_cpointer_value(arg0))), SEXP_VOID); return res; }
void terminate() { player_delete(my_player); world_delete(my_world); hud_delete(my_hud); server_disconnect(my_server); server_delete(my_server); }
/* -------------------------------------------------------------------------- * * -------------------------------------------------------------------------- */ void player_clear(void) { struct player *player; struct node *next; dlink_foreach_safe(&player_list, player, next) player_delete(player); }
Test(Player, MoveUp) { player_t *pl = player_create_at((vector2d_t){12, 12}); board_t *bd = board_create(24, 24); pl->p_dir = (vector2d_t){0, -1}; cr_assert_eq(pl->p_pos.v_y, 12); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 11); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 10); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 9); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 8); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 7); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 6); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 5); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 4); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 3); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 2); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 1); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 0); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 23, "got %d", pl->p_pos.v_y); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 22); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 21); cr_assert_eq(pl->p_pos.v_x, 12); board_delete(bd); player_delete(pl); }
/*ARGSUSED*/ void player_login(void *ud) { time_t deadline; char buf[128]; char space[128]; int res, ac, cmd, prev_state; player->proc = empth_self(); pr_id(player, C_INIT, "Empire server ready\n"); for (;;) { deadline = player_io_deadline(player, 0); if (io_outputwaiting(player->iop)) { if (io_output(player->iop, deadline) <= 0) break; continue; } if (io_gets(player->iop, buf, sizeof(buf)) < 0) { res = io_input(player->iop, deadline); if (res <= 0) break; continue; } journal_input(buf); ac = parse(buf, space, player->argp, NULL, NULL, NULL); if (ac <= 0) { pr_id(player, C_BADCMD, "Can't parse command\n"); continue; } cmd = comtch(player->argp[0], login_coms, 0); if (cmd < 0) { pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]); continue; } switch (login_coms[cmd].c_addr()) { case RET_OK: break; case RET_FAIL: break; case RET_SYN: pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form); break; default: break; } } prev_state = player->state; player->state = PS_SHUTDOWN; if (prev_state == PS_PLAYING) empth_rwlock_unlock(shutdown_lock); pr_id(player, C_EXIT, "so long...\n"); player_delete(player); empth_exit(); /*NOTREACHED*/ }
Test(Player, eject) { player_t *pl1 = player_create_at((vector2d_t){9, 9}); player_t *pl2 = player_create_at((vector2d_t){9, 9}); player_t *pl3 = player_create_at((vector2d_t){9, 9}); player_t *pl4 = player_create_at((vector2d_t){9, 15}); game_t *gm = game_create(20, 20, 7, 5); cr_assert(pl1); cr_assert(pl2); cr_assert(pl2); cr_assert(gm); game_add_team(gm, "pandas"); game_add_team(gm, "red-pandas"); pl1->p_teamname = strdup("pandas"); pl3->p_teamname = strdup("pandas"); pl2->p_teamname = strdup("red-pandas"); pl4->p_teamname = strdup("red-pandas"); cr_assert_neq(game_register_player(gm, pl1), -1); cr_assert_neq(game_register_player(gm, pl2), -1); cr_assert_neq(game_register_player(gm, pl3), -1); cr_assert_neq(game_register_player(gm, pl4), -1); pl2->p_dir = (vector2d_t){-1, 0}; player_eject(pl1, gm->ga_players, gm->ga_board); cr_expect_eq(pl2->p_pos.v_x, 8); cr_expect_eq(pl2->p_pos.v_y, 9); cr_expect_eq(pl3->p_pos.v_x, 10); cr_expect_eq(pl3->p_pos.v_y, 9); player_eject(pl1, gm->ga_players, gm->ga_board); cr_expect_eq(pl2->p_pos.v_x, 8); cr_expect_eq(pl2->p_pos.v_y, 9); cr_expect_eq(pl3->p_pos.v_x, 10); cr_expect_eq(pl3->p_pos.v_y, 9); player_delete(pl1); player_delete(pl2); player_delete(pl3); game_delete(gm); }
Test(Player, Move) { player_t *pl = player_create_at((vector2d_t){12, 12}); board_t *bd = board_create(24, 24); cr_assert_eq(pl->p_pos.v_x, 12); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 13); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 14); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 15); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 16); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 17); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 18); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 19); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 20); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 21); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 22); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 23); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 0); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 1); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 2); cr_assert_eq(pl->p_pos.v_y, 12); board_delete(bd); player_delete(pl); }
/* -------------------------------------------------------------------------- * * Destroy game heaps and cancel timer. * * -------------------------------------------------------------------------- */ void player_shutdown(void) { struct player *player; struct node *next; log(player_log, L_status, "Shutting down [player] module..."); dlink_foreach_safe(&player_list, player, next) player_delete(player); mem_static_destroy(&player_heap); log_source_unregister(player_log); }
Test(Player, TurnLeft) { player_t *pl = player_create_at((vector2d_t){12, 12}); pl->p_dir = (vector2d_t){1, 0}; player_turn_left(pl); cr_assert_eq(pl->p_dir.v_x, 0); cr_assert_eq(pl->p_dir.v_y, 1); player_turn_left(pl); cr_assert_eq(pl->p_dir.v_x, -1); cr_assert_eq(pl->p_dir.v_y, 0); player_turn_left(pl); cr_assert_eq(pl->p_dir.v_x, 0); cr_assert_eq(pl->p_dir.v_y, -1); player_turn_left(pl); cr_assert_eq(pl->p_dir.v_x, 1); cr_assert_eq(pl->p_dir.v_y, 0); player_delete(pl); }
/*ARGSUSED*/ void player_accept(void *unused) { static int conn_cnt; struct sockaddr *sap; struct player *np; socklen_t len; const char *p; int ns; int set = 1; int stacksize; char buf[128]; #ifdef RESOLVE_IPADDRESS struct hostent *hostp; #endif /* auto sockaddr_storage would be simpler, but less portable */ sap = malloc(player_addrlen); len = player_addrlen; if (getsockname(player_socket, sap, &len)) { logerror("getsockname() failed: %s", strerror(errno)); p = NULL; } else { p = sockaddr_ntop(sap, buf, sizeof(buf)); CANT_HAPPEN(!p); } logerror("Listening on %s", p ? buf : "unknown address"); while (1) { empth_select(player_socket, EMPTH_FD_READ, NULL); len = player_addrlen; ns = accept(player_socket, sap, &len); /* FIXME accept() can block on some systems (RST after select() reports ready) */ if (ns < 0) { logerror("new socket accept"); continue; } (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set)); np = player_new(ns); if (!np) { logerror("can't create player for fd %d", ns); close(ns); continue; } if (!sockaddr_ntop(sap, np->hostaddr, sizeof(np->hostaddr))) { CANT_REACH(); player_delete(np); continue; } logerror("Connect from %s", np->hostaddr); #ifdef RESOLVE_IPADDRESS hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family); if (NULL != hostp) strcpy(np->hostname, hostp->h_name); #endif /* RESOLVE_IPADDRESS */ /* FIXME ancient black magic; figure out true stack need */ stacksize = 100000 /* budget */ + MAX(WORLD_SZ() * sizeof(int) * 7, /* power */ MAXNOC * sizeof(struct powstr)); sprintf(buf, "Conn%d", conn_cnt++); empth_create(player_login, stacksize, 0, buf, np); } }
/* Read data from the network and work with it */ int network_loop () { char data[MAX_UDPDATA]; struct pkg *packet = (struct pkg *) data; int inlen, i; _net_addr addr; if (bman.state != GS_running && bman.state != GS_ready) timestamp = SDL_GetTicks (); /* as long as we get any new data, work with them */ inlen = udp_get (bman.sock, data, MAX_UDPDATA, &addr.sAddr, bman.net_ai_family); addr.port[0] = addr.host[0] = 0; if (inlen > 0) dns_filladdr (addr.host, LEN_SERVERNAME, addr.port, LEN_PORT, bman.net_ai_family, &addr.sAddr); while (inlen > 0) { do_pkg (packet, &addr, inlen); // printf ("Network : inlen (%d) typ (%d) Size (%d)\n", inlen, packet->typ, pkglen); inlen = udp_get (bman.sock, data, MAX_UDPDATA, &addr.sAddr, bman.net_ai_family); addr.port[0] = addr.host[0] = 0; if (inlen > 0) dns_filladdr (addr.host, LEN_SERVERNAME, addr.port, LEN_PORT, bman.net_ai_family, &addr.sAddr); } /* check here for old connections who aren't answering */ if (bman.state == GS_wait || bman.state == GS_ready || bman.state == GS_running) { if (GT_MP_PTPS) { if (net_check_timeout (bman.p_servnr)) { d_printf ("Server Timed Out\n"); bman.state = GS_startup; } } else if (GT_MP_PTPM) { for (i = 1; i < MAX_PLAYERS; i++) if (i != bman.p_nr && net_check_timeout (i)) { d_printf ("Player %d Timed Out\n", i); player_delete (i); } } } /* resend_cache.... */ rscache_loop (); /* dynamic calibration of the network traffic option */ net_dyn_pkgoption (); return 0; };