Page 2 of 2

Re: autoharvest gadget

Posted: 09 Dec 2009, 23:07
by thedude
Now the gadget seems to do nothing. When the "initialize code" was on unsync side the gadget printed at least the Spring.Echo things to the console. Have I somehow to tell Spring that I made a new command? Like adding the name of my caommand somewhere?
here is the new code:

Code: Select all

function gadget:GetInfo()

	return {
		name = "bla",
		desc = "blabla",
		author = "blablabla",
		date = "bla",
		license = "bla",
		layer = 0,
		enabled = true
	}

end


local CMD_MOVETO=30002


if (gadgetHandler:IsSyncedCode()) then


--SYNCED


local metalplat={}

local movetoCmd={

	name="moveto",
	tooltip="movesunitto",
	action="move",
	id=CMD_MOVETO,
	type=CMDTYPE.ICON , --CMDTYPE.ICON_MAP,
	cursor="Move",

}


function gadget:AllowCommand(u, ud, team, cmd, param, opt)

	if cmd == CMD_MOVETO then
		Spring.GiveOrderToUnit(u,CMD.MOVE,metalplat[1].pos,0);

		--return false

	end

	return true

end


function gadget:UnitCreated(u, ud, team)

	if UnitDefs[ud].customParams.canharvest=="1" then
	   Spring.Echo("harvestunit created");
	   Spring.GiveOrderToUnit(u,CMD.INSERT,{-1,CMD_MOVETO},{"alt"});

	   --Spring.GiveOrderToUnit(u,ud,team,CMD_MOVETO,0);

	end

end



function gadget:UnitFinished(u,ud,team)

	if ud == RUSK31 then
	Spring.Echo("harvestunit finished");
	Spring.GiveOrderToUnit(u,CMD.MOVETO);

	end

end


function gadget.Initialize()
	
	local i = 1

	for _,a in ipairs(Spring.GetAllFeatures()) do

		if Spring.GetFeatureDefID(a) == metal then
			local x,y,z=Spring.GetFeaturePosition(a)
			metalplat[i]={pos={x,y,z}}
			i=i+1
		end

	end

	if i==1 then
		Spring.Echo("removed")
		gadgetHandler:RemoveGadget()

	end

end	



else


--UNSYNCED


end


Re: autoharvest gadget

Posted: 10 Dec 2009, 00:21
by Google_Frog

Code: Select all

function gadget:UnitCreated(unitID, unitDefID)
	local ud = UnitDefs[unitDefID]
	if ud should have command then
     spInsertUnitCmdDesc(unitID, CmdDesc)
   end
end
This will make the command accessible to players on the console.

Code: Select all

function gadget:Initialize()
	gadgetHandler:RegisterCMDID(CMD_ID)
end
You may have to register the command if it is not going to be attached to a unit.

It seems a bit strange that you've got your own moveto command. Couldn't you just use the move command directly?

Re: autoharvest gadget

Posted: 10 Dec 2009, 18:46
by thedude
It seems a bit strange that you've got your own moveto command. Couldn't you just use the move command directly?
It's like the "step 0" of the gadget I'd like to make. I think one could use it directly it's more a placeholder than a real command. When I manage to give this command to units I can try to give them other commands. (Just an attempt to implement a new command which works :-) )

Re: autoharvest gadget

Posted: 13 Dec 2009, 19:47
by thedude
Is it not allowed to use existing commands in other commands?
Is it possible to give a command to a unit and when the unit reaches that place/unit it triggers an event (like telling "hey I reached the target")?

Re: autoharvest gadget

Posted: 22 Dec 2009, 15:31
by thedude
No video but code :wink: .
It is a long way to beeing perfect and has not all elements I'd like to have but I think this is the best I could do. It was very hard for me to code this but it seems to work. It may have bugs. It should work on maps with metal and geos. Don't build units too far from resourcepoints cause they will only search for resource nearby (you can edit the distance in the code).

Re: autoharvest gadget

Posted: 23 Dec 2009, 18:54
by thedude
version 2 (just replace the old one with this)

Re: autoharvest gadget

Posted: 26 Dec 2009, 16:24
by thedude
Does this work for someone cause when I uploaded the old one it did not have all textures so Spring just removed the models (with missing textures) from the game :?:

Re: autoharvest gadget

Posted: 26 Dec 2009, 19:14
by BrainDamage
thedude wrote:Does this work for someone cause when I uploaded the old one it did not have all textures so Spring just removed the models (with missing textures) from the game :?:
when I start it, I get a crane unit, and it's able to acquire the resources from the barrel-resources but no place is present where to deploy them, do have I to build it first? if yes, what is it's name?

Re: autoharvest gadget

Posted: 28 Dec 2009, 14:00
by thedude
When you look at the description of the crane there you should see something like ... for sweets that meens that this unit can only harvest the resource "sweets". You have to build "Barracks" so the crane can deploy it there (the description of Barracks should also contain something with sweets) hope this helps.