// ------------------------------------------------------------------------ int main( int argc, char *argv[] ) { // Create a hex board Hexboard hb = newHex( 11, 100.0, 100.0, 20); // Debugging printout printf("Created board of dimension %d\n", boardSizeHex( hb )); const Geometry* g = getGeoHex( hb ); printf("Bounding box of board: %g %g %g %g\n", g->x0, g->y0, g->x1, g->y1); printf("Hexagon dimensions:\n" " radius of inscribed circle = %g\n" " radius of circumscribed circle = %g\n" "-----------------------------------------------\n", g->h, g->s); // Create the game Game game = newGame( hb ); // Run the game startController( game, argc, argv ); // Clean up freeGame(game); freeHex( hb ); }
Game* loadBoard(char* name) { char reader = ' '; unsigned int rows = 0, cols = 0; FILE* fp = NULL; Game *g = NULL; if ( (fp = fopen(name, "r")) == NULL ) return NULL; if ( fscanf(fp, "Rows : %d\nCols : %d\n", &rows, &cols) != 2) { fclose(fp); return NULL; } g = __newGame(rows, cols); DEBUG_MSG("Rows : %d, Cols : %d\n", rows, cols); rows = 0; cols = 0; /* Reinit variable */ while ( (reader = fgetc(fp)) != EOF ) { if ( reader == '.' ) reader = DEAD_CELL; if ( reader == '#' ) reader = ALIVE_CELL; if ( reader == '\n') ++rows; else g->current_board[POS(cols, rows, g)] = reader; if ( ++cols > g->cols ) cols = 0; /* We are going to go over cols due to \n */ } fclose(fp); if ( cols != g->cols && rows != g->rows ) { freeGame(g); return NULL; } return g; }
void freeAllResources() { int i; freeLevelResources(); /* Free the hud */ freeHud(); /* Free the dialog box */ freeDialogBox(); /* Free the script */ freeScript(); /* Free the game surfaces */ freeGame(); /* Free the font */ closeFont(game.font); closeFont(game.largeFont); /* Clear the collision grid */ freeCollisionGrid(); /* Free the menus */ freeMenus(); /* Free the pak file */ freePakFile(); /* Free the texture cache */ freeTextureCache(); if (key != NULL) { for (i=0;i<MAX_PROPS_FILES;i++) { free(key[i]); } free(key); key = NULL; } if (value != NULL) { for (i=0;i<MAX_PROPS_FILES;i++) { free(value[i]); } free(value); value = NULL; } }
void MyProgram::update(float timeDelta) { if(flagForShowlevel1==true) { timeDeltaForLevel1+=timeDelta; hideAll(); EleEnd = OverlayManager::getSingleton().getOverlayElement("End"); EleEnd->setCaption("Level One!"); mEndOverlay->show(); if(timeDeltaForLevel1>showDelay*timeDelta) { flagForShowlevel1=false; timeDeltaForLevel1=0; mEndOverlay->hide(); showAll(); } } else if(flagForShowlevel2==true) { timeDeltaForLevel2+=timeDelta; hideAll(); EleEnd = OverlayManager::getSingleton().getOverlayElement("End"); EleEnd->setCaption("Level Two!"); mEndOverlay->show(); if(timeDeltaForLevel2>showDelay*timeDelta) { flagForShowlevel2=false; timeDeltaForLevel2=0; mEndOverlay->hide(); showAll(); } } else if(!gameOver) { timeBoard(timeDelta); isGameOver(); if(bMove) { if(_keyboard->isKeyDown(KC_SPACE)&&(!myTimeDelta||myTimeDelta>timeDelta*bulletDelay)) { myTimeDelta=0; for(int i=0;i<allBullet.size();++i) { if(allBullet[i]->used==false) { allBullet[i]->used=true; allBullet[i]->mySceneNode->setVisible(true); allBullet[i]->myDirection=myPlayer->myDirection; allBullet[i]->myPos=myPlayer->myPos; break; } } } if (_keyboard->isKeyDown(KC_LEFT)) { Vector3 tempVec; tempVec.x=-myPlayer->myDirection.y; tempVec.y=myPlayer->myDirection.x; tempVec.z=myPlayer->myDirection.z; myPlayer->myPos.x = myPlayer->myPos.x + tempVec.x * myPlayer->mySpeed * timeDelta; myPlayer->myPos.y = myPlayer->myPos.y + tempVec.y * myPlayer->mySpeed * timeDelta; } if (_keyboard->isKeyDown(KC_RIGHT)) { Vector3 tempVec; tempVec.x=myPlayer->myDirection.y; tempVec.y=-myPlayer->myDirection.x; tempVec.z=myPlayer->myDirection.z; myPlayer->myPos.x = myPlayer->myPos.x + tempVec.x * myPlayer->mySpeed * timeDelta; myPlayer->myPos.y = myPlayer->myPos.y + tempVec.y * myPlayer->mySpeed * timeDelta; } if (_keyboard->isKeyDown(KC_UP)) { myPlayer->myPos.x = myPlayer->myPos.x + myPlayer->myDirection.x * myPlayer->mySpeed * timeDelta; myPlayer->myPos.y = myPlayer->myPos.y + myPlayer->myDirection.y * myPlayer->mySpeed * timeDelta; } if (_keyboard->isKeyDown(KC_DOWN)) { myPlayer->myPos.x = myPlayer->myPos.x - myPlayer->myDirection.x * myPlayer->mySpeed * timeDelta; myPlayer->myPos.y = myPlayer->myPos.y - myPlayer->myDirection.y * myPlayer->mySpeed * timeDelta; } //if(_keyboard->isKeyDown(KC_Q)) //{ // myPlayer->mySceneNode->yaw(Radian(timeDelta)*yawDelay); // myPlayer->zRotation(timeDelta*yawDelay); //} //if(_keyboard->isKeyDown(KC_E)) //{ // myPlayer->mySceneNode->yaw(Radian(-timeDelta)*yawDelay); // myPlayer->zRotation(-timeDelta*yawDelay); //} // set player position myPlayer->mySceneNode->setPosition(myPlayer->myPos); outOfBount(myPlayer); myTimeDelta+=timeDelta; } createNewEnemy(timeDelta); if(bReborn) { rebornPause++; if(rebornPause == 60) // one second { bMove = true; myPlayer->mySceneNode->setVisible(true); } if(rebornPause==120) // two second { bReborn = false; rebornPause = 0; } } bulletBehavior(timeDelta); explosionBehavior(timeDelta); asteriodBehavior(timeDelta); ballBehavior(timeDelta); } else { EleEnd = OverlayManager::getSingleton().getOverlayElement("End"); if(win&&flagForGameLevel) { gameLevel++; flagForGameLevel=false; } if(gameLevel==2) { EleEnd->setCaption("You Pass!"); mEndOverlay->show(); /*string path4 = "Celebrate.wav"; fmodSystem->createSound(path4.c_str(), FMOD_2D, 0, &passSound); fmodSystem->playSound(FMOD_CHANNEL_FREE, passSound, false, &channel); passSound->release();*/ } else { if(win) EleEnd->setCaption("You Win!"); else { EleEnd->setCaption("GameOver\n Fail!"); /*string path5 = "GameOver.wav"; fmodSystem->createSound(path5.c_str(), FMOD_2D, 0, &overSound); fmodSystem->playSound(FMOD_CHANNEL_FREE,overSound, false, &channel); overSound->release();*/ } mEndOverlay->show(); } if(_keyboard->isKeyDown(KC_RETURN)) { if(gameLevel==2) gameLevel=0; mEndOverlay->hide(); if(gameLevel==0) flagForShowlevel1=true; else flagForShowlevel2=true; freeGame(); //sound->release(); sound2->release(); //SpassSound->release(); //overSound->release(); rebuildGame(); } } }
void handleGameEvent(Game * g, Message * mes) { Player * sp = NULL; Game * sg = NULL; char * nick = NULL; Message nmes; if (mes->sndr_t == O_PLAYER) { sp = mes->sndr.player; nick = getNickname(sp); } if (mes->sndr_t == O_GAME) sg = mes->sndr.game; switch(mes->type) { case MEST_PLAYER_JOIN_GAME: info("%s has joined into game\n", nick); info("Players on server/in hall: %d/%d\n", srv.nPlayers, srv.hallPlayers); { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_PLAYER_JOINS_GAME], 1); i = htonl(sp->pid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in our game */ for (mp = g->players; mp != NULL; mp = mp->next) if (mp->player != sp) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } if (g->num > 0 && g->num == g->nPlayers) { nmes.sndr_t = O_GAME; nmes.sndr.game = g; nmes.rcvr_t = O_GAME; nmes.rcvr.game = g; nmes.type = MEST_START_GAME; nmes.len = 0; nmes.data = NULL; sendMessage(&nmes); } break; case MEST_PLAYER_LEAVE_GAME: info("%s has leaved game\n", nick); info("Players on server/in hall: %d/%d\n", srv.nPlayers, srv.hallPlayers); { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_PLAYER_LEAVES_GAME], 1); i = htonl(sp->pid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in our game */ for (mp = g->players; mp != NULL; mp = mp->next) if (mp->player != sp) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } break; case MEST_PLAYER_JOINS_HALL: { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_PLAYER_JOINS_HALL], 1); i = htonl(sp->pid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in hall */ for (mp = srv.players; mp != NULL; mp = mp->next) if (mp->player != sp) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } break; case MEST_PLAYER_LEAVES_HALL: { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_PLAYER_LEAVES_HALL], 1); i = htonl(sp->pid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in hall */ for (mp = srv.players; mp != NULL; mp = mp->next) if (mp->player != sp) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } break; case MEST_NEW_GAME: { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_NEW_GAME], 1); i = htonl(((Game *)(mes->data))->gid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in hall */ for (mp = srv.players; mp != NULL; mp = mp->next) if (mp->player != sp) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } break; case MEST_PLAYERS_STAT_GAME: { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_PLAYERS_STAT_GAME], 1); i = htonl(g->nPlayers); addnStr(msg, &i, sizeof(i)); i = htonl(g->num); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in our game */ for (mp = g->players; mp != NULL; mp = mp->next) if (mp->player != sp) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } break; case MEST_COMMAND_GET: info("%s trying to get something\n", nick); if (g == NULL) { info("%s didn't get anything\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { uint32_t i = g->num; i = htonl(i); info("%s get %d\n", nick, g->num); write(sp->fd, answrs[ANSWR_STAT], 1); write(sp->fd, &i, sizeof(i)); } break; case MEST_COMMAND_SET: info("%s trying to set something into %d\n", nick, *(uint32_t *)(mes->data)); if (g == NULL) { info("%s not in game\n", nick, *(uint32_t *)(mes->data)); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { info("%s successfully sets something into %d\n", nick, *(uint32_t *)(mes->data)); g->num = *(uint32_t *)(mes->data); write(sp->fd, answrs[ANSWR_OK], 1); if (g->num > 0 && g->num <= g->nPlayers) { nmes.sndr_t = O_GAME; nmes.sndr.game = g; nmes.rcvr_t = O_GAME; nmes.rcvr.game = g; nmes.type = MEST_START_GAME; nmes.len = 0; nmes.data = NULL; sendMessage(&nmes); } } free(mes->data); break; case MEST_COMMAND_LEAVE: info("%s trying to leave us\n", nick); if (g == NULL) { info("%s not in game\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { info("%s leaves game #%d\n", nick, g->gid); IntoNil(sp); write(sp->fd, answrs[ANSWR_OK], 1); nmes.sndr_t = O_PLAYER; nmes.sndr.player = sp; nmes.rcvr_t = O_GAME; nmes.rcvr.game = g; nmes.type = MEST_PLAYER_LEAVE_GAME; nmes.len = 0; nmes.data = NULL; sendMessage(&nmes); nmes.sndr_t = O_PLAYER; nmes.sndr.player = sp; nmes.rcvr_t = O_GAME; nmes.rcvr.game = NULL; nmes.type = MEST_PLAYER_JOINS_HALL; nmes.len = sizeof(g); nmes.data = g; sendMessage(&nmes); nmes.sndr_t = O_PLAYER; nmes.sndr.player = sp; nmes.rcvr_t = O_GAME; nmes.rcvr.game = g; nmes.type = MEST_PLAYERS_STAT_GAME; nmes.len = 0; nmes.data = NULL; sendMessage(&nmes); } break; case MEST_COMMAND_START: info("%s trying to start game\n", nick); if (!sp->adm) { info("%s not granted to start games\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (g == NULL) { info("%s not in game\n", nick); char err = 1; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (g->started) { info("Game #%d is already started\n", g->gid); char err = 3; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (g->nPlayers == 0) { info("No players in game #%d\n", g->gid); char err = 2; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { write(sp->fd, answrs[ANSWR_OK], 1); nmes.sndr_t = O_PLAYER; nmes.sndr.player = sp; nmes.rcvr_t = O_GAME; nmes.rcvr.game = g; nmes.type = MEST_START_GAME; nmes.len = 0; nmes.data = NULL; sendMessage(&nmes); } break; case MEST_COMMAND_TURN: info("%s trying to turn in game\n", nick); if (g == NULL) { info("%s not in game\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (!g->started) { info("Game #%d isn't started\n", g->gid); char err = 2; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (sp->turned) { info("%s already turned\n", nick); char err = 1; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { int ntp = 0; /* not turned players */ mPlayer * mp; write(sp->fd, answrs[ANSWR_OK], 1); /* Semd info message about turned player */ { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_PLAYER_TURNED], 1); i = htonl(sp->pid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in our game */ for (mp = g->players; mp != NULL; mp = mp->next) if (mp->player != sp) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } sp->turned = 1; for (mp = g->players; mp != NULL; mp = mp->next) if (!mp->player->turned) ++ntp; info("Turned players: %d/%d\n", g->nPlayers, ntp); if (ntp > 0) /* Send info message about not turned players */ { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_NOT_TURNED_PLAYERS_LEFT], 1); i = htonl(g->nPlayers); addnStr(msg, &i, sizeof(i)); i = htonl(ntp); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in our game */ for (mp = g->players; mp != NULL; mp = mp->next) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } else { nmes.sndr_t = O_GAME; nmes.sndr.game = g; nmes.rcvr_t = O_GAME; nmes.rcvr.game = g; nmes.type = MEST_TURN_ENDED; nmes.len = 0; nmes.data = NULL; sendMessage(&nmes); } } break; case MEST_COMMAND_PROD: info("%s trying to product something in game\n", nick); { uint32_t n = *(uint32_t *)(mes->data); if (g == NULL) { info("%s not in game\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (sp->turned) { info("%s already turned\n", nick); char err = 3; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (!g->started) { info("Game #%d isn't started\n", g->gid); char err = 4; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n < 1 || n > sp->bf) { info("Nonavail product value got\n"); char err = 1; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n*(g->prodPrice) > sp->money || n*(g->prodStuff) > sp->mat) { info("Not enough resources to product\n"); char err = 2; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { write(sp->fd, answrs[ANSWR_OK], 1); sp->prodBid.c = n; } } free(mes->data); break; case MEST_COMMAND_MARKET: info("%s trying to take market information in game\n", nick); if (g == NULL) { info("%s not in game\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (!g->started) { info("Game #%d isn't started\n", g->gid); char err = 1; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { uint32_t i; char * str; Buffer * buf = newBuffer(); i = htonl(g->nPlayers); addnStr(buf, &i, sizeof(i)); i = htonl(g->turn); addnStr(buf, &i, sizeof(i)); i = htonl(g->state.sellC); addnStr(buf, &i, sizeof(i)); i = htonl(g->state.sellMinP); addnStr(buf, &i, sizeof(i)); i = htonl(g->state.buyC); addnStr(buf, &i, sizeof(i)); i = htonl(g->state.buyMaxP); addnStr(buf, &i, sizeof(i)); write(sp->fd, answrs[ANSWR_MARKET], 1); i = buf->count; str = flushBuffer(buf); write(sp->fd, str, i); free(str); clearBuffer(buf); free(buf); } break; case MEST_COMMAND_BUILD: info("%s trying to build something in game\n", nick); { uint32_t n = *(uint32_t *)(mes->data); if (g == NULL) { info("%s not in game\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (!g->started) { info("Game #%d isn't started\n", g->gid); char err = 4; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (sp->turned) { info("%s already turned\n", nick); char err = 3; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n < 1) { info("Nonavail build value got\n"); char err = 1; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if ((g->bFacPrice/2) > sp->money) { info("Not enough resources to build\n"); char err = 2; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { write(sp->fd, answrs[ANSWR_OK], 1); for (; n > 0; n--) addFactory(sp); } } free(mes->data); break; case MEST_COMMAND_SELL: info("%s trying to sell something in game\n", nick); { uint32_t n = *(uint32_t *)(mes->data); uint32_t p = *(uint32_t *)(mes->data + 4); if (g == NULL) { info("%s not in game\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (!g->started) { info("Game #%d isn't started\n", g->gid); char err = 5; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (sp->turned) { info("%s already turned\n", nick); char err = 4; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n != 0 && p > g->state.buyMaxP) { info("Price that is offered by %s is too high\n", nick); char err = 1; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n > sp->prod) { info("%s don't have enough product\n", nick); char err = 2; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n > g->state.buyC) { info("%s offered too many product to sell\n", nick); char err = 3; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { write(sp->fd, answrs[ANSWR_OK], 1); sp->sellBid.c = n; sp->sellBid.p = p; } } free(mes->data); break; case MEST_COMMAND_BUY: info("%s trying to buy something in game\n", nick); { uint32_t n = *(uint32_t *)(mes->data); uint32_t p = *(uint32_t *)(mes->data + 4); if (g == NULL) { info("%s not in game\n", nick); char err = 0; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (!g->started) { info("Game #%d isn't started\n", g->gid); char err = 5; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (sp->turned) { info("%s already turned\n", nick); char err = 4; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n != 0 && p < g->state.sellMinP) { info("Price that is offered by %s is too small\n", nick); char err = 1; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n*p > sp->money) { info("%s don't have enough money\n", nick); char err = 2; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else if (n > g->state.sellC) { info("%s offered too many material to buy\n", nick); char err = 3; write(sp->fd, answrs[ANSWR_ERROR], 1); write(sp->fd, &err, 1); } else { write(sp->fd, answrs[ANSWR_OK], 1); sp->buyBid.c = n; sp->buyBid.p = p; } } free(mes->data); break; case MEST_GAME_OVER: { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_GAME_OVER], 1); i = htonl(g->gid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in hall */ for (mp = srv.players; mp != NULL; mp = mp->next) if (mp->player != sp) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } freeGame(g); break; case MEST_START_GAME: info("Game #%d started with %d players\n", g->gid, g->nPlayers); /* Send info message about game start */ { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_START_GAME], 1); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in our game */ for (mp = g->players; mp != NULL; mp = mp->next) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } startGame(g); break; case MEST_TURN_ENDED: info("New turn in game #%d\n", g->gid); { Turn * t; Buffer * msgshr = newBuffer(); mPlayer * mp; Bid * b; char * strshr; int lenshr; uint32_t i; t = turnGame(g); /* generating shared MES part */ addnStr(msgshr, infos[INFO_TURN_ENDED], 1); i = htonl(t->turn); addnStr(msgshr, &i, sizeof(i)); i = htonl(countBids(t->sell) + countBids(t->buy)); addnStr(msgshr, &i, sizeof(i)); for (b = t->buy->f; b != NULL; b = b->next) { i = htonl(b->type); addnStr(msgshr, &i, sizeof(i)); i = htonl(b->p->pid); addnStr(msgshr, &i, sizeof(i)); i = htonl(b->count); addnStr(msgshr, &i, sizeof(i)); i = htonl(b->price); addnStr(msgshr, &i, sizeof(i)); } for (b = t->sell->f; b != NULL; b = b->next) { i = htonl(b->type); addnStr(msgshr, &i, sizeof(i)); i = htonl(b->p->pid); addnStr(msgshr, &i, sizeof(i)); i = htonl(b->count); addnStr(msgshr, &i, sizeof(i)); i = htonl(b->price); addnStr(msgshr, &i, sizeof(i)); } lenshr = msgshr->count; strshr = flushBuffer(msgshr); for (mp = g->players; mp != NULL; mp = mp->next) { Buffer * info = newBuffer(); Buffer * msg = newBuffer(); char * str; /* generating private MES part */ addnStr(msg, strshr, lenshr); if (countBids(t->prod) == 0) { i = htonl(0); addnStr(msg, &i, sizeof(i)); } else { for (b = t->prod->f; b != NULL && b->p != mp->player; b = b->next); if (b == NULL) i = htonl(0); else i = htonl(b->count); addnStr(msg, &i, sizeof(i)); } if (countBids(t->fac) == 0) { i = htonl(0); addnStr(msg, &i, sizeof(i)); } else { for (b = t->fac->f; b != NULL && b->p != mp->player; b = b->next); if (b == NULL) i = htonl(0); else i = htonl(b->count); addnStr(msg, &i, sizeof(i)); } /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } free(strshr); clearBuffer(msgshr); free(msgshr); remTurn(t); } break; case MEST_PLAYER_BANKRUPT: sp = (Player *)(mes->data); info("%s had bankrupted in game #%d\n", getNickname(sp), g->gid); { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_PLAYER_BANKRUPT], 1); i = htonl(sp->pid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in our game */ for (mp = g->players; mp != NULL; mp = mp->next) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } nmes.sndr_t = O_PLAYER; nmes.sndr.player = sp; nmes.rcvr_t = O_GAME; nmes.rcvr.game = NULL; nmes.type = MEST_PLAYER_JOINS_HALL; nmes.len = 0; nmes.data = NULL; sendMessage(&nmes); IntoNil(sp); break; case MEST_PLAYER_WIN: sp = (Player *)(mes->data); info("%s had win in game #%d\n", getNickname(sp), g->gid); { Buffer * msg = newBuffer(); Buffer * info = newBuffer(); mPlayer * mp; char * str; uint32_t i; /* generating MES part */ addnStr(msg, infos[INFO_PLAYER_WIN], 1); i = htonl(sp->pid); addnStr(msg, &i, sizeof(i)); /* generating INFO part */ addnStr(info, answrs[ANSWR_INFO], 1); i = htonl(msg->count); addnStr(info, &i, sizeof(i)); i = msg->count; str = flushBuffer(msg); addnStr(info, str, i); free(str); /* sending info message */ i = info->count; str = flushBuffer(info); /* players in our game */ for (mp = g->players; mp != NULL; mp = mp->next) write(mp->player->fd, str, i); free(str); clearBuffer(msg); free(msg); clearBuffer(info); free(info); } nmes.sndr_t = O_PLAYER; nmes.sndr.player = sp; nmes.rcvr_t = O_GAME; nmes.rcvr.game = NULL; nmes.type = MEST_PLAYER_JOINS_HALL; nmes.len = 0; nmes.data = NULL; sendMessage(&nmes); IntoNil(sp); break; } }