/*!

*/
int
GoalieMessageParser::parse( const int sender,
                            const double & ,
                            const char * msg,
                            const GameTime & current )
{
    // format:
    //    "g<pos_body:4>"
    // the length of message == 5

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "GoalieMessageParser::parse()."
                  << " Illegal message [" << msg
                  << "] len = " << std::strlen( msg )
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "GoalieMessageParser: Failed to decode Goalie Info [%s]",
                      msg );
        return -1;
    }
    ++msg;

    boost::int64_t ival = 0;
    if ( ! AudioCodec::i().decodeStrToInt64( std::string( msg, slength() - 1 ),
                                             &ival ) )
    {
        std::cerr << "GoalieMessageParser::parse()"
                  << " Failed to parse [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "GoalieMessageParser: Failed to decode Goalie Info [%s]",
                      msg );
        return -1;
    }

    Vector2D goalie_pos;
    AngleDeg goalie_body;

    goalie_body = static_cast< double >( ival % 360 - 180 );
    ival /= 360;

    goalie_pos.y = ( ival % 400 ) * 0.1 - 20.0;
    ival /= 400;

    goalie_pos.x = ( ival % 160 ) * 0.1 + ( 53.0 - 16.0 );

    dlog.addText( Logger::SENSOR,
                  "GoalieMessageParser: success! goalie pos = (%.2f %.2f) body = %.1f",
                  goalie_pos.x, goalie_pos.y, goalie_body.degree() );

    M_memory->setOpponentGoalie( sender, goalie_pos, goalie_body, current );

    return slength();
}
Beispiel #2
0
/**
 * Compares two strings together
 */
static int stringCmp(char * str1, char * str2) {
	int len1=slength(str1);
	int len2=slength(str2);
	if (len1 != len2) return 0;
	int i;
	for (i=0;i<len1;i++) {
		if (str1[i] != str2[i]) return 0;
	}
	return 1;
}
/*!

*/
int
BallMessageParser::parse( const int sender ,
                          const double & ,
                          const char * msg,
                          const GameTime & current )
{
    // format:
    //    "b<pos_vel:5>"
    // the length of message == 6

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "***ERROR*** BallMessageParser::parse()"
                  << " Illegal ball message [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "BallMessageParser: Illegal ball info [%s]",
                      msg );
        return -1;
    }
    ++msg;

    Vector2D ball_pos;
    Vector2D ball_vel;

    if ( ! AudioCodec::i().decodeStr5ToPosVel( std::string( msg, slength() - 1 ),
                                               &ball_pos, &ball_vel ) )
    {
        std::cerr << "***ERROR*** BallMessageParser::parse()"
                  << " Failed to decode ball [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "BallMessageParser: Failed to decode Ball Info [%s]",
                      msg );
        return -1;
    }

    dlog.addText( Logger::SENSOR,
                  "BallMessageParser::parse() success! pos(%.1f %.1f) vel(%.1f %.1f)",
                  ball_pos.x, ball_pos.y,
                  ball_vel.x, ball_vel.y );

    M_memory->setBall( sender, ball_pos, ball_vel, current );

    return slength();
}
/*!

*/
int
RecoveryMessageParser::parse( const int sender,
                              const double & ,
                              const char * msg,
                              const GameTime & current )
{
    // format:
    //    "r<rate:1>"
    // the length of message == 2

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "RecoveryMessageParser::parse()"
                  << " Illegal message [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "RecoveryMessageParser: Failed to decode Recovery Rate [%s]",
                      msg );
        return -1;
    }
    ++msg;

    double rate = AudioCodec::i().decodeCharToPercentage( *msg );
    if ( rate == AudioCodec::ERROR_VALUE )
    {
        std::cerr << "RecoveryMessageParser::parser()"
                  << " Failed to read recovery rate [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "RecoveryMessageParser: Failed to decode Recovery Rate [%s]",
                      msg );
        return -1;
    }

    double recovery
        = rate * ( ServerParam::i().recoverInit() - ServerParam::i().recoverMin() )
        + ServerParam::i().recoverMin();

    dlog.addText( Logger::SENSOR,
                  "RecoverMessageParser::parse() success! rate=%f recovery=%.3f",
                  rate, recovery );

    M_memory->setRecovery( sender, rate, current );

    return slength();
}
/*!

*/
int
DefenseLineMessageParser::parse( const int sender,
                                 const double & ,
                                 const char * msg,
                                 const GameTime & current )
{
    // format:
    //    "d<x_rate:1>"
    // the length of message == 2

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "DefenseLineMessageParser::parse()"
                  << " Illegal message [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "DefenseLineMessageParser: Failed to decode Defense Line Info [%s]",
                      msg );
        return -1;
    }
    ++msg;

    double rate = AudioCodec::i().decodeCharToPercentage( *msg );

    if ( rate == AudioCodec::ERROR_VALUE )
    {
        std::cerr << "DefenseLineMessageParser::parser()"
                  << " Failed to read offside line [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "DefenseLineMessageParser: Failed to decode Defense Line Info [%s]",
                      msg );
        return -1;
    }

    double defense_line_x = 52.0 + ( -10.0 + 52.0 ) * rate;

    dlog.addText( Logger::SENSOR,
                  "DefenseLineMessageParser::parse() success! x=%.1f rate=%.3f",
                  defense_line_x, rate );

    M_memory->setDefenseLine( sender, defense_line_x, current );

    return slength();
}
/*!

*/
int
StaminaMessageParser::parse( const int sender,
                             const double & ,
                             const char * msg,
                             const GameTime & current )
{
    // format:
    //    "s<rate:1>"
    // the length of message == 2

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "StaminaMessageParser::parse()"
                  << " Illegal message [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "StaminaMessageParser: Failed to decode Stamina Rate [%s]",
                      msg );
        return -1;
    }
    ++msg;

    double rate = AudioCodec::i().decodeCharToPercentage( *msg );
    if ( rate < 0.0 || 1.00001 < rate )
    {
        std::cerr << "StaminaMessageParser::parser()"
                  << " Failed to read stamina rate [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "StaminaMessageParser: Failed to decode Stamina Rate [%s]",
                      msg );
        return -1;
    }

    double stamina = ServerParam::i().staminaMax() * rate;

    dlog.addText( Logger::SENSOR,
                  "StaminaMessageParser::parse() success! rate=%f stamina=%.1f",
                  rate, stamina );

    M_memory->setStamina( sender, rate, current );

    return slength();
}
/*!

*/
int
PassRequestMessageParser::parse( const int sender,
                                 const double & ,
                                 const char * msg,
                                 const GameTime & current )
{
    // format:
    //    "h<pos:3>"
    // the length of message == 4

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "PassRequestMessageParser::parse()"
                  << " Illegal pass request message ["
                  << msg << "] len = " << std::strlen( msg )
                  << std::endl;
        return -1;
    }
    ++msg;

    Vector2D pos;

    if ( ! AudioCodec::i().decodeStr3ToPos( std::string( msg, slength() - 1 ),
                                            &pos ) )
    {
        std::cerr << "PassRequestMessage::parse()"
                  << " Failed to decode pass request potiiton. ["
                  << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "AudioSensor: Failed to decode hey pass potiiton" );
        return -1;
    }

    dlog.addText( Logger::SENSOR,
                  "PassRequestMessageParser: success! "
                  "sender = %d  request pos = (%.2f %.2f)",
                  sender, pos.x, pos.y );

    M_memory->setPassRequest( sender, pos, current );

    return slength();
}
Beispiel #8
0
WebCrawler::WebCrawler(int maxUrls, int nInitialUrls, const char **initialUrls)
{
    _urlArray = new URLRecord[nInitialUrls + maxUrls];

    _maxUrls = maxUrls;
    _headURL = 0;
    _tailURL = nInitialUrls;

    int i;
    for (i = 0; i < nInitialUrls; i++)
    {
        char *tmp = new char[strlen(initialUrls[i])];
        strcpy(tmp, initialUrls[i]);

        if (tmp[slength(tmp) - 1] != '/')
        {
            tmp = strcat(tmp, "/");
        }

        _urlArray[i]._url = tmp;
        _urlArray[i]._description = NULL;
    }

    _urlToUrlRecord = new HashTableTemplate<int>();
    _wordToURLRecordList = new HashTableTemplate<URLRecordList*>();
}
Beispiel #9
0
void loadfs(char *fname, char *lstr) {
  int flen = slength(fname);
  file f;
  int errcode = 0;

  if (flen <= 0) {
    iwrites("Error: No filename given\n");
  }
  else {
    f = fget(fname);
  }

  if (flen > 0 && f) {
    errcode = freads(lstr, f, 0);
    
    if (errcode == 0) {
      iwrites("The string loaded to the buffer looks like this:\n");
      iwrites(lstr);
      iwrites("\n");
    }
    else if (errcode == 1) {
      iwrites("Error: End of file reached\n");
    }
    else if (errcode == 2) {
      iwrites("Error: An unknown error occurred\n");
    }
  }
  else if (!f) {
    iwrites("Error: No file by that name was found\n");
  } 
}
Beispiel #10
0
/**
 * String concatenation performed on the host (and any needed data transformations)
 */
struct value_defn performStringConcatenation(struct value_defn v1, struct value_defn v2) {
	struct value_defn v;
	v.type=STRING_TYPE;
	v.dtype=SCALAR;

	sharedData->core_ctrl[myId].data[0]=v1.type;
	if (v1.type == STRING_TYPE) {
		char * v;
		cpy(&v, &v1.data, sizeof(char*));
		copyStringToSharedMemoryAndSetLocation(v, 1);
	} else {
		cpy(&sharedData->core_ctrl[myId].data[1], v1.data, 4);
	}
	sharedData->core_ctrl[myId].data[5]=v2.type;
	if (v2.type == STRING_TYPE) {
		char * v;
		cpy(&v, &v2.data, sizeof(char*));
		copyStringToSharedMemoryAndSetLocation(v, 6);
	} else {
		cpy(&sharedData->core_ctrl[myId].data[6], v2.data, 4);
	}
	sharedData->core_ctrl[myId].data[10]=STRING_TYPE;
	cpy(&sharedData->core_ctrl[myId].data[11], &sharedDataEntries, sizeof(unsigned int));

	sharedData->core_ctrl[myId].core_command=4;
	unsigned int pb=sharedData->core_ctrl[myId].core_busy;
	sharedData->core_ctrl[myId].core_busy=0;
	while (sharedData->core_ctrl[myId].core_busy==0 || sharedData->core_ctrl[myId].core_busy<=pb) { }

	v.type=STRING_TYPE;
	char * strPtr=sharedData->core_ctrl[myId].shared_data_start + sharedDataEntries;
	cpy(&v.data, &strPtr, sizeof(char*));
	sharedDataEntries+=slength(strPtr)+1;
	return v;
}
Beispiel #11
0
/**
 * Copies some string into shared memory and sets the location in the data core area
 */
static int copyStringToSharedMemoryAndSetLocation(char * string, int start) {
	int len=slength(string)+1;
	char* ptr=sharedData->core_ctrl[myId].shared_data_start + sharedDataEntries;
	cpy(ptr, string, len);
	cpy(&sharedData->core_ctrl[myId].data[start], &sharedDataEntries, sizeof(unsigned int));
	sharedDataEntries+=len;
	return len;
}
Beispiel #12
0
main()
{
char arr[25];
int len;
printf("Enter a string\n");
scanf("%s",arr);
len=slength(arr);
printf("The length of the string is %d \n",len);
}
Beispiel #13
0
/*
 * Return the number of stmts in the flowgraph reachable by 'p'.
 * The nodes should be unmarked before calling.
 *
 * Note that "stmts" means "instructions", and that this includes
 *
 *	side-effect statements in 'p' (slength(p->stmts));
 *
 *	statements in the true branch from 'p' (count_stmts(JT(p)));
 *
 *	statements in the false branch from 'p' (count_stmts(JF(p)));
 *
 *	the conditional jump itself (1);
 *
 *	an extra long jump if the true branch requires it (p->longjt);
 *
 *	an extra long jump if the false branch requires it (p->longjf).
 */
static u_int
count_stmts(struct block *p)
{
	u_int n;

	if (p == 0 || isMarked(p))
		return 0;
	Mark(p);
	n = count_stmts(JT(p)) + count_stmts(JF(p));
	return slength(p->stmts) + n + 1 + p->longjt + p->longjf;
}
Beispiel #14
0
void printf(char *fname, char *lstr) {
  int flen = slength(fname);
  int errcode = 0;
  file f;
  int slen = 0;
  //char *str;

  //iwrites("Write your string and finish it by pressing the ENTER key.\n");
  //ireads(str, 5);
  slen = slength(lstr);

  if (slen <= 0 || flen <= 0)
    errcode = 3; // at least one of the parameters was not specified

  if (errcode == 3) {
    iwrites("Error: At least one of the parameters was not specified\n");
  }
  else {
    f = fget(fname); 
  }

  if (!f) {
    iwrites("Error: No file by that name was found\n");
    iwrites("The provided name was ");
    iwrites(fname);
    iwrites(".\n");
  }
  else {
    errcode = fwrites(lstr, f, 0);
  }

  if (errcode == 0 && f) {
    iwrites("File was written to successfully\n");
  }
  else if (errcode == 1) {
    iwrites("Error: End of file reached\n");
  } 
  else if (errcode == 2) {
    iwrites("Error: An unknown error has occurred\n");
  }
}
Beispiel #15
0
void echo(char *str) {
  int len = slength(str);

  if (len <= 0) {
    iwrites("Error: No string was provided");
  }
  else {
    iwrites(str);
  }

  iwrites("\n");
}
Beispiel #16
0
void cons(char *str, char *lstr) {
  int len = slength(str);

  if (len <= 0) {
    iwrites("Error: No string entered\n");
  }
  else {
    sconcat(lstr, str);

    iwrites("The string added to the buffer looks like this:\n");
    iwrites(str);
    iwrites("\n");
  }
}
/*!

*/
int
WaitRequestMessageParser::parse( const int sender,
                                 const double & ,
                                 const char * msg,
                                 const GameTime & current )
{
    if ( *msg != sheader() )
    {
        return 0;
    }

    M_memory->setWaitRequest( sender, current );
    return slength();
}
Beispiel #18
0
void sreverse(char a[])
{
int i,j,l;
char temp;
l=slength(a);

for(i=0,j=l-1;i<j;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

printf("The reversed string is %s \n",a);
}
void test_string_lenth() {
  const char *test_type = "string", *test_content = "lenth";
  bool ok = 1;

  String s = cstr2str("123456789");
  if (slength(s) != 9) {
    ok = 0;
  }

  if (!ok) {
    printf("%s test fails on %s.\n", test_type, test_content);
  }

  sdestro(&s);
}
Beispiel #20
0
void loads(char *input, char *lstr) {
  int len = slength(input);

  if (len <= 0) {
    iwrites("Error: No string entered\n");
  }
  else if (len > 512) {
    iwrites("String cannot be larger than 512 characters.\n");
  }
  else {
    scopy(lstr, input, len);
    iwrites("The string loaded to the buffer looks like this:\n");
    iwrites(lstr);
    iwrites("\n");
  }
}
Beispiel #21
0
/**
 * Does the copying required to get input from the host, waits until this is ready and then sets the
 * type and data correctly
 */
static struct value_defn doGetInputFromUser() {
	struct value_defn v;
	v.dtype=SCALAR;
	cpy(&sharedData->core_ctrl[myId].data[6], &sharedDataEntries, sizeof(unsigned int));
	sharedData->core_ctrl[myId].core_command=2;
	unsigned int pb=sharedData->core_ctrl[myId].core_busy;
	sharedData->core_ctrl[myId].core_busy=0;
	while (sharedData->core_ctrl[myId].core_busy==0 || sharedData->core_ctrl[myId].core_busy<=pb) { }
	v.type=sharedData->core_ctrl[myId].data[0];
	if (v.type==STRING_TYPE) {
		char * strPtr=sharedData->core_ctrl[myId].shared_data_start + sharedDataEntries;
		cpy(&v.data, &strPtr, sizeof(char*));
		sharedDataEntries+=slength(strPtr)+1;
	} else {
		cpy(v.data, &sharedData->core_ctrl[myId].data[1], 4);
	}
	return v;
}
/*!

*/
int
BallGoalieMessageParser::parse( const int sender,
                                const double & ,
                                const char * msg,
                                const GameTime & current )
{
    // format:
    //    "G<bpos_bvel_gpos_gbody:9>"
    // the length of message == 10

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "BallGoalieMessageParser::parse()"
                  << " Illegal message ["
                  << msg << "] len = " << std::strlen( msg )
                  << std::endl;
        return -1;
    }
    ++msg;

    boost::int64_t ival = 0;
    if ( ! AudioCodec::i().decodeStrToInt64( std::string( msg, slength() - 1 ),
                                             &ival ) )
    {
        std::cerr << "BallGoalieMessageParser::parse()"
                  << " Failed to parse [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "BallGoalieMessageParser: Failed to decode Goalie Info [%s]",
                      msg );
        return -1;
    }

    // 74^9                       = 66540410775079424
    // 1050*680*60*60*160*400*360 = 59222016000000000
    // 1050*680*63*63*160*400*360 = 65292272640000000

    const double max_speed = ServerParam::i().ballSpeedMax();
    const double prec_speed = max_speed * 2.0 / 63.0;

    Vector2D ball_pos;
    Vector2D ball_vel;
    Vector2D goalie_pos;
    AngleDeg goalie_body;

    goalie_body = static_cast< double >( ival % 360 - 180 );
    ival /= 360;

    goalie_pos.y = ( ival % 400 ) * 0.1 - 20.0;
    ival /= 400;

    goalie_pos.x = ( ival % 160 ) * 0.1 + ( 52.5 - 16.0 );
    ival /= 160;

    ball_vel.y = ( ival % 63 ) * prec_speed - max_speed;
    ival /= 63;

    ball_vel.x = ( ival % 63 ) * prec_speed - max_speed;
    ival /= 63;

    ball_pos.y = ( ival % 680 ) * 0.1 - 34.0;
    ival /= 680;

    ball_pos.x = ( ival % 1050 ) * 0.1 - 52.5;
    //ival /= 1050;

    dlog.addText( Logger::SENSOR,
                  "BallGoalieMessageParser: success! "
                  "sender = %d  bpos(%.1f %.1f) bvel(%.1f %.1f)"
                  " gpos(%.1f %.1f) gbody %.1f",
                  sender,
                  ball_pos.x, ball_pos.y,
                  ball_vel.x, ball_vel.y,
                  goalie_pos.x, goalie_pos.y,
                  goalie_body.degree() );

    M_memory->setBall( sender, ball_pos, ball_vel, current );
    M_memory->setOpponentGoalie( sender, goalie_pos, goalie_body, current );

    return slength();
}
/*!

*/
int
OpponentMessageParser::parse( const int sender,
                              const double & ,
                              const char * msg,
                              const GameTime & current )
{
    // format:
    //    "O<unum_pos_body:4>"
    // the length of message == 5

    //11 * 105/0.7 * 68/0.7 * 360/2
    // -> 11 * 151 * 98 * 180
    // =29300040 < 4 characters(74^4=29986576)


    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "OpponentMessageParser::parse()"
                  << " Illegal message ["
                  << msg << "] len = " << std::strlen( msg )
                  << std::endl;
        return -1;
    }
    ++msg;

    boost::int64_t ival = 0;
    if ( ! AudioCodec::i().decodeStrToInt64( std::string( msg, slength() - 1 ),
                                             &ival ) )
    {
        std::cerr << "OpponentMessageParser::parse()"
                  << " Failed to parse [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "OpponentMessageParser: Failed to decode Player Info [%s]",
                      msg );
        return -1;
    }

    int player_unum = Unum_Unknown;
    Vector2D player_pos;
    AngleDeg player_body;

    // 180=360/2
    player_body = static_cast< double >( ( ival % 180 ) * 2 - 180 );
    ival /= 180;

    // 98=68/0.7=97.14
    player_pos.y = ( ival % 98 ) * 0.7 - 34.0;
    ival /= 98;

    // 151>105/0.7=150
    player_pos.x = ( ival % 151 ) * 0.7 - 52.5;
    ival /= 151;

    player_unum = ( ival % 11 ) + 1;
    // ival /= 11

    dlog.addText( Logger::SENSOR,
                  "OpponentMessageParser: success! "
                  "unum = %d  pos(%.1f %.1f) body %.1f",
                  player_unum,
                  player_pos.x, player_pos.y,
                  player_body.degree() );

    M_memory->setPlayer( sender,
                         player_unum + 11,
                         player_pos,
                         player_body.degree(),
                         -1.0, // unknown stamina
                         current );

    return slength();
}
/*!

*/
int
DribbleMessageParser::parse( const int sender,
                                   const double & ,
                                   const char * msg,
                                   const GameTime & current )
{
    // format:
    //    "D<count_pos:3>"
    // the length of message == 4

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "DribbleMessageParser::parse()"
                  << " Illegal message ["
                  << msg << "] len = " << std::strlen( msg )
                  << std::endl;
        return -1;
    }
    ++msg;

    boost::int64_t ival = 0;

    if ( ! AudioCodec::i().decodeStrToInt64( std::string( msg, slength() - 1 ),
                                             &ival ) )
    {
        std::cerr << "DribbleMessageParser::parse()"
                  << " Failed to parse [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "DribbleMessageParser: Failed to decode Dribble Info [%s]",
                      msg );
        return -1;
    }

    Vector2D pos;
    int count;

    count = static_cast< int >( ival % 10 ) + 1;
    ival /= 10;

    boost::int64_t div = static_cast< boost::int64_t >( std::ceil( 68.0 / 0.5 ) );
    pos.y = ( ival % div ) * 0.5 - 34.0;
    ival /= div;

    // div = static_cast< boost::int64_t >( std::ceil( 105.0 / 0.5 ) );
    // pos.x = ( ival % div ) * 0.5 - 52.5;
    pos.x = ival * 0.5 - 52.5;


    dlog.addText( Logger::SENSOR,
                  "DribbleMessageParser: success! "
                  "sender = %d  target_pos=(%.2f %.2f) count=%d",
                  sender, pos.x, pos.y, count );

    M_memory->setDribbleTarget( sender, pos, count, current );

    return slength();
}
/*!

*/
int
SelfMessageParser::parse( const int sender,
                          const double & ,
                          const char * msg,
                          const GameTime & current )
{
    // format:
    //    "S<pos_body_stamina:4>"
    // the length of message == 5

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "SelfMessageParser::parse()"
                  << " Illegal message ["
                  << msg << "] len = " << std::strlen( msg )
                  << std::endl;
        return -1;
    }
    ++msg;

    boost::int64_t ival = 0;
    if ( ! AudioCodec::i().decodeStrToInt64( std::string( msg, slength() - 1 ),
                                             &ival ) )
    {
        std::cerr << "SelfMessageParser::parse()"
                  << " Failed to parse [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "SelfMessageParser: Failed to decode Player Info [%s]",
                      msg );
        return -1;
    }

    int player_unum = sender;
    Vector2D player_pos;
    AngleDeg player_body;
    double stamina = -1.0;

    // 11
    stamina = ServerParam::i().staminaMax() * static_cast< double >( ( ival % 11 ) ) / 10.0;
    ival /= 11;

    // 60=360/6
    player_body = ( ival % 60 ) * 6.0 - 180.0;
    ival /= 60;

    // 171 > 68/0.4
    player_pos.y = ( ival % 171 ) * 0.4 - 34.0;
    ival /= 171;

    // 264 > 105/0.4=262.5
    player_pos.x = ( ival % 264 ) * 0.4 - 52.5;
    //ival /= 264;

    dlog.addText( Logger::SENSOR,
                  "SelfMessageParser: success! "
                  "unum = %d  pos(%.1f %.1f) body=%.1f stamina=%f",
                  player_unum,
                  player_pos.x, player_pos.y,
                  player_body.degree(),
                  stamina );

    M_memory->setPlayer( sender,
                         player_unum,
                         player_pos,
                         player_body.degree(),
                         stamina,
                         current );

    return slength();
}
Beispiel #26
0
/*
 * Allocate memory.  All allocation is done before optimization
 * is begun.  A linear bound on the size of all data structures is computed
 * from the total number of blocks and/or statements.
 */
static void
opt_init(struct block *root)
{
	bpf_u_int32 *p;
	int i, n, max_stmts;

	/*
	 * First, count the blocks, so we can malloc an array to map
	 * block number to block.  Then, put the blocks into the array.
	 */
	unMarkAll();
	n = count_blocks(root);
	blocks = (struct block **)calloc(n, sizeof(*blocks));
	if (blocks == NULL)
		bpf_error("malloc");
	unMarkAll();
	n_blocks = 0;
	number_blks_r(root);

	n_edges = 2 * n_blocks;
	edges = (struct edge **)calloc(n_edges, sizeof(*edges));
	if (edges == NULL)
		bpf_error("malloc");

	/*
	 * The number of levels is bounded by the number of nodes.
	 */
	levels = (struct block **)calloc(n_blocks, sizeof(*levels));
	if (levels == NULL)
		bpf_error("malloc");

	edgewords = n_edges / (8 * sizeof(bpf_u_int32)) + 1;
	nodewords = n_blocks / (8 * sizeof(bpf_u_int32)) + 1;

	/* XXX */
	space = (bpf_u_int32 *)malloc(2 * n_blocks * nodewords * sizeof(*space)
				 + n_edges * edgewords * sizeof(*space));
	if (space == NULL)
		bpf_error("malloc");
	p = space;
	all_dom_sets = p;
	for (i = 0; i < n; ++i) {
		blocks[i]->dom = p;
		p += nodewords;
	}
	all_closure_sets = p;
	for (i = 0; i < n; ++i) {
		blocks[i]->closure = p;
		p += nodewords;
	}
	all_edge_sets = p;
	for (i = 0; i < n; ++i) {
		register struct block *b = blocks[i];

		b->et.edom = p;
		p += edgewords;
		b->ef.edom = p;
		p += edgewords;
		b->et.id = i;
		edges[i] = &b->et;
		b->ef.id = n_blocks + i;
		edges[n_blocks + i] = &b->ef;
		b->et.pred = b;
		b->ef.pred = b;
	}
	max_stmts = 0;
	for (i = 0; i < n; ++i)
		max_stmts += slength(blocks[i]->stmts) + 1;
	/*
	 * We allocate at most 3 value numbers per statement,
	 * so this is an upper bound on the number of valnodes
	 * we'll need.
	 */
	maxval = 3 * max_stmts;
	vmap = (struct vmapinfo *)calloc(maxval, sizeof(*vmap));
	vnode_base = (struct valnode *)calloc(maxval, sizeof(*vnode_base));
	if (vmap == NULL || vnode_base == NULL)
		bpf_error("malloc");
}
/*!

*/
int
InterceptMessageParser::parse( const int sender,
                               const double & ,
                               const char * msg,
                               const GameTime & current )
{
    // format:
    //    "i<unum:1><cycle:1>"
    // the length of message == 3

    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "InterceptMessageParser::parse()"
                  << " Illegal message = [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "AudioSensor: Failed to decode intercept info [%s]",
                      msg );
        return -1;
    }
    ++msg;

    AudioCodec::CharToIntCont::const_iterator unum_it
        = AudioCodec::i().charToIntMap().find( *msg );
    if ( unum_it == AudioCodec::i().charToIntMap().end()
         || unum_it->second <= 0
         || MAX_PLAYER*2 < unum_it->second )
    {
        std::cerr << "InterceptMessageParser::parse() "
                  << " Illegal player number. message = [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "InterceptMessageParser: Failed to decode intercept info [%s]",
                      msg );
        return -1;
    }
    ++msg;

    AudioCodec::CharToIntCont::const_iterator cycle
        = AudioCodec::i().charToIntMap().find( *msg );
    if ( cycle == AudioCodec::i().charToIntMap().end() )
    {
        std::cerr << "InterceptMessageParser::parse() "
                  << " Illegal cycle. message = [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "InterceptMessageParser: Failed to decode intercept info [%s]",
                      msg );
        return -1;
    }

    dlog.addText( Logger::SENSOR,
                  "InterceptMessageParser: success! number=%d cycle=%d",
                  unum_it->second, cycle->second );

    M_memory->setIntercept( sender, unum_it->second, cycle->second, current );

    return slength();
}
/*!

*/
int
GoalieAndPlayerMessageParser::parse( const int sender,
                                   const double & ,
                                   const char * msg,
                                   const GameTime & current )
{
    // format:
    //    "g<pos_body:4,unm_pos:3>"
    // the length of message == 8

    // ( 22 * 105/0.63 * 68/0.63 ) * ( 16.0/0.1 * 40.0/0.1 * 360 )
    // -> (22 * 168 * 109) * (160 * 400 * 360) = 9281986560000

    // 74^6 = 164206490176
    //        9281986560000
    // 74^7 = 12151280273024

    // ( 22 * 105/0.55 * 68/0.55 ) * ( 16.0/0.1 * 40.0/0.1 * 360 )
    // -> (22 * 192 * 125) * (160 * 400 * 360) = 12165120000000

    // ==========
    // ( 22 * 105/0.555 * 68/0.555 ) * ( 16.0/0.1 * 40.0/0.1 * 360 )
    // -> (22 * 191 * 124) * (160 * 400 * 360) = 12004945920000
    // ==========

    // ( 22 * 105/0.56 * 68/0.55 ) * ( 16.0/0.1 * 40.0/0.1 * 360 )
    // -> (22 * 189 * 123) * (160 * 400 * 360) = 11783439360000


    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "Goalie1PlayerMessageParser::parse()."
                  << " Illegal message [" << msg
                  << "] len = " << std::strlen( msg )
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "Goalie1PlayerMessageParser: Failed to decode Goalie Info [%s]",
                      msg );
        return -1;
    }
    ++msg;

    boost::int64_t ival = 0;
    if ( ! AudioCodec::i().decodeStrToInt64( std::string( msg, slength() - 1 ),
                                             &ival ) )
    {
        std::cerr << "Goalie1PlayerMessageParser::parse()"
                  << " Failed to parse [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "Goalie1PlayerMessageParser: Failed to decode Goalie Info [%s]",
                      msg );
        return -1;
    }

    Vector2D goalie_pos;
    AngleDeg goalie_body;
    int player_number = Unum_Unknown;
    Vector2D player_pos;

    // 124 > 68/0.555 + 1
    player_pos.y = ( ival % 124 ) * 0.555 - 34.0;
    ival /= 124;

    // 191 > 105/0.555 + 1
    player_pos.x = ( ival % 191 ) * 0.555 - 52.5;
    ival /= 191;

    // 22
    player_number = ( ival % 22 ) + 1;
    ival /= 22;


    goalie_body = static_cast< double >( ival % 360 - 180 );
    ival /= 360;

    goalie_pos.y = ( ival % 400 ) * 0.1 - 20.0;
    ival /= 400;

    goalie_pos.x = ( ival % 160 ) * 0.1 + ( 53.0 - 16.0 );
    // ival /= 160;


    dlog.addText( Logger::SENSOR,
                  "GoalieAndPlayerMessageParser: success! goalie pos=(%.2f %.2f) body=%.1f",
                  goalie_pos.x, goalie_pos.y, goalie_body.degree() );
    dlog.addText( Logger::SENSOR,
                  "____ player number=%d pos=(%.2f %.2f)",
                  player_number, player_pos.x, player_pos.y );

    M_memory->setOpponentGoalie( sender, goalie_pos, goalie_body, current );
    M_memory->setPlayer( sender, player_number, player_pos, current );

    return slength();
}
Beispiel #29
0
/*
 * Returns true if successful.  Returns false if a branch has
 * an offset that is too large.  If so, we have marked that
 * branch so that on a subsequent iteration, it will be treated
 * properly.
 */
static int
convert_code_r(struct block *p)
{
	struct bpf_insn *dst;
	struct slist *src;
	u_int slen;
	u_int off;
	int extrajmps;		/* number of extra jumps inserted */
	struct slist **offset = NULL;

	if (p == 0 || isMarked(p))
		return (1);
	Mark(p);

	if (convert_code_r(JF(p)) == 0)
		return (0);
	if (convert_code_r(JT(p)) == 0)
		return (0);

	slen = slength(p->stmts);
	dst = ftail -= (slen + 1 + p->longjt + p->longjf);
		/* inflate length by any extra jumps */

	p->offset = dst - fstart;

	/* generate offset[] for convenience  */
	if (slen) {
		offset = (struct slist **)calloc(slen, sizeof(struct slist *));
		if (!offset) {
			bpf_error("not enough core");
			/*NOTREACHED*/
		}
	}
	src = p->stmts;
	for (off = 0; off < slen && src; off++) {
#if 0
		printf("off=%d src=%x\n", off, src);
#endif
		offset[off] = src;
		src = src->next;
	}

	off = 0;
	for (src = p->stmts; src; src = src->next) {
		if (src->s.code == NOP)
			continue;
		dst->code = (u_short)src->s.code;
		dst->k = src->s.k;

		/* fill block-local relative jump */
		if (BPF_CLASS(src->s.code) != BPF_JMP || src->s.code == (BPF_JMP|BPF_JA)) {
#if 0
			if (src->s.jt || src->s.jf) {
				bpf_error("illegal jmp destination");
				/*NOTREACHED*/
			}
#endif
			goto filled;
		}
		if (off == slen - 2)	/*???*/
			goto filled;

	    {
		u_int i;
		int jt, jf;
static const char ljerr[] = "%s for block-local relative jump: off=%d";

#if 0
		printf("code=%x off=%d %x %x\n", src->s.code,
			off, src->s.jt, src->s.jf);
#endif

		if (!src->s.jt || !src->s.jf) {
			bpf_error(ljerr, "no jmp destination", off);
			/*NOTREACHED*/
		}

		jt = jf = 0;
		for (i = 0; i < slen; i++) {
			if (offset[i] == src->s.jt) {
				if (jt) {
					bpf_error(ljerr, "multiple matches", off);
					/*NOTREACHED*/
				}

				dst->jt = i - off - 1;
				jt++;
			}
			if (offset[i] == src->s.jf) {
				if (jf) {
					bpf_error(ljerr, "multiple matches", off);
					/*NOTREACHED*/
				}
				dst->jf = i - off - 1;
				jf++;
			}
		}
		if (!jt || !jf) {
			bpf_error(ljerr, "no destination found", off);
			/*NOTREACHED*/
		}
	    }
filled:
		++dst;
		++off;
	}
	if (offset)
		free(offset);

#ifdef BDEBUG
	bids[dst - fstart] = p->id + 1;
#endif
	dst->code = (u_short)p->s.code;
	dst->k = p->s.k;
	if (JT(p)) {
		extrajmps = 0;
		off = JT(p)->offset - (p->offset + slen) - 1;
		if (off >= 256) {
		    /* offset too large for branch, must add a jump */
		    if (p->longjt == 0) {
		    	/* mark this instruction and retry */
			p->longjt++;
			return(0);
		    }
		    /* branch if T to following jump */
		    dst->jt = extrajmps;
		    extrajmps++;
		    dst[extrajmps].code = BPF_JMP|BPF_JA;
		    dst[extrajmps].k = off - extrajmps;
		}
		else
		    dst->jt = off;
		off = JF(p)->offset - (p->offset + slen) - 1;
		if (off >= 256) {
		    /* offset too large for branch, must add a jump */
		    if (p->longjf == 0) {
		    	/* mark this instruction and retry */
			p->longjf++;
			return(0);
		    }
		    /* branch if F to following jump */
		    /* if two jumps are inserted, F goes to second one */
		    dst->jf = extrajmps;
		    extrajmps++;
		    dst[extrajmps].code = BPF_JMP|BPF_JA;
		    dst[extrajmps].k = off - extrajmps;
		}
		else
		    dst->jf = off;
	}
	return (1);
}
/*!

*/
int
BallPlayerMessageParser::parse( const int sender,
                                const double & ,
                                const char * msg,
                                const GameTime & current )
{
    // format:
    //    "P<bpos_bvel_unum_pos_body:9>"
    // the length of message == 10


    if ( *msg != sheader() )
    {
        return 0;
    }

    if ( (int)std::strlen( msg ) < slength() )
    {
        std::cerr << "OnePlayerMessageParser::parse()"
                  << " Illegal message ["
                  << msg << "] len = " << std::strlen( msg )
                  << std::endl;
        return -1;
    }
    ++msg;

    Vector2D ball_pos;
    Vector2D ball_vel;

    if ( ! AudioCodec::i().decodeStr5ToPosVel( std::string( msg, 5 ),
                                               &ball_pos, &ball_vel ) )
    {
        std::cerr << "***ERROR*** BallPlayerMessageParser::parse()"
                  << " Failed to decode ball [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "BallPlayerMessageParser: Failed to decode Ball Info [%s]",
                      msg );
        return -1;
    }
    msg += 5;

    boost::int64_t ival = 0;
    if ( ! AudioCodec::i().decodeStrToInt64( std::string( msg, 4 ),
                                             &ival ) )
    {
        std::cerr << "BallPlayerMessageParser::parse()"
                  << " Failed to parse [" << msg << "]"
                  << std::endl;
        dlog.addText( Logger::SENSOR,
                      "BallPlayerMessageParser: Failed to decode Player Info [%s]",
                      msg );
        return -1;
    }

    int player_unum = Unum_Unknown;
    Vector2D player_pos;
    AngleDeg player_body;

    // 180=360/2
    player_body = static_cast< double >( ( ival % 180 ) * 2 - 180 );
    ival /= 180;

    // 69 > 68/1.0
    player_pos.y = ( ival % 69 ) * 1.0 - 34.0;
    ival /= 69;

    // 106 > 105/1.0
    player_pos.x = ( ival % 106 ) * 1.0 - 52.5;
    ival /= 106;

    player_unum = ( ival % 22 ) + 1;
    // ival /= 22

    dlog.addText( Logger::SENSOR,
                  "BallPlayerMessageParser: success! "
                  " bpos(%.1f %.1f) bvel(%.1f %.1f)"
                  " unum=%d  pos(%.1f %.1f) body %.1f",
                  ball_pos.x, ball_pos.y,
                  ball_vel.x, ball_vel.y,
                  player_unum,
                  player_pos.x, player_pos.y,
                  player_body.degree() );

    M_memory->setBall( sender, ball_pos, ball_vel, current );
    M_memory->setPlayer( sender,
                         player_unum,
                         player_pos,
                         player_body.degree(),
                         -1.0, // unknown stamina
                         current );

    return slength();
}