⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 object.c

📁 在LINUX下运行的仿真机器人服务器源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
TheNumber PObject::Max = 0 ;/* pfr 06/07/200 added short name support */PObject::PObject( const  PObject::obj_type& object_type,		  const Name nm, const Name snm,		  const std::string& sub_name,		  const PVector& p, const Value v)  : M_object_type ( object_type ),   M_sub_name ( sub_name ){	id = Max ;	Max++ ;	name = nm ;	/* th 19.05.00 */	short_name = snm ;	stadium = (Stadium *)NULL ;	pos = p ;	obj_ver = v ;	size = 0.0 ;	angle = 0.0 ;	enable = TRUE ;}std::ostream& operator<< (std::ostream& o, const PObject& obj) {	o << "#Ob[" << obj.id ;	if( obj.name != NULLCHAR )		o << ":" << obj.name << "" ;	return o 		<< ":pos=" << obj.pos 		<< ",size=" << obj.size 		<< ",angle=" << obj.angle		<< "]" ;}void PObject::Set(const PVector& p, const Value& s){	pos = p ;	size = s ;}Angle PObject::vangle(const PObject& obj) const{	return pos.vangle(obj.pos,angle) ;}/* *=================================================================== *Part: MPObject *=================================================================== *//* pfr 06/07/200 added short name support */MPObject::MPObject( const PObject::obj_type &object_type, 		    const Name nm, 		    const Name s_nm, 		    const std::string& sub_name )   : PObject( object_type, nm, s_nm, sub_name) { }std::ostream& operator<< (std::ostream& o, const MPObject& obj) {	o << "#Ob[" << obj.id ;	if( obj.name != NULLCHAR )		o << ":" << obj.name << "" ;	return o 		<< ":pos=" << obj.pos 	 	<< ",size=" << obj.size	 	<< ",angle=" << Rad2IDegRound(obj.angle)		<< ",vel=" << obj.vel 		<< ",acc=" << obj.acc 		<< ",decay=" << obj.decay		<< ",randp=" << obj.randp		<< "]" ;}void MPObject::Set(				const MPObjectType& t,				const PVector& p,				const Value& s,				const Angle& ang, 				const PVector& v,				const PVector& a,				const Value& d,				const Value& r){	type = t ;	pos = p ;	size = s ;	angle = ang ;	vel = v ;	acc = a ;	decay = d ;	randp = r ;}PVector MPObject::noise() {	Value maxrnd = randp * vel.r() ;	return PVector(drand(-maxrnd,maxrnd),drand(-maxrnd,maxrnd)) ;}PVector MPObject::wind(){	return PVector(vel.r() * (weather->wind_vector.x +			drand(-weather->wind_rand, weather->wind_rand) ) /			(weight * WIND_WEIGHT), vel.r() * (weather->wind_vector.y +			drand(-weather->wind_rand, weather->wind_rand)) /			(weight * WIND_WEIGHT)) ;}void MPObject::_inc() {	Value tmp ;	if (acc.x || acc.y)	  {	    double max_a = max_accel;	    double max_s = max_speed;	    if (this->type == MPO_Player)	      {          max_a /= stadium->slowdownOnTop ( pos, ((Player *)this)->team->side);	        max_s /= stadium->slowdownOnTop ( pos, ((Player *)this)->team->side);	      }	    if ((tmp = acc.r()) > max_a )	      acc *= (max_a / tmp);	    vel += acc ;	    if ((tmp = vel.r()) > max_s)	      vel *= (max_s / tmp) ;    }	if (this->type == MPO_Player)    {      angle = ((Player *)this)->angle_body ;      ((Player *)this)->angle_neck_committed = ((Player *)this)->angle_neck ;    }	vel += noise() ;	vel += wind() ;    CArea post = nearestPost( pos, size );//      std::cout << "pos = " << pos << endl;//      std::cout << "nearest post = " << post << endl;//      std::cout << "dist = " << (pos - post.center).r() << endl;    while( (pos - post.center).r() < post.r )    {//          std::cout << "In post\n";        // then the ball has overlapped the post.  Either it was moved        // there or "pushed".  Either way, we just move the ball away        // from the post        PVector diff = pos - post.center;        if( diff == PVector() )        {            boost::uniform_real< rcss::random::DefaultRNG > dir( rcss::random::DefaultRNG::instance(),                                                                 -M_PI, +M_PI );            diff = Polar2PVector( post.r, dir() );        }        else        {            diff.r( post.r );        }        pos = post.center + diff;        while( (pos - post.center).r() < post.r )        {            // noise keeps it inside the post, move it a bit further out            diff.r( diff.r() * 1.01 );            pos = post.center + diff;        }        if( vel.x != 0.0 || vel.y != 0.0 )        {            PVector pos2center = post.center - pos;            vel.rotate( -pos2center.th() );            vel.x = -vel.x;            vel.rotate( pos2center.th() );        }                post = nearestPost( pos, size );//          std::cout << "pos = " << pos << endl;//          std::cout << "nearest post = " << post << endl;//          std::cout << "dist = " << (pos - post.center).r() << endl;    }    PVector new_pos = pos + vel;    CArea second_post = nearestPost( new_pos, size );    PVector inter;    bool second = false;//      std::cout << "vel = " << vel << endl;//      std::cout << "new_pos = " << new_pos << endl;    while( pos != new_pos           && ( ( intersect( pos, new_pos, post, inter ) )                 || ( post != second_post                     && ( second = intersect( pos, new_pos, second_post, inter ) )                    )                )         )    {//          std::cout << "Collision:\n"//                    << pos << std::endl;          // handle collision        pos = inter;//          std::cout << pos << std::endl;        PVector rem = new_pos - pos;        PVector coll_2_circle;        if( second )            coll_2_circle = second_post.center - pos;        else            coll_2_circle = post.center - pos;//          std::cout << "rem = " << rem << endl;                rem.rotate( -coll_2_circle.th() );        rem.x = -rem.x;        rem.rotate( coll_2_circle.th() );        new_pos = pos + rem;//          std::cout << "rem = " << rem << endl;        // setup post and second post for next loop        post = nearestPost( pos, size );        second_post = nearestPost( new_pos, size ); //         std::cout << "pos = " << pos << endl;//          std::cout << "new_pos = " << new_pos << endl;//          std::cout << "nearest post = " << post << endl;//          std::cout << "dist = " << (pos - post.center).r() << endl;        // setup vel so it will decay normally.  The collisions are        // elastic, so the maginitude does not change, but the heading        // does        vel = Polar2PVector( vel.r(), rem.th() );//          std::cout << "vel = " << vel << endl;        second = false;    }    pos = new_pos;	vel *= decay ;	acc *= Zero ;  if( M_kicks_this_cycle != 0 )    M_was_multi_kicked = M_kicks_this_cycle > 1;    M_kicks_this_cycle = 0;}void MPObject::_turn(){    if (this->type == MPO_Player)      {        angle = ((Player *)this)->angle_body ;        ((Player *)this)->angle_neck_committed = ((Player *)this)->angle_neck ;        acc = 0.0;      }}void MPObject::collide(MPObject& obj){	Value r = size + obj.size ;	PVector dif = (pos - obj.pos) ;	Value d = pos.distance(obj.pos) ;	Angle th = fabs(dif.angle(vel)) ;	Value l1 = d * cos(th) ;	Value h = d * sin(th) ;	Value cosp = h / r ;	Value sinp = sqrt(1.0 - square(cosp)) ;	Value l2 = r * sinp ;	PVector dv = vel ;	dv.normalize(-(l1 + l2)) ;	pos += dv ;}void MPObject::step(MPObjectTable&){	++(*this) ;	//this->collisionCheck(tbl) ;	// collision checks are now performed after every object	// has been moved, not while the objects are being moved}void MPObject::push(PVector f){  acc += f ;}void MPObject::rotate(Angle a){	angle += a ;}void MPObject::rotate_to(Angle dir){	angle = dir ;}void MPObject::dash(Value f){	push(Polar2PVector(f, angle)) ;}/* *=================================================================== *Part: PObjectTable *=================================================================== */PObjectTable::PObjectTable(const TheNumber max){	Max = max ;	object = new PObject*[Max] ;	n = 0 ;}PObjectTable::~PObjectTable(){	delete object ;}std::ostream& operator<< (std::ostream& o, const PObjectTable& tbl) {	TheNumber i ;	o << "#PObjectTable[n=" << tbl.n << "/" << tbl.Max << std::endl ;	for(i = 0 ; i < tbl.n ; i++)		o << "  " << *(tbl.object[i]) << std::endl ;	return o << "]" << std::endl ;}PObject* PObjectTable::AssignObject(PObject* obj){	object[n] = obj ;	n++ ;	return obj ;}/* pfr 06/07/200 added short name support */PObject* PObjectTable::NewObject( const PObject::obj_type& object_type, 				  const Name nm, 				  const Name s_nm, 				  const std::string& sub_name ){	PObject *obj = new PObject ( object_type, nm, s_nm, sub_name );	AssignObject(obj) ;	return obj ;}/* *=================================================================== *Part: MPObjectTable *=================================================================== */MPObjectTable::MPObjectTable(const TheNumber max){	randomize() ;	Max = max ;	object = new MPObject*[Max] ;	n = 0 ;}MPObjectTable::~MPObjectTable(){	delete object ;}std::ostream& operator<< (std::ostream& o, const MPObjectTable& tbl) {	TheNumber i ;	o << "#MPObjectTable[n=" << tbl.n << "/" << tbl.Max << std::endl ;	 for(i = 0 ; i < tbl.n ; i++)		o << "\t" << *(tbl.object[i]) << std::endl ;	return o << "]" << std::endl ;}MPObject* MPObjectTable::AssignObject(MPObject* obj){	object[n] = obj ;	n++ ;	return obj ;}/* pfr 06/07/200 added short name support */MPObject* MPObjectTable::NewObject( const PObject::obj_type& object_type,				    const Name nm, 				    const Name s_nm, 				    const std::string& sub_name){	MPObject *obj = new MPObject( object_type, nm, s_nm, sub_name) ;	AssignObject(obj) ;	return obj ;}void MPObjectTable::_inc(  const PlayMode& playmode ){	TheNumber i ;	int sw ;	MPObject **obj ;	MPObject *buf ;	obj = object ;	/* randomize order */	for(i = 0; i < n ; i++) {		sw = irand(n) ;		buf = *(obj+i) ;		*(obj+i) = *(obj+sw) ;		*(obj+sw) = buf ;	}	for(i = 0; i < n ; i++) {		(**obj).step(*this) ;		obj++ ;	}	//new collisions	collisions ( playmode );}void MPObjectTable::collisions ( const PlayMode& playmode ){  bool col = false;  int max = 10;  do     {      col = false;      /*        cout << "max = " << max << std::endl; */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -