File: explosions.lua

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (153 lines) | stat: -rwxr-xr-x 4,525 bytes parent folder | download
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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    explosions.lua
--  brief:   explosions/*.tdf parser
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local explosionDefs = {}

local shared = {} -- shared amongst the lua explosiondef enviroments

local preProcFile  = 'gamedata/explosions_pre.lua'
local postProcFile = 'gamedata/explosions_post.lua'

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

local system = VFS.Include('gamedata/system.lua')


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Run a pre-processing script if one exists
--

if (VFS.FileExists(preProcFile)) then
  Shared = shared  -- make it global
  ExplosionDefs = explosionDefs  -- make it global
  VFS.Include(preProcFile)
  ExplosionDefs = nil
  Shared = nil
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Load the TDF explosiondef files
--

local function ParseColorString(str)
  local color = { 1.0, 1.0, 0.8 }
  local i = 1
  for word in string.gmatch(str, '[^,]+') do
    local val = tonumber(word)
    if (val) then
      color[i] = val
    end
    i = i + 1
    if (i > 3) then
      break
    end
  end
  return color
end


local function FixGroundFlashColor(ed)
  for spawnName, groundFlash  in pairs(ed) do
    if ((spawnName == 'groundflash') and (type(groundFlash) == 'table')) then
      local colorStr = groundFlash.color
      if (type(colorStr) == 'string') then
        groundFlash.color = ParseColorString(colorStr)
      end
    end
  end
end

local function LoadTDFs(dir)
  local tdfFiles = VFS.DirList(dir, '*.tdf')

  for _, filename in ipairs(tdfFiles) do
    local eds, err = TDF.Parse(filename)
    if (eds == nil) then
      Spring.Echo('Error parsing ' .. filename .. ': ' .. err)
    else
      for name, ed in pairs(eds) do
        ed.filename = filename
        explosionDefs[name] = ed
        FixGroundFlashColor(ed)
      end
    end
  end
 end


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


local function LoadLuas(dir)
  local luaFiles = VFS.DirList(dir, '*.lua')

  for _, filename in ipairs(luaFiles) do
    local edEnv = {}
    edEnv._G = edEnv
    edEnv.Shared = shared
    edEnv.GetFilename = function() return filename end
    setmetatable(edEnv, { __index = system })
    local success, eds = pcall(VFS.Include, filename, edEnv)
    if (not success) then
      Spring.Echo('Error parsing ' .. filename .. ': ' .. eds)
    elseif (eds == nil) then
      Spring.Echo('Missing return table from: ' .. filename)
    else
      for edName, ed in pairs(eds) do
        if ((type(edName) == 'string') and (type(ed) == 'table')) then
          ed.filename = filename
          explosionDefs[edName] = ed
		end
      end
    end
  end  
end

--  Load the TDF format explosiondef files
--  Files in effects/ will override those in gamedata/explosions/
LoadTDFs('gamedata/explosions/')
LoadTDFs('effects/')
--  Load the raw LUA format explosiondef files
--  (these will override the TDF versions)
--  Files in effects/ will override those in gamedata/explosions/
LoadLuas('gamedata/explosions/')
LoadLuas('effects/')

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Run a post-processing script if one exists
--

if (VFS.FileExists(postProcFile)) then
  Shared = shared  -- make it global
  ExplosionDefs = explosionDefs  -- make it global
  VFS.Include(postProcFile)
  ExplosionDefs = nil
  Shared = nil
end


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

return explosionDefs

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