~widelands-dev/widelands/trunk

« back to all changes in this revision

Viewing changes to test/maps/plain.wmf/scripting/test_warehouse_garrison.lua

  • Committer: The Widelands Bunnybot
  • Date: 2025-06-16 08:27:43 UTC
  • Revision ID: bunnybot@widelands.org-20250616082743-ttc14q6ld62yjz42
Improve warehouse soldier preference handling (CB #4952 / GH #6592)

Co-authored-by: Tóth András <txa-dev@posteo.hu>
Co-authored-by: Widelands Bunnybot <bunnybot@widelands.org>
Co-committed-by: Tóth András <txa-dev@posteo.hu>

(by bunnybot)
d9fe63d85ec006edeef2f05b68401b542801b59b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
include "test/scripting/stable_save.lua"
 
2
 
 
3
run(function()
 
4
   print("Testing headquarters garrison settings")
 
5
 
 
6
   game.desired_speed = 1000
 
7
   sleep(1000)
 
8
 
 
9
   local hqs = p1:get_buildings("barbarians_headquarters")
 
10
   assert_equal(1, #hqs)
 
11
   local hq = hqs[1]
 
12
   local fhq = hq.flag
 
13
 
 
14
   hq:set_soldiers{
 
15
      -- Mixed order, to test sorting on insertion
 
16
      [{0, 2, 0, 2}] = 1,
 
17
      [{0, 3, 0, 2}] = 1,
 
18
      [{3, 5, 0, 2}] = 1,
 
19
      [{0, 0, 0, 1}] = 1,
 
20
      [{0, 4, 0, 2}] = 1,
 
21
      [{0, 0, 0, 0}] = 1,
 
22
      [{0, 5, 0, 2}] = 1,
 
23
      [{2, 5, 0, 2}] = 1,
 
24
      [{1, 5, 0, 2}] = 1,
 
25
      [{0, 1, 0, 2}] = 1,
 
26
      [{0, 0, 0, 2}] = 1,
 
27
   }
 
28
 
 
29
   assert_equal(11, hq:get_workers("barbarians_soldier"))
 
30
 
 
31
   -- test limiting soldier capacity
 
32
   hq.garrison = hq.max_garrison + 20  -- should be capped
 
33
   assert_equal(hq.max_garrison, hq.garrison)
 
34
   -- negative numbers are cast to large positive ones, no need for separate test
 
35
 
 
36
   -- also for warehouse
 
37
   wh = p1:place_building("barbarians_warehouse", map:get_field(18, 25), false, true)
 
38
   assert_equal(0, wh.max_garrison)
 
39
   wh.garrison = 20  -- should be ignored
 
40
   assert_equal(0, wh.garrison)
 
41
   wh:remove()
 
42
 
 
43
   hq.soldier_preference = "heroes"
 
44
   hq.garrison = 10  -- one free soldier
 
45
 
 
46
   sentry = p1:place_building("barbarians_sentry", map:get_field(24, 25), false, true)
 
47
   assert_equal("rookies", sentry.soldier_preference)
 
48
   assert_equal(2, sentry.capacity)
 
49
 
 
50
   -- test limiting military sites too
 
51
   assert_equal(2, sentry.max_soldiers)
 
52
   sentry.capacity = 5
 
53
   assert_equal(2, sentry.capacity)
 
54
 
 
55
   -- We're all set, let's test loading too
 
56
   stable_save(game, "warehouse_garrison", 1000)
 
57
 
 
58
   -- Connect sentry to HQ, the weakest soldier should occupy the sentry
 
59
   print("Allowing only the weakest soldier to the sentry")
 
60
   sentry.soldier_preference = "heroes"
 
61
   connected_road("normal", p1, fhq, "r,r|", true)
 
62
 
 
63
   game.desired_speed = 20000
 
64
 
 
65
   while sentry:get_soldiers("present") ~= 1 do
 
66
      sleep(1000)
 
67
   end
 
68
   sleep(1000)
 
69
 
 
70
   assert_equal(10, hq:get_workers("barbarians_soldier"))
 
71
   assert_equal(1, sentry:get_soldiers("associated"))
 
72
   assert_equal(1, sentry:get_soldiers{0, 0, 0, 0}, "Not the weakest soldier occupied the sentry")
 
73
 
 
74
   -- Free up more soldiers
 
75
   print("Allow stronger soldiers to the sentry")
 
76
   hq.garrison = 3
 
77
 
 
78
   sleep(1000)
 
79
   while sentry:get_soldiers("present") < 2 or sentry:get_soldiers("associated") > 2 do
 
80
      sleep(1000)
 
81
   end
 
82
 
 
83
   assert_equal(1, sentry:get_soldiers{0, 5, 0, 2}, "Expected soldier is not in the sentry")
 
84
   if sentry:get_soldiers{0, 0, 0, 0} == 1 then
 
85
      -- exchange was not triggered, let's force it
 
86
      sentry.capacity = 1
 
87
      sleep(1000)
 
88
      sentry.capacity = 2
 
89
      while sentry:get_soldiers("present") < 2 or sentry:get_soldiers("associated") > 2 do
 
90
         sleep(1000)
 
91
      end
 
92
   end
 
93
 
 
94
   -- make sure the soldier reached the HQ
 
95
   while hq:get_soldiers("present") < 9 do
 
96
      sleep(1000)
 
97
   end
 
98
 
 
99
   assert_equal(9, hq:get_soldiers("associated"))
 
100
   assert_equal(0, sentry:get_soldiers{0, 0, 0, 0}, "Weakest soldier is still in the sentry")
 
101
   assert_equal(1, sentry:get_soldiers{0, 5, 0, 2}, "Expected soldier is not in the sentry")
 
102
   assert_equal(1, sentry:get_soldiers{0, 4, 0, 2}, "Expected soldier is not in the sentry")
 
103
   assert_equal(1, hq:get_soldiers{3, 5, 0, 2}, "Expected soldier is not in the HQ")
 
104
   assert_equal(1, hq:get_soldiers{2, 5, 0, 2}, "Expected soldier is not in the HQ")
 
105
   assert_equal(1, hq:get_soldiers{1, 5, 0, 2}, "Expected soldier is not in the HQ")
 
106
   assert_equal(0, hq:get_soldiers{0, 5, 0, 2}, "Unexpected soldier is in the HQ")
 
107
   assert_equal(0, hq:get_soldiers{0, 4, 0, 2}, "Unexpected soldier is in the HQ")
 
108
   assert_equal(1, hq:get_soldiers{0, 3, 0, 2}, "Expected soldier is not in the HQ")
 
109
   assert_equal(1, hq:get_soldiers{0, 0, 0, 0}, "Expected soldier is not in the HQ")
 
110
 
 
111
   -- Release the strongest soldiers and force exchange
 
112
   print("Allow the strongest soldiers to the sentry")
 
113
   hq.soldier_preference = "rookies"
 
114
   sentry.capacity = 1
 
115
   sleep(1000)
 
116
   sentry.capacity = 2
 
117
   sleep(1000)
 
118
 
 
119
   while sentry:get_soldiers("present") < 2 or sentry:get_soldiers("associated") > 2 do
 
120
      sleep(1000)
 
121
   end
 
122
 
 
123
   assert_equal(1, sentry:get_soldiers{3, 5, 0, 2}, "Expected soldier is not in the sentry")
 
124
   if sentry:get_soldiers{2, 5, 0, 2} == 0 then
 
125
      -- second exchange was not triggered, let's force it
 
126
      sentry.capacity = 1
 
127
      sleep(1000)
 
128
      sentry.capacity = 2
 
129
      while sentry:get_soldiers("present") < 2 or sentry:get_soldiers("associated") > 2 do
 
130
         sleep(1000)
 
131
      end
 
132
   end
 
133
 
 
134
   -- make sure the soldier reached the HQ
 
135
   while hq:get_soldiers("present") < 9 do
 
136
      sleep(1000)
 
137
   end
 
138
 
 
139
   assert_equal(9, hq:get_soldiers("associated"))
 
140
   assert_equal(0, sentry:get_soldiers{0, 5, 0, 2}, "Unexpected soldier is in the sentry")
 
141
   assert_equal(0, sentry:get_soldiers{0, 4, 0, 2}, "Unexpected soldier is in the sentry")
 
142
   assert_equal(1, sentry:get_soldiers{3, 5, 0, 2}, "Expected soldier is not in the sentry")
 
143
   assert_equal(1, sentry:get_soldiers{2, 5, 0, 2}, "Expected soldier is not in the sentry")
 
144
   assert_equal(0, hq:get_soldiers{3, 5, 0, 2}, "Unexpected soldier is in the HQ")
 
145
   assert_equal(0, hq:get_soldiers{2, 5, 0, 2}, "Unexpected soldier is in the HQ")
 
146
   assert_equal(1, hq:get_soldiers{1, 5, 0, 2}, "Expected soldier is not in the HQ")
 
147
   assert_equal(1, hq:get_soldiers{0, 5, 0, 2}, "Expected soldier is not in the HQ")
 
148
   assert_equal(1, hq:get_soldiers{0, 4, 0, 2}, "Expected soldier is not in the HQ")
 
149
 
 
150
   -- Try to get the weakest ones to go to the sentry
 
151
   print("Request rookies but don't allow the weakest soldiers to the sentry")
 
152
   sentry.soldier_preference = "rookies"
 
153
   sentry.capacity = 1
 
154
   sleep(1000)
 
155
   sentry.capacity = 2
 
156
   sleep(1000)
 
157
 
 
158
   while sentry:get_soldiers("present") < 2 or sentry:get_soldiers("associated") > 2 do
 
159
      sleep(1000)
 
160
   end
 
161
 
 
162
   assert_equal(1, sentry:get_soldiers{0, 1, 0, 2}, "Expected soldier is not in the sentry")
 
163
   if sentry:get_soldiers{0, 2, 0, 2} == 0 then
 
164
      -- second exchange was not triggered, let's force it
 
165
      sentry.capacity = 1
 
166
      sleep(1000)
 
167
      sentry.capacity = 2
 
168
      while sentry:get_soldiers("present") < 2 or sentry:get_soldiers("associated") > 2 do
 
169
         sleep(1000)
 
170
      end
 
171
   end
 
172
 
 
173
   -- make sure the soldier reached the HQ
 
174
   while hq:get_soldiers("present") < 9 do
 
175
      sleep(1000)
 
176
   end
 
177
 
 
178
   assert_equal(9, hq:get_soldiers("associated"))
 
179
   assert_equal(0, sentry:get_soldiers{0, 0, 0, 0}, "Unexpected soldier is in the sentry")
 
180
   assert_equal(0, sentry:get_soldiers{3, 5, 0, 2}, "Unexpected soldier is in the sentry")
 
181
   assert_equal(1, sentry:get_soldiers{0, 1, 0, 2}, "Expected soldier is not in the sentry")
 
182
   assert_equal(1, sentry:get_soldiers{0, 2, 0, 2}, "Expected soldier is not in the sentry")
 
183
   assert_equal(1, hq:get_soldiers{3, 5, 0, 2}, "Expected soldier is not in the HQ")
 
184
   assert_equal(1, hq:get_soldiers{2, 5, 0, 2}, "Expected soldier is not in the HQ")
 
185
   assert_equal(1, hq:get_soldiers{0, 0, 0, 0}, "Expected soldier is not in the HQ")
 
186
   assert_equal(1, hq:get_soldiers{0, 0, 0, 1}, "Expected soldier is not in the HQ")
 
187
   assert_equal(1, hq:get_soldiers{0, 0, 0, 2}, "Expected soldier is not in the HQ")
 
188
 
 
189
   print("# All Tests passed.")
 
190
   wl.ui.MapView():close()
 
191
end)