fix rent calculation

This commit is contained in:
Adolfo Reyna
2026-01-31 22:23:49 -05:00
parent fa4c6f00ca
commit 78b376ad5d
9 changed files with 276 additions and 77 deletions

View File

@@ -13,32 +13,49 @@ class PropertyModalGame : public Game {
bool dismissed;
bool is_owned;
const char* owner_name;
int owner_id; // -1 if not owned
bool can_afford;
int selected_choice; // 0: Buy, 1: Cancel
bool buy_requested;
bool rent_requested;
Player* players;
int players_count;
public:
PropertyModalGame(uint16_t width, uint16_t height, LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager, const BoardTile* prop, bool owned, const char* owner, bool affordable, Player* p_list = nullptr, int p_count = 0)
: Game(width, height, renderer, gui, input_manager), property(prop), dismissed(false), is_owned(owned), owner_name(owner), can_afford(affordable), selected_choice(0), buy_requested(false), players(p_list), players_count(p_count) {
PropertyModalGame(uint16_t width, uint16_t height, LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager, const BoardTile* prop, bool owned, const char* owner, int o_id, bool affordable, Player* p_list = nullptr, int p_count = 0)
: Game(width, height, renderer, gui, input_manager), property(prop), dismissed(false), is_owned(owned), owner_name(owner), owner_id(o_id), can_afford(affordable), selected_choice(0), buy_requested(false), rent_requested(false), players(p_list), players_count(p_count) {
if (is_owned || !can_afford) selected_choice = 1;
}
void init() override { dismissed = false; buy_requested = false; }
void init() override { dismissed = false; buy_requested = false; rent_requested = false; selected_choice = 0; }
Type get_type() const override { return Type::MONOPOLY_PROPERTY; }
bool update(const InputEvent& event) override {
if (event.type == INPUT_BUTTON_0) { // BUTTON A -> BUY
if (!is_owned && can_afford) {
buy_requested = true;
// If rent is owed, any button pays it (it's forced)
if (is_owned && owner_id != -1) {
if (event.type == INPUT_BUTTON_0 || event.type == INPUT_BUTTON_1) {
rent_requested = true;
dismissed = true;
return true;
} else if (is_owned || !can_afford) {
// If it's just the "OK" state, either button works?
// Image shows >B CONTINUE in my code, so maybe Button 1.
}
return false;
}
// Otherwise (Buy/Pass), use A to cycle and B to select
if (event.type == INPUT_BUTTON_0) { // BUTTON A -> Change selection
if (!is_owned && can_afford) {
selected_choice = (selected_choice + 1) % 2;
return true;
}
}
if (event.type == INPUT_BUTTON_1) { // BUTTON B -> AUCTION / DISMISS
dismissed = true;
return true;
if (event.type == INPUT_BUTTON_1) { // BUTTON B -> Select action
if (!is_owned && can_afford) {
if (selected_choice == 0) buy_requested = true;
dismissed = true;
return true;
} else {
// Only option was Pass
dismissed = true;
return true;
}
}
return false;
}
@@ -51,7 +68,12 @@ public:
char buf[128];
if (players && players_count > 0) {
MonopolyBoardRenderer::draw_board_perimeter(renderer, width, height, players, players_count);
// Find current player position (who is interacting with the modal)
// In MonopolyGame, p is players[current_player_idx]
// We don't have the idx here directly, but we can highlight the property itself
int property_idx = -1;
for(int i=0; i<40; i++) if(&MONOPOLY_BOARD[i] == property) property_idx = i;
MonopolyBoardRenderer::draw_board_perimeter(renderer, width, height, players, players_count, property_idx);
}
// Window background (White box)
@@ -104,26 +126,70 @@ public:
int btn_w = win_w - 30;
int btn_h = 25;
if (is_owned || !can_afford) {
// Only one option: CONTINUE (B)
if (is_owned && owner_id != -1) {
// Option: Pay Rent (A or B)
renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
renderer->set_text_color(false);
renderer->draw_string_scaled(win_x + 25, btn_y + 8, ">B CONTINUE", 1);
int rent = 0;
if (property->type == TILE_PROPERTY) {
rent = property->rent[0];
} else if (property->type == TILE_RAILROAD) {
// Calculate Railroad rent based on owner's count
int rr_count = 0;
if (owner_id != -1 && owner_id < players_count) {
for (int i = 0; i < players[owner_id].property_count; ++i) {
// Find the property in the global board to check type (or just trust the index)
int prop_idx = players[owner_id].properties_owned[i];
if (MONOPOLY_BOARD[prop_idx].type == TILE_RAILROAD) {
rr_count++;
}
}
}
if (rr_count == 1) rent = 25;
else if (rr_count == 2) rent = 50;
else if (rr_count == 3) rent = 100;
else if (rr_count == 4) rent = 200;
else rent = 25; // Fallback
} else if (property->type == TILE_UTILITY) {
// Simplified 40 or use a more complex check
rent = 40;
}
snprintf(buf, sizeof(buf), ">PAY RENT ($%d)", rent);
renderer->draw_string_scaled(win_x + (win_w - (int)strlen(buf) * 6) / 2, btn_y + 8, buf, 1);
renderer->set_text_color(true);
} else {
// Choice: Buy (A) or Auction (B)
} else if (!is_owned && can_afford) {
// Choice: Buy (A) or Pass (B). Changed to: A cycles, B selects.
// Buy Button
renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
renderer->set_text_color(false);
snprintf(buf, sizeof(buf), ">A BUY ($%d)", property->cost);
if (selected_choice == 0) renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
else renderer->draw_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
if (selected_choice == 0) renderer->set_text_color(false);
snprintf(buf, sizeof(buf), "%sBUY ($%d)", (selected_choice == 0 ? "> " : " "), property->cost);
renderer->draw_string_scaled(win_x + 20, btn_y + 8, buf, 1);
renderer->set_text_color(true);
btn_y += 30;
// Auction Button
renderer->draw_string_scaled(win_x + 20, btn_y + 8, ">B AUCTION", 1);
// Pass Button
if (selected_choice == 1) renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
if (selected_choice == 1) renderer->set_text_color(false);
snprintf(buf, sizeof(buf), "%sPASS", (selected_choice == 1 ? "> " : " "));
renderer->draw_string_scaled(win_x + 20, btn_y + 8, buf, 1);
renderer->set_text_color(true);
// Helpful hint
renderer->draw_string_scaled(win_x + 15, win_y + win_h - 15, "A:Next B:Sel", 1);
} else {
// Only one option: PASS (B) (e.g. if owned by self or can't afford)
renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
renderer->set_text_color(false);
renderer->draw_string_scaled(win_x + 25, btn_y + 8, ">B PASS", 1);
renderer->set_text_color(true);
}
}
bool is_dismissed() const { return dismissed; }
bool wants_to_buy() const { return buy_requested; }
bool wants_to_pay_rent() const { return rent_requested; }
int get_owner_id() const { return owner_id; }
};