Merge lp:~widelands-dev/widelands/bug-1675179-lua-hide-fields into lp:widelands

Proposed by GunChleoc
Status: Merged
Merged at revision: 8339
Proposed branch: lp:~widelands-dev/widelands/bug-1675179-lua-hide-fields
Merge into: lp:widelands
Diff against target: 120 lines (+47/-9)
4 files modified
src/logic/player.cc (+12/-6)
src/logic/player.h (+5/-1)
src/scripting/lua_game.cc (+6/-2)
test/maps/lua_testsuite.wmf/scripting/gplayer.lua (+24/-0)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1675179-lua-hide-fields
Reviewer Review Type Date Requested Status
SirVer Approve
kaputtnik (community) testing Approve
Review via email: mp+320981@code.launchpad.net

Commit message

Added option to Lua function player:hide_fields to mark them as unexplored.

Description of the change

New Lua function for the Barbarians 3 scenario.

For testing, one can change the Empire 1 scenario like this:

 -- Show the sea
   p1:reveal_fields(sea:region(6))
   local ship = p1:place_ship(sea)
   sleep(1000)
   p1:reveal_fields(wl.Game().map.player_slots[1].starting_field:region(50))
   sleep(4000)
   p1:hide_fields(wl.Game().map.player_slots[1].starting_field:region(50), true)
   campaign_message_box(diary_page_2)
   -- Hide the sea after 5 seconds

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

Continuous integration builds have changed state:

Travis build 2062. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/214787145.
Appveyor build 1897. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_bug_1675179_lua_hide_fields-1897.

Revision history for this message
kaputtnik (franku) wrote :

I played a bit with this new feature and it works great :-)

One could hide now also the starting position and reveal it after the map is scrolled. An imagination of blending in and out different scenes. A nice to have would be to make the blending more smooth, but a lua function that hide/relveal one of the fields of a region clockwise until the complete region is hidden/revealed could be implemented now.

review: Approve (testing)
Revision history for this message
GunChleoc (gunchleoc) wrote :

You code doesn't support blending at all at the moment - you see it, or you don't.

The clockwise reveal is a nice alternative idea and could be implemented entirely in Lua. Or maybe hide from the outside in and reveal from the inside out.

Revision history for this message
SirVer (sirver) wrote :

Some thoughts inlined.

review: Needs Fixing
Revision history for this message
GunChleoc (gunchleoc) wrote :

Good idea about the enum class and the variable name, I will change that :)

I don't think that we will have desyncs here though:

- It's an additional option to an already existing function, so we should have seen desyncs before if this was a problem
- It only affects the current player, other players' and critters' states shouldn't care about what I see
- We would have functions like Bob::set_position and Building:set_seeing trigger a playercommand down the line just for their current vision - all their state changes should have been handled already at that point

Feel free to convince me otherwise, I'm not that familiar with all the networking stuff.

Revision history for this message
SirVer (sirver) wrote :

- It only affects the current player, other players' and critters' states shouldn't care about what I see

afaik the only thing that does look at your current vision is the scout. And if you have a different vision table than some other host on the game around the area of the scout, the scout will make different decisions and desync the game.

I think this branch does not change the dangerous around this (as you said), but the feature could be used to potentially desync multiplayer scenarios if some reveal code only runs on one of the networked machines. I think this can be ignored for now.

lgtm.

@bunnybot merge

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/logic/player.cc'
2--- src/logic/player.cc 2017-03-03 18:13:55 +0000
3+++ src/logic/player.cc 2017-04-20 08:41:02 +0000
4@@ -1024,9 +1024,10 @@
5 field.vision = fvision;
6 }
7
8-void Player::unsee_node(MapIndex const i, Time const gametime, bool const forward) {
9+/// If 'mode' = UnseeMode::kUnexplore, fields will be marked as unexplored. Else, player no longer sees what's currently going on.
10+void Player::unsee_node(MapIndex const i, Time const gametime, const UnseeNodeMode mode, bool const forward) {
11 Field& field = fields_[i];
12- if (field.vision <= 1) // Already does not see this
13+ if ((mode == UnseeNodeMode::kUnsee && field.vision <= 1) || field.vision < 1) // Already does not see this
14 return;
15
16 // If this is not already a forwarded call, we should inform allied players
17@@ -1035,13 +1036,18 @@
18 update_team_players();
19 if (!forward && !team_player_.empty()) {
20 for (uint8_t j = 0; j < team_player_.size(); ++j)
21- team_player_[j]->unsee_node(i, gametime, true);
22+ team_player_[j]->unsee_node(i, gametime, mode, true);
23 }
24
25- --field.vision;
26- if (field.vision == 1)
27+ if (mode == UnseeNodeMode::kUnexplore) {
28+ field.vision = 0;
29+ } else {
30+ --field.vision;
31+ assert(1 <= field.vision);
32+ }
33+ if (field.vision < 2) {
34 field.time_node_last_unseen = gametime;
35- assert(1 <= field.vision);
36+ }
37 }
38
39 /**
40
41=== modified file 'src/logic/player.h'
42--- src/logic/player.h 2017-01-25 18:55:59 +0000
43+++ src/logic/player.h 2017-04-20 08:41:02 +0000
44@@ -445,7 +445,11 @@
45 const bool forward = false);
46
47 /// Decrement this player's vision for a node.
48- void unsee_node(const MapIndex, const Time, const bool forward = false);
49+ enum class UnseeNodeMode {
50+ kUnsee,
51+ kUnexplore
52+ };
53+ void unsee_node(const MapIndex, const Time, UnseeNodeMode mode = UnseeNodeMode::kUnsee, const bool forward = false);
54
55 /// Call see_node for each node in the area.
56 void see_area(const Area<FCoords>& area) {
57
58=== modified file 'src/scripting/lua_game.cc'
59--- src/scripting/lua_game.cc 2017-01-25 18:55:59 +0000
60+++ src/scripting/lua_game.cc 2017-04-20 08:41:02 +0000
61@@ -620,6 +620,9 @@
62 :arg fields: The fields to hide
63 :type fields: :class:`array` of :class:`wl.map.Fields`
64
65+ :arg hide_completely: *Optional*. The fields will be marked as unexplored.
66+ :type hide_completely: :class:`boolean`
67+
68 :returns: :const:`nil`
69 */
70 int LuaPlayer::hide_fields(lua_State* L) {
71@@ -628,11 +631,12 @@
72 Map& m = egbase.map();
73
74 luaL_checktype(L, 2, LUA_TTABLE);
75+ const bool mode = !lua_isnone(L, 3) && luaL_checkboolean(L, 3);
76
77 lua_pushnil(L); /* first key */
78 while (lua_next(L, 2) != 0) {
79- p.unsee_node(
80- (*get_user_class<LuaField>(L, -1))->fcoords(L).field - &m[0], egbase.get_gametime());
81+ p.unsee_node((*get_user_class<LuaField>(L, -1))->fcoords(L).field - &m[0],
82+ egbase.get_gametime(), mode ? Player::UnseeNodeMode::kUnexplore : Player::UnseeNodeMode::kUnsee);
83 lua_pop(L, 1);
84 }
85
86
87=== modified file 'test/maps/lua_testsuite.wmf/scripting/gplayer.lua'
88--- test/maps/lua_testsuite.wmf/scripting/gplayer.lua 2017-01-17 20:07:46 +0000
89+++ test/maps/lua_testsuite.wmf/scripting/gplayer.lua 2017-04-20 08:41:02 +0000
90@@ -59,6 +59,30 @@
91 assert_equal(true, player1:sees_field(self.f))
92 end
93
94+-- This test must go last, because we change the state of player1:seen_field.
95+function player_vision_tests:test_hide_completely()
96+ player1.see_all = true
97+ assert_equal(true, player1:sees_field(self.f))
98+ player1.see_all = false
99+ assert_equal(false, player1:sees_field(self.f))
100+
101+ player1:hide_fields(self.f:region(1), false)
102+ game.desired_speed = 0;
103+ assert_equal(false, player1:sees_field(self.f))
104+ assert_equal(true, player1:seen_field(self.f))
105+
106+ player1.see_all = true
107+ assert_equal(true, player1:sees_field(self.f))
108+ player1.see_all = false
109+ assert_equal(false, player1:sees_field(self.f))
110+
111+ player1:hide_fields(self.f:region(1), true)
112+ game.desired_speed = 0;
113+ assert_equal(false, player1:sees_field(self.f))
114+ assert_equal(false, player1:seen_field(self.f))
115+ player1:reveal_fields(self.f:region(1))
116+end
117+
118
119 -- =========================
120 -- Forbid & Allow buildings

Subscribers

People subscribed via source and target branches

to status/vote changes: