forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptation.rb
104 lines (91 loc) · 2.67 KB
/
adaptation.rb
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
# View or set cavern adaptation levels
# based on removebadthoughts.rb
=begin
adaptation
==========
View or set level of cavern adaptation for the selected unit or the whole fort.
Usage: ``adaptation <show|set> <him|all> [value]``. The ``value`` must be
between 0 and 800,000 inclusive.
=end
# Color constants, values mapped to color_value enum in include/ColorText.h
COLOR_GREEN = 2
COLOR_RED = 4
COLOR_YELLOW = 14
COLOR_WHITE = 15
def usage(s)
if nil != s
puts(s)
end
puts "Usage: adaptation <show|set> <him|all> [value]"
throw :script_finished
end
mode = $script_args[0] || 'help'
who = $script_args[1]
value = $script_args[2]
if 'help' == mode
usage(nil)
elsif 'show' != mode && 'set' != mode
usage("Invalid mode '#{mode}': must be either 'show' or 'set'")
end
if nil == who
usage("Target not specified")
elsif 'him' != who && 'all' != who
usage("Invalid target '#{who}'")
end
if 'set' == mode
if nil == value
usage("Value not specified")
elsif !/[[:digit:]]/.match(value)
usage("Invalid value '#{value}'")
end
if 0 > value.to_i || 800000 < value.to_i
usage("Value must be between 0 and 800000")
end
value = value.to_i
end
num_set = 0
set_adaptation_value = lambda { |u,v|
next if !df.unit_iscitizen(u)
next if u.flags2.killed
u.status.misc_traits.each { |t|
if t.id == :CaveAdapt
# TBD: expose the color_ostream console and color values of
# t.value based on adaptation level
if mode == 'show'
if df.respond_to?(:print_color)
print "Unit #{u.id} (#{u.name}) has an adaptation of "
case t.value
when 0..399999
#df.print_color(COLOR_GREEN, "#{t.value}\n")
print "#{t.value}\n"
when 400000..599999
df.print_color(COLOR_YELLOW, "#{t.value}\n")
else
df.print_color(COLOR_RED, "#{t.value}\n")
end
else
puts "Unit #{u.id} (#{u.name}) has an adaptation of #{t.value}"
end
elsif mode == 'set'
puts "Unit #{u.id} (#{u.name}) changed from #{t.value} to #{v}"
t.value = v
num_set += 1
end
end
}
}
case who
when 'him'
if u = df.unit_find
set_adaptation_value[u,value]
else
puts 'Please select a dwarf ingame'
end
when 'all'
df.unit_citizens.each { |uu|
set_adaptation_value[uu,value]
}
end
if 'set' == mode
puts "#{num_set} unit#{'s' if num_set != 1} updated."
end