File: lock.lua

package info (click to toggle)
widelands 2%3A1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 684,084 kB
  • sloc: cpp: 196,737; ansic: 19,395; python: 8,515; sh: 1,734; xml: 700; makefile: 46; lisp: 25
file content (34 lines) | stat: -rw-r--r-- 1,000 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- TODO(GunChleoc): Document more fully

-- Sleep until we acquire permission to run
function wait_for_lock(player, training_wheel_name)
   local success = false
   repeat
      success = player:acquire_training_wheel_lock(training_wheel_name)
      if not success then
         sleep(1000)
      end
   until success == true
   -- Safeguard against concurrent locking, just in case
   sleep(10)
   if not player:acquire_training_wheel_lock(training_wheel_name) then
      wait_for_lock(player, training_wheel_name)
   end
end

-- Strip directory and '.lua' file extension
function training_wheel_name_from_filename(filename)
   local basename = path.basename(filename)
   return basename:sub(0, #basename - 4)
end

-- Detect the interactive player if any
function get_interactive_player()
   local player_number = wl.Game().interactive_player
   for p_idx, player in ipairs(wl.Game().players) do
      if player.number == player_number then
         return player
      end
   end
   return nil
end