mirror of
				https://github.com/LDDestroier/CC/
				synced 2025-10-31 15:32:59 +00:00 
			
		
		
		
	Added Skynet, grayscale support (#7)
* Tried to add skynet * Update tron * Update tron * Update tron * Added grayscale support
This commit is contained in:
		
							
								
								
									
										185
									
								
								tron
									
									
									
									
									
								
							
							
						
						
									
										185
									
								
								tron
									
									
									
									
									
								
							| @@ -7,8 +7,11 @@ | ||||
|  | ||||
| local port = 701 | ||||
| local scr_x, scr_y = term.getSize() | ||||
| local isColor = term.isColor() | ||||
|  | ||||
| -- lower value = faster game. I'd reccommend 0.1 for SMP play. | ||||
| local gameDelayInit = 0.1 | ||||
|  | ||||
| local gameDelayInit = 0.1 -- lower value = faster game. I'd reccommend 0.1 for SMP play. | ||||
| local initGrid = { | ||||
| 	x1 = -100, | ||||
| 	y1 = -100, | ||||
| @@ -64,21 +67,47 @@ local resetPlayers = function() | ||||
| 		} | ||||
| 	} | ||||
| end | ||||
| local tArg = {...} | ||||
| local useSkynet = (tArg[1] or ""):lower() == "skynet" | ||||
| local skynetPath = "skynet" | ||||
| local skynetURL = "https://raw.githubusercontent.com/osmarks/skynet/master/client.lua" | ||||
|  | ||||
| local modem = peripheral.find("modem") | ||||
| if (not modem) and ccemux then | ||||
| local modem, skynet | ||||
| if useSkynet then | ||||
| 	if fs.exists(skynetPath) then | ||||
| 		skynet = dofile(skynetPath) | ||||
| 		skynet.open(port) | ||||
| 	else | ||||
| 		local prog = http.get(skynetURL) | ||||
| 		if prog then | ||||
| 			local file = fs.open(skynetPath, "w") | ||||
| 			file.write(prog.readAll()) | ||||
| 			file.close() | ||||
| 			skynet = dofile(skynetPath) | ||||
| 			skynet.open(port) | ||||
| 		else | ||||
| 			error("Could not download Skynet.") | ||||
| 		end | ||||
| 	end | ||||
| else | ||||
| 	modem = peripheral.find("modem") | ||||
| 	if (not modem) and ccemux then | ||||
| 		ccemux.attach("top", "wireless_modem") | ||||
| 		modem = peripheral.find("modem") | ||||
| 	end | ||||
| 	if modem then | ||||
| 		modem.open(port) | ||||
| 	else | ||||
| 		error("You should attach a modem.") | ||||
| 	end | ||||
| end | ||||
|  | ||||
| local transmit = function(port, message) | ||||
| 	if useSkynet then | ||||
| 		skynet.send(port, message) | ||||
| 	else | ||||
| 		modem.transmit(port, port, message) | ||||
| end | ||||
|  | ||||
| if modem then | ||||
| 	modem.open(port) | ||||
| else | ||||
| 	error("You should attach a modem.") | ||||
| 	end | ||||
| end | ||||
|  | ||||
| local gamename = "" | ||||
| @@ -86,12 +115,77 @@ local isHost | ||||
| local squareGrid = true | ||||
|  | ||||
| local waitingForGame = true | ||||
| local toblit = { | ||||
| 	[0] = " ", | ||||
| 	[colors.white] = "0", | ||||
| 	[colors.orange] = "1", | ||||
| 	[colors.magenta] = "2", | ||||
| 	[colors.lightBlue] = "3", | ||||
| 	[colors.yellow] = "4", | ||||
| 	[colors.lime] = "5", | ||||
| 	[colors.pink] = "6", | ||||
| 	[colors.gray] = "7", | ||||
| 	[colors.lightGray] = "8", | ||||
| 	[colors.cyan] = "9", | ||||
| 	[colors.purple] = "a", | ||||
| 	[colors.blue] = "b", | ||||
| 	[colors.brown] = "c", | ||||
| 	[colors.green] = "d", | ||||
| 	[colors.red] = "e", | ||||
| 	[colors.black] = "f" | ||||
| } | ||||
| local tograyCol, tograyBlit = { | ||||
| 	[0] = 0, | ||||
| 	[colors.white] = colors.white, | ||||
| 	[colors.orange] = colors.lightGray, | ||||
| 	[colors.magenta] = colors.lightGray, | ||||
| 	[colors.lightBlue] = colors.white, | ||||
| 	[colors.yellow] = colors.white, | ||||
| 	[colors.lime] = colors.lightGray, | ||||
| 	[colors.pink] = colors.lightGray, | ||||
| 	[colors.gray] = colors.gray, | ||||
| 	[colors.lightGray] = colors.lightGray, | ||||
| 	[colors.cyan] = colors.lightGray, | ||||
| 	[colors.purple] = colors.gray, | ||||
| 	[colors.blue] = colors.gray, | ||||
| 	[colors.brown] = colors.gray, | ||||
| 	[colors.green] = colors.gray, | ||||
| 	[colors.red] = colors.white, | ||||
| 	[colors.black] = colors.black | ||||
| }, {} | ||||
|  | ||||
| local termblit, termwrite, termclear = term.blit, term.write, term.clear | ||||
| local tocolors = {} | ||||
| for k,v in pairs(toblit) do | ||||
| 	tocolors[v] = k | ||||
| end | ||||
| for k,v in pairs(tograyCol) do | ||||
| 	tograyBlit[toblit[k]] = toblit[v] | ||||
| end | ||||
|  | ||||
| local termwrite, termclear = term.write, term.clear | ||||
| local termsetCursorPos, termgetCursorPos = term.setCursorPos, term.getCursorPos | ||||
| local tableunpack, tableremove = unpack, table.remove | ||||
| local mathfloor, mathceil, mathcos, mathsin = math.floor, math.ceil, math.cos, math.sin | ||||
| local termsetTextColor, termsetBackgroundColor = term.setTextColor, term.setBackgroundColor | ||||
|  | ||||
| local termsetTextColor = function(col) | ||||
| 	return term.setTextColor(isColor and col or tograyCol[col]) | ||||
| end | ||||
|  | ||||
| local termsetBackgroundColor = function(col) | ||||
| 	return term.setBackgroundColor(isColor and col or tograyCol[col]) | ||||
| end | ||||
|  | ||||
| local termblit = function(char, text, back) | ||||
| 	if isColor then | ||||
| 		return term.blit(char, text, back) | ||||
| 	else | ||||
| 		return term.blit( | ||||
| 			char, | ||||
| 			text:gsub(".", tograyBlit), | ||||
| 			back:gsub(".", tograyBlit) | ||||
| 		) | ||||
| 	end | ||||
| end | ||||
|  | ||||
| local tsv = function(visible) | ||||
| 	if term.current().setVisible then | ||||
| @@ -250,48 +344,21 @@ local images = { | ||||
| for k,v in pairs(images) do | ||||
| 	v.x = #v[1][1] | ||||
| 	v.y = #v[1] | ||||
| 	--[[ | ||||
| 	for i = 2, #v do | ||||
| 		for line = 1, #v[i] do | ||||
| 			images[k][i][line] = v[i][line]:gsub("f", " ") | ||||
| 		end | ||||
| 	end | ||||
| 	--]] | ||||
| end | ||||
|  | ||||
| local tocolors = {} | ||||
| local toblit = { | ||||
| 	[0] = " ", | ||||
| 	[colors.white] = "0", | ||||
| 	[colors.orange] = "1", | ||||
| 	[colors.magenta] = "2", | ||||
| 	[colors.lightBlue] = "3", | ||||
| 	[colors.yellow] = "4", | ||||
| 	[colors.lime] = "5", | ||||
| 	[colors.pink] = "6", | ||||
| 	[colors.gray] = "7", | ||||
| 	[colors.lightGray] = "8", | ||||
| 	[colors.cyan] = "9", | ||||
| 	[colors.purple] = "a", | ||||
| 	[colors.blue] = "b", | ||||
| 	[colors.brown] = "c", | ||||
| 	[colors.green] = "d", | ||||
| 	[colors.red] = "e", | ||||
| 	[colors.black] = "f" | ||||
| } | ||||
| for k,v in pairs(toblit) do | ||||
| 	tocolors[v] = k | ||||
| end | ||||
|  | ||||
| local drawImage = function(im, x, y) | ||||
| 	local cx, cy = termgetCursorPos() | ||||
| 	termsetBackgroundColor(tocolors[initGrid.voidcol]) | ||||
| 	termsetTextColor(tocolors[initGrid.voidcol]) | ||||
| 	termsetBackgroundColor(	tocolors[initGrid.voidcol] ) | ||||
| 	termsetTextColor(		tocolors[initGrid.voidcol] ) | ||||
| 	for iy = 1, #im[1] do | ||||
| 		for ix = 1, #im[1][iy] do | ||||
| 			termsetCursorPos(x+(ix-1),y+(iy-1)) | ||||
| 			if not (im[2][iy]:sub(ix,ix) == " " and im[3][iy]:sub(ix,ix) == " ") then | ||||
| 				termblit(im[1][iy]:sub(ix,ix), im[2][iy]:sub(ix,ix), im[3][iy]:sub(ix,ix)) | ||||
| 				termblit( | ||||
| 					im[1][iy]:sub(ix,ix), | ||||
| 					im[2][iy]:sub(ix,ix), | ||||
| 					im[3][iy]:sub(ix,ix) | ||||
| 				) | ||||
| 			end | ||||
| 		end | ||||
| 	end | ||||
| @@ -476,7 +543,11 @@ local drawGrid = function(x, y, onlyDrawGrid, useSetVisible) | ||||
| 	end | ||||
| 	for sy = 1, scr_y do | ||||
| 		termsetCursorPos(1,sy) | ||||
| 		termblit(bg[1][sy], bg[2][sy], bg[3][sy]) | ||||
| 		termblit( | ||||
| 			bg[1][sy], | ||||
| 			bg[2][sy], | ||||
| 			bg[3][sy] | ||||
| 		) | ||||
| 	end | ||||
| 	if useSetVisible then | ||||
| 		tsv(true) | ||||
| @@ -797,7 +868,9 @@ local game = function() | ||||
| 	local outcome | ||||
| 	local p, np | ||||
| 	while true do | ||||
| 		if not isHost then | ||||
| 		if isHost then | ||||
| 			sleep(gameDelay) | ||||
| 		else | ||||
| 			os.pullEvent("move_tick") | ||||
| 		end | ||||
| 		p = player[you] | ||||
| @@ -841,9 +914,6 @@ local game = function() | ||||
| 			scrollY = p.y - mathfloor(scr_y / 2) | ||||
|  | ||||
| 			render() | ||||
| 			if isHost then | ||||
| 				sleep(gameDelay) | ||||
| 			end | ||||
| 		end | ||||
| 	end | ||||
| end | ||||
| @@ -853,10 +923,13 @@ local decision | ||||
| local networking = function() | ||||
| 	local evt, side, channel, repchannel, msg, distance | ||||
| 	while true do | ||||
| 		if useSkynet then | ||||
| 			evt, channel, msg = os.pullEvent("skynet_message") | ||||
| 		else | ||||
| 			evt, side, channel, repchannel, msg, distance = os.pullEvent("modem_message") | ||||
| 		if channel == port and repchannel == port and type(msg) == "table" then | ||||
| 		end | ||||
| 		if channel == port and type(msg) == "table" then | ||||
| 			if type(msg.gameID) == "string" then | ||||
| 				 | ||||
| 				if waitingForGame and (type(msg.new) == "number") then | ||||
| 					if msg.new < os.time() then | ||||
| 						isHost = false | ||||
| @@ -883,9 +956,11 @@ local networking = function() | ||||
| 					if not isHost then | ||||
| 						if type(msg.player) == "table" then | ||||
| 							player = msg.player | ||||
| 							if msg.trail then | ||||
| 								for i = 1, #msg.trail do | ||||
| 									putTrailXY(unpack(msg.trail[i])) | ||||
| 								end | ||||
| 							end | ||||
| 							deadGuys = msg.deadGuys | ||||
| 							os.queueEvent("move_tick") | ||||
| 						end | ||||
| @@ -958,4 +1033,8 @@ local main = function() | ||||
| 	end | ||||
| end | ||||
|  | ||||
| main() | ||||
| if useSkynet then | ||||
| 	parallel.waitForAny(main, skynet.listen) | ||||
| else | ||||
| 	main() | ||||
| end | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 LDDestroier
					LDDestroier