Jump to content


LordIkol's Content

There have been 199 items by LordIkol (Search limited from 10-February 22)


By content type

See this member's


Sort by                Order  

#147529 Turtle.attack() Rednet Toggle Possible?

Posted by LordIkol on 02 October 2013 - 02:17 AM in Ask a Pro

Hi there sorry have not much time at the moment

I checked my Code again and see that you can reduce it to one while loop cause the parralell is executed in a while true loop you dont need the loop in the atk function.
Depending on your setup you can play around with the sleep command a bit to increase performance.
to make it easier for you to play around with the delay i added the delay as argument
if you start your program without arguments the delay is 1 second. if you want to start it with another delay just use <yourprogramname> x
where x is delay between attacks in seconds

rednet.open("right")
local running = true
args = ... or 1

local function atk()
if running == true then
turtle.attack()
print("attacking")
sleep(tonumber(args))
else
print("wait for command")
sleep(10)
end
end

local function switchit()
event, id, text = os.pullEvent()
if text == "on" then
running = true
elseif text =="off" then
running = false
print("Stop attack")
end
end

while true do
parallel.waitForAny(switchit,atk)
end

Greets Loki



#147346 Turtle.attack() Rednet Toggle Possible?

Posted by LordIkol on 30 September 2013 - 07:16 AM in Ask a Pro

Untested but should give you a clue about how it can work

rednet.open("right")
local running = true

local function atk()
while true do
if running == true then
turtle.attack()
print("attacking")
end
sleep(0.8)
end
end

local function switchit()
event, id, text = os.pullEvent()
if text == "on" then
running = true
elseif text =="off" then
running = false
print("Stop attack")
end
end

while true do
parallel.waitForAny(switchit,atk)
end
end

edit: corrected an error now it should work ;D



#143697 Why Won't The Print Command Work Here?

Posted by LordIkol on 06 September 2013 - 05:58 AM in Ask a Pro

peripheral.call(string side, string method, ...) any
Calls a method on a peripheral. The arguments (apart from side and method) and the return values depend on the method being called. If no peripheral is connected, returns nil.

As you see above the Wiki is saying that the return Value of peripheral.call is depending on the method called.
I suggest if you put a "return true" at the end of your turnOn and shutdown code, it will be returned by peripheral.call also.

will check that when im home.

Greets
Loki



#143696 String manipulation question

Posted by LordIkol on 06 September 2013 - 05:47 AM in Ask a Pro

View PostLordIkol, on 05 September 2013 - 03:27 AM, said:

Edit by Bubba: Keep it PG please.

First i was like Whaat???!! did i use bad language, then i found the "Stuff" :D
Sorry Bubba will watch for that next time.

View PostBigTwisty, on 05 September 2013 - 12:11 PM, said:

-Snip-

Nice code too :)
- you might want to add a number to the term.scroll() else it gives an error when he trys to make a new line.
- if the text has a single word that is longer then the screen it will not be printed. (if its a feature it should announce that this will happen ;) )

And when i checked my own piece of code i found a problem with numbers in the text. (fixed it)
its because i wrote the splitstring function for an other programm where i need numbers to be numbers)

View PostLyqyd, on 06 September 2013 - 12:57 AM, said:

It isn't an example of how he could do that though, because of those exceptions. Your answer has been shown to be inferior. The correct response is to accept the better answer, thank the poster for the correction if you desire, and move on. Ask a Pro is all about the quality of the answers, so attempting to justify a less useful answer isn't helpful. :)

Well spoken, im far away from beeing a Pro, but i try to help the people with their problems. And im always Happy if someone (mostly Bit) is correcting my mistakes or post a solution that makes my solution look toally Ugly :D
The reason is that i can learn something and the person who started the Topic learns something too, so its a Win/Win Situation.

greets
Loki

Edit: Changed something :D



#143584 String manipulation question

Posted by LordIkol on 05 September 2013 - 03:27 AM in Ask a Pro

Hi together,

View PostBlankWolf, on 05 September 2013 - 02:25 AM, said:

Well I hade done somthing similar.
Thats an example how you can do that.
str = "Build an automated cow farm to stockpile leather" -- the string you will print to the screen
termX, termY = term.getSize()
write('1. ')
for word in string.gmatch(x, "%S+") do -- that will give you word by word from the string (wihtout a space)
  x, y = term.getCursorPos()
  if (#word + x > termX) then -- checks if the word will fit to the screen
	print() -- when not go to the next line
  end
  write(word) -- write the word
  if (#word + x < termX) then -- checks if there is space for a space
	write(' ') -- write the space
  end
end

I can't test the code. So sorry if there is any syntax error ^_^/>

im not sure if print works with a monitor. And i think the term.getsize() does not include the textsize parameter. but im not sure about that will test it when im home.
When my suggestion is correct then you can use term.redirect but with that i guess you can not use textscale.

I would create an own print function with a maxchar Limit. just check how much chars you can write per line then use the print function below.
it should do the job but i didnt test it. you have to switch term.xxx to the variable for your Monitor peripheral.

if you want some explenation for the code just ask then i put some comments in.

local sm = {"I hate Cats","Coding HTML for Food","The Internet is for Stuff","Minecraft is a little boring when you not have Computercraft installed"}

local function splistring(x)
local c ={}
local i=1
for word in string.gmatch(x, "[%a%d]+") do
--if tonumber(word) == nil then
c[i]= word
--else
--c[i] = tonumber(word)
--end
i=i+1
end
return c
end

local function newprint(line, row, str, mchars)
local actline = line
if #str > mchars then
local mytext = splistring(str)
local newtext = mytext[1].." "
for i = 2,#mytext do
if #newtext+#mytext[i] < mchars then
newtext = newtext..mytext[i].." "
else
term.setCursorPos(row,actline)
term.write(newtext)
actline = actline+1
newtext = mytext[i].." "
end
end
term.setCursorPos(row,actline)
term.write(newtext)
else
term.setCursorPos(row,actline)
term.write(str)
end
end

term.clear()
newprint(1,2,sm[3],15)

Edit by Bubba: Keep it PG please.



#140885 Vi/Vim-like editor for computers.

Posted by LordIkol on 20 August 2013 - 01:08 AM in Ask a Pro

i recommend this one for ingame editing, my peronal favorite.

http://www.computerc...ame%20%20editor

have fun



#140816 Wait if there will be input in 10 seconds and then move on

Posted by LordIkol on 19 August 2013 - 01:53 PM in Ask a Pro

edit: maybe you did not remove line 7 correctly. you have to remove the end for the if statement also. else it will not work.

like this nothing is printed:


local timer = os.startTimer(10) --#The interval that you would like to wait
while true do
  local event, result = os.pullEvent()
  if event=="timer" and timer==result then --#If a timer event is pulled and its ID matches the one returned by startTimer
	break --#Break out of the loop and continue with code execution
  elseif event=="key" then
	 --print("you pressed a key")
	end
  end

if you uncomment the other line with the print it will say "you pressed a key" everytime you press a key :D



#140808 How To Make Rednet Start Redstone On Another Computer

Posted by LordIkol on 19 August 2013 - 01:31 PM in Ask a Pro

Hi mrjoshey,

i modified your code from above a bit using a table (and remove that oldpassword stuff cause i dont know what it is good for)
hope that helps you with your multiuser thing.

rednet.open("back")
term.clear()
term.setCursorPos(1,1)
users = {} --create a table
users["mrjosheyhalo"] = "code1982" --adding an entry the key is the username and the value the password
users["loki"] = "1234" --adding another user

write("Username: ")
user = read()
if not users[user] then --check if this user exists by checking if the key is in the table
term.clearLine()
write("user does not exist")  --errormessage
sleep(2)
os.reboot() --shutdown
else --if user exists go on
write("Password: ") --ask for password
    p1 = read(".")  --read the password to variable
  if p1 == users[user]  then --check if the password fits with the password value of the given user in the table
   print("Welcome "..user) --rest should be clear
   rednet.send(9,"open",true)
   print("Door Open Please wait")
   sleep(7)
   os.reboot()
	    else
   print("wrong password")
   sleep(2)
		    os.reboot()
	    end
end



#140621 [Tables] Can I Improve This?

Posted by LordIkol on 18 August 2013 - 11:50 AM in Ask a Pro

 theoriginalbit, on 18 August 2013 - 11:33 AM, said:

 LordIkol, on 18 August 2013 - 11:21 AM, said:

 theoriginalbit, on 18 August 2013 - 10:54 AM, said:

How exactly do you intend on them being able to change the command without editing the code?

i make a command for changing like this

local function changecommand(old,new)
for k,v in pairs(tcom) do
  if v == old then
   tcom[k] = new
   cb.tell(Master,"the command for k is now: "..new)
  end
end
end

then the player can say "turtle change rauf hoch". the function above then switches tcom["up"] = "rauf" to tcom["up"] = hoch" and the command is changed.
function in tfunctions["up"] is still the same. but the script now reacts on hoch instead of rauf. and everytime a change is made i would store the table in a file and reload this on startup if the file exists. else just take the standart.
So why don't you have the rename function just nil the old one and add the new one?

Data structure
local commands = {
  ["hello"] = function()
	print("Hello!")
  end
}

Checking commands
if commands[cmd] then
  commands[cmd]()
end

Changing commands
local function changeCommand(old, new)
  if not commands[old] then
	return print("No command with that name")
  end
  commands[new] = commands[old]
  commands[old] = nil
end

Thank you bit, after my last post i came to the same solution :D
sometimes im just thinking to fricking complicated.

thanks a lot for your help i will bother you the next days again when im stuck



#140617 [Tables] Can I Improve This?

Posted by LordIkol on 18 August 2013 - 11:21 AM in Ask a Pro

 theoriginalbit, on 18 August 2013 - 10:54 AM, said:

How exactly do you intend on them being able to change the command without editing the code?

i make a command for changing like this

local function changecommand(old,new)
for k,v in pairs(tcom) do
  if v == old then
   tcom[k] = new
   cb.tell(Master,"the command for k is now: "..new)
  end
end
end

then the player can say "turtle change rauf hoch". the function above then switches tcom["up"] = "rauf" to tcom["up"] = hoch" and the command is changed.
function in tfunctions["up"] is still the same. but the script now reacts on hoch instead of rauf. and everytime a change is made i would store the table in a file and reload this on startup if the file exists. else just take the standart.


 Bubba, on 18 August 2013 - 10:57 AM, said:

So you want to have different language indexes for the same function? One cool thing that Lua tables have to offer is that they allow you to use tables as keys. This allows you to do things like:
local commands = {
  [{["hello"]=true,["hola"]=true}] = function()
	print("I can respond to hello in several different languages.")
  end;
}

local input = read()

for key,func in pairs(commands) do
  if key[input] then
	func()
  end
end

hm that sounds interesting. but when i get it right i would need to define a table for every command so that the user can dynamicly add his command to the corresponding table. not sure if this fits for this project but will play around with this a bit.

another thing that came to my mind is to simply change the key in the table. but i dont know if this is possible without loosing the variable it holds.



#140609 [Tables] Can I Improve This?

Posted by LordIkol on 18 August 2013 - 10:24 AM in Ask a Pro

 MKlegoman357, on 18 August 2013 - 09:46 AM, said:

You could store the commands in one table like so:

-snip-

maybe i don't get your solution correct or i did not explain it correct but the point why im using this two tables is that i want to be able to change the command for "up" "down" etc. maybe its confusing cause in my example the key is the same like the command. will switch this. maybe another user wants german commands so with my solution he could simply change the tcom["up"] to tcom["up"] = "hoch" and the system would still work.

with your solution the command must be the key of the table. but i want that the user has the ability to change the command without editing the code.



#140605 [Tables] Can I Improve This?

Posted by LordIkol on 18 August 2013 - 08:54 AM in Ask a Pro

Hi everyone,

Im working on a script to control my turtle with chat commands. it is working good so far but i wonder if there is a better solution.
I use two tables at the moment cause i want the system to be flexible. so that you can easily change the commands for each action.
so this is how it looks at the moment.

the index of both tables is the same. you could say its the description of the command.
then the table tcom holds the Commands and tfunctions holds the function which is executed when the command is used.

local tcom = {}
tcom["up"] ="rauf"
tcom["down"]  = "runter"

local tfunctions = {}
tfunctions["up"] = moveUp
tfunctions["down"]  = moveDown

then i check the command with the tcom table and if it gets a match i call the function from the functions table with pcall.

for k,v in pairs(tcom) do
   if v == commands[2] then
	pcall(tfunctions[k],tonumber(commands[3]))
   end
  end

now i would like to know if this solution is good or if i could do it better in a different way. maybe just using one table.

another question is i heard using to many functions can make problems. is this true and if so what is too many?

thanks for your help and greets
Loki



#140144 Destroy And Place Block With Redstone Signal

Posted by LordIkol on 16 August 2013 - 02:01 AM in Ask a Pro

corrected my mistake,
and yes, waiting for redstone event is much better cause then you don't need the state part.
i always forget that os.pullEvent for redstone thing

thanks bit you are the master :D



#140142 Destroy And Place Block With Redstone Signal

Posted by LordIkol on 16 August 2013 - 01:35 AM in Ask a Pro

hi thijs,

not tested but sth like this should work.

local status = true
while true do
mysignal = redstone.getInput("back") --checks redstone signal at the back
  if mysignal == true and status == true then  --if signal true and status true digs the cable (status true is just to make sure he is not digging whole time)
   turtle.dig() --dig the cable in front
   status = false --set status to false means network is off
  elseif mysignal == false and status == false then -- if no redstone signal and network is off
   turtle.select(1) -- select slot with the cable
   turtle.place()  --place the cable
   status = true --remember that system is on now.
  end  -- end if
sleep(1) --sleep 1 second
end --end while



#140060 Not fixable Bug!

Posted by LordIkol on 15 August 2013 - 10:46 AM in Ask a Pro

ja sorry mein Fehler.
ist einfach die aterm struktur die nicht aufgeht.
hab nicht dran gedacht das die Loop auch nach shell.run() noch weiterläuft.
dennoch ist das Problem das Terminal Programm an sich.

Wenn die While schleife im Desk durch break beendet wird muss er eine neue schleife im Terminal haben die das Programm am laufen hält und die gewünschten Terminal Befehle abfängt.

Am besten alle möglichen befehle in ne table packen. nen read() machen und dann ne if abfrage die die Commands table checkt und dann die gewünschte Aktion ausführt.

so wie es jetzt ist wird es mit oder ohne break nicht laufen.

------

Sorry my fault.
i did not think about the fact that shell.run() is not breaking the loop.
but in the end its the structure of his aTerm that is not well made.

If you close the Loop in the ..Desk Programm and you want user defined commands in your Shell you need to start a new loop that caches what the user types in. for Example:
You could put all your Commands in a table. open a while loop thats asking for a command. than reads the input and checks it with the Table.



#140057 Not fixable Bug!

Posted by LordIkol on 15 August 2013 - 09:58 AM in Ask a Pro

Deleted cause my post was not 100% correct.
The break in the Loop is ok just do sth in the new programm

Greets
Loki



#140047 Not fixable Bug!

Posted by LordIkol on 15 August 2013 - 09:23 AM in Ask a Pro

just remove the break command here
-- Schliessen
if XY == "51,1" and button == 1 then
shell.run("ApfelOS/aTerm")
break
end

einfach das break wegmachen

-- Schliessen
if XY == "51,1" and button == 1 then
shell.run("ApfelOS/aTerm")
break -- dieses break weg und es geht. ausserdem macht dein terminal programm nichts ausser sagen das man shut-off eingeben soll.
end



#140035 Tables Help! Open Peripheral Railcraft Tanks

Posted by LordIkol on 15 August 2013 - 07:48 AM in Ask a Pro

Hi Talli,

Basicly you are right with that. but the table indexes are not numeric as far as i know they are strings with the id or name of the scanned entity.
For example a script i use.

local function getMaster()
t = s.getTargets()
for name, basicDetails in pairs(t) do

  if name == "Sir_Block_Alot" then
  print("Found My Master: "..name)

  -- get more details about them
  local Master = s.getTargetDetails(name)

  mHealth = Master.Health
  mx = math.floor(Master.Position.X)
  mz = math.floor(Master.Position.Z)
  my = math.floor(Master.Position.Y)
  -- print their health
  print("Health: "..mHealth)
  print("Position: "..mx.. " / " ..mz.. " / " ..my  )
  print("------")
  return mx, mz, my
  end
end
end

could also be shortened like this:

local function getMaster()
  local Master = s.getTargetDetails("Sir_Block_Alot")

  mHealth = Master.Health
  mx = math.floor(Master.Position.X)
  mz = math.floor(Master.Position.Z)
  my = math.floor(Master.Position.Y)
  -- print their health
  print("Health: "..mHealth)
  print("Position: "..mx.. " / " ..mz.. " / " ..my  )
  print("------")
  return mx, mz, my
  end
end
end

in this case its just not useful cause it will error when i am not in range. for a static setup like tanks it would be no problem.

hope that helps.



#140033 Run Co-Routine With Mouse Click Event

Posted by LordIkol on 15 August 2013 - 07:34 AM in Ask a Pro

maybe post your code then we can give you better help :)



#140032 Movement Functions Not Working

Posted by LordIkol on 15 August 2013 - 07:31 AM in Ask a Pro

you always check if bedrock variable is false but you never set it to false it is set to nil initally.
if you change that part :

local enter = nil
local bedrockF = nil
local bedrockU = nil
local bedrockD = nil
local bedrockL = nil
local bedrockR = nil
local bedrockB = nil
local bedrock = nil

to this:

local enter = false
local bedrockF = false
local bedrockU = false
local bedrockD = false
local bedrockL = false
local bedrockR = false
local bedrockB = false
local bedrock = false

it should work

Greets
Loki



#140023 Keep a touchscreen interface active while executing other functions?

Posted by LordIkol on 15 August 2013 - 04:49 AM in Ask a Pro

Lyqyd is talking about this http://computercraft...e=Term.redirect
That means your Terminal is directly redirected to the monitor instead of wrapping it as perioheral and put things there

greets
Loki



#139900 Sensor Kits

Posted by LordIkol on 14 August 2013 - 05:34 AM in Ask a Pro

View Postimmibis, on 14 August 2013 - 03:02 AM, said:

If you really don't want to use extra variables, you can use
data = select(4, NIR.get(1))
but it's harder to understand what this does if you're reading the code.

ah nice tip didnt know that.



#139899 Keep a touchscreen interface active while executing other functions?

Posted by LordIkol on 14 August 2013 - 05:31 AM in Ask a Pro

hi connerity,

if i get it correct you just want to put in a delay for the different Actions.
So that the User only can do every Action once and then has to wait until its finished.

For Example:
If i press Button1 and the Pump starts i can not press 1 again until 60 seconds have passed. But i could start the Firework with Button 2.
if this is what you want then i would go for the timer Event.

I have not so much time now to make an example code but basicly what you wanna do is.

1. Make a table that contains all buttons and a variable for their state and a variable for a timerid
2. when a button is pressed check the state of that button
3. if state its false tell the user to wait
4. if state is true set the state to false, start a timer event, and store the id of that timer in the table
5. listen for the timer event. if a timer fires the event check its ID with the timerids in your button table
6. if it matches with an id set the state of that button back to true and the timerid to 99999999 or sth like that.

hope thats not too confusing if i find some time later i will make an example

greets
Loki



#139892 Variable Insert Troubble

Posted by LordIkol on 14 August 2013 - 03:36 AM in Ask a Pro

Hi there,

did long time not Code and just a bit spare time at the moment so im not sure if my code is completely correct but maybe it helps you to get the clue.

beside i shortened your code a bit using a function for the repeating stuff.

have fun with it

print("building")
actslot = 1

local function checkslot(sn)
if turtle.getItemCount(sn) == 0  and  actslot < 16 then
  turtle.select(actslot+1)
  actslot = actslot + 1
elseif actslot == 16 then
  print("last slot reached")
end
end
local function placeshit()
  turtle.place()
  turtle.back()
  turtle.place()
  turtle.back()
  turtle.place()
  turtle.back()
  turtle.place()
  turtle.turnRight()
  turtle.place()
  turtle.turnLeft()
  turtle.back()
  turtle.turnRight()
  turtle.forward()
  turtle.place()
  turtle.turnLeft()
  turtle.back()
  turtle.turnLeft()
end

local function moveback(x)
for i=1,x do
  turtle.back()
end
end

for x = 1, 4 do
checkslot(actslot)
  for i = 1,2 do
   placeshit()
   moveback(3)
  end
placeshit()
turtle.up()
	turtle.back()
	turtle.back()
	turtle.back()
end

Edit:
just recognized that a while loop would be better for the checkslot function just use it like this to make shure the next slot is not empty too

local function checkslot(sn)
while turtle.getItemCount(sn) == 0  and  actslot < 16 do
  turtle.select(actslot+1)
  actslot = actslot + 1
end
end

Greets
Loki



#139798 [Noteblocks]Musicmaker

Posted by LordIkol on 13 August 2013 - 01:22 PM in Media

you may not need noteblocks and redstone but you need a second MOD :P
but your Programm is really sweet and i was just not knowing about the iron Noteblocks when i started this Project.

All the best and greets
Loki