Merge lp:~widelands-dev/widelands/compass into lp:widelands

Proposed by cghislai
Status: Merged
Merged at revision: 6690
Proposed branch: lp:~widelands-dev/widelands/compass
Merge into: lp:widelands
Diff against target: 291 lines (+141/-40)
3 files modified
src/logic/dismantlesite.cc (+7/-1)
src/logic/soldier.cc (+115/-31)
src/logic/worker.cc (+19/-8)
To merge this branch: bzr merge lp:~widelands-dev/widelands/compass
Reviewer Review Type Date Requested Status
SirVer Needs Fixing
Review via email: mp+177280@code.launchpad.net

Description of the change

These few fixes help a little bit workers to find their way correctly.
- Builder will correctly return to base flag after dismantling
- Workers will correctly return to baseflag if building is being dismantled
- Soldiers will try to get back to homeland if their building is destroyed. Only then they will become fugitive, but will most probably find a flag. If no land is in sight (2*sight), they will become fugitive and will most likely die.

To post a comment you must log in.
Revision history for this message
SirVer (sirver) wrote :

I think the code looks fine, however this had merge conflicts when i tried to merge it to trunk and I am too tired to figure them out right now. Could you do this please?

review: Needs Fixing
Revision history for this message
cghislai (charlyghislain) wrote :

Fixed them

Revision history for this message
SirVer (sirver) wrote :

Thanks. Merged.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/logic/dismantlesite.cc'
2--- src/logic/dismantlesite.cc 2013-07-26 20:19:36 +0000
3+++ src/logic/dismantlesite.cc 2013-08-01 08:36:29 +0000
4@@ -210,7 +210,13 @@
5 schedule_destroy(game);
6
7 worker.pop_task(game);
8- worker.start_task_leavebuilding(game, true);
9+ // No more building, so move to the flag
10+ worker.start_task_move
11+ (game,
12+ WALK_SE,
13+ worker.descr().get_right_walk_anims(false),
14+ true);
15+ worker.set_location(nullptr);
16 } else if (not m_working) {
17 m_work_steptime = game.get_gametime() + DISMANTLESITE_STEP_TIME;
18 worker.start_task_idle
19
20=== modified file 'src/logic/soldier.cc'
21--- src/logic/soldier.cc 2013-07-27 10:37:24 +0000
22+++ src/logic/soldier.cc 2013-08-01 08:36:29 +0000
23@@ -22,6 +22,7 @@
24 #include <cstdio>
25 #include <list>
26
27+#include "economy/economy.h"
28 #include "economy/flag.h"
29 #include "gamecontroller.h"
30 #include "graphic/graphic.h"
31@@ -34,6 +35,7 @@
32 #include "logic/editor_game_base.h"
33 #include "logic/findbob.h"
34 #include "logic/findimmovable.h"
35+#include "logic/findnode.h"
36 #include "logic/game.h"
37 #include "logic/game_data_error.h"
38 #include "logic/message_queue.h"
39@@ -759,6 +761,15 @@
40 return Worker::init_auto_task(game);
41 }
42
43+struct FindNodeOwned {
44+ FindNodeOwned(Player_Number owner) : m_owner(owner)
45+ {};
46+ bool accept(const Map & map, const FCoords & coords) const {
47+ return (coords.field->get_owned_by() == m_owner);
48+ }
49+private:
50+ Player_Number m_owner;
51+};
52
53 /**
54 * Leave our home building and single-mindedly try to attack
55@@ -784,6 +795,7 @@
56 State & state = top_state();
57 state.objvar1 = &building;
58 state.coords = building.get_position();
59+ state.ivar2 = 0; // Thre return state 1=go home 2=go back in known land
60
61 if (retreat) {
62 assert(retreat < 101);
63@@ -818,6 +830,12 @@
64 molog("[attack] unexpected fail\n");
65 return pop_task(game);
66 }
67+ } else if (signal == "location") {
68+ molog("[attack] Location destroyed\n");
69+ signal_handled();
70+ if (state.ivar2 == 0) {
71+ state.ivar2 = 1;
72+ }
73 } else {
74 molog
75 ("[attack] cancelled by unexpected signal '%s'\n", signal.c_str());
76@@ -834,14 +852,101 @@
77 upcast(Building, location, get_location(game));
78 upcast(Building, enemy, state.objvar1.get(game));
79
80- if (location && get_position() == location->get_position()) {
81- if (!enemy) {
82- molog("[attack] returned home\n");
83- return pop_task(game);
84- }
85- return start_task_leavebuilding(game, false);
86+ // Handle returns
87+ if (state.ivar2 > 0) {
88+ if (state.ivar2 == 1) {
89+ // Return home
90+ if (!location || !is_a(MilitarySite, location)) {
91+ molog("[attack] No more site to go back to\n");
92+ state.ivar2 = 2;
93+ return schedule_act(game, 10);
94+ }
95+ Flag & baseflag = location->base_flag();
96+ if (get_position() == baseflag.get_position()) {
97+ // At flag, enter building
98+ return
99+ start_task_move
100+ (game,
101+ WALK_NW,
102+ descr().get_right_walk_anims(does_carry_ware()),
103+ true);
104+ }
105+ if (get_position() == location->get_position()) {
106+ // At building, check if attack is required
107+ if (!enemy) {
108+ molog("[attack] returned home\n");
109+ return pop_task(game);
110+ }
111+ state.ivar2 = 0;
112+ return start_task_leavebuilding(game, false);
113+ }
114+ // Head to home
115+ if
116+ (start_task_movepath
117+ (game,
118+ baseflag.get_position(),
119+ 4, // use larger persist when returning home
120+ descr().get_right_walk_anims(does_carry_ware())))
121+ return;
122+ else {
123+ molog("[attack] failed to return home\n");
124+ return pop_task(game);
125+ }
126+ }
127+ if (state.ivar2 == 2) {
128+ // No more home, so return to homeland
129+ upcast(Flag, flag, game.map().get_immovable(get_position()));
130+ if (flag && flag->get_owner() == get_owner()) {
131+ // At a flag
132+ molog("[attack] Returned to own flag\n");
133+ return pop_task(game);
134+ }
135+ Coords target;
136+ if (get_location(game)) {
137+ // We still have a location, head for the flag
138+ target = get_location(game)->base_flag().get_position();
139+ molog("[attack] Going back to our flag\n");
140+ } else {
141+ // No location
142+ if (get_position().field->get_owned_by() == get_owner()->player_number()) {
143+ // We are in our land, become fugitive
144+ molog("[attack] Back to our land\n");
145+ return pop_task(game);
146+ }
147+ // Try to find our land
148+ Map* map = game.get_map();
149+ std::vector<Coords> coords;
150+ uint32_t maxdist = vision_range() * 2;
151+ Area<FCoords> area(map->get_fcoords(get_position()), maxdist);
152+ if
153+ (map->find_reachable_fields
154+ (area, &coords, CheckStepDefault(descr().movecaps()),
155+ FindNodeOwned(get_owner()->player_number())))
156+ {
157+ // Found home land
158+ target = coords.front();
159+ molog("[attack] Going back to our land\n");
160+ } else {
161+ // Become fugitive
162+ molog("[attack] No land in sight\n");
163+ return pop_task(game);
164+ }
165+ }
166+ if
167+ (start_task_movepath
168+ (game,
169+ target,
170+ 4, // use larger persist when returning home
171+ descr().get_right_walk_anims(does_carry_ware())))
172+ return;
173+ else {
174+ molog("[attack] failed to return to own land\n");
175+ return pop_task(game);
176+ }
177+ }
178 }
179
180+
181 if (m_battle)
182 return start_task_battle(game);
183
184@@ -853,11 +958,6 @@
185 return start_task_idle(game, get_animation("idle"), 5000);
186 }
187
188- if (!location) {
189- molog("[attack] our location disappeared during a battle\n");
190- return pop_task(game);
191- }
192-
193 // Count remaining defenders
194 if (enemy) {
195 if (upcast(MilitarySite, ms, enemy)) {
196@@ -913,26 +1013,9 @@
197 }
198 }
199 }
200- Flag & baseflag = location->base_flag();
201- if (get_position() == baseflag.get_position())
202- return
203- start_task_move
204- (game,
205- WALK_NW,
206- descr().get_right_walk_anims(does_carry_ware()),
207- true);
208-
209- if
210- (start_task_movepath
211- (game,
212- baseflag.get_position(),
213- 4, // use larger persist when returning home
214- descr().get_right_walk_anims(does_carry_ware())))
215- return;
216- else {
217- molog("[attack] failed to return home\n");
218- return pop_task(game);
219- }
220+ // Return home
221+ state.ivar2 = 1;
222+ return schedule_act(game, 10);
223 }
224
225 // At this point, we know that the enemy building still stands,
226@@ -951,6 +1034,7 @@
227 "and return home!\n");
228 state.coords = Coords::Null();
229 state.objvar1 = 0;
230+ state.ivar2 = 1;
231 return schedule_act(game, 10);
232 }
233 }
234
235=== modified file 'src/logic/worker.cc'
236--- src/logic/worker.cc 2013-08-01 03:08:19 +0000
237+++ src/logic/worker.cc 2013-08-01 08:36:29 +0000
238@@ -31,6 +31,7 @@
239 #include "logic/checkstep.h"
240 #include "logic/cmd_incorporate.h"
241 #include "logic/critter_bob.h"
242+#include "logic/dismantlesite.h"
243 #include "logic/findbob.h"
244 #include "logic/findimmovable.h"
245 #include "logic/findnode.h"
246@@ -1826,19 +1827,28 @@
247 if (upcast(Flag, flag, pos)) {
248 // Is this "our" flag?
249 if (flag->get_building() == location) {
250- if (state.ivar1 && flag->has_capacity())
251+ if (state.ivar1 && flag->has_capacity()) {
252 if (WareInstance * const item = fetch_carried_item(game)) {
253 flag->add_item(game, *item);
254 set_animation(game, descr().get_animation("idle"));
255 return schedule_act(game, 20); // rest a while
256 }
257+ }
258
259- return
260- start_task_move
261- (game,
262- WALK_NW,
263- descr().get_right_walk_anims(does_carry_ware()),
264- true);
265+ // Don't try to enter building if it is a dismantle site
266+ // It is no problem for builders since they won't return before
267+ // dismantling is complete.
268+ if (is_a(DismantleSite, location)) {
269+ set_location(0);
270+ return pop_task(game);
271+ } else {
272+ return
273+ start_task_move
274+ (game,
275+ WALK_NW,
276+ descr().get_right_walk_anims(does_carry_ware()),
277+ true);
278+ }
279 }
280 }
281 }
282@@ -2404,8 +2414,9 @@
283 return pop_task(game);
284
285 upcast(Building, building, get_location(game));
286- if (!building)
287+ if (!building) {
288 return pop_task(game);
289+ }
290
291 Flag & baseflag = building->base_flag();
292

Subscribers

People subscribed via source and target branches

to status/vote changes: