Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --this is a work in progress program.
- --written by Andrew Phifer
- --version: E.S.T - 0.01 (Early Systems Testing)
- os.unloadAPI("sensors");
- os.unloadAPI("sensorsUI");
- os.loadAPI("rom/apis/sensors");
- os.loadAPI("rom/apis/sensorsUI");
- --reactor[x][1] = Reactor hull heat
- --reactor[x][2] = ice quantity
- --reactor[x][3] = fuel quantity
- --reactor[x][4] = reactor cycle
- --reactor[x][5] = the time when the reactor began the current cycle
- --reactor[x][6] = reactor life (how long till the next fuel rod will burn out)
- --reactor[x][7] = the amount of time the reactor has been running
- --reactor[x][8] = the heat level of the hottest item in the reactor
- --reactor[x][9] = the heat level of the coldest item in the reactor
- --reactor[x][10] = the average heat level of the items in the reactor
- --reactor[x][11] = the current EU/t output of the reactor
- --reactor[x][12] = the total EU output of the reactor for this cycle
- --reactor[x][13] = the X location of the reactor
- --reactor[x][14] = the Y location of the reactor
- --reactor[x][15] = the Z location of the reactor
- --reactorMap[Xpos][Ypos][xItemDetail]
- --reactorMap[Xpos][Ypos][1] = Item Name
- --reactorMap[Xpos][Ypos][2] = Item Id
- --reactorMap[Xpos][Ypos][3] = Item Damage Value
- --reactorMap[Xpos][Ypos][4] = Stack Size
- function netSendUpdate (react)
- --this function sends a rednet message
- --containing a summary of the specified reactor's state
- end
- function netSendReactorMap(react)
- --sends a map of the specifed reactor's contents
- --and all their item's details
- print("sending reactor map!");
- if react == nil then
- return nil;
- end
- local Items = getReactorContent(react);
- local Readout = getReactorInfo(react);
- local reactorSizeX = getStorageX(Readout);
- local reactorSizeY = getStorageY(Readout);
- local item;
- local stack;
- local name;
- local damage;
- local location;
- local object;
- local u = 0;
- local reactorMapString;
- local reactorMap = {};
- --local monitor = peripheral.wrap("bottom");
- --monitor.clear();
- --monitor.setTextScale(1);
- --monitor.setCursorPos(1,4);
- print("variables initialized!");
- print("reactorX: " .. reactorSizeX);
- print("reactorY: " .. reactorSizeY);
- print("sending data...");
- os.sleep(2);
- for x = 0, tonumber(reactorSizeX) do
- for y = 0, tonumber(reactorSizeY) do
- location, item = getItemX(x, y, Items); --gets the data for the item (if there is one) at the specifed x y location
- if item ~= nil then
- --if the inventory slot does have something in it, then do this stuff
- stack = getStack(item);
- name = getName(item);
- damage = getDamage(item);
- reactorMap[u] = location;
- reactorMap[u + 1] = stack;
- reactorMap[u + 2] = name;
- reactorMap[u + 3] = damage;
- u = u + 4; --just a counter to advance the position on "reactorMap[]"
- --object = location .. " " .. stack .. " " .. name .. " " .. damage;
- else
- --this section ensures the client never sees a message with 'nil' in it
- --object = "nothing" .. " " .. "nothing" .. " " .. "nothing" .. " " .. "nothing";
- reactorMap[u] = "0,0";
- reactorMap[u + 1] = "nothing";
- reactorMap[u + 2] = "nothing";
- reactorMap[u + 3] = "nothing";
- u = u + 4; --just a counter to advance the position on "reactorMap[]"
- end
- --monitor.write(object);
- --monitor.scroll(-1);
- end
- end
- reactorMapString = textutils.serialize(reactorMap);
- print("Sucessful Sending?: " .. tostring(rednet.broadcast(reactorMapString)));
- print("end of Map!");
- end
- function getStorageY(Readout)
- --this is just a place holder function, right now, it's only being used for reactors.
- --that means that it should always return "6"
- --in the future however, i might expand this function to be used for regular storage devices
- --which do have varible Y values
- return "6" --because strings are better, and "6" is all i need right now
- end
- function getStorageX(Readout)
- --this function returns the number of inventory slots a certain reactor is wide
- --this function requires a Reactor Info Array to be passed to it
- local reactorX = getReactorSize(Readout);
- return reactorX + 3; --because the width of a reactor is 3 + It's size according to the "getReactorSize()" function
- end
- function getReactorSize(Readout)
- --returns the size of the specified reactor
- --this function requires a Reactor Info Array to be passed to it
- return Readout[10];
- end
- function getHullHeat(Readout)
- --returns the heat level of the reactor hull
- --this function requires a Reactor Info Array to be passed to it
- return Readout[2];
- end
- function getMaxComponentHeat(Items)
- --returns the heat level of the hottest
- --component in the reactor
- --this function requires a Reactor Content Map to be passed to it
- end
- function getLowestComponentHeat(Items)
- --returns the heat level of the coldest
- --componenet in the reactor
- --this function requires a Reactor Content Map to be passed to it
- end
- function getAverageComponentHeat(Items)
- --returns the average heat level
- --of the components in the reactor
- --this function requires a Reactor Content Map to be passed to it
- end
- function getIce(Items)
- --returns the amount of ice in the reactor
- --this function requires an Reactor Content Map to be passed to it
- local count = 0;
- for x = 1, table.maxn(Items), 2 do --this moves through all of the items in the reactor, searching for instances of ice.
- if getName(data[x] + 1) == "" then --need to find out what the actual item code for ice is.
- count = count + 1; --every time the loop finds an instance of Ice in the reactor, it will add one to the counter
- end
- end
- return count;
- end
- function getFuel(Items)
- --returns the amount of urnanium in the reactor
- --this function requires a Reactor Content Map to be passed to it
- local count = 0;
- for x = 1, table.maxn(Items), 2 do --this moves through all of the items in the reactor, searching for instances of uranium.
- if getName(data[x] + 1, Items) == "" then --need to find out what the actual item code for uranium is.
- count = count + 1; --every time the loop finds an instance of Uranium in the reactor, it will add one to the counter
- end
- end
- return count;
- end
- function getName(name)
- --this pulls the item name from the formatted text provided by ccSensors
- if name ~= nil then -- this is just a preventitive messure, if the function is given a 'nil' argument, then it will automaticly return 'nil'
- local stack = string.find(name, "*"); --the "*" denotes the stack indicator, the stack value precedes it
- local damage = string.find(name, "@"); --the "@" denotes the damge value indicator, the damage value follows it
- if stack == nil then -- IDIOT PROOFING: if there is no stack symbol in the argument passed to the function, it will return 'nil'
- return nil;
- end
- if damage == nil then -- IDIOT PROOFING: if there is no damage symbol in the argument passed to the function, it will return 'nil'
- return nil;
- end
- return string.sub(name, stack + 1, damage - 1); --this line takes the string data that is found between the stack and damage value markers
- end
- return nil;
- end
- function getDamage(name)
- --this pulls the item damage value from the formatted text provided by ccSensors
- if name ~= nil then -- this is just a preventitive messure, if the function is given a 'nil' argument, then it will automaticly return 'nil'
- local damage = string.find(name, "@"); --the "@" denotes the damge value indicator, the damage value follows it
- local length = string.len(name); --this gives the string's length
- if damage == nil then -- IDIOT PROOFING: if there is no damage symbol in the argument passed to the function, it will return 'nil'
- return nil;
- end
- return string.sub(name, damage + 1, length); --this line pulls the damage value and returns it
- end
- return nil;
- end
- function getStack(name)
- --this pulls the item stack value from the formatted text provided by ccSensors
- if name ~= nil then -- this is just a preventitive messure, if the function is given a 'nil' argument, then it will automaticly return 'nil'
- local stack = string.find(name, "*"); --the "*" denotes the stack indicator, the stack value precedes it
- if stack == nil then -- IDIOT PROOFING: if there is no stack symbol in the argument passed to the function, it will return 'nil'
- return nil;
- end
- return string.sub(name, 1, stack - 1); --this pulls the items stack value and returns it
- end
- return nil;
- end
- function getCycle(react)
- --returns the number of cycles the
- --reactor has been running since last
- --cool down/ shut down
- --this function requires a Reactor Content Map to be passed to it
- --THIS FUNCTION WILL HAVE TO DRAW ON MEMORY STORED IN AN ARRAY
- --THAT DATA WILL HAVE TO RELY ON SOME OTHER FUNCTION TO GENERATE IT
- end
- function getStart(react)
- --returns the amount of time that
- --has passed since the reactor was
- --last shut down
- --THIS FUNCTION WILL HAVE TO DRAW ON MEMORY STORED IN AN ARRAY
- --THAT DATA WILL HAVE TO RELY ON SOME OTHER FUNCTION TO GENERATE IT
- end
- function getCycleRemain(react)
- --returns the amount of time till
- --the next fuel rod will burn out
- --this function requires a Reactor Content Map to be passed to it
- end
- function getItemX(iX, iY, Items)
- --returns the info for the item in the specified reactor
- --at the specified location
- --this function requires an Reactor Content Map to be passed to it
- --this function returns two arguments
- --this function will return nil as the second arguement if there is nothing in the inventory slot specified
- local coords = tostring(iX) .. "," .. tostring(iY);
- for x = 1, table.maxn(Items), 2 do --this moves through all the items in the reactor, searching for an item that has a coordinate set that matches the one generated from the integer values passed to this function
- if coords == Items[x] then
- return Items[x], Items[x + 1]; --the function will automatically exit once it finds the right item and returns the item name associated with it
- end
- end
- return coords, nil;
- end
- function getReactorLocation(react)
- --returns the x, y, and z coordinates of the specified reactor
- end
- function getPowerOutput(Readout)
- --returns the current EU/t output of the
- --specified reactor
- --this function requires a Reactor Info Array to be passed to it
- return Readout[4];
- end
- function getTargetInfo(react)
- --returns the data for "Target Info"
- local side = sensors.getController();
- local probes = sensors.getSensors(side);
- local readings = sensors.getAvailableReadings(side, probes[1]);
- local targets = sensors.getAvailableTargetsforProbe(side, probes[1], readings[1]); --targets refers to which type of readings you want to pull, like information about the block, or information about what is inside the block
- local data = sensors.getSensorReadingAsTable(side, probes[1], targets[react], readings[1]) --this is the actual data you want. it is in the form of a table and the damage value appears to be embedded in the item name, and the x,y coordinates are given as a single string
- return data;
- end
- function getReactorInfo(react)
- --returns the data for "Reactor Info"
- local side = sensors.getController();
- local probes = sensors.getSensors(side);
- local readings = sensors.getAvailableReadings(side, probes[1]);
- local targets = sensors.getAvailableTargetsforProbe(side, probes[1], readings[2]); --targets refers to which type of readings you want to pull, like information about the block, or information about what is inside the block
- local data = sensors.getSensorReadingAsTable(side, probes[1], targets[react], readings[2]) --this is the actual data you want. it is in the form of a table and the damage value appears to be embedded in the item name, and the x,y coordinates are given as a single string
- return data;
- end
- function getReactorContent(react)
- --returns the data for "Reactor Content"
- local side = sensors.getController();
- local probes = sensors.getSensors(side);
- local readings = sensors.getAvailableReadings(side, probes[1]);
- local targets = sensors.getAvailableTargetsforProbe(side, probes[1], readings[5]); --targets refers to which type of readings you want to pull, like information about the block, or information about what is inside the block
- local data = sensors.getSensorReadingAsTable(side, probes[1], targets[react], readings[5]) --this is the actual data you want. it is in the form of a table and the damage value appears to be embedded in the item name, and the x,y coordinates are given as a single string
- return data;
- end
- function outputTable(data)
- --takes a table and prints every two items on the same line
- local file = fs.open("ReactorData.txt", "w");
- local message = "";
- local damage;
- local stack;
- local name;
- for x = 1, table.maxn(data), 2 do
- if data[x] ~= nil then
- message = message .. " " .. tostring(data[x]);
- end
- if data[x + 1] ~= nil then
- message = message .. " " .. tostring(getStack(data[x + 1]).. " " .. getName(data[x + 1]) .. " " .. getDamage(data[x + 1]));
- end
- file.writeLine(message);
- message = "";
- end
- file.close();
- end
- function record(i0, i1, i2 ,i3, i4)
- --prints out all server activity to the screen
- i0 = textutils.serialize(i0);
- i1 = textutils.serialize(i1);
- i2 = textutils.serialize(i2);
- i3 = textutils.serialize(i3);
- i4 = textutils.serialize(i4);
- local message = "";
- if i0 ~= nil then
- message = message .. " " .. i0;
- else
- message = message .. " " .. "nil";
- end
- if i1 ~= nil then
- message = message .. " " .. i1;
- else
- message = message .. " " .. "nil";
- end
- if i2 ~= nil then
- message = message .. " " .. i2;
- else
- message = message .. " " .. "nil";
- end
- if i3 ~= nil then
- message = message .. " " .. i3;
- else
- message = message .. " " .. "nil";
- end
- if i4 ~= nil then
- message = message .. " " .. i4;
- else
- message = message .. " " .. "nil";
- end
- print(message);
- end
- function rednetMessageSplitter(message)
- --this function takes a rednet message, in the form of a string, and looks to see if it has
- --more than item it by looking for ","
- --it returns all the found parameters as an array
- local length;
- local first;
- local newParameters = {};
- local x = 0;
- repeat
- length = string.len(message); --find the length of message
- first = string.find(message, ","); --find the first instance of a ","
- if first ~= nil then --if there is actually an instance of a "," then continue
- x = x + 1;
- newParameters[x] = string.sub(message, 1, first - 1); --this will take everything in front of the "," and place it into "newParameter"
- message = string.sub(message, first + 1, length); --this will create a new version of "message" that includes all of the original, accept for what is now in "newParameter"
- end
- until first == nil --just exit the loop if there is nothing else to do
- newParameters[x + 1] = message;
- return newParameters;
- end
- --notes:
- --i really REEEEEALLLLY don't like how "rednetMessageSplitter()" is implemented in the main method.
- --it is incredibly ugly, and in elegent and makes the that section way more difficult to read and modify!
- --it should be rewritten immediately!!!!
- --should be replaced with the "textutils.unserialize()" command!!!!
- --begin program
- reactors = 9; --how many reactors are there to monitor
- --main method
- repeat
- rednet.open("back");
- local event, param1, param2, param3, param4 = os.pullEvent("rednet_message");
- --rednet.close("back");
- if event == "rednet_message" then
- local param2x = rednetMessageSplitter(param2);
- if param2x[1] == "sendReactorMap" then
- netSendReactorMap(tonumber(param2x[2]));
- elseif param2x[1] == "sendReactorUpdate" then
- netSendUpdate(tonumber(param2x[2]));
- elseif param2x[1] == "systemShutDown" then
- os.shutdown();
- elseif param2x[1] == "systemReboot" then
- os.reboot();
- elseif param2x[1] == "sensorReboot" then
- os.reboot();
- elseif param2x[1] == "sensorShutDown" then
- os.shutdown();
- elseif param2x[1] == "updateMonitorOn" then
- peripheral.call("bottom", "clear");
- --this is a command that tells the sensor server
- --to place sensor information on the directly attached
- --monitor
- elseif param2x[1] == "updateMonitorOff" then
- peripheral.call("bottom", "clear");
- --this is a command that tells the sensor server
- --to not place sensor information on the directly
- --attached monitor
- end
- end
- record(event, param1, param2, param3, param4);
- until 1 == 0
Advertisement
Add Comment
Please, Sign In to add comment