function widget:GetInfo()
	return {
	name	= "Write World Builder Code",
	desc	= "Writes World Builder code automatically.",
	author	= "Argh",
	date	= "October 1st, 2008",
	license = "(C) Wolfe Games, 2008",
	layer	= 0,
	enabled = true	--	loaded by default?
	}
end
local Editor = {
	[UnitDefNames.world_builder_editor_daemon.id] = "world_builder_editor_daemon",
	}
local Blocker = {
	[UnitDefNames.blockernull.id] = "blockernull",
	}
local CMD_SAVE_ALL = 32700
local f
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function copyCommandQueue(u,x,y,z)

	--[[local states = Spring.GetUnitStates(u)
	local state
	if (states ~= nil) then
		state = states.firestate
		f:write('GiveOrderToUnit(u, CMD.FIRE_STATE,{'..state..'}, 0)\n')
		state = states.firestate
		f:write('GiveOrderToUnit(u, CMD.MOVE_STATE,{'..state..'}, 0)\n')
		state = states["repeat"] and 1 or 0
		f:write('GiveOrderToUnit(u, CMD.REPEAT,{'..state..'}, 0)\n')
		state = states.active and 1 or 0
    		f:write('GiveOrderToUnit(u, CMD.ONOFF,{'..state..'}, 0)\n')
		state = states.cloak and 1 or 0
		f:write('GiveOrderToUnit(u, CMD.CLOAK,{'..state..'}, 0)\n')
		state = states.trajectory and 1 or 0
		f:write('GiveOrderToUnit(u, CMD.TRAJECTORY,{'..state..'}, 0)\n')
  	end]]
 
local queue = Spring.GetCommandQueue(u);
 
	if (queue ~= nil) then
		local length = # queue
		for k,v in ipairs(queue) do  --  in order
			local opts = v.options
			if (not opts.internal) then
				if k == 1 and v.id == 5 then
					-- don't write a Wait that's the first order
				else
					f:write('GiveOrderToUnit(u,'..v.id..',{')
					for i,k in ipairs(v.params) do
						f:write(k..',')
					end
	
					f:write('},'..opts.coded..')\n')
				end
			end
		end
	end

	local buildqueue = Spring.GetRealBuildQueue(u)
	if (buildqueue ~= nil) then
		for udid,buildPair in ipairs(buildqueue) do
			local udid, count = next(buildPair, nil)
			f:write('Spring.AddBuildOrders(u,'..udid..','..count..')\n')
		end
  	end 
end


function widget:CommandNotify(name)
	if (name == CMD_SAVE_ALL) then 
		f = io.open('00001World_Builder.lua', 'w+')
		if (f) then
			f:write('function gadget:GetInfo()\n')
			f:write('	return {\n')
			f:write('		name = "World Builder (Automated)",\n')
			f:write('		desc = "Automated World Builder Logic, Created with Level Editor Daemon.",\n')
			f:write('		author = "Argh",\n')
			f:write('		date = "September 1st, 2008",\n')
			f:write('		license = "GPL, v.2, 2008",\n')
			f:write('		layer = 1,\n')
			f:write('		enabled = true,\n')
			f:write('	}\n')
			f:write('end\n')
			f:write('\n\n')
			f:write('local u\n')
			f:write('local CreateUnit = Spring.CreateUnit\n')
			f:write('local GetGaiaTeamID = Spring.GetGaiaTeamID\n')
			f:write('local GiveOrderToUnit = Spring.GiveOrderToUnit\n')
			f:write('local AddBuildOrders = Spring.AddBuildOrders\n')
			f:write('local CreateFeature = Spring.CreateFeature\n')
			f:write('\n\n')
			f:write('function GameStart()\n')
-----------------------------------------------------------------------------------------------------------------Start Feature Writing
			local myFeatures = Spring.GetAllFeatures()
			--Spring.Echo(myFeatures[1])
			if myFeatures[1] ~= nil then
				for _,u in ipairs(myFeatures) do
					local ud = Spring.GetFeatureDefID(u)
					Spring.Echo("Found Feature",u,FeatureDefs[ud].name)
					local x,y,z = Spring.GetFeaturePosition(u)
					local heading = Spring.GetFeatureHeading(u)
					if heading == 16384 then heading = 1 end
					if heading == 32767 then heading = 2 end
					if heading == -16384 then heading = 3 end
					local Featurename = FeatureDefs[ud].name
					f:write('Spring.CreateFeature("'..Featurename..'",'..math.floor(x)..','..math.floor(y)..','..math.floor(z)..','..heading..')\n')
				end
			end
-----------------------------------------------------------------------------------------------------------------End Feature Writing
-----------------------------------------------------------------------------------------------------------------Start Unit Writing
			for _,u in ipairs(Spring.GetAllUnits()) do
				if Editor[Spring.GetUnitDefID(u)] or Blocker[Spring.GetUnitDefID(u)] then
				--Do nothing, this is our editor or a Blocker Null, duh
				else
					local ud = Spring.GetUnitDefID(u)
					local x,y,z = Spring.GetUnitBasePosition(u)
					local heading = Spring.GetUnitHeading(u)
					local saveheading = heading
					if heading == 16384 then heading = 1 end
					if heading == 32767 then heading = 2 end
					if heading == -16384 then heading = 3 end

					if heading ~= 1 and heading ~= 2 and heading ~= 3 then
						heading = 0
					end

					local Unitname = UnitDefs[ud].name
					f:write('u = CreateUnit("'..Unitname..'",'..x..','..y..','..z..','..heading..',GetGaiaTeamID())\n')
					if UnitDefs[ud].canMove or UnitDefs[ud].canFly then
						if saveheading ~= 0 and saveheading ~= 16384 and saveheading ~= 32767 and saveheading ~=  -16384 then
						f:write('Spring.MoveCtrl.Enable(u)\nSpring.MoveCtrl.SetRotation(u,0,'..saveheading..',0)\nSpring.MoveCtrl.Disable(u)\n')
						end
					end
					local myqueue = Spring.GetCommandQueue(u);
					if myqueue ~= nil then
						copyCommandQueue(u,x,y,z)
					end
				end
			end
-----------------------------------------------------------------------------------------------------------------End Unit Writing

			f:write('gadgetHandler.RemoveGadget("World _Builder (Automated)")\n')
			f:write('end\n')

			f:close()
			Spring.Echo("World_Builder File has been saved to gameroot as 00001World_Builder.lua")
		end
	end
end