void CClient::newGame( CConnection *con, StartInfo *si ) { enum {SINGLE, HOST, GUEST} networkMode = SINGLE; if (con == nullptr) { CServerHandler sh; serv = sh.connectToServer(); } else { serv = con; networkMode = (con->connectionID == 1) ? HOST : GUEST; } CConnection &c = *serv; //////////////////////////////////////////////////// logNetwork->infoStream() <<"\tWill send info to server..."; CStopWatch tmh; if(networkMode == SINGLE) { ui8 pom8; c << ui8(2) << ui8(1); //new game; one client c << *si; c >> pom8; if(pom8) throw std::runtime_error("Server cannot open the map!"); }
int main() { try { SignedInteger ui4( 4 ); SignedInteger ui8( 8 ); ui4 = -8; ui4 = ui4 + 2; ui8 = 16; ui8 = ui4; std::cout << "Unsigned integer with: " << ui4.GetBitSize() << std::endl << "Bit representation: " << ui4.ToString( true ) << std::endl << "Value: " << ui4.ToString() << std::endl << "Max +: " << ui4.GetMaxPositive() << std::endl << "Max -: " << ui4.GetMaxNegative() << std::endl; } catch( std::exception* ex ) { std::cout << ex->what(); } return 0; }
static SDL_Color addColors(const SDL_Color & base, const SDL_Color & over) { return CSDL_Ext::makeColor( mixChannels(over.r, base.r, over.unused, base.unused), mixChannels(over.g, base.g, over.unused, base.unused), mixChannels(over.b, base.b, over.unused, base.unused), ui8(over.unused + base.unused * (255 - over.unused) / 256) ); }
void CBitmapHanFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const { int posX = pos.x; int posY = pos.y; SDL_LockSurface(surface); for(size_t i=0; i<data.size(); i += getCharacterSize(data[i])) { if (ui8(data[i]) < 0x80) renderCharacter(surface, getCharacterIndex(0xa3, data[i] + 0x80), color, posX, posY); else renderCharacter(surface, getCharacterIndex(data[i], data[i+1]), color, posX, posY); } SDL_UnlockSurface(surface); }
std::map<int, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors(std::string from) { std::map<int, std::pair<SDL_Color, SDL_Color> > ret; const JsonNode config(ResourceID(from, EResType::TEXT)); for(auto &m : config.Struct()) { auto index = boost::find(GameConstants::TERRAIN_NAMES, m.first); if (index == std::end(GameConstants::TERRAIN_NAMES)) { logGlobal->errorStream() << "Error: unknown terrain in terrains.json: " << m.first; continue; } int terrainID = index - std::begin(GameConstants::TERRAIN_NAMES); const JsonVector &unblockedVec = m.second["minimapUnblocked"].Vector(); SDL_Color normal = { ui8(unblockedVec[0].Float()), ui8(unblockedVec[1].Float()), ui8(unblockedVec[2].Float()), ui8(255) }; const JsonVector &blockedVec = m.second["minimapBlocked"].Vector(); SDL_Color blocked = { ui8(blockedVec[0].Float()), ui8(blockedVec[1].Float()), ui8(blockedVec[2].Float()), ui8(255) }; ret.insert(std::make_pair(terrainID, std::make_pair(normal, blocked))); } return ret; }
void CBitmapFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const { if (data.empty()) return; assert(surface); int posX = pos.x; int posY = pos.y; // Should be used to detect incorrect text parsing. Disabled right now due to some old UI code (mostly pregame and battles) //assert(data[0] != '{'); //assert(data[data.size()-1] != '}'); SDL_LockSurface(surface); // for each symbol for(auto & elem : data) { renderCharacter(surface, chars[ui8(elem)], color, posX, posY); } SDL_UnlockSurface(surface); }
size_t CBitmapHanFont::getCharacterSize(char data) const { if (ui8(data) < 0x80) return 1; return 2; }
size_t CBitmapFont::getGlyphWidth(const char * data) const { const Char & ch = chars[ui8(*data)]; return ch.leftOffset + ch.width + ch.rightOffset; }
static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base) { return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.unused * alpha / 256)); }
void CClient::loadGame(const std::string & fname, const bool server, const std::vector<int>& humanplayerindices, const int loadNumPlayers, int player_, const std::string & ipaddr, const std::string & port) { PlayerColor player(player_); //intentional shadowing logNetwork->infoStream() <<"Loading procedure started!"; CServerHandler sh; if(server) sh.startServer(); else serv = sh.justConnectToServer(ipaddr,port=="" ? "3030" : port); CStopWatch tmh; unique_ptr<CLoadFile> loader; try { std::string clientSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(fname, EResType::CLIENT_SAVEGAME)); std::string controlServerSaveName; if (CResourceHandler::get("local")->existsResource(ResourceID(fname, EResType::SERVER_SAVEGAME))) { controlServerSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(fname, EResType::SERVER_SAVEGAME)); } else// create entry for server savegame. Triggered if save was made after launch and not yet present in res handler { controlServerSaveName = clientSaveName.substr(0, clientSaveName.find_last_of(".")) + ".vsgm1"; CResourceHandler::get("local")->createResource(controlServerSaveName, true); } if(clientSaveName.empty()) throw std::runtime_error("Cannot open client part of " + fname); if(controlServerSaveName.empty()) throw std::runtime_error("Cannot open server part of " + fname); { CLoadIntegrityValidator checkingLoader(clientSaveName, controlServerSaveName, minSupportedVersion); loadCommonState(checkingLoader); loader = checkingLoader.decay(); } logNetwork->infoStream() << "Loaded common part of save " << tmh.getDiff(); const_cast<CGameInfo*>(CGI)->mh = new CMapHandler(); const_cast<CGameInfo*>(CGI)->mh->map = gs->map; pathInfo = make_unique<CPathsInfo>(getMapSize()); CGI->mh->init(); logNetwork->infoStream() <<"Initing maphandler: "<<tmh.getDiff(); } catch(std::exception &e) { logGlobal->errorStream() << "Cannot load game " << fname << ". Error: " << e.what(); throw; //obviously we cannot continue here } /* if(!server) player = PlayerColor(player_); */ std::set<PlayerColor> clientPlayers; if(server) serv = sh.connectToServer(); //*loader >> *this; if(server) { tmh.update(); ui8 pom8; *serv << ui8(3) << ui8(loadNumPlayers); //load game; one client if single-player *serv << fname; *serv >> pom8; if(pom8) throw std::runtime_error("Server cannot open the savegame!"); else logNetwork->infoStream() << "Server opened savegame properly."; } if(server) { for(auto & elem : gs->scenarioOps->playerInfos) if(!std::count(humanplayerindices.begin(),humanplayerindices.end(),elem.first.getNum()) || elem.first==player) { clientPlayers.insert(elem.first); } clientPlayers.insert(PlayerColor::NEUTRAL); } else { clientPlayers.insert(player); } std::cout << "CLIENTPLAYERS:\n"; for(auto x : clientPlayers) std::cout << x << std::endl; std::cout << "ENDCLIENTPLAYERS\n"; serialize(loader->serializer,0,clientPlayers); *serv << ui32(clientPlayers.size()); for(auto & elem : clientPlayers) *serv << ui8(elem.getNum()); serv->addStdVecItems(gs); /*why is this here?*/ //*loader >> *this; logNetwork->infoStream() << "Loaded client part of save " << tmh.getDiff(); logNetwork->infoStream() <<"Sent info to server: "<<tmh.getDiff(); //*serv << clientPlayers; serv->enableStackSendingByID(); serv->disableSmartPointerSerialization(); // logGlobal->traceStream() << "Objects:"; // for(int i = 0; i < gs->map->objects.size(); i++) // { // auto o = gs->map->objects[i]; // if(o) // logGlobal->traceStream() << boost::format("\tindex=%5d, id=%5d; address=%5d, pos=%s, name=%s") % i % o->id % (int)o.get() % o->pos % o->getHoverText(); // else // logGlobal->traceStream() << boost::format("\tindex=%5d --- nullptr") % i; // } }
std::vector<std::string> CMessage::breakText( std::string text, size_t maxLineWidth, EFonts font ) { std::vector<std::string> ret; boost::algorithm::trim_right_if(text,boost::algorithm::is_any_of(std::string(" "))); // each interation generates one output line while (text.length()) { ui32 lineWidth = 0; //in characters or given char metric ui32 wordBreak = -1; //last position for line break (last space character) ui32 currPos = 0; //current position in text bool opened = false; //set to true when opening brace is found size_t symbolSize = 0; // width of character, in bytes size_t glyphWidth = 0; // width of printable glyph, pixels // loops till line is full or end of text reached while(currPos < text.length() && text[currPos] != 0x0a && lineWidth < maxLineWidth) { symbolSize = Unicode::getCharacterSize(text[currPos]); glyphWidth = graphics->fonts[font]->getGlyphWidth(text.data() + currPos); // candidate for line break if (ui8(text[currPos]) <= ui8(' ')) wordBreak = currPos; /* We don't count braces in string length. */ if (text[currPos] == '{') opened=true; else if (text[currPos]=='}') opened=false; else lineWidth += glyphWidth; currPos += symbolSize; } // long line, create line break if (currPos < text.length() && (text[currPos] != 0x0a)) { if (wordBreak != ui32(-1)) currPos = wordBreak; else currPos -= symbolSize; } //non-blank line if(currPos != 0) { ret.push_back(text.substr(0, currPos)); if (opened) /* Close the brace for the current line. */ ret.back() += '}'; text.erase(0, currPos); } else if(text[currPos] == 0x0a) { ret.push_back(""); //add empty string, no extra actions needed } if (text.length() != 0 && text[0] == 0x0a) { /* Remove LF */ text.erase(0, 1); } else { // trim only if line does not starts with LF // FIXME: necessary? All lines will be trimmed before returning anyway boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(std::string(" "))); } if (opened) { /* Add an opening brace for the next line. */ if (text.length() != 0) text.insert(0, "{"); } } /* Trim whitespaces of every line. */ for (auto & elem : ret) boost::algorithm::trim(elem); return ret; }
void CClient::loadGame( const std::string & fname ) { logNetwork->infoStream() <<"Loading procedure started!"; CServerHandler sh; sh.startServer(); CStopWatch tmh; try { std::string clientSaveName = CResourceHandler::get()->getResourceName(ResourceID(fname, EResType::CLIENT_SAVEGAME)); std::string controlServerSaveName; if (CResourceHandler::get()->existsResource(ResourceID(fname, EResType::SERVER_SAVEGAME))) { controlServerSaveName = CResourceHandler::get()->getResourceName(ResourceID(fname, EResType::SERVER_SAVEGAME)); } else// create entry for server savegame. Triggered if save was made after launch and not yet present in res handler { controlServerSaveName = clientSaveName.substr(0, clientSaveName.find_last_of(".")) + ".vsgm1"; CResourceHandler::get()->createResource(controlServerSaveName, true); } if(clientSaveName.empty()) throw std::runtime_error("Cannot open client part of " + fname); if(controlServerSaveName.empty()) throw std::runtime_error("Cannot open server part of " + fname); unique_ptr<CLoadFile> loader; { CLoadIntegrityValidator checkingLoader(clientSaveName, controlServerSaveName); loadCommonState(checkingLoader); loader = checkingLoader.decay(); } logNetwork->infoStream() << "Loaded common part of save " << tmh.getDiff(); const_cast<CGameInfo*>(CGI)->mh = new CMapHandler(); const_cast<CGameInfo*>(CGI)->mh->map = gs->map; pathInfo = make_unique<CPathsInfo>(getMapSize()); CGI->mh->init(); logNetwork->infoStream() <<"Initing maphandler: "<<tmh.getDiff(); *loader >> *this; logNetwork->infoStream() << "Loaded client part of save " << tmh.getDiff(); } catch(std::exception &e) { logGlobal->errorStream() << "Cannot load game " << fname << ". Error: " << e.what(); throw; //obviously we cannot continue here } serv = sh.connectToServer(); serv->addStdVecItems(gs); tmh.update(); ui8 pom8; *serv << ui8(3) << ui8(1); //load game; one client *serv << fname; *serv >> pom8; if(pom8) throw std::runtime_error("Server cannot open the savegame!"); else logNetwork->infoStream() << "Server opened savegame properly."; *serv << ui32(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral for(auto it = gs->scenarioOps->playerInfos.begin(); it != gs->scenarioOps->playerInfos.end(); ++it) { *serv << ui8(it->first.getNum()); //players } *serv << ui8(PlayerColor::NEUTRAL.getNum()); logNetwork->infoStream() <<"Sent info to server: "<<tmh.getDiff(); serv->enableStackSendingByID(); serv->disableSmartPointerSerialization(); }