📄 command.cpp
字号:
#include <math.h>
#include "command.h"
#include "client.h"
#include "stopwatch.h"
#include "utils.h"
#include "worldmodel.h"
#include "perceptron.h"
#include "log.h"
#include "netif.h"
/******************* Commmand *******************************/
void Command::move(float X,float Y){//move command
x = X;
y = Y;
type = CMD_move;
time = situation.CurrentTime;
}
void Command::dash(float pow){//dash command
power = pow;
type = CMD_dash;
time = situation.CurrentTime;
}
void Command::turn(float Moment){
angle = Moment;
type = CMD_turn;
time = situation.CurrentTime;
}
void Command::kick(float pow,float ang){ //kick command
power = pow;
angle = ang;
type = CMD_kick;
time = situation.CurrentTime;
}
void Command::seize(float ang){ //seize command
if ( !ClientParam::goalie ) my_error("Only goalies can catch");
if ( situation.playmode == PM_Before_Kick_Off ) return;
type=CMD_catch;
angle=ang;
time = situation.CurrentTime;
}
void Command::say(char* msg){
sprintf(StringBuffer::cmdbuf,"(say %s)",msg);
type = CMD_say;
time = situation.CurrentTime;
}
void Command::turn_neck(float ang){
angle = ang;
pose_limitation(angle, ServerParam::min_neck_moment, ServerParam::max_neck_moment);
type = CMD_turn_neck;
time = situation.CurrentTime;
}
void Command::change_view(){
type = CMD_change_view;
time = situation.CurrentTime;
}
void Command::disconnect(){
type = CMD_bye;
time = situation.CurrentTime;
}
void Command::stay(float desire_bdfc){
type = CMD_stay;
time = situation.CurrentTime;
angle = desire_bdfc;
}
void Command::Tackle(float Power)
{
this->power= Power;
type = CMD_tackle;
time = situation.CurrentTime;
}
void Command::PointTo(float dir,float dist)
{
this->angle = dir;
this->dist= dist;
type = CMD_pointto;
time = situation.CurrentTime;
}
void Command::Attentionto(int No)
{
this->No=No;
type = CMD_attentionto;
time = situation.CurrentTime;
}
void Command::donothing(){
type = CMD_donothing;
time = situation.CurrentTime;
}
bool Command::valid(){
return (bool) (type != CMD_none);
}
bool Command::valid(Time time){
return (bool) ((type != CMD_none && this->time == time)||
((type == CMD_move) && (this->time == -1)));
}
/*********************** Actions **********************************/
//maintain the history of actions
Actions::Actions(){
latest = 0;
kicks = 0;
dashes = 0;
turns = 0;
turn_necks = 0;
says = 0;
lastcatchtime = -5;
}
/************ send initial message *******/
void Actions::send_init_message(){//send initializing message
if (ServerParam::IP_reconnect)
sprintf(StringBuffer::cmdbuf, "(reconnect %s %d)", MyTeamName, ServerParam::IP_reconnect);
else if (ClientParam::goalie){
sprintf(StringBuffer::cmdbuf, "(init %s (version %.2f) (goalie))", MyTeamName, ServerParam::version);
}
else
sprintf(StringBuffer::cmdbuf, "(init %s (version %.2f))", MyTeamName, ServerParam::version);
Network::send_message(StringBuffer::cmdbuf,Network::sock);
}
void Actions::init_clang()
{
sprintf(StringBuffer::cmdbuf, "(clang (ver %d %d))", ClientParam::clang_min_version, ClientParam::clang_max_version);
Network::send_message(StringBuffer::cmdbuf,Network::sock);
}
/****** see if commands take effects ******************/
void Actions::ActionsInvalidation(){
turn_valid = Commanded(CMD_turn, situation.LastCycle());
dash_valid = Commanded(CMD_dash, situation.LastCycle());
turn_neck_valid = Commanded(CMD_turn_neck, situation.LastCycle());
kick_valid = Commanded(CMD_kick, situation.LastCycle());
say_valid = Commanded(CMD_say, situation.LastCycle());
if (sensory.SensedInfKnown(situation.CurrentTime)){
SenseBodyInfo& sensebody = sensory.GetSenseBodyInfo(situation.CurrentTime);
if (sensebody.turns < turns){
if (!situation.ClockStopped){
my_error("Miss %d turns",turns - sensebody.turns);
DoLog(LOG_MISS,"%d turns", turns - sensebody.turns);
}
turneffects.Data(situation.LastCycle()) = 0;
turn_valid = false;
}
if (sensebody.dashes < dashes){
if (!situation.ClockStopped){
my_error("Miss %d dashes",dashes - sensebody.dashes);
DoLog(LOG_MISS,"%d dashs", dashes - sensebody.dashes);
}
dasheffects.Data(situation.LastCycle()) = 0;
usedpower.Data(situation.LastCycle()) = 0;
dash_valid = false;
}
if (sensebody.kicks < kicks){
if (!situation.ClockStopped){
my_error("Miss %d kicks",kicks - sensebody.kicks);
DoLog(LOG_MISS,"%d kicks",kicks - sensebody.kicks);
}
kickeffects.Data(situation.LastCycle()) = 0;
usedpower.Data(situation.LastCycle()) = 0;
kick_valid = false;
}
if (sensebody.turn_necks < turn_necks){
if (!situation.ClockStopped){
my_error("Miss %d turn_necks",turn_necks - sensebody.turn_necks);
DoLog(LOG_MISS,"%d turn_necks",turn_necks - sensebody.turn_necks);
}
turn_neckeffects.Data(situation.LastCycle()) = 0;
turn_neck_valid = false;
}
if (sensebody.says < says){
say_valid = false;
my_error("Miss %d says",says - sensebody.says);
DoLog(LOG_MISS,"%d says",says - sensebody.says);
}
turns = sensebody.turns;
dashes = sensebody.dashes;
kicks = sensebody.kicks;
says = sensebody.says;
turn_necks = sensebody.turn_necks;
}
}
/*************** commands are sent by this function **************/
/*************** commands are sent by this function **************/
void Actions::send_action(){
if (!commands[latest].valid(situation.CurrentTime)){
my_error("Is this command valid? CMD_type : %d",commands[latest].type);
return;
}
switch (commands[latest].type){
case CMD_attentionto:
if (commands[latest].No <=0)
sprintf(StringBuffer::cmdbuf, "(attentionto off)");
else if (commands[latest].No <=11)
sprintf(StringBuffer::cmdbuf, "(attentionto %s %d)",MyTeamName,commands[latest].No);
else if (commands[latest].No <=22)
sprintf(StringBuffer::cmdbuf, "(attentionto %s %d)",TheirTeamName,commands[latest].No-11);
else
sprintf(StringBuffer::cmdbuf, "(attentionto off)");
break;
case CMD_pointto:
if (commands[latest].dist< 0)
sprintf(StringBuffer::cmdbuf, "(pointto off)");
else
sprintf(StringBuffer::cmdbuf, "(pointto %.2f %.2f)",commands[latest].dist,commands[latest].angle);
break;
case CMD_tackle:
sprintf(StringBuffer::cmdbuf, "(tackle %.2f)",commands[latest].power);
break;
case CMD_kick:
sprintf(StringBuffer::cmdbuf,"(kick %.2f %.2f)", commands[latest].power, commands[latest].angle);
kicks++;
kickeffects.Setdata(Polar2Vector(commands[latest].power * Kickrate(), NormalizeAngle(commands[latest].angle + Self.bodyfacing)), situation.CurrentTime);
usedpower.Setdata((float)fabs(commands[latest].power) * (commands[latest].power > 0 ? 1.0f : ClientParam::negativepower_factor), situation.CurrentTime);
LogMat(LM_Ass, "%d %d %d(%.2f,%.2f),%.2f ", Ass_ActEffect, CMD_kick, situation.CurrentTime, kickeffects.Data(situation.CurrentTime).x, kickeffects.Data(situation.CurrentTime).y);
DoLog(LOG_SENDACTION,"kickeffect (%.2f %.2f)", KickEffect(situation.CurrentTime).x, KickEffect(situation.CurrentTime).y);
break;
case CMD_dash:
sprintf(StringBuffer::cmdbuf,"(dash %.2f)", commands[latest].power);
dashes++;
dasheffects.Setdata(Polar2Vector(commands[latest].power * Dashrate(), Self.bodyfacing), situation.CurrentTime);
usedpower.Setdata((float)fabs(commands[latest].power) * (commands[latest].power > 0 ? 1.0f : ClientParam::negativepower_factor), situation.CurrentTime);
LogMat(LM_Ass, "%d %d %d(%.2f,%.2f),%.2f ", Ass_ActEffect, CMD_dash, situation.CurrentTime, dasheffects.Data(situation.CurrentTime).x, dasheffects.Data(situation.CurrentTime).y);
DoLog(LOG_SENDACTION, "dasheffect (%.2f %.2f)", DashEffect(situation.CurrentTime).x, DashEffect(situation.CurrentTime).y);
break;
case CMD_turn:
sprintf(StringBuffer::cmdbuf,"(turn %.2f)", commands[latest].angle);
turns++;
turneffects.Setdata(commands[latest].angle * Turnrate(), situation.CurrentTime);
LogMat(LM_Ass, "%d %d %d %.2f ", Ass_ActEffect, CMD_turn, situation.CurrentTime, turneffects.Data(situation.CurrentTime));
DoLog(LOG_SENDACTION, "turneffect %.2f", TurnEffect(situation.CurrentTime));
break;
case CMD_turn_neck:
sprintf(StringBuffer::cmdbuf,"(turn_neck %.2f)", commands[latest].angle);
turn_necks++;
turn_neckeffects.Setdata(commands[latest].angle, situation.CurrentTime);
LogMat(LM_Ass, "%d %d %d %.2f ", Ass_ActEffect, CMD_turn_neck, situation.CurrentTime, turn_neckeffects.Data(situation.CurrentTime));
DoLog(LOG_SENDACTION, "turnneckeffect %.2f", TurnNeckEffect(situation.CurrentTime));
break;
case CMD_catch:
sprintf(StringBuffer::cmdbuf,"(catch %.2f)", commands[latest].angle);
lastcatchtime = situation.CurrentTime;
DoLog(LOG_SENDACTION, "catch %.2f", commands[latest].angle);
break;
case CMD_move:
sprintf(StringBuffer::cmdbuf,"(move %.2f %.2f)",commands[latest].x, commands[latest].y);
DoLog(LOG_SENDACTION, "move");
break;
case CMD_change_view: //obsolete
sprintf(StringBuffer::cmdbuf, "(change_view %s %s)", width_string, qual_string);
DoLog(LOG_SENDACTION, "changeview");
break;
case CMD_bye:
sprintf(StringBuffer::cmdbuf, "(bye)");
DoLog(LOG_SENDACTION, "bye");
break;
case CMD_say:
DoLog(LOG_SENDACTION, "%s", StringBuffer::cmdbuf);
break;
default:
return;
}
Network::send_message(StringBuffer::cmdbuf,Network::sock);
//DoLog(LOG_SENDACTION, StringBuffer::cmdbuf);
latest = (latest + 1)% CP_num_stored_CMDs;
}
void Actions::SetKickEffect(float x, float y, float power, Time time){
kickeffects.Setdata(Vector(x, y), time);
usedpower.Setdata(power, time);
}
void Actions::SetDashEffect(float x, float y, float power, Time time){
dasheffects.Setdata(Vector(x,y), time);
}
void Actions::SetTurnEffect(float v, Time time){
turneffects.Setdata(v, time);
}
void Actions::SetTurnNeckEffect(float v, Time time){
turn_neckeffects.Setdata(v, time);
}
void Actions::turn(float Moment){
if (Moment > ServerParam::max_moment){
//my_error("Should not turn that positive much!");
Moment = ServerParam::max_moment;
}
if (Moment < ServerParam::min_moment){
//my_error("Should not turn that negative much!");
Moment = ServerParam::min_moment;
}
commands[latest].turn(Moment);
send_action();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -