JSONObject::JSONObject(char* _buffer) : JSONBase(_buffer) { if ( !buffer ) return; try { SKIP_WHITESPACE(); ERROR_IF_NOT('{'); buffer++; while ( readValue() ) {} SKIP_WHITESPACE(); ERROR_IF_NOT('}'); *buffer++ = 0; } catch(...) { for ( auto &m : values ) { if ( m.second ) { delete m.second; m.second = 0; } } values.clear(); if ( dynamic_cast<JSONDocument*>(this) ) { buffer.free(); } throw; } }
bool PlayerMessage::applyGh( CGameHandler *gh ) { ERROR_IF_NOT(player); if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN; gh->playerMessage(player,text); return true; }
bool EndTurn::applyGh( CGameHandler *gh ) { int player = GS(gh)->currentPlayer; ERROR_IF_NOT(player); if(gh->states.checkFlag(player, &PlayerStatus::engagedIntoBattle)) COMPLAIN_AND_RETURN("Cannot end turn when in battle!"); gh->states.setFlag(GS(gh)->currentPlayer,&PlayerStatus::makingTurn,false); return true; }
bool EndTurn::applyGh( CGameHandler *gh ) { PlayerColor player = GS(gh)->currentPlayer; ERROR_IF_NOT(player); if(gh->queries.topQuery(player)) COMPLAIN_AND_RETURN("Cannot end turn before resolving queries!"); gh->states.setFlag(GS(gh)->currentPlayer,&PlayerStatus::makingTurn,false); return true; }
bool SetSelection::applyGh( CGameHandler *gh ) { ERROR_IF_NOT(player); if(!gh->getObj(id)) { tlog1 << "No such object...\n"; ERROR_AND_RETURN; } gh->sendAndApply(this); return true; }
bool SetSelection::applyGh( CGameHandler *gh ) { ERROR_IF_NOT(player); if(!gh->getObj(id)) { logNetwork->errorStream() << "No such object..."; ERROR_AND_RETURN; } gh->sendAndApply(this); return true; }
JSONArray::JSONArray(char* _buffer): JSONBase(_buffer) { if ( !buffer ) return; try { SKIP_WHITESPACE(); ERROR_IF_NOT('['); buffer++; while ( readValue() ) {} SKIP_WHITESPACE(); ERROR_IF_NOT(']'); *buffer++ = 0; } catch(...) { for ( auto &m : values ) { if ( m ) { delete m; } } values.clear(); throw; } }
bool TradeOnMarketplace::applyGh( CGameHandler *gh ) { //market must be owned or visited const IMarket *m = IMarket::castFrom(market); if(!m) COMPLAIN_AND_RETURN("market is not-a-market! :/"); ui8 player = market->tempOwner; if(player >= GameConstants::PLAYER_LIMIT) player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner; if(player >= GameConstants::PLAYER_LIMIT) COMPLAIN_AND_RETURN("No player can use this market!"); if(hero && (player != hero->tempOwner || hero->visitablePos() != market->visitablePos())) COMPLAIN_AND_RETURN("This hero can't use this marketplace!"); ERROR_IF_NOT(player); switch(mode) { case EMarketMode::RESOURCE_RESOURCE: return gh->tradeResources(m, val, player, r1, r2); case EMarketMode::RESOURCE_PLAYER: return gh->sendResources(val, player, static_cast<Res::ERes>(r1), static_cast<TPlayerColor>(r2)); case EMarketMode::CREATURE_RESOURCE: if(!hero) COMPLAIN_AND_RETURN("Only hero can sell creatures!"); return gh->sellCreatures(val, m, hero, r1, static_cast<Res::ERes>(r2)); case EMarketMode::RESOURCE_ARTIFACT: if(!hero) COMPLAIN_AND_RETURN("Only hero can buy artifacts!"); return gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1), ArtifactID(r2)); case EMarketMode::ARTIFACT_RESOURCE: if(!hero) COMPLAIN_AND_RETURN("Only hero can sell artifacts!"); return gh->sellArtifact(m, hero, ArtifactInstanceID(r1), static_cast<Res::ERes>(r2)); case EMarketMode::CREATURE_UNDEAD: return gh->transformInUndead(m, hero, r1); case EMarketMode::RESOURCE_SKILL: return gh->buySecSkill(m, hero, SecondarySkill(r2)); case EMarketMode::CREATURE_EXP: return gh->sacrificeCreatures(m, hero, r1, val); case EMarketMode::ARTIFACT_EXP: return gh->sacrificeArtifact(m, hero, ArtifactPosition(r1)); default: COMPLAIN_AND_RETURN("Unknown exchange mode!"); } }
bool JSONObject::readValue() { // Read string chunk terminated by ending quote. SKIP_WHITESPACE(); if ( *buffer == '}' ) return false; char *key = nullptr; JSONBase *value = nullptr; if ( *buffer == '"' || *buffer == '\'' ) { if ( !(key = readQuotedValue()) ) throw sfx::bad_json(); } else if ( *buffer != ',' && *buffer != ':' ) { if ( !(key = readUnquotedValue()) ) throw sfx::bad_json(); } else { throw sfx::bad_json(); } SKIP_WHITESPACE(); ERROR_IF_NOT(':'); *buffer++ = 0; SKIP_WHITESPACE(); if ( *buffer == '"' ) { value = new JSONString(readQuotedValue()); } else if ( *buffer == '[' ) { value = new JSONArray(buffer); buffer = value->buffer; } else if ( *buffer == '{' ) { value = new JSONObject(buffer); buffer = value->buffer; } else if ( *buffer != ',' ) { value = new JSONString(readUnquotedValue()); } else { throw sfx::bad_json(); } values[key]=value; SKIP_WHITESPACE(); if ( *buffer == ',' ) { *buffer++=0; return true; } return false; }
bool ExchangeArtifacts::applyGh( CGameHandler *gh ) { ERROR_IF_NOT(src.owningPlayer());//second hero can be ally return gh->moveArtifact(src, dst); }
Shape* read_ply(const string& filename, bool flipface, bool normalize, bool flipyz) { FILE* f = fopen(filename.c_str(), "rt"); if(not f) ERROR("cannot open file %s", filename.c_str()); char line[LINE_SIZE]; // magic fgets(line, LINE_SIZE, f); if(not _startswith(line, "ply")) ERROR("unknown file format"); // format fgets(line, LINE_SIZE, f); if(not _startswith(line, "format")) ERROR("unknown file format"); // skip till vertex fgets(line, LINE_SIZE, f); while(not _startswith(line, "element vertex")) fgets(line, LINE_SIZE, f); // vertex int vertex_num = 0; sscanf(line, "element vertex %d", &vertex_num); // vertex props int vertex_pos = -1, vertex_norm = -1, vertex_uv = -1, vertex_col8 = -1, vertex_colf = -1; int vertex_vals = 0; fgets(line, LINE_SIZE, f); while(_startswith(line, "property")) { if(_startswith(line, "property float x") or _startswith(line, "property float32 x")) vertex_pos = vertex_vals; if(_startswith(line, "property float nx") or _startswith(line, "property float32 nx")) vertex_norm = vertex_vals; if(_startswith(line, "property uchar diffuse_red") or _startswith(line, "property uint8 diffuse_red")) vertex_col8 = vertex_vals; if(_startswith(line, "property uchar red") or _startswith(line, "property uint8 red")) vertex_col8 = vertex_vals; if(_startswith(line, "property float diffuse_red") or _startswith(line, "property float32 diffuse_red")) vertex_colf = vertex_vals; if(_startswith(line, "property float red") or _startswith(line, "property float32 red")) vertex_colf = vertex_vals; vertex_vals ++; fgets(line, LINE_SIZE, f); } // skip till faces while(not _startswith(line, "element face")) fgets(line, LINE_SIZE, f); // face int face_num = 0; sscanf(line, "element face %d", &face_num); // face props int face_start = -1; int face_vals = 0; fgets(line, LINE_SIZE, f); while(_startswith(line, "property")) { if(_startswith(line, "property list uchar int vertex_ind")) face_start = face_vals; if(_startswith(line, "property list uchar uint vertex_ind")) face_start = face_vals; if(_startswith(line, "property list uint8 int32 vertex_ind")) face_start = face_vals; if(_startswith(line, "property list char int vertex_ind")) face_start = face_vals; if(_startswith(line, "property list int8 int32 vertex_ind")) face_start = face_vals; face_vals ++; fgets(line, LINE_SIZE, f); } ERROR_IF_NOT(face_start>=0, "bad face start"); // skip till end of header while(not _startswith(line, "end_header")) fgets(line, LINE_SIZE, f); // read vertex data vector<vec3f> pos; vector<vec3f> norm; vector<vec2f> uv; for(int i = 0; i < vertex_num; i ++) { vector<float> vdata; for(int j = 0; j < vertex_vals; j ++) { float v; fscanf(f, "%f", &v); vdata.push_back(v); } pos.push_back( vec3f(vdata[vertex_pos+0],vdata[vertex_pos+1],vdata[vertex_pos+2]) ); if(vertex_norm >= 0) norm.push_back( vec3f(vdata[vertex_norm+0],vdata[vertex_norm+1],vdata[vertex_norm+2]) ); if(vertex_uv >= 0) uv.push_back( vec2f(vdata[vertex_uv+0],vdata[vertex_uv+1] )); } if(normalize) { range3f bbox; for(auto v : pos) bbox = runion(bbox,v); vec3f c = center(bbox); float s = 1/max_component(size(bbox)); for(vec3f& v : pos) v = (v-c)*s; } if(flipyz) { for(vec3f& v : pos) swap(v.y,v.z); for(vec3f& v : norm) swap(v.y,v.z); } // read vertex data vector<vec3i> triangles; vector<vec4i> quads; for(int i = 0; i < face_num; i ++) { int n = 0; fscanf(f, "%d", &n); if(n == 3) { vec3i face; for(int j=0;j<3;j++) fscanf(f, "%d", &(face[j])); triangles.push_back(face); } if(n == 4) { vec4i face; for(int j=0;j<4;j++) fscanf(f, "%d", &(face[j])); quads.push_back(face); } if(n != 3 and n != 4) ERROR("unsupported face type"); } if(flipface) { for(vec3i& t : triangles) swap(t.y,t.z); for(vec4i& t : quads) swap(t.y,t.w); } fclose(f); if(triangles.size() == 0 and quads.size() > 0) { auto mesh = new Mesh(); mesh->pos = pos; mesh->norm = norm; mesh->texcoord = uv; mesh->quad = quads; return mesh; } else if(triangles.size() > 0 and quads.size() == 0) { auto mesh = new TriangleMesh(); mesh->pos = pos; mesh->norm = norm; mesh->texcoord = uv; mesh->triangle = triangles; return mesh; } else if(triangles.size() > 0 and quads.size() > 0) { auto mesh = new Mesh(); mesh->pos = pos; mesh->norm = norm; mesh->texcoord = uv; mesh->triangle = triangles; mesh->quad = quads; return mesh; } else { WARNING("empty mesh"); return 0; } }