ref: 6fbeb81c4c872e72c5abd85a284b964b77c5f0d2
parent: 8b5dae4088f803b1d40f1a38cdeceb59fc3d1270
author: mkf <mkf@cloud9p.org>
date: Thu May 23 11:42:46 EDT 2024
gui9: merge gui9net
--- a/gui9.c
+++ b/gui9.c
@@ -2,6 +2,7 @@
#include <libc.h>
#include <draw.h>
#include <event.h>
+#include "netclient.h"
#include "engine.h"
Font *font;
@@ -15,7 +16,17 @@
Image *won;
int debug;
+char *server = nil;
+enum
+{
+ New = 0,
+ Undo,
+ Restart,
+ Exit,
+ Border = 3,
+};
+
char *mbuttons[] =
{
"Easy",
@@ -258,7 +269,7 @@
void
usage(void)
{
- fprint(2, "usage: %s [-dg]\n", argv0);
+ fprint(2, "usage: %s [-dg] [-n server]\n", argv0);
exits("usage");
}
@@ -267,6 +278,7 @@
{
Mouse m;
Event ev;
+ Netmsg *msg;
int e, mousedown=0;
/* todo, add flags for human playing */
@@ -281,6 +293,12 @@
case 'g':
ptype[1] = Computer;
break;
+ case 'n':
+ ptype[0] = Net;
+ ptype[1] = Net;
+ networked = 1;
+ server = EARGF(usage());
+ break;
default:
usage();
}ARGEND
@@ -288,16 +306,36 @@
sysfatal("initdraw failed: %r");
einit(Emouse);
+ if(server != nil && networked)
+ {
+ srvfd = dial(server, nil, nil, nil);
+ if(srvfd < 0)
+ sysfatal("unable to connect: %r");
+ }
+
resize();
srand(time(0));
allocimages();
- initlevel(); /* must happen before "eresized" */
- eresized(0);
+ if(networked)
+ {
+ netmain(); /* CONN */
+ netmain(); /* SYNC */
+ netmain(); /* TURN/WAIT */
+ }
+ else
+ initlevel(); /* must happen before "eresized" */
+ eresized(0);
for(;;)
{
+ if(networked && waitbit && isplaying)
+ {
+ msg = netmain();
+ if(msg->tokens[0] != nil && strcmp(msg->tokens[0], "SYNC"))
+ drawlevel();
+ }
e = event(&ev);
switch(e)
{
@@ -305,7 +343,7 @@
m = ev.mouse;
if(m.buttons == 0)
{
- if(mousedown && (state == Playing || state == Start))
+ if(mousedown && isplaying)
{
mousedown = 0;
if(turn % 2 == 0)
@@ -312,7 +350,8 @@
put(m.xy);
else
move(m.xy);
- drawlevel();
+ if(!networked)
+ drawlevel();
}
}
if(m.buttons&1)