shithub: asif

Download patch

ref: b5baeb3dc3f2d6e45d0b2da3b2e82cad103c1789
parent: bbb8f4aee6098856e9171bbbd12cb5e424a8ca2e
author: qwx <qwx@sciops.net>
date: Thu Dec 16 22:45:43 EST 2021

add wip path(1)

diff: cannot open b/path//null: file does not exist: 'b/path//null'
--- /dev/null
+++ b/path/client.c
@@ -1,0 +1,108 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <pool.h>
+#include "../asif.h"
+#include "dat.h"
+#include "fns.h"
+
+extern QLock drawlock;
+int	mouseinput(Node*, Mouse);
+int	keyinput(Rune);
+Node*	scrselect(Point);
+void	updatedrw(void);
+
+static Keyboardctl *kc;
+static Mousectl *mc;
+
+void
+evloop(void)
+{
+	Rune r;
+	Mouse m;
+	Node *n;
+
+	enum{
+		Aresize,
+		Amouse,
+		Akbd,
+		Aend,
+	};
+	Alt a[] = {
+		[Aresize] {mc->resizec, nil, CHANRCV},
+		[Amouse] {mc->c, &mc->Mouse, CHANRCV},
+		[Akbd] {kc->c, &r, CHANRCV},
+		[Aend] {nil, nil, CHANEND},
+	};
+	for(;;){
+		switch(alt(a)){
+		case Aresize:
+			if(getwindow(display, Refnone) < 0)
+				sysfatal("resize failed: %r");
+			resetdrw();
+			break;
+		case Amouse:
+			m = mc->Mouse;
+			if(m.buttons == 0)
+				break;
+			if((n = scrselect(m.xy)) != nil){
+				if(m.buttons & 4)
+					n->blocked ^= 1;
+				mouseinput(n, m);
+			}
+			updatedrw();
+			break;
+		case Akbd:
+			switch(r){
+			case Kdel: threadexitsall(nil);
+			}
+			keyinput(r);
+			break;
+		}
+	}
+}
+
+static void
+usage(void)
+{
+	fprint(2, "usage: %s [-s width[,height]]\n", argv0);
+	threadexits("usage");
+}
+
+void
+init(int argc, char **argv)
+{
+	char *s;
+
+	mapwidth = 64;
+	mapheight = 64;
+	ARGBEGIN{
+	case 's':
+		mapwidth = strtol(EARGF(usage()), &s, 0);
+		if(mapwidth <= 0)
+			usage();
+		if(*s != ','){
+			mapheight = mapwidth;
+			break;
+		}
+		mapheight = strtol(s+1, nil, 0);
+		if(mapheight <= 0)
+			usage();
+		break;
+	default: usage();
+	}ARGEND
+	//mainmem->flags |= POOL_PARANOIA | POOL_NOREUSE;
+	fmtinstall('P', Pfmt);
+	fmtinstall('R', Rfmt);
+	initfs();
+	initmap();
+	initgrid();
+	initdrw();
+	if((kc = initkeyboard(nil)) == nil)
+		sysfatal("initkeyboard: %r");
+	if((mc = initmouse(nil, screen)) == nil)
+		sysfatal("initmouse: %r");
+}
--- /dev/null
+++ b/path/dat.h
@@ -1,0 +1,13 @@
+typedef struct Vertex Vertex;
+typedef struct Node Node;
+
+struct Vertex{
+	int x;
+	int y;
+};
+struct Node{
+	int blocked;
+};
+extern Node *map;
+extern int mapwidth, mapheight;
+extern Node *selected;
--- /dev/null
+++ b/path/dijkstra.c
@@ -1,0 +1,28 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include "../asif.h"
+#include "dat.h"
+#include "fns.h"
+
+int
+mouseinput(Node *, Mouse)
+{
+	return 0;
+}
+
+int
+keyinput(Rune)
+{
+	return 0;
+}
+
+void
+threadmain(int argc, char **argv)
+{
+	init(argc, argv);
+	evloop();
+}
--- /dev/null
+++ b/path/drw.c
@@ -1,0 +1,145 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include "../asif.h"
+#include "dat.h"
+#include "fns.h"
+
+QLock drawlock;
+Node *selected;
+
+typedef Vertex Point;
+
+enum{
+	Nodesz = 8,
+};
+enum{
+	Cbg,
+	Cgrid,
+	Copen,
+	Cclosed,
+	Cend,
+};
+static Image *col[Cend];
+static Point viewΔ;
+static Rectangle viewr, hudr;
+static Image *view;
+
+static Image *
+eallocimage(Rectangle r, int repl, ulong col)
+{
+	Image *i;
+
+	if((i = allocimage(display, r, screen->chan, repl, col)) == nil)
+		sysfatal("allocimage: %r");
+	return i;
+}
+
+Node *
+scrselect(Point p)
+{
+	p = subpt(addpt(subpt(p, screen->r.min), viewΔ), Pt(1,1));
+	if(!ptinrect(p, viewr)){
+		selected = nil;
+		return nil;
+	}
+	p = divpt(p, Nodesz);
+	p.x = MIN(p.x, mapwidth-1);
+	p.y = MIN(p.y, mapheight-1);
+	selected = map + p.y * mapwidth + p.x;
+	return selected;
+}
+
+static void
+flushdrw(void)
+{
+	draw(screen, screen->r, view, nil, viewΔ);
+	flushimage(display, 1);
+}
+
+static void
+drawhud(void)
+{
+	char s[64], *sp;
+	Node *n;
+
+	draw(screen, hudr, col[Cbg], nil, ZP);
+	sp = seprint(s, s+sizeof s, "map size: %R", viewr);
+	if((n = selected) != nil){
+		assert(n >= map && n < map + mapwidth * mapheight);
+		sp = seprint(sp, s+sizeof s, " selected: %P: %s",
+			Pt((n-map) % mapwidth, (n-map) / mapwidth),
+			n->blocked ? "blocked" : "");
+	}
+	USED(sp);
+	string(screen, addpt(screen->r.min, subpt(Pt(2, viewr.max.y+2), viewΔ)), col[Copen], ZP, font, s);
+}
+
+static void
+drawmap(void)
+{
+	Rectangle r;
+	Node *n;
+
+	draw(view, view->r, col[Copen], nil, ZP);
+	r = viewr;
+	while(r.min.x < viewr.max.x){
+		r.max.x = r.min.x;
+		line(view, r.min, r.max, 0, 0, 0, col[Cgrid], ZP);
+		r.min.x += Nodesz;
+	}
+	r = viewr;
+	while(r.min.y < viewr.max.y){
+		r.max.y = r.min.y;
+		line(view, r.min, r.max, 0, 0, 0, col[Cgrid], ZP);
+		r.min.y += Nodesz;
+	}
+	for(n=map; n<map+mapwidth*mapheight; n++)
+		if(n->blocked){
+			r.min.x = (n - map) % mapwidth * Nodesz + 1;
+			r.min.y = (n - map) / mapwidth * Nodesz + 1;
+			r.max = addpt(r.min, Pt(Nodesz-1, Nodesz-1));
+			draw(view, r, col[Cclosed], nil, ZP);
+		}
+}
+
+static void
+redraw(void)
+{
+	drawmap();
+}
+
+void
+updatedrw(void)
+{
+	qlock(&drawlock);
+	redraw();
+	qunlock(&drawlock);
+	drawhud();
+	flushdrw();
+}
+
+void
+resetdrw(void)
+{
+	viewr = Rpt(ZP, Pt(mapwidth*Nodesz+1, mapheight*Nodesz+1));
+	viewΔ = divpt(addpt(subpt(ZP, subpt(screen->r.max, screen->r.min)), viewr.max), 2);
+	hudr.min = addpt(screen->r.min, subpt(Pt(2, viewr.max.y+2), viewΔ));
+	hudr.max = addpt(hudr.min, Pt(viewr.max.x-2, font->height*3));
+	freeimage(view);
+	view = eallocimage(viewr, 0, DNofill);
+	draw(screen, screen->r, col[Cbg], nil, ZP);
+	updatedrw();
+}
+
+void
+initdrw(void)
+{
+	if(initdraw(nil, nil, "path") < 0)
+		sysfatal("initdraw: %r");
+	col[Cbg] = display->black;
+	col[Cgrid] = eallocimage(Rect(0,0,1,1), 1, 0x222222ff);
+	col[Copen] = eallocimage(Rect(0,0,1,1), 1, 0x777777ff);
+	col[Cclosed] = display->black;
+	resetdrw();
+}
--- /dev/null
+++ b/path/fns.h
@@ -1,0 +1,7 @@
+void	init(int, char**);
+void	evloop(void);
+void	initfs(void);
+void	initmap(void);
+void	initgrid(void);
+void	initdrw(void);
+void	resetdrw(void);
--- /dev/null
+++ b/path/fs.c
@@ -1,0 +1,10 @@
+#include <u.h>
+#include <libc.h>
+#include "../asif.h"
+#include "dat.h"
+#include "fns.h"
+
+void
+initfs(void)
+{
+}
--- /dev/null
+++ b/path/grid.c
@@ -1,0 +1,15 @@
+#include <u.h>
+#include <libc.h>
+#include "../asif.h"
+#include "dat.h"
+#include "fns.h"
+
+struct PNode{
+	Vertex;
+};
+
+void
+initgrid(void)
+{
+
+}
--- /dev/null
+++ b/path/map.c
@@ -1,0 +1,14 @@
+#include <u.h>
+#include <libc.h>
+#include "../asif.h"
+#include "dat.h"
+#include "fns.h"
+
+Node *map;
+int mapwidth, mapheight;
+
+void
+initmap(void)
+{
+	map = emalloc(mapwidth * mapheight * sizeof *map);
+}
--- /dev/null
+++ b/path/mkfile
@@ -1,0 +1,24 @@
+</$objtype/mkfile
+BIN=$home/bin/$objtype/path
+TARG=\
+	dijkstra\
+
+HFILES=../asif.h dat.h fns.h
+OFILES=\
+	../emalloc.$O\
+	client.$O\
+	drw.$O\
+	fs.$O\
+	grid.$O\
+	map.$O\
+
+</sys/src/cmd/mkmany
+
+$O.dijkstra: $OFILES dijkstra.$O ../pheap.$O
+
+dicks:V:
+	mkdir -p $BIN
+install:V:	dicks
+%.$O:	%.c
+	$CC -o $target $CFLAGS $stem.c
+CLEANFILES=`{echo ../*.[$OS]}