BrieF ConTenTS - No Starch Press

B ri e f Co nte nts

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Chapter 1: Getting Started with ComputerCraft . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Chapter 3: Talking to Your Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Chapter 4: Programming Turtles to Dance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Chapter 5: Making a Better Dancer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Chapter 6: Programming a Robot Lumberjack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Chapter 7: Creating Modules to Reuse Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Chapter 8: Running an Automated Tree Farm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Chapter 9: Building a Cobblestone Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Chapter 10: Making a Stone Brick Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Chapter 11: Constructing Walls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Chapter 12: Constructing Rooms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Chapter 13: Constructing Floors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Chapter 14: Programming a Robotic Farm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Chapter 15: Programming a Staircase Miner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Function Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Name ID Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

Co nte nts i n Detai l

Acknowledgments

xv

Introduction

xvii

What Are Minecraft Mods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii What Is ComputerCraft? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii How to Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix What's in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

1

Getting Started with ComputerCraft

1

Installing Minecraft, ATLauncher, and ComputerCraft . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Buying Minecraft Online . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Downloading and Installing ATLauncher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Downloading and Installing ComputerCraft . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Running Minecraft . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Creating a New World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Minecraft Game Mode Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2

Programming Basics

9

Getting Started with Turtles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Crafting a Mining Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Running Programs in the Turtle GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Fueling the Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Moving the Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Getting Started with Lua Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Running the Lua Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Goodbye Lua: Exiting Lua's Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Letting Lua Do the Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 That's So Random: Generating Random Numbers . . . . . . . . . . . . . . . . . . . . . 22

Storing Values with Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Checking the Turtle's Fuel Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3

Talking to Your Turtle

27

Teaching Your Turtle to Say Hello! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Running the hello Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Listing All Files with the ls Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Displaying Text with the print() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 The String Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Stringing Strings Together with Concatenation . . . . . . . . . . . . . . . . . . . . . . . . 31

Retrieving Turtle Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Getting Keyboard Input with the io.read() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Bonus Activity: Proper Introductions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Giving Text a Typewriter Effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Changing Turtle Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Bonus Activity: A Turtle by Any Other Name . . . . . . . . . . . . . . . . . . . . . . . . . . 34 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

4

Programming Turtles to Dance

35

Writing a Dance Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Running the mydance Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Using Comments in Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Turtle Movement Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Experimenting with Moving the Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Looping with for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Taking the Turtle for a Spin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Doing a Little Hop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

Bonus Activity: New Dance Moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Sharing and Downloading Programs Online . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

Deleting Files Off the Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Bonus Activity: Maze Runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

5

Making a Better Dancer

47

Writing a Better Dance Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Running the mydance2 Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The Boolean Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The nil Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Looping with while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Making Decisions with if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Comparing Two Values with Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Making Alternate Decisions with elseif Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Nested Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Making a Decision . . . or Else! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

Moving Up and Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Spinning All Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Bonus Activity: Watchturtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

x Contents in Detail

6

Programming a Robot Lumberjack

59

Equipping Turtles with Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Designing a Tree-Chopping Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Writing the choptree Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Running the choptree Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Detecting Blocks with the Turtle Detection Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

The not Boolean Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 The and Boolean Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The or Boolean Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Terminating Programs with the error() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Mining Blocks with the Turtle Digging Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Comparing Blocks with the Turtle Comparison Functions . . . . . . . . . . . . . . . . . . . . . . . 69 Returning to the Ground . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Startup Programs and the shell.run() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Bonus Activity: Going Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

7

Creating Modules to Reuse Your Code

71

Creating Functions with the function Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Making a Module of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Loading a Module with the os.loadAPI() Function . . . . . . . . . . . . . . . . . . . . . . 76 Experimenting with the hare Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

Looking at the Turtle's Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Selecting an Inventory Slot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Counting the Number of Items in a Slot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Getting Item Details from a Slot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 The Table Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Examining the Table Returned by the turtle.getItemDetail() Function . . . . . . . . . . 81

Global and Local Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Finding an Item with a for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Selecting an Empty Inventory Slot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

Bonus Activity: Dance Move Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

8

Running an Automated Tree Farm

85

Designing a Tree-Farming Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Writing the farmtrees Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Running the farmtrees Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Tree Types in Minecraft . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Chunk Loading in Minecraft . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Loading Modules with the os.loadAPI() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Checking for Files with the fs.exists() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Selecting Saplings in the Turtle's Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Planting a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Inspecting Blocks and Waiting for the Tree to Grow . . . . . . . . . . . . . . . . . . . . 92

Contents in Detail xi

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download