This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
drkmachine's profile picture

Building an arena.

Started by drkmachine, 27 February 2014 - 11:25 PM
drkmachine #1
Posted 28 February 2014 - 12:25 AM
I am relatively new to CC and coding, so perhaps this was not my wisest of choices for a project. At any rate, I am building an arena using spawners from Soul Shards (disabled with a constant redstone signal), a force field from MFFS (to lock both players and mobs in the arena), seer stones from ars magica (emits a redstone signal when mobs are present), extra peripherals( to connect to project red bundled wire).

here is the skinny, I have things set up so that people can enter the arena, press a button and the force field engages, then I am trying to set it up so that the computer will enable one spawner at a time. As each mob is killed it will move to the next one(9 spawners so far, possibly expanding later). Then once all spawners have been gone through, the field will go do down and allow the player to leave, then the system will reset for the next time.

here is the code I have so far, am I even close?

http://pastebin.com/Wpyr2xrp
(for some reason I can't use the code tags in the editor So I pastebin'd it.)

any assistance would be greatly appreciated.
Bomb Bloke #2
Posted 28 February 2014 - 02:31 AM
Sort of… not really? It looks promising on the surface, and you've certainly got some idea as to how to go about it, but even with the larger bugs ironed out of it I can't see it doing exactly what you want without a fair bit of a re-write.

If I've got this right, the goal is to have the computer wait for a redstone signal from its bottom, then enable the force field (by emitting a redstone signal from its top) and start cycling through the mob spawners (by sending signals to the left) based on seer stone input (from the back). If a second button (leading to the computer's right) is pressed during combat then the force field goes down and the mob spawner is stopped.

One issue here is that giving a signal to the mob spawner, to my understanding, will not simply cause it to "spawn a mob". Instead, it "activates" the spawner, which will create groups of mobs at somewhat random intervals in somewhat random amounts. So it'll take a while for the seer stone to activate after the mob spawner does, and once it DOES activate, it won't DEactivate until the player is able to clear the room… during which time the spawner is going to keep pumping mobs in there… Depending on the terrain and abilities of the player, this could lead to either very long or very short battles.

Seems to me you want to rig up a timer system - run the spawner for something like a minute, then wait for all the mobs to go away, then move to the next spawner.

Anyway, I've attempted such a re-write myself, but I've not tested it.

Spoiler
local p = peripheral.wrap("left")
local entColors = { colors.purple, colors.green, colors.brown, colors.blue, colors.red, colors.cyan, colors.lightgrey, colors.grey, colors.pink }

local function doCombat()
	-- Enable force field:
	rs.setOutput("top",true)
	
	-- Loop through each mob spawner:
	for i=1,#entColors do
		p.setBundledOutput(entColors[i])  -- Enable the current mob spawner.
		
		local myTimer = os.startTimer(60)  -- Rig a timer to expire a minute from now, save its ID.
		
		while true do
			local myEvent, par1 = os.pullEvent()  -- Wait for any event.
			
			if myEvent == "timer" and par1 == myTimer then  -- Spawner's been active for a minute.
				break   -- Exit the while loop.
			elseif myEvent == "redstone" and rs.getInput("right") then  -- Panic button's been pushed.
				return  -- Exit the function.
			end
		end
		
		p.setBundledOutput(0)  -- Turn the mob spawner off.

		if rs.getInput("back") then  -- If there are still mobs around, then...
			while true do
				os.pullEvent("redstone")  -- Wait for a redstone event.

				if not rs.getInput("back") then   -- Mobs are all dead.
					break   -- Exit the while loop.
				elseif rs.getInput("right") then  -- Panic button's been pushed.
					return  -- Exit the function.
				end
			end
		end
		
		sleep(10)  -- Maybe give the player a break before continuing?
	end
	
	print("Player has defeated all mobs! Congratulations!")
end

while true do
	-- Make sure all outputs are disabled.
	rs.setOutput("top",false)
	p.setBundledOutput(0)
	
	print("Waiting to start...")
	
	-- Wait for the initial button press that starts the action.
	while true do
		os.pullEvent("redstone")  -- Wait here until any redstone input changes.
		if rs.getInput("bottom") then break end
	end
	
	print("Arena button pressed! Starting combat in five seconds!")
	
	sleep(5)  -- Maybe give the player some time to move into the arena.
	
	doCombat()
end
drkmachine #3
Posted 28 February 2014 - 03:29 AM
First, thank you for the time you have taken to help.

Yes, you have the jist of it. Altho the back and bottom are reversed, simple change in the code.

The only issue I have with using a timer is that soul shards doesn't always spawn a mob within a minute, I was hoping to use the seer stones as a switch for both detecting when mobs initially spawn (turning the active spawner off) and detecting when to move to the next one. I will give this code a run in game shortly, perhaps the timer will be a moot point with a little adjustment to the time. :)/>
Bomb Bloke #4
Posted 28 February 2014 - 04:18 AM
You could, instead of waiting for a timer, wait for the seer stone's input to turn on - you'd use pretty much the same code that waits for it to turn off.

I've also somewhat of a memory that maybe the spawners are only disabled when a redstone signal is applied (as opposed to removed). If that's the case, then you'd instead use this to turn them all off:

p.setBundledOutput(65535)

65535 being the result of "colors.combine()"ing all possible colours.

And this would enable any given one:

p.setBundledOutput(colors.subtract(65535, entColors[i]))
drkmachine #5
Posted 28 February 2014 - 04:38 AM
lol, was just working through that colors.combine as I noticed this was updated. :)/> that is superb! Will adjust the code and test again, I thank you for your time and effort!