From 8edbb986c79ebec3ecb329a201a8b8f1e709899e Mon Sep 17 00:00:00 2001 From: Redo Date: Fri, 28 Oct 2022 16:28:16 -0600 Subject: [PATCH] add robot prototype --- bricks/special/robot-init.lua | 1 - bricks/special/robot-input.lua | 3 --- bricks/special/robot-update.lua | 25 ++++++++----------------- bricks/special/robot.cs | 32 +++++++++++++++++++------------- 4 files changed, 27 insertions(+), 34 deletions(-) diff --git a/bricks/special/robot-init.lua b/bricks/special/robot-init.lua index fc56888..0bd3480 100644 --- a/bricks/special/robot-init.lua +++ b/bricks/special/robot-init.lua @@ -1,6 +1,5 @@ return function(gate) - print("robot init") gate.brickcolor = 0 gate.brickcolorfx = 0 gate.brickshapefx = 0 diff --git a/bricks/special/robot-input.lua b/bricks/special/robot-input.lua index a066443..5cd735a 100644 --- a/bricks/special/robot-input.lua +++ b/bricks/special/robot-input.lua @@ -7,10 +7,7 @@ local function intToPort(gate, port, len, v) end return function(gate, argv) - print(table.concat(argv, "|\n")) - print("robot input") local arg = argv[1]:gsub("^ +", ""):gsub(" +$", "") - print("\""..arg.."\"") for word in arg:gmatch("[^\t]+") do local first, rest = word:sub(1, 1), word:sub(2, #word) local vec = {} diff --git a/bricks/special/robot-update.lua b/bricks/special/robot-update.lua index 1b9d184..e76f4ca 100644 --- a/bricks/special/robot-update.lua +++ b/bricks/special/robot-update.lua @@ -16,9 +16,7 @@ local function rotateVector(vec, rot) end return function(gate) - print("robot update") if not gate.waiting then - print("not waiting") local action = "" if Gate.getportstate(gate, 7)~=0 then -- remove brick action = action.."R\t" @@ -37,33 +35,26 @@ return function(gate) end local movePos = {0, 0, 0} - if Gate.getportstate(gate, 9)~=0 then movePos[3] = movePos[3]-1 end -- down - if Gate.getportstate(gate, 10)~=0 then movePos[3] = movePos[3]+1 end -- up - if Gate.getportstate(gate, 11)~=0 then movePos[3] = movePos[1]+1 end -- right - if Gate.getportstate(gate, 12)~=0 then movePos[3] = movePos[1]-1 end -- left - if Gate.getportstate(gate, 13)~=0 then movePos[3] = movePos[2]-1 end -- back - if Gate.getportstate(gate, 14)~=0 then movePos[3] = movePos[2]+1 end -- fore + if Gate.getportstate(gate, 9)~=0 then movePos[3] = movePos[3]-0.2 end -- down + if Gate.getportstate(gate, 10)~=0 then movePos[3] = movePos[3]+0.2 end -- up + if Gate.getportstate(gate, 11)~=0 then movePos[1] = movePos[1]+0.5 end -- right + if Gate.getportstate(gate, 12)~=0 then movePos[1] = movePos[1]-0.5 end -- left + if Gate.getportstate(gate, 13)~=0 then movePos[2] = movePos[2]-0.5 end -- back + if Gate.getportstate(gate, 14)~=0 then movePos[2] = movePos[2]+0.5 end -- forward local moveRotation = 0 --if Gate.getportstate(gate, 13)~=0 then moveRotation = moveRotation+1 end -- right --if Gate.getportstate(gate, 14)~=0 then moveRotation = moveRotation-1 end -- left gate.robotdir = (gate.robotdir + moveRotation)%4 - if movePos[1]~=0 or movePos[2]~=0 or movePos[3]~=0 then + if movePos[1]~=0 or movePos[2]~=0 or movePos[3]~=0 or moveRotation~=0 then movePos = rotateVector(movePos, gate.robotdir) gate.robotpos = { gate.robotpos[1]+movePos[1], gate.robotpos[2]+movePos[2], gate.robotpos[3]+movePos[3] } - action = action.."M "..gate.robotpos[1].." "..gate.robotpos[2].." "..gate.robotpos[3].."\t" - end - if moveRotation~=0 then - action = action.."A "..gate.robotdir.."\t" + action = action.."M "..gate.robotpos[1].." "..gate.robotpos[2].." "..gate.robotpos[3].." "..gate.robotdir.."\t" end if action~="" then - print("robot action") - print(action) Gate.cb(gate, action:sub(1, #action-1)) gate.waiting = true end - else - print("waiting") end end diff --git a/bricks/special/robot.cs b/bricks/special/robot.cs index 0abfe85..fd81135 100644 --- a/bricks/special/robot.cs +++ b/bricks/special/robot.cs @@ -1,12 +1,3 @@ -if(!isObject(LuaLogic_RobotBrickGroup)) { - new SimGroup(LuaLogic_RobotBrickGroup) - { - bl_id = 12345678; - name = "\c1Robot"; - QuotaObject = GlobalQuota; - }; - mainBrickGroup.add(LuaLogic_RobotBrickGroup); -} datablock StaticShapeData(LuaLogic_RobotShapeData) { shapeFile = $LuaLogic::Path @ "bricks/special/cube.dts"; @@ -156,8 +147,6 @@ datablock fxDTSBrickData(LogicGate_RobotController_Data) { }; function LogicGate_RobotController_Data::LuaLogic_Callback(%this, %brick, %data) { - talk("robot cb " @ %brick SPC %data); - %robot = %brick.luaLogicRobot; if(!isObject(%robot)) { talk("brick " @ %brick @ " has no robot!"); return; } %pos = %robot.getPosition(); @@ -209,6 +198,12 @@ function LogicGate_RobotController_Data::LuaLogic_Callback(%this, %brick, %data) LuaLogic_RobotBrickGroup.add(%nbrick); %nbrick.setTrusted(1); } + } else if(%first $= "M") { // move + %pos = getWords(%field, 1, 3); + %rot = getWord(%field, 4); + %robot.setTransform(%pos SPC "0 0 1" SPC (%rot*$pi/2)); + } else { + talk("invalid robot callback " @ %field); } } @@ -227,7 +222,20 @@ function LogicGate_RobotController_Data::getRelativeVector(%this, %obj, %vec) { return %x SPC %y SPC %z; } +function LogicRobot_CreateBrickGroup() { + if(!isObject(LuaLogic_RobotBrickGroup)) { + new SimGroup(LuaLogic_RobotBrickGroup) { + bl_id = 12345678; + name = "\c1Robot"; + QuotaObject = GlobalQuota; + }; + mainBrickGroup.add(LuaLogic_RobotBrickGroup); + } +} + function LogicGate_RobotController_Data::createRobot(%this, %obj) { + LogicRobot_CreateBrickGroup(); + if(isObject(%obj.luaLogicRobot)) %obj.luaLogicRobot.delete(); @@ -246,13 +254,11 @@ function LogicGate_RobotController_Data::createRobot(%this, %obj) { } function LogicGate_RobotController_Data::Logic_onRemove(%this, %obj) { - talk("robot remove"); if(isObject(%obj.luaLogicRobot)) %obj.luaLogicRobot.delete(); } function LogicGate_RobotController_Data::Logic_onPlant(%this, %obj) { - talk("robot plant"); if(!isObject(%obj.luaLogicRobot)) %this.createRobot(%obj); }