Jaizu85

Untitled

May 19th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.47 KB | None | 0 0
  1. #include "global.h"
  2. #include "main.h"
  3. #include "dns.h"
  4. #include "palette.h"
  5. #include "start_menu.h"
  6. #include "overworld.h"
  7. #include "battle_main.h"
  8. #include "rtc.h"
  9. #include "constants/map_types.h"
  10. #include "constants/rgb.h"
  11.  
  12. /*******************************************************/
  13. /********* Day and Night Configuration **********/
  14. /********************************************************
  15. * You can customize the DNS by editing the following *
  16. * timelapses and the filters used to change the *
  17. * palette colours. *
  18. * In addition to that, you can also configure which *
  19. * palettes are affected by the system, as well as *
  20. * establishing sprite palettes exceptions by its TAG. *
  21. *
  22. * It is highly recommended to read the following config*
  23. * options, to understand how the dns works. *
  24. * ******************************************************/
  25.  
  26. // Partes del día (Times of day)
  27. enum
  28. {
  29. TIME_MORNING
  30. TIME_DAY,
  31. TIME_NIGHT
  32. };
  33.  
  34. // Partes del día (Horas)
  35. #define MORNING_END_HOUR 10 // Se acaba a las 10:00 am
  36. #define DAY_END_HOUR 20 // Se acaba a las 8:00 pm
  37. #define NIGHT_END_HOUR 4 // Se acaba a las 4:00 am
  38.  
  39. // Horas en las que están las ventanas iluminadas
  40. #define LIGHTNING_START_HOUR 10
  41. #define LIGHTNING_END_HOUR 7
  42.  
  43. // Ventanas Iluminadas
  44. const struct LightingColour gLightingColours[] =
  45. {
  46. {
  47. .paletteNum = 5,
  48. .colourNum = 1,
  49. .lightColour = RGB2(27, 19, 10),
  50. },
  51. {
  52. .paletteNum = 5,
  53. .colourNum = 2,
  54. .lightColour = RGB2(17, 11, 7),
  55. },
  56. {
  57. .paletteNum = 5,
  58. .colourNum = 3,
  59. .lightColour = RGB2(25, 16, 9),
  60. },
  61. {
  62. .paletteNum = 5,
  63. .colourNum = 4,
  64. .lightColour = RGB2(20, 13, 9),
  65. },
  66. };
  67.  
  68. // Mapas no afectados por el DNS
  69. const u8 gDnsMapExceptions[] =
  70. {
  71. MAP_TYPE_NONE,
  72. MAP_TYPE_INDOOR,
  73. MAP_TYPE_UNDERGROUND,
  74. MAP_TYPE_SECRET_BASE,
  75. };
  76.  
  77. /* Configure each palette slot to be affected or not by DNS *
  78. * while you are in the overworld. */
  79. const struct DnsPalExceptions gOWPalExceptions =
  80. {
  81. .pal = {
  82. DNS_PAL_ACTIVE, //0
  83. DNS_PAL_ACTIVE, //1
  84. DNS_PAL_ACTIVE, //2
  85. DNS_PAL_ACTIVE, //3
  86. DNS_PAL_ACTIVE, //4
  87. DNS_PAL_ACTIVE, //5
  88. DNS_PAL_ACTIVE, //6
  89. DNS_PAL_ACTIVE, //7
  90. DNS_PAL_ACTIVE, //8
  91. DNS_PAL_ACTIVE, //9
  92. DNS_PAL_ACTIVE, //10
  93. DNS_PAL_ACTIVE, //11
  94. DNS_PAL_ACTIVE, //12
  95. DNS_PAL_EXCEPTION, //13
  96. DNS_PAL_EXCEPTION, //14
  97. DNS_PAL_EXCEPTION, //15
  98. DNS_PAL_ACTIVE, //16
  99. DNS_PAL_ACTIVE, //17
  100. DNS_PAL_ACTIVE, //18
  101. DNS_PAL_ACTIVE, //19
  102. DNS_PAL_ACTIVE, //20
  103. DNS_PAL_ACTIVE, //21
  104. DNS_PAL_ACTIVE, //22
  105. DNS_PAL_ACTIVE, //23
  106. DNS_PAL_ACTIVE, //24
  107. DNS_PAL_ACTIVE, //25
  108. DNS_PAL_ACTIVE, //26
  109. DNS_PAL_ACTIVE, //27
  110. DNS_PAL_ACTIVE, //28
  111. DNS_PAL_ACTIVE, //29
  112. DNS_PAL_ACTIVE, //30
  113. DNS_PAL_ACTIVE, //31
  114. }
  115. };
  116.  
  117. /* Configure each palette slot to be affected or not by DNS *
  118. * while in combat. */
  119. const struct DnsPalExceptions gCombatPalExceptions =
  120. {
  121. .pal = {
  122. DNS_PAL_EXCEPTION, //0
  123. DNS_PAL_EXCEPTION, //1
  124. DNS_PAL_ACTIVE, //2
  125. DNS_PAL_ACTIVE, //3
  126. DNS_PAL_ACTIVE, //4
  127. DNS_PAL_EXCEPTION, //5
  128. DNS_PAL_ACTIVE, //6
  129. DNS_PAL_ACTIVE, //7
  130. DNS_PAL_ACTIVE, //8
  131. DNS_PAL_ACTIVE, //9
  132. DNS_PAL_ACTIVE, //10
  133. DNS_PAL_ACTIVE, //11
  134. DNS_PAL_ACTIVE, //12
  135. DNS_PAL_ACTIVE, //13
  136. DNS_PAL_ACTIVE, //14
  137. DNS_PAL_ACTIVE, //15
  138. DNS_PAL_EXCEPTION, //16
  139. DNS_PAL_EXCEPTION, //17
  140. DNS_PAL_EXCEPTION, //18
  141. DNS_PAL_EXCEPTION, //19
  142. DNS_PAL_EXCEPTION, //20
  143. DNS_PAL_EXCEPTION, //21
  144. DNS_PAL_EXCEPTION, //22
  145. DNS_PAL_EXCEPTION, //23
  146. DNS_PAL_EXCEPTION, //24
  147. DNS_PAL_EXCEPTION, //25
  148. DNS_PAL_EXCEPTION, //26
  149. DNS_PAL_EXCEPTION, //27
  150. DNS_PAL_EXCEPTION, //28
  151. DNS_PAL_EXCEPTION, //29
  152. DNS_PAL_EXCEPTION, //30
  153. DNS_PAL_EXCEPTION, //31
  154. }
  155. };
  156.  
  157.  
  158. /*******************************************************/
  159. /************* DNS Colour Filters ***************/
  160. /*******************************************************/
  161. /* DNS filters are actual 15bit RGB colours. *
  162. * This colours R - G - B channels are substracted from *
  163. * the original colour in the palette buffer during the *
  164. * transfer from the buffer to the palette RAM. *
  165. * *
  166. * [BUFFER] -> (Value - Filter) -> [PAL_RAM] *
  167. * *
  168. * This means that you shouln't use too high values for *
  169. * RGB channels in the filters. Otherwie, the channels *
  170. * will easily reach 0, giving you plain colours. *
  171. * I Suggest to not use channels with a value above 16. *
  172. * *
  173. * Feel free to experiment with your own filters. *
  174. * ******************************************************
  175. * DNS Alternative Filtering System *
  176. * I've created and alternative filtering system, which *
  177. * substracts the level of each channel porportionally. *
  178. * I personally prefer this alternative method, since *
  179. * the filters are blended "softer". *
  180. * This is more noticeable with the darker filters. *
  181. */
  182.  
  183. /* Filters used at midnight. *
  184. * From 00:00 to 01:00 filters are cycled every *
  185. * 8 minutes. *
  186. * From 01:00 to 07:00 the last filter is used. */
  187. const u16 gMidnightFilters[] =
  188. {
  189. RGB2(14, 14, 6), //CE19
  190. RGB2(14, 14, 7), //CE1D
  191. RGB2(14, 14, 8), //CE21
  192. RGB2(15, 15, 8), //EF21
  193. RGB2(15, 15, 9), //EF25
  194. RGB2(15, 15, 9), //EF25
  195. RGB2(16, 16, 9), //1026
  196. RGB2(16, 16, 10), //102A
  197. };
  198. /* Filters used at dawn. (30 filters). *
  199. * From 07:00 to 08:00 filters are cycled every *
  200. * 2 minutes. */
  201. const u16 gDawnFilters[] =
  202. {
  203. RGB2(15, 15, 10),
  204. RGB2(15, 15, 10), //1
  205. RGB2(14, 14, 10), //2
  206. RGB2(13, 13, 10), //3
  207. RGB2(12, 12, 10), //4
  208. RGB2(11, 11, 10), //5
  209. RGB2(10, 10, 10), //6
  210. RGB2(9, 9, 10), //7
  211. RGB2(8, 8, 10), //8
  212. RGB2(8, 8, 11), //9
  213. RGB2(7, 7, 11), //10
  214. RGB2(6, 6, 11), //11
  215. RGB2(5, 5, 11), //12
  216. RGB2(4, 4, 11), //13
  217. RGB2(3, 3, 11), //14
  218. RGB2(2, 2, 11), //15
  219. RGB2(1, 1, 11), //16
  220. RGB2(0, 0, 11), //17
  221. RGB2(0, 0, 10), //18
  222. RGB2(0, 0, 9), //19
  223. RGB2(0, 0, 8), //20
  224. RGB2(0, 0, 7), //21
  225. RGB2(0, 0, 6), //22
  226. RGB2(0, 0, 5), //23
  227. RGB2(0, 0, 4), //24
  228. RGB2(0, 0, 3), //0003
  229. RGB2(0, 0, 2), //0002
  230. RGB2(0, 0, 1), //0001
  231. RGB2(0, 0, 0), //0000
  232. RGB2(0, 0, 0), //0000
  233. };
  234.  
  235. /* Filters used at day. (no filter actually lul)*/
  236. const u16 gDayFilter = RGB2(0, 0, 0); //0000
  237.  
  238. /* Filters used at sunset. (30 filters). *
  239. * From 19:00 to 20:00 filters are cycled every *
  240. * 2 minutes. */
  241. const u16 gSunsetFilters[] =
  242. {
  243. RGB2(0, 0, 1), //0004
  244. RGB2(0, 1, 1), //2004
  245. RGB2(0, 1, 2), //2008
  246. RGB2(0, 1, 3), //200C
  247. RGB2(0, 2, 3), //400C
  248. RGB2(0, 2, 4), //4010
  249. RGB2(0, 2, 5), //4014
  250. RGB2(0, 3, 5), //6014
  251. RGB2(0, 3, 6), //6018
  252. RGB2(0, 3, 7), //601C
  253. RGB2(0, 4, 7), //801C
  254. RGB2(0, 4, 8), //8020
  255. RGB2(0, 4, 9), //8024
  256. RGB2(0, 5, 9), //A024
  257. RGB2(0, 5, 10), //A028
  258. RGB2(0, 5, 11), //A02C
  259. RGB2(0, 6, 11), //C02C
  260. RGB2(0, 6, 12), //C030
  261. RGB2(0, 6, 13), //C034
  262. RGB2(0, 7, 13), //E034
  263. RGB2(0, 7, 14), //E038
  264. RGB2(0, 7, 14), //E038
  265. RGB2(0, 8, 14), //0039
  266. RGB2(0, 9, 14), //2039
  267. RGB2(0, 10, 14), //4039
  268. RGB2(0, 11, 14), //6039
  269. RGB2(0, 12, 14), //8039
  270. RGB2(0, 13, 14), //A039
  271. RGB2(0, 14, 14), //C039
  272. RGB2(0, 14, 14), //C039
  273. };
  274.  
  275. /* Filters used at nightfall. (30 filters). *
  276. * From 20:00 to 21:00 filters are cycled every *
  277. * 2 minutes. */
  278. const u16 gNightfallFilters[] =
  279. {
  280. RGB2(0, 14, 14), //39C0
  281. RGB2(0, 14, 14), //39C0
  282. RGB2(0, 14, 13), //35C0
  283. RGB2(0, 14, 12), //31C0
  284. RGB2(0, 14, 11), //2DC0
  285. RGB2(0, 14, 10), //29C0
  286. RGB2(1, 14, 10), //29C1
  287. RGB2(1, 14, 9), //25C1
  288. RGB2(0, 14, 8), //21C0
  289. RGB2(1, 14, 7), //1DC1
  290. RGB2(1, 14, 6), //19C1
  291. RGB2(2, 14, 6), //19C2
  292. RGB2(2, 14, 5), //15C2
  293. RGB2(2, 14, 4), //11C2
  294. RGB2(2, 14, 3), //0DC2
  295. RGB2(2, 14, 2), //09C2
  296. RGB2(2, 14, 2), //09C2
  297. RGB2(3, 14, 3), //0DC3
  298. RGB2(4, 14, 4), //11C4
  299. RGB2(5, 14, 5), //15C5
  300. RGB2(6, 14, 6), //19C6
  301. RGB2(7, 14, 6), //19C7
  302. RGB2(8, 14, 6), //19C8
  303. RGB2(9, 14, 6), //19C9
  304. RGB2(10, 14, 6), //19CA
  305. RGB2(11, 14, 6), //19CB
  306. RGB2(12, 14, 6), //19CC
  307. RGB2(13, 14, 6), //19CD
  308. RGB2(14, 14, 6), //19CE
  309. RGB2(14, 14, 6), //19CE
  310. };
  311.  
  312. /* Filter used at night. From 21:00 to 24:00 */
  313. const u16 gNightFilter = RGB2(6, 6, 6); //19CE
  314.  
  315. /************* SpritePalette Dns exceptions by TAG **************
  316. * If you are using any dynamic sprite palette allocation system, *
  317. * you will most likely want to use this system to avoid certain *
  318. * palette tags to be "banned" from dns, as the palettes may get *
  319. * loaded in different slots each time. */
  320. const u16 gPaletteTagExceptions[] =
  321. {
  322. 0xD6FF, //TAG_HEALTHBOX_PLAYER1_TILE
  323. 0xD700, //TAG_HEALTHBOX_PLAYER2_TILE
  324. 0xD701, //TAG_HEALTHBOX_OPPONENT1_TILE
  325. 0xD702, //TAG_HEALTHBOX_OPPONENT2_TILE
  326. 0X0006, // Pokes de la animación de MOs
  327. 0x3333, //MUGSHOTS DE DIZZY
  328. 0x3333, //MUGSHOTS DE DIZZY
  329. 0x3335, //MUGSHOTS DE DIZZY
  330. 0x3336, //MUGSHOTS DE DIZZY
  331. 0x3337, //MUGSHOTS DE DIZZY
  332. 0x3338, //MUGSHOTS DE DIZZY
  333. 0x3339, //MUGSHOTS DE DIZZY
  334. 0x3340, //MUGSHOTS DE DIZZY
  335. 0x3341, //MUGSHOTS DE DIZZY
  336. 0x3342, //MUGSHOTS DE DIZZY
  337. };
  338. /***********************************************
  339. * --------- DNS CONFIGURATION END ----------- *
  340. * ******************************************* */
  341.  
  342.  
  343. //Functions
  344. static u16 DnsApplyFilterToColour(u16 colour, u16 filter);
  345. static u16 DnsApplyProportionalFilterToColour(u16 colour, u16 filter);
  346. static void DoDnsLightning();
  347. static u16 GetDNSFilter();
  348. static bool8 IsMapDNSException();
  349. static bool8 IsSpritePaletteTagDnsException(u8 palNum);
  350. static bool8 IsOverworld();
  351. static bool8 IsCombat();
  352. static bool8 IsLightActive();
  353.  
  354. //Dns palette buffer in EWRAM
  355. ALIGNED(4) EWRAM_DATA static u16 sDnsPaletteDmaBuffer[512] = {0};
  356.  
  357.  
  358. /* **************************************************** *
  359. * **************** D&N for pokeemerald *************** *
  360. * **************************************************** *
  361. * Based on Prime Dialga DNS for Pokemon GBA Games. *
  362. * Additional credits to Andrea & Eing *
  363. * Author: Xhyz/Samu *
  364. ****************************************************** */
  365.  
  366. //Called from TransferPlttBuffer
  367. void DnsTransferPlttBuffer(void *src, void *dest)
  368. {
  369. if ((IsOverworld() || IsCombat()) && !IsMapDNSException())
  370. {
  371. DmaCopy16(3, sDnsPaletteDmaBuffer, dest, PLTT_SIZE);
  372. }
  373. else
  374. {
  375. DmaCopy16(3, src, dest, PLTT_SIZE);
  376. }
  377. }
  378.  
  379. /* Applies filter to palette colours, stores new palettes in EWRAM buffer. *
  380. * It must be called from CB2 if the DNS wants to be used (similar to *
  381. * TransferPlttBuffer) in VBlank callbacks */
  382. void DnsApplyFilters()
  383. {
  384. u8 palNum, colNum;
  385. u16 colour, rgbFilter;
  386. struct DnsPalExceptions palExceptionFlags;
  387.  
  388. rgbFilter = GetDNSFilter();
  389.  
  390. palExceptionFlags = gMain.inBattle ? gCombatPalExceptions : gOWPalExceptions; //Init pal exception slots
  391.  
  392. for (palNum = 0; palNum < 32; palNum++)
  393. if (palExceptionFlags.pal[palNum] && (palNum < 15 || !IsSpritePaletteTagDnsException(palNum - 16)))
  394. for (colNum = 0; colNum < 16; colNum++) //Transfer filtered palette to buffer
  395. sDnsPaletteDmaBuffer[palNum * 16 + colNum] = DnsApplyFilterToColour(gPlttBufferFaded[palNum * 16 + colNum], rgbFilter);
  396. else
  397. for (colNum = 0; colNum < 16; colNum++) //Transfers palette to buffer without filtering
  398. sDnsPaletteDmaBuffer[palNum * 16 + colNum] = gPlttBufferFaded[palNum * 16 + colNum];
  399.  
  400. if (!IsMapDNSException() && IsLightActive() && !gMain.inBattle)
  401. DoDnsLightning();
  402. }
  403.  
  404. //Applies filter to a colour. Filters RGB channels are substracted from colour RGB channels.
  405. //Based on Andrea's DNS filtering system
  406. static u16 DnsApplyFilterToColour(u16 colour, u16 filter)
  407. {
  408. u16 red, green, blue;
  409.  
  410. red = (colour & 0x1F) - (filter & 0x1F);
  411. green = ((colour & 0x3E0) - (filter & 0x3E0)) >> 5;
  412. blue = ((colour & 0x7C00) - (filter & 0x7C00)) >> 10;
  413.  
  414. return RGB2(red <= 31 ? red : 0, green <= 31 ? green : 0, blue <= 31 ? blue : 0);
  415. }
  416.  
  417. /*Alternative way to apply filter. Works similar to the first one, but colours are substracted PROPORTIONALLY.
  418. This system is great if you want to avoid colours with low rgb channels getting donw to 0 too fast.
  419. That's something that can easily happen with above Andrea's filtering system.*/
  420. static u16 DnsApplyProportionalFilterToColour(u16 colour, u16 filter)
  421. {
  422. u32 red, green, blue;
  423.  
  424. red = (colour & 0x1F) * (0x1F - (filter & 0x1F)) >> 5;
  425. green = ((colour & 0x3E0) >> 5) * ((0x3E0 - (filter & 0x3E0)) >> 5) >> 5;
  426. blue = ((colour & 0x7C00) >> 10) * ((0x7C00 - (filter & 0x7C00)) >> 10) >> 5;
  427.  
  428. return RGB2(red <= 31 ? red : 0, green <= 31 ? green : 0, blue <= 31 ? blue : 0);
  429. }
  430.  
  431. //returns the filter to use depending on RTC time.
  432. static u16 GetDNSFilter()
  433. {
  434. u8 hour = gLocalTime.hours;
  435. u8 minutes = gLocalTime.minutes;
  436.  
  437. switch(GetDnsTimeLapse(hour))
  438. {
  439. case TIME_MIDNIGHT:
  440. if (hour < 1)
  441. return gMidnightFilters[minutes >> 3];
  442. else
  443. return gMidnightFilters[7];
  444.  
  445. case TIME_DAWN:
  446. return gDawnFilters[minutes >> 1];
  447.  
  448. case TIME_DAY:
  449. return gDayFilter;
  450.  
  451. case TIME_SUNSET:
  452. return gSunsetFilters[minutes >> 1];
  453.  
  454. case TIME_NIGHTFALL:
  455. return gNightfallFilters[minutes >> 1];
  456.  
  457. case TIME_NIGHT:
  458. return gNightFilter;
  459. }
  460.  
  461. return 0;
  462. }
  463.  
  464. static void DoDnsLightning()
  465. {
  466. u8 i;
  467.  
  468. for (i = 0; i < sizeof(gLightingColours)/sizeof(gLightingColours[0]); i++)
  469. {
  470. u16 colourSlot = gLightingColours[i].paletteNum * 16 + gLightingColours[i].colourNum;
  471.  
  472. if (gPaletteFade.active || gPlttBufferUnfaded[colourSlot] != 0x0000)
  473. {
  474. sDnsPaletteDmaBuffer[colourSlot] = gPlttBufferFaded[colourSlot];
  475. gPlttBufferUnfaded[colourSlot] = gLightingColours[i].lightColour;
  476. }
  477. else
  478. {
  479. sDnsPaletteDmaBuffer[colourSlot] = gLightingColours[i].lightColour;
  480. }
  481. }
  482. }
  483.  
  484. //Returns Dns time lapse
  485. u8 GetDnsTimeLapse(u8 hour)
  486. {
  487. if (hour < MIDNIGHT_END_HOUR)
  488. return TIME_MIDNIGHT;
  489. else if (hour < DAWN_END_HOUR)
  490. return TIME_DAWN;
  491. else if (hour < DAY_END_HOUR)
  492. return TIME_DAY;
  493. else if (hour < SUNSET_END_HOUR)
  494. return TIME_SUNSET;
  495. else if (hour < NIGHTFALL_END_HOUR)
  496. return TIME_NIGHTFALL;
  497. else
  498. return TIME_NIGHT;
  499. }
  500.  
  501. //Checks if current map is affected by dns
  502. static bool8 IsMapDNSException()
  503. {
  504. u8 i;
  505. for (i=0; i < sizeof(gDnsMapExceptions)/sizeof(gDnsMapExceptions[0]); i++)
  506. if (gMapHeader.mapType == gDnsMapExceptions[i])
  507. return TRUE;
  508. return FALSE;
  509. }
  510.  
  511. //Returns true if the palette should not be affected by DNS filtering
  512. static bool8 IsSpritePaletteTagDnsException(u8 palNum)
  513. {
  514. u8 i;
  515.  
  516. for (i = 0; i < sizeof(gPaletteTagExceptions)/sizeof(gPaletteTagExceptions[0]); i++)
  517. if (GetSpritePaletteTagByPaletteNum(palNum) == gPaletteTagExceptions[i])
  518. return TRUE;
  519. return FALSE;
  520. }
  521.  
  522. //Returns true if overworld is running
  523. static bool8 IsOverworld()
  524. {
  525. if (gMain.callback2 == CB2_Overworld || gMain.callback2 ==CB2_OverworldBasic)
  526. return TRUE;
  527. else
  528. return FALSE;
  529. }
  530.  
  531. //Returns true if combat is running
  532. static bool8 IsCombat()
  533. {
  534. if (gMain.callback2 == BattleMainCB2)
  535. return TRUE;
  536. else
  537. return FALSE;
  538. }
  539.  
  540. static bool8 IsLightActive()
  541. {
  542. if (gLocalTime.hours >= LIGHTNING_START_HOUR || gLocalTime.hours < LIGHTNING_END_HOUR)
  543. return TRUE;
  544. return FALSE;
  545. }
Advertisement
Add Comment
Please, Sign In to add comment