Advertisement
Guest User

WriteScript.lua

a guest
Oct 13th, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.94 KB | None | 0 0
  1.  
  2. local scriptfile=nil
  3. local tab_count=0
  4.  
  5. local function WriteString(str)
  6.     scriptfile:write(str)
  7. end
  8.  
  9. local function WriteEndLine()
  10.     return WriteString("\r\n")
  11. end
  12.  
  13. local function WriteTabbedLine(str)
  14.     for tabs=1,tab_count do
  15.         WriteString("\t")
  16.     end
  17.     WriteString(str)
  18.     WriteEndLine()
  19. end
  20.  
  21. local function WriteFieldName(str)
  22.     for tabs=1,tab_count do
  23.         WriteString("\t")
  24.     end
  25.     WriteString(str)
  26.     WriteString("=")
  27. end
  28.  
  29. local function WriteFieldValue(str)
  30.     WriteString(str)
  31.     WriteString(";")
  32.     WriteEndLine()
  33. end
  34.  
  35. local function WriteNumFieldValue(fieldvalue)
  36.     WriteFieldValue(tostring(fieldvalue))
  37. end
  38.  
  39. local function WriteField(fieldname,fieldvalue)
  40.     WriteFieldName(fieldname)
  41.     WriteFieldValue(fieldvalue)
  42. end
  43.  
  44. local function WriteNumField(fieldname,fieldvalue)
  45.     WriteFieldName(fieldname)
  46.     WriteNumFieldValue(fieldvalue)
  47. end
  48.  
  49. local function WriteFieldAndComment(fieldname,fieldvalue,comment)
  50.     WriteFieldName(fieldname)
  51.     WriteString(fieldvalue)
  52.     WriteString(";// ")
  53.     WriteString(comment)
  54.     WriteEndLine()
  55. end
  56.  
  57. local function WriteNumFieldAndComment(fieldname,fieldvalue,comment)
  58.     WriteFieldAndComment(fieldname,fieldvalue,comment)
  59. end
  60.  
  61. local function StartSubSection(str)
  62.     for tabs=1,tab_count do
  63.         WriteString("\t")
  64.     end
  65.     WriteString("[")
  66.     WriteString(str)
  67.     WriteString("]")
  68.     WriteEndLine()
  69.     WriteTabbedLine("{")
  70.     tab_count=tab_count+1
  71. end
  72.  
  73. local function EndSubSection()
  74.     tab_count=tab_count-1
  75.     WriteTabbedLine("}")
  76. end
  77.  
  78.  
  79. function WriteScript(GtW,ScriptFileName)
  80.  
  81.     scriptfile = io.open(ScriptFileName or ModSpecific.ScriptFileName,"wb")-- So the second argument of WriteScript(.,.) is optional
  82.  
  83.     tab_count=0
  84.  
  85.     WriteEndLine()
  86.     WriteTabbedLine("// To run this script, drag and drop over "..ModSpecific.ExecutableFileName)
  87.     WriteTabbedLine("//")
  88.     WriteTabbedLine("// Generated by zwzsg's Lua code from "..Game.modName..":")
  89.     WriteTabbedLine("// "..GtW.description)
  90.     WriteTabbedLine("// "..Game.modShortName.." script written by \""..(widget or gadget):GetInfo().name.."\" "..((widget and "widget") or (gadget and "gadget") or "lua").." version "..((widget or gadget):GetInfo().version or "?"))
  91.     WriteEndLine()
  92.  
  93.     StartSubSection("GAME")
  94.  
  95.     WriteField("GameType",ModSpecific.ModFileName)
  96.     WriteField("Mapname",GtW.map.InternalFileName)
  97.     WriteEndLine()
  98.  
  99.     -- List of modoptions
  100.     StartSubSection("MODOPTIONS")
  101.     WriteTabbedLine("// "..Game.modShortName.." specific:")
  102.     for key,value in pairs(GtW.ModOptions) do
  103.         local desc=nil
  104.         if ModSpecific and ModSpecific.MultiplayerModOptions and ModSpecific.MultiplayerModOptions then
  105.             for _,k in pairs(ModSpecific.MultiplayerModOptions) do
  106.                 if k.key==key and k.desc then
  107.                     desc=k.desc
  108.                 end
  109.             end
  110.         end
  111.         if desc then
  112.             WriteFieldAndComment(key,value,desc)
  113.         else
  114.             WriteField(key,value)
  115.         end
  116.     end
  117.     if GtW.ModOptions.missionscript then
  118.         WriteField("MissionScript",GtW.ModOptions.missionscript)
  119.     end
  120.     WriteEndLine()
  121.     WriteTabbedLine("// Generic, may not apply to "..Game.modShortName..":")
  122.     WriteNumField("GameMode",GtW.EndCondition)
  123.     WriteTabbedLine("\t// 0 for \"Kill everything!\"")
  124.     WriteTabbedLine("\t// 1 for \"Kill all commanding units!\"")
  125.     WriteTabbedLine("\t// 2 for \"Kill the first unit!\"")
  126.     WriteTabbedLine("\t// 3 for \"Never ends!\"")
  127.     WriteField("ghostedbuildings","1")
  128.     WriteField("fixedallies","0")
  129.     WriteField("MaxUnits","512")
  130.     WriteField("MinSpeed","0.1")
  131.     WriteField("MaxSpeed","10")
  132.     WriteField("StartMetal","1024")
  133.     WriteField("StartEnergy","1024")
  134.     --WriteField("LimitDGun","0")
  135.     --WriteField("DiminishingMMs","0")
  136.     WriteField("LauncherName",(widget or gadget):GetInfo().name.." "..((widget and "widget") or (gadget and "gadget") or "lua"))
  137.     WriteField("LauncherVersion",(widget or gadget):GetInfo().version or "?")
  138.     WriteField("DateTime",os.date("%A %d %B %Y at %X"))
  139.    
  140.     EndSubSection()
  141.     WriteEndLine()
  142.  
  143.     if GtW.MapOptions then
  144.         StartSubSection("MAPOPTIONS")
  145.         for key,value in pairs(GtW.MapOptions) do
  146.             WriteField(key,value)
  147.         end
  148.         EndSubSection()
  149.         WriteEndLine()
  150.     end
  151.  
  152.     WriteField("HostIP","localhost")
  153.     WriteField("HostPort","0")
  154.     WriteField("IsHost","1")
  155.     WriteEndLine()
  156.     WriteFieldAndComment("StartPosType",(GtW.StartPosType or 0)>1 and "3" or "0","0 for fixed, 1 for random, 2 for chosen ingame, 3 for chosen out of game")
  157.     WriteField("MyPlayerNum","0")
  158.     WriteField("MyPlayerName",GtW.PlayerName)
  159.     WriteNumField("NumPlayers",1)
  160.     WriteNumField("NumUsers",#GtW.players)
  161.     WriteNumField("NumTeams",#GtW.players)
  162.     WriteField("NumAllyTeams","2")-- <- Uhoh, NOT!
  163.     WriteEndLine()
  164.  
  165.     WriteTabbedLine("// List of human controlled players:")
  166.     StartSubSection("PLAYER0")
  167.     WriteField("name",GtW.PlayerName)
  168.     WriteNumField("Team",(GtW.isSpeccing~=0 and 0 or GtW.PlayerTeamNum))
  169.     WriteNumField("Spectator",GtW.isSpeccing)
  170.     EndSubSection()
  171.     WriteEndLine()
  172.  
  173.     WriteTabbedLine("// List of AI controlled players:")
  174.     local friendcount,enemycout=0,0
  175.     for p,_ in ipairs(GtW.players) do
  176.         if p~=1+GtW.PlayerTeamNum or GtW.isSpeccing~=0 then
  177.             StartSubSection("AI"..(p-1))
  178.             local nick=GtW.players[p].aioverride or GtW.players[p].bot.ShortName
  179.             if GtW.isSpeccing~=0 then
  180.                 friendcount=friendcount+1
  181.                 enemycout=enemycout+1
  182.                 WriteField("Name",nick.." #"..friendcount)
  183.             elseif GtW.players[p].allyteam==GtW.players[1+GtW.PlayerTeamNum].allyteam then
  184.                 friendcount=friendcount+1
  185.                 WriteField("Name",nick.." (Friend #"..friendcount..")")
  186.             else
  187.                 enemycout=enemycout+1
  188.                 WriteField("Name",nick.." (Enemy #"..enemycout..")")
  189.             end
  190.             if GtW.players[p].aioverride then
  191.                 WriteNumFieldAndComment("ShortName",GtW.players[p].bot.ShortName,"But will be overriden to \""..GtW.players[p].aioverride.."\"")
  192.             else
  193.                 WriteNumField("ShortName",GtW.players[p].bot.ShortName)
  194.             end
  195.             WriteField("//Version","?")
  196.             WriteNumField("Team",p-1)
  197.             WriteNumFieldAndComment("Host",0,"Number of the PLAYER hosting the AI")
  198.             EndSubSection()
  199.         end
  200.     end
  201.     WriteEndLine()
  202.  
  203.     WriteTabbedLine("// List of \"teams\" (or \"players\" depending on terminology):")
  204.     for p,teaminfo in ipairs(GtW.players) do
  205.         StartSubSection("TEAM"..(p-1))
  206.         WriteField("TeamLeader","0")
  207.         WriteNumField("AllyTeam",teaminfo.allyteam)
  208.         WriteField("Handicap","0")
  209.         if teaminfo.noons then
  210.             WriteNumFieldAndComment("noons",1,"No ONS for this player")
  211.         end
  212.         if teaminfo.sos then
  213.             WriteNumFieldAndComment("sos",teaminfo.sos,"Save our Soul specific to this player")
  214.         end
  215.         WriteField("Side",teaminfo.faction.InternalName)
  216.         WriteFieldName("RGBColor")
  217.         -- sprintf(colorthing,"%.4f %.4f %.4f",(float)teaminfo->color[0]/255,(float)teaminfo->color[1]/255,(float)teaminfo->color[2]/255)
  218.         WriteFieldValue(teaminfo.color[1].." "..teaminfo.color[2].." "..teaminfo.color[3])
  219.         if p~=1+GtW.PlayerTeamNum or GtW.isSpeccing~=0 then
  220.             WriteField("AIDLL",teaminfo.bot.ShortName)
  221.         end
  222.         if teaminfo.aioverride then
  223.             WriteField("aioverride",teaminfo.aioverride)
  224.         end
  225.         if teaminfo.StartPosX and teaminfo.StartPosZ then
  226.             WriteNumField("StartPosX",teaminfo.StartPosX)
  227.             WriteNumField("StartPosZ",teaminfo.StartPosZ)
  228.         end
  229.         if GtW.players[p].ExtraDoubleCustomKeys then
  230.             for _,key in ipairs(GtW.players[p].ExtraDoubleCustomKeys) do
  231.                 WriteField(key.name,key.value)
  232.             end
  233.         end
  234.         EndSubSection()
  235.     end
  236.     WriteEndLine()
  237.  
  238.     WriteTabbedLine("// List of ally teams: This is for assymetrical alliances")
  239.     maxallyteam=GetMaxAllyTeam(GtW.players)
  240.     for at=0,maxallyteam do
  241.         local nbrallies=0
  242.         StartSubSection("ALLYTEAM"..at)
  243.         for p,_ in ipairs(GtW.players) do
  244.             if GtW.players[p].allyteam==at then
  245.                 nbrallies=nbrallies+1
  246.             end
  247.         end
  248.         WriteNumField("NumAllies",0*nbrallies)
  249.         EndSubSection()
  250.     end
  251.     WriteEndLine()
  252.  
  253.     WriteTabbedLine("// List of restrictions")
  254.     WriteNumField("NumRestrictions",0)
  255.     StartSubSection("RESTRICT")
  256.     EndSubSection()
  257.  
  258.     if GtW.HeightMapV1 then
  259.         WriteEndLine()
  260.         WriteTabbedLine("// Heightmap:")
  261.         WriteField("HeightMapV1",GtW.HeightMapV1)
  262.     end
  263.  
  264.     EndSubSection()
  265.  
  266.     scriptfile:flush()
  267.     scriptfile:close()
  268.  
  269. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement