View difference between Paste ID: vLfV3QfT and AiyFczy0
SHOW: | | - or go back to the newest paste.
1
--Farside's Fuzzy Navigation API
2
--get file stuff sorted declare variables
3
4
map = {}
5
newdirection = 0
6
7
function getmapy()
8
  --reads for file and gets coords
9
  if fs.exists("mapy.sqrl") then
10
    memory = fs.open("mapy.sqrl", "r")
11
    --do stuff
12
    for i = 1, 5, 1  do
13
      local line = memory.readLine()
14
      if line then
15
         map[i] = line
16
      end
17
    end
18
  else
19
    memory = fs.open("mapy.sqrl", "a")
20
    memory.writeLine("0")
21
    memory.writeLine("0")
22
    memory.writeLine("0")
23
    memory.writeLine("1")
24
    memory.writeLine(tostring(turtle.getFuelLevel()))
25
  end
26
      memory.close()
27
end
28
--x = 0
29
--y = 0
30
--z = 0
31
--compass = 1
32
--new function write to file new position
33
local function mapy()
34
  --write coords and heading to file
35
  memory = fs.open("mapy.sqrl", "w")
36
  for i = 1, 5, 1 do
37
    memory.writeLine(map[i])
38
  end
39
  memory.close()
40
end
41
local function locationy()
42
  getmapy()
43
  x = tonumber(map[1])
44
  y = tonumber(map[2])
45
  z = tonumber(map[3])
46
  compass = tonumber(map[4])
47
  print("locationy function")
48
  print("compass = ", map[4])
49
  
50
  if compass == 1 then
51
    x = x + 1
52
    map[1] = tostring(x)
53
    mapy()
54
    print("moving")
55
  end
56
  if compass == 2 then
57
    y = y + 1
58
    map [2] = tostring(y)
59
    mapy()
60
  end
61
  if compass == 3 then
62
    x = x - 1
63
    map[1] = tostring(x) 
64
    mapy()
65
  end
66
  if compass == 4 then
67
    y = y - 1
68
    map[2] = tostring(y)
69
    mapy()
70
  end
71
  
72
  memory.close()
73
end
74
75
function forward()
76
 -- print(map[1], ", ", map[2])
77
  print("forward function")
78
  local fuel = tonumber(map[5])
79
  turtle.forward()
80
  local fuelb = turtle.getFuelLevel()
81
 -- print("fuel and fuelb ", fuel, ", ", fuelb)
82
 
83
  if fuel > fuelb then
84
    locationy()
85
    map[5] = tostring(turtle.getFuelLevel())
86
    mapy()
87
    -- print(x)
88
   -- print(y)
89
   -- print(z)
90
   -- print(compass)
91
   
92
  end
93
end
94
95
function heading()
96
  if newdirection == 1 then
97
    compass = 1
98
    map[4] = tostring(1)
99
  end
100
  if newdirection == 2 then
101
    compass = 2
102
    map[4] = tostring(2)
103
  end
104
  if newdirection == 3 then
105
    compass = 3
106
    map[4] = tostring(3)
107
  end
108
  if newdirection == 4 then
109
    compass = 4
110
    map[4] = tostring(4)
111
  end
112
 -- else
113
 --   print("I have lost my direction", newdirection)
114
 -- end
115
  mapy()
116
end
117
  
118
119
function turnRight()
120
  turtle.turnRight()
121
  if tonumber(map[4]) < 4 then
122
    newdirection = tonumber(map[4]) + 1
123
    print("here", newdirection)
124
    heading()
125
  
126
  elseif tonumber(map[4]) == 4 then
127
    newdirection = 1
128
    heading()
129
  end
130
 -- else
131
   -- print("turnright failed")
132
133
end
134
function turnLeft()
135
  turtle.turnLeft()
136
  if tonumber(map[4]) > 1 then
137
    newdirection = tonumber(map[4]) - 1
138
    heading()
139
  
140
  elseif tonumber(map[4]) == 1 then
141
    newdirection = 4
142
    heading()
143
  end
144
end
145
146
function up()
147
  local fuel = tonumber(map[5])
148
  turtle.up()
149
  local fuelb = turtle.getFuelLevel()
150
  if fuel > fuelb then
151
    map[3] = map[3] + 1
152
    map[5] = fuelb
153
    mapy()
154
  end
155
end
156
function down()
157
  local fuel = tonumber(map[5])
158
  turtle.down()
159
  local fuelb = turtle.getFuelLevel()
160
  if fuel > fuelb then
161
    map[3] = map[3] - 1
162
    map[5] = fuelb
163
    mapy()
164
  end  
165
end