Merge lp:~widelands-dev/widelands/bug-1751440-smugglers-desync into lp:widelands

Proposed by GunChleoc
Status: Work in progress
Proposed branch: lp:~widelands-dev/widelands/bug-1751440-smugglers-desync
Merge into: lp:widelands
Diff against target: 96 lines (+48/-20)
1 file modified
data/maps/MP_Scenarios/Smugglers.wmf/scripting/smuggling.lua (+48/-20)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1751440-smugglers-desync
Reviewer Review Type Date Requested Status
Widelands Developers Pending
Review via email: mp+340735@code.launchpad.net

Commit message

To address desync problems in Smugglers, going round robin rather than smuggling a random ware. Also, allow smuggling if players are playing different tribes - only wares that both tribes have will be smuggled.

Description of the change

I'm not 100% sure if this branch actually fixes the problem, by I was able to play a full game with 2 players having 2 smuggling routes and the 2 other players idle.

Also, we can now expand the scenario in the future to allow players to choose their tribes, but we need to finish the game loading screen UI first before we can start working on that.

ETA: Just got another desync, so it's not fixed.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'data/maps/MP_Scenarios/Smugglers.wmf/scripting/smuggling.lua'
2--- data/maps/MP_Scenarios/Smugglers.wmf/scripting/smuggling.lua 2016-03-26 09:09:43 +0000
3+++ data/maps/MP_Scenarios/Smugglers.wmf/scripting/smuggling.lua 2018-03-05 10:38:57 +0000
4@@ -35,8 +35,27 @@
5 game_over_done = true
6 end
7
8-function do_smuggling(route_descr, recv_plr, send_plr, recv_whf, send_whf)
9- while 1 do
10+function do_smuggling(route_descr, recv_plr, send_plr, recv_wh_field, send_wh_field)
11+ -- Collect ware types that both sending and receiving player can use
12+ local wares = {}
13+ for idx,ware in pairs(send_plr.tribe.wares) do
14+ if recv_plr.tribe:has_ware(ware.name) then
15+ table.insert(wares, ware.name)
16+ end
17+ end
18+
19+ -- If the tribes don't have any wares in common, nothing can be smuggled
20+ -- This should not happen, but let's have a safeguard anyway.
21+ if #wares < 1 then
22+ do_game_over()
23+ return
24+ end
25+
26+ -- We start counting at 0 so that we can use the modulo (%) operator
27+ -- for going round robin
28+ local last_ware_index = 0;
29+
30+ while true do
31 sleep(10000) -- Sleep 10s
32
33 if points[1] >= points_to_win or
34@@ -46,36 +65,45 @@
35 break
36 end
37
38- if not send_whf.immovable or
39- send_whf.immovable.descr.type_name ~= "warehouse" or
40- send_whf.immovable.owner ~= send_plr or
41- not recv_whf.immovable or
42- recv_whf.immovable.descr.type_name ~= "warehouse" or
43- recv_whf.immovable.owner ~= recv_plr
44+ if not send_wh_field.immovable or
45+ send_wh_field.immovable.descr.type_name ~= "warehouse" or
46+ send_wh_field.immovable.owner ~= send_plr or
47+ not recv_wh_field.immovable or
48+ recv_wh_field.immovable.descr.type_name ~= "warehouse" or
49+ recv_wh_field.immovable.owner ~= recv_plr
50 then
51 send_to_all(smuggling_route_broken:bformat(
52 (ngettext("%i point", "%i points", route_descr.value)):format(route_descr.value), recv_plr.name, send_plr.name)
53 )
54 run(wait_for_established_route, route_descr)
55- break
56+ return
57 end
58
59- -- Warp one ware
60- local wares = send_whf.immovable:get_wares("all")
61- local wn = {}
62- for name,count in pairs(wares) do
63- if count > 0 then
64- wn[#wn + 1] = name
65+ -- Warp the next available ware, going round robin
66+ local empty_warehouse_guard = #wares
67+ local warp_index = last_ware_index
68+ local ware_to_warp = nil
69+ while empty_warehouse_guard > 0 do
70+ -- Index shift, because Lua tables start counting at 1
71+ local candidate = wares[warp_index + 1]
72+ if send_wh_field.immovable:get_wares(candidate) > 0 then
73+ ware_to_warp = candidate
74+ break
75 end
76+
77+ warp_index = (warp_index + 1) % #wares;
78+ empty_warehouse_guard = empty_warehouse_guard - 1
79 end
80- if #wn > 0 then
81- local ware_to_warp = wn[math.random(#wn)]
82- send_whf.immovable:set_wares(ware_to_warp, wares[ware_to_warp] - 1)
83- recv_whf.immovable:set_wares(
84- ware_to_warp, recv_whf.immovable:get_wares(ware_to_warp) + 1
85+
86+ if ware_to_warp ~= nil then
87+ send_wh_field.immovable:set_wares(ware_to_warp, send_wh_field.immovable:get_wares(ware_to_warp) - 1)
88+ recv_wh_field.immovable:set_wares(
89+ ware_to_warp, recv_wh_field.immovable:get_wares(ware_to_warp) + 1
90 )
91 points[recv_plr.team] = points[recv_plr.team] + route_descr.value
92 end
93+ -- Next round robin index
94+ last_ware_index = (last_ware_index + 1) % #wares;
95 end
96 end
97

Subscribers

People subscribed via source and target branches

to status/vote changes: