-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm_world.c
3098 lines (2735 loc) · 91.7 KB
/
dm_world.c
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
// Contains all definitions for the world
#include "mt_rand.h"
#include "dm_algorithm.h"
#include "dm_debug.h"
#include <stdlib.h>
#include <ncurses.h>
#include <math.h>
#include "dm_world.h"
#include "dm_dungeon.h"
///////////////////////////
// RAW DATA
#define MALLOC_ITEM_SIZE 10
#define MALLOC_MOB_SIZE 10
#define WLD_COLOR_BASE 100
#define WLD_COLOR_COUNT 8
int curses_colors[] = {
COLOR_BLACK, // 0
COLOR_GREEN, // 1
COLOR_WHITE, // 2
COLOR_RED, // 3
COLOR_BLUE, // 4
COLOR_CYAN, // 5
COLOR_YELLOW, // 6
COLOR_MAGENTA, // 7
};
struct wld_tiletype *wld_tiletypes;
struct wld_mobtype *wld_mobtypes;
struct wld_itemtype *wld_itemtypes;
struct wld_effecttype *wld_effecttypes;
char* wld_tutorials[] = {
// movement
"TUTORIAL: Movement\n\n"
"You can move yourself (@) with keys:\n"
"\n"
"q: up-left w: up e: up-right\n"
"a: left d: right\n"
"z: down-left s: down x: down-right\n"
"\n"
"Moving over items picks them up, over doors attempts to open them, "
"into enemies attempts to attack them.",
// ghost
"TUTORIAL: Ghost Control\n\n"
"Your ghost (#) follows you as a focus point. Use it to observe, aim, or interact. "
"You can control it with arrow keys or numpad:\n"
"\n"
"7: up-left 8: up 9: up-right\n"
"4: left 6: right\n"
"1: down-left 2: down 3: down-right",
// commands
"TUTORIAL: Commands\n\n"
"Your situation context can illicit different commands with certain keys. "
"Some common commands are:\n"
"\n"
"i: inventory g: get item\n"
"p: rest y: aim equipped weapon\n"
"escape: menu",
// inventory
"TUTORIAL: Inventory\n\n"
"You can open your inventory with \"i\"\n"
"\n"
"Your inventory holds items you find. Items are automaticallly picked up when you walk over them or by pressing \"g\". "
"Opening an item in the inventory provides options for use. "
"Some items can be quaffed (drank), some cast, some worn as armor, some worn as weapons, some thrown. "
"Some items have a limited number of uses, such as scrolls or ammunition.",
// combat
"TUTORIAL: Combat\n\n"
"With no equipped weapons you are capable of only unarmed melee attacks. "
"You can equip one weapon at a time in your inventory. Once equipped you can "
"weild it for combat with \"y\".\n"
"\n"
"Melee weapons can automatically attack adjacent enemies you move into. Ranged "
"weapons must be wielded to use. While weilding, your weapon's targetable area is shown. "
"For ranged weapons your ghost will act as your direction and point of attack.",
};
///////////////////////////
// WORLD METHODS START
struct wld_world* wld_new_world(int seed, int count)
{
dm_seed(seed);
struct wld_world* world = (struct wld_world*)malloc(sizeof(struct wld_world));
world->seed;
world->maps_length = count;
world->maps = (struct wld_map**)malloc(world->maps_length * sizeof(struct wld_map*));
struct dng_dungeon* dungeon = dng_gendungeon(seed, world->maps_length, false);
for (int i=0; i < dungeon->maps_length; i++) {
// convert dungeon maps to game maps
// iterate the cellmap
struct dng_cellmap* cellmap = dungeon->maps[i];
struct wld_map* map = wld_new_map(i, cellmap->difficulty, cellmap->width, cellmap->height);
map->world = world;
map->is_first_map = (i == 0);
// populate
wld_generate_tiles(map, cellmap);
wld_generate_mobs(map, cellmap);
wld_generate_items(map, cellmap);
world->maps[i] = map;
}
dng_deldungeon(dungeon);
return world;
}
void wld_delete_world(struct wld_world* world)
{
for (int i=0; i < world->maps_length; i++) {
wld_delete_map(world->maps[i]);
}
free(world->maps);
free(world);
}
void wld_transition_player(struct wld_world* world, struct wld_map* from_map, struct wld_map* to_map, bool at_entrance)
{
// remove player instance from current map and move him to the next map
struct wld_mob* mob = from_map->player;
wld_map_remove_mob(from_map, from_map->player);
if (at_entrance)
wld_map_add_mob_at_entrance(to_map, mob);
else
wld_map_add_mob_at_exit(to_map, mob);
to_map->player = mob;
to_map->cursor->x = mob->map_x + 1;
to_map->cursor->y = mob->map_y + 1;
}
void wld_setup()
{
// build color maps of all bg/fg color pairs
for (int i=0; i < WLD_COLOR_COUNT; i++) {
for (int j=0; j < WLD_COLOR_COUNT; j++) {
int index = j * WLD_COLOR_COUNT + i;
int colorpair_id = WLD_COLOR_BASE + index;
init_pair(colorpair_id, curses_colors[j], curses_colors[i]);
}
}
// copy tiletypes into malloc
struct wld_tiletype tts[] = { // bg fg membg memfg block transformable short desc, title
{ TILE_VOID, ' ', WCLR_BLACK, WCLR_BLACK, ' ', WCLR_BLACK, WCLR_BLACK, false, true, "", "" },
{ TILE_GRASS, '"', WCLR_BLACK, WCLR_GREEN, '"', WCLR_BLACK, WCLR_BLUE, false, true, "a small tuft of grass", "Grass" },
{ TILE_WATER, '~', WCLR_BLUE, WCLR_BLUE, '~', WCLR_BLACK, WCLR_BLUE, false, true, "a pool of water glistens", "Water" },
{ TILE_TREE, 'T', WCLR_BLACK, WCLR_GREEN, 'T', WCLR_BLACK, WCLR_BLUE, false, true, "a large tree", "Tree" },
{ TILE_STONEWALL, '#', WCLR_WHITE, WCLR_WHITE, '#', WCLR_BLACK, WCLR_BLUE, true, false, "a rough stone wall", "Stone Wall" },
{ TILE_STONEFLOOR, '.', WCLR_BLACK, WCLR_WHITE, '.', WCLR_BLACK, WCLR_BLUE, false, true, "a rough stone floor", "Stone Floor" },
{ TILE_ENTRANCE, '>', WCLR_BLACK, WCLR_CYAN, '>', WCLR_BLACK, WCLR_CYAN, false, false, "the entrance back up", "Entrance" },
{ TILE_EXIT, '<', WCLR_BLACK, WCLR_CYAN, '<', WCLR_BLACK, WCLR_CYAN, false, false, "an exit further down", "Exit" },
{ TILE_STONEDOOR, '+', WCLR_BLACK, WCLR_WHITE, '+', WCLR_BLACK, WCLR_BLUE, false, false, "an old stone door", "Stone Door" },
{ TILE_DEEPWATER, ' ', WCLR_BLACK, WCLR_BLACK, '~', WCLR_BLACK, WCLR_BLUE, false, false, "dark and deep water", "Deep Water" },
{ TILE_SUMMONCIRCLE_SF_INERT, '.', WCLR_BLACK, WCLR_WHITE, '.', WCLR_BLACK, WCLR_BLUE, false, true, "a strange stone floor", "Strange Stone Floor" },
{ TILE_SUMMONCIRCLE_ACTIVE, '0', WCLR_BLACK, WCLR_CYAN, '0', WCLR_BLACK, WCLR_BLUE, false, true, "a runic summon center", "Summon Circle" },
{ TILE_SUMMONCIRCLE_NODE, '+', WCLR_BLACK, WCLR_CYAN, '+', WCLR_BLACK, WCLR_BLUE, false, true, "a runic summon node", "Summon Node" },
{ TILE_TUTORIAL_STONE, '=', WCLR_BLACK, WCLR_CYAN, '=', WCLR_BLACK, WCLR_BLUE, false, true, "a guidestone", "Guidestone" },
};
wld_tiletypes = (struct wld_tiletype*)malloc(ARRAY_SIZE(tts) * sizeof(struct wld_tiletype));
for (int i=0; i<ARRAY_SIZE(tts); i++) {
wld_tiletypes[i].type = tts[i].type;
wld_tiletypes[i].sprite = tts[i].sprite;
wld_tiletypes[i].fg_color = tts[i].fg_color;
wld_tiletypes[i].bg_color = tts[i].bg_color;
wld_tiletypes[i].memory_sprite = tts[i].memory_sprite;
wld_tiletypes[i].memory_fg_color = tts[i].memory_fg_color;
wld_tiletypes[i].memory_bg_color = tts[i].memory_bg_color;
wld_tiletypes[i].is_block = tts[i].is_block;
wld_tiletypes[i].is_transformable = tts[i].is_transformable;
wld_tiletypes[i].short_desc = tts[i].short_desc;
wld_tiletypes[i].title = tts[i].title;
}
// copy mob types into malloc
struct wld_mobtype mts [] = {
// hp, difficuly sprite, color, desc, title
{ MOB_VOID, 0, 0, 0, ' ', WCLR_BLACK, "", "" },
{ MOB_PLAYER, 100, 20, 0, '@', WCLR_MAGENTA, "yourself", "You" },
{ MOB_NPC_MOTHER, 100, 20, 0, '@', WCLR_CYAN, "mother", "Mother" },
// mobs elevating in difficulty
// name hp vision dffclty sprite color short desc title
{ MOB_RAT, 5, 7, 1, 'r', WCLR_RED, "a hideous rat", "rat" },
{ MOB_JACKAL, 35, 20, 3, 'j', WCLR_RED, "a small jackal", "jackal" },
};
wld_mobtypes = (struct wld_mobtype*)malloc(ARRAY_SIZE(mts) * sizeof(struct wld_mobtype));
for (int i=0; i<ARRAY_SIZE(mts); i++) {
wld_mobtypes[i].type = mts[i].type;
wld_mobtypes[i].base_health = mts[i].base_health;
wld_mobtypes[i].base_vision = mts[i].base_vision;
wld_mobtypes[i].difficulty = mts[i].difficulty;
wld_mobtypes[i].sprite = mts[i].sprite;
wld_mobtypes[i].fg_color = mts[i].fg_color;
wld_mobtypes[i].short_desc = mts[i].short_desc;
wld_mobtypes[i].title = mts[i].title;
}
// copy item types into malloc
struct wld_itemtype its [] = {
{
ITEM_VOID,
' ',
WCLR_BLACK,
false,false,false,false, // weapon, armor, key, ammo
"",
"",
NULL,
NULL,
NULL,
NULL, // coh
NULL,
NULL,
0,0,//range radius
false, 0, // uses
0,0,//min max dmgs
ITEM_VOID,
"",
"",
""
},
{
ITEM_POTION_MINOR_HEAL,
';',
WCLR_YELLOW,
false,false,false,false, // weapon, armor, key, ammo
"a potion of minor healing",
"minor healing potion",
itm_drink_minorhealth,
itm_target_ranged_los,
itm_can_use_ranged_los,
itm_coh_ranged, // coh
itm_use_ranged_los,
itm_hit_minorhealth,
5,0, // range radius
true,
1,
6,22,//min max dmgs
ITEM_VOID, // ammo type
"quaff",
"throw",
/////////////////////////////////////////////////////////
"The glass of the potion is warm to the touch. Its properties should heal a small amount."
},
{
ITEM_WEAPON_SHORTSWORD,
'/',
WCLR_YELLOW,
true,false,false,false, // weapon, armor, key, ammo
"a shortsword",
"shortsword",
NULL,
itm_target_melee,
itm_can_use_melee,
itm_coh_melee, // coh
itm_use_melee,
itm_hit_melee_swordstyle,
1,0, // range radius
false, 0, // uses
7,16,//min max dmgs
ITEM_VOID, // ammo type
"",
"strike",
/////////////////////////////////////////////////////////
"Though short, its sharp point could plunge deeply into a soft skinned enemy."
},
{
ITEM_WEAPON_SHORTBOW,
')',
WCLR_YELLOW,
true,false,false,false, // weapon, armor, key, ammo
"a shortbow",
"shortbow",
NULL,
itm_target_ranged_los,
itm_can_use_ranged_los,
itm_coh_ranged, // coh
itm_use_ranged_los,
itm_hit_ranged_los_bowstyle,
10,0, // range radius
false, 0, // uses
5,13,//min max dmgs
ITEM_AMMO_ARROW, // ammo type
"",
"shoot",
/////////////////////////////////////////////////////////
"Its string has been worn but the wood is strong, this small bow could fell small creatures."
},
{
ITEM_AMMO_ARROW,
'1',
WCLR_YELLOW,
false,false,false,true, // weapon, armor, key, ammo
"some arrows",
"arrows",
NULL,
NULL, // target
NULL, // can use
NULL, // chance of hit
NULL, // use
NULL, // hit
0,0, // range radius
true, 1, // uses
5,13,//min max dmgs
ITEM_VOID, // ammo type
"",
"shoot",
/////////////////////////////////////////////////////////
"The shafts are straight and the fletching secure but the wood appears aged and brittle. It may be good for one shot."
},
{
ITEM_SCROLL_FIREBOMB,
'=',
WCLR_YELLOW,
false,false,false,false, // weapon, armor, key, ammo
"a scroll of firebomb",
"scroll of firebomb",
NULL,
itm_target_ranged_aoe,
itm_can_use_ranged_aoe,
NULL, // coh
itm_use_ranged_aoe,
itm_hit_ranged_aoe_firebomb,
9,3, // base range, radius
true,1,//uses
2,8,//min max dmgs (lower because of fire effect)
ITEM_VOID, // ammo type
"", // consume
"cast", // use
/////////////////////////////////////////////////////////
"{Daedum Kalkuum Brios Faraos} This runic scroll's surface shows a large swathe of fire in a circular arc."
},
{
ITEM_ARMOR_LEATHER,
'M',
WCLR_YELLOW,
false,true,false,false, // weapon, armor, key, ammo
"a set of leather armor",
"leather armor",
NULL,
NULL,
NULL,
NULL, // coh
NULL,
NULL,
0,0, // range radius
false,0, // uses
0,0,//min max dmgs
ITEM_VOID, // ammo type
"",
"",
/////////////////////////////////////////////////////////
"Humble but sturdy this set of leather armor is a rogue's favorite friend."
},
{
ITEM_KEY_BASIC, '*',
WCLR_YELLOW,
false,false,true,false, // weapon, armor, key, ammo
"a small bronze key",
"bronze key",
NULL,
itm_target_key,
itm_can_use_key,
NULL, // coh
itm_use_key,
itm_hit_key,
1,0, // range, radius
true,1, // uses (key uses increment on failure)
0,0,//min max dmgs
ITEM_VOID, // ammo type
"",
"use",
/////////////////////////////////////////////////////////
"This lost, tarnished bronze key may fit a lock to a nearby door or chest."
},
};
wld_itemtypes = (struct wld_itemtype*)malloc(ARRAY_SIZE(its) * sizeof(struct wld_itemtype));
for (int i=0; i<ARRAY_SIZE(its); i++) {
wld_itemtypes[i].type = its[i].type;
wld_itemtypes[i].sprite = its[i].sprite;
wld_itemtypes[i].fg_color = its[i].fg_color;
wld_itemtypes[i].is_weq = its[i].is_weq;
wld_itemtypes[i].is_aeq = its[i].is_aeq;
wld_itemtypes[i].is_key = its[i].is_key;
wld_itemtypes[i].is_ammo = its[i].is_ammo;
wld_itemtypes[i].short_desc = its[i].short_desc;
wld_itemtypes[i].title = its[i].title;
wld_itemtypes[i].fn_drink = its[i].fn_drink;
wld_itemtypes[i].fn_target = its[i].fn_target;
wld_itemtypes[i].fn_can_use = its[i].fn_can_use;
wld_itemtypes[i].fn_coh = its[i].fn_coh;
wld_itemtypes[i].fn_use = its[i].fn_use;
wld_itemtypes[i].fn_hit = its[i].fn_hit;
wld_itemtypes[i].base_range = its[i].base_range;
wld_itemtypes[i].base_radius = its[i].base_radius;
wld_itemtypes[i].has_uses = its[i].has_uses;
wld_itemtypes[i].base_uses = its[i].base_uses;
wld_itemtypes[i].min_val = its[i].min_val;
wld_itemtypes[i].max_val = its[i].max_val;
wld_itemtypes[i].ammo_type = its[i].ammo_type;
wld_itemtypes[i].drink_label = its[i].drink_label;
wld_itemtypes[i].use_label = its[i].use_label;
wld_itemtypes[i].use_text = its[i].use_text;
}
// Copy effect types into malloc
struct wld_effecttype ets [] = {
// iters, sprite, color, desc, reveal, function title
{ EFFECT_FIRE, 5, '^', WCLR_YELLOW, -1, true, wld_effect_on_fire, "fire" },
{ EFFECT_VISION_BOOST, 20, ' ', WCLR_YELLOW, -1, false, wld_effect_vision_boost, "vision boost" },
};
wld_effecttypes = (struct wld_effecttype*)malloc(ARRAY_SIZE(ets) * sizeof(struct wld_effecttype));
for (int i=0; i<ARRAY_SIZE(ets); i++) {
wld_effecttypes[i].type = ets[i].type;
wld_effecttypes[i].iterations = ets[i].iterations;
wld_effecttypes[i].sprite = ets[i].sprite;
wld_effecttypes[i].fg_color = ets[i].fg_color;
wld_effecttypes[i].bg_color = ets[i].bg_color;
wld_effecttypes[i].reveal = ets[i].reveal;
wld_effecttypes[i].on_update_mob = ets[i].on_update_mob;
wld_effecttypes[i].title = ets[i].title;
}
}
void wld_teardown()
{
free(wld_effecttypes);
free(wld_itemtypes);
free(wld_mobtypes);
free(wld_tiletypes);
}
char* wld_get_tut_1()
{
return wld_tutorials[0];
}
// WORLD METHODS END
///////////////////////////
///////////////////////////
// GENERATORS START
struct wld_mob* gen_mob_player(struct wld_map* map, int c, int r)
{
struct wld_mob *mob = wld_new_mob(map, MOB_PLAYER, c, r);
mob->is_player = true;
mob->mutate_ding = XP_START;
mob->mutate_drain_rate = 3; // every three cycles
mob->heal_rate = 5;
map->player = mob; // assign to map specifically
// lets give him some items for playtesting
mob->inventory[0] = (struct wld_item*)malloc(sizeof(struct wld_item));
wld_init_item(mob->inventory[0], ITEM_WEAPON_SHORTSWORD);
mob->inventory[2] = (struct wld_item*)malloc(sizeof(struct wld_item));
wld_init_item(mob->inventory[2], ITEM_SCROLL_FIREBOMB);
mob->inventory[3] = (struct wld_item*)malloc(sizeof(struct wld_item));
wld_init_item(mob->inventory[3], ITEM_WEAPON_SHORTBOW);
mob->inventory[4] = (struct wld_item*)malloc(sizeof(struct wld_item));
wld_init_item(mob->inventory[4], ITEM_POTION_MINOR_HEAL);
mob->inventory[5] = (struct wld_item*)malloc(sizeof(struct wld_item));
wld_init_item(mob->inventory[5], ITEM_WEAPON_SHORTBOW);
mob->stat_strength = dm_randii(5, 16);
mob->stat_dexterity = dm_randii(5, 16);
mob->stat_constitution = dm_randii(5, 16);
rpg_apply_stats(mob);
mob->health = mob->maxhealth;
return mob;
}
struct wld_mob* gen_mob_npc_mother(struct wld_map* map, int c, int r)
{
struct wld_mob *mob = wld_new_mob(map, MOB_NPC_MOTHER, c, r);
mob->ai_wander = ai_default_wander; // moves around TODO make this wander until within range of player, then stand still
mob->ai_converse = ai_npc_intro_converse;
mob->stat_strength = dm_randii(2, 5);
mob->stat_dexterity = dm_randii(2, 5);
mob->stat_constitution = dm_randii(2, 4);
mob->static_xp_gain = XP_START;
rpg_apply_stats(mob);
mob->health = mob->maxhealth;
return mob;
}
struct wld_mob* gen_mob_rat(struct wld_map* map, int c, int r)
{
struct wld_mob *mob = wld_new_mob(map, MOB_RAT, c, r);
mob->ai_wander = ai_default_wander;
mob->ai_is_hostile = ai_is_hostile_player;
mob->ai_detect_combat = ai_detect_combat_visible_hostile;
mob->ai_decide_combat = ai_decide_combat_melee_with_flee;
mob->ai_attacked = ai_attacked_trigger_hive;
mob->flee_threshold = .5;
mob->stat_strength = dm_randii(2, 8);
mob->stat_dexterity = dm_randii(3, 8);
mob->stat_constitution = dm_randii(4, 8);
rpg_apply_stats(mob);
mob->health = mob->maxhealth;
return mob;
}
struct wld_mob* gen_mob_jackal(struct wld_map* map, int c, int r)
{
struct wld_mob *mob = wld_new_mob(map, MOB_JACKAL, c, r);
mob->ai_wander = ai_default_wander;
mob->ai_is_hostile = ai_is_hostile_player;
mob->ai_detect_combat = ai_detect_combat_visible_hostile;
mob->ai_decide_combat = ai_decide_combat_melee_with_flee;
mob->flee_threshold = .4;
mob->stat_strength = dm_randii(3, 16);
mob->stat_dexterity = dm_randii(3, 16);
mob->stat_constitution = dm_randii(3, 16);
rpg_apply_stats(mob);
mob->health = mob->maxhealth;
return mob;
}
// GENERATORS END
///////////////////////////
///////////////////////////
// MAP INITIALIZATION
void wld_generate_tiles(struct wld_map *map, struct dng_cellmap* cellmap)
{
int *tile_map_array = (int*)malloc(map->length * sizeof(int));
int *mob_map_array = (int*)malloc(map->length * sizeof(int));
int *item_map_array = (int*)malloc(map->length * sizeof(int));
map->tiles = (struct wld_tile*)malloc(map->length * sizeof(struct wld_tile));
map->tiles_length = map->length;
int tutorial_id = 0;
for (int r = 0; r < cellmap->height; r++) { // rows
for (int c=0; c < cellmap->width; c++){ // cols
// get cell from map
int index = r * cellmap->width + c;
struct dng_cell *cell = cellmap->cells[index];
// get tile
struct wld_tile *tile = &map->tiles[index];
tile->id = index;
tile->map = map;
tile->map_x = wld_calcx(index, map->cols);
tile->map_y = wld_calcy(index, map->cols);
tile->map_index = index;
tile->map = map;
tile->is_visible = false;
tile->was_visible = false;
tile->is_blocked = false;
tile->is_door = false;
tile->is_door_open = false;
tile->is_door_locked = false;
tile->door_lock_id = -1;
tile->dead_mob_type = NULL;
tile->on_mob_enter = NULL;
tile->astar_node = dm_astar_newnode();
tile->astar_node->owner = (void*)tile;
tile->astar_node->get_x = wld_tile_get_x;
tile->astar_node->get_y = wld_tile_get_y;
tile->tutorial_id = 0;
// TODO more
if (cell->is_wall) {
tile->type = &wld_tiletypes[TILE_STONEWALL];
} else if (cell->is_door) {
tile->type = &wld_tiletypes[TILE_STONEDOOR];
tile->is_blocked = true; // cell->is_door_locked; // locked based on cell door quality
tile->is_door = true;
tile->is_door_open = false;
tile->is_door_locked = cell->is_door_locked;
tile->door_lock_id = cell->door_lock_id;
} else if (cell->is_exit_transition || cell->is_entrance_transition) {
if (cell->is_entrance_transition) {
if (cellmap->id > 0) { // only make it traversable if it is not the first level
tile->type = &wld_tiletypes[TILE_ENTRANCE];
tile->on_mob_enter = wld_tile_on_mob_enter_entrance;
} else {
tile->type = &wld_tiletypes[TILE_STONEFLOOR];
}
map->entrance_tile = tile;
}
if (cell->is_exit_transition) {
tile->type = &wld_tiletypes[TILE_EXIT];
tile->on_mob_enter = wld_tile_on_mob_enter_exit;
map->exit_tile = tile;
}
} else if (cell->is_tutorial) {
tile->type = &wld_tiletypes[TILE_TUTORIAL_STONE];
tile->on_mob_enter = wld_tile_on_mob_enter_tutorial;
tile->tutorial_id = tutorial_id++;
} else {
if (cell->tile_style == DNG_TILE_STYLE_GRASS) {
tile->type = &wld_tiletypes[TILE_GRASS];
} else if(cell->tile_style == DNG_TILE_STYLE_WATER) {
tile->type = &wld_tiletypes[TILE_WATER];
} else if(cell->tile_style == DNG_TILE_STYLE_DEEPWATER) {
tile->type = &wld_tiletypes[TILE_DEEPWATER];
} else if(cell->tile_style == DNG_TILE_STYLE_SUMMONCIRCLE) {
tile->type = &wld_tiletypes[TILE_SUMMONCIRCLE_SF_INERT];
tile->on_mob_enter = wld_tile_on_mob_enter_summoncircle;
} else {
tile->type = &wld_tiletypes[TILE_STONEFLOOR];
}
}
tile_map_array[index] = tile->id; // set tile map to this tile id
mob_map_array[index] = -1; // initialize to -1 for "no mob"
item_map_array[index] = -1; // no item
}
}
map->tile_map = tile_map_array;
map->mob_map = mob_map_array;
map->item_map = item_map_array;
}
struct wld_mob* wld_new_mob(struct wld_map *map, enum WLD_MOBTYPE type, int x, int y)
{
struct wld_mob *mob = (struct wld_mob*)malloc(sizeof(struct wld_mob));
mob->is_player = false;
mob->state = MS_START;
mob->queue_x = 0;
mob->queue_y = 0;
mob->ai_wander = NULL;
mob->ai_is_hostile = NULL;
mob->ai_detect_combat = NULL;
mob->ai_decide_combat = NULL;
mob->ai_player_input = NULL;
mob->ai_converse = NULL;
mob->ai_attacked = NULL;
mob->ai_killed = NULL;
mob->cursor_target_index = -1;
mob->mode = MODE_PLAY;
mob->target_mode = TMODE_NONE;
mob->is_dead = false;
mob->static_xp_gain = 0;
mob->target_x = 0;
mob->target_y = 0;
mob->active_item = NULL;
mob->is_destroy_queued = false;
mob->type = &wld_mobtypes[type];
mob->active_effects_length = 0;
mob->vision = mob->type->base_vision;
mob->vision_boost = 0;
mob->flee_threshold = .5;
// create inventory (pointers to malloc items)
mob->inventory = (struct wld_item**)malloc(INVENTORY_SIZE * sizeof(struct wld_item*));
for (int j=0; j < INVENTORY_SIZE; j++) {
mob->inventory[j] = NULL;
}
// create pointers to aberrations
mob->can_mutate = false;
mob->mutate_xp = 0;
mob->mutate_ding = 0;
mob->mutate_drain_rate = 0;
mob->heal_rate = 8; // little slower than player
mob->aberrations = (struct wld_aberration**)malloc(MAX_ABERRATIONS * sizeof(struct wld_aberration*));
for (int j=0; j < MAX_ABERRATIONS; j++) {
mob->aberrations[j] = NULL;
}
mob->aberrations_length = 0;
mob->current_aberration = NULL;
mob->can_mutate_more = false;
wld_map_new_mob(map, mob, x, y);
return mob;
}
void wld_generate_mobs(struct wld_map *map, struct dng_cellmap* cellmap)
{
map->mobs = NULL;
map->mobs_length = 0;
for (int r = 0; r < cellmap->height; r++) { // rows
for (int c=0; c < cellmap->width; c++){ // cols
// get cell from map
int index = r * cellmap->width + c;
struct dng_cell *cell = cellmap->cells[index];
// Spawn player and first dungeon entrance
if (cell->is_entrance_transition && map->is_first_map) {
struct wld_mob *mob = gen_mob_player(map, c, r);
// set cursor nearby
map->cursor->x = mob->map_x + 2;
map->cursor->y = mob->map_y;
map->cursor->index = wld_calcindex(map->cursor->x, map->cursor->y, map->cols);
} else if (cell->has_mob) {
switch (cell->mob_style) {
case DNG_MOB_STYLE_NPC_INTRO:
gen_mob_npc_mother(map, c, r);
break;
case DNG_MOB_STYLE_HOARD:
gen_mob_rat(map, c, r);
break;
default:
if (dm_chance(1,4)) {
gen_mob_jackal(map, c, r);
} else {
gen_mob_rat(map, c, r);
}
break;
}
}
}
}
}
void wld_init_item(struct wld_item* item, enum WLD_ITEMTYPE type)
{
item->type = &wld_itemtypes[type];
item->has_dropped = false;
item->uses = wld_itemtypes[type].base_uses;
item->id = -1;
item->map_index = -1;
item->map_x = 0;
item->map_y = 0;
item->key_id = -1;
item->map_found -1;
}
void wld_generate_items(struct wld_map *map, struct dng_cellmap* cellmap)
{
map->items = NULL;
map->items_length = 0;
// TODO we need to work on assigning items to players, mobs etc
for (int r = 0; r < cellmap->height; r++) { // rows
for (int c=0; c < cellmap->width; c++){ // cols
// get cell from map
int index = r * cellmap->width + c;
struct dng_cell *cell = cellmap->cells[index];
if (cell->has_item) {
struct wld_item* item = (struct wld_item*)malloc(sizeof(struct wld_item));
switch (cell->item_style) {
default:
case DNG_ITEM_LOOT:
switch (dm_randii(0, 5)) {
case 0:
wld_init_item(item, ITEM_WEAPON_SHORTSWORD);
wld_map_new_item(map, item, c, r);
break;
case 1:
wld_init_item(item, ITEM_WEAPON_SHORTBOW);
wld_map_new_item(map, item, c, r);
break;
case 2:
wld_init_item(item, ITEM_POTION_MINOR_HEAL);
wld_map_new_item(map, item, c, r);
break;
case 3:
wld_init_item(item, ITEM_ARMOR_LEATHER);
wld_map_new_item(map, item, c, r);
break;
case 4:
wld_init_item(item, ITEM_AMMO_ARROW);
wld_map_new_item(map, item, c, r);
item->uses = dm_randii(1,3);
break;
}
break;
case DNG_ITEM_KEY:
wld_init_item(item, ITEM_KEY_BASIC);
wld_map_new_item(map, item, c, r);
item->key_id = cell->key_id; // matches a door somewhere spooky
break;
}
}
}
}
}
struct wld_map* wld_new_map(int id, int difficulty, int width, int height)
{
struct wld_map *map = (struct wld_map*)malloc(sizeof(struct wld_map));
// data
map->id = id;
map->rows = height;
map->cols = width;
map->length = width * height;
map->difficulty = difficulty;
map->tile_map = NULL;
map->mob_map = NULL;
map->tiles = NULL;
map->mobs = NULL;
map->mobs_length = 0;
map->mobs_capacity = 0;
map->items = NULL;
map->items_length = 0;
map->items_capacity = 0;
map->player = NULL;
map->cursor = (struct wld_cursor*)malloc(sizeof(struct wld_cursor));
map->cursor->x = 0;
map->cursor->y = 0;
map->cursor->index = 0;
// function events
map->on_player_map_transition = NULL;
map->on_cursormove = NULL;
map->on_playermove = NULL;
map->on_mob_heal = NULL;
map->on_mob_attack_mob = NULL;
map->on_mob_attack_player = NULL;
map->on_mob_whiff = NULL;
map->on_mob_whiff_mob = NULL;
map->on_mob_whiff_player = NULL;
map->on_mob_kill_mob = NULL;
map->on_mob_kill_player = NULL;
map->on_player_heal = NULL;
map->on_player_attack_mob = NULL;
map->on_player_whiff = NULL;
map->on_player_whiff_mob = NULL;
map->on_player_kill_mob = NULL;
map->on_player_pickup_item = NULL;
map->on_player_pickup_item_fail = NULL;
map->on_player_drop_item = NULL;
map->on_player_drop_item_fail = NULL;
return map;
}
// frees mob memory
void wld_delete_mob(struct wld_mob *mob)
{
for (int j=0; j<MAX_ABERRATIONS; j++)
if (mob->aberrations[j] != NULL)
free(mob->aberrations[j]);
free(mob->aberrations);
for (int j=0; j<INVENTORY_SIZE; j++)
if (mob->inventory[j] != NULL)
free(mob->inventory[j]);
free(mob->inventory);
free(mob);
}
void wld_delete_map(struct wld_map *map)
{
free(map->cursor);
for (int i=0; i<map->items_length; i++)
if (map->items[i] != NULL)
free(map->items[i]);
free(map->items);
free(map->item_map);
for (int i=0; i<map->mobs_length; i++)
wld_delete_mob(map->mobs[i]);
free(map->mobs);
free(map->mob_map);
for (int i=0; i<map->tiles_length; i++)
free(map->tiles[i].astar_node);
free(map->tiles);
free(map->tile_map);
free(map);
}
// MAP INITALIZATION END
///////////////////////////
///////////////////////////
// MAP METHODS START
int wld_calcindex(int x, int y, int cols)
{
return y * cols + x;
}
// TODO I think these are wrong!
int wld_calcx(int index, int cols)
{
return index % cols;
}
// TODO I think these are wrong!
int wld_calcy(int index, int rows)
{
return index / rows;
}
void wld_map_insert_item(struct wld_map* map, struct wld_item* item, int x, int y, int id)
{
int index = wld_calcindex(x, y, map->cols);
// assign items properties for the map
item->id = id;
item->map_x = x;
item->map_y = y;
item->map_index = index;
// assign map the items id and stuff
map->items[id] = item;
map->item_map[index] = id;
}
void wld_map_new_item(struct wld_map* map, struct wld_item* item, int x, int y)
{
// if out of room expand
if (map->items_length == map->items_capacity) {
map->items = (struct wld_item**)realloc(map->items, (map->items_capacity + MALLOC_MOB_SIZE) * sizeof(struct wld_item*));
map->items_capacity += MALLOC_MOB_SIZE;
}
// copy to the next spot
map->items[map->items_length] = item;
// update the map to contain the item
wld_map_insert_item(map, item, x, y, map->items_length);
// increment our list
map->items_length++;
}
void wld_map_remove_item(struct wld_map* map, struct wld_item* item)
{
bool found = false;
for (int i=0; i<map->items_length; i++) {
if (found) {
// shift backwards a spot
struct wld_item *next_item = map->items[i];
next_item->id = i - 1;
map->items[next_item->id] = next_item;
// his id is at his position in the map, we need to point that to his new id
map->item_map[next_item->map_index] = next_item->id;
} else if(map->items[i] == item) {
// if we find the item, null his instance and -1 his item map
int index = item->map_index;
map->items[i] = NULL;
map->item_map[index] = -1;
found = true;
item->id = -1;
item->map_index = -1;
item->map_x = 0;
item->map_y = 0;
}
}
if (found) {
// turn the last index into a null
map->items[map->items_length - 1] = NULL;
map->items_length--;
}
}
// take a mob instance and add him to the mob list
void wld_map_insert_mob(struct wld_map* map, struct wld_mob* mob, int x, int y, int id)
{
int index = wld_calcindex(x, y, map->cols);
// assign mobs properties for the map
mob->id = id;
mob->map = map;
mob->map_x = x;
mob->map_y = y;
mob->map_index = index;
// assign map the mobs id and stuff
map->mobs[id] = mob;
map->mob_map[index] = id;
}
// This is called when we want the map to take ownership of the mob