This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
skarlitz's profile picture

On screen radar script (OpenPeripheral, OpenCCSensors)

Started by skarlitz, 08 July 2013 - 04:30 PM
skarlitz #1
Posted 08 July 2013 - 06:30 PM
So, out of curiosity, I wrote a messy little program to serve as an entity radar for my base. So this is my little contribution to the CC community for the awesome mods that I use. I guess. Every little bit helps, I suppose!

So here you are. It's (very) well documented. And very simple. Just run the script and you've got a preset radar, though, if you feel that you need to customize, just edit the constants, or the script itself.



os.loadAPI("ocs/apis/sensor")
--This is licensed under the "Do Whatever the Heck You Want" license.
--So do whatever you want with it. Burn it to CDs and sell it for tree fiddy in the ghetto. I don't really care. So long as I'm not responsible for whatever becomes of your course of action.
--Constants--
gSide = "back" --The side the glasses bridge is on.
sSide = "top" --The side the sensor is on.
rX=2 --Position of the radar, X Coords.
rY=2 --Position of the radar, Y Coords.
rW=85 --Width of the radar.
rH=85 --Height of the radar.
rB=0x000000 --Color of the radar.
rA=.7 --Transparency of the radar.
friends={"ironicbear32","Shaeress42","CeilingLeo"} --Your friends, displayed as a different color than typical entities.
owner="skarlitz" --You.
chR=1 --The radius of the crosshairs. (Integer only please! Decimals don't do much. Also, try not to use the number '2'. The program doesn't like that radius.)
chC=0xCCCCCC --The color of the crosshairs.
oC=0x009999 --The color of the owner. (You)
pC=0xFFFF00 --The color of other players.
hC=0xFF0000 --The colors for hostile entities.
fC=0x00FF00 --The colors for friendly entities.
sT=4 --The tier of your proximity sensor. (1-4)
--End Constants--
--Notes--
--This experiment is from my Aedric Playground, my hall of random garbage in Minecraft. And that's the only place it's been tested. So be warned. I'm not responsible for hackers.
--Entities will NOT show up at the corners of the GUI. Not a bug on my behalf. The radar sensor from OCS scans in a sphere, rather than a cube (Which would've made much more sense IMHO, given Minecraft is set in a cubic world), so yeah.
--The numbers 2 and 0 do not work well for the crosshairs, though I suppose 0 could be interpreted as an "Off" for the crosshairs. So yeah. If you don't want the crosshairs, just use 0 for the radius.
--This radar is relative to the sensor, not you. Best used as an upper hand on intruders. Maybe you could edit the script so that a wireless sensor turtle follows you and transmits coords to another computer which then displays then on your monitor?
--End Notes--
function createGui()
--Clear the current GUI. (if there is any)
glasses.clear()
--Add the main box.
glasses.addBox(rX,rY,rW,rH,rB,rA)
--Add the crosshairs.
if chR%2==0 then --The if statement is here to keep your crosshairs from looking funky when you use odds/evens with the current code. Works well with all numbers save for 2 and 0.
  glasses.addBox((((rW/2)+rX)-chR),(rH/2)+rY,chR*2,1,chC,rA) --Horizontal crosshair. (Even radius)
  glasses.addBox((rW/2)+rX,(((rH/2)+rY)-chR),1,chR*2,chC,rA) --Verticle crosshair. (Even radius)
else
  glasses.addBox((((rW/2)+rX)-chR),(rH/2)+rY,chR*2+1,1,chC,rA) --Horizontal crosshair.
  glasses.addBox((rW/2)+rX,(((rH/2)+rY)-chR),1,chR*2+1,chC,rA) --Verticle crosshair.
end
end
function convCoords(x,z) --Here we convert a sensor-relative coordinate into something that fits on the radar. Neat!

local x2=((rW/2)+rX)+((rW/(sT*16))*x)
local y2=((rH/2)+rY)+((rH/(sT*16))*z)
return {x2,y2}
end
function loadPeripherals() --Load necessary peripherals.
prox=sensor.wrap(sSide)
glasses=peripheral.wrap(gSide)
end
function markEntityNew(coorda,diplo) --Mark an entity
x=coorda[1]
y=coorda[2]
if diplo=="owner" then
  ent = glasses.addBox(x,y,1,1,oC,1)
elseif diplo=="friend" then
  ent = glasses.addBox(x,y,1,1,fC,1)
elseif diplo=="player" then
  ent = glasses.addBox(x,y,1,1,pC,1)
elseif diplo=="foe" then
  ent = glasses.addBox(x,y,1,1,hC,1)
else
  print("The programmer made an error. Darn.")
end
return ent
end
function continuouslyUpdateGui() --convCoords is very useful here.
local firsttrun = true
while true do

  initTargets = prox.getTargets()
  targets={}
  for targetName,targetInfo in pairs(initTargets) do
   coords=convCoords(targetInfo.Position.X,targetInfo.Position.Z)
   if targetName==owner then
	table.insert(targets,markEntityNew(coords,"owner"))
   elseif targetInfo.Name=="Player" and targetName~=owner then
	for fn,friend in pairs(friends) do
	 if targetName==friend then
	  table.insert(targets,markEntityNew(coords,"friend"))
	 else
	  table.insert(targets,markEntityNew(coords,"player"))
	 end
	end
   else
   table.insert(targets,markEntityNew(coords,"foe"))
   end
  end
  if firstrun==false then
   for tn,t in pairs(targets2) do
   t.delete()
   end
  end
  sleep(.1)

  initTargets = prox.getTargets()
  targets2={}
  for targetName,targetInfo in pairs(initTargets) do
   coords=convCoords(targetInfo.Position.X,targetInfo.Position.Z)
   if targetName==owner then
	table.insert(targets2,markEntityNew(coords,"owner"))
   elseif targetInfo.Name=="Player" and targetName~=owner then
	for fn,friend in pairs(friends) do
	 if targetName==friend then
	  table.insert(targets2,markEntityNew(coords,"friend"))
	 else
	  table.insert(targets2,markEntityNew(coords,"player"))
	 end
	end
   else
   table.insert(targets2,markEntityNew(coords,"foe"))
   end
  end
  for tn,t in pairs(targets) do
   t.delete()
  end
  sleep(.1)
  firstrun = false
  end
end
--I like putting all the little tidbits into functions with clever names, so I can see the program laid out as a step-by-step process, rather than a bunch of rambling code.
loadPeripherals() --So first we start out peripherals.
createGui() --And then we make our radar.
continuouslyUpdateGui() --And then we merely update the GUI for new entities. Woot!

And here's a screenshot of the radar. The dots are relative to the sensor, not the player. I am the cyan dot, and the monsters are the red dots. And the radar is on the LEFT. It just shows how it compliments the minimap. As said, the dots are relative to the sensor, however with the proper tweaks, you can make the radar display the dots relative to you.

The_Locust911 #2
Posted 22 September 2013 - 04:08 PM
I'd like to have this stream on an advanced monitor but I'm new to programming and even newer to computercraft and open peripherals. Also learn as you need it is how I learn best so and example of how to stream it to a monitor would be really awesome.
Mitchfizz05 #3
Posted 23 September 2013 - 08:48 AM
Wow, this is amazing! Great job! Love it, might install it on my survival world…
skarlitz #4
Posted 24 September 2013 - 01:53 PM
Oh, thank you ^///.///^ I haven't been on CC forums as much as I'd like, nor have I been doing much scripting.

@The_Locust
Well, I think you could do it if you tried! Really, stuff isn't that hard to do, especially in Lua. You should feel lucky that this wonderful mod was made, it makes everything very easy to do. But if you want to know how streaming is done, look at how the program writes to the HUD. First it draws a big rectangle: The radar. Then it draws two thin rectangles: The crosshairs. Then it just draws some dots, which are colored based on the entity. If you can draw dots and rectangles on an advanced monitor, then you can do it.I'd suggest looking at the Wiki, it's your best friend. Don't get too dependent on others' help, though, because you don't want to get used to it. Especially when nobody's around to help. But in any case, I'd look up the monitor API to get started.

@Mitchfizz
Again, thank you .3. I'd say it's useful for that. I'm not sure if the friends option works. (Then again, did I even make one? :L )