function gadget:GetInfo()
  return {
    name      = "World Builder Editor",
    desc      = "Allows World Builder to save and erase Units and Features",
    author    = "Argh",
    date      = "September 30th, 2008",
    license   = "GNU GPL, v2",
    layer     = 0,
    enabled   = true
  }
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 CMD_ERASE_ALL = 32701
local CMD_CONIFEROUS_TREES = 32702
local CMD_DECIDUOUS_TREES = 32703
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
if (gadgetHandler:IsSyncedCode()) then
-----------------------------------------------SYNCHED
----------------------------------------------------------------
------------------------------------------------------------------------
function GameStart()
	local x,y,z = Spring.GetTeamStartPosition(0)
	Spring.CreateUnit('world_builder_editor_daemon',x,y,z,0,0)
	Spring.SendCommands({"cheat"})
	Spring.SendCommands({"nocost"})
	Spring.SendCommands({"resbar 0"})
end

SaveAll = {
		id=CMD_SAVE_ALL,
		type=CMDTYPE.ICON,
		name="Save World",
		texture="&.9x.9&bitmaps/icons/blank.tif&bitmaps/icons/group_select.tif",
		tooltip="Allows the editor to save all of the objects into a World Builder template.\r\nWarning:  this may take several seconds, do not interrupt.",
		action="Write world contents.",
		}

EraseAll = {
		id=CMD_ERASE_ALL,
		type=CMDTYPE.ICON,
		name="Erase World",
		texture="&.9x.9&bitmaps/icons/blank.tif&bitmaps/icons/transform.tif",
		tooltip="Erases all Units and Features in the world.\r\nWarning:  this may take several seconds, do not interrupt.",
		action="Erase world contents.",
		}

DeciduousTrees = {
		id=CMD_DECIDUOUS_TREES,
		type=CMDTYPE.ICON_MAP,
		name="Deciduous Tree",
		texture="&.9x.9&bitmaps/icons/blank.tif&bitmaps/icons/group_select.tif",
		tooltip="Click on the ground.\r\n\Makes a random deciduous tree.",
		action="Deciduous Tree.",
		}

ConiferousTrees = {
		id=CMD_CONIFEROUS_TREES,
		type=CMDTYPE.ICON_MAP,
		name="Coniferous Tree",
		texture="&.9x.9&bitmaps/icons/blank.tif&bitmaps/icons/group_select.tif",
		tooltip="Click on the ground.\r\n\Makes a random deciduous tree.",
		action="Coniferous Tree.",
		}

function gadget:Initialize()
gadgetHandler:RegisterGlobal("WriteScript", WriteScript)
Spring.Echo("testing")
end

function gadget:UnitCreated(u, ud, team)
	if Editor[Spring.GetUnitDefID(u)] then
		Spring.InsertUnitCmdDesc(u, SaveAll)
		Spring.InsertUnitCmdDesc(u, EraseAll)
		Spring.InsertUnitCmdDesc(u, DeciduousTrees)
		Spring.InsertUnitCmdDesc(u, ConiferousTrees)
	end
end

function gadget:CommandFallback(u, ud, team, cmd, param, opts)

--Important Note:
--CMD_SAVE_ALL activates a Widget via CommandNotify, see WriteScript.lua, apparently Gadgets may not use io.open etc., but Widgets may do so at this time
--This is why CMD_SAVE_ALL does not appear here.

	if cmd  == CMD_ERASE_ALL then
		for _,f in ipairs(Spring.GetAllFeatures()) do
			Spring.DestroyFeature(f)
		end
		for _,f in ipairs(Spring.GetAllUnits()) do
			if Editor[Spring.GetUnitDefID(f)] then
			--Do nothing, this is our editor, duh
			else
				Spring.DestroyUnit(f,true,false)
			end
		end
	return true, true;
	end

	if cmd  == CMD_CONIFEROUS_TREES then
		local CenterX,CenterY,CenterZ = param[1], param[2], param[3]
		local RandomFacing = math.random(0,3)
		local RandomTree = math.random (1,7)
		Spring.CreateFeature("treetype"..RandomTree, CenterX,CenterY,CenterZ, RandomFacing,0)
	return true, true;
	end

	if cmd  == CMD_DECIDUOUS_TREES then
		local CenterX,CenterY,CenterZ = param[1], param[2], param[3]
		local RandomFacing = math.random(0,3)
		local RandomTree = math.random (8,14)
		Spring.CreateFeature("treetype"..RandomTree, CenterX,CenterY,CenterZ, RandomFacing,0)
	return true, true;
	end

	return false
end

--////////////////////////////////////////////////////////////////////
local function copyCommandQueue(targetID, unitID)

  local states = Spring.GetUnitStates(targetID)

  if (states ~= nil) then
    Spring.GiveOrderToUnit(unitID, CMD.FIRE_STATE, { states.firestate }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.MOVE_STATE, { states.movestate }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.REPEAT,     { states['repeat']  and 1 or 0 }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.ONOFF,      { states.active     and 1 or 0 }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.CLOAK,      { states.cloak      and 1 or 0 }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.TRAJECTORY, { states.trajectory and 1 or 0 }, 0)
  end
 
  local queue = Spring.GetCommandQueue(targetID);
 
  if (queue ~= nil) then

    for k,v in ipairs(queue) do  --  in order
      local opts = v.options
      if (not opts.internal) then
        local newopts = {}
        if (opts.alt)   then table.insert(newopts, "alt")   end
        if (opts.ctrl)  then table.insert(newopts, "ctrl")  end
        if (opts.right) then table.insert(newopts, "right") end
        table.insert(newopts, "shift")
        Spring.GiveOrderToUnit(unitID, v.id, v.params, opts.coded)
      end
    end
  end

  local buildqueue = Spring.GetRealBuildQueue(targetID)
  if (buildqueue ~= nil) then
    for udid,buildPair in ipairs(buildqueue) do
      local udid, count = next(buildPair, nil)
      Spring.AddBuildOrders(unitID, udid, count)
    end
  end
 
end
--////////////////////////////////////////////////////////////////////

function gadget:GameFrame(f)
	if f == 20 then
		local _,_,_,team = Spring.GetPlayerInfo(0)
		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
				Spring.SetUnitNoSelect(u,false)
				local queue = Spring.GetCommandQueue(u);
 				if (queue[1] ~= nil) then
					local x,y,z = Spring.GetUnitPosition(u)
					local id = Spring.GetUnitDefID(u)

					local heading = Spring.GetUnitHeading(u)
					local saveheading = 0
					if heading == 16384 then heading = 1 end
					if heading == 32767 then heading = 2 end
					if heading == -16384 then heading = 3 end

					saveheading = heading

					if heading ~= 0 and heading ~= 16384 and heading ~= 32767 and heading ~= -16384 then
						heading = 0
					end

					local me = Spring.CreateUnit(id,x,y,z,heading,team)
					copyCommandQueue(u,me)
					Spring.MoveCtrl.Enable(me)
					Spring.MoveCtrl.SetRotation(me,0,saveheading,0)
					Spring.MoveCtrl.Disable(me)

					Spring.DestroyUnit(u,false,true)
				else
					Spring.TransferUnit(u,0,true)
				end
			end
		end
		for _,u in ipairs(Spring.GetAllFeatures()) do
			Spring.TransferFeature(u,0,true)
		end
	end
end
---------------------------------------------------------------------------------------------------------------END
end