-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_main.cfg
More file actions
898 lines (872 loc) · 45.2 KB
/
_main.cfg
File metadata and controls
898 lines (872 loc) · 45.2 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
#textdomain wesnoth-Real_Ranged_Attacks
{~add-ons/Real_Ranged_Attacks/ai}
{~add-ons/Real_Ranged_Attacks/lua}
{~add-ons/Real_Ranged_Attacks/gui}
[modification]
id=Real_Ranged_Attacks
name= _"Real Ranged Attacks"
type=hybrid
require_modification=yes
description= _ "Allows you and AI to attack over long ranges without right-click menu."
[options]
[slider]
id=ranged_attacks_max_range
name="Max range: "
description="Maximal range unit's can shoot on, 1 means adjacent tile."
default=3
min=2
max=6
[/slider]
[slider]
id=ranged_attacks_min_range
name="Min range: "
description="Minimal range unit's can shoot on, 1 means adjacent tile."
default=2
min=1
max=6
[/slider]
[checkbox]
id=real_ranged_attacks_close_lock
default=yes
name="Ranged attacks close lock"
description="Unit's cannot attack with ranged when adjacent to an enemy, but they still can defend."
[/checkbox]
[/options]
### MODIFY UNITS RANGE
[event]
name=unit placed, post_advance
first_time_only=no
id=real_ranged_attacks_grant
[filter]
[not]
id=$last_unit_placed_ranged_attack_id
[/not]
[or]
[not]
type=$last_unit_placed_ranged_attack_type
max_hitpoints=$last_unit_placed_ranged_attack_max_hitpoints
[/not]
[/or]
[/filter]
[lua]
code=<<
local unit = wesnoth.units.get(wesnoth.current.event_context.x1, wesnoth.current.event_context.y1)
unit_cfg = unit.__cfg
for i, weapon in ipairs(unit.attacks) do
if weapon.range == "ranged" then
weapon_cfg = wml.get_nth_child(unit_cfg, "attack", i)
weapon_cfg.max_range=wml.variables["ranged_attacks_max_range"]
weapon_cfg.min_range=wml.variables["ranged_attacks_min_range"]
end
end
wml.variables["last_unit_placed_ranged_attack_id"] = unit.id
wml.variables["last_unit_placed_ranged_attack_type"] = unit.type
wml.variables["last_unit_placed_ranged_attack_max_hitpoints"] = unit.max_hitpoints
wesnoth.units.extract(unit)
wesnoth.units.to_map(unit_cfg)
>>
[/lua]
[/event]
### no ranged attacks when adjacent to an enemy
[event]
name=unit placed
first_time_only=no
id=real_ranged_attacks_no_adjecent_attack_grant
[filter]
[not]
[filter_wml]
[modifications]
[object]
id="real_ranged_attacks_no_adjecent_attack"
[/object]
[/modifications]
[/filter_wml]
[/not]
[/filter]
#wesnoth.units.to_map({ type = "Elvish Archer",side=1 }, 10, 10)
#u = wesnoth.units.get({10,10})
#wesnoth.units.add_modification(u, "object",{{"effect",{apply_to="attack", range="ranged", {"set_specials",{mode="append",{"sample",{id="close_lock", name="close lock", apply_to="self", {"filter_self",{{"filter_adjacent",{is_enemy=true, count="1-6"}}}}}}}}}}})
#print("does attack have [sample] special? " .. tostring(u.attacks[2]:matches({special_type="sample"})))
#print("does attack have ACTIVE [sample] special? "..tostring(u.attacks[2]:matches({special_type_active="sample"})))
#print("placing adjacent enemy unit")
#wesnoth.units.to_map({ type = "Orcish Grunt", side=2 }, 10, 9)
#print("does attack have [sample] special? " .. tostring(u.attacks[2]:matches({special_type="sample"})))
#print("does attack have ACTIVE [sample] special? "..tostring(u.attacks[2]:matches({special_type_active="sample"})))
#wesnoth.wml_actions.store_unit({{"filter",{{"has_attack",{special_type="sample"}}}},variable="my_unit_sample"})
#print("WML stored id of unit which has [sample] special: ")
#print(wml.variables["my_unit_sample.id"])
#wesnoth.wml_actions.store_unit({{"filter",{{"has_attack",{special_type_active="sample"}}}},variable="my_unit_sample_active"})
#print("WML stored id of unit which has [sample] special ACTIVE: ")
#print(wml.variables["my_unit_sample_active.id"])
[if]
[variable]
name=real_ranged_attacks_close_lock
equals=yes
[/variable]
[then]
[object]
[filter]
x,y=$x1,$y1
[/filter]
silent=yes
id=real_ranged_attacks_no_adjecent_attack
take_only_once=no
[effect]
apply_to=attack
range="ranged"
[set_specials]
mode=append
[attacks]
id=close_lock_attacks
apply_to=self
value=0
[filter_self]
side="$|side_number|"
[filter_adjacent]
is_enemy=yes
count="1-6"
[/filter_adjacent]
[/filter_self]
[/attacks]
[disable]
id=close_lock
name= _ "close lock"
female_name= _ "female^close lock"
description= _ "A mystical bind that seizes ranged arms in the enemy's shadow, rendering them inert at arm's reach."
special_note=_ "A mystical bind that seizes ranged arms in the enemy's shadow, rendering them inert at arm's reach."
apply_to=self
[filter_self]
side="$|side_number|"
[filter_adjacent]
is_enemy=yes
count="1-6"
[/filter_adjacent]
[/filter_self]
[/disable]
[/set_specials]
[/effect]
[/object]
[/then]
[/if]
[/event]
### AI
[event]
name=prestart
first_time_only=no
id=real_ranged_attacks_ai
[modify_ai]
action=delete
path=stage["main_loop"].candidate_action["combat"]
[/modify_ai]
[modify_ai]
action=add
path=stage["main_loop"].candidate_action[]
[candidate_action]
id=real_ranged_attack
engine=lua
name=real_ranged_attack
max_score={AI_CA_COMBAT_SCORE}
location="~add-ons/Real_Ranged_Attacks/ai/lua/real_ranged_attack.lua"
[/candidate_action]
[/modify_ai]
[/event]
### UI
[event]
name=preload
first_time_only=no
id=real_ranged_attacks_ui
[lua]
code=<<
wesnoth.game_events.on_mouse_button = function(x, y, button, event)
if not (button == "left" and event == "click") then
return false
end
if not(wesnoth.interface.get_selected_hex()) then
return false
end
local unit = wesnoth.units.get(wesnoth.interface.get_selected_hex() )
if unit.type~="Mage" then
return false
end
wesnoth.interface.select_unit(nil)
wesnoth.interface.add_chat_message("Mage was selected")
return true
end
local BC = wesnoth.require "~add-ons/Real_Ranged_Attacks/ai/lua/battle_calcs.lua"
local AH = wesnoth.require "~add-ons/Real_Ranged_Attacks/ai/lua/ai_helper.lua"
wesnoth.game_events.on_mouse_button = function(x, y, button, event)
if not (button == "left" and event == "click") then
return false
end
if not(wesnoth.interface.get_selected_hex() and wesnoth.interface.get_hovered_hex()) then
return false
end
local unit = wesnoth.units.get(wesnoth.interface.get_selected_hex() )
local second_unit = wesnoth.units.get(wesnoth.interface.get_hovered_hex() )
if not(unit and second_unit and unit.side == wml.variables["side_number"] and wesnoth.sides.is_enemy(unit.side,second_unit.side)) then
return false
end
if not AH.is_visible_unit(unit.side, second_unit) then
return false
end
local distance = wesnoth.map.distance_between(unit.loc, second_unit.loc)
-- if distance == 1 then
-- return false
-- end
local attack = "empty"
local attack_location = unit.loc
local move_and_attack = false
for i = 1, #unit.attacks do
if unit.attacks[i].attacks_used <= unit.attacks_left and unit.attacks[i].__cfg.max_range >= distance and distance >= unit.attacks[i].__cfg.min_range and unit.attacks[i]:matches({special_type_active="disable"}) == false then
local _,_,attacker_stats,_ = wesnoth.simulate_combat(unit, i, second_unit)
if attacker_stats.num_blows ~= 0 then
std_print("attack is being set")
attack = unit.attacks[i]
attacking_weapon_index = i - 1
end
end
end
local attack_location = unit.loc
local move_and_attack = false
if attack == "empty" then
std_print("attack is empty")
local weapon_ranges={}
for i,weapon in ipairs(unit.attacks) do
if unit.attacks[i].attacks_used <= unit.attacks_left then
weapon_ranges[i] = {weapon.__cfg.max_range,weapon.__cfg.min_range}
end
end
local map=wesnoth.paths.find_cost_map(unit,{},{ignore_units=false, ignore_teleport=false, viewing_side=unit.side})
local best_cost = {0,0,unit.max_moves + 1}
for i = 1, #map do
if map[i][3] <= unit.max_moves and map[i][3] ~= -1 then
local dist = wesnoth.map.distance_between({map[i][1],map[i][2]},second_unit.loc)
--std_print(dist)
--std_print(map[i][1],map[i][2],map[i][3])
if map[i][3] < best_cost[3] and wesnoth.units.get({map[i][1],map[i][2]}) == nil then
for i2,range in ipairs(weapon_ranges) do
if range[1] >= dist and range[2] <= dist and dist ~= 1 then --hardcoded TODO (theres a mainline dialog if dist=1)
best_cost={map[i][1],map[i][2],map[i][3]}
end
end
end
end
end
std_print(best_cost[1],best_cost[2],best_cost[3],unit.max_moves + 1)
if tonumber(best_cost[3]) == tonumber(unit.max_moves + 1) then
std_print("returning false")
return false
else
move_and_attack = true
attack_location=wesnoth.named_tuple({best_cost[1],best_cost[2]}, {'x', 'y'})
--distance = wesnoth.map.distance_between(attack_location, second_unit.loc)
end
end
local unit_attack = wml.load '~add-ons/Real_Ranged_Attacks/gui/unit_attack.cfg'
local attack_predictions = wml.load '~add-ons/Real_Ranged_Attacks/gui/attack_predictions.cfg'
local red_green_scale="df0000,e20000,e40000,e70000,ea0000,ec0000,ef0000,f20000,f40000,f70000,fa0000,fc0000,ff0000,ff0500,ff0b00,ff1000,ff1500,ff1b00,ff2000,ff2500,ff2b00,ff3000,ff3500,ff3a00,ff4000,ff4500,ff4a00,ff5000,ff5500,ff5a00,ff6000,ff6500,ff6a00,ff7000,ff7500,ff7a00,ff8000,ff8500,ff8a00,ff8f00,ff9500,ff9a00,ff9f00,ffa500,ffaa00,ffaf00,ffb500,ffba00,ffbf00,ffc500,ffca00,ffcf00,ffd500,ffda00,ffdf00,ffe400,ffea00,ffef00,fff400,fffa00,ffff00,faff00,f4ff00,efff00,eaff00,e4ff00,dfff00,daff00,d5ff00,cfff00,caff00,c5ff00,bfff00,baff00,b5ff00,afff00,aaff00,a5ff00,9fff00,9aff00,95ff00,8fff00,8aff00,85ff00,80ff00,7aff00,75ff00,70ff00,6aff00,65ff00,60ff00,5aff00,55ff00,50ff00,4aff00,45ff00,40ff00,3aff00,35ff00,30ff00,2bff00,25ff00,20ff00,1bff00,15ff00,10ff00,0bff00,05ff00,00ff00,00fc00,00fa00,00f700,00f400,00f200,00ef00,00ec00,00ea00,00e700,00e400,00e200,00df00"
local unit_type_color="f5e6c1"
local good_dmg_color ={130,240,50,255}
local bad_dmg_color ={250,140,80,255}
local function rgba_to_hex(color)
return string.format("#%02X%02X%02X%02X", color[1], color[2], color[3], color[4])
end
local function xp_color(xp_to_advance,can_advance,has_amla)
local near_advance_color ={255,255,255,255}
local mid_advance_color ={150,255,255,255}
local far_advance_color ={0,205,205,255}
local normal_color ={0,160,225,255}
local near_amla_color ={225,0,255,255}
local mid_amla_color ={169,30,255,255}
local far_amla_color ={139,0,237,255}
local amla_color ={170,0,255,255}
local near_advance = math.floor(xp_to_advance) <= wesnoth.game_config.kill_experience
local mid_advance = math.floor(xp_to_advance) <= wesnoth.game_config.kill_experience * 2
local far_advance = math.floor(xp_to_advance) <= wesnoth.game_config.kill_experience * 3
local color = normal_color
if can_advance then
if near_advance then
color = near_advance_color
elseif mid_advance then
color = mid_advance_color
elseif far_advance then
color = far_advance_color
end
else
if has_amla then
if near_advance then
color = near_amla_color
elseif mid_advance then
color = mid_amla_color
elseif far_advance then
color = far_amla_color
else
color = amla_color
end
end
end
return(color)
end
local function red_green_scale_function(value)
value = math.max(0.0, math.min(100.0, value))
local colors = {}
for color in red_green_scale:gmatch("[^,]+") do
table.insert(colors, color)
end
local index = math.floor(value / 100 * (#colors - 1)) + 1
return colors[index]
end
function hp_color(hitpoints, max_hitpoints)
local unit_energy = 0.0
local energy_color = {0, 0, 0, 255} -- {r, g, b, a}
if max_hitpoints > 0 then
unit_energy = hitpoints / max_hitpoints
end
if unit_energy == 1.0 then
energy_color[1] = 33
energy_color[2] = 225
energy_color[3] = 0
elseif unit_energy > 1.0 then
energy_color[1] = 100
energy_color[2] = 255
energy_color[3] = 100
elseif unit_energy >= 0.75 then
energy_color[1] = 170
energy_color[2] = 255
energy_color[3] = 0
elseif unit_energy >= 0.5 then
energy_color[1] = 255
energy_color[2] = 175
energy_color[3] = 0
elseif unit_energy >= 0.25 then
energy_color[1] = 255
energy_color[2] = 155
energy_color[3] = 0
else
energy_color[1] = 255
energy_color[2] = 0
energy_color[3] = 0
end
return energy_color
end
function table_to_string(input)
local result = {}
for i, v in ipairs(input) do
table.insert(result, tostring(v))
end
return table.concat(result, ", ")
end
local leader_crown = "misc/leader-crown.png"
local size_normal=17
local size_small = 15 * (size_normal / 17)
local function unit_attack_info(unit, cache) --FROM battle.calcs
local cind = 'UI-' .. unit.id .. unit.max_hitpoints .. unit.side
-- If cache for this unit exists, return it
if cache and cache[cind] then
return cache[cind]
end
-- Otherwise collect the information
local unit_info = {
attacks = {},
resist_mod = {},
alignment = unit.alignment
}
local attacks = unit.attacks
for i_a = 1,#attacks do
local attack = attacks[i_a]
-- Extract information for specials; we do this first because some
-- custom special might have the same name as one of the default scalar fields
local a = {}
for _,sp in ipairs(attack.specials) do
if (sp[1] == 'damage') then -- this is 'backstab'
if (sp[2].id == 'backstab') then
a.backstab = true
else
if (sp[2].id == 'charge') then a.charge = true end
end
else
if (sp[1] == 'chance_to_hit') then
a[sp[2].id or 'no_id'] = true
else
a[sp[1]] = true
end
end
end
a.damage = attack.damage
a.type = attack.type
a.range = attack.range
a.number = attack.number or 0
table.insert(unit_info.attacks, a)
end
local attack_types = { "arcane", "blade", "cold", "fire", "impact", "pierce" }
for _,attack_type in ipairs(attack_types) do
unit_info.resist_mod[attack_type] = 1 - unit:resistance_against(attack_type) / 100.
end
if cache then cache[cind] = unit_info end
return unit_info
end
local function strike_damage(attacker, defender, att_weapon, def_weapon, dst, cache)
local att_lawful_bonus = wesnoth.schedule.get_illumination(dst).lawful_bonus
local def_lawful_bonus = wesnoth.schedule.get_illumination(defender).lawful_bonus
local cind = 'SD-' .. attacker.id .. attacker.max_hitpoints .. attacker.side
cind = cind .. 'x' .. defender.id .. defender.max_hitpoints .. defender.side
cind = cind .. '-' .. att_weapon .. 'x' .. def_weapon
cind = cind .. '-' .. att_lawful_bonus .. 'x' .. def_lawful_bonus
if cache and cache[cind] then
return cache[cind].att_damage, cache[cind].def_damage, cache[cind].att_attack, cache[cind].def_attack
end
local attacker_info = unit_attack_info(attacker, cache)
local defender_info = unit_attack_info(defender, cache)
local att_damage = attacker_info.attacks[att_weapon].damage
local att_multiplier = defender_info.resist_mod[attacker_info.attacks[att_weapon].type] or 1
att_multiplier = att_multiplier * AH.get_unit_time_of_day_bonus(attacker_info.alignment, att_lawful_bonus)
local def_damage, def_multiplier = 0, 1.
if (def_weapon ~= 0) then
def_damage = defender_info.attacks[def_weapon].damage
def_multiplier = attacker_info.resist_mod[defender_info.attacks[def_weapon].type] or 1
def_multiplier = def_multiplier * AH.get_unit_time_of_day_bonus(defender_info.alignment, def_lawful_bonus)
end
if attacker_info.attacks[att_weapon].charge then
att_damage = att_damage * 2
def_damage = def_damage * 2
end
if (att_multiplier > 1) then
att_damage = mathx.round(att_damage * att_multiplier - 0.001)
else
att_damage = mathx.round(att_damage * att_multiplier + 0.001)
end
if (def_weapon ~= 0) then
if (def_multiplier > 1) then
def_damage = mathx.round(def_damage * def_multiplier - 0.001)
else
def_damage = mathx.round(def_damage * def_multiplier + 0.001)
end
end
if cache then
cache[cind] = {
att_damage = att_damage,
def_damage = def_damage,
att_attack = attacker_info.attacks[att_weapon],
def_attack = defender_info.attacks[def_weapon]
}
end
return att_damage, def_damage, attacker_info.attacks[att_weapon], defender_info.attacks[def_weapon]
end
local function draw_hp_graph(hp_graph, attacker,attack_location, defender, attacker_weapon_index, for_unit)
local for_unit = for_unit or "attacker"
local background_color_disabled = "64, 64, 64, 64"
local font_color_disabled = "128, 128, 128, 255"
local graph_width = 270
local graph_height = 170
local graph_max_rows = 10
local fs = size_small
local hp_sep = 30
local percent_sep = 50
local bar_space = graph_width - hp_sep - percent_sep - 4
local contents = {wml.tag.rectangle
{
x = 0,
y = 0,
w = "(width)",
h = "(height)",
fill_color = background_color_disabled,
border_thickness = 2,
border_color = font_color_disabled
},wml.tag.rectangle
{
x =hp_sep,
y = 0,
w = 2,
h = "(height - 2)",
fill_color =font_color_disabled
},wml.tag.rectangle
{
x = "(width-2)"..-percent_sep,
y = 0,
w = 2,
h = "(height - 2)",
fill_color =font_color_disabled
}
}
std_print(attacker_weapon_index)
local attacker_condition,defender_condition,attacker_stats,defender_stats = BC.simulate_combat_loc(attacker,attack_location,defender,attacker_weapon_index)
local unit = attacker
local unit_condition = attacker_condition
local unit_stats = attacker_stats
local second_unit = defender
local second_unit_condition = defender_condition
local second_unit_stats = defender_stats
if for_unit ~= "attacker" then
unit = defender
unit_condition = defender_condition
unit_stats = defender_stats
second_unit = attacker
second_unit_condition = attacker_condition
second_unit_stats = attacker_stats
end
local pair_table = {}
for index, value in pairs(unit_condition.hp_chance) do
if value ~= 0 then
table.insert(pair_table, {value = value, index = index})
end
end
table.sort(pair_table, function(a, b) return a.value > b.value end)
local top_ten = {}
for i = 1, math.min(10, #pair_table) do
table.insert(top_ten, pair_table[i])
end
table.sort(top_ten, function(a, b) return a.index > b.index end)
for i = 1, math.min(10, #top_ten) do
local hp = top_ten[i].index
local prob = top_ten[i].value
local row_color
if hp == 0 then
row_color = {229, 0, 0}
elseif hp < unit.hitpoints then
if second_unit_stats.petrifies then
row_color = {154, 154, 154}
else
row_color = {244, 201, 0}
end
else
row_color = {8, 202, 0}
end
local bar_len = math.max(math.floor((prob * (bar_space - 4)) + 0.5), 2)
table.insert(contents,wml.tag.rectangle
{
x = hp_sep + 4,
y = 6 + (fs + 2) * (i - 1),
w = bar_len,
h = 8,
fill_color = string.format("%d, %d, %d", row_color[1], row_color[2], row_color[3]),
})
table.insert(contents,wml.tag.text
{
text = tostring(hp),
x = 4,
y = 2 + (fs + 2) * (i - 1),
w = "(text_width)",
h = "(text_height)",
font_size = 12,
color = "255, 255, 255, 255",
text_alignment = "(text_alignment)",
})
table.insert(contents,wml.tag.text
{
text = string.format("%.1f", (prob*100)).."%",
x = graph_width - percent_sep + 2,
y = 2 + (fs + 2) * (i - 1),
w = "(text_width)",
h = "(text_height)",
font_size = 12,
color = "255, 255, 255, 255",
text_alignment = "(text_alignment)",
})
end
hp_graph:set_canvas(1,contents)
end
local weapon_index_list={}
local attack_locations={}
local distances = {}
local selected_index
function preshow(dialog) --#TODO missing specials, abilities (both in weapon list and combat statistics), no idea what label_helper is, skipping, end no-mainline-dialog, get rid of the gray and selected units
local unit_advances_to = unit.advances_to[1] ~= nil
local unit_amla = unit.advancements[1] ~= nil
local unit_mods = "~RC(magenta>" ..wesnoth.sides[unit.side].color .. ")"..unit.image_mods
if unit.canrecruit then
unit_mods = unit_mods .. "~BLIT(" .. leader_crown .. ")"
end
for i2 = 1, #unit.overlays do
unit_mods = unit_mods .. "~BLIT(" .. unit.overlays[i2] .. ")"
end
dialog["attacker_pane"].type_image.label=unit.__cfg.image .. unit_mods
dialog["attacker_pane"].type_details_minimal.use_markup = true
dialog["attacker_pane"].type_details_minimal.label="<span size='large'>"..unit.name.."</span>\n"
.."<span color='#"..unit_type_color.."'>"..unit.__cfg.language_name.."</span>\n"
.."Lvl "..unit.level.."\n"
..unit.alignment.."\n"
..table_to_string(unit.traits).."\n"
.."<span color='"..rgba_to_hex(hp_color(unit.hitpoints,unit.max_hitpoints)).."'>HP: "..unit.hitpoints.."/"..unit.max_hitpoints.."</span>\n"
.."<span color='"..rgba_to_hex(xp_color(unit.max_experience - unit.experience,unit_advances_to,unit_amla)).."'>XP: "..unit.experience.."/"..unit.max_experience.."</span>"
--dialog["attacker_pane"].type_name.label="type_name"
dialog["attacker_pane"].type_profile.on_button_click= function() gui.show_help("unit_"..unit.type) end
local second_unit_advances_to = second_unit.advances_to[1] ~= nil
local second_unit_amla = second_unit.advancements[1] ~= nil
local second_unit_mods = "~RC(magenta>" ..wesnoth.sides[second_unit.side].color .. ")"..second_unit.image_mods
if second_unit.canrecruit then
second_unit_mods = second_unit_mods .. "~BLIT(" .. leader_crown .. ")"
end
for i2 = 1, #second_unit.overlays do
second_unit_mods = second_unit_mods .. "~BLIT(" .. second_unit.overlays[i2] .. ")"
end
second_unit_mods = second_unit_mods .. "~FL(horiz)"
dialog["defender_pane"].type_image.label=second_unit.__cfg.image .. second_unit_mods
dialog["defender_pane"].type_details_minimal.use_markup = true
dialog["defender_pane"].type_details_minimal.label="<span size='large'>"..second_unit.name.."</span>\n"
.."<span color='#"..unit_type_color.."'>"..second_unit.__cfg.language_name.."</span>\n"
.."Lvl "..second_unit.level.."\n"
..second_unit.alignment.."\n"
..table_to_string(second_unit.traits).."\n"
.."<span color='"..rgba_to_hex(hp_color(second_unit.hitpoints,second_unit.max_hitpoints)).."'>HP: "..second_unit.hitpoints.."/"..second_unit.max_hitpoints.."</span>\n"
.."<span color='"..rgba_to_hex(xp_color(second_unit.max_experience - second_unit.experience,second_unit_advances_to,second_unit_amla)).."'>XP: "..second_unit.experience.."/"..second_unit.max_experience.."</span>"
dialog["defender_pane"].type_profile.on_button_click= function() gui.show_help("unit_"..second_unit.type) end
local weapon_row = 1
for i = 1, #unit.attacks do
attack_locations[i]=-1
distances[i]=-1
if unit.attacks[i].attacks_used <= unit.attacks_left then
local seek_paths = true
if unit.attacks[i].__cfg.max_range >= distance and distance >= unit.attacks[i].__cfg.min_range and unit.attacks[i]:matches({special_type_active="disable"}) == false then
local _,_,attacker_stats,_ = wesnoth.simulate_combat(unit,i , second_unit)
std_print("max, distance, min:"..unit.attacks[i].__cfg.max_range.." >= "..distance.." >= "..unit.attacks[i].__cfg.min_range)
if attacker_stats.num_blows ~= 0 then
seek_paths = false
end
attack_locations[i]=wesnoth.named_tuple({unit.x,unit.y}, {'x', 'y'})
distances[i]=wesnoth.map.distance_between(unit.loc, second_unit.loc)
end
if seek_paths then
std_print("seeking paths")
local map=wesnoth.paths.find_cost_map(unit,{},{ignore_units=false, ignore_teleport=false, viewing_side=unit.side})
local best_cost = {0,0,unit.max_moves + 1}
for map_iter = 1, #map do
if map[map_iter][1] == 9 and map[map_iter][2] == 11 then
std_print("#############################################")
std_print("moves needed: "..map[map_iter][3])
std_print("dist "..wesnoth.map.distance_between({map[map_iter][1],map[map_iter][2]},second_unit.loc))
std_print("#############################################")
end
if map[map_iter][3] <= unit.max_moves and map[map_iter][3] ~= -1 then
local dist = wesnoth.map.distance_between({map[map_iter][1],map[map_iter][2]},second_unit.loc)
std_print("dist "..dist)
if map[map_iter][3] < best_cost[3] and wesnoth.units.get({map[map_iter][1],map[map_iter][2]}) == nil then
if unit.attacks[i].__cfg.max_range >= dist and unit.attacks[i].__cfg.min_range <= dist then
best_cost={map[map_iter][1],map[map_iter][2],map[map_iter][3]}
end
end
end
end
std_print(best_cost[1],best_cost[2],best_cost[3])
if best_cost[3] ~= unit.max_moves + 1 then
attack_locations[i]=wesnoth.named_tuple({best_cost[1],best_cost[2]}, {'x', 'y'})
distances[i] = wesnoth.map.distance_between(attack_locations[i], second_unit.loc)
end
end
end
--std_print("Best attack tile and coset: "..attack_locations[i][1],attack_locations[i][2],attack_locations[i][3])
--std_print("counted if weapon usable "..attack_locations[i])
if attack_locations[i] ~= -1 then
std_print("making weapon row")
--BfBH.table.std_print(attack_locations[i])
local weapon = unit.attacks[i]
local _,_,attacker_stats,defender_stats = BC.simulate_combat_loc(unit, attack_locations[i], second_unit, i)
if attacker_stats.num_blows ~= 0 then
std_print("attacker stats chance to hit: "..attacker_stats.chance_to_hit)
local second_weapon = nil
if defender_stats.attack_num ~= -1 then
second_weapon = second_unit.attacks[defender_stats.attack_num + 1]
end
weapon_index_list[#weapon_index_list + 1] = {i, defender_stats.attack_num + 1}
dialog["weapon_list"][weapon_row].attacker_weapon.use_markup = true
dialog["weapon_list"][weapon_row].attacker_weapon_icon.label = weapon.icon
dialog["weapon_list"][weapon_row].attacker_weapon.label="<b>"..weapon.name.."</b>\n"
..weapon.type.."\n"
..mathx.round(attacker_stats.damage).."×"..mathx.round(attacker_stats.num_blows).."\n"
.."<span color='#"..red_green_scale_function(attacker_stats.chance_to_hit).."'>"..mathx.round(attacker_stats.chance_to_hit).."%</span>"
dialog["weapon_list"][weapon_row].attacker_weapon.tooltip="Weapon: <b>"..weapon.name.."</b>\n"
.."Type: "..weapon.type.."\n"
.."Damage: "..mathx.round(attacker_stats.damage).."\n"
.."Attacks: "..mathx.round(attacker_stats.num_blows).."\n"
.."Chance to hit: <span color='#"..red_green_scale_function(attacker_stats.chance_to_hit).."'>"..mathx.round(attacker_stats.chance_to_hit).."%</span>"
dialog["weapon_list"][weapon_row].range.use_markup = true
dialog["weapon_list"][weapon_row].range.label="<span color='#a69275'>— "..weapon.range.." —</span>"
dialog["weapon_list"][weapon_row].defender_weapon.use_markup = true
if second_weapon ~= nil then
dialog["weapon_list"][weapon_row].defender_weapon_icon.label = second_weapon.icon
dialog["weapon_list"][weapon_row].defender_weapon.label="<b>"..second_weapon.name.."</b>\n"
..second_weapon.type.."\n"
..mathx.round(defender_stats.damage).."×"..mathx.round(defender_stats.num_blows).."\n"
.."<span color='#"..red_green_scale_function(defender_stats.chance_to_hit).."'>"..mathx.round(defender_stats.chance_to_hit).."%</span>"
dialog["weapon_list"][weapon_row].defender_weapon.tooltip="Weapon: <b>"..second_weapon.name.."</b>\n"
.."Type: "..second_weapon.type.."\n"
.."Damage: "..mathx.round(defender_stats.damage).."\n"
.."Attacks: "..mathx.round(defender_stats.num_blows).."\n"
.."Chance to hit: <span color='#"..red_green_scale_function(defender_stats.chance_to_hit).."'>"..mathx.round(defender_stats.chance_to_hit).."%</span>"
else
dialog["weapon_list"][weapon_row].defender_weapon_icon.label = "attacks/blank-attack.png"
dialog["weapon_list"][weapon_row].defender_weapon.label="<b>".."".."</b>\n"
.."".."\n"
.."0".."×".."0".."\n"
.."<span color='#"..red_green_scale_function(0).."'>".."0".."%</span>"
end
weapon_row = weapon_row + 1
--BfBH.table.std_print(attacker_stats)
end
end
end
local best_rating=nil
for iter=1, #weapon_index_list do
local rating = BC.ranged_attack_rating(unit, second_unit, { attack_locations[weapon_index_list[iter][1]].x, attack_locations[weapon_index_list[iter][1]].y }, {att_weapon = weapon_index_list[iter][1], def_weapon = weapon_index_list[iter][2]} )
if best_rating == nil or rating > best_rating then
best_rating=rating
dialog["weapon_list"].selected_index = iter
end
end
dialog.damage_calculation.on_button_click= function()
function preshow(dialog_2)
local weapon_index = weapon_index_list[dialog["weapon_list"].selected_index][1]
local weapon = unit.attacks[weapon_index]
local attacker_condition,defender_condition,attacker_stats,defender_stats = BC.simulate_combat_loc(unit,attack_locations[weapon_index],second_unit,weapon_index)
local opp_weapon_index = nil
local opp_weapon = nil
if defender_stats.attack_num ~= -1 then
opp_weapon_index = weapon_index_list[dialog["weapon_list"].selected_index][2]
opp_weapon = second_unit.attacks[opp_weapon_index]
end
draw_hp_graph(dialog_2["attacker_hp_graph"],unit,attack_locations[weapon_index],second_unit,weapon_index)
draw_hp_graph(dialog_2["defender_hp_graph"],unit,attack_locations[weapon_index],second_unit,weapon_index,"defender")
dialog_2["attacker_chance_unscathed"].label = "<span color='#"..red_green_scale_function(attacker_condition.untouched * 100).."'>"..string.format("%.1f", (attacker_condition.untouched * 100)).."%</span>"
if unit.attacks[weapon_index] then
dialog_2["attacker_base_damage"].label = unit.attacks[weapon_index].damage.." (<i>"..unit.attacks[weapon_index].name.."</i>)"
local resistance_modifier = 100 - wesnoth.units.resistance_against(second_unit, weapon.type)
if resistance_modifier ~= 100 then
local ss = ""
if resistance_modifier < 100 then
ss = ss..("Attacker resistance vs").." " --#TODOmake it translate-able string
else
ss = ss..("Attacker vulnerability vs").." " --#TODO make it translate-able string
end
dialog_2["attacker_resis"].label = "×" .. math.floor(resistance_modifier / 100) .. "." .. math.floor((resistance_modifier % 100) / 10)
dialog_2["attacker_resis_label"].label=ss .. weapon.type
dialog_2["attacker_resis_label"].visible="visible"
else
dialog_2["attacker_resis"].label = ""
dialog_2["attacker_resis_label"].label = ""
dialog_2["attacker_resis_label"].visible = "hidden"
end
--wesnoth.map.get(unit.loc).illuminated_time --#TODO
--unit.alignment
--#wesnoth.units.find({fearless=true,x=unit.x,y=unit.y})
dialog_2["attacker_tod_modifier"].label = ""
dialog_2["attacker_tod_modifier_label"].label = ""
dialog_2["attacker_tod_modifier_label"].visible = "hidden"
dialog_2["attacker_leadership_modifier"].label = ""
dialog_2["attacker_leadership_modifier_label"].label = ""
dialog_2["attacker_leadership_modifier_label"].visible = "hidden"
dialog_2["attacker_slowed_modifier"].label = ""
dialog_2["attacker_slowed_modifier_label"].label = ""
dialog_2["attacker_slowed_modifier_label"].visible = "hidden"
local ss = math.floor(attacker_stats.damage)
if attacker_stats.damage > weapon.damage then
ss="<span color='"..rgba_to_hex(good_dmg_color).."'>" .. ss .. "</span>"
elseif attacker_stats.damage < weapon.damage then
ss="<span color='"..rgba_to_hex(bad_dmg_color).."'>" .. ss .. "</span>"
end
dialog_2["attacker_total_damage"].label = ss .."×"..math.floor(attacker_stats.num_blows)
dialog_2["attacker_chance_to_hit"].label = "<span color='#"..red_green_scale_function(attacker_stats.chance_to_hit).."'>"..attacker_stats.chance_to_hit.. "%</span>"
end
dialog_2["defender_chance_unscathed"].label = "<span color='#"..red_green_scale_function(defender_condition.untouched * 100).."'>"..string.format("%.1f", (defender_condition.untouched * 100)).."%</span>"
if opp_weapon then
dialog_2["defender_base_damage"].label = second_unit.attacks[opp_weapon_index].damage.." (<i>"..second_unit.attacks[opp_weapon_index].name.."</i>)"
local resistance_modifier = 100 - wesnoth.units.resistance_against(unit, opp_weapon.type)
if resistance_modifier ~= 100 then
local ss = ""
if resistance_modifier < 100 then
ss = ss..("Defender resistance vs").." " --#TODOmake it translate-able string
else
ss = ss..("Defender vulnerability vs").." " --#TODO make it translate-able string
end
dialog_2["defender_resis"].label = "×" .. math.floor(resistance_modifier / 100) .. "." .. math.floor((resistance_modifier % 100) / 10)
dialog_2["defender_resis_label"].label=ss .. opp_weapon.type
dialog_2["defender_resis_label"].visible="visible"
else
dialog_2["defender_resis"].label = ""
dialog_2["defender_resis_label"].label = ""
dialog_2["defender_resis_label"].visible = "hidden"
end
--wesnoth.map.get(unit.loc).illuminated_time
--unit.alignment
--#wesnoth.units.find({fearless=true,x=unit.x,y=unit.y}) --nefunguje, filtrovat trait
dialog_2["defender_tod_modifier"].label = ""
dialog_2["defender_tod_modifier_label"].label = ""
dialog_2["defender_tod_modifier_label"].visible = "hidden"
dialog_2["defender_leadership_modifier"].label = ""
dialog_2["defender_leadership_modifier_label"].label = ""
dialog_2["defender_leadership_modifier_label"].visible = "hidden"
dialog_2["defender_slowed_modifier"].label = ""
dialog_2["defender_slowed_modifier_label"].label = ""
dialog_2["defender_slowed_modifier_label"].visible = "hidden"
local ss = math.floor(defender_stats.damage)
if defender_stats.damage > opp_weapon.damage then
ss="<span color='"..rgba_to_hex(good_dmg_color).."'>" .. ss .. "</span>"
elseif defender_stats.damage < opp_weapon.damage then
ss="<span color='"..rgba_to_hex(bad_dmg_color).."'>" .. ss .. "</span>"
end
dialog_2["defender_total_damage"].label = ss .."×"..math.floor(defender_stats.num_blows)
dialog_2["defender_chance_to_hit"].label = "<span color='#"..red_green_scale_function(defender_stats.chance_to_hit).."'>"..defender_stats.chance_to_hit.. "%</span>"
else
dialog_2["defender_base_damage"].label = "No usable weapon"
dialog_2["defender_resis"].label = ""
dialog_2["defender_resis_label"].label = ""
dialog_2["defender_resis_label"].visible = "hidden"
dialog_2["defender_tod_modifier"].label = ""
dialog_2["defender_tod_modifier_label"].label = ""
dialog_2["defender_tod_modifier_label"].visible = "hidden"
dialog_2["defender_leadership_modifier"].label = ""
dialog_2["defender_leadership_modifier_label"].label = ""
dialog_2["defender_leadership_modifier_label"].visible = "hidden"
dialog_2["defender_slowed_modifier"].label = ""
dialog_2["defender_slowed_modifier_label"].label = ""
dialog_2["defender_slowed_modifier_label"].visible = "hidden"
end
end
function postshow(dialog_2)
end
gui.show_dialog(wml.get_child(attack_predictions, 'resolution'), preshow, postshow)
end
end
function postshow(dialog)
selected_index = dialog["weapon_list"].selected_index
end
local return_value=gui.show_dialog(wml.get_child(unit_attack, 'resolution'), preshow, postshow)
if return_value == -1 then
--wesnoth.interface.select_unit(unit.x,unit.y,false)
--wesnoth.interface.select_unit(unit.x,unit.y)
wesnoth.select_unit(nil)
local path = wesnoth.paths.find_path({unit.x,unit.y},{attack_locations[weapon_index_list[selected_index][1]].x,attack_locations[weapon_index_list[selected_index][1]].y})
local path_x,path_y = unit.x,unit.y
for i=2, #path do
path_x = path_x..","..path[i][1]
path_y = path_y..","..path[i][2]
end
if attack_locations[weapon_index_list[selected_index][1]].x ~= unit.x or attack_locations[weapon_index_list[selected_index][1]].y ~= unit.y then
wesnoth.wml_actions.do_command({
{"move",{x=path_x, y=path_y}}
})
end
if unit.x == attack_locations[weapon_index_list[selected_index][1]].x and unit.y == attack_locations[weapon_index_list[selected_index][1]].y then
wesnoth.wml_actions.do_command({
{"attack",{weapon = (weapon_index_list[selected_index][1] - 1),defender_weapon = (weapon_index_list[selected_index][2] - 1),{"source", {x = unit.x, y = unit.y}}, {"destination", {x = second_unit.x, y = second_unit.y}}}}
})
end
wesnoth.select_unit(nil)
else
wesnoth.interface.deselect_hex()
wesnoth.units.select(unit)
-- unit:extract()
-- unit:to_map()
-- unit.moves=0
--unit.x=unit.x+1
std_print("NO ATTACK")
-- wesnoth.wml_actions.do_command({
-- {"attack",{weapon = #unit.attacks,defender_weapon = (weapon_index_list[selected_index][2] - 1),{"source", {x = unit.x, y = unit.y}}, {"destination", {x = second_unit.x, y = second_unit.y}}}}
-- })
--unit:extract()
--wesnoth.game_events.fire("empty_event", unit.loc, second_unit.loc)
end
return true
end
>>
[/lua]
[/event]
[/modification]