2024-05-17 12:39:04 -05:00
|
|
|
var debug = {};
|
|
|
|
|
2024-07-11 14:25:45 -05:00
|
|
|
debug.build = function(fn) { fn(); }
|
|
|
|
|
2024-04-07 13:16:54 -05:00
|
|
|
debug.fn_break = function(fn,obj = globalThis) {
|
2024-03-19 23:01:31 -05:00
|
|
|
if (typeof fn !== 'function') return;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
var newfn = function() {
|
|
|
|
console.log("broke");
|
|
|
|
fn();
|
|
|
|
};
|
|
|
|
obj[fn.name] = newfn;
|
|
|
|
}
|
2023-08-17 20:13:17 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.draw_phys = false;
|
|
|
|
debug.draw_bb = false;
|
|
|
|
debug.draw_gizmos = false;
|
|
|
|
debug.draw_names = false;
|
|
|
|
debug.draw = function() {
|
2024-03-20 16:07:23 -05:00
|
|
|
if (this.draw_phys) game.all_objects(function(x) { debug.draw_gameobject(x); });
|
2024-03-19 23:01:31 -05:00
|
|
|
|
|
|
|
if (this.draw_bb)
|
|
|
|
game.all_objects(function(x) { debug.boundingbox(x.boundingbox(), Color.debug.boundingbox.alpha(0.05)); });
|
2023-08-17 20:13:17 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
if (this.draw_gizmos)
|
|
|
|
game.all_objects(function(x) {
|
|
|
|
if (!x.icon) return;
|
2024-04-03 17:17:32 -05:00
|
|
|
gui.image(x.icon, game.camera.world2view(x.pos));
|
2024-03-19 23:01:31 -05:00
|
|
|
});
|
2023-08-24 16:22:52 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
if (this.draw_names)
|
|
|
|
game.all_objects(function(x) {
|
2024-04-03 00:44:08 -05:00
|
|
|
render.text(x, game.camera.view2screen(x.pos).add([0,32]), 1, Color.debug.names);
|
2024-03-19 23:01:31 -05:00
|
|
|
});
|
2023-09-12 00:02:57 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
if (debug.gif.rec) {
|
2024-03-26 07:53:36 -05:00
|
|
|
render.text("REC", [0,40], 1);
|
|
|
|
render.text(time.timecode(time.timenow() - debug.gif.start_time, debug.gif.fps), [0,30], 1);
|
2024-03-19 23:01:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
2024-03-26 07:53:36 -05:00
|
|
|
if (sim.paused()) render.text("PAUSED", [0,0],1);
|
2024-03-19 23:01:31 -05:00
|
|
|
|
2024-03-26 07:53:36 -05:00
|
|
|
render.text(sim.playing() ? "PLAYING"
|
2024-03-19 23:01:31 -05:00
|
|
|
: sim.stepping() ?
|
2024-04-03 00:44:08 -05:00
|
|
|
"STEP" :
|
|
|
|
sim.paused() ?
|
|
|
|
"PAUSED; EDITING" :
|
|
|
|
"EDIT", [0, 0], 1);
|
2024-03-19 23:01:31 -05:00
|
|
|
}
|
2023-08-17 20:13:17 -05:00
|
|
|
|
2024-04-07 13:16:54 -05:00
|
|
|
function assert(op, str = `assertion failed [value '${op}']`)
|
2023-12-18 17:12:05 -06:00
|
|
|
{
|
2024-03-13 16:30:55 -05:00
|
|
|
if (!op) {
|
2024-04-07 13:16:54 -05:00
|
|
|
console.error(str);
|
2024-03-15 11:21:36 -05:00
|
|
|
os.quit();
|
2024-03-13 16:30:55 -05:00
|
|
|
}
|
2023-12-18 17:12:05 -06:00
|
|
|
}
|
|
|
|
|
2023-04-22 16:44:26 -05:00
|
|
|
var Gizmos = {
|
|
|
|
pick_gameobject_points(worldpos, gameobject, points) {
|
2023-11-29 17:31:41 -06:00
|
|
|
var idx = Math.grab_from_points(worldpos, points.map(gameobject.this2world,gameobject), 25);
|
2023-12-18 06:45:27 -06:00
|
|
|
if (idx === -1) return undefined;
|
|
|
|
return idx;
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-04-07 13:16:54 -05:00
|
|
|
profile.cpu = function(fn, times = 1, q = "unnamed") {
|
2024-03-20 09:04:28 -05:00
|
|
|
var start = profile.now();
|
|
|
|
for (var i = 0; i < times; i++)
|
2024-03-01 11:45:06 -06:00
|
|
|
fn();
|
2024-03-20 09:04:28 -05:00
|
|
|
|
|
|
|
var elapsed = profile.now() - start;
|
|
|
|
var avgt = profile.best_t(elapsed/times);
|
|
|
|
var totalt = profile.best_t(elapsed);
|
2024-03-15 11:21:36 -05:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
say(`profile [${q}]: ${profile.best_t(avgt)} average [${profile.best_t(totalt)} for ${times} loops]`);
|
|
|
|
}
|
2023-12-18 06:45:27 -06:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
profile.ms = function(t) { return t/1000000; }
|
|
|
|
profile.secs = function(t) { return t/1000000000; }
|
2024-01-14 10:24:31 -06:00
|
|
|
|
2023-08-28 17:00:53 -05:00
|
|
|
/* These controls are available during editing, and during play of debug builds */
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.inputs = {};
|
|
|
|
debug.inputs.f1 = function () { debug.draw_phys = !debug.draw_phys; };
|
|
|
|
debug.inputs.f1.doc = "Draw physics debugging aids.";
|
|
|
|
debug.inputs.f3 = function() { debug.draw_bb = !debug.draw_bb; };
|
|
|
|
debug.inputs.f3.doc = "Toggle drawing bounding boxes.";
|
|
|
|
debug.inputs.f4 = function() {
|
|
|
|
debug.draw_names = !debug.draw_names;
|
|
|
|
debug.draw_gizmos = !debug.draw_gizmos;
|
2023-08-24 16:22:52 -05:00
|
|
|
};
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.inputs.f4.doc = "Toggle drawing gizmos and names of objects.";
|
2024-03-19 14:39:19 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.gif = {
|
2023-09-12 12:45:54 -05:00
|
|
|
w: 640, /* Max width */
|
|
|
|
h: 480, /* Max height */
|
|
|
|
stretch: false, /* True if you want to stretch */
|
2023-09-13 07:31:00 -05:00
|
|
|
cpf: 4,
|
2023-09-14 12:49:29 -05:00
|
|
|
depth: 16,
|
2023-09-12 00:02:57 -05:00
|
|
|
file: "out.gif",
|
|
|
|
rec: false,
|
2023-09-12 12:45:54 -05:00
|
|
|
secs: 6,
|
|
|
|
start_time: 0,
|
|
|
|
fps: 0,
|
|
|
|
start() {
|
|
|
|
var w = this.w;
|
|
|
|
var h = this.h;
|
|
|
|
if (!this.stretch) {
|
2024-03-18 08:16:25 -05:00
|
|
|
var win = window.height / window.width;
|
2023-09-12 12:45:54 -05:00
|
|
|
var gif = h/w;
|
|
|
|
if (gif > win)
|
|
|
|
h = w * win;
|
|
|
|
else
|
|
|
|
w = h / win;
|
|
|
|
}
|
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
// cmd(131, w, h, this.cpf, this.depth);
|
2023-09-12 12:45:54 -05:00
|
|
|
this.rec = true;
|
|
|
|
this.fps = (1/this.cpf)*100;
|
2024-03-14 14:10:06 -05:00
|
|
|
this.start_time = time.now();
|
2023-09-12 12:45:54 -05:00
|
|
|
|
2023-09-13 07:31:00 -05:00
|
|
|
timer.oneshot(this.stop.bind(this), this.secs, this, true);
|
2023-09-12 12:45:54 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
if (!this.rec) return;
|
2024-03-19 14:39:19 -05:00
|
|
|
// cmd(132, this.file);
|
2023-09-12 12:45:54 -05:00
|
|
|
this.rec = false;
|
|
|
|
},
|
2023-09-12 00:02:57 -05:00
|
|
|
};
|
2023-09-12 12:45:54 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.inputs.f8 = function() {
|
2023-09-25 12:29:04 -05:00
|
|
|
var now = new Date();
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.gif.file = now.toISOString() + ".gif";
|
|
|
|
debug.gif.start();
|
2023-09-25 12:29:04 -05:00
|
|
|
};
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.inputs.f9 = function() {
|
|
|
|
debug.gif.stop();
|
2023-09-25 12:29:04 -05:00
|
|
|
}
|
2023-09-12 00:02:57 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.inputs.f10 = function() { time.timescale = 0.1; };
|
|
|
|
debug.inputs.f10.doc = "Toggle timescale to 1/10.";
|
|
|
|
debug.inputs.f10.released = function () { time.timescale = 1.0; };
|
2024-04-03 17:17:32 -05:00
|
|
|
debug.inputs.f12 = function() { gui.defaults.debug = !gui.defaults.debug; console.warn("gui toggle debug");};
|
|
|
|
debug.inputs.f12.doc = "Toggle drawing gui debugging aids.";
|
2023-08-24 16:22:52 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.inputs['M-1'] = render.normal;
|
|
|
|
debug.inputs['M-2'] = render.wireframe;
|
|
|
|
debug.inputs['C-M-f'] = function() {};
|
|
|
|
debug.inputs['C-M-f'].doc = "Enter camera fly mode.";
|
2023-08-28 17:00:53 -05:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.api = {};
|
|
|
|
debug.api.doc_entry = function(obj, key)
|
2023-10-23 08:08:11 -05:00
|
|
|
{
|
2024-02-23 16:05:30 -06:00
|
|
|
if (typeof key !== 'string') {
|
|
|
|
console.warn("Cannot print a key that isn't a string.");
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-10-23 08:08:11 -05:00
|
|
|
var title = key;
|
|
|
|
|
|
|
|
var o = obj[key];
|
|
|
|
if (typeof o === 'undefined' && obj.impl && typeof obj.impl[key] !== 'undefined')
|
|
|
|
o = obj.impl[key];
|
|
|
|
|
|
|
|
var t = typeof o;
|
2024-02-23 16:05:30 -06:00
|
|
|
if (Array.isArray(o)) t = "array";
|
|
|
|
else if (t === 'function') {
|
2023-10-23 08:08:11 -05:00
|
|
|
title = o.toString().tofirst(')') + ")";
|
2024-02-23 16:05:30 -06:00
|
|
|
title = title.fromfirst('(');
|
|
|
|
title = key + "(" + title;
|
2023-10-23 08:08:11 -05:00
|
|
|
if (o.doc) doc = o.doc;
|
|
|
|
t = "";
|
2024-02-23 16:05:30 -06:00
|
|
|
} else if (t === 'undefined') t = "";
|
2023-10-23 08:08:11 -05:00
|
|
|
|
2024-02-23 16:05:30 -06:00
|
|
|
if (t) t = "**" + t + "**\n";
|
2023-10-23 08:08:11 -05:00
|
|
|
|
2024-02-23 16:05:30 -06:00
|
|
|
var doc = "";
|
|
|
|
if (o.doc) doc = o.doc;
|
|
|
|
else if (obj.doc && obj.doc[key]) doc = obj.doc[key];
|
|
|
|
else if (Array.isArray(o)) doc = json.encode(o);
|
2023-10-23 08:08:11 -05:00
|
|
|
|
2024-07-03 16:38:29 -05:00
|
|
|
return `#### ${title}
|
2023-10-23 08:08:11 -05:00
|
|
|
${t}
|
|
|
|
${doc}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
debug.api.print_doc = function(name)
|
2023-10-23 08:08:11 -05:00
|
|
|
{
|
2024-02-23 16:05:30 -06:00
|
|
|
var obj = name;
|
|
|
|
if (typeof name === 'string') {
|
|
|
|
obj = eval(name);
|
|
|
|
if (!obj) {
|
|
|
|
console.warn(`Cannot print the API of '${name}', as it was not found.`);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = globalThis[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = eval(name);
|
|
|
|
|
|
|
|
if (!Object.isObject(obj)) {
|
|
|
|
console.warn("Cannot print the API of something that isn't an object.");
|
|
|
|
return undefined;
|
2023-10-23 08:08:11 -05:00
|
|
|
}
|
|
|
|
|
2024-02-23 16:05:30 -06:00
|
|
|
if (!obj) {
|
|
|
|
console.warn(`Object '${name}' does not exist.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var mdoc = "# " + name + "\n";
|
2023-10-23 08:08:11 -05:00
|
|
|
if (obj.doc?.doc) mdoc += obj.doc.doc + "\n";
|
|
|
|
else if (typeof obj.doc === 'string') mdoc += obj.doc + "\n";
|
2024-07-03 16:38:29 -05:00
|
|
|
|
|
|
|
var keys = Object.keys(obj);
|
|
|
|
for (var key of keys) {
|
2023-10-23 08:08:11 -05:00
|
|
|
if (key === 'doc') continue;
|
2023-10-26 11:48:02 -05:00
|
|
|
if (key === 'toString') continue;
|
2024-02-23 16:05:30 -06:00
|
|
|
|
2024-03-19 23:01:31 -05:00
|
|
|
mdoc += debug.api.doc_entry(obj, key) + "\n";
|
2023-10-23 08:08:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return mdoc;
|
|
|
|
}
|
2024-02-27 10:09:15 -06:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
debug.log = {};
|
|
|
|
|
|
|
|
debug.log.time = function(fn, name, avg=0)
|
|
|
|
{
|
|
|
|
debug.log.time[name] ??= [];
|
|
|
|
var start = profile.now();
|
|
|
|
fn();
|
|
|
|
debug.log.time[name].push(profile.now()-start);
|
|
|
|
}
|
|
|
|
|
2024-07-11 16:37:24 -05:00
|
|
|
debug.kill = function()
|
|
|
|
{
|
|
|
|
assert = function() {};
|
|
|
|
debug.build = function() {};
|
|
|
|
debug.fn_break = function() {};
|
|
|
|
}
|
|
|
|
|
2024-02-27 10:09:15 -06:00
|
|
|
return {
|
2024-03-19 23:01:31 -05:00
|
|
|
debug,
|
2024-03-01 11:45:06 -06:00
|
|
|
Gizmos,
|
2024-03-11 15:11:39 -05:00
|
|
|
assert
|
2024-02-27 10:09:15 -06:00
|
|
|
}
|