File: parse_fbi.lua

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (240 lines) | stat: -rw-r--r-- 6,367 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    parse_fbi.lua
--  brief:   unitdef FBI parser
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

if (FBIparser) then
  return FBIparser
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local TDF = TDFparser or VFS.Include('gamedata/parse_tdf.lua')
local SND = SNDparser or VFS.Include('gamedata/parse_snd.lua')


--------------------------------------------------------------------------------

local soundTypes = {}


local soundTypes, err = SND.Parse('gamedata/sound.tdf')
if (soundTypes == nil) then
  if (false) then -- sound.tdf is no longer required, do not complain
    Spring.Echo('could not load sound data: ' .. err)
  end
  soundTypes = {}
end


--------------------------------------------------------------------------------

local buildOptions = {}


local buildOptions, err = TDF.Parse('gamedata/sidedata.tdf')
buildOptions = buildOptions or {}
buildOptions = buildOptions['canbuild']
buildOptions = buildOptions or {}

do
  local newBuildOptions = {}

  local function AddBuildOptions(unitName, buildTable)
    local sorted = {}
    for buildID, buildName in pairs(buildTable) do
      local idstr = string.lower(buildID)
      local s, e, idnum = string.find(idstr, 'canbuild(%d+)')
      idnum = tonumber(idnum)
      if (idnum) then
        table.insert(sorted, { idnum, string.lower(buildName) })
      end
    end
    table.sort(sorted, function(a, b) return a[1] < b[1] end)
    for i, buildName in ipairs(sorted) do
      sorted[i] = sorted[i][2]
    end
    newBuildOptions[unitName] = sorted
  end

  for unitName, buildTable in pairs(buildOptions) do
    AddBuildOptions(string.lower(unitName), buildTable)
  end

  buildOptions = newBuildOptions
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local function SetupWeapons(tdf)
  local weapons = {}

  for w = 1, 32 do
    local name = tdf['weapon' .. w]
    if (name == nil) then
      if (w > 3) then
        break
      end
    else
      local weapon = {}

      weapon.name = name

      local btc = tdf['badtargetcategory'  .. w]
      if (w == 1) then
        btc = tdf['wpri_badtargetcategory'] or btc
      elseif (w == 2) then
        btc = tdf['wsec_badtargetcategory'] or btc
      elseif (w == 3) then
        btc = tdf['wspe_badtargetcategory'] or btc
      end
      weapon.badtargetcategory = btc

      weapon.onlytargetcategory = tdf['onlytargetcategory' .. w]

      weapon.slaveto     = tdf['weaponslaveto'   .. w]
      weapon.maindir     = tdf['weaponmaindir'   .. w]
      weapon.fuelusage   = tdf['weaponfuelusage' .. w]
      weapon.maxangledif = tdf['maxangledif'     .. w]

      -- clear the old style parameters
      tdf['weapon' .. w]             = nil
      tdf['badtargetcategory'  .. w] = nil
      tdf['wpri_badtargetcategory']  = nil
      tdf['wsec_badtargetcategory']  = nil
      tdf['wspe_badtargetcategory']  = nil
      tdf['onlytargetcategory' .. w] = nil
      tdf['weaponslaveto'   .. w]    = nil
      tdf['weaponmaindir'   .. w]    = nil
      tdf['weaponfuelusage' .. w]    = nil
      tdf['maxangledif'     .. w]    = nil

      weapons[w] = weapon
    end
  end

  if (next(weapons)) then
    tdf.weapons = weapons
  end

  return tdf
end


--------------------------------------------------------------------------------

local function SetupSounds(tdf, soundTypes)
  local soundCat = tdf.soundcategory
  if (soundCat) then
    soundCat = string.lower(soundCat)
    local soundTable = soundTypes[soundCat]
    if (soundTable) then
      tdf.sounds = soundTable
    end
    tdf.soundcategory = nil -- clear the old style parameter
  end
  return tdf
end


--------------------------------------------------------------------------------

local function SetupSpecialEffects(tdf)
  local sfx = tdf.sfxtypes
  if (type(sfx) ~= 'table') then
    return tdf
  end

  local exps = {}
  local e = 0
  while (true) do
    local name = sfx['explosiongenerator' .. e]
    sfx['explosiongenerator' .. e] = nil -- clear the old style parameter
    if (name) then
      e = e + 1
      exps[e] = name
    else
      break
    end
  end

  sfx.explosiongenerators = exps

  return tdf
end


--------------------------------------------------------------------------------

local function SetupBuildOptions(tdf)
  if (tdf.unitname == nil) then
    return
  end
  if (tdf.buildOptions) then
    return
  end
  local name = string.lower(tdf.unitname)
  tdf.buildoptions = buildOptions[name]
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local function ParseFBI(filename)
  local tdf, err = TDF.Parse(filename)
  if (tdf == nil) then
    return nil, err
  end
  if (tdf.unitinfo == nil) then
    return nil, "no [UNITINFO] table"
  end
  tdf = tdf.unitinfo

  -- override the unitname, 0.75b2 expects it to be filename derived
  local lowername = string.lower(filename)
  local s, e, basename = string.find(lowername, '([^\\/]+)%....$')
  tdf.unitname = basename

  -- backwards compatible hack for non-standard parameter naming
  if (tdf.init_cloaked ~= nil) then
    if (tdf.initcloaked == nil) then
      tdf.initcloaked = tdf.init_cloaked
    end
    tdf.init_cloaked = nil
  end
  
  SetupWeapons(tdf)

  SetupSounds(tdf, soundTypes)

  SetupSpecialEffects(tdf)

  SetupBuildOptions(tdf)

  return tdf
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

FBIparser = {
  Parse = ParseFBI
}

return FBIparser

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------