forked from obsidian-level-maker/Obsidian
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem.lua
More file actions
1110 lines (808 loc) · 25.3 KB
/
Copy pathitem.lua
File metadata and controls
1110 lines (808 loc) · 25.3 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
------------------------------------------------------------------------
-- ITEM SELECTION / PLACEMENT
------------------------------------------------------------------------
--
-- Oblige Level Maker
--
-- Copyright (C) 2006-2017 Andrew Apted
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2,
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
------------------------------------------------------------------------
--class HMODEL
--[[
--
-- This represents the weapons, health and ammo that a player has at
-- a particular time. When each room is visited, Player_give_room_stuff()
-- updates the hmodel for the items in the room. When placing items, we
-- exclude stuff that the player already has.
--
-- There is one 'HMODEL' for each player class (in HEXEN there are three
-- player classes). So LEVEL.hmodels is indexed by the class name.
--
-- The GAME.PLAYER_MODEL table contains the initial model, especially the
-- weapons that a player always holds.
--
weapons : table -- what weapons the player has
stats : table -- what health and ammo the player has
--]]
-- Doom flags
DOOM_FLAGS =
{
EASY = 1,
MEDIUM = 2,
HARD = 4,
DEAF = 8
}
-- Hexen thing flags
HEXEN_FLAGS =
{
FIGHTER = 32,
CLERIC = 64,
MAGE = 128,
DM = 1024
}
-- Quake flags
QUAKE_FLAGS =
{
DEAF = 1,
NOT_EASY = 256,
NOT_MEDIUM = 512,
NOT_HARD = 1024,
NOT_DM = 2048
}
-- Hexen2 flags [NOT USED YET]
HEXEN2_FLAGS =
{
NOT_PALADIN = 256,
NOT_CLERIC = 512,
NOT_NECRO = 1024,
NOT_ASSASSIN = 2048,
NOT_EASY = 4096,
NOT_MEDIUM = 8192,
NOT_HARD = 16384,
NOT_DM = 32768
}
function Player_init(LEVEL)
LEVEL.hmodels = table.deep_copy(GAME.PLAYER_MODEL)
for CL,hmodel in pairs(LEVEL.hmodels) do
hmodel.class = CL
end
end
function Player_give_weapon(LEVEL, weapon, only_CL)
gui.printf("Giving weapon: %s\n", weapon)
for CL,hmodel in pairs(LEVEL.hmodels) do
if not only_CL or (only_CL == CL) then
hmodel.weapons[weapon] = 1
end
end
end
function Player_give_class_weapon(slot)
for name,W in pairs(GAME.WEAPONS) do
for CL,hmodel in pairs(LEVEL.hmodels) do
if W.slot == slot and W.class == CL then
hmodel.weapons[name] = 1
end
end
end
end
function Player_give_map_stuff(LEVEL)
if LEVEL.assume_weapons then
for name,_ in pairs(LEVEL.assume_weapons) do
if name == "weapon2" then Player_give_class_weapon(2)
elseif name == "weapon3" then Player_give_class_weapon(3)
elseif name == "weapon4" then Player_give_class_weapon(4)
else
Player_give_weapon(name)
end
end
end
end
function Player_give_room_stuff(LEVEL, R)
-- give weapons, plus any ammo they come with
for _,name in pairs(R.weapons) do
Player_give_weapon(LEVEL, name)
local weap = GAME.WEAPONS[name]
if weap and weap.give then
for CL,hmodel in pairs(LEVEL.hmodels) do
Player_give_stuff(hmodel, weap.give)
end
end
EPISODE.seen_weapons[name] = 1
end
-- take nice items into account too (except for secrets)
if not R.is_secrets then
for _,name in pairs(R.items) do
local info = GAME.NICE_ITEMS[name] or GAME.PICKUPS[name]
if info and info.give then
for CL,hmodel in pairs(LEVEL.hmodels) do
Player_give_stuff(hmodel, info.give)
end
end
end
end
-- handle storage rooms too
if R.storage_items then
for _,pair in pairs(R.storage_items) do
local info = assert(pair.item)
if info.give then
for CL,hmodel in pairs(LEVEL.hmodels) do
Player_give_stuff(hmodel, info.give)
end
end
end
end
end
function Player_give_stuff(hmodel, give_list)
for _,give in pairs(give_list) do
if give.health then
gui.debugf("Giving [%s] health: %d\n",
hmodel.class, give.health)
hmodel.stats.health = hmodel.stats.health + give.health
elseif give.ammo then
gui.debugf("Giving [%s] ammo: %dx %s\n",
hmodel.class, int(give.count), give.ammo)
hmodel.stats[give.ammo] = (hmodel.stats[give.ammo] or 0) + give.count
elseif give.weapon then
gui.debugf("Giving [%s] weapon: %s\n",
hmodel.class, give.weapon)
hmodel.weapons[give.weapon] = 1
else
error("Bad give item : not health, ammo or weapon")
end
end
end
function Player_firepower(LEVEL)
-- The 'firepower' is (roughly) how much damage per second
-- the player would normally do using their current set of
-- weapons.
--
-- If there are different classes (Hexen) then the result
-- will be an average of each class, as all classes face
-- the same monsters.
local function get_firepower(hmodel)
local firepower = 0
local divisor = 0
for weapon,_ in pairs(hmodel.weapons) do
local info = GAME.WEAPONS[weapon]
if not info then
error("Missing weapon info for: " .. weapon)
end
local dm = info.damage * info.rate
if info.splash then dm = dm + info.splash[1] end
-- melee attacks are hard to use, and
-- projectiles miss more often than hitscan
if info.attack == "melee" then
dm = dm / 3.0
elseif info.attack == "missile" then
dm = dm / 1.3
end
local pref = info.pref or 1
--- gui.debugf(" weapon:%s dm:%1.1f pref:%1.1f\n", weapon, dm, pref)
firepower = firepower + dm * pref
divisor = divisor + pref
end
if divisor == 0 then
error("Player_firepower: no weapons???")
end
return firepower / divisor
end
---| Player_firepower |---
local fp_total = 0
local class_num = 0
for CL,hmodel in pairs(LEVEL.hmodels) do
fp_total = fp_total + get_firepower(hmodel)
class_num = class_num + 1
end
assert(class_num > 0)
return fp_total / class_num
end
function Player_has_weapon(LEVEL, weap_needed)
local function class_has_one(hmodel)
for name,_ in pairs(hmodel.weapons) do
if weap_needed[name] then
return true
end
end
return false
end
--| Player_has_weapon |--
-- we require a match for every class
for CL,hmodel in pairs(LEVEL.hmodels) do
if not class_has_one(hmodel) then
return false
end
end
return true -- OK
end
function Player_max_damage(LEVEL)
local result = 5
for name,info in pairs(GAME.WEAPONS) do
local W_damage = info.rate * info.damage
if W_damage > result and Player_has_weapon(LEVEL, { [name]=1 }) then
result = W_damage
end
end
return result
end
function Player_find_initial_weapons(LEVEL)
-- find with weapons the player always owns
local list = {}
for CL,hmodel in pairs(LEVEL.hmodels) do
for name,_ in pairs(hmodel.weapons) do
list[name] = 1
end
end
return list
end
function Player_find_zone_weapons(Z, list)
for _,R in pairs(Z.rooms) do
for _,name in pairs(R.weapons) do
list[name] = 1
end
end
end
function Player_weapon_palettes(LEVEL)
local Middle = 1.00
local High = 2.20
local Highest = 4.80
local Low = 0.44
local Lowest = 0.21
local function insert_multiple(list, count, what)
for i = 1, count do
table.insert(list, what)
end
end
local function decide_quantities(total)
local list = {}
-- Note: result is often longer than strictly required
local num_low = int(total / 2 + gui.random())
local num_high = total - num_low
insert_multiple(list, num_low, Low)
insert_multiple(list, num_high, High)
if total >= 2 then
local num_very = int(total / 6 + gui.random())
insert_multiple(list, num_very, Lowest)
insert_multiple(list, num_very, Highest)
end
assert(#list >= total)
rand.shuffle(list)
return list
end
local function apply_pref_table(pal, prefs)
if not prefs then return end
for name,factor in pairs(prefs) do
if pal[name] then
pal[name] = pal[name] * factor
end
end
end
local function gen_palette(got_weaps)
local total = table.size(got_weaps)
if total < 2 then return {} end
local pal = {}
-- decide number of "normal" weapons : at least one!
local normal_num = int(total / 3 + gui.random())
if normal_num < 1 then normal_num = 1 end
got_weaps = table.copy(got_weaps)
for n = 1, normal_num do
local name = rand.key_by_probs(got_weaps)
pal[name] = Middle
got_weaps[name] = nil
end
-- decide what to give everything else
total = total - normal_num
local quants = decide_quantities(total)
for name,_ in pairs(got_weaps) do
pal[name] = table.remove(quants, 1)
end
-- apply level and theme preferences
apply_pref_table(pal, LEVEL.weap_prefs)
apply_pref_table(pal, THEME.weap_prefs)
return pal
end
local function dump_palette(pal)
for weap,qty in pairs(pal) do
gui.debugf(" %-9s* %1.2f\n", weap, qty)
end
end
---| Player_weapon_palettes |---
-- Note: not using initial_weapons() here, they tend to be melee
-- weapons and it sucks to promote them.
local got_weaps = {}
for _,Z in pairs(LEVEL.zones) do
Player_find_zone_weapons(Z, got_weaps)
Z.weap_palette = gen_palette(got_weaps)
gui.debugf("Weapon palette in ZONE_%d:\n", Z.id)
dump_palette(Z.weap_palette)
end
end
------------------------------------------------------------------------
function Item_simulate_battle(LEVEL, R)
local function make_empty_stats()
local stats = {}
for CL,_ in pairs(GAME.PLAYER_MODEL) do
stats[CL] = {}
end
return stats
end
local function user_adjust_result(stats)
-- apply the user's health/ammo adjustments here
local heal_mul = HEALTH_FACTORS[OB_CONFIG.health]
local ammo_mul = AMMO_FACTORS[OB_CONFIG.ammo]
heal_mul = heal_mul * (PARAM.health_factor or 1)
ammo_mul = ammo_mul * (PARAM.ammo_factor or 1)
if LEVEL.is_procedural_gotcha and PARAM.bool_boss_gen == 1 then
ammo_mul = ammo_mul * (PARAM.float_boss_gen_ammo * PARAM.boss_gen_mult)
heal_mul = heal_mul * PARAM.float_boss_gen_heal
end
-- give less ammo in later maps (to counter the build-up over an episode)
if PARAM.bool_pistol_starts == 0 then
local along = math.clamp(0, LEVEL.ep_along - 0.2, 0.8)
local factor = 1.1 - along * 0.25
ammo_mul = ammo_mul * factor
end
if PARAM.bool_scale_items_with_map_size and PARAM.bool_scale_items_with_map_size == 1 then
heal_mul = heal_mul * (1 + (LEVEL.map_W / 75))
ammo_mul = ammo_mul * (1 + (LEVEL.map_W / 75))
end
for name,qty in pairs(stats) do
if name == "health" then
stats[name] = qty * heal_mul
else
stats[name] = qty * ammo_mul
end
end
end
local function subtract_stuff_we_have(stats, hmodel)
for name,have_qty in pairs(hmodel.stats) do
local need_qty = stats[name] or 0
if have_qty > 0 and need_qty > 0 then
local min_q = math.min(have_qty, need_qty)
stats[name] = stats[name] - min_q
hmodel.stats[name] = hmodel.stats[name] - min_q
end
end
end
local function give_monster_drops(hmodel, mon_list)
for _,M in pairs(mon_list) do
if M.is_cage then goto continue end
if M.info.give then
Player_give_stuff(hmodel, M.info.give)
end
::continue::
end
end
local function is_weapon_upgraded(name, list)
for _,W in pairs(list) do
if W.info.upgrades == name then
return true
end
end
return false
end
local function collect_weapons(hmodel)
local list = {}
local seen = {}
for name,_ in pairs(hmodel.weapons) do
local info = assert(GAME.WEAPONS[name])
local factor = R.zone.weap_palette[name]
if info.pref then
table.insert(list, { info=info, factor=factor })
seen[name] = true
end
end
if PARAM.bool_pistol_starts == 0 then
-- allow weapons from previous levels
for name,_ in pairs(EPISODE.seen_weapons) do
if not seen[name] then
local info = assert(GAME.WEAPONS[name])
assert(info.pref)
table.insert(list, { info=info, factor=0.5 })
end
end
end
if #list == 0 then
error("No usable weapons???")
end
-- remove "upgraded" weapons (e.g. supershotgun > shotgun)
for i = #list, 1, -1 do
if is_weapon_upgraded(list[i].info.name, list) then
table.remove(list, i)
end
end
return list
end
local function battle_for_class(CL, hmodel)
local mon_list = R.monster_list
local weap_list = collect_weapons(hmodel)
local stats = R.item_stats[CL]
gui.debugf("Fight Simulator @ %s class: %s\n", R.name, CL)
gui.debugf("weapons = \n")
for _,W in pairs(weap_list) do
gui.debugf(" %s\n", W.info.name)
end
Fight_Simulator(mon_list, weap_list, stats)
-- gui.debugf("raw result = \n%s\n", table.tostr(stats,1))
user_adjust_result(stats)
-- gui.debugf("adjusted result = \n%s\n", table.tostr(stats,1))
give_monster_drops(hmodel, mon_list)
subtract_stuff_we_have(stats, hmodel)
end
---| Item_simulate_battle |---
assert(R.monster_list)
R.item_stats = make_empty_stats()
if #R.monster_list >= 1 then
for CL,hmodel in pairs(LEVEL.hmodels) do
battle_for_class(CL, hmodel)
end
end
end
function Item_distribute_stats(LEVEL)
--
-- This distributes the item statistics (how much health and ammo to
-- give to the player) into earlier rooms.
--
-- health mainly stays in same room (a reward for killing the monsters).
-- ammo mainly goes back, to prepare player for the fight.
local HEALTH_RATIO = 0.35
local AMMO_RATIO = 0.90
local function get_earlier_rooms(R)
local list = {}
local ratio = 1.0
local total = 0.0
local N = R
while N.entry_conn do
N = N.entry_conn:other_room(N)
-- do not cross zones
if N.zone ~= R.zone then break; end
-- never move stuff into hallways
if N.is_hallway then goto continue end
-- give more in larger rooms
local val = ratio * (N.svolume ^ 0.7)
table.insert(list, { room=N, ratio=val })
total = total + val
ratio = ratio * 0.7
::continue::
end
-- handle hallways that are entered from a different zone
-- (i.e. via a keyed door).
if R.is_hallway and table.empty(list) then
N = R.entry_conn:other_room(N)
table.insert(list, { room=N, ratio=1.0 })
total = 1.0
end
-- adjust ratio values to be in range 0.0 - 1.0,
if total > 0 then
for _,loc in pairs(list) do
loc.ratio = loc.ratio / total
end
end
return list
end
local function distribute_to_room(R, N, ratio)
-- ratio is a value between 0.0 and 1.0, based on the number
-- and size of the earlier rooms (in the loc list).
for CL,R_stats in pairs(R.item_stats) do
local N_stats = N.item_stats[CL]
for stat,qty in pairs(R_stats) do
if qty <= 0 then goto continue end
local value = qty * ratio
-- apply a ratio based on type of item (on top of the room ratio)
-- [ for hallways, we need EVERYTHING to go elsewhere ]
if R.is_hallway then
-- no change
elseif stat == "health" then
value = value * HEALTH_RATIO
else
value = value * AMMO_RATIO
end
N_stats[stat] = (N_stats[stat] or 0) + value
R_stats[stat] = R_stats[stat] - value
-- gui.debugf(" distributing %s:%1.1f [%s] %s --> %s\n",
-- stat, value, CL, R.name, N.name)
::continue::
end
end
end
local function visit_room(R)
-- no stats?
if not R.item_stats then return end
for _,loc in pairs(get_earlier_rooms(R)) do
distribute_to_room(R, loc.room, loc.ratio)
end
end
local function dump_results()
for _,R in pairs(LEVEL.rooms) do
if R.item_stats then
gui.debugf("final result @ %s = \n%s\n", R.name,
table.tostr(R.item_stats, 2))
end
end
end
---| Item_distribute_stats |---
for _,R in pairs(LEVEL.rooms) do
visit_room(R)
end
--DEBUG:
-- dump_results()
end
function Item_pickups_for_class(LEVEL, CL)
--
-- Once all monsters have been placed and all battles simulated
-- (including cages and traps), then we can decide *what* pickups to add
-- (the easy part) and *where* to place them (the hard part).
--
-- this accumulates excess stats
-- e.g. if wanted health == 20 and we give a medikit, add 5 to excess["health"]
local excess = {}
local function grab_a_big_spot(R)
local result = table.pick_best(R.big_spots,
function(A, B) return A.score > B.score end, "remove")
-- update remaining scores so next one chosen is far away
for _,spot in pairs(R.big_spots) do
local dist = Monster_dist_between_spots(spot, result, 80) / 256
spot.score = spot.score + dist
end
return result
end
local function place_item(item_name, x, y, z)
local props = {}
if PARAM.use_spawnflags then
-- no change
else
props.flags = DOOM_FLAGS.EASY + DOOM_FLAGS.MEDIUM + DOOM_FLAGS.HARD
end
Trans.entity(item_name, x, y, z, props)
end
local function place_item_in_spot(item_name, spot)
local x, y = geom.box_mid(spot.x1, spot.y1, spot.x2, spot.y2)
place_item(item_name, x, y, spot.z1)
end
local function find_cluster_spot(R, prev_spots, item_name)
if #prev_spots == 0 then
local spot = table.remove(R.item_spots, 1)
table.insert(prev_spots, spot)
return spot
end
local best_idx
local best_dist
-- FIXME: optimise this!
for index = 1,#R.item_spots do
local spot = R.item_spots[index]
local dist = 9e9
for _,prev in pairs(prev_spots) do
local d = Monster_dist_between_spots(prev, spot)
dist = math.min(dist, d)
end
-- prefer closest row to a wall
if spot.wall_dist then
dist = dist + spot.wall_dist * 200
end
-- avoid already used spots
if spot.used then dist = dist + 100000 end
if not best_idx or dist < best_dist then
best_idx = index
best_dist = dist
end
end
assert(best_idx)
local spot = table.remove(R.item_spots, best_idx)
if #prev_spots >= 3 then
table.remove(prev_spots, 1)
end
table.insert(prev_spots, spot)
return spot
end
local function place_item_list(R, item_list)
for _,pair in pairs(item_list) do
local item = pair.item
local count = pair.count
-- big item?
if ((item.rank or 0) >= 2 or pair.is_storage) and count == 1 and
not table.empty(R.big_spots)
then
local spot = grab_a_big_spot(R)
place_item_in_spot(item.name, spot)
goto continue
end
-- keep track of a limited number of previously chosen spots.
-- when making clusters, this is used to find the next spot.
local prev_spots = {}
for i = 1,count do
if table.empty(R.item_spots) then
gui.printf("Unable to place items: %s x %d\n", item.name, count+1-i)
break;
end
local spot = find_cluster_spot(R, prev_spots, item.name)
place_item_in_spot(item.name, spot)
-- reuse spots if they run out
spot.used = true
table.insert(R.item_spots, spot)
end
::continue::
end
end
local function decide_pickup(R, stat, qty)
local item_tab = {}
for name,info in pairs(GAME.PICKUPS) do
local prob = info.add_prob or 0
if prob > 0 and
(stat == "health" and info.give and info.give[1].health) or
(info.give and info.give[1].ammo == stat)
then
item_tab[name] = prob
if R.is_start and info.start_prob then
item_tab[name] = info.start_prob
end
end
end
if table.empty(item_tab) then
--error("Missing linked item for " .. stat .. "!") -- Test for when a user purposefully sets pickups to 0 for a category - Dasho
return nil, 0
end
local name = rand.key_by_probs(item_tab)
local info = GAME.PICKUPS[name]
local count = 1
if info.cluster then
local each_qty = info.give[1].health or info.give[1].count
local min_num = info.cluster[1]
local max_num = info.cluster[2]
if max_num > 9 then
error("Cannot handle a cluster of more than 9 for pickup " .. stat .. "!")
end
--- count = rand.irange(min_num, max_num)
if min_num * each_qty >= qty then
count = min_num
elseif max_num * each_qty <= qty then
count = max_num - rand.sel(20,1,0)
else
count = 1 + int(qty / each_qty)
end
end
return GAME.PICKUPS[name], count
end
local function bonus_for_room(R, stat)
local bonus = 0
-- more stuff in start room
if R.is_start then
if stat == "health" then
bonus = 20 * HEALTH_FACTORS[OB_CONFIG.health]
end
end
-- when getting a weapon, should get some ammo for it too
if R.weapons then
for _,name in pairs(R.weapons) do
local info = GAME.WEAPONS[name]
if info.ammo and info.ammo == stat and info.bonus_ammo then
bonus = bonus + info.bonus_ammo * AMMO_FACTORS[OB_CONFIG.ammo]
end
end
end
if PARAM.float_strength == 12 then
bonus = bonus * 2
end
-- compensation for environmental hazards
if stat == "health" and R.hazard_health then
bonus = bonus + R.hazard_health * HEALTH_FACTORS[OB_CONFIG.health]
end
if PARAM.bool_scale_items_with_map_size and PARAM.bool_scale_items_with_map_size == 1 then
bonus = bonus * (1 + (LEVEL.map_W / 75))
end
return bonus
end
local function do_select_pickups(R, item_list, stat, qty)
assert(qty >= 0)
while qty > 0 do
local item, count = decide_pickup(R, stat, qty)
if item then
table.insert(item_list, { item=item, count=count, random=gui.random() })
end
if stat == "health" then
if item then
qty = qty - item.give[1].health * count
else
qty = 0
end
else
if item then
assert(item.give[1].ammo)
qty = qty - item.give[1].count * count
else
qty = 0
end
end
end
-- return the excess amount
return (-qty)
end
local function select_pickups(R, item_list, stat, qty)
assert(qty >= 0)
if excess[stat] == nil then
excess[stat] = 0
end