Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
ArchAngel075

SAD - Supply and Demand thing

Recommended Posts

(I created this out of curiosity and desire for something that does what this does, feel free to critic and help me catch bugs or suggest features!)

Ever wanted to programmatically control logistics and machine setups?

Ever Wanted to control these setups across multiple OC computers?

With SAD you can!

How to Install SAD :

1. First place SAD.lua (get it here) anywhere (try /home/SAD.lua)

2. Install dkjson (get it here) to /libs as "dkjson.lua"

Using SAD :

first Create a Query :

path/to/SAD -query=BatteryCharger

NOTE that you must not use spaces in arguments, including Query Names!

This will create the Query and open the Query in edit.

you will see something like

local function Query(SAD)
	return 0
end
return Query;

You can modify the contents of the function as you wish, have it return any primitive value (String, Bool, integer etc)

here the Query returns 0.

 

Next we want to make a Actor for the query :

path/to/SAD -actor=BatteryCharger&Power

Note that we enter both the query name and the actor name (actor name after the ampersand symbol)

This will create and open the actor 'Power'  for query 'BatteryCharger' for editing

you will see something like

local json = require("dkjson")
local function Act(SAD,result)
	result = json.decode(result)
	return true
end
return Act

Here the result of the Query is given as a JSON string [result] which is decoded for you.

Now say we wish to print ("Hello World") when result is 0, You will end up with something like :

local json = require("dkjson")
local function Act(SAD,result)
	result = json.decode(result)
	if result[1] == 0 then
		print("Hello World")
	end
end
return Act

 

We can create more Actors for the Query, each will execute when the query runs, and each actor do as they wish with the result.

Finally to run SAD in the background sop that it actually executes Queries and Actors use :

path/to/SAD -startServer

Servers will broadcast the result of Queries to whatever SAD client is within range.

If SAD is installed on another network enabled machine within range use :

path/to/SAD -startClient

This will make the client process any received packets send by a server.

IF the Client has a matching Query to the one sent in a packet then any actors for the local client will be executed with the result of the servers Query.

This way you can have tablets and other devices also act based on a query.

To stop a SAD client or server use

path/to/SAD -stop

 

Queries and actors can do anything they like to produce results or act on results.

In some single player instance i have a Query test if a machines inventory contains something and if it does a Actor will turn on a power generating device.

Another actor will turn off the power generating device if the inventory becomes empty.

 

Using with Other editors :

SAD related files are stored @"/home/SAD"

the *actual* query file used in Queries is stored @"/home/SAD/Queries/[QueryName]/_.lua"

and actors are located within @"/home/SAD/Queries/[QueryName]/"

Enjoy.

Link to post
Share on other sites

Im intending to make some recording or imgur gallery to display the use of SAD once I get to that point in the server I play on.

Though off the top of my head i have this for some setup using rotarycraft (in modpack rev|3)

--query "Grinder"
local component = require("component")
local function Query(SAD)
  local grinder = component.proxy(component.get("address_of_rotarycraft_grinder"))
  local item = grinder.getSlot(0) --returns item name of item in grinder, or nil if empty
  local result = 0
  if item ~= nil then
    result = 1
  end
  return result
end
return Query

 

and actor :

--actor "ManageGrinder" @ "Grinder"
local json = require("dkjson")
local component = require("component")
local function Act(SAD,result)
  	local ecu = component.proxy(component.get("address_of_rotarycraft_ECU_under_some_engine"))
	result = json.decode(result)
	if result[1] == 0 then --the grinder has nothing inside it, power off the engine via its ecu
		ecu.setECU(0) --makes the engine stop, therefore not burn fuel and therefore conserve fuel
	elseif result[1] == 1 then --the grinder has something to grind in it, we will want to power it.
    	ecu.setECU(4) --sets engine to power to 100%, push maximum power to grinder.
    end
end
return Act

 

What it does :

In rotarycraft a producer "engine" will burn fuel and produce power even if that power has nowhere to go, but with a ECU you can make the engine slower or turn off to reduce usage or stop usage of fuel.

this query and actor setup will automate the process of turning the engine on and off only when its needed.

This isnt the best setup, but one can go as far as query the engines fuel reserves and act when low or full, showing that SAD isnt just for charger-battery systems, it is for any system that can use logic code to determine the result.

 

another mission will be to use tablets to update displays based on Actors, or even OC-Glasses HUD with actors from some computer.

 

Link to post
Share on other sites

Discovered an issue in SAD.lua

The check for modem isnt done correctly and causes a crash. For now allways use a network or wireless card until i post a fix later today.

Otherwise im at a point in the server im on where i can use the program so once i make some 'real world' queries or actors ill happily share them too including a video to showcase.

Link to post
Share on other sites

Updated SAD.lua pastebin link to point to v2

Changes :

+ Added vars, now can manipulate variables in SAD, use ` SAD.vars ` to store variables.

A important use of variables is often Queries and actors need to interact with external components obtained via component.proxy()... Previously these components would need to be proxied EVERY query run and actor run. This is uneeded memory usage and CPU usage especially on the lowest tier setups. Now instead one can do ` SAD.vars.my_tank = SAD.vars.my_tank or component.proxy("address_here") ` . Thus will either fetch the variable if set or set it by calling proxy once. For best practices use a namespace in vars : ` SAD.myQuery.my_tank ` this can prevent overlapping variables among queries and actors.

 

+ Added check for modem properly now.

The check previously would error if no modem is present, now it performs a isAvailable("modem") call as it should.

 

+ Performance improvements!

Before one would have to do ` require("component") ` and any other requires (such as for dkjson) themselves on EVERY query and actor, this makes alot of reading and loading and hurts the cpu.

Now SAD does a load once strategy, IF you want a allways available load once module (from Open computers) let me know and ill quickly update, to get the load once modules just do SAD.[module] ie ` SAD.components ` or ` SAD.modem `

 

Also before SAD would read in the query and actor files before running them EVERY cycle, now SAD loads once on startServer/startClient and uses that load. this SIGNIFICANTLY reduces CPU load, before it was nearly impossible to use the machine while SAD runs in background, now its usable on even low tier machines (although still with some felt performance hits on low end machines)

/

 

Also i will later be working on a example query actor i will be using myself on the server I play for roatarycraft AC engine management and gearbox lubricant monitors. If and weh ni get around to completing it i will video the example and share.

Enjoy

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.