Posted 21 September 2012 - 09:49 PM
Started on a rpg for CC, but never finished it. I only worked on it for some hours. So everyone is free to test it and improve it:
The server code:
Client code:
The server code:
Spoiler
local code = textutils.serialize;
local uncode = textutils.unserialize;
local player = {}
local weaponSet = {head = 0, sword = 0, legs = 0, body = 0}
local inventorySet = {}
local mobs = {x=0,y=0,hp=0,att=0,def=0,loot=0}
local items = {name="",value=0,chr="",image=""}
local weapons = {item=0,damage=0}
local armore = {item=0,defence=0,Atype=0}
local tile = {chr="",isSolid=false,onT=nil}
local map = {}
map[0] = {}
local playerId = {}
local SPAWN = {x=5,y=5}
local LEATHER = 0;
local IRON = 1;
local STEEL = 2;
--Map VARS
local PLAYER_C = 1;
local AIR = 0;
local TILE = 2;
local MOB = 3;
--Connection VARS
local playerId = {}
playerId[""] = 0
local connected = {}
local run = true
rednet.open("top")
--Map function
local function setMap(size)
for i=0, size do
map[i] = {}
for j=0, size do
map[i][j] = {Atype = AIR,index = 0}
end
end
end
local function load_map(file)
end
local function send_map()
end
local function setMapTile(mapIndex,x,y,tile)
end
--Player and inventory
local function new_player(name)
player[#player+1] = {name = name,x=SPAWN.x,y=SPAWN.y,hp=100,dir=0,att=5,weapon_set=-1,inventory_set=-1}
map[SPAWN.x][SPAWN.y].Atype = PLAYER_C
return #player
end
local function new_inventory(slots)
local ssf = #inventorySet+1
inventorySet[sff] = {}
for i = 0, slots-1 do
inventorySet[sff][i] = 0
end
end
local function addToInventory(index,item)
for i=0, inventorySet[index][#inventorySet[index]] do
if inventorySet[index][i] == 0 then
inventorySet[index][i] = item
return true
end
end
return false
end
local function movePlayer(index,direction)
player[index].dir = direction;
local x = player[index].x
local y = player[index].y
local moved = false
if direction == 0 then --RIGHT
if map[x+1][y].Atype == AIR and not (x + 1 >= sqrt(#map)) then
player[index].x = x + 1
map[x+1][y].Atype = PLAYER_C
map[x+1][y].index = index
moved = true
end
end
if direction == 1 and not (y - 1 < 0) then--UP
if map[x][y-1].Atype == AIR then
player[index].y = y - 1
map[x][y-1].Atype = PLAYER_C
map[x][y-1].index = index
moved = true
end
end
if direction == 2 and not (x - 1 < 0) then--LEFT
if map[x-1][y].Atype == AIR then
player[index].x = x - 1
map[x-1][y].Atype = PLAYER_C
map[x-1][y].index = index
moved = true
end
end
if direction == 3 and not (y + 1 >= sqrt(#map)) then --DOWN
if map[x][y+1].Atype == AIR then
player[index].y = y + 1
map[x][y+1].Atype = PLAYER_C
map[x][y+1].index = index
moved = true
end
end
if moved then
map[x][y].Atype = AIR
map[x][y].index = 0
end
end
--Rednet
function red_rec(id,message)
mess = uncode(message)
if not (mess == nil) then
print("Creating user step 1")
if (mess[1] == "1") then
print("Creating user step 2")
if not (mess[2] == nil) then
print("Creating user step 3")
playerId["5"] =
new_player(mess[2])
connected[#connected+1] = id
rednet.send(id,code({"100","Connected"}))
end
end
if mess[1] == "2" then
movePlayer(playerId[string(id)].index,0)
end
if mess[1] == "3" then
movePlayer(playerId[string(id)].index,1)
end
if mess[1] == "4" then
movePlayer(playerId[string(id)].index,2)
end
if mess[1] == "5" then
movePlayer(playerId[string(id)].index,3)
end
end
end
function red_send()
local temp_tabel = {"101"}
for i=0, #connected-1 do
for j = 2, #map[0]+1 do
temp_tabel[j] = {}
for k =0, #map[0] do
temp_tabel[j][k] = map[j-1][k]
end
end
if connected[i] ~=nil then
print("Sending map to " .. connected[i])
rednet.send(connected[i],code(temp_tabel))
rednet.send(connected[i],code({"102",player[playerId["5"]].x,player[playerId["5"]].x}))
end
end
end
--Updater
function input_handler()
event,p1,p2 = os.pullEvent()
if event == "timer" then
os.startTimer(0.5)
end
if event == "rednet_message" then
print(event .. ": " .. p1 .. ", " .. p2)
red_rec(p1,p2)
end
end
function update()
input_handler()
red_send()
--draw()
end
function draw()
term.clear()
for i=0, #map[0] do
for j=0, #map[0] do
term.setCursorPos(i,j)
term.write(" ")
if (map[x][y].Atype == PLAYER_C) then
term.write("X")
end
end
end
end
function startUpdater()
while run do
update()
end
end
print("ID: " .. os.getComputerID())
os.startTimer(1)
setMap(100)
startUpdater()
print("Server shutdown")
Client code:
local code = textutils.serialize;
local uncode = textutils.unserialize;
local run = true
local server = nil
local map_data = {}
map_data[0] = {}
rednet.open("top")
while server == nil do
print("Server ID")
server = tonumber(read())
end
print("You nick:")
local nick = read()
rednet.send(server,code({"1",nick}))
id,mess = rednet.receive()
print(mess)
print("Message received")
os.sleep(1)
function red_rec(id,message)
mess = uncode(message)
print(mess)
if not mess == nil then
if mess[1] == "101" then
if not mess[2] == nil then
for i=2, #mess[0] - 1 do
map_date[i-1] = {}
for k=1, #mess[0] do
print(mess[i][k])
map_date[i-1][k] = mess[i][k]
end
end
end
end
if mess[1] == "2" then
end
if mess[1] == "3" then
end
if mess[1] == "4" then
end
if mess[1] == "5" then
end
end
end
function input_handler()
event,p1,p2 = os.pullEvent()
if event == "timer" then
os.startTimer(0.5)
end
if event == "rednet_message" then
red_rec(p1,p2)
end
end
function update()
input_handler()
draw()
end
function draw()
--term.clear()
--for i=1, #map_data[0] do
--for k=1, #map_data[0] do
-- term.setCursorPos(i,k)
-- term.write("X") --map_date[i-1][k] = mess[i][k]
--end
-- end
end
function startUpdater()
while run do
update()
end
end
draw()
os.sleep(3)
os.startTimer(1)
startUpdater()