add robot prototype

This commit is contained in:
Redo 2022-10-28 16:28:16 -06:00
parent 5ecd2ce020
commit 8edbb986c7
4 changed files with 27 additions and 34 deletions

View File

@ -1,6 +1,5 @@
return function(gate) return function(gate)
print("robot init")
gate.brickcolor = 0 gate.brickcolor = 0
gate.brickcolorfx = 0 gate.brickcolorfx = 0
gate.brickshapefx = 0 gate.brickshapefx = 0

View File

@ -7,10 +7,7 @@ local function intToPort(gate, port, len, v)
end end
return function(gate, argv) return function(gate, argv)
print(table.concat(argv, "|\n"))
print("robot input")
local arg = argv[1]:gsub("^ +", ""):gsub(" +$", "") local arg = argv[1]:gsub("^ +", ""):gsub(" +$", "")
print("\""..arg.."\"")
for word in arg:gmatch("[^\t]+") do for word in arg:gmatch("[^\t]+") do
local first, rest = word:sub(1, 1), word:sub(2, #word) local first, rest = word:sub(1, 1), word:sub(2, #word)
local vec = {} local vec = {}

View File

@ -16,9 +16,7 @@ local function rotateVector(vec, rot)
end end
return function(gate) return function(gate)
print("robot update")
if not gate.waiting then if not gate.waiting then
print("not waiting")
local action = "" local action = ""
if Gate.getportstate(gate, 7)~=0 then -- remove brick if Gate.getportstate(gate, 7)~=0 then -- remove brick
action = action.."R\t" action = action.."R\t"
@ -37,33 +35,26 @@ return function(gate)
end end
local movePos = {0, 0, 0} local movePos = {0, 0, 0}
if Gate.getportstate(gate, 9)~=0 then movePos[3] = movePos[3]-1 end -- down 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]+1 end -- up if Gate.getportstate(gate, 10)~=0 then movePos[3] = movePos[3]+0.2 end -- up
if Gate.getportstate(gate, 11)~=0 then movePos[3] = movePos[1]+1 end -- right if Gate.getportstate(gate, 11)~=0 then movePos[1] = movePos[1]+0.5 end -- right
if Gate.getportstate(gate, 12)~=0 then movePos[3] = movePos[1]-1 end -- left if Gate.getportstate(gate, 12)~=0 then movePos[1] = movePos[1]-0.5 end -- left
if Gate.getportstate(gate, 13)~=0 then movePos[3] = movePos[2]-1 end -- back if Gate.getportstate(gate, 13)~=0 then movePos[2] = movePos[2]-0.5 end -- back
if Gate.getportstate(gate, 14)~=0 then movePos[3] = movePos[2]+1 end -- fore if Gate.getportstate(gate, 14)~=0 then movePos[2] = movePos[2]+0.5 end -- forward
local moveRotation = 0 local moveRotation = 0
--if Gate.getportstate(gate, 13)~=0 then moveRotation = moveRotation+1 end -- right --if Gate.getportstate(gate, 13)~=0 then moveRotation = moveRotation+1 end -- right
--if Gate.getportstate(gate, 14)~=0 then moveRotation = moveRotation-1 end -- left --if Gate.getportstate(gate, 14)~=0 then moveRotation = moveRotation-1 end -- left
gate.robotdir = (gate.robotdir + moveRotation)%4 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) movePos = rotateVector(movePos, gate.robotdir)
gate.robotpos = { gate.robotpos[1]+movePos[1], gate.robotpos[2]+movePos[2], gate.robotpos[3]+movePos[3] } 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" action = action.."M "..gate.robotpos[1].." "..gate.robotpos[2].." "..gate.robotpos[3].." "..gate.robotdir.."\t"
end
if moveRotation~=0 then
action = action.."A "..gate.robotdir.."\t"
end end
if action~="" then if action~="" then
print("robot action")
print(action)
Gate.cb(gate, action:sub(1, #action-1)) Gate.cb(gate, action:sub(1, #action-1))
gate.waiting = true gate.waiting = true
end end
else
print("waiting")
end end
end end

View File

@ -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) { datablock StaticShapeData(LuaLogic_RobotShapeData) {
shapeFile = $LuaLogic::Path @ "bricks/special/cube.dts"; 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) { function LogicGate_RobotController_Data::LuaLogic_Callback(%this, %brick, %data) {
talk("robot cb " @ %brick SPC %data);
%robot = %brick.luaLogicRobot; %robot = %brick.luaLogicRobot;
if(!isObject(%robot)) { talk("brick " @ %brick @ " has no robot!"); return; } if(!isObject(%robot)) { talk("brick " @ %brick @ " has no robot!"); return; }
%pos = %robot.getPosition(); %pos = %robot.getPosition();
@ -209,6 +198,12 @@ function LogicGate_RobotController_Data::LuaLogic_Callback(%this, %brick, %data)
LuaLogic_RobotBrickGroup.add(%nbrick); LuaLogic_RobotBrickGroup.add(%nbrick);
%nbrick.setTrusted(1); %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; 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) { function LogicGate_RobotController_Data::createRobot(%this, %obj) {
LogicRobot_CreateBrickGroup();
if(isObject(%obj.luaLogicRobot)) if(isObject(%obj.luaLogicRobot))
%obj.luaLogicRobot.delete(); %obj.luaLogicRobot.delete();
@ -246,13 +254,11 @@ function LogicGate_RobotController_Data::createRobot(%this, %obj) {
} }
function LogicGate_RobotController_Data::Logic_onRemove(%this, %obj) { function LogicGate_RobotController_Data::Logic_onRemove(%this, %obj) {
talk("robot remove");
if(isObject(%obj.luaLogicRobot)) if(isObject(%obj.luaLogicRobot))
%obj.luaLogicRobot.delete(); %obj.luaLogicRobot.delete();
} }
function LogicGate_RobotController_Data::Logic_onPlant(%this, %obj) { function LogicGate_RobotController_Data::Logic_onPlant(%this, %obj) {
talk("robot plant");
if(!isObject(%obj.luaLogicRobot)) if(!isObject(%obj.luaLogicRobot))
%this.createRobot(%obj); %this.createRobot(%obj);
} }