forked from nedbrek/TrueAtlanteans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_utils.tcl
216 lines (184 loc) · 4.34 KB
/
client_utils.tcl
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
package require atlantis_dbtools
package require atlantis_reader
package require Itcl
package provide client_utils 1.0
### classes
itcl::class Unit {
public variable db_id
public variable name; # not including "(num)"
public variable num; # just the num
public variable items; # list of items
public variable orders
public variable skills
public variable flags
public variable region
public variable object
constructor {args} {
set db_id [dGet $args Id]
set name [dGet $args Name]
set num [dGet $args Num]
set items [dGet $args Items]
set orders [dGet $args Orders]
set skills [dGet $args Skills]
set flags [dGet $args Flags]
set region [dGet $args Region]
set object [dGet $args Object]
if {$name eq ""} {
if {[lindex $num 0] eq "new"} {
set name $num
} else {
set name "Unit $num"
}
}
}
method print {} {
puts "$name ($num) '$flags' '$skills' '$orders' '$items'"
}
# filter instant orders, including "form <id>"/end and "turn/endturn"
# return new units (created by form)
method filterInstantOrders {}
method countItem {abbr} {
return [::countItem $items $abbr]
}
method setItem {abbr ct} {
set full_abbr [format {[%s]} $abbr]
for {set i 0} {$i < [llength $items]} {incr i} {
set il [lindex $items $i]
if {[lindex $il 2] eq $full_abbr} {
set il [lreplace $il 0 0 $ct]
set items [lreplace $items $i $i $il]
return
}
}
set name [db onecolumn {SELECT name FROM items WHERE abbr=$abbr}]
lappend items [list $ct $name $full_abbr]
}
}
itcl::body Unit::filterInstantOrders {} {
set bool_flags {
avoid
hold
behind
noaid
guard
nocross
autotax
}
set enum_flags {
consume
reveal
}
set enum_flag_vals {
""
unit
faction
}
foreach {x y z} $region {}
set new_units [list]
set new_orders [list]
set skip 0
for {set i 0} {$i < [llength $orders]} {incr i} {
set o [cleanOrder [lindex $orders $i]]
set cmd [string tolower [lindex $o 0]]
if {$skip} {
if {$cmd eq "endturn"} {
set skip 0
}
continue
}
if {$cmd eq "turn"} {
set skip 1
continue
}
# handle bool flags
if {[lsearch $bool_flags $cmd] != -1} {
set arg [lindex $o 1]
if {[checkBool $arg]} {
puts "$name ($x, $y, $z) $cmd bad argument $arg"
}
# TODO: apply flags
continue
}
# enum flags
if {[lsearch $enum_flags $cmd] != -1} {
set arg [lindex $o 1]
if {[lsearch -nocase $enum_flag_vals $arg] == -1} {
puts "$name ($x, $y, $z) $cmd bad argument $arg"
}
# TODO: apply flags
continue
}
# TODO spoils
if {$cmd eq "spoils"} {
continue
}
# anything but form
if {![regexp {^form +(.*)} $o -> new_id]} {
lappend new_orders $o
continue
}
set new_o [list]
for {incr i} {$i < [llength $orders]} {incr i} {
set o [lindex $orders $i]
if {[regexp { *end *$} $o]} {
break
}
lappend new_o $o
}
set new_unit [itcl::code [Unit #auto Num "new $new_id" Orders $new_o Flags $flags Region $region Object $object]]
lappend new_units $new_unit
set tmp [$new_unit filterInstantOrders]
if {$tmp ne ""} {
puts "Error: nested FORM will not work [$u cget -name] ($x, $y, $z)"
itcl:delete object $tmp
}
}
set orders $new_orders
return $new_units
}
proc loadData {filename} {
set tfile [open $filename]
updateDb db [reader::parseFile $tfile]
close $tfile
}
proc getBuyRace {sells peasants} {
set maxRace 0
set raceList [list]
set price 0
foreach {saleItem cost} $sells {
set race [string trim [lindex $saleItem end] {[]}]
if {[lsearch $::men $race] == -1} {
set fullRace [lrange $saleItem 1 end-1]
if {[lsearch $peasants $fullRace] == -1} {
continue
}
lappend ::men $race
}
set ct [lindex $saleItem 0]
lappend raceList [join [lrange $saleItem 1 end]]
set maxRace [expr {max($maxRace, $ct)}]
set price $cost
}
return [list $maxRace $raceList $price]
}
proc getUnitObjects {id} {
set res [::db eval {
SELECT x, y, z
FROM detail
WHERE id=$id
}]
foreach {x y z} $res {}
set region "$x $y $z"
set ret [list]
set units [::db eval {
SELECT id, name, uid, orders, items, skills, flags
FROM units
WHERE units.regionId=$id AND units.detail='own'
}]
foreach {id name num orders items skills flags} $units {
lappend ret [Unit #auto Id $id \
Name $name Num $num Items $items Orders $orders Skills $skills Flags $flags Region $region Object ""
]
}
return $ret
}