View difference between Paste ID: DDkxQxad and mV94veBL
SHOW: | | - or go back to the newest paste.
1
-- Define Stuff.
2-
local playerName = "-YourName-"
2+
local playerName = "-chucky_100-"
3
local xpGained = 0
4
local xpLevel = 0
5
local sword = "bs"
6
local armorEq = "no"
7
local health = 20
8
local mana = 20
9
local armor = 0
10
11
local guySpeaking = "Tjakka5"
12
13
local allVariables = {}
14
allVariables.playerName = playerName
15
allVariables.xpGained = xpGained
16
allVariables.xpLevel = xpLevel
17
allVariables.sword = sword
18
allVariables.armorAq = armorEq
19
allVariables.health = health
20
allVariables.mana = mana
21
allVariables.armor = armor
22
23
tableOfVariables = textutils.serialize(allVariables)
24
25
local loadedMap = {}  --set loadedmap as empty table
26
local loadedProps = {}
27
local playerSpot
28
local loadedMapName
29
30
-- testMap1.
31
32
local testMap1 =  {"[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",
33
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
34
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
35
                   "[]", "  ", "[]", "  ", "o/", "  ", "[]", "  ", "[]",
36
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
37
                   "[]", "  ", "[]", "  ", "  ", "  ", "[]", "  ", "[]",
38
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
39
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
40
                   "[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",}
41
42
local testMap2 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
43
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
44
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",       
45
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
46
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
47
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
48
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
49
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
50
                   "[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]"}     
51
52
local testMap3 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
53
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
54
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",       
55
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
56
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
57
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
58
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
59
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
60
                   "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}     
61
62
local testMap4 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
63
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
64
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",       
65
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
66
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
67
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
68
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
69
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
70
                   "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}     
71
72
local testMap5 =  {"[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",
73
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
74
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",       
75
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
76
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
77
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
78
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
79
                   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
80
                   "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}     
81
82
function checkEvent(spot)
83
  if loadedMapName == "testMap1" then
84
    if spot == 32 then
85
      xpGained = xpGained+1
86
    end
87
  end
88
end
89
    
90
91
function levelUp()
92
  if xpLevel == 0 then
93
    xpReq = 5
94
  else
95
    xpReq = xpLevel*10
96
    if xpLevel > 10 then
97
      xpReq = xpLevel*20
98
    end
99
  end
100
  if xpGained == xpReq then
101
    xpGained = xpGained-xpReq
102
    xpLevel = xpLevel+1
103
  elseif xpGained > xpReq then
104
    xpGained = xpGained-xpReq
105
    xpLevel = xpLevel+1
106
  end
107
end
108
109
function doNewNum(pos, time)
110
  if time == 1 then
111
    pos = pos-10
112
    return(pos)
113
  elseif time == 2 then
114
    pos = pos+1
115
    return(pos)
116
  elseif time == 3 then
117
    pos = pos+1
118
    return(pos)
119
  elseif time == 4 then
120
    pos = pos+7
121
    return(pos)
122
  elseif time == 5 then
123
    pos = pos+2
124
    return(pos)
125
  elseif time == 6 then
126
    pos = pos+7
127
    return(pos)
128
  elseif time == 7 then
129
    pos = pos+1
130
    return(pos)
131
  elseif time == 8 then
132
    pos = pos+1
133
    return(pos)
134
  end
135
end
136
137
function checkValidClick(button, x, y)
138
  pos = checkPlayerPos()
139
  y = y-1
140
  y = y*18
141
  cp = x+y    
142
  cp = cp/2
143
  nextNum = 0.5
144
  for i = 1, 81 do
145
    if cp == nextNum then
146
      cp = cp+0.5
147
      break
148
    else
149
      nextNum = nextNum+1
150
    end
151
  end  
152
  nextNum = 0.5   
153
  for i = 1, 8 do
154
    pos = doNewNum(pos, i)
155
    if pos == cp then
156
      checkEvent(cp)
157
      break
158
    end
159
  end
160
end 
161
162
function loadAll()
163
  if fs.exists("RPG_Game_SaveFile") then
164
    local file = fs.open("RPG_Game_SaveFile", "r")
165
    things = file[1]
166
    print("" ..things)
167
  end
168
end
169
170
function saveAll()
171
  if fs.exists("RPG_Game_SaveFile") then
172
    local file = fs.open("RPG_Game_SaveFile", "w")
173
    file.write(tableOfVariables)
174
    file.close()
175
    print("Saved!")
176
  end
177
end
178
179
function gameMenu()
180
  term.clear()
181
  term.setCursorPos(1, 1)
182
  print("+------------------------------------------------+")
183
  print("| MENU                                       |")
184
  print("|                                                |")
185
  print("|                                                |")
186
  print("|                    Resume                      |")
187
  print("|                    Save                        |")
188
  print("|                    Load                        |")
189
  print("|                    Log                         |")
190
  print("|                    Exit                        |")
191
  print("|                                                |")
192
  print("|                                                |")
193
  print("+------------------------------------------------+")
194
  inOptionScreen = 1
195
  while inOptionScreen == 1 do
196
    event, button, x, y = os.pullEvent("mouse_click")
197
    if (x == 22) or (x == 23) or (x == 24) or (x == 25) or (x == 26) or (x == 27) then
198
      if y == 5 then
199
        i = 1000000000000000
200
        inOptionScreen = 0
201
      elseif y == 6 then
202
        saveAll()
203
      elseif y == 7 then
204
        loadAll()
205
      elseif y == 8 then
206
        print("Log")
207
      elseif y == 9 then
208
        os.shutdown()
209
      end
210
    end
211
    runOs()
212
  end
213
end
214
215
216
function printTalk()
217
  term.setCursorPos(20, 11)
218
  print("  " ..guySpeaking)
219
  term.setCursorPos(20, 12)
220
  print("+-----------------------------+")
221
  term.setCursorPos(20, 13)
222
  print("| Hey Guys!                   |")
223
  term.setCursorPos(20, 14)
224
  print("| This part is not quite done |")
225
  term.setCursorPos(20, 15)
226
  print("| but here you can see what   |")
227
  term.setCursorPos(20, 16)
228
  print("| the people say to you.      |")
229
  term.setCursorPos(20, 17)
230
  print("| Stay Tuned!                 |")
231
  term.setCursorPos(20, 18)
232
  print("+-----------------------------+")
233
end
234
235
function checkStats()
236
  if armorEq == "no" then
237
    armor = 0
238
  elseif armorEq == "le" then
239
    armor = 1
240
  end
241
end
242
243
function printInv()
244
  term.setCursorPos(20, 1)
245
  print("+-----------------------------+")
246
  term.setCursorPos(20, 2)
247
  print("|                             |")
248
  term.setCursorPos(20, 3)
249
  term.write("| Sword:  ")
250
  
251
  if sword == "bs" then
252
    print("Basic Sword         |")
253
    term.setCursorPos(20, 4)
254
    term.write("| This sword does basic damage|")
255
  
256
  elseif sword == "as" then
257
    print("Advanced Sword      |")
258
    term.setCursorPos(20, 4)
259
    term.write("| Just a bit sharper...       |")
260
  
261
  elseif sword == "bk" then
262
    print("Basic Knife         |")
263
    term.setCursorPos(20, 4)
264
    term.write("| Grose, it has blood on it!  |")
265
  
266
  elseif sword == "ls" then
267
    print("Long Sword          |")
268
    term.setCursorPos(20, 4)
269
    term.write("| Distant killing!            |")
270
  end
271
  
272
  term.setCursorPos(20, 5)
273
  print("|                             |")
274
  term.setCursorPos(20, 6)
275
  term.write("| Armor:  ")
276
  
277
  if armorEq == "no" then
278
    print("No armor equiped    |")
279
    term.setCursorPos(20, 7)
280
    term.write("| You're naked!               |")
281
  elseif armorEq == "le" then
282
    print("Leather armor       |")
283
    term.setCursorPos(20, 7)
284
    term.write("| Warm, cozy, weak.           |")
285
  end
286
 
287
  term.setCursorPos(20, 8)
288
  print("|                             |")
289
  term.setCursorPos(20, 9)
290
  print("+-----------------------------+")
291
end 
292
   
293
function printStats()
294
  term.setCursorPos(1, 11)
295
  print("  " ..playerName)
296
  print("+----------------+")
297
  print("|                |")
298
  term.write("| Health:  " ..health)
299
  if health > 9 then
300
    print("    |")
301
  else
302
    print("     |")
303
  end
304
  term.write("| Mana:    " ..mana)
305
  if mana > 9 then
306
    print("    |")
307
  else
308
    print("     |")
309
  end
310
  print("| Armor:   " ..armor.. "     |")
311
  term.write("| xpLvl:   " ..xpLevel)
312
  if xpLevel > 9 then
313
    print("    |")
314
  else
315
    print("     |")
316
  end 
317
  print("+----------------+")
318
end
319
320
321
function loadMap(map)
322
  for i = 1, 81 do
323
   
324
    if map == testMap1 then
325
      loadedMapName = "testMap1"
326
      loadedMap[i] = testMap1[i]
327
      setProps(testMap1)
328
   
329
    elseif map == testMap2 then
330
      loadedMapName = "testMap2"
331
      loadedMap[i] = testMap2[i]
332
      setProps(testMap2)
333
    
334
    elseif map == testMap3 then
335
      loadedMapName = "testMap3"
336
      loadedMap[i] = testMap3[i]
337
      setProps(testMap3)
338
      
339
    elseif map == testMap4 then
340
      loadedMapName = "testMap4"
341
      loadedMap[i] = testMap4[i]
342
      setProps(testMap4)
343
    
344
    elseif map == testMap5 then
345
      loadedMapName = "testMap5"
346
      loadedMap[i] = testMap5[i]
347
      setProps(testMap5)
348
    end 
349
  end
350
end
351
352
353
function setProps(map)
354
  for i = 1, 81 do
355
    if map == testMap1 then
356
      o = testMap1[i]
357
    elseif map == testMap2 then
358
      o = testMap2[i]
359
    elseif map == testMap3 then
360
      o = testMap3[i]
361
    elseif map == testMap4 then
362
      o = testMap4[i]
363
    elseif map == testMap5 then
364
      o = testMap5[i]
365
    end  
366
      
367
    if o == "[]" then
368
      loadedProps[i] = "s"
369
    elseif o == "  " then
370
      loadedProps[i] = "a"
371
    elseif o == "O/" then
372
      loadedProps[i] = "p" 
373
    elseif o == "o/" then
374
      loadedProps[i] = "e"
375
    end
376
  end
377
end 
378
                                      
379
function printMap()
380
  term.clear()
381
  term.setCursorPos(1, 1)
382
  for i = 1, 81 do
383
    term.write("" ..loadedMap[i])
384
    if (i == 9) or (i == 18) or (i == 27) or (i == 36) or (i == 45) or (i == 54) or (i == 63) or (i == 72) then
385
      print("")
386
    end                                                                           
387
  end
388
end
389
390
function checkPlayerPos()
391
  for i = 1, 81 do
392
    o = loadedProps[i]                
393
    if o == "p" then
394
      playerSpot = i
395
      return(playerSpot)      
396
    end
397
  end
398
end                                
399
400
function checkValidSpot(direction)
401
  playerSpot = checkPlayerPos()
402
  if direction == "up" then
403
    if loadedProps[playerSpot-9] == "a" then
404
      return(true)
405
    else
406
      return(false)
407
    end
408
  elseif direction == "down" then
409
    if loadedProps[playerSpot+9] == "a" then
410
      return(true)
411
    else 
412
      return(false)      
413
    end
414
  elseif direction == "left"then
415
    if loadedProps[playerSpot-1] == "a" then
416
      return(true)
417
    else
418
      return(false)
419
    end
420
  elseif direction == "right" then
421
    if loadedProps[playerSpot+1] == "a" then
422
      return(true)
423
    else
424
      return(false)
425
    end
426
  end
427
end
428
                                                                                                                                
429
function movePlayer(direction)
430
  valid = checkValidSpot(direction)                                                                                                                                                                                                                                                                  
431
  playerPos = checkPlayerPos() 
432
  if valid == true then
433
    loadedMap[playerPos] = "  "
434
    loadedProps[playerPos] = "a"
435
    if direction == "up" then
436
      loadedMap[playerPos-9] = "O/"
437
      loadedProps[playerPos-9] = "p"
438
    elseif direction == "down" then
439
      loadedMap[playerPos+9] = "O/"
440
      loadedProps[playerPos+9] = "p"
441
    elseif direction == "left" then
442
      loadedMap[playerPos-1] = "O/"
443
      loadedProps[playerPos-1] = "p"
444
    elseif direction == "right" then
445
      loadedMap[playerPos+1] = "O/"
446
      loadedProps[playerPos+1] = "p"
447
    end
448
  end
449
end
450
451
function checkTp()
452
  playerPos = checkPlayerPos()
453
  if loadedMapName == "testMap1" then
454
    if playerPos == 5 then
455
      loadMap(testMap2)
456
      loadedMap[68] = "O/"
457
      loadedProps[68] = "p"
458
    end
459
    if playerPos == 45 then
460
      loadMap(testMap3)
461
      loadedMap[38] = "O/"
462
      loadedProps[38] = "p"
463
    end
464
    if playerPos == 37 then
465
      loadMap(testMap4)
466
      loadedMap[44] = "O/"
467
      loadedProps[44] = "p"
468
    end
469
    if playerPos == 77 then
470
      loadMap(testMap5)
471
      loadedMap[14] = "O/"
472
      loadedProps[14] = "p"
473
    end
474
  end
475
  if loadedMapName == "testMap2" then
476
    if playerPos == 77 then
477
      loadMap(testMap1)
478
      loadedMap[14] = "O/"
479
      loadedProps[14] = "p"
480
    end
481
  end
482
  if loadedMapName == "testMap3" then
483
    if playerPos == 37 then
484
      loadMap(testMap1)
485
      loadedMap[44] = "O/"
486
      loadedProps[44] = "p"
487
    end
488
  end
489
  if loadedMapName == "testMap4" then
490
    if playerPos == 45 then
491
      loadMap(testMap1)
492
      loadedMap[38] = "O/"
493
      loadedProps[38] = "p"
494
    end
495
  end
496
  if loadedMapName == "testMap5" then
497
    if playerPos == 5 then
498
      loadMap(testMap1)
499
      loadedMap[68] = "O/"
500
      loadedProps[68] = "p"
501
    end
502
  end
503
end
504
505
-- Update tick functions, Oh gawd, here is where the fun part starts.
506
507
function waitOs()
508
  os.startTimer(0.05)
509
  event, button, x, y = os.pullEvent()
510
  if event == "mouse_click" then
511
    checkValidClick(button, x, y)
512
    os.pullEvent("timer")
513
  elseif event == "key" then
514
    pressedKey = button
515
    if (pressedKey == 200) or (pressedKey == 17) then
516
      movePlayer("up")
517
    elseif (pressedKey == 203) or (pressedKey == 30) then
518
      movePlayer("left")
519
    elseif (pressedKey == 208) or (pressedKey == 31) then
520
      movePlayer("down")
521
    elseif (pressedKey == 205) or (pressedKey == 32) then
522
      movePlayer("right")
523
    
524
    elseif pressedKey == 28 then
525
      gameMenu()
526
    end
527
    
528
    os.pullEvent("timer")
529
  end
530
  checkTp()
531
  printMap()
532
  checkStats()
533
  printStats()
534
  printInv()
535
  printTalk()
536
  levelUp()
537
end   
538
539
540
-- Function for middlePrinting, as many suggested...
541
w, h = term.getSize()
542
543
function centerPrint(y, text)
544
  term.setCursorPos(w/2 - #text/2, y)
545
  write(text)
546
end
547
    
548
-- Startup Stuff
549
550
551
loadMap(testMap1)
552
loadedMap[68] = "O/"
553
loadedProps[68] = "p"
554
555
function runOs()
556
  for i = 1, 10000000000000000000000000000000000000000000000000000000 do --Dont play this long, please.
557
    waitOs()
558
  end
559
end
560
561
runOs()