Skip to content

Commit ee9baf8

Browse files
committed
Fix #43
1 parent 5461bf7 commit ee9baf8

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

src/karts/kart.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,13 @@ void Kart::reset()
481481

482482
// Item Policy No Collision Karts karts are different because they have bodies but can't collide with projectiles or with other karts
483483
ItemPolicy *item_policy = RaceManager::get()->getItemPolicy();
484-
uint32_t rules = item_policy->m_policy_sections[0].m_rules;
484+
uint32_t rules = item_policy->m_policy_sections[0].m_rules;
485485

486-
if (rules & ItemPolicyRules::IPT_GHOST_KARTS) {
486+
if (rules & ItemPolicyRules::IPT_GHOST_KARTS) {
487487
Physics::get()->addKart(this, true /*ghost*/);
488-
} else {
488+
} else {
489489
Physics::get()->addKart(this, false /*ghost*/);
490-
}
490+
}
491491
}
492492

493493
m_min_nitro_ticks = 0;
@@ -1340,6 +1340,9 @@ void Kart::collectedItem(ItemState *item_state)
13401340
const Item::ItemType type = item_state->getType();
13411341
bool is_mini;
13421342
auto& stk_config = STKConfig::get();
1343+
uint32_t rules;
1344+
int sec;
1345+
ItemPolicy *item_policy = NULL;
13431346

13441347
switch (type)
13451348
{
@@ -1361,7 +1364,18 @@ void Kart::collectedItem(ItemState *item_state)
13611364
break;
13621365
}
13631366

1364-
m_tyres->commandChange(item_state->m_compound, item_state->m_stop_time);
1367+
item_policy = RaceManager::get()->getItemPolicy();
1368+
sec = item_policy->getSectionForKart(this);
1369+
if (sec == -1)
1370+
sec = 0;
1371+
1372+
rules = item_policy->m_policy_sections[sec].m_rules;
1373+
1374+
if (rules & ItemPolicyRules::IPT_TYRE_CHANGE_TIME_OVERRIDE) {
1375+
m_tyres->commandChange(item_state->m_compound, item_policy->m_policy_sections[sec].m_tyre_change_time);
1376+
} else {
1377+
m_tyres->commandChange(item_state->m_compound, item_state->m_stop_time);
1378+
}
13651379

13661380
break;
13671381
case Item::ITEM_BUBBLEGUM:

src/race/item_policy.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "utils/string_utils.hpp"
2626
#include "race/item_policy.hpp"
2727
#include "modes/world.hpp"
28+
#include "modes/linear_world.hpp"
2829
#include "items/item.hpp"
2930
#include "items/powerup.hpp"
3031
#include "items/powerup_manager.hpp"
@@ -159,6 +160,7 @@ void ItemPolicy::applySectionRules(ItemPolicySection &section, Kart *kart, int n
159160
// Returns the section that was applied. Returns -1 if it tried to appply an unsupported section or if there are no sections
160161
int ItemPolicy::applyRules(Kart *kart, int current_lap, int current_time, int total_laps_of_race) {
161162
if (m_policy_sections.size() == 0) return -1;
163+
162164
for (unsigned i = 0; i < m_policy_sections.size(); i++) {
163165
int next_section_start_laps = total_laps_of_race;
164166
int prev_lap_item_amount = kart->m_item_amount_last_lap;
@@ -192,6 +194,44 @@ int ItemPolicy::applyRules(Kart *kart, int current_lap, int current_time, int to
192194
}
193195
return -1;
194196
}
197+
198+
int ItemPolicy::getSectionForKart(Kart *kart) {
199+
if (!World::getWorld()->raceHasLaps())
200+
return -1;
201+
202+
LinearWorld *lin_world = dynamic_cast<LinearWorld*>(World::getWorld());
203+
int current_lap = lin_world->getFinishedLapsOfKart(kart->getWorldKartId());
204+
int current_time = World::getWorld()->getTime();
205+
206+
if (m_policy_sections.size() == 0) return -1;
207+
for (unsigned i = 0; i < m_policy_sections.size(); i++) {
208+
if (i+1 == m_policy_sections.size()) {
209+
return i;
210+
break;
211+
} else if (m_policy_sections[i].m_section_type == IP_TIME_BASED) {
212+
Log::error("ItemPolicy", "Time-implemented item policy sections are not implemented yet");
213+
return i;
214+
break;
215+
} else if (m_policy_sections[i].m_section_type == IP_LAPS_BASED) {
216+
if (m_policy_sections[i+1].m_section_type == IP_TIME_BASED) {
217+
Log::error("ItemPolicy", "Time-implemented item policy sections are not implemented yet");
218+
return i;
219+
break;
220+
} else if (m_policy_sections[i+1].m_section_type == IP_LAPS_BASED) {
221+
if (current_lap >= m_policy_sections[i].m_section_start &&
222+
current_lap < m_policy_sections[i+1].m_section_start)
223+
{
224+
return i;
225+
break;
226+
} else {
227+
continue;
228+
}
229+
}
230+
}
231+
}
232+
return -1;
233+
234+
}
195235
//--------------------------------------------------
196236
static std::string fetch(std::vector<std::string>& strings, unsigned idx) {
197237
if (idx >= strings.size())

src/race/item_policy.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ struct ItemPolicy {
154154
int current_time, int prev_lap_item_amount);
155155

156156
int applyRules(Kart *kart, int current_lap, int current_time, int total_laps_of_race);
157+
int getSectionForKart(Kart *kart);
157158

158159
bool isHitValid(float sender_distance, float sender_lap, int sender_position, float recv_distance, int recv_position, float recv_lap, float track_length);
159160

0 commit comments

Comments
 (0)