BorrowedTorch.Drivers
2.0.0
Prefix Reserved
dotnet add package BorrowedTorch.Drivers --version 2.0.0
NuGet\Install-Package BorrowedTorch.Drivers -Version 2.0.0
<PackageReference Include="BorrowedTorch.Drivers" Version="2.0.0" />
<PackageVersion Include="BorrowedTorch.Drivers" Version="2.0.0" />
<PackageReference Include="BorrowedTorch.Drivers" />
paket add BorrowedTorch.Drivers --version 2.0.0
#r "nuget: BorrowedTorch.Drivers, 2.0.0"
#:package BorrowedTorch.Drivers@2.0.0
#addin nuget:?package=BorrowedTorch.Drivers&version=2.0.0
#tool nuget:?package=BorrowedTorch.Drivers&version=2.0.0
BorrowedTorch.Drivers
Drivers for MonoGame: values worked out every frame by a graph of nodes - inputs, logic, maths, memory and timers, and animations started, reversed and stopped by pulses. A lamp brighter as the player comes near, a door that opens when a plate is stood on and closes two seconds after it is left, a counter of switches pressed: each is a graph whose Output gives the values it drives - one port each, as many as it drives.
Install
dotnet add package BorrowedTorch.Drivers
Targets net10.0. Depends on BorrowedTorch.Animations 1.1.0 (the Animation node's clip and time line). It is compiled
against MonoGame 3.8.1.303 without depending on it: the game brings its own MonoGame - DesktopGL, WindowsDX or
BorrowedTorch's compute fork, 3.8.1.303 or newer - whose Vector4 the values are.
Quick start
What a graph reads and drives of the host's things is the host's: it gives the library an Input node, whose ports
each read one thing, and an Output node, whose ports each drive one - both binding their ports
(DriverNodeKind.BindsPorts), a port for each of the node's Bindings.
using BorrowedTorch.Drivers;
// A lamp's intensity: 20 at 2 m from the player, 0 from 10 m on.
var library = DriverNodeLibrary<Entity>.CreateWithBuiltIns().Add(myDistanceKind).Add(myInputKind).Add(myOutputKind);
var graph = DriverGraph<Entity>.Empty
.With(library.CreateNode("Distance", 1, new Vector2(0, 0)) with { References = [player, lamp] })
.With(library.CreateNode(DriverKinds.Remap, 2, new Vector2(200, 0)) with { Values = [new(0), new(2), new(10), new(20), new(0)] })
.With(library.CreateNode("Output", 3, new Vector2(400, 0)) with { Bindings = [new DriverBinding<Entity>(lamp, "Light.Intensity")] })
.Connect(new DriverWire(1, 0, 2, 0))
.Connect(new DriverWire(2, 0, 3, 0));
var program = DriverProgram<Entity>.Compile(graph, library);
var run = program.CreateRun();
run.Start(); // as the game starts
// Update, once per frame:
run.Evaluate(new DriverFrame(elapsedSeconds, isPlaying));
if (run.Holds(0, lampOwnIntensity)) lamp.Intensity = run.OutputValue(0).X;
Values
Every wire carries a Vector4, of one of seven DriverTypes: yes/no (X: 1 or 0), number (X), Vector2
(X, Y), Vector3 (X, Y, Z), Vector4 (X, Y, Z, W), Color3 (X, Y, Z - red, green, blue) and Color4 (X, Y, Z,
W - red, green, blue, alpha); the parts a type hasn't are 0. A pulse is a yes for one frame - a trigger entered, an
Edge, an animation finished. DriverValues.ValueTypes lists them by their numbers - a Constant's options.
A value goes into a port of another type by itself where it can: a yes is 1 and a no 0, a number above 0.5 is yes, a
number is spread to every part of a vector or a color, and a vector or a color goes into another by the parts both have,
the rest 0 - into a Color4 from one without alpha, an alpha of 1. A vector or a color into a number takes a Split or a
Length - wired straight in, it is an error. A port of Any type takes the widest of what is wired into it - by parts,
a color over a vector of as many: Vector2, Vector3, Color3, Vector4, Color4; a yes/no counted as a number for maths - and
an unwired one takes a number of its own, spread to that type.
Graph, program, run
DriverGraph<TRef>is the driver as data: its nodes and wires, immutable, so it is shared, compared and swapped whole - an edit (With,Without,Connect,Disconnect,WithPortAdded,WithPortRemoved) makes a new one. Every node has the same fields -Kind,Position,Values(each unwired input's own value),Option,Flag,Inputs(a growing node's),ReferencesandName(what a host's node reads),Bindings(a bound node's ports),Clip(an animation's) - so a graph is saved and read as plain data.TRefis what the host's nodes name things by: the engine's entities.DriverProgram<TRef>.Compile(graph, library)works it out once: each node after what is wired into it, each port's type, a slot for every value, itsOutputs- oneDriverOutputfor each input of its Output: the port's type, whether it is wired, the Animation wired straight into it, the nodes it is worked out from - andDiagnostics: a node of no known kind, a type that can't go where it is wired, a loop (only through a Last frame may a value come round), two Outputs, two of a kind a graph has one of (IsSingle: a host's Input). A graph with an error drives nothing; one without an Output drives nothing, and does what its actions do.DriverRun<TRef>runs it:Evaluate(frame)once a frame, allocating nothing;OutputValue(port)what each port was given;Start()as the game starts (its state reset, the animations that play on start started);Command(Play / Pause / Stop, nodeId)for a tool's preview,Command(Forward / Reverse)for what plays it as its own pulses would - an action elsewhere;ValueOf(node, output)for an editor's live values. Made again as its graph is edited (program.CreateRun(previous)), it carries over the state of every node still there, of the same kind, reading the same things: an animation goes on playing.Holds(port, own): whether a port drives what it is bound to, given that thing's own value - it is wired, and gave something other than the own value, or an animation it is worked out from is not stopped (IsActive(port),StatesUpstreamOf(port, kind)). Otherwise the thing is its own: a door's closed turn can be edited while its animation stands at the start. A host writes a value only while it is held, and puts the own value back as the run lets go.
Nodes
| Group | Nodes |
|---|---|
| Inputs | Time (seconds since the game started), Playing, Constant - and the host's Input |
| Animation | Animation: an AnimationClip<Vector4> of a value of its type (its option), played from what comes into From - a property's own value, as the host's Input reads it. Play runs it on from where it is (one at its end stays there), Reverse back toward the start, Pause holds it, Stop puts it back; Stop and Play together start it over. Plays on start, unless switched off. Gives Value, Running, Finished (a pulse) and Progress |
| Generators | Wave: sine, triangle, square or saw from 0 to 1, by period and phase; Noise: wanders smoothly between 0 and 1, by speed and seed |
| Logic | And, Or (growing), Xor, Not, Compare (> ≥ < ≤ = ≠, within a tolerance), In range, Select |
| Math | Add, Multiply (growing), Subtract, Divide (0 by 0), Negate, Abs, Min, Max, Clamp, Remap (clamped or not), Lerp, Round (round, floor, ceiling, to a step), Wrap, Power, Square root - part by part; Curve: a value 0 to 1 through a shaping function (DriverNode.Curve) |
| Vector | Split, Combine (a Vector2, Vector3, Vector4, Color3 or Color4), Length, Dot and Normalize (of the parts the type has) |
| Memory | Edge (a pulse as its input turns on, off or either), Toggle, Latch (Set, Reset), Counter (up, down, reset, within a range or wrapping), Pulse (on for some seconds), Delay, Stay on, Smooth (easing, or at a speed), Last frame, Integrate (its input added up over time, Reset), Sample (its input taken on a pulse) |
| Output | the host's Output: what the graph drives, a port each |
| Subgraphs | Subgraph input (a name, a type, a default), Subgraph output (a name): a subgraph's ports; Subgraph: a use of one (hidden from a node search - listed by subgraph) |
Memory and time go by the frame's Elapsed - the game's clock, which pauses and steps with it. A pulse into a node is
taken as it comes on: a yes held down counts once.
Subgraphs
A subgraph is a graph of its own, used as one node in others: its Subgraph input nodes are its uses' inputs and its
Subgraph output nodes their outputs, each by its name, listed top to bottom as they stand. The host keeps them in
its library, by name - library.Subgraphs = library.Subgraphs.SetItem("Flicker", graph) - and a Subgraph node
names the one it uses (library.CreateUse("Flicker", id, position)). InputsOf(node) and OutputsOf(node) give any
node's ports - a use's its subgraph's, a bound node's by its bindings - and Uses(graph, name) whether a graph uses
one, through the subgraphs it uses: what an editor leaves out of a subgraph's own node search.
As a program is worked out, each use is put in its place as its subgraph's nodes: a use keeps a memory of its own - a
Toggle, an Animation in it counts for that use alone - its nodes run under ids of their own
(DriverProgram.InlinedId(useId, nodeId), the same each time, so their state carries over as the graph or the subgraph
is edited). An unwired input of a use takes the use's own value, else the input's default. What is wrong inside is said
on the use ("In 'Flicker': ..."); a use of no subgraph, or of one it is within, is an error. A subgraph is the same
wherever it is used, so what names the host's things is its user's: an Output, or a host's Input, in a subgraph is left
out, and said. ValueOf, InputTypesOf and OutputTypesOf answer for a use through its ports.
DriverSubgraphs.Extract(graph, nodeIds, name, library) makes a graph's nodes a subgraph - but those that name the
host's things (DriverSubgraphs.CanBeTaken): the subgraph, and the graph with a use of it in their place, wired as they
were.
A host's own nodes
A DriverNodeKind<TRef> is a node's id, title and group, its ports (DriverPort: name, type, whether it carries
pulses, the value it takes unwired), its settings (Options, FlagName), a state made as its run is reset
(CreateState), and Evaluate, handed a DriverCall<TRef>: its inputs already of its ports' types (In, Number,
Yes), its outputs to set (Out), its node, its state and the frame (Elapsed, IsPlaying). TypeOf gives the type
of its Any ports from the node; Check says what is wrong with a node as it is set, for the editor to show;
References (DriverReference: a title, whether the node's switch hides it, whether the node acts on it), NameTitle
and NameChoices (DriverChoices: the ids a node's Name picks among, as the host has them - keys, modes, handlers) tell
an editor what to give pickers for - or, NameIsText, a box to type it in. IsHidden leaves a kind out of an editor's
node search. A node that acts as its input comes on keeps a DriverEdge in its state: Rose, Fell, Changed.
A kind that reads or drives the host's things by its ports binds them: BindsPorts the side (its outputs, an Input's;
its inputs, an Output's - IsOutput), a port for each of the node's Bindings (DriverBinding: the thing, what of it
by Name, how by Mode - the host's to mean), made by PortOf and checked by CheckPort, both pure: an editor asks
them from its own thread. IsSingle keeps it one a graph.
Changelog
2.0.0
- One graph drives several values: the Output's inputs each one (
DriverProgram.Outputs,DriverOutput), and a run gives each its own (OutputValue,Holds,IsActive,StatesUpstreamOf) - in place ofOutputType,DirectAnimationNodeId,HasOutput,IsHoldingandEvaluate's value. - Bound ports:
DriverNode.Bindings,DriverBinding,DriverPortSide;DriverNodeKind.BindsPorts,PortOf,CheckPort,IsSingle,OutputCountOf,InputOf(node, index),OutputOf;DriverGraph.WithPortAdded/WithPortRemovedand the library's;DriverDiagnostic.Port;DriverReference.IsActedOn. DriverFrameis the frame alone (Elapsed,IsPlaying): a graph reads the host's things - an own value among them - by the host's Input.Compile(graph, library),TypeOf(node)andExtract(graph, nodeIds, name, library)take no driven type.- The Animation plays from its From input, of the type its option gives. The Own value and Output nodes are gone - the host gives its Input and Output.
- A node's state carries over only while it reads the same things (
Name,Bindings). DriverSubgraphs.CanBeTaken; an Input or Output in a subgraph is left out.
1.3.0
- Value types Vector2, Vector3, Vector4, Color3 and Color4 in place of vector and colour.
1.2.0
- Subgraphs:
DriverNodeLibrary.Subgraphs,CreateUse,InputsOf/OutputsOf,InterfaceOf(DriverSubgraphInterface),Uses; the Subgraph input, Subgraph output and Subgraph nodes; each use put in its place as a program is worked out (DriverProgram.InlinedId);DriverSubgraphs.Extract. DriverNodeKind.NameIsTextandIsHidden.
1.1.0
- Nodes: Noise, Power, Square root, Curve (
DriverNode.Curve, aBaseShapingFunction), Dot, Normalize, Integrate, Sample. DriverNodeKind.NameChoicesandDriverChoice;DriverEdge;DriverCommand.ForwardandReverse.
1.0.0
DriverGraph<TRef>,DriverNodeLibrary<TRef>,DriverProgram<TRef>andDriverRun<TRef>; the inputs, animation, generator, logic, maths, vector, memory and output nodes.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- BorrowedTorch.Animations (>= 1.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on BorrowedTorch.Drivers:
| Package | Downloads |
|---|---|
|
BorrowedTorch.Engine
Kindling, Borrowed Torch's game engine: what a game's own code builds against - its features registering components, systems, keys and UI handlers - for the Kindling editor and player to run |
GitHub repositories
This package is not used by any popular GitHub repositories.