📄 memplayer.c
字号:
float available_power, needed_power = *dash_power; if ( needed_power < 0 ){ needed_power *= -2; } if ( needed_power < 0 ) my_error("power should be positive now"); float new_stamina = MyStamina() - MyEffort() * needed_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"); */ available_power = MyStamina()/MyEffort(); if ( *dash_power >= 0 ) { *dash_power = available_power; } else { *dash_power = -available_power/2; } } 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 */ /* THIS SHOULDN'T HAPPEN ANYMORE */ /* 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) ){ */ /* body_ang += expected_delta; */ body_ang += LastActionAngle()/(1.0 + SP_inertia_moment * MySpeed()); NormalizeAngleDeg(&body_ang); /* } */ /* else */ /* my_error("Turns should NOT happen instantaneously anymore"); */ break; case CMD_dash: if ( my_vel_time > time ) break; vel = NewVelFromDash( vel, LastActionPower() ); break; default: ; }}/*********************************************************************************/void PlayerInfo::update_self_estimate(Time time){ update_self_neck_rel_ang(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_self_neck_rel_ang(Time time){ if ( SensedInfoKnown(time) ) SetMyNeckRelAng(GetMySensedNeckAngle(time)); else if ( SensedInfoKnown(time-1) ){ /* Bring it up to date from the action */ AngleDeg neck_ang = GetMySensedNeckAngle(time-1); if ( TurnNeck.valid(time-1) ){ neck_ang += TurnNeck.angle; if ( neck_ang < SP_min_neck_angle ) neck_ang = SP_min_neck_angle; if ( neck_ang > SP_max_neck_angle ) neck_ang = SP_max_neck_angle; } SetMyNeckRelAng(neck_ang); } else /* could write an "estimate_neck_angle" that updates from the last known time */ /* could also assume neck unchanged */ ;/*my_error("Don't know neck angle at time %d.%d or %d.%d", time.t,time.s,(time-1).t,(time-1).s);*/}/*********************************************************************************/void PlayerInfo::update_stamina(Time time){ if ( NewAction && LastActionType() == CMD_dash ) stamina -= (LastActionPower() > 0) ? LastActionPower() : (-2.0 * 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; if (stamina >= SP_effort_inc_thr * SP_stamina_max && effort < 1.0){ effort += SP_effort_inc; if ( effort > 1.0 ) effort = 1.0; } stamina += recovery * SP_stamina_inc; if ( stamina > SP_stamina_max ) stamina = SP_stamina_max; }}/*********************************************************************************/void PlayerInfo::reset_stamina(){ stamina = SP_stamina_max; effort = recovery = 1.0;}/*********************************************************************************/Time PlayerInfo::update_time(int time){ LastTime = CurrentTime; if ( ClockStopped ){ if ( CurrentTime.t != time ){ if ( CurrentTime.t == time - 1 ) /* Sometimes happens in offsides mode */ CurrentTime = Time(time,0); else my_error("server time should be the same %d %d %d",CurrentTime.t, CurrentTime.s, time); } else CurrentTime.s = StoppedClockMSec/SP_simulator_step; } else if ( LastStartClockTime.t == time ) CurrentTime = LastStartClockTime; else CurrentTime = Time(time,0); return CurrentTime;}/*********************************************************************************//*********************************************************************************//*********************************************************************************/Bool PlayerInfo::SightPredictedEarlyThisCycle(){ if ( InterruptsThisCycle > CP_interrupts_per_cycle/2 ) /* already past the beginning of the cycle */ return FALSE; /* Number of full cycles since last sight * simulator_step */ if ( MySightInterval() - ((CurrentTime-LastSightTime)-1)*SP_simulator_step <= SP_simulator_step/2 ) return TRUE; return FALSE;}/*********************************************************************************/ Bool PlayerInfo::GotSightFromCurrentPosition(){ if (FirstActionOpSinceLastSight && /* sight from this time or didn't change view angle last time step */ /* could replace valids with MyNeckGlobalAng() == my_last_neck_global_ang)) */ /* but when the angle's estimated, it might be off by up to 10 degrees */ (LastSightTime == CurrentTime || (!LastAction->valid(CurrentTime-1) && !TurnNeck.valid(CurrentTime-1)))) return TRUE; return FALSE;}/*********************************************************************************//*********************************************************************************//*********************************************************************************/AngleDeg PlayerInfo::MyViewAngle(Time time){ AngleDeg view_angle = SP_visible_angle; Vwidth width; if ( time < ViewWidthTime ) width = LastViewWidth; else width = ViewWidth; if ( width == VW_Narrow ) view_angle /= 2; else if ( width == VW_Wide ) view_angle *= 2; return view_angle/2;}/*********************************************************************************/Bool PlayerInfo::InViewAngle(Time time, AngleDeg ang, float buffer){ if ( fabs(ang) < MyViewAngle(time) - buffer ) return TRUE; return FALSE;}/*********************************************************************************/int PlayerInfo::MySightInterval(){ int interval = SP_send_step; if ( ViewWidth == VW_Narrow ) interval /= 2; else if ( ViewWidth == VW_Wide ) interval *= 2; if ( ViewQuality == VQ_Low ) interval /=2; return interval;}/*********************************************************************************/int PlayerInfo::PredictedNextSightInterval(){ int interval = MySightInterval(); if ( interval < SP_simulator_step ) /* 37 or 75 */ return 1; if ( interval == 3*SP_simulator_step ) /* 300 */ return 3; if ( interval == 1.5*SP_simulator_step ) /* 150 */ return (LastSightInterval <= 1 ? 2 : 1); my_error("Sight interval should be 37, 75, 150, or 300: %d",MySightInterval()); return 0;}/*********************************************************************************/void PlayerInfo::SetMySensedInfo(float st, float e, float sp, float ha, int k, int d, int tu, int sa, int tn, Time ti){ if ( sense_time == ti ) return; prev_sense_time = sense_time; sense_time = ti; prev_stamina = last_stamina; last_stamina = st; prev_effort = last_effort; last_effort = e; prev_speed = last_speed; last_speed = sp; prev_neck_rel_ang = last_neck_rel_ang; last_neck_rel_ang = ha;// neck_rel_ang = ha; /** Want to do this here??? No! **/ prev_kicks = last_kicks; last_kicks = k; if ( last_kicks != kicks ){ if (!ClockStopped) my_error("Server missed a kick at time %d (%d %d)",prev_sense_time.t,last_kicks,kicks); LastAction->type = CMD_none; kicks = last_kicks; Mem->GetBall()->forget_past_kick(LastAction->time); /* RequestResend = TRUE; ResendType = CMD_kick; ResendTime = LastActionTime(); */ } prev_dashes = last_dashes; last_dashes = d; if ( last_dashes != dashes ){ if (!ClockStopped) my_error("Server missed a dash at time %d (%d %d)",prev_sense_time.t,last_dashes,dashes); LastAction->type = CMD_none; dashes = last_dashes; /* RequestResend = TRUE; ResendType = CMD_dash; ResendTime = LastActionTime(); */ } prev_turns = last_turns; last_turns = tu; if ( last_turns != turns ){ if (!ClockStopped) my_error("Server missed a turn at time %d (%d %d)",prev_sense_time.t,last_turns,turns); LastAction->type = CMD_none; turns = last_turns; /* RequestResend = TRUE; ResendType = CMD_turn; ResendTime = LastActionTime(); */ } prev_turn_necks = last_turn_necks; last_turn_necks = tn; if ( last_turn_necks != turn_necks ){ if (!ClockStopped) my_error("Server missed a turn_neck at time %d (%d %d)",prev_sense_time.t,last_turn_necks,turn_necks); TurnNeck.type = CMD_none; turn_necks = last_turn_necks; /* RequestResend = TRUE; ResendType = CMD_turn; ResendTime = LastActionTime(); */ } prev_says = last_says; last_says = sa; if ( last_says != says ){ says = last_says; }}/*********************************************************************************/float PlayerInfo::GetMySensedSpeed(Time time){ if (time == sense_time) return last_speed; if (time == prev_sense_time) return prev_speed; my_error("Don't know my speed at time %d",time.t); return 0;}/*********************************************************************************/float PlayerInfo::GetMySensedStamina(Time time){ if (time == sense_time) return last_stamina; if (time == prev_sense_time) return prev_stamina; my_error("Don't know my stamina at time %d",time.t); return 0;}/*********************************************************************************/float PlayerInfo::GetMySensedEffort(Time time){ if (time == sense_time) return last_effort; if (time == prev_sense_time) return prev_effort; my_error("Don't know my effort at time %d",time.t); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -