209 lines
7.8 KiB
C
209 lines
7.8 KiB
C
#include "game.h"
|
|
|
|
const struct SpeciesInfo species[SPECIES_COUNT] = {
|
|
{"GOLDEN", "LV THREE", "WILD GOLDEN CREATURE", "GOLDEN USED BUMP", "NORMAL", GOLDEN_HP, 5, 7},
|
|
{"FERNFOX", "LV FOUR", "WILD FERNFOX APPEARED", "FERNFOX USED LEAF SWIPE", "GRASS", 18, 4, 7},
|
|
{"EMBERWING", "LV FOUR", "WILD EMBERWING APPEARED", "EMBERWING USED SPARK", "FIRE", 20, 4, 4},
|
|
{"PEBBLIT", "LV FIVE", "WILD PEBBLIT APPEARED", "PEBBLIT USED ROLL", "ROCK", 22, 5, 9},
|
|
{"TIDEFIN", "LV FOUR", "HOOKED A WILD TIDEFIN", "TIDEFIN USED SPLASH JET", "WATER", 24, 4, 9}
|
|
};
|
|
struct BattleState battle;
|
|
unsigned random_state = 0x6d2b79f5u;
|
|
int grass_steps;
|
|
|
|
TEST_INTERNAL unsigned next_random(void)
|
|
{
|
|
if (!random_state) random_state = 0x6d2b79f5u;
|
|
random_state ^= random_state << 13;
|
|
random_state ^= random_state >> 17;
|
|
random_state ^= random_state << 5;
|
|
return random_state;
|
|
}
|
|
|
|
int start_battle(enum Species enemy)
|
|
{
|
|
if (!partner_owned || battle.phase != BATTLE_OFF || (unsigned)enemy >= SPECIES_COUNT)
|
|
return 0;
|
|
battle.phase = BATTLE_TURN;
|
|
battle.enemy = enemy;
|
|
battle.sprout_hp = partner_hp();
|
|
battle.enemy_hp = species[enemy].hp;
|
|
battle.move = 0;
|
|
battle.turns = 0;
|
|
battle.notice = NOTICE_NONE;
|
|
grass_steps = 0;
|
|
return 1;
|
|
}
|
|
|
|
int near_golden(int x, int y)
|
|
{
|
|
return current_room == OUTSIDE && !has_caught(GOLDEN) &&
|
|
x >= 163 && x <= 205 && y >= 84 && y <= 126;
|
|
}
|
|
|
|
int begin_battle(int x, int y)
|
|
{
|
|
if (!near_golden(x, y)) return 0;
|
|
return start_battle(GOLDEN);
|
|
}
|
|
|
|
int check_grass_encounter(int old_x, int old_y, int x, int y)
|
|
{
|
|
if (!partner_owned || !in_tall_grass(x, y)) { grass_steps = 0; return 0; }
|
|
if (battle.phase != BATTLE_OFF || (old_x == x && old_y == y)) return 0;
|
|
++grass_steps;
|
|
/* No encounters while idle, and a 24-pixel breather after every battle. */
|
|
if (grass_steps < 24 || (grass_steps - 24) % 8) return 0;
|
|
if ((next_random() & 3u) && grass_steps < 64) return 0;
|
|
return start_battle((enum Species)(FERNFOX + next_random() % 3u));
|
|
}
|
|
|
|
static void enemy_reply(void)
|
|
{
|
|
battle.sprout_hp -= species[battle.enemy].attack;
|
|
if (battle.sprout_hp <= 0) {
|
|
battle.sprout_hp = 0;
|
|
battle.phase = BATTLE_LOST;
|
|
}
|
|
}
|
|
|
|
/* A weakened creature is easier to catch; quarter health guarantees it. */
|
|
static unsigned catch_chance(void)
|
|
{
|
|
int maximum = species[battle.enemy].hp;
|
|
if (battle.enemy_hp * 4 <= maximum) return 100;
|
|
return 35u + (unsigned)((maximum - battle.enemy_hp) * 50 / maximum);
|
|
}
|
|
|
|
static int throw_ball(void)
|
|
{
|
|
if (ball_count == 0) { battle.notice = NOTICE_NO_BALLS; return 1; }
|
|
if (caught_count >= CAUGHT_CAPACITY) { battle.notice = NOTICE_TEAM_FULL; return 1; }
|
|
--ball_count;
|
|
++battle.turns;
|
|
if (next_random() % 100 < catch_chance()) {
|
|
caught_species[caught_count++] = (unsigned char)battle.enemy;
|
|
battle.phase = BATTLE_CAUGHT;
|
|
battle.notice = NOTICE_NONE;
|
|
} else {
|
|
battle.notice = NOTICE_BROKE_FREE;
|
|
enemy_reply();
|
|
}
|
|
save_progress(); /* Save spent balls as well as successful catches. */
|
|
return 1;
|
|
}
|
|
|
|
/* One fresh button press performs one turn. */
|
|
int battle_input(unsigned pressed)
|
|
{
|
|
if (battle.phase == BATTLE_OFF || !pressed) return 0;
|
|
if (battle.phase != BATTLE_TURN) {
|
|
if (!(pressed & (A | B))) return 0;
|
|
battle.phase = BATTLE_OFF;
|
|
return 1;
|
|
}
|
|
if (pressed & B) {
|
|
battle.phase = BATTLE_FLED;
|
|
return 1;
|
|
}
|
|
if (pressed & SELECT) return throw_ball();
|
|
if (pressed & (LEFT | RIGHT | A)) battle.notice = NOTICE_NONE;
|
|
if (pressed & LEFT) battle.move = 0;
|
|
if (pressed & RIGHT) battle.move = 1;
|
|
if (!(pressed & A)) return !!(pressed & (LEFT | RIGHT));
|
|
int damage = 4;
|
|
if (battle.move) {
|
|
if (!active_member || caught_species[active_member - 1] == FERNFOX)
|
|
damage = species[battle.enemy].leaf_damage;
|
|
else
|
|
damage = species[caught_species[active_member - 1]].attack + 3;
|
|
}
|
|
battle.enemy_hp -= damage;
|
|
++battle.turns;
|
|
if (battle.enemy_hp <= 0) {
|
|
battle.enemy_hp = 0;
|
|
battle.phase = BATTLE_WON;
|
|
} else {
|
|
enemy_reply();
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static void health_bar(int x, int y, int hp, int maximum)
|
|
{
|
|
rectangle(x, y, 92, 6, RGB(7, 12, 10));
|
|
rectangle(x + 1, y + 1, 90, 4, RGB(21, 22, 18));
|
|
u16 color = hp * 4 <= maximum ? RGB(29, 9, 8) :
|
|
hp * 2 <= maximum ? RGB(29, 23, 7) : RGB(10, 25, 12);
|
|
rectangle(x + 1, y + 1, hp * 90 / maximum, 4, color);
|
|
}
|
|
|
|
void draw_battle_hud(void)
|
|
{
|
|
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
|
|
health_bar(16, 44, battle.enemy_hp, species[battle.enemy].hp);
|
|
health_bar(136, 97, battle.sprout_hp, partner_hp());
|
|
if (battle.phase == BATTLE_CAUGHT) {
|
|
/* Remove the creature and show its closed ball without a full-screen copy. */
|
|
rectangle(155, 18, 64, 44, RGB(23, 29, 25));
|
|
rectangle(155, 62, 64, 14, RGB(18, 26, 15));
|
|
rectangle(155, battle.enemy == GOLDEN ? 61 : 70, 64, 5, RGB(12, 21, 11));
|
|
rectangle(179, 57, 10, 12, ink);
|
|
rectangle(178, 59, 12, 8, ink);
|
|
rectangle(180, 58, 8, 5, RGB(29, 8, 9));
|
|
rectangle(180, 64, 8, 4, RGB(31, 31, 28));
|
|
rectangle(183, 62, 3, 3, ink);
|
|
rectangle(184, 63, 1, 1, RGB(31, 31, 28));
|
|
}
|
|
rectangle(8, 111, 224, 43, ink);
|
|
rectangle(10, 113, 220, 39, cream);
|
|
if (battle.phase == BATTLE_TURN) {
|
|
large_text(22, 119, "TACKLE", ink);
|
|
large_text(132, 119, partner_move(), ink);
|
|
int arrow_x = battle.move ? 122 : 12;
|
|
rectangle(arrow_x, 119, 2, 7, RGB(7, 17, 11));
|
|
rectangle(arrow_x + 2, 121, 2, 3, RGB(7, 17, 11));
|
|
text(190, 119, "BALLS", ink);
|
|
char count[] = {(char)('0' + ball_count / 10), (char)('0' + ball_count % 10), 0};
|
|
text(212, 119, count, ink);
|
|
const char *message = battle.notice == NOTICE_NO_BALLS ? "NO POKE BALLS LEFT" :
|
|
battle.notice == NOTICE_TEAM_FULL ? "YOUR TEAM IS FULL NO BALL USED" :
|
|
battle.notice == NOTICE_BROKE_FREE ? "IT BROKE FREE AND ATTACKED" :
|
|
battle.turns ? species[battle.enemy].reply : "CHOOSE YOUR PARTNERS MOVE";
|
|
text(16, 132, message, ink);
|
|
text(16, 143, "ARROWS CHOOSE A HIT SELECT BALL B RUN R MUSIC", ink);
|
|
} else {
|
|
const char *result = battle.phase == BATTLE_CAUGHT ? "CAUGHT IT" : battle.phase == BATTLE_WON ? "YOU WON" :
|
|
battle.phase == BATTLE_LOST ? "YOUR PARTNER NEEDS A REST" : "YOU GOT AWAY SAFELY";
|
|
large_text(20, 120, result, ink);
|
|
text(20, 134, "A BACK TO ADVENTURE", ink);
|
|
text(20, 144, battle.phase == BATTLE_CAUGHT ? "NEW FRIEND SAVED TO YOUR TEAM" :
|
|
"YOUR PARTNER WILL REST AND RECOVER", ink);
|
|
}
|
|
}
|
|
|
|
void draw_battle(void)
|
|
{
|
|
rectangle(0, 0, WIDTH, HEIGHT, RGB(28, 29, 23));
|
|
status(species[battle.enemy].intro);
|
|
rectangle(0, 18, WIDTH, 44, RGB(23, 29, 25));
|
|
rectangle(0, 62, WIDTH, 49, RGB(18, 26, 15));
|
|
rectangle(155, battle.enemy == GOLDEN ? 61 : 70, 64, 5, RGB(12, 21, 11));
|
|
rectangle(18, 104, 78, 6, RGB(12, 21, 11));
|
|
static const u16 gold_palette[] = {
|
|
0, RGB(9, 8, 6), RGB(23, 15, 5), RGB(30, 23, 7),
|
|
RGB(31, 28, 14), RGB(31, 31, 28), RGB(29, 13, 10), RGB(17, 8, 7)
|
|
};
|
|
if (battle.enemy == GOLDEN) scaled_creature(184, 62, 2, gold_palette);
|
|
else draw_wild_creature(battle.enemy, 184, 71);
|
|
draw_member(active_member, 57, 106, 0);
|
|
rectangle(10, 24, 106, 29, RGB(30, 30, 26));
|
|
large_text(16, 28, species[battle.enemy].name, RGB(5, 8, 11));
|
|
text(16, 37, species[battle.enemy].type, RGB(5, 8, 11));
|
|
text(69, 37, species[battle.enemy].level, RGB(5, 8, 11));
|
|
rectangle(130, 76, 102, 31, RGB(30, 30, 26));
|
|
large_text(136, 80, partner_name(), RGB(5, 8, 11));
|
|
text(179, 90, active_member ? species[caught_species[active_member - 1]].level : "LV FIVE", RGB(5, 8, 11));
|
|
draw_battle_hud();
|
|
}
|