int doRPC(Client *C) { int rc; char c; printf("enter (h|m<c>|g): "); scanf("%c", &c); rc=doRPCCmd(C,c); printf("doRPC: rc=0x%x\n", rc); return rc; }
void Wander(Client *C, int direction) { if (direction == 0) { Proto_Client *client = (Proto_Client *) C->ph; client->rpc_session.shdr.returnCode = RIGHT; doRPCCmd(C, 'm'); // query map } else if (direction == 1) { Proto_Client *client = (Proto_Client *) C->ph; client->rpc_session.shdr.returnCode = DOWN; doRPCCmd(C, 'm'); // query map } else if (direction == 2) { Proto_Client *client = (Proto_Client *) C->ph; client->rpc_session.shdr.returnCode = LEFT; doRPCCmd(C, 'm'); // query map } else if (direction == 3) { Proto_Client *client = (Proto_Client *) C->ph; client->rpc_session.shdr.returnCode = UP; doRPCCmd(C, 'm'); // query map direction = -1; //Resets direction } //struct timespec tim, tim2; //tim.tv_sec = 0; //tim.tv_nsec = 5000000000L;//Change speed of requests here //nanosleep(&tim , &tim2); sleep(1); direction++; Wander(C, direction); }
int doRPC(Client *C) { int rc; char c; // Enter command, h=hello, m<c> = move c steps, g = goodbye if (PROTO_PRINT_DUMPS==1) printf("enter (h|m<c>|g): \n"); scanf("%c", &c); rc=doRPCCmd(C,c); if (PROTO_PRINT_DUMPS==1) printf("doRPC: rc=0x%x\n", rc); return rc; }
extern sval ui_keypress(UI *ui, SDL_KeyboardEvent *e, Client *C) { SDLKey sym = e->keysym.sym; SDLMod mod = e->keysym.mod; if (e->type == SDL_KEYDOWN) { if (sym == SDLK_LEFT && mod == KMOD_NONE) { fprintf(stderr, "%s: move left\n", __func__); Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = LEFT; doRPCCmd(C, 'm'); return 2; } if (sym == SDLK_RIGHT && mod == KMOD_NONE) { fprintf(stderr, "%s: move right\n", __func__); Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = RIGHT; doRPCCmd(C, 'm'); return 2; } if (sym == SDLK_UP && mod == KMOD_NONE) { fprintf(stderr, "%s: move up\n", __func__); Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; doRPCCmd(C, 'm'); return 2; } if (sym == SDLK_t && mod == KMOD_NONE) { Wander(C, 0); return 2; } if (sym == SDLK_DOWN && mod == KMOD_NONE) { fprintf(stderr, "%s: move down\n", __func__); Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = DOWN; doRPCCmd(C, 'm'); return 2; } if (sym == SDLK_f && mod == KMOD_NONE) { fprintf(stderr, "%s: pickup \n", __func__); doRPCCmd(C, 'f'); return 2; } if (sym == SDLK_q && mod == KMOD_NONE) { doRPCCmd(C, 'g'); return -1; } if (sym == SDLK_z && mod == KMOD_NONE){ Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; Player *me = getPlayer(&client->game, client->playerID); return ui_zoom(ui, 1, &client->game, me); } if (sym == SDLK_z && mod & KMOD_SHIFT ){ Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; Player *me = getPlayer(&client->game, client->playerID); return ui_zoom(ui,-1, &client->game, me); } if (sym == SDLK_LEFT && mod & KMOD_SHIFT){ Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; Player *me = getPlayer(&client->game, client->playerID); return ui_pan(ui,-1,0, &client->game, me); } if (sym == SDLK_RIGHT && mod & KMOD_SHIFT){ Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; Player *me = getPlayer(&client->game, client->playerID); return ui_pan(ui,1,0, &client->game, me); } if (sym == SDLK_UP && mod & KMOD_SHIFT){ Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; Player *me = getPlayer(&client->game, client->playerID); return ui_pan(ui, 0,-1, &client->game, me); } if (sym == SDLK_DOWN && mod & KMOD_SHIFT){ Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; Player *me = getPlayer(&client->game, client->playerID); return ui_pan(ui, 0,1, &client->game, me); } else { fprintf(stderr, "%s: key pressed: %d\n", __func__, sym); } } else { fprintf(stderr, "%s: key released: %d\n", __func__, sym); } return 1; }
int main(int argc, char **argv) { Client c; if (!FASTINPUTMODE) fprintf(stderr, "Type 'connect <host:port>' to connect to a game.\n"); if (clientInit(&c) < 0) { fprintf(stderr, "ERROR: clientInit failed\n"); return -1; } initGlobals(argc, argv); if (FASTINPUTMODE) { startConnection(&c, globals.host, globals.port, update_event_handler); doRPCCmd(&c, 'q'); //query for the map if (STRESS_TEST==1) proto_client_hello(c.ph); } shell(&c); // Cannot put shell on a separate thread because fget() function doesn't work for some reason // pthread_t tid; // pthread_create(&tid, NULL, shell, &c); Proto_Client *proto_client = c.ph; Player *me = getPlayer(&proto_client->game, proto_client->playerID); if (DISPLAYUI==1) { // If I am not the host and we are stress testing, I should be wandering if (me->isHost==0 && STRESS_TEST==1) { // Wander(&c, 0); docmd(&c, "test\n"); } // The host will be the only ones that has UI showing, other players just wonder else { //window will be consistently 20x20 // pthread_t tid; // pthread_create(&tid, NULL, shell, NULL); // Init for UI stuff tty_init(STDIN_FILENO); ui_init(&(ui)); // WITH OSX ITS IS EASIEST TO KEEP UI ON MAIN THREAD // SO JUMP THROW HOOPS :-( Proto_Client *client = (Proto_Client *) c.ph; Player *me = getPlayer(&client->game, client->playerID); doRPCCmd(&c, 'q'); //query for the map ui_main_loop(ui, (32 * WINDOW_SIZE), (32 * WINDOW_SIZE), &client->game, me, &c); } } // launchUI(&c); return 0; }
int docmd(Client *C, char *cmd) { int rc = 1; Proto_Client *client = (Proto_Client *) C->ph; // If this is a connect attempt char input[50]; strcpy(input, cmd); int connectAttempt = check_if_connect(input); if (connectAttempt==1) { // Ok startup our connection to the server if (startConnection(C, globals.host, globals.port, update_event_handler)<0) { fprintf(stderr, "ERROR: startConnection failed\n"); return -1; } else { fprintf(stderr, "Successfully connected to <%s:%d>\n", globals.host, globals.port); proto_client_hello(C->ph); doRPCCmd(C, 'q'); //query for the map return 1; } return 1; } strcpy(input,cmd); if (strcmp(cmd, "disconnect\n")==0) { doRPCCmd(C, 'g'); rc=-1; } else if (strcmp(cmd, "where\n")==0) where(); else if (strcmp(cmd, "numhome 1\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numHome1!=0) fprintf(stderr, "%d\n", client->game.map.numHome1); } else if (strcmp(cmd, "numhome 2\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numHome2!=0) fprintf(stderr, "%d\n", client->game.map.numHome2); } else if (strcmp(cmd, "numjail 1\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numJail1!=0) fprintf(stderr, "%d\n", client->game.map.numJail1); } else if (strcmp(cmd, "numjail 2\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numHome2!=0) fprintf(stderr, "%d\n", client->game.map.numJail2); } else if (strcmp(cmd, "numwall\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numFixedWall!=0 && client->game.map.numNonfixedWall!=0) fprintf(stderr, "%d\n", client->game.map.numFixedWall + client->game.map.numNonfixedWall); } else if (strcmp(cmd, "numfloor\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numFloor1!=0 && client->game.map.numFloor2!=0) fprintf(stderr, "%d\n", client->game.map.numFloor1+client->game.map.numFloor2); } else if (strcmp(cmd, "dim\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.dimension.x!=0 && client->game.map.dimension.y!=0) fprintf(stderr, "%dx%d\n", client->game.map.dimension.x, client->game.map.dimension.y); } else if (strcmp(cmd, "dump\n")==0) { doRPCCmd(C, 'q'); // query map printMap(&client->game.map); if (DISPLAYUI==1) ui_update(ui); } else if (containsString(input, "cinfo")>0) { doRPCCmd(C, 'q'); // query map cinfo(cmd, C); } else if (strcmp(cmd, "start\n")==0) { doRPCCmd(C, 's'); // query map } else if (strcmp(cmd, "test\n")==0) { if (STRESS_TEST == 1) { pid_t myChild; myChild = fork(); if (myChild == 0) { Wander(C, 0); } else{} } } // MOVEMENT else if (strcmp(cmd, "w\n")==0) { Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; doRPCCmd(C, 'm'); // query map } else if (strcmp(cmd, "a\n")==0) { Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = LEFT; doRPCCmd(C, 'm'); // query map } else if (strcmp(cmd, "d\n")==0) { Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = RIGHT; doRPCCmd(C, 'm'); // query map } else if (strcmp(cmd, "s\n")==0) { Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = DOWN; doRPCCmd(C, 'm'); // query map } // END OF MOVEMENT //PICKUP else if (strcmp(cmd, "f\n")==0){ Proto_Client *client = C->ph; doRPCCmd(C, 'f'); } //END PICKUP else if (strcmp(cmd, "O\n")==0) proto_debug_on(); else if (strcmp(cmd, "o\n")==0) proto_debug_off(); else if (strcmp(cmd, "rh\n")==0) rc = proto_client_hello(C->ph); else if (strcmp(cmd, "q\n")==0) { fprintf(stderr, "Game Over: You Quit\n"); doRPCCmd(C, 'g'); rc=-1; } else if (strcmp(cmd, "quit\n")==0) { doRPCCmd(C, 'g'); fprintf(stderr, "Game Over: You Quit\n"); rc=-1; } else if (strcmp(cmd, "\n")==0) { rc = proto_client_update(C->ph); rc=1; } else { fprintf(stderr, "Unknown command\n"); } return rc; }