BRIEF CONTENTS

Front Matter

Introduction

Chapter 1: Getting Started with ComputerCraft

Chapter 2: Programming Basics

Chapter 3: Talking to Your Turtle

Chapter 4: Programming Turtles to Dance

Chapter 5: Making a Better Dancer

Chapter 6: Programming a Robot Lumberjack

Chapter 7: Creating Modules to Reuse Your Code

Chapter 8: Running an Automated Tree Farm

Chapter 9: Building a Cobblestone Generator

Chapter 10: Making a Stone Brick Factory

Chapter 11: Constructing Walls

Chapter 12: Constructing Rooms

Chapter 13: Constructing Floors

Chapter 14: Programming a Robotic Farm

Chapter 15: Programming a Staircase Miner

Function Reference

Name ID Reference

CONTENTS IN DETAIL

FRONT MATTER

INTRODUCTION

What Are Minecraft Mods?

What Is ComputerCraft?

How to Use This Book

What’s in This Book?

Getting Help

Online Resources

What You Learned

1
GETTING STARTED WITH COMPUTERCRAFT

Installing Minecraft, ATLauncher, and ComputerCraft

Buying Minecraft Online

Downloading and Installing ATLauncher

Downloading and Installing ComputerCraft

Running Minecraft

Creating a New World

Minecraft Game Mode Differences

What You Learned

2
PROGRAMMING BASICS

Getting Started with Turtles

Crafting a Mining Turtle

Running Programs in the Turtle GUI

Fueling the Turtle

Moving the Turtle

Getting Started with Lua Programming

Running the Lua Shell

Goodbye Lua: Exiting Lua’s Prompt

Letting Lua Do the Math

Order of Operations

That’s So Random: Generating Random Numbers

Storing Values with Variables

Checking the Turtle’s Fuel Levels

What You Learned

3
TALKING TO YOUR TURTLE

Teaching Your Turtle to Say Hello!

Running the hello Program

Listing All Files with the ls Command

Displaying Text with the print() Function

The String Data Type

Stringing Strings Together with Concatenation

Retrieving Turtle Names

Getting Keyboard Input with the io.read() Function

Bonus Activity: Proper Introductions

Giving Text a Typewriter Effect

Changing Turtle Names

Bonus Activity: A Turtle by Any Other Name

What You Learned

4
PROGRAMMING TURTLES TO DANCE

Writing a Dance Program

Running the mydance Program

Using Comments in Your Code

Turtle Movement Functions

Experimenting with Moving the Turtle

Looping with for Loops

Taking the Turtle for a Spin

Doing a Little Hop

Bonus Activity: New Dance Moves

Sharing and Downloading Programs Online

Deleting Files Off the Turtle

pastebin.com Limitations

turtleappstore.com

Bonus Activity: Maze Runner

What You Learned

5
MAKING A BETTER DANCER

Writing a Better Dance Program

Running the mydance2 Program

The Boolean Data Type

The nil Data Type

Looping with while Loops

Making Decisions with if Statements

Comparing Two Values with Comparison Operators

Making Alternate Decisions with elseif Statements

Nested Code Blocks

Making a Decision . . . or Else!

Moving Up and Down

Spinning All Around

Bonus Activity: Watchturtle

What You Learned

6
PROGRAMMING A ROBOT LUMBERJACK

Equipping Turtles with Tools

Designing a Tree-Chopping Algorithm

Writing the choptree Program

Running the choptree Program

Detecting Blocks with the Turtle Detection Functions

The not Boolean Operator

The and Boolean Operator

The or Boolean Operator

Terminating Programs with the error() Function

Mining Blocks with the Turtle Digging Functions

Comparing Blocks with the Turtle Comparison Functions

Returning to the Ground

Startup Programs and the shell.run() Function

Bonus Activity: Going Down

What You Learned

7
CREATING MODULES TO REUSE YOUR CODE

Creating Functions with the function Statement

Arguments and Parameters

Return Values

Making a Module of Functions

Loading a Module with the os.loadAPI() Function

Experimenting with the hare Module

Looking at the Turtle’s Inventory

Selecting an Inventory Slot

Counting the Number of Items in a Slot

Getting Item Details from a Slot

The Table Data Type

Examining the Table Returned by the turtle.getItemDetail() Function

Global and Local Scope

Finding an Item with a for Loop

Selecting an Empty Inventory Slot

Bonus Activity: Dance Move Module

What You Learned

8
RUNNING AN AUTOMATED TREE FARM

Designing a Tree-Farming Program

Writing the farmtrees Program

Running the farmtrees Program

Tree Types in Minecraft

Chunk Loading in Minecraft

Loading Modules with the os.loadAPI() Function

Checking for Files with the fs.exists() Function

Selecting Saplings in the Turtle’s Inventory

Planting a Tree

Inspecting Blocks and Waiting for the Tree to Grow

Breaking Out of Loops with break Statements

Running Other Programs with the shell.run() Function

Handling Items with the Turtle’s Drop Functions

Rewriting Your Code for When You Have No Bone Meal

Bonus Activity: Lone Turtle Farmer

What You Learned

9
BUILDING A COBBLESTONE GENERATOR

Blueprints for the Cobblestone Generator

Setting Up Furnaces for Smelting the Cobblestone

Writing the cobminer Program

Running the cobminer Program

Setting Up Your Program and Making a Constant Variable

Mining the Cobblestone from the Generator

Interacting with Furnaces

Making Code Readable with Constants

Dropping the Cobblestone into the Furnaces

Rounding Numbers with the math.floor() and math.ceil() Functions

Calculating the Cobblestone to Distribute in Each Furnace

Moving the Cobblestone Miner Back into Position

Bonus Activity: Cobble Together More Cobblestone Furnaces

What You Learned

10
MAKING A STONE BRICK FACTORY

Designing a Program to Craft Stone Bricks

Crafting a Crafty Turtle

Writing the brickcrafter Program

Running the brickcrafter Program

Setup for the brickcrafter Program

Checking the Turtle’s Fuel

Collecting Stone from the Furnaces

Crafting Stone Bricks

Moving the Turtle Back into Position

Bonus Activity: Cake Factory

Creating a Factory Building

What You Learned

11
CONSTRUCTING WALLS

Extending the hare Module

Counting Inventory Items with countInventory()

Selecting and Placing a Block

Designing a Wall-Building Algorithm

Writing the buildWall() Function

Writing and Running the buildwall Program

Loading the hare Module

Using the Array Data Type

Reading Command Line Arguments

Creating Usage Messages

Calling hare.buildWall() to Build a Wall

Bonus Activity: mylabel Program with Command Line Arguments

What You Learned

12
CONSTRUCTING ROOMS

Designing a Room-Building Algorithm

Extending the hare Module

Calculating the Total Number of Blocks Needed to Build a Room

Writing the buildRoom() Function

Writing the buildroom Program

Running the buildroom Program

Bonus Activity: Plus-Shaped Rooms

What You Learned

13
CONSTRUCTING FLOORS

Designing the Sweeping Algorithm

Building the Floor

Returning to the Starting Position

Passing Functions to Functions

Extending the hare Module

Calling the sweepFunc() Function

Moving Along the Rows and Columns

Figuring Out If a Number Is Even or Odd with the Modulus Operator

The Even-Width and Odd-Width Paths

Writing the buildFloor() Function

Writing the buildfloor Program

Running the buildfloor Program

Creating a Patterned Floor

Writing the buildcheckerboard Program

Running the buildcheckerboard Program

Writing the placeCheckerboard() Function

Calling the sweepField() Function

Bonus Activity: Different-Colored Checkerboards

What You Learned

14
PROGRAMMING A ROBOTIC FARM

Setting Up a Wheat Field

Designing the Wheat-Farming Algorithm

Extending the hare Module

Writing the farmwheat Program

Running the farmwheat Program

Setup for the farmwheat Program

Writing Functions to Use in the Main Program

Checking the Crop

Planting Seeds

Storing Wheat

Farming with a Loop

Bonus Activity: Giant Wheat Fields

Tips for Automating Other Kinds of Farming

Farming Vegetables

Milking Cows and Shearing Sheep

Gathering Chicken Eggs

Farming Cacti and Sugar Cane

What You Learned

15
PROGRAMMING A STAIRCASE MINER

Designing the Stair-Mining Algorithm

Extending the hare Module

Writing the digUntilClear() and digUpUntilClear() Functions

Writing the stairminer Program

Running the stairminer Program

Setup for the stairminer Program

Creating the First Stair Step

Mining Downward

Checking the Turtle’s Fuel

Checking the Turtle’s Inventory

Mining Upward

Bonus Activity: Tall Tunnel

What You Learned

FUNCTION REFERENCE

fs (File System) API

hare API

io (Input/Output) API

math API

os (Operating System) API

shell API

string API

textutils API

turtle API

Building Functions

Fueling Functions

Inventory Functions

Movement Functions

Perception Functions

Tool-Related Functions

Lua Functions

NAME ID REFERENCE

Finding a Block’s Name ID

Distinguishing Between Blocks That Share Name IDs

List of Block Name IDs