-
-
Save Scors4/365a3f5136c32f6ffc1e80c89d2b4a6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@name "Veldspar Auto Miner"; | |
@model "models/cerus/weapons/plas_cannon.mdl"; | |
server { | |
entity self = system.getEntity(); | |
int distanceBase = 75; | |
vector TPos = self.getPos(); | |
hologram body = new hologram(); | |
table construct = new table(); | |
table lazors = new table(); | |
table lazors_wl = new table(); | |
table solars = new table(); | |
table batteries = new table(); | |
table storages = new table(); | |
table cores = new table(); | |
table nodes = new table(); | |
int stage = 1; | |
// Misc data field to pass on multiple or single batteries/storages. | |
// Storage/Battery => 1 = 1 of each, 2 = one or zero batteries, 3 = one storage, 4 = multiple of both | |
int data = 0; | |
function void Loop() { | |
} | |
function void SetColors(table entities, color col) | |
{ | |
foreach(entity ent in entities) | |
{ | |
ent.setColor(col); | |
} | |
} | |
function void ErrorOut(string err) | |
{ | |
SetColors(construct, color(255,255,255)); | |
SetColors(solars, color(255,255,255)); | |
error theError = new error(err); | |
system.print(theError.message()); | |
system.throw(theError); | |
} | |
function bool ValidatePotentialClass(entity e) | |
{ | |
table valid_classes = new table("resource_node", "storage_energy"); | |
string e_class = e.getClass(); | |
if(e_class == "generator_energy_solar") | |
{ | |
solars.push(e); | |
return true; | |
} | |
elseif(e_class.startWith("st_ore_lazor")) | |
{ | |
lazors.push(e); | |
construct.push(e); | |
lazors_wl.push(e.getWirelink()); | |
return true; | |
} | |
elseif(e_class.startWith("st_ore_storage")) | |
{ | |
construct.push(e); | |
return true; | |
} | |
elseif(e_class.startWith("st_core")) | |
{ | |
construct.push(e); | |
return true; | |
} | |
elseif(valid_classes.contains(e_class)) | |
{ | |
construct.push(e); | |
return true; | |
} | |
return false; | |
} | |
function void KickEntities(table entities, number mult) | |
{ | |
foreach(entity ent in entities) | |
{ | |
if(ent.isFrozen()) | |
continue; | |
if(ent.Vel().length() > 2) | |
continue; | |
number x = math.random(-10, 10); | |
number y = math.random(-10, 10); | |
number z = math.random(-10, 10); | |
ent.applyForce(vector(x * mult, y * mult, z * 3*mult) * ent.getMass() * 2); | |
} | |
} | |
function void ParentContraption() | |
{ | |
SetColors(construct, color(255)); | |
SetColors(lazors, color(255)); | |
SetColors(solars, color(255)); | |
foreach(entity ent in construct) | |
{ | |
//ent.setParent(self); | |
} | |
system.out("Parenting complete. Miner now online."); | |
foreach(wirelink wl in lazors_wl) | |
{ | |
//wl["On", number] = 1 | |
} | |
//event.add("Think", "Loop", Loop); | |
} | |
function bool MoveItemToTarget(entity Ent, vector TPos, quaternion TarDirAng, number tolerance | |
, number dampening, number FMult) | |
{ | |
bool phase1 = false; | |
bool phase2 = false; | |
vector PosError = (TPos) - Ent.pos(); | |
if(PosError.length() <= tolerance) | |
{ | |
phase1 = true; | |
} | |
PosError = (PosError + vector(0,0, 9.25 * Ent.getGravity())) * Ent.getMass() * FMult; | |
PosError = (PosError - Ent.Vel() * dampening); | |
Ent.applyForce(PosError) | |
quat currAng = quat(Ent); | |
vector Torque = Ent.toLocal(quaternion.rotationVector(TarDirAng/currAng) + Ent.getPos()); | |
Torque = 700 * Torque - 30 * Ent.angVelVector(); | |
Ent.applyTorque(Torque * Ent.inertia()); | |
if((TarDirAng.dot(currAng)) <= 0.1) | |
{ | |
phase2 = true; | |
} | |
if(phase1 && phase2) | |
{ | |
Ent.setFrozen(true); | |
return true; | |
} | |
return false; | |
} | |
function void FreezeEntities(table entities, bool freeze) | |
{ | |
foreach(entity ent in entities) | |
{ | |
ent.setFrozen(freeze); | |
} | |
} | |
function void MoveLazor() | |
{ | |
bool complete = false; | |
quat TarAng = quat(self.toWorld(angle(90,0,0))); | |
TPos = self.getPos(); | |
if(#lazors == 1) | |
{ | |
entity lazor = lazors[1]; | |
TPos = self.toWorld(vector(distanceBase, 0, self.boxCenter().getZ())); | |
complete = MoveItemToTarget(lazor, TPos, TarAng, 0.25, 3, 1); | |
if(complete) | |
{ | |
lazor.setFrozen(true); | |
lazor.setColor(color(0,0,255)); | |
} | |
} | |
else | |
{ | |
number interval = (2 * math.pi()) / #lazors | |
complete = true; | |
for(int i = 1; #lazors; 1) | |
{ | |
entity lazor = lazors[i]; | |
if(lazor.getColor() == color(0,0,255)) | |
continue; | |
number ang = interval * i; | |
TPos = self.toWorld(vector(distanceBase, math.sin(ang), math.cos(ang) + self.boxCenter().getZ())); | |
if(!MoveItemToTarget(lazor, TPos, TarAng, 0.25, 3, 1)) | |
{ | |
complete = false; | |
} | |
else | |
{ | |
lazor.setColor(color(0,0,255)); | |
} | |
} | |
} | |
if(complete) | |
{ | |
SetColors(lazors, color(0,255,0)); | |
event.remove("Think", "MoveLazor"); | |
system.out("Entering parenting phase"); | |
timer.remove("KickEntities"); | |
timer.simple(1, ParentContraption); | |
} | |
} | |
function void MoveCore() | |
{ | |
bool complete = false; | |
quat TarAng = quat(self); | |
TPos = self.getPos(); | |
if(#cores == 1) | |
{ | |
entity core = cores[1]; | |
TPos = self.toWorld(vector((distanceBase / 2) + (core.boxSize().getX() / 2), 0, self.boxCenter().getZ())); | |
complete = MoveItemToTarget(core, TPos, TarAng, 0.25, 3, 1); | |
if(complete) | |
{ | |
core.setFrozen(true); | |
core.setColor(color(0,0,255)); | |
} | |
} | |
else | |
{ | |
number interval = (2 * math.pi()) / #cores; | |
complete = true; | |
for(int i = 1; #cores; 1) | |
{ | |
entity core = cores[i]; | |
if(core.getColor() == color(0,0,255)) | |
continue; | |
TPos = self.toWorld(vector((distanceBase / 2) + (core.boxSize().getX() / 2), math.sin(interval * i), math.cos(interval * i) + self.boxCenter().getZ())); | |
if(!MoveItemToTarget(core, TPos, TarAng, 0.25, 3, 1)) | |
{ | |
complete = false; | |
} | |
else | |
{ | |
core.setColor(color(0,0,255)); | |
} | |
} | |
} | |
FreezeEntities(lazors, false); | |
if(complete) | |
{ | |
SetColors(cores, color(0,255,0)); | |
event.remove("Think", "MoveCore"); | |
system.out("Entering lazor build phase"); | |
timer.remove("KickEntities"); | |
event.add("Think", "MoveLazor", MoveLazor); | |
timer.create("KickEntities", 5, 0, KickEntities, lazors, 10); | |
} | |
} | |
function void SanitizeNodes() | |
{ | |
entity temp_node = nodes[1]; | |
nodes = new table(temp_node); | |
} | |
function void MoveNode() | |
{ | |
entity node = nodes[1]; | |
bool complete = false; | |
number dist = (distanceBase * 0.8) + node.boxSize().getZ(); | |
TPos = self.toWorld(vector(-dist * 0.8, 0, self.boxCenter().getZ())); | |
quat TarAng = quat(self); | |
complete = MoveItemToTarget(node, TPos, TarAng, 1, 2, 1); | |
FreezeEntities(cores, false); | |
if(complete) | |
{ | |
node.setFrozen(true); | |
SetColors(nodes, color(0,255,0)); | |
event.remove("Think", "MoveNode"); | |
system.out("Entering core build phase"); | |
timer.remove("KickEntities"); | |
event.add("Think", "MoveCore", MoveCore) | |
timer.create("KickEntities", 5, 0, KickEntities, cores, 10); | |
} | |
} | |
function void MoveStorage() | |
{ | |
bool complete = false; | |
vector TPos = self.getPos(); | |
quat TarAng = quat(self) | |
if(data % 2) | |
{ | |
number dist = distanceBase + ((entity)nodes[1]).boxSize().getZ(); | |
if(data == 1) | |
dist = ((entity)batteries[1]).boxSize().getZ() + (dist * 1.25) | |
TPos = self.toWorld(vector(-distanceBase * 1.5, 0, self.boxCenter().getZ())); | |
complete = MoveItemToTarget((entity)storages[1], TPos, TarAng, 5, 20, 0.5); | |
if(complete) | |
((entity)storages[1]).setFrozen(true); | |
} | |
else | |
{ | |
number interval = (2 * math.pi()) / #storages | |
complete = true; | |
for(int i = 1; #storages; 1) | |
{ | |
entity ent = storages[i]; | |
if(ent.getColor() == color(0,0,255)) | |
continue; | |
number ang = interval * i; | |
ang = ang + (interval / 2); | |
TPos = self.toWorld(vector(-distanceBase * 1.5, math.cos(ang) * distanceBase / 3, math.sin(ang) * distanceBase / 3)); | |
if(!MoveItemToTarget(ent, TPos, TarAng, 5, 20, 3)) | |
{ | |
complete = false; | |
} | |
else | |
{ | |
ent.setColor(color(0,0,255)); | |
} | |
} | |
} | |
FreezeEntities(nodes, false); | |
if(complete) | |
{ | |
SetColors(storages, color(0,255,0)); | |
event.remove("Think", "MoveStorage"); | |
system.out("Entering node build phase"); | |
SanitizeNodes(); | |
timer.remove("KickEntities"); | |
event.add("Think", "MoveNode", MoveNode) | |
timer.create("KickEntities", 5, 0, KickEntities, nodes, 10); | |
} | |
} | |
function void MoveBattery() | |
{ | |
bool complete = false; | |
if(data == 0 || data > 4) | |
{ | |
system.out("Data was set to ", data, " which is not valid for battery stage (1 - 4)"); | |
error theError = error("Data was set to an invalid option."); | |
system.throw(theError); | |
} | |
quat TarAng = quat(self.toWorld(angle(90,0,0))) | |
if(data == 2 && #batteries == 0) | |
{ | |
complete = true; | |
} | |
elseif(data <= 2) | |
{ | |
number dist = distanceBase * 1.5; | |
if (data == 1) | |
{ | |
dist = distanceBase * 3; | |
} | |
TPos = self.toWorld(vector(-dist,0,self.boxCenter().getZ())); | |
complete = MoveItemToTarget((entity)batteries[1], TPos, TarAng, 1, 2, 1) | |
if(complete) | |
((entity)batteries[1]).setFrozen(true); | |
} | |
else | |
{ | |
number dist = (distanceBase * (2/3)) | |
if(data == 4) | |
{ | |
dist = dist | |
} | |
complete = true; | |
number interval = (2 * math.pi()) / #batteries; | |
for(int i = 1; #batteries; 1) | |
{ | |
entity ent = batteries[i]; | |
if(ent.getColor() == color(0,0,255)) | |
continue; | |
number ang = interval * i; | |
TPos = self.toWorld(vector(-distanceBase, math.cos(ang) * dist, math.sin(ang) * dist)); | |
if(!MoveItemToTarget(ent, TPos, TarAng, 1, 10, 1)) | |
{ | |
complete = false; | |
} | |
else | |
{ | |
ent.setColor(color(0,0,255)); | |
} | |
} | |
} | |
FreezeEntities(storages, false); | |
if(complete) | |
{ | |
SetColors(batteries, color(0,255,0)); | |
event.remove("Think", "MoveBattery"); | |
system.out("Entering storage build phase"); | |
timer.remove("KickEntities"); | |
timer.create("KickEntities", 5, 0, KickEntities, storages, 10); | |
event.add("Think", "MoveStorage", MoveStorage) | |
} | |
} | |
function void BuildMiner() | |
{ | |
foreach (entity e in construct) | |
{ | |
string e_class = e.getClass(); | |
if(e_class.startWith("st_ore_storage")) | |
{ | |
storages.push(e); | |
} | |
elseif(e_class == "resource_node") | |
{ | |
nodes.push(e); | |
} | |
elseif(e_class == "storage_energy") | |
{ | |
batteries.push(e); | |
} | |
elseif(e_class.startWith("st_cores")) | |
{ | |
cores.push(e); | |
} | |
} | |
if(#storages == 0) | |
{ | |
ErrorOut("No ore storages detected!"); | |
} | |
elseif(#nodes == 0) | |
{ | |
ErrorOut("NO resource nodes detected!"); | |
} | |
elseif(#nodes > 1) | |
{ | |
system.print("The end contraption does not need more than one node. Disregarding the rest."); | |
} | |
elseif(#cores == 0) | |
{ | |
//ErrorOut("It is illegal for this code to operate without a core!") | |
} | |
if(#batteries == 1 && #storages == 1) | |
{ | |
data = 1; | |
} | |
elseif(#batteries <= 1) | |
{ | |
data = 2; | |
} | |
elseif(#storages == 1) | |
{ | |
data = 3; | |
} | |
else | |
{ | |
data = 4; | |
} | |
//timer.simple(2, ParentContraption); | |
FreezeEntities(batteries, false); | |
system.out("Entering battery build phase"); | |
stage = 1; | |
event.add("Think", "MoveBattery", MoveBattery); | |
timer.create("KickEntities", 5, 0, KickEntities, batteries, 100); | |
} | |
function void Main() { | |
system.setInterval(250); | |
body.setModel("models/ce_miningmodels/miner_bodies/shadowseries/shadowseries_mk3.mdl"); | |
body.setAng(angle(90,0,0)); | |
body.setPos(self.getPos() + self.boxCenter()); | |
body.setScale(vector(0.125)); | |
body.parent(self); | |
find parts = new find(); | |
parts.findInSphere(self.getPos(), 1500); | |
table potentials = parts.toArray(); | |
self.setFrozen(true); | |
self.setPos(self.toWorld(vector(0,0,250))); | |
foreach(entity e in potentials) | |
{ | |
if(e.owner() != self.owner() || e.totalConstraints()) | |
continue; | |
if(ValidatePotentialClass(e)) | |
{ | |
if(!e.isFrozen()) | |
e.setFrozen(true); | |
e.setColor(color(255,0,0)); | |
} | |
} | |
timer.simple(3, BuildMiner); | |
} | |
Main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment