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

KaoS' Programs and utilities

Started by KaoS, 16 January 2013 - 12:43 AM
KaoS #1
Posted 16 January 2013 - 01:43 AM
I will be posting an updated summary of the public programs and utilities I make here for those who are interested, where I deem necessary the pastebin will include an example of usage. You can steal/modify/copy any of my utilities without permission or credit. I like credit but I will not make people give me credit if they don't want to

In order of creation (will start with messy, unprofessional code, sorry)

SCREEN BUFFER:
SpoilerAfter running this program the term api is modified, as you write to the screen it records what has been written in a table. call term.getTextColor() and term.getBackgroundColor() to get the current colors and use term.getTextAt(x,y,[len]) to return whatever text is at those co-ordinates on the screen, it also returns the text color and the background color at x,y

BACKGROUND REDNET:
SpoilerAfter executing this program shell is re-launched and a rednet coroutine is run, you can then call myNet.send(message,id) to send a message to the specified ID, if ID is omitted it sends to all computers, use myNet.receive() to receive messages, while this is running all myNet messages will be automatically forwarded through computers and turtles running it so if you have 2 computers far away from eachother and a bunch of wireless turtles doing absolutely anything in between then it will forward through those turtles and get to the second computer. name the file startup and it will run startup2 after execution

ADVANCED COMPUTER UTILITIES:
SpoilerA collection of functions I used in a few instances that I think may be useful. you can then call term.getBackgroundColor() and term.getTextColor() to recall the current color configuration, draw(x,y,colour) will colour the box at x,y the specified colour, then it has 2 functions for mouse listening, make a table
a={}
and use it in the first function which adds areas to the table, those areas will be used in the next function, call addArea(table,x1,y1,x2,y2,function) and it will write the area x1-x2; y1-y2 to your table to call function when it is selected, then call listen(table) to listen to the areas in that table, if someone clicks in one of those areas it will execute function with the parameters: (xCD,yCD,button) where xCD and yCD are the exact co-ordinates of the click and button is the mouse button used.
EXAMPLE
myfunc=function(x,y,btn)
  print("you pressed button "..btn.." at CDS: ["..x..", "..y.."]")
end
myT={}
addArea(myT,2,2,5,5,myfunc)
listen(myT)
then click at 2,3 and it will print "you pressed button 1 at CDS [2, 3]" but if you click outside of the area 2-5;2-5 it will do nothing

TOP LEVEL COROUTINE EDIT:
SpoilerAfter running this program the top level shell will be terminated, a new instance of shell, rednet and a messaging coroutine will then run, call addCo(function,ref) to add a function to the coroutine stack which will then be run with the others, each coroutine is assigned a reference which can be used to remove it, you can assign it with ref or it will assign you a numeric reference that will be returned by the addCo function, then call remCo(ref) to cancel a coroutine, a coroutine cannot cancel itself. if a coroutine ends you will be notified at the top right of the screen, call os.queueEvent("message",message) to show the message for 2 seconds at the top of the screen

2D PATHFINDER:
SpoilerAn intelligent pathfinder function. pass it a table in the correct format and the co-ordinates you want to get to (you start at 1,1) and it will find the path to it and will print it in the form of a string. for example
"lluurr"
means go left twice, up twice and right twice
this function was inspired by kylergs' post in Ask a Pro

3D PATHFINDER:
SpoilerExactly the same as the 2D version for a 3D map, once again thanks to kylergs for reminding me to build this and thanks to TheOriginalBIT for this post :D/>

PATHFINDER API:
SpoilerThis is the API version of the 3D pathfinder, it includes 2 functions, a ever so slightly modified version of the path function (just variable renames) and an edit function which you can use to create compatible maps or edit maps already created with it. use the code

local mymap=apiname.edit()
to start the creator, use the arrow keys to navigate and press 0 to place an empty space, 1 to place a wall, + to move up a layer, - to move down a layer, delete to remove the current layer, insert to add a new layer. once the map is done press "s" to close the editor and the create function will return the map. call apiname.edit(mymap) to then modify that map. then use

apiname.path(mymap,targetx,targety,targetz)
to path through the map and search for a path to [targetx,targety,targetz] from [1,1,1]. it will return it in string form as described in the 2D pathfinder or it will return false if you cannot get to the target
the visual display is disabled but still coded in this one. just remove the comments on line 219 and 220

if you are using this to pathfind your CC turtles you can use the included follow command to make your turtle follow the path. use
apiname.follow(path,[direction]) where path is the path returned by apiname.path and the direction is "u", "d", "l" or "r" (defaults to "d") and the turtle will follow the path given. please note that consecutive layers go DOWNWARDS not up

LAYER API:
Spoilerrun this file as a program and it changes the term API to be redirected to my layer rendering system. basically it behaves just like the screen logger (except less buggy and uses tables for the rows recording rather than strings) and then you can call term.newLayer(<index>,tParams) to create a new rendering layer at z-level <index>.
tParams is a table of options, valid options are:

showscroll
scrolling
linked
showscroll renders a scroll-bar on the right hand side of the screen
scrolling enables or disables layer.scroll()
linked links the layer to the term layer, all linked layers scroll together

each layer is like a separate term, it has its own cursor pos, record, text color etc. here is a usage example

local myLayer=term.newLayer(5,{linked=true})
myLayer.setCursorPos(5,5)
myLayer.write("hi")
and "hi" will appear at 5,5 however it is a layer on its own so term.scroll() will not scroll it up, it will remain there even if you write to its location etc. this way you can design GUIs with displays independant of eachother, you can clear layers rather than the whole screen etc. term.getTextAt still functions as it did in the screen logger, screenshot &amp; loadscreen have been removed though

it also adds term.newWindow(offsetX,offsetY,limX,limY,<index>) so you can create layers limited to the location [offsetX-limX,offsetY-limY], they literally think that their little window is the entire screen so window.getSize() returns the size of the window and window.setCursorPos(1,1) sets the position to the origin of the window, not actually [1,1].

layer.write now accepts an additional 2 parameters (both are booleans), the first is whether it automatically scrolls onto the next line rather than print over the edge of the page, the second is whether it should auto-indent and scroll to the same x co-ordinate it started at rather than x=1

it has since been updated to include a speed increase and a few more parameters present in the latest KaoShell

KaoShell: HAS BEEN MOVED TO A SEPARATE POST HERE
Edited on 17 July 2013 - 08:18 AM
theoriginalbit #2
Posted 16 January 2013 - 03:03 AM
Firstly very nice set of utilities… :D/> Have a few questions though…

SCREEN LOGGER:
Does it also remember colour changing?

BACKGROUND REDNET:
automatically forwarded through computers and turtles running it so if you have 2 computers far away from eachother and a bunch of wireless turtles doing absolutely anything in between then it will forward through those turtles and get to the second computer.
How have you dealt with signal propagation and bouncing? also does it use a routing protocol such as RIP or OSPF? Or will every computer in range of another computer pick it up and send it even if the target has already got the message?

I would also show my hidden startup file script but it may not go over so well with Cloudy, sorry guys ^_^/>
Is it malicious? :blink:/> :P/>
KaoS #3
Posted 16 January 2013 - 05:14 AM
thanks for the compliment,

1: nope, it's outdated :(/>

2: every computer broadcasts the message once and not again, it ignores the same message for the next second so it doesn't bounce and to prevent spam

3: you know me :D/> it is designed to modify the entire fs system so you cannot see or edit the startup file that is implanted there, great for viruses
theoriginalbit #4
Posted 16 January 2013 - 11:33 AM
2: every computer broadcasts the message once and not again, it ignores the same message for the next second so it doesn't bounce and to prevent spam
Ahh i see that now in the code, kept looking over it… lol…

3: you know me :D/> it is designed to modify the entire fs system so you cannot see or edit the startup file that is implanted there, great for viruses
Haha nice… yeh I can understand why now, however, should PM it to me, I'm interested in seeing it ;)/>
KaoS #5
Posted 26 January 2013 - 04:28 AM
I have updated the screen logger, it is now more efficient, has a simpler interface and supports text and background colors, I will be updating it soon with a text selector daemon :)/>

EDIT: I accidentally left my testing stuff in there, sorry. it is sorted out now
Edited on 26 January 2013 - 03:39 AM
KaoS #6
Posted 26 January 2013 - 07:20 AM
I updated the screen logger again, fixing the modification of recorded lines, including the altername spelling of color not being cought and adding another two functions. term.screenShot() returns 3 tables, one is the text, one is the text colors of each character and the last is the background colors of each character, call term.loadScreen(t1,t2,t3) to restore from a screenshot however you can specify a color number for t2 or t3 rather than a table if you like and it will use those colors for all text and backgrounds, you can also leave them out and it will use the current colors
KaoS #7
Posted 06 February 2013 - 08:47 PM
updated OP with pathfinder
theoriginalbit #8
Posted 06 February 2013 - 08:59 PM
2D PATHFINDER:

an intelligent pathfinder function. pass it a table in the correct format and the co-ordinates you want to get to (you start at 1,1) and it will find the path to it and will print it in the form of a string. for example
"lluurr"
means go left twice, up twice and right twice
this function was inspired by kylergs' post in Ask a Pro
So its a (x,y) or (z,y) path finder not (x,z) since it returns up.
KaoS #9
Posted 06 February 2013 - 09:08 PM
So its a (x,y) or (z,y) path finder not (x,z) since it returns up.

well it works either way really… any 2 dimensions you like, just use string.gsub on the results. the current results would be (x,y). should I make a 3d version? it wouldn't be hard at all based off this code, I have 90% of the work done
theoriginalbit #10
Posted 06 February 2013 - 09:11 PM
should I make a 3d version?
Yeh would be cool :)/>
KaoS #11
Posted 06 February 2013 - 10:42 PM
should I make a 3d version?
Yeh would be cool :)/>

ok, coded and added. the pastebin includes a test scenario. give it a try :)/>
theoriginalbit #12
Posted 06 February 2013 - 10:54 PM
naw kylergs gets all the credit for the 3D one too :(/> :P/>
KaoS #13
Posted 06 February 2013 - 11:08 PM
naw kylergs gets all the credit for the 3D one too :(/> :P/>

is that better? :P/>

EDIT: a review/opinion would be great :)/>
Edited on 06 February 2013 - 10:10 PM
KaoS #14
Posted 07 February 2013 - 12:03 AM
updated both pathfinders with a minor fix to correct the map render. please note that the sleep command at the end of the loop is entirely unnecessary and is only there so you can see it pathfinding, you should never get a too long without yielding error on it as it completes in under a second without the sleep command

EDIT: please note that the 3d pathfinder has a few issues with direction preference. this can be seen when using the map:


{{
"00000000000000000010",
"00000000000000000001",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000"
},
{
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"000000000000000000000"
},
{
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000",
"00000000000000000000"
}}
Edited on 06 February 2013 - 11:11 PM
theoriginalbit #15
Posted 07 February 2013 - 01:03 AM
is that better? :P/>

EDIT: a review/opinion would be great :)/>
Haha I was joking but thanx :P/>

Yeh ill check it out properly soon when I get power back.
KaoS #16
Posted 07 February 2013 - 01:04 AM
okay, the direction preference issue has been resolved. it will prefer to move in the x-axis, then the y-axis, then the z-axis when it is moving toward the target but when searching for another path it will prefer to use the axis with the lowest displacement thereby avoiding using only one axis
KaoS #17
Posted 08 February 2013 - 05:38 AM
updated OP with the API form which includes a new map creating function

EDIT: another update with a minor bugfix and replaced create with edit
Edited on 08 February 2013 - 05:13 AM
theoriginalbit #18
Posted 08 February 2013 - 07:46 AM
Based of the description the "Screen Logger" would be more of a "Screen Buffer" wouldn't it?

2D path finder. I had a play around with it and I love it especially the little render animation you have in the code :)/> looks good though … I also do like how it stops and goes back when it sees that its going to be caught in a loop. seems like a very solid program provided you can supply the map.

3D path finder. Man I cant trick the damn thing into not working or erroring. :P/> Truly fantastic scripts.

the pathfinder api rounds these both out nicely! although when i supply my own map it errors out on line 59… so either the parameter needs to be removed or I need to be corrected on how to run the program with your own map :P/>

Just one query with the pathfinders… the string output, is relational to the map array passed in, meaning that when it moves down on the map it prints d. so that means that 'd' you would actually (for example) use turtle.forward() for since chances are your passing in an (x,z) map not an (x,y) or (z,y)… correct?
KaoS #19
Posted 08 February 2013 - 07:55 AM
Based of the description the "Screen Logger" would be more of a "Screen Buffer" wouldn't it?
you are probably right :)/> corrected


the pathfinder api rounds these both out nicely! although when i supply my own map it errors out on line 59… so either the parameter needs to be removed or I need to be corrected on how to run the program with your own map :P/>
are you passing it a map created with the edit function or one you wrote by hand? at the moment it assumes you have the right metatable, the layer functions and currently selected area. I will change that now


Just one query with the pathfinders… the string output, is relational to the map array passed in, meaning that when it moves down on the map it prints d. so that means that 'd' you would actually (for example) use turtle.forward() for since chances are your passing in an (x,z) map not an (x,y) or (z,y)… correct?
yes that is how it works, it was designed for use with anything, not just turtles. I can make a converter for it if you like

THANKS FOR THE REVIEW :)/>
theoriginalbit #20
Posted 08 February 2013 - 08:04 AM
are you passing it a map created with the edit function or one you wrote by hand? at the moment it assumes you have the right metatable, the layer functions and currently selected area. I will change that now
Ahh ok I was just passing it one from the 2D path finder, or was it the 3D… doesn't matter, makes sense why it was erroring out now :P/>

yes that is how it works, it was designed for use with anything, not just turtles. I can make a converter for it if you like
I knew it was to work with anything ;)/> Well the converter would have to know the relation though, whether the layers represent x, y, or z… unless you just force it to be one way and have an isForTurtle parameter or something.

THANKS FOR THE REVIEW :)/>
Np… someones got to do it :P/> ( look at all the replies, its either me or you :P/> )
KaoS #21
Posted 08 February 2013 - 09:31 AM
ok, updated again, you can now pass handwritten maps to the function and it will convert them, the follow command was added for turtle functionality
kylergs #22
Posted 10 February 2013 - 02:13 PM
Pretty awesome - any chance on pathing from a non-(1,1) location? Also, could you comment your pathfinder code a bit more, I'm having trouble understanding all the logic (but I do understand A* a bit now :)/> )

Also, love the rednet program!
KaoS #23
Posted 10 February 2013 - 04:07 PM
sure, I will implement custom start points and comment the code now. I have also re-worked most of these systems into a single unit that I will post soon. they were designed to work together so they have been modified to do so. at the moment I have a working top level coroutine override with message notification system to notify you of any coroutines ending or any change in peripheral configuration (note system now uses screen logger to display more effectively as it can re-write to the screen what it overwrote), a position tracking system that is a modification of the turtle api, the coroutine override is also used to run my background rednet in addition to making them into mobile gps satellites that don't need to run the gps host file ( as long as 3-4 turtles are in the area you can gps.locate(0.5) ) and finally a complete rewrite of the fs modification I did not post above to prevent any modification of the files it uses, it also hides them and redirects fs.open calls to a separate file so they do not get in the way
KaoS #24
Posted 10 February 2013 - 04:41 PM
ok, I have commented the 3d pathfinder and made it so you can select your start point on execution.

PLEASE NOTE the pathfinder API works better than the 3d pathfinder so once you understand the 3d PF read through the API version to get the best way of doing things…

if you do not understand any part of it please ask. they are here for you guys so you might as well be able to understand them
KaoS #25
Posted 18 February 2013 - 04:37 AM
updated OP with layers addon and the KaoShell project. beta testers needed PLEASE
kylergs #26
Posted 25 February 2013 - 11:32 AM
I was actually just about to recommend you make something similar to that new shell, I would love to Beta test but don't know if i'll have the time to commit (exams). Regardless, great stuff KaoS !

Edit: Just had a look through it and seriously, this is exactly what I was planning to try to make just implemented better than I ever could have done.

Excellent work, I hope I can find the time to work with you on this stuff!
KaoS #27
Posted 28 February 2013 - 05:34 AM
I was actually just about to recommend you make something similar to that new shell, I would love to Beta test but don't know if i'll have the time to commit (exams). Regardless, great stuff KaoS !

Edit: Just had a look through it and seriously, this is exactly what I was planning to try to make just implemented better than I ever could have done.

Excellent work, I hope I can find the time to work with you on this stuff!

Thanks kylergs :)/> if you could find time to test it and post any issues that would be great

EDIT: apologies on the late reply. I was offline for a day

I have also updated the layer API

term.write no longer scrolls automatically. it's usage is term.write(string,bScroll,bIndent) if bScroll is true then it will automatically scroll, if bIndent is true then it will scroll onto the same x-value as it started.

layer creation now accepts a second parameter which is a table of options.

local myLayer=term.newLayer(<z-val eg. 2>,{showscroll=true, linked=true})
the available options are

showscroll
linked
scrolling
showscroll renders a scroll-bar on the right showing how far you have scrolled
linked links the layer to the term layer, all linked layers scroll together
scrolling is whether the layer can scroll at all, layer.scroll() does nothing if scrolling is false

I have also changed how the record works so now if you scroll a layer the text that scrolls out of the top is still available, you can then layer.getTextAt a negative y-value and scroll backwards with layer.scroll(-1)
Edited on 28 February 2013 - 04:43 AM
kylergs #28
Posted 28 February 2013 - 06:58 AM
Pretty cool, I'll try to help out if I get a chance, maybe not for a while though…
KaoS #29
Posted 28 February 2013 - 07:21 AM
thanks anyways :)/>

I have updated KaoShell to use and upgrade the latest layer API, you can now scroll the console with your mouse or by clicking on the rendered up/down arrows, click above the scroll-bar to scroll a page up and below to scroll a page down, the cursor is moved so it remains at the same point on the text (scroll down and it moves up) and writing to that location automatically jumps the screen back to where you can see it. unfortunately the read() command breaks this a sit jumps back to its starting cursor pos which is changed by the scroll, I will have to find a way around that
KaoS #30
Posted 01 March 2013 - 05:39 AM
Once again updated KaoShell, implemented an automatic update system that checks your version and updates if you are behind, the addCo function accepts a third param for whether you should be notified when the coroutine dies and the messaging coroutine's params changed a little as well
KaoS #31
Posted 01 March 2013 - 07:35 AM
ok, edited the update function and ironed out some issues with it. please re-download for correct update features
kylergs #32
Posted 01 March 2013 - 07:37 AM
Cool, I had a thought for the layer api:
How about being able to set an 'active' layer so that any term.* commands would run in that layer (including term.getSize() ) This is just because of the third party programs which you may want to only run in a given layer or window (for example when using gui api's or shell.run() )?
KaoS #33
Posted 01 March 2013 - 07:42 AM
good idea, the layer api has an issue with redirecting. once that is sorted out I will add functionality to redirect to any layer
kylergs #34
Posted 01 March 2013 - 07:43 AM
Awesome, look forward to it
KaoS #35
Posted 01 March 2013 - 09:19 AM
Cool, I had a thought for the layer api:
How about being able to set an 'active' layer so that any term.* commands would run in that layer (including term.getSize() ) This is just because of the third party programs which you may want to only run in a given layer or window (for example when using gui api's or shell.run() )?

ok, you can now term.redirect like normal so normal functionality has been fixed. term.redirect(t) will also call t.redirect() if it exists and term.restore(t) will call t.restore() so that you can design a system that changes when it is redirected to. you can also redirect to any layer now however redirecting to a window will not work correctly, this will be resolved soon (most likely tomorrow as I am going home now)

EDIT: auto update will install this feature in-case you were wondering
Edited on 01 March 2013 - 08:19 AM
Shurhaian #36
Posted 13 March 2013 - 10:47 PM
Having started to expand operations somewhat, I thought it would be a good idea to get turtles to do my harvesting for me - and to be able to give them a specific position for refueling and dropoff stations, I thought GPS would be helpful. But I'd rather the computers it's running on be useful for other things than just as beacons.

So I put KaoShell on four computers in the area (three coplanar, one above them) as /startup, put wireless modems on each, and… now I'm kind of stuck.

I tried setting up their positions with gps host, but they don't seem to record their positions, and they don't seem to passively act as GPS hosts. Too, when I install KaoShell on a new turtle, it doesn't do the "check, move back, check again" step that the OP says they do to establish heading. If I have all four computers running "gps host", I can "gps locate" just fine, but the background functionality doesn't seem to be there. I might have thought I just needed to use functions in Lua rather than use the stock gps program, but stuff that seems like it should be happening automatically, isn't - at least not all of it; the computers do state that they're running KaoShell 1.1.3 and they do switch on their modems without my telling them to, but the GPS functionality doesn't seem to be there. What might I be missing?

I am using the FTB Ultimate pack, which runs CC 1.5.
KaoS #37
Posted 14 March 2013 - 12:10 AM
many apologies… it seems like the system had a few minor issues (I hadn't updated it to the channel system XD) if you boot your turtles up again they will automatically update to KaoShell 1.1.4, determine their position and facing and act as gps hosts. please be sure to avoid standing behind them so they can move back (it cancels the face calculation if it cannot move back) also make sure that the turtle has fuel

EDIT: I have tested it on your modpack to be 100% sure it will work. your modpack automatically enables http so make sure you did not turn it off or you will have to manually redownload
Edited on 13 March 2013 - 11:12 PM
KaoS #38
Posted 16 March 2013 - 12:21 AM
I also feel I should mention that you do not use gps host to set position. you can set/change the position of the turtle by editing the global table 'pos' or you can use turtle.setPos(x,y,z,face) face is a numeric value like shown in the MC debug screen

ALSO I would like suggestions for things you guys would like to see in KaoShell, I have avoided many things to keep it lightweight, forced GUIs etc however if you guys want it I will implement it
KaoS #39
Posted 23 March 2013 - 05:24 AM
I have released a major bugfix caused by a small addon… sorry if things went down temporarily
KaoS #40
Posted 18 May 2013 - 10:09 AM
Layer API has been updated and KaoShell moved to its own post
KaoS #41
Posted 18 May 2013 - 03:04 PM
minor bugfix for the Layer API correcting the redirect and restore functions
Zudo #42
Posted 22 May 2013 - 03:18 AM
Nice!
KaoS #43
Posted 22 May 2013 - 08:41 AM
Nice!

thanks :)/> are you sure you don't have any requests?
MCGamer20000 #44
Posted 09 June 2013 - 11:25 AM
Screen Buffer doesn't seem to change print() or write()
MCGamer20000 #45
Posted 09 June 2013 - 11:45 AM
Screen Buffer doesn't seem to change print() or write()
I was testing if this was possible. It is. I made a file named hi:
Spoiler

oPrint=print
function print(...)
oPrint(...)
oPrint(...)
end
oWrite=write
function write(...)
oWrite("=)")
oWrite(...)
end
Log:
Spoiler

> lua
Interactive Lua prompt.
Call exit() to exit.
lua> function print() end
lua> print()
lua> function write() end
lua> write()
lua> exit()
> lua
Interactive Lua prompt.
Call exit() to exit.
lua> print()
1
lua> exit()
> edit hi
> hi
> lua
Interactive Lua prompt.
Interactive Lua prompt.
Call exit() to exit.
Call exit() to exit.
=)lua> exit()
unobtanium #46
Posted 09 June 2013 - 11:55 AM
I think you have to use term.write(string) and term.setCursorPos(x,y) to get it to work.
KaoS #47
Posted 09 June 2013 - 02:56 PM
print and write work perfectly… unless I borked it in the latest update. Give me a minute here

EDIT: I tested it, you can recall text written with print, print uses term.write and I completely rewrote term.write. Are you sure you didn't term.restore()?
MCGamer20000 #48
Posted 13 June 2013 - 04:10 PM
print and write work perfectly… unless I borked it in the latest update. Give me a minute here

EDIT: I tested it, you can recall text written with print, print uses term.write and I completely rewrote term.write. Are you sure you didn't term.restore()?
Oh. I didn't know that print used term.write. Which is strange because I'm ALWAYS looking at the bios.lua file.
MCGamer20000 #49
Posted 13 June 2013 - 04:23 PM
I'll tell you if I find any glitches.
EDIT: I edited this about 10 times before anyone read it.
Edited on 13 June 2013 - 02:39 PM
theoriginalbit #50
Posted 14 June 2013 - 12:12 AM
Oh. I didn't know that print used term.write. Which is strange because I'm ALWAYS looking at the bios.lua file.
Well technically print uses write, with a new line character (\n) added to the end of the input string, and write uses term.write.
anthonysteck #51
Posted 14 June 2013 - 04:32 AM
best ever :)/>:):)/>:) :D/> :D/> :D/> :D/>
KaoS #52
Posted 15 June 2013 - 08:45 AM
best ever :)/>:) :)/>:) :D/> :D/> :D/> :D/>

haha thanks
MCGamer20000 #53
Posted 02 July 2013 - 02:23 PM
Can I add these to my API kit called DevKit? I'll give you credit.
MCGamer20000 #54
Posted 02 July 2013 - 05:32 PM
Screen Buffer causes CC-EMU to crash when write() is called when the cursor Y pos is set to 1. I've been doing a lot of testing and couldn't find a solution, but I did find the problem:

rep(record,tex)
rep(tcols,string.rep(tS,#tex))
rep(tback,string.rep(bS,#tex))
All three of those seem to cause it to crash. The thing is that, when it crashes the computer gives an error similar to the "Not mounted" error (caused when ComputerCraft is in a folder labeled ComputerCraft1.53 for example) except it then immediately turns off. The error message will most often not show up but if it does, it only shows for a millisecond, too fast for my video recording software to catch the error. So, I don't know what the error is…
MCGamer20000 #55
Posted 02 July 2013 - 08:45 PM
Can you make it so when a layer is rendered, it will only try to render lines that would be onscreen at the time? (Based on layer.scrolled) I tried to do it myself and failed badly.
KaoS #56
Posted 03 July 2013 - 02:08 AM
Can I add these to my API kit called DevKit? I'll give you credit.
Of course. No credit is required but it is appreciated

Screen Buffer causes CC-EMU to crash when write() is called when the cursor Y pos is set to 1. I've been doing a lot of testing and couldn't find a solution, but I did find the problem:

rep(record,tex)
rep(tcols,string.rep(tS,#tex))
rep(tback,string.rep(bS,#tex))
All three of those seem to cause it to crash. The thing is that, when it crashes the computer gives an error similar to the "Not mounted" error (caused when ComputerCraft is in a folder labeled ComputerCraft1.53 for example) except it then immediately turns off. The error message will most often not show up but if it does, it only shows for a millisecond, too fast for my video recording software to catch the error. So, I don't know what the error is…
many apologies. I sorted this out in KaoShell but not in the buffer it seems. I will correct it ASAP

Can you make it so when a layer is rendered, it will only try to render lines that would be onscreen at the time? (Based on layer.scrolled) I tried to do it myself and failed badly.
Isn't that how it works at the moment? please attach a screenshot of what it is doing vs. what it should be doing
KaoS #57
Posted 03 July 2013 - 02:50 AM
Screen Buffer causes CC-EMU to crash when write() is called when the cursor Y pos is set to 1. I've been doing a lot of testing and couldn't find a solution, but I did find the problem:

rep(record,tex)
rep(tcols,string.rep(tS,#tex))
rep(tback,string.rep(bS,#tex))
All three of those seem to cause it to crash. The thing is that, when it crashes the computer gives an error similar to the "Not mounted" error (caused when ComputerCraft is in a folder labeled ComputerCraft1.53 for example) except it then immediately turns off. The error message will most often not show up but if it does, it only shows for a millisecond, too fast for my video recording software to catch the error. So, I don't know what the error is…
I ran it in CC Emu with no issues, I could write to [1,1] [1,-1] and [-1,1]. Please re-download it to make sure you have the right version
You are right, I was using the layer API. I have corrected the issue
Edited on 03 July 2013 - 12:56 AM
MCGamer20000 #58
Posted 05 July 2013 - 12:12 AM
Okay. And I can't take a snapshot but you will understand what I mean if I give a example. Just type these in the shell:

CraftOS 1.5
> lua
Interactive Lua prompt.
Call exit() to exit.
lua> print("Random stuff.")
Random stuff.
1
lua> a=layers[1].record
lua> textutils.serialize(a)
At this point the terminal freezes for a second (at least on CC-EMU) and once it prints the serialized string, write() will freeze the terminal briefly while it tries to render.
MCGamer20000 #59
Posted 05 July 2013 - 12:22 AM
And even if it is set to only display lines that would be on screen, the problem is that it's working with a big array. I've experienced it before when making an improvement to your screen buffer script. The improvement was making it more accurate with colors. If someone did term.set____Color(3) it would store 2 instead (because that is what color would display) making screenShot more accurate. But the problem is it would make a GIANT array with each possible number (1 to 65535) and it lagged when the API was loaded. FYI, the array was like this (Except it was auto-generated):
{ "w", "o", "o", "m", "m", "m", "m", "B", ... }
Anyways, the point is that big arrays slow down ComputerCraft. Also, there's a bug with the screen logger:
local tsym={ ... ,r=8192,a=32768}
Red is missing.
MCGamer20000 #60
Posted 05 July 2013 - 12:51 AM
Also, in the Screen Buffer script:
term.setTextColor=function(col)
        tC=col
        oldt.write(col)
        return oldt.setTextColor(col)
end
Was the oldt.write(col) for testing reasons?
theoriginalbit #61
Posted 05 July 2013 - 01:22 AM
The improvement was making it more accurate with colors. If someone did term.set____Color(3) it would store 2 instead (because that is what color would display) making screenShot more accurate. But the problem is it would make a GIANT array with each possible number (1 to 65535) and it lagged when the API was loaded. FYI, the array was like this (Except it was auto-generated):
{ "w", "o", "o", "m", "m", "m", "m", "B", ... }
Ok so this is a very inefficient way to "normalise" the colours. The first method that came to mind was the following:
Spoiler


--# a function to round a number to the nearest integer
math.round = function( num, idp )
  local mult = 10^(idp or 0)
  if num >= 0 then
	return math.floor(num * mult + 0.5 ) / mult
  end
  return math.ceil(num * mult - 0.5) / mult
end

--# a function to calculate n from the result of 2^n
math.log2 = function( num )
  --# normally we would expect to get a valid colour and use math.floor
  --# however in this case we know we may not get a valid colour so we
  --# use math.round (which we made above) so that we get a valid result
  return math.round( math.log(num) / math.log(2) )
end

--# a function to round an invalid colour to a valid one
local function parseColor( col )
  --# turn the colour from binary scale to a normalized integer between 0-15 with math.log2
  --# then put it back to binary scale with 2 ^
  return 2 ^ math.log2( col )
end

However after performing tests with this code
Spoiler


for i = 2^0, 2^15 do
  print(i)
  h.write('Before: '..i..' After: '..parseColor(i)..'\n')
  os.queueEvent('a')
  coroutine.yield()
end
I discovered that there were inaccuracies on the borders of the lower numbers that wouldn't result in the same colour as is printed.

So I took a peek into CC and read how the devs do their rounding and made the fix the code:
Spoiler


--# a function to calculate n from the result of 2^n
math.log2 = function( num )
  --# we need to floor the result because of how casting a Double to an int works in Java
  return math.floor( math.log(num) / math.log(2) )
end

--# a function to round an invalid colour to a valid one
local function parseColor( col )
  --# turn the colour from binary scale to a normalized integer between 0-15 with math.log2
  --# then put it back to binary scale with 2 ^
  return 2 ^ math.log2( col )
end

And for interests sake made a direct port of the way the CC devs do it too.
Spoiler

local function getHighestBit(group)
  local result = 0
  while group > 0 do
	group = bit.brshift(group, 1)
	result = result + 1
  end
  return result
end

local function parseColor( col )
  if type(col) ~= "number" then
	error( "Expected number, got "..type(col), 2)
  end

  --# this is to replicate casting a Double to an int in Java
  col = math.floor(col)

  if col <= 0 then
	error("Colour out of range", 2)
  end

  col = 16 - getHighestBit( col )

  if col < 0 or col > 15 then
	error("Colour out of range", 2)
  end

  --# this line differs from the ComputerCraft source, it was producing inverted results
  return 2 ^ (15 - col)
end
And testing this with the test code resulted in the correct colour output. It is much more efficient than creating/populating a table, and then performing a lookup.

Anyways, the point is that big arrays slow down ComputerCraft.
Not completely true, I've worked with some pretty big tables with no problems, it is all about optimising.


Lastly, please use the edit button instead of posting multiple replies.
Edited on 04 July 2013 - 11:25 PM
KaoS #62
Posted 05 July 2013 - 08:57 AM
Anyways, the point is that big arrays slow down ComputerCraft
Not really… running for loops through them like serializing does will slow things down, that is natural. I could use a string for each line rather than a table of characters but that is besides the point. it runs nice and fast. You should not be serializing its records. I understand serializing them for transfer between computers but there are better transfer methods…

EDIT: for more clarity on the subject I keep everything even off the screen so that you can scroll back up if you like and get information that has scrolled off the screen. Being able to scoll up and down freely and then resume what you were doing

Red is missing.
Apologies, this is one of my old nub codes from back when I was clueless. Corrected

Was the oldt.write(col) for testing reasons?
Yep. Apologies again and also corrected

Ok so this is a very inefficient way to "normalise" the colours. The first method that came to mind was the following:
In my opinion it shouldn't fix the colors. If you pass an invalid color it should do nothing but maybe you are right
Edited on 05 July 2013 - 07:03 AM
theoriginalbit #63
Posted 05 July 2013 - 09:27 PM
In my opinion it shouldn't fix the colors. If you pass an invalid color it should do nothing but maybe you are right
Oh definitely, imo you shouldn't either. A person should only pass a valid colour. That being said the CC devs did add a colour parser, and bunny was attempting to mimic that, so I was just offering fixes :)/>
Dave-ee Jones #64
Posted 06 July 2013 - 01:44 AM
thanks for the compliment,

1: nope, it's outdated :(/>

2: every computer broadcasts the message once and not again, it ignores the same message for the next second so it doesn't bounce and to prevent spam

3: you know me :D/> it is designed to modify the entire fs system so you cannot see or edit the startup file that is implanted there, great for viruses

You can open it in other ways though…*rubs hands together in a mischievous way* hehehe.

EDIT: How do you make that kind of protection though?
KaoS #65
Posted 06 July 2013 - 02:13 AM
In my opinion it shouldn't fix the colors. If you pass an invalid color it should do nothing but maybe you are right
Oh definitely, imo you shouldn't either. A person should only pass a valid colour. That being said the CC devs did add a colour parser, and bunny was attempting to mimic that, so I was just offering fixes :)/>
Oh I didn't know they fixed it :| my bad lol

thanks for the compliment,

1: nope, it's outdated :(/>

2: every computer broadcasts the message once and not again, it ignores the same message for the next second so it doesn't bounce and to prevent spam

3: you know me :D/> it is designed to modify the entire fs system so you cannot see or edit the startup file that is implanted there, great for viruses

You can open it in other ways though…*rubs hands together in a mischievous way* hehehe.

EDIT: How do you make that kind of protection though?
well a basic version of it is now included in KaoShell. Take a look :)/> In this case fs.old is included because KaoShell is not meant to deny you access to anything at all but without it you could not access the file(s) hidden at all