-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
2890 lines (2531 loc) · 85.1 KB
/
main.cpp
File metadata and controls
2890 lines (2531 loc) · 85.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <stdint.h>
#include <algorithm>
#include <map>
#include <list>
#include <string>
#include <iostream>
#include "net.hpp"
#include "mullib.hpp"
#include "game.hpp"
#include "file.hpp"
#include "gfx.hpp"
#ifdef _LINUX
#include <SDL2/SDL.h>
#else
#include <SDL2/SDL.h>
#endif
extern int cnt_render_commands;
extern int cnt_use_program;
extern int cnt_bind_tex0;
extern int cnt_bind_tex1;
extern int cnt_glbegin;
extern int cnt_glend;
const int ping_frequency = 30000;
const int TYPE_LAND = 0;
const int TYPE_STATIC = 1;
const int TYPE_ITEM = 2;
const int TYPE_MOBILE = 3;
const int TYPE_GUMP = 4;
const int TYPE_GUMP_WIDGET = 5;
int window_width = 800;
int window_height = 600;
std::wstring chat_str;
struct pick_target_t
{
int type;
union
{
struct
{
int x, y, z;
int tile_id;
} land;
struct
{
int x, y, z;
int item_id;
} static_item;
struct
{
item_t *item;
} item;
struct
{
mobile_t *mobile;
} mobile;
struct
{
gump_t *gump;
} gump;
struct
{
gump_t *gump;
gump_widget_t *widget;
} gump_widget;
};
};
static pick_target_t pick_slots[0x10000];
static bool picking_enabled = false;
static int next_pick_id = 0;
const int DRAGTYPE_NONE = 0;
const int DRAGTYPE_ITEM = 1;
const int DRAGTYPE_GUMP = 2;
static struct
{
int type;
union
{
struct
{
uint32_t serial;
int item_id;
int hue_id;
} item;
struct
{
gump_t *gump;
int handle_x;
int handle_y;
} gump;
};
} dragging;
static int layer_draw_order[32] =
{
9, 20, 11, 16, 18, 4, 14, 3,
8, 24, 5, 13, 17, 10, 6, 19,
7, 22, 23, 12, 1, 2, 21, 15,
0, 25, 26, 27, 28, 29, 30, 31,
};
int pick_land(int x, int y, int z, int tile_id)
{
if (!picking_enabled)
{
return -1;
}
assert(next_pick_id < sizeof(pick_slots)/sizeof(pick_slots[0]));
pick_slots[next_pick_id].type = TYPE_LAND;
pick_slots[next_pick_id].land.x = x;
pick_slots[next_pick_id].land.y = y;
pick_slots[next_pick_id].land.z = z;
pick_slots[next_pick_id].land.tile_id = tile_id;
return next_pick_id++;
}
int pick_static(int x, int y, int z, int item_id)
{
if (!picking_enabled)
{
return -1;
}
assert(next_pick_id < sizeof(pick_slots)/sizeof(pick_slots[0]));
pick_slots[next_pick_id].type = TYPE_STATIC;
pick_slots[next_pick_id].static_item.x = x;
pick_slots[next_pick_id].static_item.y = y;
pick_slots[next_pick_id].static_item.z = z;
pick_slots[next_pick_id].static_item.item_id = item_id;
return next_pick_id++;
}
int pick_item(item_t *item)
{
if (!picking_enabled)
{
return -1;
}
assert(next_pick_id < sizeof(pick_slots)/sizeof(pick_slots[0]));
pick_slots[next_pick_id].type = TYPE_ITEM;
pick_slots[next_pick_id].item.item = item;
return next_pick_id++;
}
int pick_mobile(mobile_t *mobile)
{
if (!picking_enabled)
{
return -1;
}
assert(next_pick_id < sizeof(pick_slots)/sizeof(pick_slots[0]));
pick_slots[next_pick_id].type = TYPE_MOBILE;
pick_slots[next_pick_id].mobile.mobile = mobile;
return next_pick_id++;
}
int pick_gump(gump_t *gump)
{
if (!picking_enabled)
{
return -1;
}
assert(next_pick_id < sizeof(pick_slots)/sizeof(pick_slots[0]));
pick_slots[next_pick_id].type = TYPE_GUMP;
pick_slots[next_pick_id].gump.gump = gump;
return next_pick_id++;
}
int pick_gump_widget(gump_t *gump, gump_widget_t *widget)
{
if (!picking_enabled)
{
return -1;
}
assert(next_pick_id < sizeof(pick_slots)/sizeof(pick_slots[0]));
pick_slots[next_pick_id].type = TYPE_GUMP_WIDGET;
pick_slots[next_pick_id].gump_widget.gump = gump;
pick_slots[next_pick_id].gump_widget.widget = widget;
return next_pick_id++;
}
long now;
std::map <int, item_t *> items;
std::map <int, multi_t *> multis;
std::map <int, mobile_t *> mobiles;
std::list<gump_t *> gump_list;
int draw_ceiling = 128;
bool draw_roofs = true;
int prg_blit_picking;
int prg_blit_hue;
/* A simple function that prints a message, the error code returned by SDL,
* and quits the application */
void sdl_die(const char *msg)
{
printf("%s: %s\n", msg, SDL_GetError());
SDL_Quit();
exit(1);
}
void checkSDLError(int line = -1)
{
#ifndef NDEBUG
const char *error = SDL_GetError();
if (*error != '\0')
{
printf("SDL Error: %s\n", error);
if (line != -1)
printf(" + line: %i\n", line);
SDL_ClearError();
}
#endif
}
void dump_tga(const char *filename, int width, int height, void *argb1555_data)
{
uint16_t *data = (uint16_t *)argb1555_data;
int size = width * height;
uint8_t header[] =
{
0,
0,
2,
0, 0,
0, 0,
0,
0, 0,
0, 0,
(uint8_t)(width & 0xff),
(uint8_t)(width >> 8),
(uint8_t)(height & 0xff),
(uint8_t)(height >> 8),
24,
0x20
};
FILE *f = fopen(filename, "wb");
fwrite(header, 18, 1, f);
for (int i = 0; i < size; i++)
{
uint16_t pixel = data[i];
uint8_t a = (pixel >> 8) & 0x80;
uint8_t r = (pixel >> 7) & 0xf8;
uint8_t g = (pixel >> 2) & 0xf8;
uint8_t b = (pixel << 3) & 0xf8;
uint8_t buf[4] = { r, g, b, a };
fwrite(buf, 3, 1, f);
}
fclose(f);
}
struct anim_frame_t
{
int center_x, center_y;
pixel_storage_t ps;
};
static struct
{
struct
{
bool valid;
pixel_storage_t ps;
} entries[8*375];
} hue_cache;
static struct
{
struct
{
bool valid;
bool fetching;
int fetching_entries;
struct
{
int frame_count;
anim_frame_t *frames;
} directions[5];
} entries[1100*35]; // 1100 bodies, each with 35 actions
} anim_cache;
static struct
{
struct
{
bool valid;
bool fetching;
pixel_storage_t ps;
} entries[0x4000];
} land_cache;
static struct
{
struct
{
bool valid;
bool fetching;
pixel_storage_t ps;
} entries[0x10000];
} static_cache;
static struct
{
struct
{
bool valid;
bool fetching;
pixel_storage_t ps;
} entries[0x10000];
} gump_cache;
static struct
{
struct
{
bool valid;
bool fetching;
ml_multi *multi;
} entries[0x2000];
} multi_cache;
struct land_block_t
{
struct {
int tile_id;
int z;
} tiles[8*8];
};
static struct
{
struct
{
bool valid;
bool fetching;
int x, y;
land_block_t land_block;
} entries[8 * 8];
} land_block_cache;
struct statics_block_entry_t
{
int item_id;
int dx, dy, z;
};
struct statics_block_t
{
int statics_count;
int roof_heights[8*8];
statics_block_entry_t *statics;
};
static struct
{
struct
{
bool valid;
bool fetching;
int x, y;
statics_block_t statics_block;
} entries[8 * 8];
} statics_block_cache;
struct string_cache_entry_t
{
bool fetching;
pixel_storage_t ps;
};
static struct
{
std::map<std::wstring, string_cache_entry_t> entries;
} string_cache;
pixel_storage_t get_hue_tex(int hue_id)
{
assert(hue_id > 0 && hue_id < 8*375);
if (!hue_cache.entries[hue_id-1].valid)
{
hue_cache.entries[hue_id-1].valid = true;
ml_hue *hue = ml_get_hue(hue_id-1);
hue_cache.entries[hue_id-1].ps = gfx_upload_tex2d(32, 1, hue->colors);
}
return hue_cache.entries[hue_id-1].ps;
}
void write_anim_frames(int body_id, int action, int direction, ml_anim *anim)
{
int body_action_id = body_id * 35 + action;
anim_frame_t *frames = (anim_frame_t *)malloc(anim->frame_count * sizeof(anim_frame_t));
for (int j = 0; j < anim->frame_count; j++)
{
frames[j].center_x = anim->frames[j].center_x;
frames[j].center_y = anim->frames[j].center_y;
frames[j].ps = gfx_upload_tex2d(anim->frames[j].width, anim->frames[j].height, anim->frames[j].data);
}
anim_cache.entries[body_action_id].directions[direction].frame_count = anim->frame_count;
anim_cache.entries[body_action_id].directions[direction].frames = frames;
free(anim);
anim_cache.entries[body_action_id].fetching_entries -= 1;
assert(anim_cache.entries[body_action_id].fetching_entries >= 0);
if (anim_cache.entries[body_action_id].fetching_entries == 0)
{
anim_cache.entries[body_action_id].fetching = false;
}
}
anim_frame_t *get_anim_frames(int body_id, int action, int direction, int *frame_count)
{
assert(body_id >= 0 && body_id < 1100);
assert(action >= 0 && action < 35);
assert(direction >= 0 && direction < 5);
int body_action_id = body_id * 35 + action;
if (!anim_cache.entries[body_action_id].valid)
{
anim_cache.entries[body_action_id].valid = true;
anim_cache.entries[body_action_id].fetching = true;
anim_cache.entries[body_action_id].fetching_entries = 5;
for (int i = 0; i < 5; i++)
{
//printf("anim_cache : loading %d %d %d\n", body_id, action, i);
mlt_read_anim(body_id, action, i, write_anim_frames);
//write_anim_frames(body_id, action, i);
}
}
if (anim_cache.entries[body_action_id].fetching)
{
// TODO: return dummy item
return NULL;
}
else
{
*frame_count = anim_cache.entries[body_action_id].directions[direction].frame_count;
return anim_cache.entries[body_action_id].directions[direction].frames;
}
}
void write_land_ps(int land_id, ml_art *l)
{
land_cache.entries[land_id].ps = gfx_upload_tex2d(l->width, l->height, l->data);
free(l);
land_cache.entries[land_id].fetching = false;
}
pixel_storage_t *get_land_ps(int land_id)
{
assert(land_id >= 0 && land_id < 0x4000);
if (!land_cache.entries[land_id].valid)
{
land_cache.entries[land_id].valid = true;
land_cache.entries[land_id].fetching = true;
//printf("land_cache : loading %d\n", land_id);
//write_land_ps(land_id);
mlt_read_land_art(land_id, write_land_ps);
}
if (land_cache.entries[land_id].fetching)
{
return NULL;
}
else
{
return &land_cache.entries[land_id].ps;
}
}
void write_static_ps(int item_id, ml_art *s)
{
static_cache.entries[item_id].ps = gfx_upload_tex2d(s->width, s->height, s->data);
free(s);
static_cache.entries[item_id].fetching = false;
}
pixel_storage_t *get_static_ps(int item_id)
{
assert(item_id >= 0 && item_id < 0x10000);
if (!static_cache.entries[item_id].valid)
{
static_cache.entries[item_id].valid = true;
static_cache.entries[item_id].fetching = true;
//printf("static_cache : loading %d\n", item_id);
//async_get_static_ps(item_id);
mlt_read_static_art(item_id, write_static_ps);
}
if (static_cache.entries[item_id].fetching)
{
return NULL;
}
else
{
return &static_cache.entries[item_id].ps;
}
}
void write_gump_ps(int gump_id, ml_gump *g)
{
gump_cache.entries[gump_id].ps = gfx_upload_tex2d(g->width, g->height, g->data);
free(g);
gump_cache.entries[gump_id].fetching = false;
}
pixel_storage_t *get_gump_ps(int gump_id)
{
assert(gump_id >= 0 && gump_id < 0x10000);
if (!gump_cache.entries[gump_id].valid)
{
gump_cache.entries[gump_id].valid = true;
gump_cache.entries[gump_id].fetching = true;
//printf("gump_cache : loading %d\n", gump_id);
mlt_read_gump(gump_id, write_gump_ps);
}
if (gump_cache.entries[gump_id].fetching)
{
return NULL;
}
else
{
return &gump_cache.entries[gump_id].ps;
}
}
void write_multi(int multi_id, ml_multi *m)
{
multi_cache.entries[multi_id].multi = m;
multi_cache.entries[multi_id].fetching = false;
}
ml_multi *get_multi(int multi_id)
{
assert(multi_id >= 0 && multi_id < 0x10000);
if (!multi_cache.entries[multi_id].valid)
{
multi_cache.entries[multi_id].valid = true;
multi_cache.entries[multi_id].fetching = true;
printf("multi_cache : loading %d\n", multi_id);
write_multi(multi_id, ml_read_multi(multi_id));
}
if (multi_cache.entries[multi_id].fetching)
{
return NULL;
}
else
{
return multi_cache.entries[multi_id].multi;
}
}
void write_land_block(int map, int block_x, int block_y, ml_land_block *lb)
{
int cache_block_x = block_x % 8;
int cache_block_y = block_y % 8;
int cache_block_index = cache_block_x + cache_block_y * 8;
for (int i = 0; i < 8 * 8; i++)
{
land_block_cache.entries[cache_block_index].land_block.tiles[i].tile_id = lb->tiles[i].tile_id;
land_block_cache.entries[cache_block_index].land_block.tiles[i].z = lb->tiles[i].z;
}
free(lb);
land_block_cache.entries[cache_block_index].fetching = false;
}
land_block_t *get_land_block(int map, int block_x, int block_y)
{
// TODO: remove these assumptions
assert(map == 0 || map == 1);
assert(block_x >= 0 && block_x < 896);
assert(block_y >= 0 && block_y < 512);
int cache_block_x = block_x % 8;
int cache_block_y = block_y % 8;
// TODO: also care about map in the cache lookup
int cache_block_index = cache_block_x + cache_block_y * 8;
// TODO: add thrashing check
if (!land_block_cache.entries[cache_block_index].valid ||
land_block_cache.entries[cache_block_index].x != block_x ||
land_block_cache.entries[cache_block_index].y != block_y)
{
land_block_cache.entries[cache_block_index].valid = true;
land_block_cache.entries[cache_block_index].fetching = true;
land_block_cache.entries[cache_block_index].x = block_x;
land_block_cache.entries[cache_block_index].y = block_y;
//printf("land_block_cache : loading %d %d %d\n", map, block_x, block_y);
mlt_read_land_block(map, block_x, block_y, write_land_block);
}
if (land_block_cache.entries[cache_block_index].fetching)
{
return NULL;
}
else
{
return &land_block_cache.entries[cache_block_index].land_block;
}
}
void write_statics_block(int map, int block_x, int block_y, ml_statics_block *sb)
{
int cache_block_x = block_x % 8;
int cache_block_y = block_y % 8;
// TODO: also care about map in the cache lookup
int cache_block_index = cache_block_x + cache_block_y * 8;
statics_block_cache.entries[cache_block_index].statics_block.statics_count = sb->statics_count;
statics_block_cache.entries[cache_block_index].statics_block.statics = (statics_block_entry_t *)malloc(sb->statics_count * sizeof(statics_block_entry_t));
for (int i = 0; i < 8*8; i++)
{
statics_block_cache.entries[cache_block_index].statics_block.roof_heights[i] = sb->roof_heights[i];
}
for (int i = 0; i < sb->statics_count; i++)
{
statics_block_cache.entries[cache_block_index].statics_block.statics[i].item_id = sb->statics[i].tile_id;
statics_block_cache.entries[cache_block_index].statics_block.statics[i].dx = sb->statics[i].dx;
statics_block_cache.entries[cache_block_index].statics_block.statics[i].dy = sb->statics[i].dy;
statics_block_cache.entries[cache_block_index].statics_block.statics[i].z = sb->statics[i].z;
}
free(sb);
statics_block_cache.entries[cache_block_index].fetching = false;
}
statics_block_t *get_statics_block(int map, int block_x, int block_y)
{
// TODO: remove these assumptions
assert(map == 0 || map == 1);
assert(block_x >= 0 && block_x < 896);
assert(block_y >= 0 && block_y < 512);
int cache_block_x = block_x % 8;
int cache_block_y = block_y % 8;
int cache_block_index = cache_block_x + cache_block_y * 8;
// TODO: add thrashing check
if (!statics_block_cache.entries[cache_block_index].valid ||
statics_block_cache.entries[cache_block_index].x != block_x ||
statics_block_cache.entries[cache_block_index].y != block_y)
{
// if we're throwing an old block out, free its resources!
if (statics_block_cache.entries[cache_block_index].valid)
{
free(statics_block_cache.entries[cache_block_index].statics_block.statics);
}
statics_block_cache.entries[cache_block_index].valid = true;
statics_block_cache.entries[cache_block_index].fetching = true;
statics_block_cache.entries[cache_block_index].x = block_x;
statics_block_cache.entries[cache_block_index].y = block_y;
//printf("statics_block_cache: loading %d %d %d\n", map, block_x, block_y);
mlt_read_statics_block(map, block_x, block_y, write_statics_block);
}
if (statics_block_cache.entries[cache_block_index].fetching)
{
return NULL;
}
else
{
return &statics_block_cache.entries[cache_block_index].statics_block;
}
}
void write_string_ps(int font_id, std::wstring str, ml_art *a)
{
string_cache.entries[str].ps = gfx_upload_tex2d(a->width, a->height, a->data);
free(a);
string_cache.entries[str].fetching = false;
}
pixel_storage_t *get_string_ps(int font_id, std::wstring str)
{
std::map<std::wstring, string_cache_entry_t>::iterator it = string_cache.entries.find(str);
if (it == string_cache.entries.end())
{
string_cache_entry_t entry;
entry.fetching = true;
string_cache.entries[str] = entry;
it = string_cache.entries.find(str);
//std::wcout << "string_cache : loading " << str << std::endl;
//mlt_read_string(font_id, str, write_string_ps);
write_string_ps(font_id, str, ml_render_string(font_id, str));
}
if (it->second.fetching)
{
return NULL;
}
else
{
return &it->second.ps;
}
}
void blit_ps(pixel_storage_t *ps, int x, int y, int draw_prio, int hue_id, int pick_id)
{
int xs[4] = { x, x + ps->width, x + ps->width, x };
int ys[4] = { y, y, y + ps->height, y + ps->height };
gfx_render(ps, xs, ys, draw_prio, hue_id, pick_id);
}
void blit_ps_flipped(pixel_storage_t *ps, int x, int y, int draw_prio, int hue_id, int pick_id)
{
int xs[4] = { x + ps->width, x, x, x + ps->width };
int ys[4] = { y, y, y + ps->height, y + ps->height };
gfx_render(ps, xs, ys, draw_prio, hue_id, pick_id);
}
mobile_t player =
{
12345, // serial
401, // body_id
1212, 1537, 0, // xyz
0, // dir
0, // hue
0, // noto
0, // flags
NULL, // paperdoll gump
{NULL}, // equipped items
{0}, // speech
0, // last_dir
0, // last_movement
4, // action_id
};
// Lord British' throne
//int player.x = 1323;
//int player.y = 1624;
// secret practice place
//int player.x = 1212;
//int player.y = 1537;
//int player.z = 0;
int screen_center_x = window_width / 2;
int screen_center_y = window_height / 2;
void world_to_screen(int world_x, int world_y, int world_z, int *screen_x, int *screen_y)
{
//int player.x = 3;
//int player.y = 3;
//int player.z = 0;
int world_dx = world_x - player.x;
int world_dy = world_y - player.y;
int world_dz = world_z - player.z;
int screen_dx = world_dx * 22 - world_dy * 22;
int screen_dy = world_dx * 22 + world_dy * 22 - world_dz * 4;
*screen_x = screen_center_x + screen_dx;
*screen_y = screen_center_y + screen_dy;
}
int world_draw_prio(int world_x, int world_y, int world_z)
{
int world_dx = world_x - player.x;
int world_dy = world_y - player.y;
world_dx += 32;
world_dy += 32;
world_z += 128;
assert(world_dx >= 0 && world_dx < 64);
assert(world_dy >= 0 && world_dy < 64);
assert(world_z >= 0 && world_z < 256);
// 32 layers, 2 types of foliage (is foliage/is not foliage)
int prio = 2 * 32 * (64 * world_dx + 64 * world_dy + world_z);
return prio;
}
int land_block_z_slow(int x, int y)
{
int block_x = x / 8;
int block_y = y / 8;
int dx = x % 8;
int dy = y % 8;
land_block_t *lb = get_land_block(1, block_x, block_y);
if (lb)
{
int z = lb->tiles[dx + dy * 8].z;
return z;
}
else
{
return 0;
}
}
int land_block_z_averaged(int x, int y)
{
int z_sum = 0;
const int dxs[4] = { 0, 1, 1, 0 };
const int dys[4] = { 0, 0, 1, 1 };
for (int i = 0; i < 4; i++)
{
z_sum += land_block_z_slow(x + dxs[i], y + dys[i]);
}
return z_sum / 4;
}
// this might be optimized when it comes to z calculations
void draw_world_land_ps(pixel_storage_t *ps, int x, int y, int pick_id)
{
int dxs[4] = { 0, 1, 1, 0 };
int dys[4] = { 0, 0, 1, 1 };
int xs[4];
int ys[4];
int zs[4];
for (int i = 0; i < 4; i++)
{
zs[i] = land_block_z_slow(x + dxs[i], y + dys[i]);
}
for (int i = 0; i < 4; i++)
{
int screen_x;
int screen_y;
world_to_screen(x + dxs[i], y + dys[i], zs[i], &screen_x, &screen_y);
// land tiles are shifted half a world unit upwards
screen_y -= 22;
xs[i] = screen_x;
ys[i] = screen_y;
}
// find smallest z and use that for prio order
// seems to work ok...
int min_z = zs[0];
for (int i = 1; i < 4; i++)
{
if (zs[i] < min_z)
{
min_z = zs[i];
}
}
gfx_render(ps, xs, ys, world_draw_prio(x, y, min_z-1), 0, pick_id);
}
void draw_world_art_ps(pixel_storage_t *ps, int x, int y, int z, int height, int bonus_prio, int hue_id, int pick_id)
{
if (z >= draw_ceiling)
{
return;
}
int screen_x;
int screen_y;
world_to_screen(x, y, z, &screen_x, &screen_y);
// offset x by art's width/2
screen_x -= ps->width / 2;
// offset y by art's height
screen_y -= ps->height;
// statics tiles are shifted half a world unit downwards
screen_y += 22;
blit_ps(ps, screen_x, screen_y, world_draw_prio(x, y, z) + bonus_prio, hue_id, pick_id);
}
static int gump_draw_order = 0;
void draw_screen_text(int x, int y, int font_id, int align_x, std::wstring s, int pick_id)
{
pixel_storage_t *ps = get_string_ps(font_id, s);
// TODO: this null check shouldn't be necessary
if (ps)
{
int draw_x;
if (align_x < 0) draw_x = x;
else if (align_x == 0) draw_x = x - ps->width / 2;
else /*if (align_x > 0)*/ draw_x = x - ps->width;
blit_ps(ps, draw_x, y, gump_draw_order++, 0, pick_id);
}
}
void draw_world_text(int x, int y, int z, int row_offset, int font_id, std::wstring s, int pick_id)
{
int screen_x;
int screen_y;
world_to_screen(x, y, z, &screen_x, &screen_y);
draw_screen_text(screen_x, screen_y - row_offset, font_id, 0, s, pick_id);
}
void draw_world_anim_frame(anim_frame_t *frame, bool flip, int x, int y, int z, int layer_prio, int hue_id, int pick_id)
{
pixel_storage_t *ps = &frame->ps;
int screen_x;
int screen_y;
world_to_screen(x, y, z, &screen_x, &screen_y);
// offset by frame's center in y
screen_y -= frame->center_y;
// ...and offset y by frame's height
screen_y -= ps->height;
if (flip)
{
// offset by frame's center in x
screen_x -= (frame->ps.width - frame->center_x - 1);
blit_ps_flipped(ps, screen_x, screen_y, world_draw_prio(x, y, z) + layer_prio, hue_id, pick_id);
}
else
{
// offset by frame's center in x
screen_x -= frame->center_x;
blit_ps(ps, screen_x, screen_y, world_draw_prio(x, y, z) + layer_prio, hue_id, pick_id);
}
}
void draw_world_land(int tile_id, int x, int y, int pick_id)
{
pixel_storage_t *ps = get_land_ps(tile_id);
// TODO: this null check shouldn't be necessary
if (ps)
{
draw_world_land_ps(ps, x, y, pick_id);
}
}
void draw_world_land_block(int map, int block_x, int block_y)
{
land_block_t *lb = get_land_block(map, block_x, block_y);
// TODO: this null check shouldn't be necessary
if (lb)
{
for (int dy = 0; dy < 8; dy++)
for (int dx = 0; dx < 8; dx++)
{
int tile_id = lb->tiles[dx + dy * 8].tile_id;
int z = lb->tiles[dx + dy * 8].z;
int x = 8 * block_x + dx;