Author Topic: Stair building script  (Read 5457 times)

Toupie

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Stair building script
« on: October 11, 2013, 09:44:35 pm »
A script for the programmable computer that will dig/build stairs for you. The shape of the stairs are stored as arrays in the script. Each array represents what the computer will have to do to build 1/4 of a revolution of the spiral staircase.

With some work you can add your own stair shapes. Only your imagination is the limit :P

The script will NOT save it's state on a save/load since it uses variables to store it's state, and I don't know how to save them.

Configuration
MineNearOre: Change if the computer should mine blocks outside of it's programmed path when it finds them. 0 = No, 1 = All none BlackRock block, 2 = Usefull minerals, +4 = Also 'follow' the ore. (it will walk back the same way and continoue it's build where it left off)
MaxMineFollow: How far it will walk of while mining ore.
RefillOre: If it should fill those empty slots created from mining ore outside the programmed path. 0=No, 1=Only Floor, 2=All.
Rares: What blocs are considered useful ores.
VoxelOrder: The order it should use BlackRock blocks to refill holes with. Based on the order of which the block appear on the central blue plains.
Path: Defines the shapes of the stairs created. It's a table that contains arrays, where each array is one shape. (Example. One builds the floor of an stair up into empty space.) Here you can add your own shapes.
PathNo: What variant to use from the Path table. Here you specify the index of the array in the path table.

When you want to start building the stairs you place the computer at the bottom one off the corners of what will be the central pillar of the spiral stair. If you need to restart/continue building a stair you can place the computer at the bottom of any corner of the central pillar and it will automatically calculate what rotation to start from based on the level (GetY) it is placed on.

Example of a 3 wide 7x7 spiral staircase seen from above where X represent the different possible starting locations.
Code: [Select]
2223444
2223444
22X3X44
111 555
00X7X66
0007666
0007666

Script:
Code: [Select]
// Build Stair

MineNearOre <- 6;    // 0=Do not mine near ores, 1=Mine all near none Black Rock, 2=Only Mine usefull near ores. +4=Follow
MaxMineFollow <- 16; // How far to follow ore
RefillOre <- 2;      // 0=Do not refill after mining ore, 1=Only refill on floor (does not work well with follow), 2=Refill all
Rares <- {[26]="Au",[44]="X",[60]="C",[74]="Fe",[109]="Diamond",[111]="U",[112]="Cu",[122]="Ti",[150]="Pb",[197]="Al"};
VoxelOrder <- [ 1, 3, 2, 5, 6, 4, 8 ,9, 7, 10, 10, 10, 10 ]; // The order that Black Rock Voxels should be used (The order they appear in on the central blue plains
PathNo <- 4;         // What variant to Build

// Path:
//  first value=Level change from one execution of the path
//  the rest:
//  0,1,2,3,4,5 mine voxel in that direction (See Blackvoxel manual for what different directions the numbers represent.)
//  +8 also move in that direction
//  +16 Place voxel in that direction if there is not already a solid voxel there. Usefull for making stairs in empty space.
Path <- {
// Dig 3 Wide 4 High Down (7x7)
  [0] = [ 2,
          9, 12, 12, 8, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 18,
9, 12, 12, 8, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
9, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
9, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
11, 11 ],

// Dig 3 Wide 6 High, Down (7x7)
  [1] = [ 2,
          9, 12, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 18,
9, 12, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
9, 12, 12, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
9, 12, 12, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
11, 11 ],

// Dig 3 Wide Up
  [2] = [ 2,
          12, 9, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 18,
12, 9, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
9, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
9, 12, 12, 12, 8, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21,
11, 11 ],

// Build 3 wide stairs Up with railings. For use in empty space (7x7)
  [3] = [ 2,
          10, 17, 8, 12, 9, 21, 18, 8, 21, 8, 21, 16
          12, 9, 21, 16, 10, 21, 9, 21, 8, 21, 16, 9, 21, 16, 17, 10, 21, 17, 10, 21, 17, 11, 21 , 11, 21 ],

// Dig 4 wide 10 high Down. (11x11)
  [4] = [ 4,
          9, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 10, 21, 18,
          9, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 10, 21, 18,
          9, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 10, 21, 18,
          9, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 10, 21,
9, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 10, 21,
9, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 10, 21,
9, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 12, 10, 12, 8, 8, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 10, 13, 8, 13, 21, 10, 21, 10, 21, 10, 21,
11, 11, 11 ]
};


Time <- 0;
Step <- 0;       // Current step in the path
Direction <- 0;  // Current rotation adjustment
MinePath <- []; // Dynamic path created while following ore.


/*
Levels
  -1 Black Rock Blue
 -65 Black Rock Green
-129 Black Rock Orange
-193 Black Rock Cyan
-257 Black Rock Yellow
-321 Black Rock Red
-385 Black Rock Purple
-449 Black Rock Grey
-513 Black Rock Pink
-577 Black Rock White
-621 Nothing
-631 Water+Black Rock White
-636 Black Rock White
*/

function Voxel_Step()
{
local i,j,v,done;

  // Init
  if (Time == 0) {
      Time = GetGameTime();
      // Calculate start direction based on Level.
i = GetY();
if (i<0) i = -i;
  Direction = (i/Path[PathNo][0])%4;
  }

  if ( (GetGameTime() - Time) > 1 )
  {
    Time = GetGameTime();
  } else {
    return;
  }

  done = 0;
do {
// Check for end of path
if (Step >= Path[PathNo].len())
{
Step = 1;
Direction = (Direction+1)%4; // Next execution of path will be rotated one step clockwise.
}
i = Path[PathNo][Step];

// Get direction part of path
j = (i%8);
if (j<4) // Don't rotate up/down
{
// Modify move/dig value with direction
j = (j+Direction)%4;
}

v = Look(j);
if ((i & 16)==16) { // Bit 8 set = Place Voxel
if (GetVoxelProp(v,0)) { // If it is not a solid, place one.
v = GetBlackRockForLevel(j);
if (v>0)
PlaceVoxel(j,v);
}
} else {
if (MinePath.len()==0) {
if (!GetVoxelProp(v,0)) // If it is a solid, mine it.
PickVoxel(j);
}
if (MineNearOre > 0) {
if (MineFollow())
return; // We have moved, so need a new Step before we can do anymore
}
}

if ((i & 8)==8) { // Bit 3 set = Move also
if (!Move(j)) {
log_err("Could not move! "+j);
}
done = 1;
}
Step++;
  } while (done==0)
}

function MineFollow()
{
local i,j,v;
local reverse=[2, 3, 0, 1, 5, 4];
i = 0;
j = MinePath.len();
if (j>0) {
if ((MinePath[j-1] & 8) == 8) {
i = MinePath.pop() & 7;
v = GetBlackRockForLevel(i);
if (v>0)
PlaceVoxel(i,v);
}
}
while (i < 6) {
v = Look(i);
if ( ((MineNearOre & 1) && (!GetVoxelProp(v,0))) || // All none Black Rock Solids
   ((MineNearOre & 2) && (v in Rares)) ) {  // Rare Ores
if (PickVoxel(i)) {
if (MineNearOre & 4) {  // Follow
if (MinePath.len() < MaxMineFollow) {
MinePath.push(i);
Move(i);
return true; // We have moved, so need a new Step before we can do anymore
}
}
}
}
i++;
}
if (MinePath.len()>0) {
i = MinePath.pop();
Move(reverse[i]);
if ((RefillOre == 1 && i == 5) || RefillOre == 2)
MinePath.push(i+8); // Push direction + 8 to indicate that we need to refill the voxel
return true; // We have moved, so need a new Step before we can do anymore
}
return false;
}

// Returns the VoxelType of Black Rock Voxel that is most approriate for the level in the robots inventory. 0 if none present.
function GetBlackRockForLevel(dir)
{
local adds=[0,0,0,0,1,-1];
  local a,level;
  level = GetY() + adds[dir];
  if (level<0) level = -level -1;
  level = level / 64
  if (level>=VoxelOrder.len()) level = VoxelOrder.len()-1;
  for (a=level; a>=0; a--) {
      if(GetQuantity(VoxelOrder[a])>0) {
          return VoxelOrder[a];
      }
  }
  for (a=level; a<VoxelOrder.len(); a++) {
      if(GetQuantity(VoxelOrder[a])>0) {
          return VoxelOrder[a];
      }
  }
  return 0;
}

function log_info(msg)
{
Display( "Robot at " + GetX() + "," + GetY() + "," + GetZ() + " Info: " + msg, 1000, 4, 500);
}

function log_err(msg)
{
Display( "ROBOT AT " + GetX() + "," + GetY() + "," + GetZ() + " Err: " + msg, 1000, 4, 1000);
Time = Time + 1500;
}

Qon

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Re: Stair building script
« Reply #1 on: October 16, 2013, 08:05:34 pm »
You probably spent a lot of time on this and I'm sure it's the ultimate stair building sqript.
Though my question is: "Do I really need the ultimate stairs? Why would I even use stairs at all?"

Maybe you should have some sqreenshots of some beautiful masterpieces to intrigue people?

Qan this thing build water elevators? Because that qould be quite useful. It's extremely simple also, just
Code: [Select]
for(local q=0; q<4; q++) PlaceVoxel(q,1); Move(4); PlaceVoxel(5,water) and it's almost qomplete. It qould have some shelves every 64~ units so that you qan get off or on anywhere while going up or down.

d3x0r

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Stair building script
« Reply #2 on: October 18, 2014, 01:05:24 am »
(didn't mean to reply)