You a expert about cpp, you're using right now, c++ 20, vscode and windows
You task is to responds of any question about someone, the task is very help the users where the error is, and doing after the code to fix that error
You need these context to make the task :
deltaTime = tickSkip * (1 / 120.f);
ballState = arena->ball->GetState();
ball = PhysObj(ballState);
ballInv = ball.Invert();
players.resize(arena->_cars.size());
auto carItr = arena->_cars.begin();
for (int i = 0; i < players.size(); i++) {
auto& player = players[i];
player.UpdateFromCar(*carItr, arena->tickCount, tickSkip);
if (player.ballTouchedStep)
lastTouchCarID = player.carId;
carItr++;
}
if (!boostPadIndexMapBuilt) {
boostPadIndexMapMutex.lock();
// Check again? This seems stupid but also makes sense to me
// Without this, we could lock as the index map is building, then go build again
// I would like to keep the mutex inside the if statement so it is only checked a few times
if (!boostPadIndexMapBuilt)
_BuildBoostPadIndexMap(arena);
boostPadIndexMapMutex.unlock();
}
for (int i = 0; i < CommonValues::BOOST_LOCATIONS_AMOUNT; i++) {
int idx = boostPadIndexMap[i];
int invIdx = boostPadIndexMap[CommonValues::BOOST_LOCATIONS_AMOUNT - i - 1];
auto state = arena->_boostPads[idx]->GetState();
auto stateInv = arena->_boostPads[invIdx]->GetState();
boostPads[i] = state.isActive;
boostPadsInv[i] = stateInv.isActive;
boostPadTimers[i] = state.cooldown;
boostPadTimersInv[i] = stateInv.cooldown;
}
// Update goal scoring
// If you don't have a GoalScoreCondition then that's not my problem lmao
if (Math::IsBallScored(ball.pos))
scoreLine[1 - (int)RS_TEAM_FROM_Y(ball.pos.y)]++;
lastTickCount = arena->tickCount;
PhysObj phys, physInv;
CarState carState;
// matchAssists: being the passer to a teammate who shot and scored
// matchBumps: any bump against an opponent, including demos
int
matchGoals = 0,
matchSaves = 0,
matchAssists = 0,
matchShots = 0,
matchShotPasses = 0,
matchBumps = 0,
matchDemos = 0,
boostPickups = 0;
bool hasJump;
bool hasFlip;
float boostFraction; // From 0 to 1
bool ballTouchedStep; // True if the player touched the ball during any of tick of the step
bool ballTouchedTick; // True if the player is touching the ball on the final tick of the step
void UpdateFromCar(Car* car, uint64_t tickCount, int tickSkip);
const PhysObj& GetPhys(bool inverted) const {
return inverted ? physInv : phys;
}
};
bool inv = player.team == Team::ORANGE;
auto& ball = state.GetBallPhys(inv);
auto& pads = state.GetBoostPads(inv);
result += ball.pos * posCoef;
result += ball.vel * velCoef;
result += ball.angVel * angVelCoef;
for (int i = 0; i < prevAction.ELEM_AMOUNT; i++)
result += prevAction[i];
for (int i = 0; i < CommonValues::BOOST_LOCATIONS_AMOUNT; i++)
result += (float)pads[i];
FList selfOBS = {};
AddPlayerToOBS(selfOBS, player, inv);
result += selfOBS;
int playerObsSize = selfOBS.size();
FList2 teammates = {}, opponents = {};
for (auto& otherPlayer : state.players) {
if (otherPlayer.carId == player.carId)
continue;
FList playerObs = {};
AddPlayerToOBS(
playerObs,
otherPlayer,
inv
);
((otherPlayer.team == player.team) ? teammates : opponents).push_back(playerObs);
}
if (teammates.size() > maxPlayers - 1)
RG_ERR_CLOSE("DefaultOBSPadded: Too many teammates for OBS, maximum is " << (maxPlayers - 1));
if (opponents.size() > maxPlayers)
RG_ERR_CLOSE("DefaultOBSPadded: Too many opponents for OBS, maximum is " << maxPlayers);
for (int i = 0; i < 2; i++) {
auto& playerList = i ? teammates : opponents;
int targetCount = i ? maxPlayers - 1 : maxPlayers;
while (playerList.size() < targetCount) {
FList pad = FList(playerObsSize);
playerList.push_back(pad);
}
}
// Shuffle both lists
std::shuffle(teammates.begin(), teammates.end(), ::Math::GetRandEngine());
std::shuffle(opponents.begin(), opponents.end(), ::Math::GetRandEngine());
for (auto& teammate : teammates)
result += teammate;
for (auto& opponent : opponents)
result += opponent;
return result;
}
#pragma once
#include "DefaultOBS.h"
namespace RLGSC {
// Verion of DefaultOBS that supports a varying number of players (i.e. 1v1, 2v2, 3v3, etc.)
// Maximum player count can be however high you want
// Opponent and teammate slots are randomly shuffled to prevent slot bias
class DefaultOBSPadded : public DefaultOBS {
public:
// reward the bot for being on the wall
class PlayerOnWallReward : public RewardFunction {
public:
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
return player.carState.isOnGround && player.phys.pos.z > 300;
};
};
// reward the player for having speed towards the ball mid-air
class AerialReward : public RewardFunction {
public:
int BALL_MIN_HEIGHT = 300;
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) override {
};
// reward the player for being the closest to the ball and their team having touched the ball last
// ambiguous in 1v1.
class PossessionReward : public RewardFunction {
public:
float prevTeamTouch = -1.f;
float stacking = 0;
PossessionReward(float minDist = 200.f) : minDist(minDist) {};
virtual void Reset(const GameState& state) {
prevTeamTouch = -1;
stacking = 0;
};
virtual void PreStep(const GameState& state) {
for (auto& player : state.players) {
if (player.ballTouchedStep && !((int)player.team == prevTeamTouch)) {
prevTeamTouch = (int)player.team;
stacking = 0;
}
}
}
};
// reward the player for being in the air (opposite of lavafloor)
class InAirReward : public RewardFunction {
public:
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
if (!player.carState.isOnGround) {
return 1;
}
else {
return 0;
}
};
};
// reward the player for dribbling and matching the speed of the ball.
class DribbleReward : public RewardFunction {
public:
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
const float MIN_BALL_HEIGHT = 109.0f;
const float MAX_BALL_HEIGHT = 180.0f;
const float MAX_DISTANCE = 197.0f;
const float SPEED_MATCH_FACTOR = 2.0f;
// reward the player for high goal speed, (warning very explosive as it's a high return reward, LOW WEIGHT.)
class GoalSpeedReward : public RewardFunction {
public:
GoalSpeedReward(float rewardFactor = 0.1f) : rewardFactor(rewardFactor) {};
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
float reward = 0.0f;
// reward the player for moving fast at the ball
class SpeedTowardBallReward : public RewardFunction {
public:
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
return RS_MAX(0, player.phys.vel.Dot((state.ball.pos - player.phys.pos).Normalized()) / CommonValues::CAR_MAX_SPEED);
};
};
// reward the player for touching the ball mid air
class JumpTouchReward : public RewardFunction {
public:
JumpTouchReward(float minHeight = 200.0f, float exp = 1.0f)
: minHeight(minHeight), exp(exp), div(std::pow(CommonValues::CEILING_Z / 2 - CommonValues::BALL_RADIUS, exp)) {}
// reward the player for moving fast toward the ball, but only on kickoff state.
class SpeedflipKickoffReward : public RewardFunction {
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
if (state.ball.vel.IsZero() and player.boostFraction < 0.02) {
return RS_MAX(0, player.phys.vel.Dot((state.ball.pos - player.phys.pos).Normalized()) / CommonValues::CAR_MAX_SPEED);
};
return 0;
};
};
// reward the player for going fast
class LightingMcQueenReward : public RewardFunction {
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
return sqrt(RS_CLAMP(player.phys.vel.Length() / 1800, 0, 1));
}
};
// Aerial Reward
class TouchAerial : public RewardFunction {
public:
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
constexpr float
MAX_HEIGHT = 1400, // most of the way to the ceiling
MAX_AIR_TIME = 1.0f; // in seconds
float reward = RS_MIN(state.ball.pos.z / MAX_HEIGHT, player.carState.airTime / MAX_AIR_TIME);
float result = player.ballTouchedStep ? RS_MIN(reward, 1.0f) : 0;
return result;
};
};
// punish the bot for being on the ground
class LavaFloorReward : public RewardFunction {
public:
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) {
return -int(player.carState.isOnGround);
};
};
Certainly! Let's enhance your BallCatchSoccar reward function by adding conditions to ensure that both the ball and the player are above specific heights before calculating the reward. Specifically:
Ball's Height: Must be greater than 512 units.
Player's Height: Must be greater than 256 units.
If either of these conditions is not met, the function will return 0.0, indicating no reward.
Updated BallCatchSoccar Class
Here's the modified version of your BallCatchSoccar class with the added height conditions:
cpp
class BallCatchSoccar : public RewardFunction {
public:
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) override {
// Define the minimum required heights
constexpr float MIN_BALL_HEIGHT = 512.0f;
constexpr float MIN_PLAYER_HEIGHT = 256.0f;
// Check if both the ball and the player meet the height requirements
if (state.ball.pos.z <= MIN_BALL_HEIGHT || player.phys.pos.z <= MIN_PLAYER_HEIGHT) {
return 0.0f; // No reward if conditions are not met
}
// Calculate the direction vector from the player to the ball
Vec dirToBall = (state.ball.pos - player.phys.pos).Normalized();
// Calculate the velocity component towards the ball
float velToBall = player.phys.vel.Dot(dirToBall);
// Define the maximum distance for normalization
constexpr float MAX_DIST = 3000.0f;
// Calculate the distance fraction
float distFrac = player.phys.pos.Dist(state.ball.pos) / MAX_DIST;
// Compute the raw reward value
float rawReward = (velToBall / 2300.0f) * (1.0f - distFrac);
// Clamp the reward between 0.0 and 1.0 to ensure it stays within desired bounds
return RS_CLAMP(rawReward, 0.0f, 1.0f);
}
};
These constants define the minimum z-values required for the ball and the player.
Height Condition Check:
cpp
if (state.ball.pos.z <= MIN_BALL_HEIGHT || player.phys.pos.z <= MIN_PLAYER_HEIGHT) {
return 0.0f; // No reward if conditions are not met
}
Before performing any reward calculations, the function checks if either the ball's or the player's height is below the specified thresholds.
If either condition fails, the function immediately returns 0.0, indicating no reward.
Existing Reward Logic:
The rest of the function remains unchanged, ensuring that rewards are only computed when both the ball and the player are sufficiently elevated.
Additional Recommendations
Use of override:
It's good practice to use the override keyword to ensure that you're correctly overriding a virtual function from the base class.
This helps catch errors at compile time if the function signature doesn't match any base class methods.
Code Readability:
Introducing constants for minimum heights improves readability and makes it easier to adjust these values in the future.
Safety Checks:
Ensure that the Dist and Dot functions handle edge cases appropriately to prevent unexpected behavior or crashes.
Final Thoughts
With these modifications, your BallCatchSoccar reward function now accurately rewards the player only when both the ball and the player are above the specified heights. This ensures that rewards are contextually relevant and can help in training more effective behaviors in scenarios where elevation is a critical factor.
If you encounter any further issues or need additional enhancements, feel free to ask!