Merge lp:~widelands-dev/widelands/bug-1380287 into lp:widelands

Proposed by TiborB
Status: Merged
Merged at revision: 7441
Proposed branch: lp:~widelands-dev/widelands/bug-1380287
Merge into: lp:widelands
Diff against target: 1463 lines (+610/-173)
23 files modified
src/ai/ai_help_structs.h (+1/-1)
src/ai/defaultai.cc (+4/-4)
src/economy/portdock.cc (+1/-2)
src/logic/game.cc (+2/-2)
src/logic/game.h (+3/-2)
src/logic/playercommand.cc (+10/-10)
src/logic/playercommand.h (+5/-5)
src/logic/ship.cc (+41/-26)
src/logic/ship.h (+16/-6)
src/logic/walkingdir.cc (+42/-42)
src/logic/walkingdir.h (+3/-3)
src/scripting/lua_game.cc (+37/-0)
src/scripting/lua_game.h (+1/-0)
src/scripting/lua_map.cc (+273/-9)
src/scripting/lua_map.h (+10/-1)
src/wui/shipwindow.cc (+8/-8)
test/maps/expedition.wmf/scripting/init.lua (+15/-30)
test/maps/expedition.wmf/scripting/test_ship_movement_controls.lua (+115/-0)
test/maps/expedition.wmf/scripting/test_starting_and_immediately_canceling.lua (+6/-2)
test/maps/expedition.wmf/scripting/test_starting_wait_a_while_cancel.lua (+2/-2)
test/maps/ship_transportation.wmf/scripting/init.lua (+0/-17)
test/maps/ship_transportation.wmf/scripting/test_rip_portdock_with_worker_and_ware_in_transit.lua (+3/-1)
test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_worker_in_portdock.lua (+12/-0)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1380287
Reviewer Review Type Date Requested Status
SirVer Needs Fixing
GunChleoc Approve
Review via email: mp+252507@code.launchpad.net

Description of the change

I implemented some LUA interface for ships, f.e.:

map:get_ships - to get list of player' ships on map
ships.field - to get a field where ship is located
ships.status - to get status of ship (what is it doing)
port:start_expedition(), port:cancel_expedition(), port.expedition_in_progress - selfexplanatory...
ship.scout_direction, ship.island_scout_direction - sets and get direction of ship if in expedition mode
ship:build_colonization_port()

Please review

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

I just had a very quick look and it seems to be OK overall - I will have to look at the code properly. One thing though: it might be better to have strings rather than numbers in Lua for the scouting direction. Operating on the pure numbers will make the code hard to read, and also break the code if the C++ enum should change.

Revision history for this message
TiborB (tiborb95) wrote :

@GunChleoc - of course I was thinking about this - using text instead of integers for directions. But I failed to find good conventor string<->integer like nw=6 and so. I mean a pair of functions that would change them forth and back.
I could implement the conversion in this code, but it would broke if somebody changed the enum somewhere... But I could do this, allright... I will consider it....

Revision history for this message
GunChleoc (gunchleoc) wrote :

Do something like this (I don't remember the exact enum variable names):

if (direction == Ship::Direction::kCounterClockwise) {
    lua_pushstring(L, "ccw")
} elseif ...

then everything will always be referenced by a name.

I have done something similar in lua_map.cc for the BuildingDescription types, in case you want to steal some code.

Revision history for this message
TiborB (tiborb95) wrote :

This is the easier conversion, but I need also backward conversion... But it seems there is also example for this.... I will look at it once more...

Revision history for this message
GunChleoc (gunchleoc) wrote :

I forgot that in this direction, it is better to use a switch statement. Something I learned from SirVer - the code will run faster, because it just jumps using the variable as an address instead of making a full comparison.

Revision history for this message
TiborB (tiborb95) wrote :

reworked to use the strings

Revision history for this message
GunChleoc (gunchleoc) wrote :

I refactored the enums, made the status function return strings, and added some tests. Please review my code before merging.

review: Approve
Revision history for this message
SirVer (sirver) wrote :

I found a couple more nits. Otherwise LGTM, please merge after fixing them.

review: Needs Fixing
Revision history for this message
TiborB (tiborb95) wrote :

comments fixed, I rerun the regression test (all OK) and merged it to trunk

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/ai/ai_help_structs.h'
--- src/ai/ai_help_structs.h 2015-03-26 06:59:37 +0000
+++ src/ai/ai_help_structs.h 2015-04-07 20:58:43 +0000
@@ -438,7 +438,7 @@
438438
439 // a ship circumvents all islands in the same direction, the value439 // a ship circumvents all islands in the same direction, the value
440 // is assigned only once440 // is assigned only once
441 Widelands::ScoutingDirection island_circ_direction = Widelands::ScoutingDirection::kClockwise;441 Widelands::IslandExploreDirection island_circ_direction = Widelands::IslandExploreDirection::kClockwise;
442 bool waiting_for_command_ = false;442 bool waiting_for_command_ = false;
443 int32_t last_command_time = 0;443 int32_t last_command_time = 0;
444};444};
445445
=== modified file 'src/ai/defaultai.cc'
--- src/ai/defaultai.cc 2015-03-26 20:35:19 +0000
+++ src/ai/defaultai.cc 2015-04-07 20:58:43 +0000
@@ -3795,9 +3795,9 @@
3795 allships.push_back(ShipObserver());3795 allships.push_back(ShipObserver());
3796 allships.back().ship = &ship;3796 allships.back().ship = &ship;
3797 if (game().get_gametime() % 2 == 0) {3797 if (game().get_gametime() % 2 == 0) {
3798 allships.back().island_circ_direction = ScoutingDirection::kClockwise;3798 allships.back().island_circ_direction = IslandExploreDirection::kClockwise;
3799 } else {3799 } else {
3800 allships.back().island_circ_direction = ScoutingDirection::kCounterClockwise;3800 allships.back().island_circ_direction = IslandExploreDirection::kCounterClockwise;
3801 }3801 }
3802}3802}
38033803
@@ -4058,10 +4058,10 @@
4058 so.waiting_for_command_ = false;4058 so.waiting_for_command_ = false;
4059 ;4059 ;
4060 } else {4060 } else {
4061 // 2.B Yes, pick one of avaiable directions4061 // 2.B Yes, pick one of avaliable directions
4062 const Direction final_direction =4062 const Direction final_direction =
4063 possible_directions.at(gametime % possible_directions.size());4063 possible_directions.at(gametime % possible_directions.size());
4064 game().send_player_ship_scout_direction(*so.ship, final_direction);4064 game().send_player_ship_scouting_direction(*so.ship, static_cast<WalkingDir>(final_direction));
4065 so.last_command_time = gametime;4065 so.last_command_time = gametime;
4066 so.waiting_for_command_ = false;4066 so.waiting_for_command_ = false;
4067 }4067 }
40684068
=== modified file 'src/economy/portdock.cc'
--- src/economy/portdock.cc 2015-02-16 20:23:15 +0000
+++ src/economy/portdock.cc 2015-04-07 20:58:43 +0000
@@ -213,12 +213,11 @@
213213
214 PlayerImmovable::cleanup(egbase);214 PlayerImmovable::cleanup(egbase);
215215
216 //now let attempt to recreate the portdock216 // Now let's attempt to recreate the portdock.
217 if (wh) {217 if (wh) {
218 if (!wh->m_cleanup_in_progress){218 if (!wh->m_cleanup_in_progress){
219 if (upcast(Game, game, &egbase)) {219 if (upcast(Game, game, &egbase)) {
220 if (game->is_loaded()) { //do not attempt when shutting down220 if (game->is_loaded()) { //do not attempt when shutting down
221 Player& player = owner();
222 wh->restore_portdock_or_destroy(egbase);221 wh->restore_portdock_or_destroy(egbase);
223 }222 }
224 }223 }
225224
=== modified file 'src/logic/game.cc'
--- src/logic/game.cc 2015-02-08 19:08:36 +0000
+++ src/logic/game.cc 2015-04-07 20:58:43 +0000
@@ -874,7 +874,7 @@
874}874}
875875
876876
877void Game::send_player_ship_scout_direction(Ship & ship, uint8_t direction)877void Game::send_player_ship_scouting_direction(Ship & ship, WalkingDir direction)
878{878{
879 send_player_command879 send_player_command
880 (*new CmdShipScoutDirection880 (*new CmdShipScoutDirection
@@ -888,7 +888,7 @@
888 (get_gametime(), ship.get_owner()->player_number(), ship.serial(), coords));888 (get_gametime(), ship.get_owner()->player_number(), ship.serial(), coords));
889}889}
890890
891void Game::send_player_ship_explore_island(Ship & ship, ScoutingDirection direction)891void Game::send_player_ship_explore_island(Ship & ship, IslandExploreDirection direction)
892{892{
893 send_player_command893 send_player_command
894 (*new CmdShipExploreIsland894 (*new CmdShipExploreIsland
895895
=== modified file 'src/logic/game.h'
--- src/logic/game.h 2015-02-08 19:08:36 +0000
+++ src/logic/game.h 2015-04-07 20:58:43 +0000
@@ -41,6 +41,7 @@
41struct Flag;41struct Flag;
42struct Path;42struct Path;
43struct PlayerImmovable;43struct PlayerImmovable;
44enum class IslandExploreDirection;
44enum class ScoutingDirection;45enum class ScoutingDirection;
45struct Ship;46struct Ship;
46struct PlayerEndStatus;47struct PlayerEndStatus;
@@ -181,9 +182,9 @@
181 void send_player_enemyflagaction182 void send_player_enemyflagaction
182 (const Flag &, PlayerNumber, uint32_t count);183 (const Flag &, PlayerNumber, uint32_t count);
183184
184 void send_player_ship_scout_direction(Ship &, uint8_t);185 void send_player_ship_scouting_direction(Ship &, WalkingDir);
185 void send_player_ship_construct_port(Ship &, Coords);186 void send_player_ship_construct_port(Ship &, Coords);
186 void send_player_ship_explore_island(Ship &, ScoutingDirection);187 void send_player_ship_explore_island(Ship &, IslandExploreDirection);
187 void send_player_sink_ship(Ship &);188 void send_player_sink_ship(Ship &);
188 void send_player_cancel_expedition_ship(Ship &);189 void send_player_cancel_expedition_ship(Ship &);
189190
190191
=== modified file 'src/logic/playercommand.cc'
--- src/logic/playercommand.cc 2015-02-19 19:37:09 +0000
+++ src/logic/playercommand.cc 2015-04-07 20:58:43 +0000
@@ -777,7 +777,7 @@
777 PlayerCommand (0, des.unsigned_8())777 PlayerCommand (0, des.unsigned_8())
778{778{
779 serial = des.unsigned_32();779 serial = des.unsigned_32();
780 dir = des.unsigned_8();780 dir = static_cast<WalkingDir>(des.unsigned_8());
781}781}
782782
783void CmdShipScoutDirection::execute (Game & game)783void CmdShipScoutDirection::execute (Game & game)
@@ -796,7 +796,7 @@
796 (ship->state_is_expedition())?"Y":"N");796 (ship->state_is_expedition())?"Y":"N");
797 return;797 return;
798 }798 }
799 ship->exp_scout_direction(game, dir);799 ship->exp_scouting_direction(game, dir);
800 }800 }
801}801}
802802
@@ -805,7 +805,7 @@
805 ser.unsigned_8 (PLCMD_SHIP_SCOUT);805 ser.unsigned_8 (PLCMD_SHIP_SCOUT);
806 ser.unsigned_8 (sender());806 ser.unsigned_8 (sender());
807 ser.unsigned_32(serial);807 ser.unsigned_32(serial);
808 ser.unsigned_8 (dir);808 ser.unsigned_8 (static_cast<uint8_t>(dir));
809}809}
810810
811#define PLAYER_CMD_SHIP_SCOUT_DIRECTION_VERSION 1811#define PLAYER_CMD_SHIP_SCOUT_DIRECTION_VERSION 1
@@ -818,7 +818,7 @@
818 PlayerCommand::read(fr, egbase, mol);818 PlayerCommand::read(fr, egbase, mol);
819 serial = get_object_serial_or_zero<Ship>(fr.unsigned_32(), mol);819 serial = get_object_serial_or_zero<Ship>(fr.unsigned_32(), mol);
820 // direction820 // direction
821 dir = fr.unsigned_8();821 dir = static_cast<WalkingDir>(fr.unsigned_8());
822 } else822 } else
823 throw GameDataError("unknown/unhandled version %u", packet_version);823 throw GameDataError("unknown/unhandled version %u", packet_version);
824 } catch (const WException & e) {824 } catch (const WException & e) {
@@ -837,7 +837,7 @@
837 fw.unsigned_32(mos.get_object_file_index_or_zero(egbase.objects().get_object(serial)));837 fw.unsigned_32(mos.get_object_file_index_or_zero(egbase.objects().get_object(serial)));
838838
839 // direction839 // direction
840 fw.unsigned_8(dir);840 fw.unsigned_8(static_cast<uint8_t>(dir));
841}841}
842842
843843
@@ -912,7 +912,7 @@
912 PlayerCommand (0, des.unsigned_8())912 PlayerCommand (0, des.unsigned_8())
913{913{
914 serial = des.unsigned_32();914 serial = des.unsigned_32();
915 scouting_direction = static_cast<ScoutingDirection>(des.unsigned_8());915 island_explore_direction = static_cast<IslandExploreDirection>(des.unsigned_8());
916}916}
917917
918void CmdShipExploreIsland::execute (Game & game)918void CmdShipExploreIsland::execute (Game & game)
@@ -931,7 +931,7 @@
931 (ship->state_is_expedition())?"Y":"N");931 (ship->state_is_expedition())?"Y":"N");
932 return;932 return;
933 }933 }
934 ship->exp_explore_island(game, scouting_direction);934 ship->exp_explore_island(game, island_explore_direction);
935 }935 }
936}936}
937937
@@ -940,7 +940,7 @@
940 ser.unsigned_8 (PLCMD_SHIP_EXPLORE);940 ser.unsigned_8 (PLCMD_SHIP_EXPLORE);
941 ser.unsigned_8 (sender());941 ser.unsigned_8 (sender());
942 ser.unsigned_32(serial);942 ser.unsigned_32(serial);
943 ser.unsigned_8 (static_cast<uint8_t>(scouting_direction));943 ser.unsigned_8 (static_cast<uint8_t>(island_explore_direction));
944}944}
945945
946#define PLAYER_CMD_SHIP_EXPLORE_ISLAND_VERSION 1946#define PLAYER_CMD_SHIP_EXPLORE_ISLAND_VERSION 1
@@ -952,7 +952,7 @@
952 if (packet_version == PLAYER_CMD_SHIP_EXPLORE_ISLAND_VERSION) {952 if (packet_version == PLAYER_CMD_SHIP_EXPLORE_ISLAND_VERSION) {
953 PlayerCommand::read(fr, egbase, mol);953 PlayerCommand::read(fr, egbase, mol);
954 serial = get_object_serial_or_zero<Ship>(fr.unsigned_32(), mol);954 serial = get_object_serial_or_zero<Ship>(fr.unsigned_32(), mol);
955 scouting_direction = static_cast<ScoutingDirection>(fr.unsigned_8());955 island_explore_direction = static_cast<IslandExploreDirection>(fr.unsigned_8());
956 } else956 } else
957 throw GameDataError("unknown/unhandled version %u", packet_version);957 throw GameDataError("unknown/unhandled version %u", packet_version);
958 } catch (const WException & e) {958 } catch (const WException & e) {
@@ -971,7 +971,7 @@
971 fw.unsigned_32(mos.get_object_file_index_or_zero(egbase.objects().get_object(serial)));971 fw.unsigned_32(mos.get_object_file_index_or_zero(egbase.objects().get_object(serial)));
972972
973 // Direction of exploration973 // Direction of exploration
974 fw.unsigned_8(static_cast<uint8_t>(scouting_direction));974 fw.unsigned_8(static_cast<uint8_t>(island_explore_direction));
975}975}
976976
977977
978978
=== modified file 'src/logic/playercommand.h'
--- src/logic/playercommand.h 2015-02-05 12:11:20 +0000
+++ src/logic/playercommand.h 2015-04-07 20:58:43 +0000
@@ -313,7 +313,7 @@
313struct CmdShipScoutDirection : public PlayerCommand {313struct CmdShipScoutDirection : public PlayerCommand {
314 CmdShipScoutDirection() : PlayerCommand(), serial(0) {} // For savegame loading314 CmdShipScoutDirection() : PlayerCommand(), serial(0) {} // For savegame loading
315 CmdShipScoutDirection315 CmdShipScoutDirection
316 (int32_t const t, PlayerNumber const p, Serial s, uint8_t direction)316 (int32_t const t, PlayerNumber const p, Serial s, WalkingDir direction)
317 : PlayerCommand(t, p), serial(s), dir(direction)317 : PlayerCommand(t, p), serial(s), dir(direction)
318 {}318 {}
319319
@@ -329,7 +329,7 @@
329329
330private:330private:
331 Serial serial;331 Serial serial;
332 uint8_t dir;332 WalkingDir dir;
333};333};
334334
335struct CmdShipConstructPort : public PlayerCommand {335struct CmdShipConstructPort : public PlayerCommand {
@@ -357,8 +357,8 @@
357struct CmdShipExploreIsland : public PlayerCommand {357struct CmdShipExploreIsland : public PlayerCommand {
358 CmdShipExploreIsland() : PlayerCommand(), serial(0) {} // For savegame loading358 CmdShipExploreIsland() : PlayerCommand(), serial(0) {} // For savegame loading
359 CmdShipExploreIsland359 CmdShipExploreIsland
360 (int32_t const t, PlayerNumber const p, Serial s, ScoutingDirection direction)360 (int32_t const t, PlayerNumber const p, Serial s, IslandExploreDirection direction)
361 : PlayerCommand(t, p), serial(s), scouting_direction(direction)361 : PlayerCommand(t, p), serial(s), island_explore_direction(direction)
362 {}362 {}
363363
364 void write(FileWrite &, EditorGameBase &, MapObjectSaver &) override;364 void write(FileWrite &, EditorGameBase &, MapObjectSaver &) override;
@@ -373,7 +373,7 @@
373373
374private:374private:
375 Serial serial;375 Serial serial;
376 ScoutingDirection scouting_direction;376 IslandExploreDirection island_explore_direction;
377};377};
378378
379struct CmdShipSink : public PlayerCommand {379struct CmdShipSink : public PlayerCommand {
380380
=== modified file 'src/logic/ship.cc'
--- src/logic/ship.cc 2015-02-05 12:11:20 +0000
+++ src/logic/ship.cc 2015-04-07 20:58:43 +0000
@@ -550,15 +550,15 @@
550 case EXP_SCOUTING: {550 case EXP_SCOUTING: {
551 if (m_expedition->island_exploration) { // Exploration of the island551 if (m_expedition->island_exploration) { // Exploration of the island
552 if (exp_close_to_coast()) {552 if (exp_close_to_coast()) {
553 if (m_expedition->direction == 0) {553 if (m_expedition->scouting_direction == WalkingDir::IDLE) {
554 // Make sure we know the location of the coast and use it as initial direction we554 // Make sure we know the location of the coast and use it as initial direction we
555 // come from555 // come from
556 m_expedition->direction = WALK_SE;556 m_expedition->scouting_direction = WALK_SE;
557 for (uint8_t secure = 0; exp_dir_swimable(m_expedition->direction); ++secure) {557 for (uint8_t secure = 0; exp_dir_swimable(m_expedition->scouting_direction); ++secure) {
558 assert(secure < 6);558 assert(secure < 6);
559 m_expedition->direction = get_cw_neighbour(m_expedition->direction);559 m_expedition->scouting_direction = get_cw_neighbour(m_expedition->scouting_direction);
560 }560 }
561 m_expedition->direction = get_backward_dir(m_expedition->direction);561 m_expedition->scouting_direction = get_backward_dir(m_expedition->scouting_direction);
562 // Save the position - this is where we start562 // Save the position - this is where we start
563 m_expedition->exploration_start = get_position();563 m_expedition->exploration_start = get_position();
564 } else {564 } else {
@@ -579,18 +579,18 @@
579 // The ship is supposed to follow the coast as close as possible, therefore the check579 // The ship is supposed to follow the coast as close as possible, therefore the check
580 // for580 // for
581 // a swimable field begins at the neighbour field of the direction we came from.581 // a swimable field begins at the neighbour field of the direction we came from.
582 m_expedition->direction = get_backward_dir(m_expedition->direction);582 m_expedition->scouting_direction = get_backward_dir(m_expedition->scouting_direction);
583 if (m_expedition->scouting_direction == ScoutingDirection::kClockwise) {583 if (m_expedition->island_explore_direction == IslandExploreDirection::kClockwise) {
584 do {584 do {
585 m_expedition->direction = get_ccw_neighbour(m_expedition->direction);585 m_expedition->scouting_direction = get_ccw_neighbour(m_expedition->scouting_direction);
586 } while (!exp_dir_swimable(m_expedition->direction));586 } while (!exp_dir_swimable(m_expedition->scouting_direction));
587 } else {587 } else {
588 do {588 do {
589 m_expedition->direction = get_cw_neighbour(m_expedition->direction);589 m_expedition->scouting_direction = get_cw_neighbour(m_expedition->scouting_direction);
590 } while (!exp_dir_swimable(m_expedition->direction));590 } while (!exp_dir_swimable(m_expedition->scouting_direction));
591 }591 }
592 state.ivar1 = 1;592 state.ivar1 = 1;
593 return start_task_move(game, m_expedition->direction, descr().get_sail_anims(), false);593 return start_task_move(game, m_expedition->scouting_direction, descr().get_sail_anims(), false);
594 } else {594 } else {
595 // The ship got the command to scout around an island, but is not close to any island595 // The ship got the command to scout around an island, but is not close to any island
596 // Most likely the command was send as the ship was on an exploration and just leaving596 // Most likely the command was send as the ship was on an exploration and just leaving
@@ -615,10 +615,10 @@
615 return start_task_idle(game, descr().main_animation(), 1500);615 return start_task_idle(game, descr().main_animation(), 1500);
616 }616 }
617 } else { // scouting towards a specific direction617 } else { // scouting towards a specific direction
618 if (exp_dir_swimable(m_expedition->direction)) {618 if (exp_dir_swimable(m_expedition->scouting_direction)) {
619 // the scouting direction is still free to move619 // the scouting direction is still free to move
620 state.ivar1 = 1;620 state.ivar1 = 1;
621 start_task_move(game, m_expedition->direction, descr().get_sail_anims(), false);621 start_task_move(game, m_expedition->scouting_direction, descr().get_sail_anims(), false);
622 return;622 return;
623 }623 }
624 // coast reached624 // coast reached
@@ -791,9 +791,9 @@
791 m_expedition.reset(new Expedition());791 m_expedition.reset(new Expedition());
792 m_expedition->seen_port_buildspaces.reset(new std::list<Coords>());792 m_expedition->seen_port_buildspaces.reset(new std::list<Coords>());
793 m_expedition->island_exploration = false;793 m_expedition->island_exploration = false;
794 m_expedition->direction = 0;794 m_expedition->scouting_direction = WalkingDir::IDLE;
795 m_expedition->exploration_start = Coords(0, 0);795 m_expedition->exploration_start = Coords(0, 0);
796 m_expedition->scouting_direction = ScoutingDirection::kClockwise;796 m_expedition->island_explore_direction = IslandExploreDirection::kClockwise;
797 m_expedition->economy.reset(new Economy(*get_owner()));797 m_expedition->economy.reset(new Economy(*get_owner()));
798798
799 // We are no longer in any other economy, but instead are an economy of our799 // We are no longer in any other economy, but instead are an economy of our
@@ -824,13 +824,20 @@
824824
825/// Initializes / changes the direction of scouting to @arg direction825/// Initializes / changes the direction of scouting to @arg direction
826/// @note only called via player command826/// @note only called via player command
827void Ship::exp_scout_direction(Game&, uint8_t direction) {827void Ship::exp_scouting_direction(Game&, WalkingDir scouting_direction) {
828 assert(m_expedition);828 assert(m_expedition);
829 m_ship_state = EXP_SCOUTING;829 m_ship_state = EXP_SCOUTING;
830 m_expedition->direction = direction;830 m_expedition->scouting_direction = scouting_direction;
831 m_expedition->island_exploration = false;831 m_expedition->island_exploration = false;
832}832}
833833
834WalkingDir Ship::get_scouting_direction() {
835 if (m_expedition && m_ship_state == EXP_SCOUTING && !m_expedition->island_exploration) {
836 return m_expedition->scouting_direction;
837 }
838 return WalkingDir::IDLE;
839}
840
834/// Initializes the construction of a port at @arg c841/// Initializes the construction of a port at @arg c
835/// @note only called via player command842/// @note only called via player command
836void Ship::exp_construct_port(Game&, const Coords& c) {843void Ship::exp_construct_port(Game&, const Coords& c) {
@@ -840,16 +847,24 @@
840 m_ship_state = EXP_COLONIZING;847 m_ship_state = EXP_COLONIZING;
841}848}
842849
843/// Initializes / changes the direction the island exploration in @arg scouting_direction direction850/// Initializes / changes the direction the island exploration in @arg island_explore_direction direction
844/// @note only called via player command851/// @note only called via player command
845void Ship::exp_explore_island(Game&, ScoutingDirection scouting_direction) {852void Ship::exp_explore_island(Game&, IslandExploreDirection island_explore_direction) {
846 assert(m_expedition);853 assert(m_expedition);
847 m_ship_state = EXP_SCOUTING;854 m_ship_state = EXP_SCOUTING;
848 m_expedition->scouting_direction = scouting_direction;855 m_expedition->island_explore_direction = island_explore_direction;
849 m_expedition->direction = 0;856 m_expedition->scouting_direction = WalkingDir::IDLE;
850 m_expedition->island_exploration = true;857 m_expedition->island_exploration = true;
851}858}
852859
860IslandExploreDirection Ship::get_island_explore_direction() {
861 if (m_expedition && m_ship_state == EXP_SCOUTING && m_expedition->island_exploration) {
862 return m_expedition->island_explore_direction;
863 }
864 return IslandExploreDirection::kNotSet;
865}
866
867
853/// Cancels a currently running expedition868/// Cancels a currently running expedition
854/// @note only called via player command869/// @note only called via player command
855void Ship::exp_cancel(Game& game) {870void Ship::exp_cancel(Game& game) {
@@ -995,11 +1010,11 @@
995 // whether scouting or exploring1010 // whether scouting or exploring
996 m_expedition->island_exploration = fr.unsigned_8() == 1;1011 m_expedition->island_exploration = fr.unsigned_8() == 1;
997 // current direction1012 // current direction
998 m_expedition->direction = fr.unsigned_8();1013 m_expedition->scouting_direction = static_cast<WalkingDir>(fr.unsigned_8());
999 // Start coordinates of an island exploration1014 // Start coordinates of an island exploration
1000 m_expedition->exploration_start = read_coords_32(&fr);1015 m_expedition->exploration_start = read_coords_32(&fr);
1001 // Whether the exploration is done clockwise or counter clockwise1016 // Whether the exploration is done clockwise or counter clockwise
1002 m_expedition->scouting_direction = static_cast<ScoutingDirection>(fr.unsigned_8());1017 m_expedition->island_explore_direction = static_cast<IslandExploreDirection>(fr.unsigned_8());
1003 }1018 }
1004 } else1019 } else
1005 m_ship_state = TRANSPORT;1020 m_ship_state = TRANSPORT;
@@ -1114,11 +1129,11 @@
1114 // whether scouting or exploring1129 // whether scouting or exploring
1115 fw.unsigned_8(m_expedition->island_exploration ? 1 : 0);1130 fw.unsigned_8(m_expedition->island_exploration ? 1 : 0);
1116 // current direction1131 // current direction
1117 fw.unsigned_8(m_expedition->direction);1132 fw.unsigned_8(static_cast<uint8_t>(m_expedition->scouting_direction));
1118 // Start coordinates of an island exploration1133 // Start coordinates of an island exploration
1119 write_coords_32(&fw, m_expedition->exploration_start);1134 write_coords_32(&fw, m_expedition->exploration_start);
1120 // Whether the exploration is done clockwise or counter clockwise1135 // Whether the exploration is done clockwise or counter clockwise
1121 fw.unsigned_8(static_cast<uint8_t>(m_expedition->scouting_direction));1136 fw.unsigned_8(static_cast<uint8_t>(m_expedition->island_explore_direction));
1122 }1137 }
11231138
1124 fw.unsigned_32(mos.get_object_file_index_or_zero(m_lastdock.get(egbase)));1139 fw.unsigned_32(mos.get_object_file_index_or_zero(m_lastdock.get(egbase)));
11251140
=== modified file 'src/logic/ship.h'
--- src/logic/ship.h 2015-02-05 12:11:20 +0000
+++ src/logic/ship.h 2015-04-07 20:58:43 +0000
@@ -38,9 +38,10 @@
38class PortDock;38class PortDock;
3939
40// This can't be part of the Ship class because of forward declaration in game.h40// This can't be part of the Ship class because of forward declaration in game.h
41enum class ScoutingDirection {41enum class IslandExploreDirection {
42 kCounterClockwise = 0, // This comes first for savegame compatibility (used to be = 0)42 kCounterClockwise = 0, // This comes first for savegame compatibility (used to be = 0)
43 kClockwise = 143 kClockwise = 1,
44 kNotSet
44};45};
4546
46struct NoteShipMessage {47struct NoteShipMessage {
@@ -202,9 +203,18 @@
202 return m_expedition->seen_port_buildspaces.get();203 return m_expedition->seen_port_buildspaces.get();
203 }204 }
204205
205 void exp_scout_direction(Game &, uint8_t);206 void exp_scouting_direction(Game &, WalkingDir);
206 void exp_construct_port (Game &, const Coords&);207 void exp_construct_port (Game &, const Coords&);
207 void exp_explore_island (Game &, ScoutingDirection);208 void exp_explore_island (Game &, IslandExploreDirection);
209
210 //Returns integer of direction, or WalkingDir::IDLE if query invalid
211 //Intended for LUA scripting
212 WalkingDir get_scouting_direction();
213
214 //Returns integer of direction, or IslandExploreDirection::kNotSet
215 //if query invalid
216 //Intended for LUA scripting
217 IslandExploreDirection get_island_explore_direction();
208218
209 void exp_cancel (Game &);219 void exp_cancel (Game &);
210 void sink_ship (Game &);220 void sink_ship (Game &);
@@ -242,9 +252,9 @@
242 std::unique_ptr<std::list<Coords> > seen_port_buildspaces;252 std::unique_ptr<std::list<Coords> > seen_port_buildspaces;
243 bool swimable[LAST_DIRECTION];253 bool swimable[LAST_DIRECTION];
244 bool island_exploration;254 bool island_exploration;
245 uint8_t direction;255 WalkingDir scouting_direction;
246 Coords exploration_start;256 Coords exploration_start;
247 ScoutingDirection scouting_direction;257 IslandExploreDirection island_explore_direction;
248 std::unique_ptr<Economy> economy;258 std::unique_ptr<Economy> economy;
249 };259 };
250 std::unique_ptr<Expedition> m_expedition;260 std::unique_ptr<Expedition> m_expedition;
251261
=== modified file 'src/logic/walkingdir.cc'
--- src/logic/walkingdir.cc 2014-07-14 10:45:44 +0000
+++ src/logic/walkingdir.cc 2015-04-07 20:58:43 +0000
@@ -22,61 +22,61 @@
22namespace Widelands {22namespace Widelands {
2323
24/// \returns the neighbour direction in clockwise24/// \returns the neighbour direction in clockwise
25uint8_t get_cw_neighbour(uint8_t dir) {25WalkingDir get_cw_neighbour(WalkingDir dir) {
26 switch (dir) {26 switch (dir) {
27 case WALK_NE:27 case WalkingDir::WALK_NE:
28 return WALK_E;28 return WalkingDir::WALK_E;
29 case WALK_E:29 case WalkingDir::WALK_E:
30 return WALK_SE;30 return WalkingDir::WALK_SE;
31 case WALK_SE:31 case WalkingDir::WALK_SE:
32 return WALK_SW;32 return WalkingDir::WALK_SW;
33 case WALK_SW:33 case WalkingDir::WALK_SW:
34 return WALK_W;34 return WalkingDir::WALK_W;
35 case WALK_W:35 case WalkingDir::WALK_W:
36 return WALK_NW;36 return WalkingDir::WALK_NW;
37 case WALK_NW:37 case WalkingDir::WALK_NW:
38 return WALK_NE;38 return WalkingDir::WALK_NE;
39 default:39 default:
40 return 0;40 return WalkingDir::IDLE;
41 }41 }
42}42}
4343
44/// \returns the neighbour direction in counterclockwise44/// \returns the neighbour direction in counterclockwise
45uint8_t get_ccw_neighbour(uint8_t dir) {45WalkingDir get_ccw_neighbour(WalkingDir dir) {
46 switch (dir) {46 switch (dir) {
47 case WALK_E:47 case WalkingDir::WALK_E:
48 return WALK_NE;48 return WalkingDir::WALK_NE;
49 case WALK_NE:49 case WalkingDir::WALK_NE:
50 return WALK_NW;50 return WalkingDir::WALK_NW;
51 case WALK_NW:51 case WalkingDir::WALK_NW:
52 return WALK_W;52 return WalkingDir::WALK_W;
53 case WALK_W:53 case WalkingDir::WALK_W:
54 return WALK_SW;54 return WalkingDir::WALK_SW;
55 case WALK_SW:55 case WalkingDir::WALK_SW:
56 return WALK_SE;56 return WalkingDir::WALK_SE;
57 case WALK_SE:57 case WalkingDir::WALK_SE:
58 return WALK_E;58 return WalkingDir::WALK_E;
59 default:59 default:
60 return 0;60 return WalkingDir::IDLE;
61 }61 }
62}62}
6363
64uint8_t get_backward_dir(uint8_t dir) {64WalkingDir get_backward_dir(WalkingDir dir) {
65 switch (dir) {65 switch (dir) {
66 case WALK_E:66 case WalkingDir::WALK_E:
67 return WALK_W;67 return WalkingDir::WALK_W;
68 case WALK_NE:68 case WalkingDir::WALK_NE:
69 return WALK_SW;69 return WalkingDir::WALK_SW;
70 case WALK_NW:70 case WalkingDir::WALK_NW:
71 return WALK_SE;71 return WalkingDir::WALK_SE;
72 case WALK_W:72 case WalkingDir::WALK_W:
73 return WALK_E;73 return WalkingDir::WALK_E;
74 case WALK_SW:74 case WalkingDir::WALK_SW:
75 return WALK_NE;75 return WalkingDir::WALK_NE;
76 case WALK_SE:76 case WalkingDir::WALK_SE:
77 return WALK_NW;77 return WalkingDir::WALK_NW;
78 default:78 default:
79 return 0;79 return WalkingDir::IDLE;
80 }80 }
81}81}
8282
8383
=== modified file 'src/logic/walkingdir.h'
--- src/logic/walkingdir.h 2014-07-05 16:41:51 +0000
+++ src/logic/walkingdir.h 2015-04-07 20:58:43 +0000
@@ -37,9 +37,9 @@
37 LAST_DIRECTION = 6,37 LAST_DIRECTION = 6,
38};38};
3939
40uint8_t get_cw_neighbour(uint8_t dir);40WalkingDir get_cw_neighbour(WalkingDir dir);
41uint8_t get_ccw_neighbour(uint8_t dir);41WalkingDir get_ccw_neighbour(WalkingDir dir);
42uint8_t get_backward_dir(uint8_t dir);42WalkingDir get_backward_dir(WalkingDir dir);
4343
44}44}
4545
4646
=== modified file 'src/scripting/lua_game.cc'
--- src/scripting/lua_game.cc 2015-01-31 16:03:59 +0000
+++ src/scripting/lua_game.cc 2015-04-07 20:58:43 +0000
@@ -97,6 +97,7 @@
97 METHOD(LuaPlayer, hide_fields),97 METHOD(LuaPlayer, hide_fields),
98 METHOD(LuaPlayer, reveal_scenario),98 METHOD(LuaPlayer, reveal_scenario),
99 METHOD(LuaPlayer, reveal_campaign),99 METHOD(LuaPlayer, reveal_campaign),
100 METHOD(LuaPlayer, get_ships),
100 METHOD(LuaPlayer, get_buildings),101 METHOD(LuaPlayer, get_buildings),
101 METHOD(LuaPlayer, get_suitability),102 METHOD(LuaPlayer, get_suitability),
102 METHOD(LuaPlayer, allow_workers),103 METHOD(LuaPlayer, allow_workers),
@@ -628,6 +629,7 @@
628int LuaPlayer::reveal_campaign(lua_State * L) {629int LuaPlayer::reveal_campaign(lua_State * L) {
629 if (get_game(L).get_ipl()->player_number() != player_number())630 if (get_game(L).get_ipl()->player_number() != player_number())
630 report_error(L, "Can only be called for interactive player!");631 report_error(L, "Can only be called for interactive player!");
632 report_error(L, "Can only be called for interactive player!");
631633
632 CampaignVisibilitySave cvs;634 CampaignVisibilitySave cvs;
633 cvs.set_campaign_visibility(luaL_checkstring(L, 2), true);635 cvs.set_campaign_visibility(luaL_checkstring(L, 2), true);
@@ -635,6 +637,41 @@
635 return 0;637 return 0;
636}638}
637639
640/* RST
641 .. method:: get_ships()
642
643 :returns: array of player's ships
644 :rtype: :class:`array` or :class:`table`
645*/
646int LuaPlayer::get_ships(lua_State * L) {
647 EditorGameBase & egbase = get_egbase(L);
648 Map * map = egbase.get_map();
649 PlayerNumber p = (get(L, egbase)).player_number();
650 lua_newtable(L);
651 uint32_t cidx = 1;
652
653 std::set<OPtr<Ship>> found_ships;
654 for (int16_t y = 0; y < map->get_height(); ++y) {
655 for (int16_t x = 0; x < map->get_width(); ++x) {
656 FCoords f = map->get_fcoords(Coords(x, y));
657 // there are too many bobs on the map so we investigate
658 // only bobs on water
659 if (f.field->nodecaps() & MOVECAPS_SWIM) {
660 for (Bob* bob = f.field->get_first_bob(); bob; bob = bob->get_next_on_field()) {
661 if (upcast(Ship, ship, bob)) {
662 if (ship->get_owner()->player_number() == p && !found_ships.count(ship)) {
663 found_ships.insert(ship);
664 lua_pushuint32(L, cidx++);
665 LuaMaps::upcasted_map_object_to_lua(L, ship);
666 lua_rawset(L, -3);
667 }
668 }
669 }
670 }
671 }
672 }
673 return 1;
674}
638675
639/* RST676/* RST
640 .. method:: get_buildings(which)677 .. method:: get_buildings(which)
641678
=== modified file 'src/scripting/lua_game.h'
--- src/scripting/lua_game.h 2015-02-09 05:57:08 +0000
+++ src/scripting/lua_game.h 2015-04-07 20:58:43 +0000
@@ -83,6 +83,7 @@
83 int hide_fields(lua_State * L);83 int hide_fields(lua_State * L);
84 int reveal_scenario(lua_State * L);84 int reveal_scenario(lua_State * L);
85 int reveal_campaign(lua_State * L);85 int reveal_campaign(lua_State * L);
86 int get_ships(lua_State * L);
86 int get_buildings(lua_State * L);87 int get_buildings(lua_State * L);
87 int get_suitability(lua_State * L);88 int get_suitability(lua_State * L);
88 int allow_workers(lua_State * L);89 int allow_workers(lua_State * L);
8990
=== modified file 'src/scripting/lua_map.cc'
--- src/scripting/lua_map.cc 2015-02-12 21:03:20 +0000
+++ src/scripting/lua_map.cc 2015-04-07 20:58:43 +0000
@@ -32,6 +32,7 @@
32#include "logic/maphollowregion.h"32#include "logic/maphollowregion.h"
33#include "logic/mapregion.h"33#include "logic/mapregion.h"
34#include "logic/player.h"34#include "logic/player.h"
35#include "logic/ship.h"
35#include "logic/soldier.h"36#include "logic/soldier.h"
36#include "logic/warelist.h"37#include "logic/warelist.h"
37#include "logic/widelands_geometry.h"38#include "logic/widelands_geometry.h"
@@ -2681,13 +2682,13 @@
2681 ==========================================================2682 ==========================================================
2682 */2683 */
26832684
2685
2684/*2686/*
2685 ==========================================================2687 ==========================================================
2686 C METHODS2688 C METHODS
2687 ==========================================================2689 ==========================================================
2688 */2690 */
26892691
2690
2691/* RST2692/* RST
2692Building2693Building
2693--------2694--------
@@ -2806,10 +2807,13 @@
2806 METHOD(LuaWarehouse, get_workers),2807 METHOD(LuaWarehouse, get_workers),
2807 METHOD(LuaWarehouse, set_soldiers),2808 METHOD(LuaWarehouse, set_soldiers),
2808 METHOD(LuaWarehouse, get_soldiers),2809 METHOD(LuaWarehouse, get_soldiers),
2810 METHOD(LuaWarehouse, start_expedition),
2811 METHOD(LuaWarehouse, cancel_expedition),
2809 {nullptr, nullptr},2812 {nullptr, nullptr},
2810};2813};
2811const PropertyType<LuaWarehouse> LuaWarehouse::Properties[] = {2814const PropertyType<LuaWarehouse> LuaWarehouse::Properties[] = {
2812 PROP_RO(LuaWarehouse, portdock),2815 PROP_RO(LuaWarehouse, portdock),
2816 PROP_RO(LuaWarehouse, expedition_in_progress),
2813 {nullptr, nullptr, nullptr},2817 {nullptr, nullptr, nullptr},
2814};2818};
28152819
@@ -2829,6 +2833,26 @@
2829 return upcasted_map_object_to_lua(L, get(L, get_egbase(L))->get_portdock());2833 return upcasted_map_object_to_lua(L, get(L, get_egbase(L))->get_portdock());
2830}2834}
28312835
2836/* RST
2837 .. attribute:: expedition_in_progress
2838
2839 (RO) If this Warehouse is a port, and an expedition is in
2840 progress, returns true, otherwise nil
2841*/
2842int LuaWarehouse::get_expedition_in_progress(lua_State * L) {
2843
2844 Warehouse* wh = get(L, get_egbase(L));
2845
2846 if (upcast(Game, game, &get_egbase(L))) {
2847 PortDock* pd = wh->get_portdock();
2848 if (pd) {
2849 if (pd->expedition_started()){
2850 return 1;
2851 }
2852 }
2853 }
2854 return 0;
2855}
28322856
2833/*2857/*
2834 ==========================================================2858 ==========================================================
@@ -2895,13 +2919,72 @@
2895 return do_set_soldiers(L, wh->get_position(), wh, wh->get_owner());2919 return do_set_soldiers(L, wh->get_position(), wh, wh->get_owner());
2896}2920}
28972921
2922/* RST
2923 .. method:: start_expedition(port)
2924
2925 :arg port
2926
2927 Starts preparation for expedition
2928
2929*/
2930int LuaWarehouse::start_expedition(lua_State* L) {
2931
2932 Warehouse* Wh = get(L, get_egbase(L));
2933
2934 if (!Wh) {
2935 return 0;
2936 }
2937
2938 if (upcast(Game, game, &get_egbase(L))) {
2939 PortDock* pd = Wh->get_portdock();
2940 if (!pd) {
2941 return 0;
2942 }
2943 if (!pd->expedition_started()){
2944 game->send_player_start_or_cancel_expedition(*Wh);
2945 return 1;
2946 }
2947 }
2948
2949 return 0;
2950}
2951
2952/* RST
2953 .. method:: cancel_expedition(port)
2954
2955 :arg port
2956
2957 Cancels an expedition if in progress
2958
2959*/
2960int LuaWarehouse::cancel_expedition(lua_State* L) {
2961
2962 Warehouse* Wh = get(L, get_egbase(L));
2963
2964 if (!Wh) {
2965 return 0;
2966 }
2967
2968 if (upcast(Game, game, &get_egbase(L))) {
2969 PortDock* pd = Wh->get_portdock();
2970 if (!pd) {
2971 return 0;
2972 }
2973 if (pd->expedition_started()){
2974 game->send_player_start_or_cancel_expedition(*Wh);
2975 return 1;
2976 }
2977 }
2978
2979 return 0;
2980}
2981
2898/*2982/*
2899 ==========================================================2983 ==========================================================
2900 C METHODS2984 C METHODS
2901 ==========================================================2985 ==========================================================
2902 */2986 */
29032987
2904
2905/* RST2988/* RST
2906ProductionSite2989ProductionSite
2907--------------2990--------------
@@ -3179,6 +3262,7 @@
3179 {nullptr, nullptr},3262 {nullptr, nullptr},
3180};3263};
3181const PropertyType<LuaBob> LuaBob::Properties[] = {3264const PropertyType<LuaBob> LuaBob::Properties[] = {
3265 PROP_RO(LuaBob, field),
3182 {nullptr, nullptr, nullptr},3266 {nullptr, nullptr, nullptr},
3183};3267};
31843268
@@ -3189,6 +3273,27 @@
3189 */3273 */
31903274
3191/* RST3275/* RST
3276 .. attribute:: field //working here
3277
3278 (RO) The field the bob is located on
3279*/
3280// UNTESTED
3281int LuaBob::get_field(lua_State * L) {
3282
3283 EditorGameBase & egbase = get_egbase(L);
3284
3285 Coords coords = get(L, egbase)->get_position();
3286
3287 return to_lua<LuaMaps::LuaField>(L, new LuaMaps::LuaField(coords.x, coords.y));
3288}
3289
3290
3291/*
3292 ==========================================================
3293 LUA METHODS
3294 ==========================================================
3295 */
3296/* RST
3192 .. method:: has_caps(capname)3297 .. method:: has_caps(capname)
31933298
3194 Similar to :meth:`Field::has_caps`.3299 Similar to :meth:`Field::has_caps`.
@@ -3214,13 +3319,6 @@
3214 return 1;3319 return 1;
3215}3320}
32163321
3217
3218/*
3219 ==========================================================
3220 LUA METHODS
3221 ==========================================================
3222 */
3223
3224/*3322/*
3225 ==========================================================3323 ==========================================================
3226 C METHODS3324 C METHODS
@@ -3240,12 +3338,16 @@
3240const MethodType<LuaShip> LuaShip::Methods[] = {3338const MethodType<LuaShip> LuaShip::Methods[] = {
3241 METHOD(LuaShip, get_wares),3339 METHOD(LuaShip, get_wares),
3242 METHOD(LuaShip, get_workers),3340 METHOD(LuaShip, get_workers),
3341 METHOD(LuaShip, build_colonization_port),
3243 {nullptr, nullptr},3342 {nullptr, nullptr},
3244};3343};
3245const PropertyType<LuaShip> LuaShip::Properties[] = {3344const PropertyType<LuaShip> LuaShip::Properties[] = {
3246 PROP_RO(LuaShip, debug_economy),3345 PROP_RO(LuaShip, debug_economy),
3247 PROP_RO(LuaShip, last_portdock),3346 PROP_RO(LuaShip, last_portdock),
3248 PROP_RO(LuaShip, destination),3347 PROP_RO(LuaShip, destination),
3348 PROP_RO(LuaShip, state),
3349 PROP_RW(LuaShip, scouting_direction),
3350 PROP_RW(LuaShip, island_explore_direction),
3249 {nullptr, nullptr, nullptr},3351 {nullptr, nullptr, nullptr},
3250};3352};
32513353
@@ -3285,6 +3387,149 @@
3285 return upcasted_map_object_to_lua(L, get(L, egbase)->get_lastdock(egbase));3387 return upcasted_map_object_to_lua(L, get(L, egbase)->get_lastdock(egbase));
3286}3388}
32873389
3390/* RST
3391 .. attribute:: state
3392
3393 Query which state the ship is in:
3394
3395 - transport,
3396 - exp_waiting, exp_scouting, exp_found_port_space, exp_colonizing,
3397 - sink_request, sink_animation
3398
3399 (RW) returns the :class:`string` ship's state, or :const:`nil` if there is no valid state.
3400
3401
3402*/
3403// UNTESTED sink states
3404int LuaShip::get_state(lua_State* L) {
3405 if (upcast(Game, game, &get_egbase(L))) {
3406 switch (get(L, get_egbase(L))->get_ship_state()) {
3407 case Ship::TRANSPORT:
3408 lua_pushstring(L, "transport");
3409 break;
3410 case Ship::EXP_WAITING:
3411 lua_pushstring(L, "exp_waiting");
3412 break;
3413 case Ship::EXP_SCOUTING:
3414 lua_pushstring(L, "exp_scouting");
3415 break;
3416 case Ship::EXP_FOUNDPORTSPACE:
3417 lua_pushstring(L, "exp_found_port_space");
3418 break;
3419 case Ship::EXP_COLONIZING:
3420 lua_pushstring(L, "exp_colonizing");
3421 break;
3422 case Ship::SINK_REQUEST:
3423 lua_pushstring(L, "sink_request");
3424 break;
3425 case Ship::SINK_ANIMATION:
3426 lua_pushstring(L, "sink_animation");
3427 break;
3428 default:
3429 lua_pushnil(L);
3430 return 0;
3431 }
3432 return 1;
3433 }
3434 return 0;
3435}
3436
3437int LuaShip::get_scouting_direction(lua_State* L) {
3438 if (upcast(Game, game, &get_egbase(L))) {
3439 switch (get(L, get_egbase(L))->get_scouting_direction()) {
3440 case WalkingDir::WALK_NE:
3441 lua_pushstring(L, "ne");
3442 break;
3443 case WalkingDir::WALK_E:
3444 lua_pushstring(L, "e");
3445 break;
3446 case WalkingDir::WALK_SE:
3447 lua_pushstring(L, "se");
3448 break;
3449 case WalkingDir::WALK_SW:
3450 lua_pushstring(L, "sw");
3451 break;
3452 case WalkingDir::WALK_W:
3453 lua_pushstring(L, "w");
3454 break;
3455 case WalkingDir::WALK_NW:
3456 lua_pushstring(L, "nw");
3457 break;
3458 default:
3459 return 0;
3460 }
3461 return 1;
3462 }
3463 return 0;
3464}
3465
3466int LuaShip::set_scouting_direction(lua_State* L) {
3467 if (upcast(Game, game, &get_egbase(L))) {
3468 std::string dirname = luaL_checkstring(L, 3);
3469 WalkingDir dir = WalkingDir::IDLE;
3470
3471 if (dirname == "ne") {
3472 dir = WalkingDir::WALK_NE;
3473 } else if (dirname == "e") {
3474 dir = WalkingDir::WALK_E;
3475 } else if (dirname == "se") {
3476 dir = WalkingDir::WALK_SE;
3477 } else if (dirname == "sw") {
3478 dir = WalkingDir::WALK_SW;
3479 } else if (dirname == "w") {
3480 dir = WalkingDir::WALK_W;
3481 } else if (dirname == "nw") {
3482 dir = WalkingDir::WALK_NW;
3483 } else {
3484 return 0;
3485 }
3486 game->send_player_ship_scouting_direction(*get(L, get_egbase(L)), dir);
3487 return 1;
3488 }
3489 return 0;
3490
3491}
3492
3493/* RST
3494 .. attribute:: island_explore_direction
3495
3496 (RW) actual direction if the ship sails around an island.
3497 Sets/returns cw, ccw or nil
3498
3499*/
3500int LuaShip::get_island_explore_direction(lua_State* L) {
3501 if (upcast(Game, game, &get_egbase(L))) {
3502 switch (get(L, get_egbase(L))->get_island_explore_direction()) {
3503 case IslandExploreDirection::kCounterClockwise:
3504 lua_pushstring(L, "ccw");
3505 break;
3506 case IslandExploreDirection::kClockwise:
3507 lua_pushstring(L, "cw");
3508 break;
3509 default:
3510 return 0;
3511 }
3512 return 1;
3513 }
3514 return 0;
3515}
3516
3517int LuaShip::set_island_explore_direction(lua_State* L) {
3518 if (upcast(Game, game, &get_egbase(L))) {
3519 Ship* ship = get(L, get_egbase(L));
3520 std::string dir = luaL_checkstring(L, 3);
3521 if (dir == "ccw"){
3522 game->send_player_ship_explore_island(*ship, IslandExploreDirection::kCounterClockwise);
3523 } else if (dir == "cw") {
3524 game->send_player_ship_explore_island(*ship, IslandExploreDirection::kClockwise);
3525 } else {
3526 return 0;
3527 }
3528 return 1;
3529 }
3530 return 0;
3531}
3532
3288/*3533/*
3289 ==========================================================3534 ==========================================================
3290 LUA METHODS3535 LUA METHODS
@@ -3341,6 +3586,25 @@
3341 return 1;3586 return 1;
3342}3587}
33433588
3589/* RST
3590 .. method:: build_colonization_port()
3591
3592 Returns true if port space construction was started (ship was in adequate
3593 status and a found portspace was nearby)
3594
3595 :returns: true/false
3596*/
3597int LuaShip::build_colonization_port(lua_State* L) {
3598 Ship* ship = get(L, get_egbase(L));
3599 if (ship->get_ship_state() == Widelands::Ship::EXP_FOUNDPORTSPACE) {
3600 if (upcast(Game, game, &get_egbase(L))) {
3601 game->send_player_ship_construct_port(*ship, ship->exp_port_spaces()->front());
3602 return 1;
3603 }
3604 }
3605 return 0;
3606}
3607
3344/*3608/*
3345 ==========================================================3609 ==========================================================
3346 C METHODS3610 C METHODS
33473611
=== modified file 'src/scripting/lua_map.h'
--- src/scripting/lua_map.h 2015-02-12 21:03:20 +0000
+++ src/scripting/lua_map.h 2015-04-07 20:58:43 +0000
@@ -688,6 +688,7 @@
688 * Properties688 * Properties
689 */689 */
690 int get_portdock(lua_State* L);690 int get_portdock(lua_State* L);
691 int get_expedition_in_progress(lua_State* L);
691692
692 /*693 /*
693 * Lua Methods694 * Lua Methods
@@ -698,6 +699,8 @@
698 int set_workers(lua_State*);699 int set_workers(lua_State*);
699 int set_soldiers(lua_State*);700 int set_soldiers(lua_State*);
700 int get_soldiers(lua_State*);701 int get_soldiers(lua_State*);
702 int start_expedition(lua_State*);
703 int cancel_expedition(lua_State*);
701704
702 /*705 /*
703 * C Methods706 * C Methods
@@ -806,6 +809,7 @@
806 /*809 /*
807 * Properties810 * Properties
808 */811 */
812 int get_field(lua_State *);
809 int has_caps(lua_State *);813 int has_caps(lua_State *);
810814
811 /*815 /*
@@ -885,12 +889,17 @@
885 int get_debug_economy(lua_State * L);889 int get_debug_economy(lua_State * L);
886 int get_last_portdock(lua_State* L);890 int get_last_portdock(lua_State* L);
887 int get_destination(lua_State* L);891 int get_destination(lua_State* L);
888892 int get_state(lua_State* L);
893 int get_scouting_direction(lua_State* L);
894 int set_scouting_direction(lua_State* L);
895 int get_island_explore_direction(lua_State* L);
896 int set_island_explore_direction(lua_State* L);
889 /*897 /*
890 * Lua methods898 * Lua methods
891 */899 */
892 int get_wares(lua_State* L);900 int get_wares(lua_State* L);
893 int get_workers(lua_State* L);901 int get_workers(lua_State* L);
902 int build_colonization_port(lua_State* L);
894903
895 /*904 /*
896 * C methods905 * C methods
897906
=== modified file 'src/wui/shipwindow.cc'
--- src/wui/shipwindow.cc 2015-02-05 12:11:20 +0000
+++ src/wui/shipwindow.cc 2015-04-07 20:58:43 +0000
@@ -68,9 +68,9 @@
68 void act_destination();68 void act_destination();
69 void act_sink();69 void act_sink();
70 void act_cancel_expedition();70 void act_cancel_expedition();
71 void act_scout_towards(uint8_t);71 void act_scout_towards(WalkingDir);
72 void act_construct_port();72 void act_construct_port();
73 void act_explore_island(ScoutingDirection);73 void act_explore_island(IslandExploreDirection);
7474
75private:75private:
76 InteractiveGameBase & m_igbase;76 InteractiveGameBase & m_igbase;
@@ -120,7 +120,7 @@
120 m_btn_explore_island_cw =120 m_btn_explore_island_cw =
121 make_button121 make_button
122 (exp_top, "expcw", _("Explore the island’s coast clockwise"), pic_explore_cw,122 (exp_top, "expcw", _("Explore the island’s coast clockwise"), pic_explore_cw,
123 boost::bind(&ShipWindow::act_explore_island, this, ScoutingDirection::kClockwise));123 boost::bind(&ShipWindow::act_explore_island, this, IslandExploreDirection::kClockwise));
124 exp_top->add(m_btn_explore_island_cw, 0, false);124 exp_top->add(m_btn_explore_island_cw, 0, false);
125125
126 m_btn_scout[WALK_NE - 1] =126 m_btn_scout[WALK_NE - 1] =
@@ -156,7 +156,7 @@
156 m_btn_explore_island_ccw =156 m_btn_explore_island_ccw =
157 make_button157 make_button
158 (exp_bot, "expccw", _("Explore the island’s coast counter clockwise"), pic_explore_ccw,158 (exp_bot, "expccw", _("Explore the island’s coast counter clockwise"), pic_explore_ccw,
159 boost::bind(&ShipWindow::act_explore_island, this, ScoutingDirection::kCounterClockwise));159 boost::bind(&ShipWindow::act_explore_island, this, IslandExploreDirection::kCounterClockwise));
160 exp_bot->add(m_btn_explore_island_ccw, 0, false);160 exp_bot->add(m_btn_explore_island_ccw, 0, false);
161161
162 m_btn_scout[WALK_SE - 1] =162 m_btn_scout[WALK_SE - 1] =
@@ -312,11 +312,11 @@
312}312}
313313
314/// Sends a player command to the ship to scout towards a specific direction314/// Sends a player command to the ship to scout towards a specific direction
315void ShipWindow::act_scout_towards(uint8_t direction) {315void ShipWindow::act_scout_towards(WalkingDir direction) {
316 // ignore request if the direction is not swimable at all316 // ignore request if the direction is not swimable at all
317 if (!m_ship.exp_dir_swimable(direction))317 if (!m_ship.exp_dir_swimable(static_cast<Direction>(direction)))
318 return;318 return;
319 m_igbase.game().send_player_ship_scout_direction(m_ship, direction);319 m_igbase.game().send_player_ship_scouting_direction(m_ship, direction);
320}320}
321321
322/// Constructs a port at the port build space in vision range322/// Constructs a port at the port build space in vision range
@@ -327,7 +327,7 @@
327}327}
328328
329/// Explores the island cw or ccw329/// Explores the island cw or ccw
330void ShipWindow::act_explore_island(ScoutingDirection direction) {330void ShipWindow::act_explore_island(IslandExploreDirection direction) {
331 bool coast_nearby = false;331 bool coast_nearby = false;
332 bool moveable = false;332 bool moveable = false;
333 for (Direction dir = 1; (dir <= LAST_DIRECTION) && (!coast_nearby || !moveable); ++dir) {333 for (Direction dir = 1; (dir <= LAST_DIRECTION) && (!coast_nearby || !moveable); ++dir) {
334334
=== modified file 'test/maps/expedition.wmf/scripting/init.lua'
--- test/maps/expedition.wmf/scripting/init.lua 2014-08-01 15:30:12 +0000
+++ test/maps/expedition.wmf/scripting/init.lua 2015-04-07 20:58:43 +0000
@@ -93,24 +93,6 @@
93 assert_equal(1, port:get_workers("builder"))93 assert_equal(1, port:get_workers("builder"))
94end94end
9595
96function start_expedition()
97 assert_true(click_building(p1, "port"))
98 sleep(100)
99 assert_true(click_button("start_expedition"))
100 sleep(100)
101 close_windows()
102 sleep(100)
103end
104
105function cancel_expedition_in_port()
106 assert_true(click_building(p1, "port"))
107 sleep(100)
108 assert_true(click_button("cancel_expedition"))
109 sleep(100)
110 close_windows()
111 sleep(100)
112end
113
114function cancel_expedition_in_shipwindow(which_ship)96function cancel_expedition_in_shipwindow(which_ship)
115 click_on_ship(which_ship or first_ship)97 click_on_ship(which_ship or first_ship)
116 assert_true(click_button("cancel_expedition"))98 assert_true(click_button("cancel_expedition"))
@@ -174,7 +156,7 @@
174 game.desired_speed = 10 * 1000156 game.desired_speed = 10 * 1000
175157
176 -- Start a new expedition.158 -- Start a new expedition.
177 start_expedition()159 port:start_expedition()
178 wait_for_message("Expedition Ready")160 wait_for_message("Expedition Ready")
179 game.desired_speed = 10 * 1000161 game.desired_speed = 10 * 1000
180 sleep(10000)162 sleep(10000)
@@ -205,14 +187,15 @@
205 game.desired_speed = 10 * 1000187 game.desired_speed = 10 * 1000
206188
207 -- Start a new expedition.189 -- Start a new expedition.
208 start_expedition()190 port:start_expedition()
209 wait_for_message("Expedition Ready")191 wait_for_message("Expedition Ready")
210 game.desired_speed = 10 * 1000192 game.desired_speed = 10 * 1000
211 sleep(10000)193 sleep(10000)
212194
213 click_on_ship(first_ship)195 first_ship.island_explore_direction="ccw"
214 assert_true(click_button("expccw"))196 sleep(2000)
215 sleep(8000)197 assert_equal("ccw",first_ship.island_explore_direction)
198 sleep(6000)
216199
217 stable_save("sailing")200 stable_save("sailing")
218 assert_equal(1, p1:get_workers("builder"))201 assert_equal(1, p1:get_workers("builder"))
@@ -235,13 +218,14 @@
235 game.desired_speed = 10 * 1000218 game.desired_speed = 10 * 1000
236219
237 -- Send expedition to port space.220 -- Send expedition to port space.
238 start_expedition()221 port:start_expedition()
239 wait_for_message("Expedition Ready")222 wait_for_message("Expedition Ready")
240 assert_equal(1, p1:get_workers("builder"))223 assert_equal(1, p1:get_workers("builder"))
241 sleep(500)224 sleep(500)
242225
243 click_on_ship(first_ship)226 first_ship.island_explore_direction="ccw"
244 assert_true(click_button("expccw"))227 sleep(2000)
228 assert_equal("ccw",first_ship.island_explore_direction)
245 wait_for_message("Port Space Found")229 wait_for_message("Port Space Found")
246 sleep(500)230 sleep(500)
247 assert_equal(1, p1:get_workers("builder"))231 assert_equal(1, p1:get_workers("builder"))
@@ -272,12 +256,13 @@
272 port:set_wares("blackwood", 100)256 port:set_wares("blackwood", 100)
273257
274258
275 start_expedition()259 port:start_expedition()
276 wait_for_message("Expedition Ready")260 wait_for_message("Expedition Ready")
277 click_on_ship(first_ship)261 first_ship.island_explore_direction="ccw"
278 assert_true(click_button("expccw"))262 sleep(2000)
263 assert_equal("ccw",first_ship.island_explore_direction)
279 wait_for_message("Port Space Found")264 wait_for_message("Port Space Found")
280 assert_true(click_button("buildport"))265 first_ship:build_colonization_port()
281 sleep(500)266 sleep(500)
282 assert_equal(1, p1:get_workers("builder"))267 assert_equal(1, p1:get_workers("builder"))
283 wait_for_message("Port")268 wait_for_message("Port")
284269
=== added file 'test/maps/expedition.wmf/scripting/test_ship_movement_controls.lua'
--- test/maps/expedition.wmf/scripting/test_ship_movement_controls.lua 1970-01-01 00:00:00 +0000
+++ test/maps/expedition.wmf/scripting/test_ship_movement_controls.lua 2015-04-07 20:58:43 +0000
@@ -0,0 +1,115 @@
1run(function()
2 game.desired_speed = 30 * 1000
3 p1:place_bob("ship", map:get_field(10, 10))
4
5 port = map:get_field(16, 16).immovable
6 port:set_wares("log", 10) -- no sense to wait
7 port:set_wares("blackwood", 10)
8
9 --getting table with all our ships (single one only)
10 ships = p1:get_ships()
11
12 --veryfing that ship is indeed placed where should be :)
13 assert_equal(10,ships[1].field.x)
14 assert_equal(10,ships[1].field.y)
15
16 --ships table should contain 1 item (1 ship)
17 assert_equal(1, #ships)
18
19 --ship has no wares on it
20 assert_equal(0,ships[1]:get_wares())
21
22 --no destination is set
23 assert(not ships[1].destination)
24
25 --ships in transport state
26 assert_equal("transport", ships[1].state)
27
28 --the warehouse is probably not in expedition state :)
29 assert(not map:get_field(8, 18).immovable.expedition_in_progress)
30
31 --starting prepartion for expedition
32 assert(not port.expedition_in_progress)
33 port:start_expedition()
34 sleep (300)
35 assert(port.expedition_in_progress)
36
37 --ships changes state when exp ready
38 while ships[1].state == "transport" do sleep(2000) end
39 assert_equal("exp_waiting", ships[1].state)
40
41 --sending NW and verifying
42 ships[1].scouting_direction="nw"
43 sleep(6000)
44 assert_equal("nw", ships[1].scouting_direction)
45 assert_equal("exp_scouting", ships[1].state)
46
47 while ships[1].scouting_direction == "nw" do
48 sleep (2000)
49 end
50
51 --now ships stops nearby NW coast, so sending it back
52 ships[1].scouting_direction="se"
53 sleep(4000)
54 assert_equal("se", ships[1].scouting_direction)
55
56 --testing remaining directions
57 ships[1].scouting_direction="e"
58 sleep(2000)
59 assert_equal("e", ships[1].scouting_direction)
60
61 ships[1].scouting_direction="w"
62 sleep(2000)
63 assert_equal("w", ships[1].scouting_direction)
64
65 ships[1].scouting_direction="sw"
66 sleep(2000)
67 assert_equal("sw", ships[1].scouting_direction)
68
69 ships[1].scouting_direction="ne"
70 sleep(2000)
71 assert_equal("ne", ships[1].scouting_direction)
72
73 --back to original course
74 ships[1].scouting_direction="se"
75 sleep(2000)
76 assert_equal("se", ships[1].scouting_direction)
77
78 --waiting till it stops (no direction/nil is returned)
79 while ships[1].scouting_direction do sleep(2000) end
80
81 --sending to scout the island
82 ships[1].island_explore_direction="ccw";
83 sleep(3000)
84 assert_equal("ccw", ships[1].island_explore_direction)
85 assert_equal("exp_scouting", ships[1].state)
86
87 --fine, now change the direction
88 ships[1].island_explore_direction="cw";
89 sleep(3000)
90 assert_equal("cw", ships[1].island_explore_direction)
91
92 -- wait till it finds a port
93 wait_for_message("Port Space Found")
94 sleep(500)
95 assert_equal("exp_found_port_space", ships[1].state)
96
97 --starting colonization port here
98 assert(ships[1]:build_colonization_port())
99 sleep(500)
100 assert_equal("exp_colonizing", ships[1].state)
101 sleep(15000)
102 stable_save("port_in_constr")
103
104 -- while unfinished yet, removing it
105 new_port=map:get_field(16,2).immovable
106 assert(new_port)
107 new_port:remove()
108 sleep(3000)
109
110 --yes, the ships is back in transport mode
111 assert_equal("transport", ships[1].state)
112
113 print("# All Tests passed.")
114 wl.ui.MapView():close()
115end)
0116
=== modified file 'test/maps/expedition.wmf/scripting/test_starting_and_immediately_canceling.lua'
--- test/maps/expedition.wmf/scripting/test_starting_and_immediately_canceling.lua 2013-10-29 20:22:08 +0000
+++ test/maps/expedition.wmf/scripting/test_starting_and_immediately_canceling.lua 2015-04-07 20:58:43 +0000
@@ -5,8 +5,12 @@
55
6 -- Start and immediately cancel an expedition.6 -- Start and immediately cancel an expedition.
7 print("---- 1 -----")7 print("---- 1 -----")
8 start_expedition()8 port:start_expedition()
9 cancel_expedition_in_port()9 sleep(500)
10 assert(port.expedition_in_progress)
11 port:cancel_expedition()
12 sleep(500)
13 assert(not port.expedition_in_progress)
10 sleep(500)14 sleep(500)
11 assert_equal(1, p1:get_workers("builder"))15 assert_equal(1, p1:get_workers("builder"))
1216
1317
=== modified file 'test/maps/expedition.wmf/scripting/test_starting_wait_a_while_cancel.lua'
--- test/maps/expedition.wmf/scripting/test_starting_wait_a_while_cancel.lua 2013-10-29 20:22:08 +0000
+++ test/maps/expedition.wmf/scripting/test_starting_wait_a_while_cancel.lua 2015-04-07 20:58:43 +0000
@@ -4,12 +4,12 @@
44
5 -- Start an expedition, but let them carry some wares into it. This also5 -- Start an expedition, but let them carry some wares into it. This also
6 -- gives the builder enough time to walk over.6 -- gives the builder enough time to walk over.
7 start_expedition()7 port:start_expedition()
8 sleep(50000)8 sleep(50000)
9 stable_save("cancel_in_port")9 stable_save("cancel_in_port")
10 assert_equal(1, p1:get_workers("builder"))10 assert_equal(1, p1:get_workers("builder"))
1111
12 cancel_expedition_in_port()12 port:cancel_expedition()
13 sleep(500)13 sleep(500)
14 assert_equal(1, p1:get_workers("builder"))14 assert_equal(1, p1:get_workers("builder"))
1515
1616
=== modified file 'test/maps/ship_transportation.wmf/scripting/init.lua'
--- test/maps/ship_transportation.wmf/scripting/init.lua 2015-02-10 21:25:14 +0000
+++ test/maps/ship_transportation.wmf/scripting/init.lua 2015-04-07 20:58:43 +0000
@@ -37,23 +37,6 @@
37 return nil37 return nil
38end38end
3939
40function portdock2()
41 local portdock = map:get_field(15, 4).immovable
42 if portdock then
43 return portdock
44 end
45 local portdock = map:get_field(14, 5).immovable
46 if portdock then
47 return portdock
48 end
49 local portdock = map:get_field(14, 4).immovable
50 if portdock then
51 return portdock
52 end
53 print ("portdock not found")
54 return nill
55end
56
57function start_building_farm()40function start_building_farm()
58 p1:place_building("farm", map:get_field(18, 4), true, true)41 p1:place_building("farm", map:get_field(18, 4), true, true)
59 connected_road(p1, map:get_field(18,5).immovable, "l,l|tl,tr|", true)42 connected_road(p1, map:get_field(18,5).immovable, "l,l|tl,tr|", true)
6043
=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_portdock_with_worker_and_ware_in_transit.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_portdock_with_worker_and_ware_in_transit.lua 2015-02-10 21:25:14 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_portdock_with_worker_and_ware_in_transit.lua 2015-04-07 20:58:43 +0000
@@ -32,7 +32,9 @@
32 stable_save("restored_port")32 stable_save("restored_port")
33 33
34 -- remove the portdock while the blackwood is in transit.34 -- remove the portdock while the blackwood is in transit.
35 portdock2():remove()35 port2_portdock=port2().portdock
36 assert(port2_portdock)
37 port2_portdock:remove()
36 38
37 sleep(5000)39 sleep(5000)
3840
3941
=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_worker_in_portdock.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_worker_in_portdock.lua 2014-01-18 12:40:08 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_worker_in_portdock.lua 2015-04-07 20:58:43 +0000
@@ -5,6 +5,13 @@
5 create_first_port()5 create_first_port()
6 create_second_port()6 create_second_port()
77
8 --removing portdock first
9 portdock_fields=port2().portdock.fields
10 portdock_fields[1].immovable:remove()
11 sleep(100)
12 --portdock should be back, as port is still there
13 assert (portdock_fields[1].immovable)
14
8 start_building_farm()15 start_building_farm()
9 port1():set_workers{16 port1():set_workers{
10 builder = 1,17 builder = 1,
@@ -16,8 +23,13 @@
16 assert_equal(p1:get_workers("builder"), 1)23 assert_equal(p1:get_workers("builder"), 1)
17 assert_equal(port1():get_workers("builder"), 0)24 assert_equal(port1():get_workers("builder"), 0)
1825
26 portdock_fields=port2().portdock.fields
19 port2():remove()27 port2():remove()
20 sleep(100)28 sleep(100)
29 --verify that also portdock was removed
30 assert (not portdock_fields[1].immovable)
31
32 sleep(100)
2133
22 stable_save("worker_in_portdock")34 stable_save("worker_in_portdock")
2335

Subscribers

People subscribed via source and target branches

to status/vote changes: