File: generic.lua

package info (click to toggle)
naev 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 386,084 kB
  • sloc: ansic: 93,149; xml: 87,292; python: 2,347; sh: 904; makefile: 654; lisp: 162; awk: 4
file content (66 lines) | stat: -rw-r--r-- 1,641 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
--[[
-- @brief Adds an outfit from a list of outfits to the pilot.
--
-- Outfits parameter should be something like:
--
-- outfits = {
--    "Laser Cannon",
--    { "Unicorp Fury Launcher", { "Fury Missile", 10 } }
-- }
-- 
--    @param p Pilot to add outfits to.
--    @param o Table to add what was added.
--    @param outfits Table of outfits to add.  Look at description for special formatting.
--]]
function pilot_outfitAdd( p, o, outfits )
   local r = rnd.rnd(1, #outfits)
   if type(outfits[r]) == "string" then
      _insertOutfit( p, o, outfits[r], 1 )
   elseif type(outfits[r]) == "table" then
      _insertOutfit( p, o, outfits[r][2][1], outfits[r][2][2] )
      _insertOutfit( p, o, outfits[r][1], 1 ) -- Add launcher later
   end
end
function _insertOutfit( p, o, name, quantity )
   local q = 0
   -- If pilot actually exists, add outfit
   if type(p) == "userdata" then
      local i = quantity
      while i > 0 do
         local added = p:addOutfit( name )
         if not added then
            break
         end
         i = i - 1
         q = q + 1
      end
   else
      q = quantity
   end

   -- Must have quantity
   if q > 0 then
      return
   end

   -- Try to find in table first
   for k,v in ipairs(o) do
      if v[1] == name then
         v[2] = v[2] + q
         return
      end
   end
   -- Insert
   table.insert( o, { name, q } )
end
--[[
-- @brief Adds a set of outfit as generated by pilot_outfitAdd to a pilot.
--
--    @param p Pilot to add set to.
--    @param set Set of outfits to add.
--]]
function pilot_outfitAddSet( p, set )
   for k,v in ipairs(set) do
      p:addOutfit( v )
   end
end