Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
summerock

Drone Harvesting program request

Question

Hi, i play on OhGamings organised crime 4 which has opencomputers. I would like a program for a drone to harvest a cocaine plant, which is 3 blocks tall, and i would want it to harvest it from the 2nd block and then have it collect the items and put it in a chest or something. But i only want it to break the 2nd block of the plant when it reaches atleast 3 blocks tall, i have no coding experience and i have no clue how to code. I do not want to code or learn, ive tried and failed. This is just a request, but if you can i will love you forever. Thanks

Link to post
Share on other sites

24 answers to this question

Recommended Posts

  • 0

This was a fun challenge, tried to make it simple, with almost no need to change the code while also being complex enough to expand into big farms. I made this with sugarcane in mind because I am pretty sure they work exactly the same.


local WAIT_TIME = 60 -- time to wait between cycles

local drone = component.proxy(component.list("drone")())
local nav = component.proxy(component.list("navigation")())

local vector
vector = {
	__add = function(vec1,vec2)
		local vec3 = vector {}
		for i = 1,#vec1  do
			vec3[i] = vec1[i] + vec2[i]
		end
		return vec3
	end,
	__sub = function(vec1,vec2)
		local vec3 = vector {}
		for i = 1,#vec1  do
			vec3[i] = vec1[i] - vec2[i]
		end
		return vec3
	end,
	__unm = function(vec1)
		local vec2 = vector {}
		for i = 1,#vec1  do
			vec2[i] = vec1[i]
		end
		return vec2
	end,
	unpack = function(tbl)
		return table.unpack(tbl)
	end
}
vector.__index = vector

setmetatable(vector,
	{
		__call = function(mt,arg1,...)
			return setmetatable(type(arg1) == "table" and arg1 or {arg1,...},vector)
		end
	}
)
local plants = {}
local charger = vector {0,0,0}
local chest = charger

do
	local list = nav.findWaypoints(16) -- Change to increase waypoint search radius, probably uses more power idk
	
	for i = 1,#list do
		local wp = list[i]
		local direction,length = wp.label:match("^plant:([2-5]):([0-9]+)$") 
		if direction then
			direction = tonumber(direction)
			local axis,negate = direction > 3 and 1 or 3, (direction % 2)*2 - 1
			for i = 0,negate*(length-1),negate do
				local pos = {table.unpack(wp.position)}
				pos[axis] = pos[axis] + i
				pos[2] = pos[2] + 3 -- Crop height
				plants[#plants+1] = vector(pos)
			end
		end
		if wp.label:find"chest" then
			chest = vector(wp.position)
		end
		if wp.label:find"charger" then
			WAIT_TIME = tonumber(wp.label:match("%[([0-9]+)%]")) or WAIT_TIME -- if charger waypoint label contains [number] the waittime will become that number
			charger = vector(wp.position)
		end
	end
end

local pos = vector {0,0,0}

local function move(vec,halt)

	drone.move(vec:unpack())
	
	if(halt) then
		local speed = drone.getVelocity()
		while(true) do
			computer.pullSignal(0.1)
			local tmpspeed = drone.getVelocity() 
			if tmpspeed < speed and tmpspeed < 2 then break end
			speed = tmpspeed
		end
		computer.pullSignal(0.5)
	end
	
	pos = pos + vec
end

local invSize = drone.inventorySize()
local currSlot = 1

while true do
	for i = 1,#plants do
		move(plants[i] - pos,true)
		if drone.detect(0) then 
			drone.swing(0)
			move(vector{0,-1,0},true)
			drone.swing(0)
		end
		
		if drone.count(currSlot) == 64 then
			currSlot = currSlot + 1
			if currSlot > invSize then
				move(chest - pos,true)
				for i = 1,invSize do
					drone.select(i)
					drone.drop(0)
				end
				currSlot = 1
			end
		end
		
	end
	move(chest - pos,true)
	for i = 1,currSlot do
		drone.select(i)
		drone.drop(0)
	end
	currSlot = 1
	move(charger - pos)
	computer.pullSignal(WAIT_TIME)
end

 

If you want to set up a farm you need atleast these two things:

  1. A drone with a navigation upgrade and atleast one inventory upgrade.
  2. A waypoint block to mark out where the crops are.

The waypoint labels are what configure the drone, when it starts up it will search for all waypoints in a 16 block radius. There are three tags for the waypoint labels: plant, chest and charger. chest and/or charger are not required if your drone starts up above the chest and/or beside a charging face of the charger, look at the first attached image as an example. If you don't have the drone spawning on one of them you will need a waypoint to mark out where it is, using the label chest or charger. The plant waypoint is a bit more complicated, it uses a format of:

plant:direction:length

Where direction is the sides index that the line of crops extends and length is the number of crops in that direction. Use the sides api to get the correct direction. As an example my farm had 3 sugarcane crops extending south/positive z direction. So my label, was plant:3:3. You can see in the second picture what that looked like.

Hope this works, I actually never knew drones could break/place blocks until now.

2017-02-25_18.18.13.png

2017-02-25_18.32.41.png

Link to post
Share on other sites
  • 0

You need to put the code into an eeprom and then the eeprom into the drone, either when your assembling it, or after by putting the drone and the eeprom into a crafting bench.

To put the code into the eeprom, start up a computer as normal, when it has fully loaded, swap out the lua eeprom for a blank one. Then type "edit /dev/eeprom" and copy paste the code into the editor via the Insert key. Then save the code and exit, the eeprom will now have the code. Craft it with the drone to insert it.

Link to post
Share on other sites
  • 0

Updated original code so it will stop to empty the inventory if inventory is full then continue where it left off. It also only ever dropped off the first slot so that would obviously be a big issue in a farm with over 32 plant crops, and 43x43 is alot bigger than that.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.