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

📄 memplayer.c

📁 RoboCup 2D 仿真组冠军源代码之1998年冠军队——CMUnited98源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- Mode: C -*- *//* MemPlayer.C * CMUnited98 (soccer client for Robocup98) * Peter Stone <pstone@cs.cmu.edu> * Computer Science Department * Carnegie Mellon University * Copyright (C) 1998 Peter Stone * * CMUnited-98 was created by Peter Stone, Manuela Veloso, and Patrick Riley * * You may copy and distribute this program freely as long as you retain this notice. * If you make any changes or have any comments we would appreciate a message. *//* MemPlayer.C stores all the information relating to the client itself: * position on the field, speed, stamina, etc. */#include "types.h"#include "MemPlayer.h"#include "client.h"/*********************************************************************************//*********************************************************************************//*********************************************************************************/PlayerInfo::PlayerInfo(){  Initialized = FALSE;  ServerAlive = FALSE;  ViewQuality = VQ_High;  ViewWidth   = VW_Normal;  NewSight = FALSE;  FirstActionOpSinceLastSight = FALSE;  ClockStopped = FALSE;  StoppedClockMSec = 0;  LastStartClockTime = Time(-1,0); /* If a problem, change back to 0,0 */  SecondLastStartClockTime = LastStartClockTime;  Action = new Command;  LastAction = new Command;  RequestResend = FALSE;  last_dashes = prev_dashes = dashes = 0;  last_turns  = prev_turns  = turns  = 0;  last_kicks  = prev_kicks  = kicks  = 0;  last_says   = prev_says   = says   = 0;  TheirTeamName[0] = '\n';}/*********************************************************************************/PlayerInfo::~PlayerInfo(){  if (CP_save_log)    fclose(SaveLogFile);  if (CP_save_sound_log)    fclose(SaveSoundLogFile);  delete Action;  delete LastAction;}/*********************************************************************************/void PlayerInfo::Initialize(){  /* printf("Calling Player Initialize\n"); */  TheirSide = ( MySide == 'l' ? 'r' : 'l' );  MyTeamNameLen = strlen(MyTeamName);  TestVersion = (VP_test || ( MySide == 'l' && VP_test_l ) || ( MySide == 'r' && VP_test_r )) ? TRUE : FALSE;  if ( TestVersion == TRUE ) printf("%d : test version\n",MyNumber);  if ( VP_train_DT == TRUE ) printf("%d : training DT\n",MyNumber);  MyScore    = IP_my_score;  TheirScore = IP_their_score;  if (CP_save_log){    sprintf(SaveLogFileName,"Logfiles/%s%d-%c.log",MyTeamName,(int)MyNumber,MySide);    SaveLogFile = fopen(SaveLogFileName,"w");    SaveLogCounter = 0;  }  if (CP_save_sound_log){    sprintf(SaveSoundLogFileName,"Logfiles/%s%d-%c-sounds.log",MyTeamName,(int)MyNumber,MySide);    SaveSoundLogFile = fopen(SaveSoundLogFileName,"w");    SaveSoundLogCounter = 0;  }  TimerInterval = SP_simulator_step/CP_senses_per_cycle;  stamina = SP_stamina_max;  effort = 1;  recovery = 1;  RecoveryDecThreshold = SP_stamina_max * SP_recover_dec_thr;  EffortDecThreshold   = SP_stamina_max * SP_effort_dec_thr;  EffortIncThreshold   = SP_stamina_max * SP_effort_inc_thr;  my_vel_time = my_pos_time = 0;  Initialized = TRUE;}/*********************************************************************************//*********************************************************************************//*********************************************************************************/void PlayerInfo::SetPlayMode(Pmode mode){  /* If clock is starting, save the old time */  if ( ClockStopped ){    ClockStopped = FALSE;    SecondLastStartClockTime = LastStartClockTime;    LastStartClockTime = LastActionOpTime;    StoppedClockMSec = 0;     sanitize_time(CurrentTime);    sanitize_time(LastSightTime);    sanitize_time(LastSoundTime);    sanitize_time(sense_time);  }  PlayMode = mode;  PlayModeTime = CurrentTime;  if ( mode == PM_Before_Kick_Off || mode == PM_My_Offside_Kick || mode == PM_Their_Offside_Kick ){    if ( StoppedClockMSec != 0 ) my_error("StoppedClockMSec should have been reset already");    ClockStopped = TRUE;  }    if (mode == PM_Half_Time || mode == PM_Extended_Time)    reset_stamina();#ifndef RELEASE_VERSION  if ( PlayMode != PM_Play_On )     EndSetPlay(); #endif/* in others: *///    if ( Mem->QActionTaken )//      Mem->CloseRewards();}/*********************************************************************************/void PlayerInfo::sanitize_time(Time &tm){  if ( !LastStartClockTime ) return;  /* This is to take care of times that were prematurely updated before we knew that     the clock was about to start again */  if ( tm.t == LastStartClockTime.t && tm.s > LastStartClockTime.s ){    tm = Time( LastStartClockTime.t + (tm.s - LastStartClockTime.s), 0 );  }}/*********************************************************************************/void PlayerInfo::EstimateMyPos(){  /* Takes me from previous time to time */  if ( MyConf() && MyVelConf() )    pos += vel;}/*********************************************************************************/void PlayerInfo::EstimateMyVel(Time time){  if ( my_vel_time == time && vel_conf == CP_max_conf )    return;    /* Takes me from previous time to time */  if ( SensedInfoKnown(time) ){    float old_speed = vel.mod();    if ( old_speed )      vel = vel * GetMySensedSpeed(time)/old_speed; /* don't change the direction */    else       vel = Polar2Vector( GetMySensedSpeed(time), MyAng() );  /* use my direction */    vel_conf = CP_max_conf;  }  else if ( vel_conf < CP_max_conf && SensedInfoKnown(time-1) ) {    vel = Polar2Vector( GetMySensedSpeed(time-1)*SP_player_decay, MyAng() );    vel_conf = CP_conf_decay;  }  else if ( !MyVelConf() )    return;  else if ( my_vel_time == time-1 ){    vel *= SP_player_decay;    vel_conf *= CP_conf_decay;  }  else if ( my_vel_time > time-10 ){  /* missed up to 10 cycles */    while ( my_vel_time < time && MyVelConf() ){      vel *= SP_player_decay;      vel_conf *= CP_conf_decay;      ++my_vel_time;    }  }  else    my_error("Having trouble in vel update -- must have missed at least 10 cycles %.1f %.1f    %f",	     (float)my_vel_time.t,(float)my_vel_time.s,MyVelConf());  my_vel_time = time;}/*********************************************************************************/Vector PlayerInfo::NewVelFromDash(Vector old_vel, float dash_power){   float effective_power = MyEffort() * dash_power;   effective_power *= SP_dash_power_rate;   Vector new_vel = old_vel +  Polar2Vector( effective_power, MyAng() );   if ( new_vel.mod() > SP_player_speed_max )     new_vel *= ( SP_player_speed_max/new_vel.mod() );   return new_vel;}/*********************************************************************************/void PlayerInfo::VerifyDash(float *dash_power){  /* Check if recovery going down, or max_speed exceeded */  float new_stamina = MyStamina() -  MyEffort() * (fabs(*dash_power));  if ( new_stamina <= SP_recover_dec_thr * SP_stamina_max && recovery > SP_recover_min ){    /* printf("%d:%d.%d ",MyNumber,CurrentTime.t,CurrentTime.s); */    /* printf("WARNING: recovery about to go to %.3f\n",recovery - SP_recover_dec); */    ;  }  if ( new_stamina <= SP_effort_dec_thr * SP_stamina_max && effort > SP_effort_min ){    /* printf("WARNING: effort about to go to %.2f\n",MyEffort() - SP_effort_dec); */  }  if ( new_stamina < 0 ){    /* printf("%d:%d.%d ",MyNumber,CurrentTime.t,CurrentTime.s); */    /* printf("WARNING: not enough stamina for dash\n"); */    *dash_power = signf(*dash_power)*MyStamina()/MyEffort();  }  if ( NewVelFromDash( MyVel(), *dash_power ).mod() > SP_player_speed_max ){    /* printf("%d:%d.%d ",MyNumber,CurrentTime.t,CurrentTime.s); */    /* printf("WARNING: can't move that fast (assuming vel and dash in same dir)\n"); */    /* printf("my speed %f   dash_power %f   ",MySpeed(),*dash_power); */    *dash_power = signf(*dash_power)*(SP_player_speed_max - MySpeed())/(MyEffort()*SP_dash_power_rate);    /* printf("new dash_power %f\n",*dash_power); */  }}/*********************************************************************************/void PlayerInfo::UpdateFromMyAction(Time time){  /* Assume vel and pos are correct for time -- going to time+1 */  if ( !MyConf() ) my_error("Can't update from action if not localized");   /* But I'm pretty good at estimating... up conf_decay?? */  if ( !NewAction || !(LastActionValid(time)) ) my_error("No action at that time");  AngleDeg delta_ang, expected_delta;  switch(LastActionType()){  case CMD_turn:    if ( my_pos_time > time ) break;    /* be careful not to estimate in a turn that's already been seen --        server updates turns instantaneously */    delta_ang = GetNormalizeAngleDeg(ang - my_last_ang);    expected_delta = LastActionAngle()/(1.0 + SP_inertia_moment * MySpeed());    /* only if the change is closer to 0 than to the expected change */    if ( fabs(delta_ang) < fabs(delta_ang-expected_delta) ){      ang += expected_delta;      NormalizeAngleDeg(&ang);    }    break;  case CMD_dash:    if ( my_vel_time > time ) break;    vel = NewVelFromDash( vel, LastActionPower() );    break;  default: ;  }}/*********************************************************************************/void PlayerInfo::update_self_estimate(Time time){  if ( !MyConf() ){    vel_conf = 0;   /* If don't know my position, can't know my velocity */    return;  }  if (CP_use_new_position_based_vel) {    if ( my_pos_time == time ){ /* just vel */      if ( my_vel_time == time )	return;      if ( NewAction && LastActionValid(my_vel_time) )	UpdateFromMyAction(my_vel_time);            EstimateMyVel(time);    }  } else {    if ( my_pos_time == time ){ /* just vel */      if ( my_vel_time == time ) my_error("my pos and vel already updated\n");      if ( NewAction && LastActionValid(my_vel_time) )	UpdateFromMyAction(my_vel_time);            EstimateMyVel(time);    }  }      while ( my_pos_time < time ){    if ( NewAction && LastActionValid(my_pos_time) )      UpdateFromMyAction(my_pos_time);    ++my_pos_time;    EstimateMyPos();    EstimateMyVel(time);    conf *= CP_conf_decay;  }}/*********************************************************************************/void PlayerInfo::update_stamina(Time time){  if ( NewAction && LastActionType() == CMD_dash )    stamina -= effort * fabs(LastActionPower());  if ( stamina < 0 ) stamina = 0;  if ( stamina <= SP_recover_dec_thr * SP_stamina_max && recovery > SP_recover_min ) {    recovery -= SP_recover_dec;  }  if ( SensedInfoKnown(time) ){    stamina = GetMySensedStamina(time);    effort  = GetMySensedEffort(time);  }  else {    if ( stamina <= SP_effort_dec_thr * SP_stamina_max && effort > SP_effort_min )      effort -= SP_effort_dec;

⌨️ 快捷键说明

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