瀏覽代碼

dns update palettes

SBird1337 8 年之前
父節點
當前提交
58ef5ee5bf
共有 4 個檔案被更改,包括 274 行新增24 行删除
  1. 15
    0
      bpre.sym
  2. 10
    0
      patches/dns/core.asm
  3. 240
    23
      src/dns/dns_core.c
  4. 9
    1
      src/specials/custom_specials.c

+ 15
- 0
bpre.sym 查看文件

@@ -31,12 +31,27 @@ palette_unfaded_buffer = 0x020371F8;
31 31
 palette_faded_buffer = 0x020375F8;
32 32
 current_mapheader = 0x02036DFC;
33 33
 
34
+script_something = 0x08069AA8|1;
35
+camera_update = 0x0805ABB0|1;
36
+foo_115798 = 0x08115798|1;
37
+foo_5ae28 = 0x0805AE28|1;
38
+foo_6ffbc = 0x0806ffbc|1;
39
+tilemaps_sync = 0x080F67B8|1;
40
+
41
+mapdata_load_palettes_to_gpu = 0x08059AD8|1;
42
+npc_paltag_by_palslot = 0x0805FF34|1;
43
+
44
+oe_read_word = 0x08083550|1;
45
+some_weather_func = 0x0807AA8C|1;
46
+
34 47
 blockset_load_palette_to_gpu = 0x080599C8|1;
35 48
 npc_pal_idx_for_given_tag = 0x0805F5A0|1;
36 49
 gpu_pal_apply = 0x080703EC|1;
37 50
 tint_palette = 0x08083598|1;
38 51
 tint_filter = 0x02036E28;
39 52
 gpu_pal_tags_index_of = 0x080089E8|1;
53
+load_palette_3 = 0x080598CC|1;
54
+lz77u_wram = 0x0800EBB4|1;
40 55
 
41 56
 vram_decompress = 0x081E3B6C|1;
42 57
 pal_decompress_slice_to_faded_and_unfaded = 0x080703A8|1;

+ 10
- 0
patches/dns/core.asm 查看文件

@@ -11,4 +11,14 @@
11 11
 .org 0x0805F538
12 12
     ldr r2, =pal_patch_for_npc+1
13 13
     bx r2
14
+    .pool
15
+
16
+.org 0x08083614
17
+    ldr r1, =overworld_anim_load_pal+1
18
+    bx r1
19
+    .pool
20
+
21
+.org 0x08056578
22
+    ldr r0, =overworld_loop+1
23
+    bx r0
14 24
     .pool

+ 240
- 23
src/dns/dns_core.c 查看文件

@@ -2,7 +2,9 @@
2 2
 
3 3
 enum time_type {
4 4
     DAY,
5
-    NIGHT
5
+    NIGHT,
6
+    EVENING,
7
+    MORNING
6 8
 };
7 9
 
8 10
 enum map_type {
@@ -23,8 +25,8 @@ struct mapdata_header {
23 25
     u32 height;
24 26
     void* border;
25 27
     void* data;
26
-    void* blockset_one;
27
-    void* blockset_two;
28
+    struct blockset* blockset_one;
29
+    struct blockset* blockset_two;
28 30
     u8 border_width;
29 31
     u8 border_height;
30 32
     u16 unknown;
@@ -67,32 +69,98 @@ struct pal_replace {
67 69
     struct color color;
68 70
 };
69 71
 
72
+struct blockset {
73
+    u8 is_compressed;
74
+    u8 is_secondary;
75
+    u16 padding;
76
+    void* tiles;
77
+    void* palette;
78
+    void* block_tiles;
79
+    void* funcptr;
80
+    void* behavior;
81
+};
82
+
83
+struct dynamic_pal {
84
+    u8 type;
85
+    u8 count;
86
+    u16 tag;
87
+};
88
+
89
+#define dynamic_palettes ((struct dynamic_pal*) 0x0203FF00)
90
+
91
+void blockset_load_virtual_palette_and_shade(struct blockset* blockset, u16 start, u16 len, struct color* destination);
70 92
 extern struct npc_palette* npc_palettes;
71 93
 extern volatile u8 tint_filter;
72
-void apply_shaders(u8 pal, u8 fade_copy);
94
+void apply_shaders(u8 pal, u8 fade_copy, struct color* to_apply);
73 95
 struct color alpha_blend(struct color a, struct color b, u8 alpha);
74 96
 extern void blockset_load_palette_to_gpu(void* blockset, u16 start, u16 len);
75 97
 extern void gpu_pal_apply(struct color* src, u16 dst, u16 len);
98
+extern void load_palette_3(u16 start, u16 len);
76 99
 extern void tint_palette(u8 pal_slot);
77 100
 extern u8 gpu_pal_tags_index_of(u16 pal_tag);
101
+extern void lz77u_wram(void* src, void* dst);
102
+extern void* oe_read_word(void* oe_script_pointer);
103
+extern void some_weather_func(u8 pal_index);
78 104
 void copy_unfaded(u8 slot);
105
+void pal_patch_for_npc(u16 tag, u8 pal);
79 106
 u8 is_current_map_dn_valid(enum map_type current_type);
80 107
 //care, this has to be manually patched to be u16
81 108
 extern u16 npc_pal_idx_for_given_tag(u16 tag);
82 109
 struct color_shade get_color_from_time(enum time_type current_time);
83 110
 enum time_type get_time_of_day();
84
-void apply_lighting(void* blockset, u8 copy);
111
+void update_dns_palettes();
112
+void apply_lighting(void* blockset, u8 copy, struct color* to_apply);
113
+
114
+extern void mapdata_load_palettes_to_gpu(struct mapdata_header* data_header);
115
+extern u16 npc_paltag_by_palslot(u8 slot);
116
+
117
+extern void script_something();
118
+extern void camera_update();
119
+extern void foo_115798();
120
+extern void foo_5ae28();
121
+extern void foo_6ffbc();
122
+extern void tilemaps_sync();
85 123
 
86 124
 enum time_type get_time_of_day() {
87
-    return NIGHT;
125
+    volatile u8* time_pointer = (u8*) (0x0203FAB1);
126
+    return *time_pointer;
88 127
 }
89 128
 
90 129
 static struct pal_replace lightmap[] = {
91
-    {(void*) 0x082D4AAC, 9, 10, {31, 31, 0}},
92
-    {(void*) 0x082D4AAC, 9, 9, {31, 31, 0}},
93
-    {(void*) 0x082D4AAC, 9, 8, {31, 31, 0}}
130
+    {(void*) 0x082D4AAC, 9, 10, {
131
+            31, 31, 0
132
+        }}
133
+
134
+    ,
135
+    {(void*) 0x082D4AAC, 9, 9, {
136
+            31, 31, 0
137
+        }}
138
+
139
+    ,
140
+    {(void*) 0x082D4AAC, 9, 8, {
141
+            31, 31, 0
142
+        }}
94 143
 };
95 144
 
145
+void overworld_loop() {
146
+    script_something();
147
+    task_exec();
148
+    objc_exec();
149
+    camera_update();
150
+    foo_115798();
151
+    foo_5ae28();
152
+    obj_sync();
153
+    fade_update();
154
+    foo_6ffbc();
155
+    tilemaps_sync();
156
+
157
+    volatile u8* test_pointer = (u8*) (0x0203FAB0);
158
+    if (*test_pointer == 1 && (fade_controller.mix_color & 0x8000) == 0) {
159
+        update_dns_palettes();
160
+        *test_pointer = 0;
161
+    }
162
+}
163
+
96 164
 static struct color_shade color_shade_day = {
97 165
     {0, 0, 0}, 255
98 166
 };
@@ -100,40 +168,184 @@ static struct color_shade color_shade_night = {
100 168
     {0, 6, 16}, 120
101 169
 };
102 170
 
171
+static struct color_shade color_shade_evening = {
172
+    {12, 7, 17}, 120
173
+};
174
+
175
+static struct color_shade color_shade_morning = {
176
+    {6, 16, 6}, 150
177
+};
178
+
179
+void npc_load_virtual_pal(struct color* dst, u16 tag, u8 slot, u8 shade) {
180
+    u16 npc_map_entry = npc_pal_idx_for_given_tag(tag);
181
+    memcpy(dst + (16 + slot)*16, npc_palettes[npc_map_entry].palette, 32);
182
+    if (shade) {
183
+        tint_palette(slot);
184
+        apply_shaders(slot + 16, 0, dst);
185
+    }
186
+}
187
+
188
+struct color* dns_alloc_virtual_pal_and_load() {
189
+    struct color* destination = malloc(0x400);
190
+    blockset_load_virtual_palette_and_shade(current_mapheader.data_header->blockset_one, 0x0, 0xE0, destination);
191
+    blockset_load_virtual_palette_and_shade(current_mapheader.data_header->blockset_two, 0x70, 0xC0, destination);
192
+    
193
+    npc_load_virtual_pal(destination, 0x1120, 0, false);
194
+    load_palette_3(0x0, 0xD0);
195
+    for (int i = 0; i < 16; ++i) {
196
+        if (dynamic_palettes[i].tag >= 0x1100) {
197
+            npc_load_virtual_pal(destination, dynamic_palettes[i].tag, i, true);
198
+        } else if (dynamic_palettes[i].tag == 0x1004) {
199
+            //oe_0001
200
+            struct color* oe_pal_1 = (struct color*) (0x08398FA8);
201
+            memcpy(destination + (16 + i) * 16, oe_pal_1, 32);
202
+            apply_shaders(16 + i, 0, destination);
203
+
204
+        } else if (dynamic_palettes[i].tag == 0x1005) {
205
+            //oe_0002
206
+            struct color* oe_pal_2 = (struct color*) (0x08398FC8);
207
+            memcpy(destination + (16 + i) * 16, oe_pal_2, 32);
208
+            apply_shaders(16 + 1, 0, destination);
209
+        }
210
+    }
211
+    return destination;
212
+}
213
+
214
+void update_dns_palettes() {
215
+    struct color* destination = dns_alloc_virtual_pal_and_load();
216
+    gpu_pal_apply(destination, 0x0, 0x1A0);
217
+    gpu_pal_apply(destination + 0x100, 0x100, 0x200);
218
+    free(destination);
219
+}
220
+
221
+void overworld_anim_load_pal(void** oe_script_pointer) {
222
+    struct npc_palette* pal_to_apply = oe_read_word(oe_script_pointer);
223
+    u16 current_idx = gpu_pal_tags_index_of(pal_to_apply->tag);
224
+
225
+    struct color* temp_color = malloc(32);
226
+    memcpy(temp_color, pal_to_apply->palette, 32);
227
+    apply_shaders(0, 0, temp_color);
228
+
229
+    struct resource temp_resource = {temp_color, pal_to_apply->tag, pal_to_apply->fill};
230
+    gpu_pal_obj_alloc_tag_and_apply(&temp_resource);
231
+    free(temp_color);
232
+    if (current_idx != 0xFF) {
233
+        current_idx = gpu_pal_tags_index_of(pal_to_apply->tag);
234
+        tint_palette(current_idx);
235
+    }
236
+    current_idx = gpu_pal_tags_index_of(pal_to_apply->tag);
237
+    some_weather_func(current_idx);
238
+    *oe_script_pointer += 4;
239
+    return;
240
+}
241
+
242
+void blockset_load_virtual_palette_and_shade(struct blockset* blockset, u16 start, u16 len, struct color* destination) {
243
+    if (blockset == 0)
244
+        return;
245
+    if (blockset->is_secondary) {
246
+        if (blockset->is_secondary == 1) {
247
+            memcpy(destination + start, blockset->palette + (start * 2), len);
248
+            for (u8 i = 7; i < 7 + 6; ++i)
249
+                apply_shaders(i, 0, destination);
250
+            apply_lighting(blockset, 0, destination);
251
+            //gpu_pal_apply((color_memory), start, len);
252
+            //load_palette_3(start, len >> 1);
253
+        } else {
254
+            struct color* pal_buffer = (struct color*) (0x02037ACC);
255
+            lz77u_wram(blockset->palette, pal_buffer);
256
+            memcpy(destination + start, pal_buffer + (start * 2), len);
257
+            for (u8 i = 7; i < 7 + 6; ++i)
258
+                apply_shaders(i, 0, destination);
259
+            apply_lighting(blockset, 0, destination);
260
+            //gpu_pal_apply(pal_buffer, start, len);
261
+            //load_palette_3(start, len >> 1);
262
+        }
263
+    } else {
264
+        //struct color* color_memory = malloc(224);
265
+        memcpy(destination, blockset->palette, len);
266
+        for (u8 i = 0; i < 7; ++i)
267
+            apply_shaders(i, 0, destination);
268
+        apply_lighting(blockset, 0, destination);
269
+        //gpu_pal_apply(color_memory, start, len);
270
+        //load_palette_3(start, len >> 1);
271
+        //free(color_memory);
272
+    }
273
+    return;
274
+}
275
+
276
+void blockset_load_palette_to_gpu_and_shade(struct blockset* blockset, u16 start, u16 len) {
277
+    if (blockset == 0)
278
+        return;
279
+    if (blockset->is_secondary) {
280
+        if (blockset->is_secondary == 1) {
281
+            struct color* color_memory = malloc(224);
282
+            memcpy(color_memory, blockset->palette + 0xE0, 224);
283
+            for (u8 i = 0; i < 7; ++i)
284
+                apply_shaders(i, 0, color_memory);
285
+            apply_lighting(blockset, 1, color_memory);
286
+            gpu_pal_apply((color_memory), start, len);
287
+            load_palette_3(start, len >> 1);
288
+            free(color_memory);
289
+        } else {
290
+            struct color* pal_buffer = (struct color*) (0x02037ACC);
291
+            lz77u_wram(blockset->palette, pal_buffer);
292
+            for (u8 i = 7; i < 7 + 6; ++i)
293
+                apply_shaders(i, 0, pal_buffer);
294
+            apply_lighting(blockset, 0, pal_buffer);
295
+            gpu_pal_apply(pal_buffer, start, len);
296
+            load_palette_3(start, len >> 1);
297
+        }
298
+    } else {
299
+        struct color* color_memory = malloc(224);
300
+        memcpy(color_memory, blockset->palette, 224);
301
+        for (u8 i = 0; i < 7; ++i)
302
+            apply_shaders(i, 0, color_memory);
303
+        apply_lighting(blockset, 0, color_memory);
304
+        gpu_pal_apply(color_memory, start, len);
305
+        load_palette_3(start, len >> 1);
306
+        free(color_memory);
307
+    }
308
+    return;
309
+}
310
+
103 311
 void mapdata_load_palette_two(struct mapdata_header* data_header) {
104
-    blockset_load_palette_to_gpu(data_header->blockset_two, 0x70, 0xC0);
105
-    for (u8 i = 7; i < 7 + 6; ++i)
106
-        apply_shaders(i, 0);
107
-    apply_lighting(data_header->blockset_two, 0);
312
+    //struct color* destination = malloc(0x200);
313
+    //blockset_load_virtual_palette_and_shade(data_header->blockset_two, 0x70, 0xC0, destination);
314
+    //gpu_pal_apply(destination, 0x70, 0xC0);
315
+    //load_palette_3(0x70, 0xC0 >> 1);
316
+    blockset_load_palette_to_gpu_and_shade(data_header->blockset_two, 0x70, 0xC0);
108 317
     return;
109 318
 }
110 319
 
111 320
 void mapdata_load_palette_one(struct mapdata_header* data_header) {
112
-    blockset_load_palette_to_gpu(data_header->blockset_one, 0x0, 0xE0);
113
-    for (u8 i = 0; i < 7; ++i)
114
-        apply_shaders(i, 0);
115
-    apply_lighting(data_header->blockset_one, 0);
321
+    //struct color* destination = malloc(0x200);
322
+    //blockset_load_virtual_palette_and_shade(data_header->blockset_one, 0x0, 0xE0, destination);
323
+    //gpu_pal_apply(destination, 0x0, 0xE0);
324
+    //load_palette_3(0x0, 0xE0 >> 1);
325
+
326
+    blockset_load_palette_to_gpu_and_shade(data_header->blockset_one, 0x0, 0xE0);
116 327
     return;
117 328
 }
118 329
 
119
-void apply_lighting(void* blockset, u8 copy) 
120
-{
330
+void apply_lighting(void* blockset, u8 secondary, struct color* buffer) {
331
+    if (get_time_of_day() != NIGHT)
332
+        return;
121 333
     for (int i = 0; i < (sizeof (lightmap) / sizeof (lightmap[0])); ++i) {
122 334
         if (blockset == lightmap[i].blockset) {
123
-            palette_unfaded_buffer[lightmap[i].pal * 16 + lightmap[i].index] = lightmap[i].color;
335
+            buffer[(lightmap[i].pal - (secondary ? 7 : 0)) * 16 + lightmap[i].index] = lightmap[i].color;
124 336
         }
125 337
     }
126 338
     return;
127 339
 }
128 340
 
129
-void apply_shaders(u8 pal, u8 fade_copy) {
341
+void apply_shaders(u8 pal, u8 fade_copy, struct color* buffer) {
130 342
     if (tint_filter != 0)
131 343
         return;
132 344
     if (!is_current_map_dn_valid(current_mapheader.maptype))
133 345
         return;
134 346
     for (int i = pal * 16; i < (pal * 16) + 16; ++i) {
135 347
         struct color_shade current_shade = get_color_from_time(get_time_of_day());
136
-        palette_unfaded_buffer[i] = alpha_blend(palette_unfaded_buffer[i], current_shade.color, current_shade.alpha);
348
+        buffer[i] = alpha_blend(buffer[i], current_shade.color, current_shade.alpha);
137 349
     }
138 350
     if (fade_copy)
139 351
         copy_unfaded(pal);
@@ -161,7 +373,12 @@ struct color_shade get_color_from_time(enum time_type current_time) {
161 373
     switch (current_time) {
162 374
         case NIGHT:
163 375
             return color_shade_night;
376
+        case EVENING:
377
+            return color_shade_evening;
378
+        case MORNING:
379
+            return color_shade_morning;
164 380
         case DAY:
381
+
165 382
         default:
166 383
             return color_shade_day;
167 384
     }
@@ -185,6 +402,6 @@ void pal_patch_for_npc(u16 tag, u8 pal_slot) {
185 402
     u16 npc_map_entry = npc_pal_idx_for_given_tag(tag);
186 403
     gpu_pal_apply(npc_palettes[npc_map_entry].palette, (pal_slot + 16) * 16, 32);
187 404
     tint_palette(pal_slot);
188
-    apply_shaders(pal_slot + 16, 1);
405
+    apply_shaders(pal_slot + 16, 1, palette_unfaded_buffer);
189 406
 }
190 407
 /*use fade in animation, load palettes according to current fade state*/

+ 9
- 1
src/specials/custom_specials.c 查看文件

@@ -3,6 +3,13 @@
3 3
 #include "camera_move.h"
4 4
 #include <debug.h>
5 5
 
6
+void dns_switch()
7
+{
8
+    volatile u8* test_pointer = (u8*) (0x0203FAB0);
9
+    *(test_pointer+1) = (*(test_pointer+1) == 3 ? 0 : (*(test_pointer+1)) + 1);
10
+    *test_pointer = 1;
11
+}
12
+
6 13
 void init_script()
7 14
 {
8 15
   for(u16 i = 0x1000; i < 0x1800; ++i)
@@ -11,11 +18,12 @@ void init_script()
11 18
   }
12 19
 }
13 20
 
14
-static callback special_routines[4] = {
21
+static callback special_routines[5] = {
15 22
 	play_meteor,
16 23
 	sp_move_camera,
17 24
 	init_script,
18 25
         some_test,
26
+        dns_switch,
19 27
 };
20 28
 
21 29