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

My First Program (Miner) (And a question)

Started by eglove, 07 June 2014 - 07:10 PM
eglove #1
Posted 07 June 2014 - 09:10 PM
Pastebin: http://pastebin.com/G50HBmnV

I just found this plugin and I started playing with it today. I made a turtle that digs a mine by spiraling in given a specified length. It ends up looking something like this (with 15 as the input):



It leaves two walls between each row for efficiency when the player checks for anything it missed. Golng up one at the end of its spiral is an unintended consequence, but nothing I'm worried about. ;)/>

The reason I have the initial message saying "(less than 64)" for length is because I gave that as a very rough estimate for how far the turtle can go with a full stack of torches before running out. Looking at the API I think it'd be pretty easy to go as far as the user wants, assuming they stay in the same chunk load. If anyone knows of a good way of calculating exactly how many torches would be needed for a given input please let me know. The spiral turns this into a tricky question, but it's a fun thing to try and figure out.

My question is about the variables. I declared 'n' as a local and count as a global (at the top). Was it necessary to pass n every time? And is it standard to place global variables at the top? I've never used LUA before and I'm curious about common syntax/usage. :)/>

Bonus Question: Is there any chance of the plugin expanding into item numbers in the future? That is, turtles being able to check for specific item numbers around them or in their own inventories without having that item already with them? It'd be a very powerful addition. :)/>
CometWolf #2
Posted 08 June 2014 - 12:03 AM
My question is about the variables. I declared 'n' as a local and count as a global (at the top). Was it necessary to pass n every time? And is it standard to place global variables at the top? I've never used LUA before and I'm curious about common syntax/usage. :)/>
Since this is a standalone program and not an API, all your variables(This means functions as well) should be declared locally, as they are of no use to other programs. Provided you declare n locally prior to the functions using it, you wouldn't need to pass it to them either. As for globals, when making an API i suppose it would be pretty standard to declare them at the top, along with any local variables used in multiple functions/places. Each of them properly commented ofcourse. Makes it a lot easier to read for someone who did not code it.

Bonus Question: Is there any chance of the plugin expanding into item numbers in the future? That is, turtles being able to check for specific item numbers around them or in their own inventories without having that item already with them? It'd be a very powerful addition.
Nope, it has been deemed too powerful and will never be added. There are however some mods that add this functionailty. OpenP comes to mind, it enables getting item info(name,amount,id,metaid) of items in the turtle or nearby inventories. I believe there's some for direct block detection aswell, but they're probably outdated.
Edited on 07 June 2014 - 10:06 PM
eglove #3
Posted 08 June 2014 - 03:51 AM
Since this is a standalone program and not an API, all your variables(This means functions as well) should be declared locally, as they are of no use to other programs. Provided you declare n locally prior to the functions using it, you wouldn't need to pass it to them either. As for globals, when making an API i suppose it would be pretty standard to declare them at the top, along with any local variables used in multiple functions/places. Each of them properly commented ofcourse. Makes it a lot easier to read for someone who did not code it.

Ok, that makes sense. Thanks very much. :)/>

Nope, it has been deemed too powerful and will never be added. There are however some mods that add this functionailty. OpenP comes to mind, it enables getting item info(name,amount,id,metaid) of items in the turtle or nearby inventories. I believe there's some for direct block detection aswell, but they're probably outdated.

Too powerful huh? I see a lot of potential with the plugin. Specific block detection and even x,y,z coordinate knowledge (without creating your own) would make it easier to work with. I know Minecolonies is considering making their NPC's chunk loaders. It'd be nice to have a config option for this with turtles. I can so see people crashing their client and destroying worlds a lot, but hey, welcome to programming right? lol

Thanks for the answers. :D/>
Dragon53535 #4
Posted 09 June 2014 - 04:09 AM
My question is about the variables. I declared 'n' as a local and count as a global (at the top). Was it necessary to pass n every time? And is it standard to place global variables at the top? I've never used LUA before and I'm curious about common syntax/usage. :)/>/>

If you put the variables at the top of a program, and localize them, you can use them anywhere in the program, just not in any other program. You can view everything as a box, the main OS is a box, and the programs are a box inside that box, functions are a box inside the program box. Each box can interact with ALL the things outside of them except other boxes in their same layer, however they can only interact with the specific parts inside their box, and not inside the other box. (The OS can just because it's programmed that way :P/>/>)
Edited on 09 June 2014 - 02:10 AM