void GameHandler::newrandompiece() { //Move nextpiece to falling newpiece(pieces[nextpiece_type], gameopt.columns/2, -1, 0.0, PhysicPiece::FALLING_PIECE, graphicpieces_uncutted[nextpiece_type].get()); //Create randomnextpiece randomnextpiece(); }
/** * Add a piece to a square * * @throw std::out_of_range if not a valid Position * @throw std::runtime_error if square is already occupied * @param pos where to add the piece * @param player which player this piece should belong to * @param type whether the piece is a king */ void DraughtsBoard::addPiece(const Position &pos,const int player, const int &type){ if(!squareExists(pos) || !squareExists(pos)){ std::string errmsg("Attempted to add piece out of bounds:"); errmsg+=pos.toString(); throw std::out_of_range(errmsg); } else if(squareIsOccupied(pos)){ std::string errmsg("square is occupied:"); errmsg+=pos.toString(); throw std::runtime_error(errmsg); } Square newpiece(player,type); m_board[pos.m_x][pos.m_y] = newpiece; player==1 ? m_piecesp1++ : m_piecesp2++; if(type) player==1 ? m_kingsp1++ : m_kingsp2++; }
void GameHandler::cutline(float from, float to) { float x0 = 0; float y0 = from; float x1 = gameopt.columns; float y1 = to; struct DeletePiece { std::list<polygon<float>> remainders; GamePiece * pgp; int originaltype; float x,y,rot; #ifdef DEBUGAREALOSS std::list<polygon<float>> midremainders; #endif }; std::list<DeletePiece> deletelist; physichandler.getpieces_in_rect(x0, y0, x1, y1, [y0, y1, &deletelist](PhysicPiece * php){ polygon<float> p (static_cast<GamePiece *>(php->getUserData())->p.getshape()); struct DeletePiece dp; // Transform the piece coordinates from local to global for (auto & vertex: p) { vertex.rotate(php->getRot()); vertex.translate(php->getX(), php->getY()); } #ifndef DEBUGAREALOSS struct { void push_back(const polygon<float> & p) { (void) p; } } midcontainer; #else auto & midcontainer = dp.midremainders; #endif // Here cutter uses the tolerance bool isinbetween = cutter(p, dp.remainders, dp.remainders, midcontainer, y0, y1, 0.1F); if (isinbetween) { dp.pgp = static_cast<GamePiece *>(php->getUserData()); dp.originaltype = dp.pgp->p.getType(); dp.x = php->getX(); dp.y = php->getY(); dp.rot = php->getRot(); deletelist.push_back(std::move(dp)); } }); for (auto & dp: deletelist) { GamePiece * pgp = dp.pgp; #ifdef DEBUGAREALOSS float prevarea = pgp->p.getshape().area(), newarea = 0; #endif // Create remainders for (auto & pol: dp.remainders) { // Transform the piece coordinates from global to local again for (auto & vertex: pol) { vertex.translate(-dp.x, -dp.y); vertex.rotate(-dp.rot); } if (isugly(pol)) continue; piece<float> newp (pol, dp.originaltype); if (!newp.empty()) newpiece(newp, dp.x, dp.y, dp.rot, PhysicPiece::OLD_PIECE); #ifdef DEBUGAREALOSS newarea += pol.area(); #endif } #ifdef DEBUGAREALOSS for (auto & pol: dp.midremainders) { newarea += pol.area(); } if (fabs(prevarea - newarea) > 1.0) raise(SIGTRAP); #endif deletepiece(pgp); } }