FixedPoint heightDifference2Velocity(const FixedPoint& h_diff) { // no restrictions for moving downhill if(h_diff < FixedPoint::ZERO) return 1; if(h_diff >= FixedPoint(2)) return FixedPoint::ZERO; return (FixedPoint(2) - h_diff)/FixedPoint(2); }
void Inventory::dropItemSlot(World& world, const Location& position, int i) { if(this->wieldedItems[i] == 0) return; int id = world.nextUnitID(); world.addItem(*(this->wieldedItems[i]), VisualWorld::ModelType(this->wieldedItems[i]->intVals["MODEL_TYPE"]), id); RandomMachine random; random.setSeed(i); FixedPoint x = FixedPoint( (random.getInt() % 30)-15, 40); FixedPoint z = FixedPoint( (random.getInt() % 30)-15, 40); FixedPoint y = FixedPoint( (random.getInt() % 15)+5, 40); world.items[id].velocity = Location(x, y, z); world.items[id].position = position; delete this->wieldedItems[i]; this->wieldedItems[i] = 0; }
/** * \brief Renders a tile to the given path using the test styles. * \param tilePath path to the resulting tile * \param id identifier of the tile that should be rendered */ void renderTile(const char* tilePath, shared_ptr<TileIdentifier> id) { BOOST_TEST_MESSAGE("Render: " << tilePath); RenderAttributes attr; Style* canvas = attr.getCanvasStyle(); canvas->fill_color = Color(1.0f, 1.0f, 1.0f, 1.0f); coord_t x0, x1, y0, y1; tileToMercator(id->getX(), id->getY(), id->getZoom(), x0, y0); tileToMercator(id->getX() + 1, id->getY() + 1, id->getZoom(), x1, y1); FixedRect r = FixedRect(FixedPoint(x0, y0), FixedPoint(x1, y1)); auto nodes = data->getNodeIDs(r); auto ways = data->getWayIDs(r); auto relations = data->getRelationIDs(r); generateStyles(); BOOST_TEST_MESSAGE(" - ways " << ways->size()); styleWays(ways, attr); BOOST_TEST_MESSAGE(" - nodes " << nodes->size()); styleNodes(nodes, attr); BOOST_TEST_MESSAGE(" - relations " << relations->size()); styleRelations(relations, attr); shared_ptr<MetaIdentifier> mid = MetaIdentifier::Create(id); shared_ptr<MetaTile> meta = boost::make_shared<MetaTile>(mid); renderer->renderMetaTile(attr, meta); shared_ptr<Tile> tile = boost::make_shared<Tile>(id); renderer->sliceTile(meta, tile); BOOST_TEST_MESSAGE("Writing the tile:"); std::ofstream out; out.open(tilePath); BOOST_TEST_MESSAGE("- get data"); Tile::ImageType png = tile->getImage(); BOOST_TEST_MESSAGE("- writing (" << png->size() << " Bytes)"); out.write((const char*) png->data(), png->size()); out.close(); }
//---------------------------------------------------------------------- FixedPoint& FixedPoint::operator/=( const float rhs ) { return operator/=( FixedPoint( rhs ) ); }
void UnitTicker::tickUnit(World& world, Unit& unit, Model* model) { unit.intVals["D"] *= 0.95; if(unit.human()) { if(unit.intVals["HEALTH"] < 0) { unit.position = Location(); } int zen_modifier = unit.getModifier("ZEN"); int& sanity = unit["SANITY"]; // sanity tremble if(sanity < 60) { RandomMachine random; random.setSeed(world.currentWorldFrame); int maxEffect = (60 - sanity < 30) ? (60 - sanity) : 30; int mouse_effect1 = 3 * (random.getInt() % (maxEffect)) * ((random.getInt() & 1) * 2 - 1); int mouse_effect2 = 3 * (random.getInt() % (maxEffect)) * ((random.getInt() & 1) * 2 - 1); unit.angle += mouse_effect1; unit.upangle += mouse_effect2; } if(world.currentWorldFrame % (10 * zen_modifier) == 0) { --sanity; if(sanity < 0) { sanity = 0; unit.last_damage_dealt_by = unit.id; unit.takeDamage(20, DamageType::PURE); unit("DAMAGED_BY") = "depression"; } } } if(world.currentWorldFrame % 50 == 0) unit.regenerate(); // for server it's ok that there are no models sometimes :G if(world.visualworld->isActive()) { assert(model && "this should never happen"); model->rotate_y(unit.getAngle()); model->updatePosition(unit.position.x.getFloat(), unit.position.y.getFloat(), unit.position.z.getFloat()); } // some physics & game world information if( (unit.velocity.y + unit.position.y - FixedPoint(1, 20)) <= world.lvl.getHeight(unit.position.x, unit.position.z) ) unit.mobility |= Unit::MOBILITY_STANDING_ON_GROUND; if(unit.hasSupportUnderFeet()) { if(unit.velocity.y < FixedPoint(-7, 10)) { sendMsg(SoundEvent("jump_land", 100000, unit.getEyePosition())); } unit.landingDamage(); unit.applyFriction(); if(unit.hasGroundUnderFeet()) { unit.position.y = world.lvl.getHeight(unit.position.x, unit.position.z); unit.velocity.y = FixedPoint::ZERO; } } unit_ai.tick(world, unit); Location tmp; for(int i=0; i<world.apomath.DEGREES_360; i+=world.apomath.DEGREES_360/32) { FixedPoint& tmp_x = world.apomath.getCos(i); FixedPoint& tmp_z = world.apomath.getSin(i); if(world.lvl.getHeight(unit.position.x + tmp_x, unit.position.z + tmp_z) > 8) { unit.position += Location(-tmp_x * FixedPoint(2, 20), 0, -tmp_z * FixedPoint(2, 20)); } } unit.applyGravity(); unit.updateMobility(); unit.processInput(world); // weapon activations if(unit.getMouseAction(Unit::MOUSE_LEFT)) { unit.activateCurrentItemPrimary(world); } if(unit.getMouseAction(Unit::MOUSE_RIGHT)) { unit.activateCurrentItemSecondary(world); } if(unit.getKeyAction(Unit::RELOAD)) { unit.activateCurrentItemReload(world); } if(unit.getKeyAction(Unit::INTERACT)) { WorldItem* item = unit.itemPick.get(); if((item != 0) && (item->dead == 0)) { unit.inventory.pickUp(world, unit, item); } } FixedPoint reference_x = unit.position.x + unit.velocity.x; FixedPoint reference_z = unit.position.z + unit.velocity.z; FixedPoint reference_y = world.lvl.getHeight(reference_x, reference_z); FixedPoint y_diff = reference_y - unit.position.y; FixedPoint yy_val = heightDifference2Velocity(y_diff); unit.tick(yy_val); world.lvl.clampToLevelArea(unit); unit.postTick(); }
//---------------------------------------------------------------------- bool FixedPoint::operator!=( const double rhs ) const { return operator!=( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- bool FixedPoint::operator!=( const float rhs ) const { return operator!=( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- bool FixedPoint::operator!=( const unsigned int rhs ) const { return operator!=( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- bool FixedPoint::operator>=( const int rhs ) const { return operator>=( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- const FixedPoint& FixedPoint::operator=( float& copy ) { *this = FixedPoint( copy ); return *this; }
#include "fixed_point.h" const FixedPoint FixedPoint::ZERO = FixedPoint(0);
//---------------------------------------------------------------------- FixedPoint FixedPoint::operator/( const float rhs ) const { return operator/( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- FixedPoint& FixedPoint::operator/=( const unsigned int rhs ) { return operator/=( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- FixedPoint FixedPoint::operator/( const unsigned int rhs ) const { return operator/( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- FixedPoint& FixedPoint::operator*=( const int rhs ) { return operator*=( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- FixedPoint FixedPoint::operator*( const int rhs ) const { return operator*( FixedPoint( rhs ) ); }
//---------------------------------------------------------------------- const FixedPoint& FixedPoint::operator=( double& copy ) { *this = FixedPoint( copy ); return *this; }
void SweeperStatisticsScene::drawStatistics() { Vec2 origin(100, 100); Size size(750, 400); // 绘制统计图坐标; m_coordinateRenderer->clear(); m_coordinateRenderer->drawSegment(origin, Vec2(origin.x+size.width, origin.y), 1.0f, Color4F(1.0f, 1.0f, 1.0f, 0.9f)); m_coordinateRenderer->drawSegment(origin, Vec2(origin.x, origin.y+size.height), 1.0f, Color4F(1.0f, 1.0f, 1.0f, 0.9f)); // 绘制适应值曲线; m_bestFitnessRenderer->clear(); m_averageFitnessRenderer->clear(); m_worstFitnessRenderer->clear(); // 获取统计数据; auto &dataList = GeneticAlgorithm::instance()->getStatisticsData(); int count = dataList.size(); // 获取最优适应值的最大值,用于绘制坐标单位; Fixed max = 0; for (const auto &data : dataList) { if (data.bestFitness > max) { max = data.bestFitness; } } // 平均间隔; Fixed unitWidth = size.width / count; Fixed unitHeight = size.height / max; // 绘制线段; auto bestStart = FixedPoint(origin.x, origin.y); auto averageStart = FixedPoint(origin.x, origin.y); auto worstStart = FixedPoint(origin.x, origin.y); for (const auto &data : dataList) { // 绘制最差值线段; { Fixed x = worstStart.x + unitWidth; Fixed y = origin.y + unitHeight * data.worstFitness; FixedPoint worstEnd(x, y); m_averageFitnessRenderer->drawSegment(Vec2(worstStart.x, worstStart.y), Vec2(worstEnd.x, worstEnd.y), 1.0f, Color4F(1.0f, 0.0f, 0.0f, 0.9f)); worstStart = worstEnd; } // 绘制平均值线段; { Fixed x = averageStart.x + unitWidth; Fixed y = origin.y + unitHeight * data.averageFitness; FixedPoint averageEnd(x, y); m_averageFitnessRenderer->drawSegment(Vec2(averageStart.x, averageStart.y), Vec2(averageEnd.x, averageEnd.y), 1.0f, Color4F(0.0f, 0.0f, 1.0f, 0.9f)); averageStart = averageEnd; } // 绘制最优值线段; { Fixed x = bestStart.x + unitWidth; Fixed y = origin.y + unitHeight * data.bestFitness; FixedPoint bestEnd(x, y); m_bestFitnessRenderer->drawSegment(Vec2(bestStart.x, bestStart.y), Vec2(bestEnd.x, bestEnd.y), 1.0f, Color4F(0.0f, 1.0f, 0.0f, 0.9f)); bestStart = bestEnd; } } // 显示当前时代的适应值; if (count > 0) { auto &data = dataList[count-1]; if (m_labelBestFitness) { char s[32]; sprintf(s, "best: %.02f", (float)data.bestFitness); m_labelBestFitness->setString(s); } if (m_labelWorstFitness) { char s[32]; sprintf(s, "worst: %.02f", (float)data.worstFitness); m_labelWorstFitness->setString(s); } if (m_labelAverageFitness) { char s[32]; sprintf(s, "average: %.02f", (float)data.averageFitness); m_labelAverageFitness->setString(s); } } }
//---------------------------------------------------------------------- FixedPoint FixedPoint::operator/( const double rhs ) const { return operator/( FixedPoint( rhs ) ); }
int main() { { Location p1(0,0,0); Location p2(10,0,0); Location p3(0,0,10); Location p(10,0,0); interpolate(p1, p2, p3, p); assertEqual(p, Location(10,0,0)); } { Location p1(0,0,0); Location p2(10,0,0); Location p3(0,0,10); Location p(1,0,1); interpolate(p1, p2, p3, p); assertEqual(p, Location(1,0,1)); } { Location p1(0,2,0); Location p2(2,0,0); Location p3(0,0,2); Location p(1,0,0); interpolate(p1, p2, p3, p); assertEqual(p, Location(1,1,0)); } { Location p1(0,2,0); Location p2(2,0,0); Location p3(0,0,2); Location p(0,0,1); interpolate(p1, p2, p3, p); assertEqual(p, Location(0,1,1)); } { Location p1(0,2,0); Location p2(2,0,0); Location p3(0,0,2); Location p(0,0,1); interpolate(p1, p2, p3, p); assertEqual(p, Location(0,1,1)); } { Location p1(0,2,0); Location p2(2,0,0); Location p3(0,0,2); Location p(FixedPoint(1,2),0,FixedPoint(1,2)); interpolate(p1, p2, p3, p); assertEqual(p, Location(FixedPoint(1,2),1,FixedPoint(1,2))); } }
//---------------------------------------------------------------------- FixedPoint& FixedPoint::operator/=( const double rhs ) { return operator/=( FixedPoint( rhs ) ); }