add robot prototype
This commit is contained in:
parent
718c9ba725
commit
d768193275
12
bricks/special/robot-init.lua
Normal file
12
bricks/special/robot-init.lua
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
return function(gate)
|
||||||
|
gate.brickcolor = 0
|
||||||
|
gate.brickcolorfx = 0
|
||||||
|
gate.brickshapefx = 0
|
||||||
|
gate.brickray = 0
|
||||||
|
gate.brickcol = 0
|
||||||
|
gate.brickren = 0
|
||||||
|
gate.waiting = true
|
||||||
|
gate.robotpos = {0, 0, 0}
|
||||||
|
gate.robotdir = 0
|
||||||
|
end
|
31
bricks/special/robot-input.lua
Normal file
31
bricks/special/robot-input.lua
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
local function intToPort(gate, port, len, v)
|
||||||
|
for i = 1, len do
|
||||||
|
local p = math.pow(2, i-1)
|
||||||
|
Gate.setportstate(gate, port+i-1, math.floor(v/p)%2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return function(gate, argv)
|
||||||
|
for word in argv[1]:gmatch("[^\t]+") do
|
||||||
|
local first, rest = word:sub(1, 1), word:sub(2, #word)
|
||||||
|
local vec = {}
|
||||||
|
for a in rest:gmatch("[^ ]+") do table.insert(vec, tonumber(a) or error("invalid number "..a)) end
|
||||||
|
if first=="P" then -- set position
|
||||||
|
assert(#vec==3, "invalid position given to robot: "..word)
|
||||||
|
brick.robotpos = vec
|
||||||
|
elseif first=="B" then -- detected brick info
|
||||||
|
assert(#vec==7, "invalid brick info given to robot: "..word)
|
||||||
|
gate.brickexists = vec[1]
|
||||||
|
gate.brickcolor = vec[2]
|
||||||
|
gate.brickcolorfx = vec[3]
|
||||||
|
gate.brickshapefx = vec[4]
|
||||||
|
gate.brickray = vec[5]
|
||||||
|
gate.brickcol = vec[6]
|
||||||
|
gate.brickren = vec[7]
|
||||||
|
intToPort(gate, 15, 6, gate.brickcolor)
|
||||||
|
Gate.setportstate(gate, 21, gate.brickexists)
|
||||||
|
else error("invalid control word given to robot: "..word) end
|
||||||
|
end
|
||||||
|
gate.waiting = false
|
||||||
|
end
|
63
bricks/special/robot-update.lua
Normal file
63
bricks/special/robot-update.lua
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
|
||||||
|
local function intFromPort(gate, port, len)
|
||||||
|
local v = 0
|
||||||
|
for i = 1, len do
|
||||||
|
v = v + Gate.getportstate(gate, port+i-1)*math.pow(2, i-1)
|
||||||
|
end
|
||||||
|
return v
|
||||||
|
end
|
||||||
|
|
||||||
|
local function rotateVector(vec, rot)
|
||||||
|
if rot==0 then return { vec[1], vec[2], vec[3]}
|
||||||
|
elseif rot==1 then return { vec[2], -vec[1], vec[3]}
|
||||||
|
elseif rot==2 then return {-vec[1], -vec[2], vec[3]}
|
||||||
|
elseif rot==3 then return {-vec[2], vec[1], vec[3]}
|
||||||
|
else error("invalid rot "..rot) end
|
||||||
|
end
|
||||||
|
|
||||||
|
return function(gate)
|
||||||
|
if not gate.waiting then
|
||||||
|
local action = ""
|
||||||
|
if Gate.getportstate(gate, 7)~=0 then -- remove brick
|
||||||
|
action = action.."R\t"
|
||||||
|
end
|
||||||
|
if Gate.getportstate(gate, 8)~=0 then -- plant brick
|
||||||
|
local color = intFromPort(gate, 1, 6)
|
||||||
|
local colorfx = 0
|
||||||
|
local shapefx = 0
|
||||||
|
local ray = 1
|
||||||
|
local col = 1
|
||||||
|
local ren = 1
|
||||||
|
action = action.."P "..color.." "..colorfx.." "..shapefx.." "..ray.." "..col.." "..ren.."\t"
|
||||||
|
end
|
||||||
|
if Gate.getportstate(gate, 22)~=0 then -- detect brick
|
||||||
|
action = action.."D\t"
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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"
|
||||||
|
end
|
||||||
|
|
||||||
|
if action~="" then
|
||||||
|
Gate.cb(gate, action:sub(1, #action-1))
|
||||||
|
gate.waiting = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -28,7 +28,7 @@ datablock fxDTSBrickData(LogicGate_RobotController_Data) {
|
|||||||
|
|
||||||
logicInit = lualogic_readfile($LuaLogic::Path @ "bricks/special/robot-init.lua" );
|
logicInit = lualogic_readfile($LuaLogic::Path @ "bricks/special/robot-init.lua" );
|
||||||
logicUpdate = lualogic_readfile($LuaLogic::Path @ "bricks/special/robot-update.lua");
|
logicUpdate = lualogic_readfile($LuaLogic::Path @ "bricks/special/robot-update.lua");
|
||||||
logicGlobal = lualogic_readfile($LuaLogic::Path @ "bricks/special/robot-global.lua");
|
logicInput = lualogic_readfile($LuaLogic::Path @ "bricks/special/robot-input.lua" );
|
||||||
|
|
||||||
logicUIName = "Robot Controller";
|
logicUIName = "Robot Controller";
|
||||||
logicUIDesc = "Creates and controls a mobile robot that can detect and place bricks";
|
logicUIDesc = "Creates and controls a mobile robot that can detect and place bricks";
|
||||||
@ -146,124 +146,107 @@ datablock fxDTSBrickData(LogicGate_RobotController_Data) {
|
|||||||
logicPortUIName[21] = "Detect Brick";
|
logicPortUIName[21] = "Detect Brick";
|
||||||
};
|
};
|
||||||
|
|
||||||
function LogicGate_RobotController_Data::getRelativeVector(%this, %obj, %vec)
|
function LogicGate_RobotController_Data::LuaLogic_Callback(%this, %brick, %data) {
|
||||||
{
|
%robot = %brick.luaLogicRobot;
|
||||||
%rot = %obj.angleID;
|
if(!isObject(%robot)) { talk("brick " @ %brick @ " has no robot!"); return; }
|
||||||
%x = getWord(%vec, 0);
|
|
||||||
%y = getWord(%vec, 1);
|
|
||||||
%z = getWord(%vec, 2);
|
|
||||||
%ax = %x;
|
|
||||||
switch(%rot)
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
%x = %y;
|
|
||||||
%y = -%ax;
|
|
||||||
case 2:
|
|
||||||
%x = -%x;
|
|
||||||
%y = -%y;
|
|
||||||
case 3:
|
|
||||||
%x = -%y;
|
|
||||||
%y = %ax;
|
|
||||||
}
|
|
||||||
|
|
||||||
return %x SPC %y SPC %z;
|
|
||||||
}
|
|
||||||
|
|
||||||
function LogicGate_RobotController_Data::onDeath(%this, %obj)
|
|
||||||
{
|
|
||||||
if(isObject(%obj.illogicRobot))
|
|
||||||
%obj.illogicRobot.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
function LogicGate_RobotController_Data::onRemove(%this, %obj)
|
|
||||||
{
|
|
||||||
if(isObject(%obj.illogicRobot))
|
|
||||||
%obj.illogicRobot.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
function LogicGate_RobotController_Data::doLogic(%this, %obj)
|
|
||||||
{
|
|
||||||
%x = (!$LBC::Ports::LastBrickState[%obj, 10] && $LBC::Ports::BrickState[%obj, 10]) - (!$LBC::Ports::LastBrickState[%obj, 11] && $LBC::Ports::BrickState[%obj, 11]);
|
|
||||||
%y = (!$LBC::Ports::LastBrickState[%obj, 13] && $LBC::Ports::BrickState[%obj, 13]) - (!$LBC::Ports::LastBrickState[%obj, 12] && $LBC::Ports::BrickState[%obj, 12]);
|
|
||||||
%z = (!$LBC::Ports::LastBrickState[%obj, 9] && $LBC::Ports::BrickState[%obj, 9]) - (!$LBC::Ports::LastBrickState[%obj, 8] && $LBC::Ports::BrickState[%obj, 8]);
|
|
||||||
%vec = %this.getRelativeVector(%obj, %x*0.5 SPC %y*0.5 SPC %z*0.2);
|
|
||||||
|
|
||||||
%robot = %obj.illogicRobot;
|
|
||||||
|
|
||||||
if(%vec !$= "0 0 0")
|
|
||||||
%robot.setTransform(vectorAdd(%robot.getPosition(), %vec));
|
|
||||||
|
|
||||||
%pos = %robot.getPosition();
|
%pos = %robot.getPosition();
|
||||||
|
|
||||||
if(!$LBC::Ports::LastBrickState[%obj, 21] && $LBC::Ports::BrickState[%obj, 21])
|
initContainerBoxSearch(%pos, "0.49 0.49 0.19", $TypeMasks::FxBrickAlwaysObjectType);
|
||||||
{
|
%tbrick = containerSearchNext();
|
||||||
initContainerBoxSearch(%pos, "0.49 0.49 0.19", $TypeMasks::FxBrickObjectType);
|
|
||||||
if(isObject(%sobj = containerSearchNext()))
|
|
||||||
{
|
|
||||||
%obj.Logic_SetOutput(20, true);
|
|
||||||
|
|
||||||
%color = %sobj.getColorID();
|
%output = "";
|
||||||
for(%i = 0; %i < 6; %i++)
|
|
||||||
%obj.Logic_SetOutput(14+%i, (%color >> %i) & 1);
|
|
||||||
|
|
||||||
%obj.illogicRobotBrick = %sobj;
|
for(%i=0; %i<getFieldCount(%data); %i++) {
|
||||||
}
|
%field = getField(%data, %i);
|
||||||
else
|
%first = getWord(%field, 0);
|
||||||
{
|
|
||||||
%obj.Logic_SetOutput(20, false);
|
|
||||||
|
|
||||||
for(%i = 0; %i < 6; %i++)
|
if(%first $= "R" || %first $= "D") { // remove / detect brick
|
||||||
%obj.Logic_SetOutput(14+%i, false);
|
if(isObject(%tbrick)) {
|
||||||
|
if(%first $= "D") {
|
||||||
%obj.illogicRobotBrick = 0;
|
%field = "B" SPC
|
||||||
|
%tbrick.getColorId() SPC
|
||||||
|
%tbrick.getColorFxID() SPC
|
||||||
|
%tbrick.getShapeFxId() SPC
|
||||||
|
%tbrick.isRaycasting() SPC
|
||||||
|
%tbrick.isColliding() SPC
|
||||||
|
%tbrick.isRendering();
|
||||||
|
%output = %output @ %field @ "\t";
|
||||||
|
} else if(%first $= "R") {
|
||||||
|
%tbrick.schedule(0, delete);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if(%first $= "P") { // plant brick
|
||||||
if(!$LBC::Ports::LastBrickState[%obj, 6] && $LBC::Ports::BrickState[%obj, 6])
|
%nbrick = new fxDTSBrick() {
|
||||||
{
|
|
||||||
initContainerBoxSearch(%pos, "0.49 0.49 0.19", $TypeMasks::FxBrickObjectType);
|
|
||||||
if(isObject(%sobj = containerSearchNext()) && %sobj.getGroup() == nameToID(LuaLogic_RobotBrickGroup))
|
|
||||||
%sobj.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!$LBC::Ports::LastBrickState[%obj, 7] && $LBC::Ports::BrickState[%obj, 7])
|
|
||||||
{
|
|
||||||
for(%i = 0; %i < 6; %i++)
|
|
||||||
%color += mPow(2, %i) * $LBC::Ports::BrickState[%obj, %i];
|
|
||||||
|
|
||||||
%brick = new fxDTSBrick()
|
|
||||||
{
|
|
||||||
datablock = brick1x1fData;
|
datablock = brick1x1fData;
|
||||||
position = %pos;
|
position = %pos;
|
||||||
colorID = %color;
|
colorId = getWord(%field, 1);
|
||||||
|
colorFxId = getWord(%field, 2);
|
||||||
|
shapeFxId = getWord(%field, 3);
|
||||||
isPlanted = 1;
|
isPlanted = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
%err = %brick.plant();
|
%err = %nbrick.plant();
|
||||||
if(%err != 0 && %err != 2)
|
if(%err != 0 && %err != 2) {
|
||||||
%brick.delete();
|
%nbrick.delete();
|
||||||
else
|
} else {
|
||||||
{
|
%nbrick.setRaycasting(getWord(%field, 4));
|
||||||
LuaLogic_RobotBrickGroup.add(%brick);
|
%nbrick.setColliding (getWord(%field, 5));
|
||||||
%brick.setTrusted(true);
|
%nbrick.setRendering (getWord(%field, 6));
|
||||||
|
|
||||||
|
LuaLogic_RobotBrickGroup.add(%nbrick);
|
||||||
|
%nbrick.setTrusted(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lualogic_sendinput(%brick, 1, trim(%output));
|
||||||
}
|
}
|
||||||
|
|
||||||
function LogicGate_RobotController_Data::Logic_onGateAdded(%this, %obj)
|
function LogicGate_RobotController_Data::getRelativeVector(%this, %obj, %vec) {
|
||||||
{
|
%rot = %obj.angleID;
|
||||||
if(isObject(%obj.illogicRobot))
|
%x = getWord(%vec, 0); %y = getWord(%vec, 1); %z = getWord(%vec, 2);
|
||||||
%obj.illogicRobot.delete();
|
%ax = %x;
|
||||||
|
switch(%rot) {
|
||||||
|
case 1: %x = %y; %y = -%ax;
|
||||||
|
case 2: %x = -%x; %y = -%y ;
|
||||||
|
case 3: %x = -%y; %y = %ax;
|
||||||
|
}
|
||||||
|
return %x SPC %y SPC %z;
|
||||||
|
}
|
||||||
|
|
||||||
|
function LogicGate_RobotController_Data::createRobot(%this, %obj) {
|
||||||
|
if(isObject(%obj.luaLogicRobot))
|
||||||
|
%obj.luaLogicRobot.delete();
|
||||||
|
|
||||||
%pos = %obj.getPosition();
|
%pos = %obj.getPosition();
|
||||||
%rpos = vectorAdd(%pos, %this.getRelativeVector(%obj, "0.25 7.75 0"));
|
%rpos = vectorAdd(%pos, %this.getRelativeVector(%obj, "0.25 7.75 0"));
|
||||||
%robot = new StaticShape()
|
%robot = new StaticShape() {
|
||||||
{
|
datablock = LuaLogic_RobotShapeData;
|
||||||
datablock = LuaLogic_BrickData;
|
|
||||||
position = %rpos;
|
position = %rpos;
|
||||||
};
|
};
|
||||||
%robot.setNodeColor("ALL", "1 1 1 1");
|
%robot.setNodeColor("ALL", "1 1 1 1");
|
||||||
missionCleanup.add(%robot);
|
missionCleanup.add(%robot);
|
||||||
|
|
||||||
%obj.illogicRobot = %robot;
|
%obj.luaLogicRobot = %robot;
|
||||||
|
|
||||||
|
lualogic_sendinput(%brick, 1, "P" @ %rpos);
|
||||||
|
}
|
||||||
|
|
||||||
|
function LogicGate_RobotController_Data::onDeath(%this, %obj) {
|
||||||
|
if(isObject(%obj.luaLogicRobot))
|
||||||
|
%obj.luaLogicRobot.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
function LogicGate_RobotController_Data::onRemove(%this, %obj) {
|
||||||
|
if(isObject(%obj.luaLogicRobot))
|
||||||
|
%obj.luaLogicRobot.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
function LogicGate_RobotController_Data::onPlant(%this, %obj) {
|
||||||
|
if(!isObject(%obj.luaLogicRobot))
|
||||||
|
%this.createRobot(%obj);
|
||||||
|
}
|
||||||
|
function LogicGate_RobotController_Data::onLoadPlant(%this, %obj) {
|
||||||
|
if(!isObject(%obj.luaLogicRobot))
|
||||||
|
%this.createRobot(%obj);
|
||||||
}
|
}
|
||||||
|
@ -30,17 +30,19 @@ package lualogic_rom_update {
|
|||||||
};
|
};
|
||||||
schedule(0, 0, activatePackage, lualogic_rom_update);
|
schedule(0, 0, activatePackage, lualogic_rom_update);
|
||||||
|
|
||||||
function lualogic_rom_updatebrick(%brick, %instant) {
|
function lualogic_rom_updatebrick(%brick) {
|
||||||
cancel(%brick.lualogic_rom_updatebrick_schedule);
|
cancel(%brick.lualogic_rom_updatebrick_schedule);
|
||||||
if(%instant) lualogic_rom_updatebrick_final(%brick);
|
%brick.lualogic_rom_updatebrick_schedule = schedule(100, 0, lualogic_rom_updatebrick_final, %brick, %brick.getWorldBox());
|
||||||
else %brick.lualogic_rom_updatebrick_schedule = schedule(100, 0, lualogic_rom_updatebrick_final, %brick);
|
|
||||||
}
|
}
|
||||||
function lualogic_rom_updatebrick_final(%brick) {
|
function lualogic_rom_updatebrick_final(%brick, %box) {
|
||||||
if(!isObject(%brick)) return;
|
if(isObject(%brick)) {
|
||||||
if(%brick.lualogic_rom_delete) { if(%brick.lualogic_rom_deletedone) return; %brick.lualogic_rom_deletedone = 1; }
|
if(%brick.lualogic_rom_delete) {
|
||||||
|
if(%brick.lualogic_rom_deletedone) return;
|
||||||
|
%brick.lualogic_rom_deletedone = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// update any roms below
|
// update any roms below
|
||||||
%box = %brick.getWorldBox();
|
|
||||||
%boxl = getWords(%box, 0, 2); %boxlz = getWord(%boxl, 2);
|
%boxl = getWords(%box, 0, 2); %boxlz = getWord(%boxl, 2);
|
||||||
%boxh = getWords(%box, 3, 5);
|
%boxh = getWords(%box, 3, 5);
|
||||||
%boxs = vectorSub(%boxh, %boxl);
|
%boxs = vectorSub(%boxh, %boxl);
|
||||||
@ -60,10 +62,12 @@ function lualogic_rom_updatebrick_final(%brick) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if rom, update
|
// if rom, update
|
||||||
|
if(isObject(%brick)) {
|
||||||
if(%brick.lualogic_rom_delete) return;
|
if(%brick.lualogic_rom_delete) return;
|
||||||
if(%brick.getDatablock().isLogicRom) {
|
if(%brick.getDatablock().isLogicRom) {
|
||||||
lualogic_rom_updatedata(%brick);
|
lualogic_rom_updatedata(%brick);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function lualogic_rom_updatedata(%brick) {
|
function lualogic_rom_updatedata(%brick) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user