pokeemerald/src/wonder_news.c

157 lines
4.5 KiB
C
Raw Normal View History

2019-03-26 15:11:55 -04:00
#include "global.h"
#include "mystery_gift.h"
2019-03-26 15:11:55 -04:00
#include "random.h"
#include "event_data.h"
#include "wonder_news.h"
2022-11-22 02:17:03 -05:00
#include "constants/items.h"
2019-03-26 15:11:55 -04:00
2022-11-22 02:17:03 -05:00
// Every 4th reward for sending Wonder News to a link partner is a "big" reward.
#define MAX_SENT_REWARD 4
// Only up to 5 rewards can be received in a short period. After this the player
// must take 500 steps before any more rewards can be received.
#define MAX_REWARD 5
static u32 GetRewardItem(struct WonderNewsMetadata *);
static u32 GetRewardType(struct WonderNewsMetadata *);
static void IncrementRewardCounter(struct WonderNewsMetadata *);
static void IncrementSentRewardCounter(struct WonderNewsMetadata *);
static void ResetSentRewardCounter(struct WonderNewsMetadata *);
void WonderNews_SetReward(u32 newsType)
2019-03-26 15:11:55 -04:00
{
struct WonderNewsMetadata *data = GetSavedWonderNewsMetadata();
2019-03-26 15:11:55 -04:00
2021-10-17 02:09:31 -04:00
data->newsType = newsType;
switch (newsType)
2019-03-26 15:11:55 -04:00
{
2021-10-17 02:09:31 -04:00
case WONDER_NEWS_NONE:
2019-03-26 15:11:55 -04:00
break;
2021-10-17 02:09:31 -04:00
case WONDER_NEWS_RECV_FRIEND:
case WONDER_NEWS_RECV_WIRELESS:
2022-11-22 02:17:03 -05:00
// Random berry between ITEM_RAZZ_BERRY and ITEM_NOMEL_BERRY
data->berry = (Random() % 15) + ITEM_TO_BERRY(ITEM_RAZZ_BERRY);
2019-03-26 15:11:55 -04:00
break;
2021-10-17 02:09:31 -04:00
case WONDER_NEWS_SENT:
2022-11-22 02:17:03 -05:00
// Random berry between ITEM_CHERI_BERRY and ITEM_IAPAPA_BERRY
data->berry = (Random() % 15) + ITEM_TO_BERRY(ITEM_CHERI_BERRY);
2019-03-26 15:11:55 -04:00
break;
}
}
2022-11-22 02:17:03 -05:00
void WonderNews_Reset(void)
2019-03-26 15:11:55 -04:00
{
struct WonderNewsMetadata *data = GetSavedWonderNewsMetadata();
2019-03-26 15:11:55 -04:00
2021-10-17 02:09:31 -04:00
data->newsType = WONDER_NEWS_NONE;
2022-11-22 02:17:03 -05:00
data->sentRewardCounter = 0;
data->rewardCounter = 0;
data->berry = 0;
VarSet(VAR_WONDER_NEWS_STEP_COUNTER, 0);
2019-03-26 15:11:55 -04:00
}
2022-11-22 02:17:03 -05:00
// Only used in FRLG
void WonderNews_IncrementStepCounter(void)
2019-03-26 15:11:55 -04:00
{
2022-11-22 02:17:03 -05:00
u16 *stepCounter = GetVarPointer(VAR_WONDER_NEWS_STEP_COUNTER);
struct WonderNewsMetadata *data = GetSavedWonderNewsMetadata();
2019-03-26 15:11:55 -04:00
2022-11-22 02:17:03 -05:00
// If the player has reached the reward limit, start counting steps.
// When they reach 500 steps reset the reward counter to allow them to
// receive rewards again.
if (data->rewardCounter >= MAX_REWARD && ++(*stepCounter) >= 500)
2019-03-26 15:11:55 -04:00
{
2022-11-22 02:17:03 -05:00
data->rewardCounter = 0;
*stepCounter = 0;
2019-03-26 15:11:55 -04:00
}
}
2022-11-22 02:17:03 -05:00
// Only used in FRLG
u16 WonderNews_GetRewardInfo(void)
2019-03-26 15:11:55 -04:00
{
2021-10-17 02:09:31 -04:00
u16 *result = &gSpecialVar_Result;
struct WonderNewsMetadata *data = GetSavedWonderNewsMetadata();
2022-11-22 02:17:03 -05:00
u16 rewardType;
2019-03-26 15:11:55 -04:00
2022-09-11 14:14:49 -04:00
// Checks if Mystery Event is enabled, not Mystery Gift?
if (!IsMysteryEventEnabled() || !ValidateSavedWonderNews())
2022-11-22 02:17:03 -05:00
return NEWS_REWARD_NONE;
2019-03-26 15:11:55 -04:00
2022-11-22 02:17:03 -05:00
rewardType = GetRewardType(data);
2019-03-26 15:11:55 -04:00
2022-11-22 02:17:03 -05:00
switch (rewardType)
2019-03-26 15:11:55 -04:00
{
2022-11-22 02:17:03 -05:00
case NEWS_REWARD_RECV_SMALL:
case NEWS_REWARD_RECV_BIG:
*result = GetRewardItem(data);
2019-03-26 15:11:55 -04:00
break;
2022-11-22 02:17:03 -05:00
case NEWS_REWARD_SENT_SMALL:
*result = GetRewardItem(data);
IncrementSentRewardCounter(data);
2019-03-26 15:11:55 -04:00
break;
2022-11-22 02:17:03 -05:00
case NEWS_REWARD_SENT_BIG:
*result = GetRewardItem(data);
ResetSentRewardCounter(data);
2019-03-26 15:11:55 -04:00
break;
2022-11-22 02:17:03 -05:00
case NEWS_REWARD_NONE:
case NEWS_REWARD_WAITING:
case NEWS_REWARD_AT_MAX:
2019-03-26 15:11:55 -04:00
break;
}
2022-11-22 02:17:03 -05:00
return rewardType;
2019-03-26 15:11:55 -04:00
}
2022-11-22 02:17:03 -05:00
static u32 GetRewardItem(struct WonderNewsMetadata *data)
2019-03-26 15:11:55 -04:00
{
2022-11-22 02:17:03 -05:00
u32 itemId;
2021-10-17 02:09:31 -04:00
data->newsType = WONDER_NEWS_NONE;
2023-01-20 12:34:15 -03:00
itemId = data->berry + FIRST_BERRY_INDEX - 1;
2022-11-22 02:17:03 -05:00
data->berry = 0;
IncrementRewardCounter(data);
return itemId;
2019-03-26 15:11:55 -04:00
}
2022-11-22 02:17:03 -05:00
static void ResetSentRewardCounter(struct WonderNewsMetadata *data)
2019-03-26 15:11:55 -04:00
{
2022-11-22 02:17:03 -05:00
data->sentRewardCounter = 0;
2019-03-26 15:11:55 -04:00
}
2022-11-22 02:17:03 -05:00
// Track number of times a reward was received (or attmepted to receive) for sending Wonder News to a link partner.
static void IncrementSentRewardCounter(struct WonderNewsMetadata *data)
2019-03-26 15:11:55 -04:00
{
2022-11-22 02:17:03 -05:00
data->sentRewardCounter++;
if (data->sentRewardCounter > MAX_SENT_REWARD)
data->sentRewardCounter = MAX_SENT_REWARD;
2019-03-26 15:11:55 -04:00
}
2022-11-22 02:17:03 -05:00
static void IncrementRewardCounter(struct WonderNewsMetadata *data)
2019-03-26 15:11:55 -04:00
{
2022-11-22 02:17:03 -05:00
data->rewardCounter++;
if (data->rewardCounter > MAX_REWARD)
data->rewardCounter = MAX_REWARD;
2019-03-26 15:11:55 -04:00
}
2022-11-22 02:17:03 -05:00
static u32 GetRewardType(struct WonderNewsMetadata *data)
2019-03-26 15:11:55 -04:00
{
2022-11-22 02:17:03 -05:00
if (data->rewardCounter == MAX_REWARD)
return NEWS_REWARD_AT_MAX;
2019-03-26 15:11:55 -04:00
2021-10-17 02:09:31 -04:00
switch (data->newsType)
2019-03-26 15:11:55 -04:00
{
2021-10-17 02:09:31 -04:00
case WONDER_NEWS_NONE:
2022-11-22 02:17:03 -05:00
return NEWS_REWARD_WAITING;
2021-10-17 02:09:31 -04:00
case WONDER_NEWS_RECV_FRIEND:
2022-11-22 02:17:03 -05:00
return NEWS_REWARD_RECV_SMALL;
2021-10-17 02:09:31 -04:00
case WONDER_NEWS_RECV_WIRELESS:
2022-11-22 02:17:03 -05:00
return NEWS_REWARD_RECV_BIG;
2021-10-17 02:09:31 -04:00
case WONDER_NEWS_SENT:
2022-11-22 02:17:03 -05:00
if (data->sentRewardCounter < MAX_SENT_REWARD - 1)
return NEWS_REWARD_SENT_SMALL;
return NEWS_REWARD_SENT_BIG;
2019-03-26 15:11:55 -04:00
default:
AGB_ASSERT(0);
2022-11-22 02:17:03 -05:00
return NEWS_REWARD_NONE;
2019-03-26 15:11:55 -04:00
}
}