Skip to content

Commit 6eb1692

Browse files
committed
Backport improvements spawned from the first round of code review of kimden#45
1 parent ec5dafd commit 6eb1692

18 files changed

+580
-522
lines changed

src/items/flyable.cpp

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -557,19 +557,10 @@ bool Flyable::hit(Kart *kart_hit, PhysicalObject* object)
557557
if (isOwnerImmunity(kart_hit))
558558
return false;
559559

560-
m_has_hit_something=true;
560+
m_has_hit_something=true;
561561
if (kart_hit == NULL)
562562
return true;
563563

564-
ItemPolicy *policy = RaceManager::get()->getItemPolicy();
565-
int leader_section_idx = policy->m_leader_section;
566-
// If leader is not in a valid section, allow the hit
567-
if (leader_section_idx <= -1)
568-
return true;
569-
// If blue flags are not enabled, ALSO allow the hit
570-
if (!(policy->m_policy_sections[leader_section_idx].m_rules & ItemPolicyRules::IPT_BLUE_FLAGS))
571-
return true;
572-
573564
// Now, if blue flags are enabled and the leader is in a valid section, that's when we start to question if the hit could be forbidden (a lapper illegally hitting a lapping or vice versa).
574565
LinearWorld *lin_world = dynamic_cast<LinearWorld*>(World::getWorld());
575566
float track_length = Track::getCurrentTrack()->getTrackLength();
@@ -578,44 +569,11 @@ bool Flyable::hit(Kart *kart_hit, PhysicalObject* object)
578569

579570
int sender_lap = lin_world->getFinishedLapsOfKart(m_owner->getWorldKartId());
580571
int recv_lap = lin_world->getFinishedLapsOfKart(kart_hit->getWorldKartId());
581-
582-
float minimum_distance_empirical = 200.0f;
583-
584-
float distance_normal = std::fabs(sender_distance - recv_distance);
585-
float distance_complimentary = track_length - distance_normal;
586-
587-
bool across_finish_line;
588-
bool forwards_throw;
589-
if (distance_complimentary < distance_normal) {
590-
across_finish_line = true;
591-
if (sender_distance > recv_distance)
592-
forwards_throw = true;
593-
else
594-
forwards_throw = false;
595-
} else {
596-
across_finish_line = false;
597-
}
598-
599-
// if the distance is less than 5% from half the track length,
600-
// it is nonsense to try to predict if the hit is across the finish line
601-
if (distance_normal/track_length > 0.45 && distance_normal/track_length < 0.55)
602-
across_finish_line = false;
603-
604-
// for too short tracks we instead take 1/5th of the track
605-
if (track_length < 750.0f)
606-
minimum_distance_empirical = track_length/5.0f;
607-
608-
bool hit_is_valid;
609-
// sender with a 1 lap difference whose distance is less than an empirical number are almost certainly hitting each other across the start/finish line
610-
if (across_finish_line && forwards_throw) {
611-
hit_is_valid = (recv_lap - sender_lap) == 1;
612-
} else if (across_finish_line && !forwards_throw) {
613-
hit_is_valid = (sender_lap - recv_lap) == 1;
614-
} else {
615-
hit_is_valid = sender_lap == recv_lap;
616-
}
617-
618-
return hit_is_valid;
572+
573+
// Blue flag settings could make the hit invalid, if it's between a lapper and a lapped or vice versa. Let Itempolicy decide this
574+
575+
ItemPolicy *policy = RaceManager::get()->getItemPolicy();
576+
return policy->isHitValid(sender_distance, sender_lap, recv_distance, recv_lap, track_length);
619577
} // hit
620578

621579
// ----------------------------------------------------------------------------

src/items/item.cpp

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void ItemState::initItem(ItemType type, const Vec3& xyz, const Vec3& normal, int
125125
setDisappearCounter();
126126
} // initItem
127127

128-
static int getRespawnTime(ItemState::ItemType type) {
128+
static int getRespawnTicks(ItemState::ItemType type) {
129129
auto& stk_config = STKConfig::get();
130130
switch (type)
131131
{
@@ -169,73 +169,7 @@ void ItemState::update(int ticks)
169169
} // if collected
170170

171171
ItemPolicy *policy = RaceManager::get()->getItemPolicy();
172-
if (policy->m_policy_sections.size() == 0)
173-
return;
174-
175-
int current_section = policy->m_leader_section;
176-
177-
if (current_section <= -1)
178-
current_section = 0;
179-
180-
uint16_t rules_curr = policy->m_policy_sections[current_section].m_rules;
181-
uint16_t rules_prev;
182-
if (current_section > 0)
183-
rules_prev = policy->m_policy_sections[current_section-1].m_rules;
184-
else
185-
rules_prev = rules_curr;
186-
187-
bool was_gum = (m_original_type==ITEM_BUBBLEGUM) || (m_type==ITEM_BUBBLEGUM_NOLOK) ||
188-
(m_original_type==ITEM_BUBBLEGUM_SMALL) || (m_type==ITEM_BUBBLEGUM_SMALL_NOLOK);
189-
190-
bool is_nitro = (m_type==ITEM_NITRO_SMALL) || (m_type==ITEM_NITRO_BIG);
191-
bool was_nitro = (m_original_type==ITEM_NITRO_SMALL) || (m_original_type==ITEM_NITRO_BIG);
192-
193-
bool forbid_prev = ((rules_prev & ItemPolicyRules::IPT_FORBID_BONUSBOX) && m_type==ITEM_BONUS_BOX) ||
194-
((rules_prev & ItemPolicyRules::IPT_FORBID_BANANA) && m_type==ITEM_BANANA) ||
195-
((rules_prev & ItemPolicyRules::IPT_FORBID_NITRO) && (is_nitro || was_nitro));
196-
197-
bool forbid_curr = ((rules_curr & ItemPolicyRules::IPT_FORBID_BONUSBOX) && m_type==ITEM_BONUS_BOX) ||
198-
((rules_curr & ItemPolicyRules::IPT_FORBID_BANANA) && m_type==ITEM_BANANA) ||
199-
((rules_curr & ItemPolicyRules::IPT_FORBID_NITRO) && (is_nitro || was_nitro));
200-
201-
int fuel_mode = std::get<0>(RaceManager::get()->getFuelAndQueueInfo());
202-
if (fuel_mode == 0 && m_type == ITEM_TYRE_CHANGE && m_compound == 123) // If we're a fuel tyre changer and fuel is off, don't spawn
203-
forbid_curr = true;
204-
205-
206-
// Gums that were switched into nitro are NEVER forbidden
207-
bool instant = false;
208-
if (was_gum && is_nitro) {
209-
instant = true;
210-
} else {
211-
instant = false;
212-
}
213-
214-
auto& stk_config = STKConfig::get();
215-
// There's redundant cases here, but it is like this for maintainability
216-
if (forbid_prev && forbid_curr)
217-
m_ticks_till_return = stk_config->time2Ticks(99999);
218-
else if (!forbid_prev && forbid_curr)
219-
m_ticks_till_return = stk_config->time2Ticks(99999);
220-
else if (forbid_prev && !forbid_curr) {
221-
int respawn_ticks = getRespawnTime(m_type);
222-
// If the ticks till return are abnormally high, set them back to normal.
223-
// If we don't do it like this, it will set the ticks till return perpetually
224-
// when transitioning from a section without to a section with this item type allowed.
225-
if (m_ticks_till_return > 10*respawn_ticks)
226-
m_ticks_till_return = respawn_ticks;
227-
}
228-
else if (!forbid_prev && !forbid_curr) {
229-
// Nothing to do
230-
// This wouldn't be needed normally, but we do it in case of switched items
231-
int respawn_ticks = getRespawnTime(m_type);
232-
if (m_ticks_till_return > 10*respawn_ticks && m_type != ITEM_EASTER_EGG)
233-
m_ticks_till_return = respawn_ticks;
234-
}
235-
if (instant) {
236-
m_ticks_till_return = 0;
237-
}
238-
172+
m_ticks_till_return = policy->computeItemTicksTillReturn(m_original_type, m_type, getRespawnTicks(m_type), m_ticks_till_return);
239173
} // update
240174

241175
// ----------------------------------------------------------------------------
@@ -263,7 +197,7 @@ void ItemState::collected(const Kart *kart)
263197
}
264198
else
265199
{
266-
m_ticks_till_return = getRespawnTime(m_type);
200+
m_ticks_till_return = getRespawnTicks(m_type);
267201
}
268202

269203
if (RaceManager::get()->isBattleMode())

src/items/item.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Defines the various collectibles and weapons of STK.
2525
*/
2626

27-
#include "karts/kart.hpp"
27+
//#include "karts/kart.hpp"
2828
#include "utils/cpp2011.hpp"
2929
#include "utils/leak_check.hpp"
3030
#include "utils/log.hpp"

src/items/powerup_manager.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,42 +95,37 @@ void PowerupManager::unloadPowerups()
9595
}
9696
} // removeTextures
9797

98+
// Must match the order of PowerupType in powerup_manager.hpp!!
99+
static const std::string powerup_names[] = {
100+
"nothing", /* Nothing */
101+
"bubblegum", "cake", "bowling", "zipper", "plunger", "switch",
102+
"swatter", "rubber-ball", "parachute", "nitro-hack", "electro-shield",
103+
"mini-wish", "anchor"
104+
};
105+
98106
//-----------------------------------------------------------------------------
99107
/** Determines the powerup type for a given name.
100108
* \param name Name of the powerup to look up.
101109
* \return The type, or POWERUP_NOTHING if the name is not found
102110
*/
103-
PowerupManager::PowerupType
104-
PowerupManager::getPowerupType(const std::string &name) {
105-
// Must match the order of PowerupType in powerup_manager.hpp!!
106-
static const std::string powerup_names[] = {
107-
"nothing", /* Nothing */
108-
"bubblegum", "cake", "bowling", "zipper", "plunger", "switch",
109-
"swatter", "rubber-ball", "parachute", "nitro-hack", "electro-shield",
110-
"mini-wish", "anchor"
111-
};
112-
111+
PowerupManager::PowerupType PowerupManager::getPowerupType(const std::string &name)
112+
{
113113
for(unsigned int i=POWERUP_FIRST; i<=POWERUP_LAST; i++)
114114
{
115-
if(name=="") return POWERUP_NOTHING;
116-
if(powerup_names[i]==name) return(PowerupType)i;
115+
if (name=="")
116+
return POWERUP_NOTHING;
117+
if (powerup_names[i]==name)
118+
return(PowerupType)i;
117119
}
118120
return POWERUP_NOTHING;
119121
} // getPowerupType
120122

121-
std::string
122-
PowerupManager::getPowerupAsString(PowerupManager::PowerupType type) {
123-
// Must match the order of PowerupType in powerup_manager.hpp!!
124-
static const std::string powerup_names[] = {
125-
"nothing", /* Nothing */
126-
"bubblegum", "cake", "bowling", "zipper", "plunger", "switch",
127-
"swatter", "rubber-ball", "parachute", "nitro-hack", "electro-shield",
128-
"mini-wish", "anchor"
129-
};
123+
std::string PowerupManager::getPowerupAsString(PowerupManager::PowerupType type)
124+
{
130125
int size = sizeof(powerup_names)/sizeof(*powerup_names);
131126
if (type == POWERUP_NOTHING) return "nothing";
132127
if (size < type-POWERUP_FIRST) return "nothing";
133-
return powerup_names[type-POWERUP_FIRST+1];
128+
return powerup_names[type - POWERUP_FIRST + 1];
134129
} // getPowerupAsString
135130

136131

src/karts/kart.cpp

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ void Kart::reset()
528528

529529
unsetSquash();
530530

531-
item_amount_last_lap = 0;
532-
item_type_last_lap = PowerupManager::POWERUP_NOTHING;
531+
m_item_amount_last_lap = 0;
532+
m_item_type_last_lap = PowerupManager::POWERUP_NOTHING;
533533

534534
m_last_used_powerup = PowerupManager::POWERUP_NOTHING;
535535
m_race_position = m_initial_position;
@@ -678,9 +678,9 @@ void Kart::instantSpeedIncrease(unsigned int category, float add_max_speed,
678678

679679
// -----------------------------------------------------------------------------
680680
void Kart::setSlowdown(unsigned int category, float max_speed_fraction,
681-
int fade_in_time)
681+
int fade_in_time, int duration)
682682
{
683-
m_max_speed->setSlowdown(category, max_speed_fraction, fade_in_time);
683+
m_max_speed->setSlowdown(category, max_speed_fraction, fade_in_time, duration);
684684
} // setSlowdown
685685

686686
// -----------------------------------------------------------------------------
@@ -3203,36 +3203,10 @@ void Kart::updatePhysics(int ticks)
32033203
// Cap speed if necessary
32043204
const Material *m = getMaterial();
32053205

3206-
ItemPolicy *itempolicy = RaceManager::get()->getItemPolicy();
3207-
bool is_restart = itempolicy->m_virtualpace_code <= -3;
3208-
bool did_restart = false;
3209-
if (is_restart) {
3210-
// Reaffirm the penalty in case someone tried to be funny and pit for tyres in the middle of a safety car restart
3211-
m_max_speed->setSlowdown(MaxSpeed::MS_DECREASE_STOP, 0.1f, stk_config->time2Ticks(0.1f), -1);
3212-
int restart_time = -(itempolicy->m_virtualpace_code + 3);
3213-
float gap = itempolicy->m_policy_sections[itempolicy->m_leader_section].m_virtualpace_gaps;
3214-
gap *= getPosition();
3215-
restart_time += gap;
3216-
int current_time = World::getWorld()->getTime();
3217-
if (current_time > restart_time) {
3218-
// Set slowdown time to 0 (disable it) if its time to restart
3219-
m_max_speed->setSlowdown(MaxSpeed::MS_DECREASE_STOP, 0.1f, stk_config->time2Ticks(0.1f), stk_config->time2Ticks(0));
3220-
did_restart = true;
3221-
}
3222-
}
3223-
3224-
bool is_last = getPosition() == RaceManager::get()->getNumberOfKarts();
32253206

3226-
if (is_last && did_restart) {
3227-
itempolicy->m_virtualpace_code = -1;
3228-
itempolicy->m_restart_count = -1;
3229-
}
3230-
3231-
// the only reason such a ridiculous infinite pit penalty (-1) can be given is if it's a virtual pace car restart
3232-
// plainly, the only reason this exists is because first place won't get its penalty overturned if for some reason
3233-
if (itempolicy->m_virtualpace_code == -1 && m_max_speed->getSpeedDecreaseTicksLeft(MaxSpeed::MS_DECREASE_STOP) == -1) {
3234-
m_max_speed->setSlowdown(MaxSpeed::MS_DECREASE_STOP, 0.1f, stk_config->time2Ticks(0.1f), stk_config->time2Ticks(0));
3235-
}
3207+
ItemPolicy *item_policy = RaceManager::get()->getItemPolicy();
3208+
item_policy->enforceVirtualPaceCarRulesForKart(this);
3209+
32363210

32373211
float min_speed = m && m->isZipper() ? m->getZipperMinSpeed() : -1.0f;
32383212
m_max_speed->setMinSpeed(min_speed);

src/karts/kart.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ class Kart : public Moveable
422422
void setStints (std::vector<std::tuple<unsigned, unsigned>> x) { m_stints = x; }
423423

424424
// amount in previous lap
425-
int item_amount_last_lap;
426-
PowerupManager::PowerupType item_type_last_lap;
425+
int m_item_amount_last_lap;
426+
PowerupManager::PowerupType m_item_type_last_lap;
427427

428428
bool m_is_refueling;
429429
float m_target_refuel;
@@ -633,7 +633,7 @@ class Kart : public Moveable
633633
* \param max_speed_fraction Fraction of top speed to allow only.
634634
* \param fade_in_time How long till maximum speed is capped. */
635635
virtual void setSlowdown(unsigned int category, float max_speed_fraction,
636-
int fade_in_time);
636+
int fade_in_time, int duration = -1);
637637
// ----------------------------------------------------------------------------------------
638638
/** Returns how much increased speed time is left over in the given
639639
* category.

src/modes/linear_world.cpp

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#include "network/stk_host.hpp"
4444
#include "network/stk_peer.hpp"
4545
#include "race/race_manager.hpp"
46-
#include "race/itempolicy.hpp"
46+
#include "race/item_policy.hpp"
4747
#include "race/history.hpp"
4848
#include "states_screens/race_gui_base.hpp"
4949
#include "tracks/check_manager.hpp"
@@ -487,46 +487,17 @@ void LinearWorld::newLap(unsigned int kart_index)
487487
// duplicated race positions as well.
488488
updateRacePosition();
489489

490-
ItemPolicy *itempolicy = RaceManager::get()->getItemPolicy();
491-
492-
int sec = itempolicy->applyRules(kart, kart_info.m_finished_laps, World::getWorld()->getTime());
493-
if (kart->getPosition() == 1) {
494-
itempolicy->m_leader_section = sec;
495-
int start_lap = itempolicy->m_policy_sections[sec].m_section_start;
496-
int16_t rules = itempolicy->m_policy_sections[sec].m_rules;
497-
bool do_virtualpace = rules & ItemPolicyRules::IPT_VIRTUALPACE;
498-
bool do_unlapping = rules & ItemPolicyRules::IPT_UNLAPPING;
499-
if (do_virtualpace && start_lap == kart_info.m_finished_laps) {
500-
itempolicy->m_restart_count = 0;
501-
itempolicy->m_virtualpace_code = do_unlapping
502-
? start_lap // Lappings must slow down when they reach the lead lap
503-
: -2; // Lappings must slow down as soon as possible
504-
}
505-
}
506-
kart->m_tyres->m_deg_mult = itempolicy->m_policy_sections[sec].m_deg_mult;
507-
kart->m_tyres->m_c_fuel_rate = itempolicy->m_policy_sections[sec].m_fuel_mult;
508-
509-
kart->item_type_last_lap = kart->getPowerup()->getType();
510-
kart->item_amount_last_lap = kart->getPowerup()->getNum();
511-
512-
bool slowed_down = false;
513-
if (itempolicy->m_virtualpace_code == kart_info.m_finished_laps || itempolicy->m_virtualpace_code == -2) {
514-
auto& stk_config = STKConfig::get();
515-
itempolicy->m_restart_count += 1;
516-
kart->m_max_speed->setSlowdown(MaxSpeed::MS_DECREASE_STOP, 0.1f, stk_config->time2Ticks(0.1f), stk_config->time2Ticks(99999));
517-
slowed_down = true;
518-
}
490+
ItemPolicy *item_policy = RaceManager::get()->getItemPolicy();
491+
492+
int sec = item_policy->applyRules(kart, kart_info.m_finished_laps, World::getWorld()->getTime(), RaceManager::get()->getNumLaps());
493+
494+
kart->m_item_type_last_lap = kart->getPowerup()->getType();
495+
kart->m_item_amount_last_lap = kart->getPowerup()->getNum();
496+
kart->m_tyres->m_deg_mult = item_policy->m_policy_sections[sec].m_deg_mult;
497+
kart->m_tyres->m_c_fuel_rate = item_policy->m_policy_sections[sec].m_fuel_mult;
519498

520-
bool is_last = itempolicy->m_restart_count == RaceManager::get()->getNumberOfKarts();
521-
// Technically there could be ghosts but you can't get into that situation. Probably.
522-
// If the kart is last, also fire the virtual pace car restart procedure
523-
if (slowed_down && is_last) {
524-
int time = World::getWorld()->getTime();
525-
time = (-time) - 3;
526-
// code till be added to 3 and inverted to get time
527-
itempolicy->m_virtualpace_code = time;
528-
}
529499

500+
item_policy->checkAndApplyVirtualPaceCarRules(kart, sec, kart_info.m_finished_laps);
530501

531502
// Race finished
532503
// We compute the exact moment the kart crossed the line

src/network/packet_types.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ enum LobbyEvent: uint8_t
3838
LE_CONNECTION_REQUESTED = 1, // a connection to the server
3939
LE_CONNECTION_REFUSED, // Connection to server refused
4040
LE_CONNECTION_ACCEPTED, // Connection to server accepted
41-
LE_ITEMPOLICY, // inform client about item policy
41+
LE_ITEM_POLICY, // inform client about item policy
4242
LE_SERVER_INFO, // inform client about server info
4343
LE_REQUEST_BEGIN, // begin of kart selection
4444
LE_UPDATE_PLAYER_LIST, // inform client about player list update

src/network/protocols/client_lobby.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ bool ClientLobby::notifyEvent(Event* event)
208208
case LE_UPDATE_PLAYER_LIST: updatePlayerList(event); break;
209209
case LE_CHAT: handleChat(event); break;
210210
case LE_CONNECTION_ACCEPTED: connectionAccepted(event); break;
211-
case LE_ITEMPOLICY: handleItemPolicy(event); break;
211+
case LE_ITEM_POLICY: handleItemPolicy(event); break;
212212
case LE_SERVER_INFO: handleServerInfo(event); break;
213213
case LE_PLAYER_DISCONNECTED : disconnectedPlayer(event); break;
214214
case LE_CONNECTION_REFUSED: connectionRefused(event); break;

0 commit comments

Comments
 (0)