- Added torpedoes and ships collision events and rendering
authorMatthew Mondor <mmondor@pulsar-zone.net>
Wed, 25 Jun 2014 14:25:42 +0000 (14:25 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Wed, 25 Jun 2014 14:25:42 +0000 (14:25 +0000)
14 files changed:
mmsoftware/mystic_ships/client/src/conf.h
mmsoftware/mystic_ships/client/src/main.c
mmsoftware/mystic_ships/client/src/main.h
mmsoftware/mystic_ships/client/src/packets.c
mmsoftware/mystic_ships/common/packets_common.h
mmsoftware/mystic_ships/server/GNUmakefile
mmsoftware/mystic_ships/server/src/collision.c [new file with mode: 0644]
mmsoftware/mystic_ships/server/src/collision.h [new file with mode: 0644]
mmsoftware/mystic_ships/server/src/main.c
mmsoftware/mystic_ships/server/src/packets.c
mmsoftware/mystic_ships/server/src/packets.h
mmsoftware/mystic_ships/server/src/ships.c
mmsoftware/mystic_ships/server/src/torp.c
mmsoftware/mystic_ships/server/src/torp.h

index e1027ba..7838a4e 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: conf.h,v 1.3 2014/06/21 17:16:32 mmondor Exp $ */
+/* $Id: conf.h,v 1.4 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -14,7 +14,8 @@
  * Various hardcoded configuration parameters.
  */
 
-#define SERVER_HOST    "mmondor.pulsar-zone.net"
+//#define SERVER_HOST  "mmondor.pulsar-zone.net"
+#define SERVER_HOST    "192.168.1.15"
 #define SERVER_PORT    7777
 
 #define CLIENT_VERSION 1
index a4bf801..33cc8d4 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.7 2014/06/25 12:53:13 mmondor Exp $ */
+/* $Id: main.c,v 1.8 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -676,6 +676,14 @@ torp_draw(int x, int y, int r)
            0x7f + (rand() % 0x80));
 }
 
+void
+collision_draw(int x, int y, int r)
+{
+
+       /* XXX Should be fancier */
+       filledCircleRGBA(screen_surface, x, y, r, 0xff, 0xff, 0xff, 0xa0);
+}
+
 static SDL_Surface *
 bmp_load_key(const uint8_t *data, size_t size)
 {
index f94e482..66cabdd 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: main.h,v 1.3 2014/06/25 12:53:13 mmondor Exp $ */
+/* $Id: main.h,v 1.4 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -25,6 +25,7 @@
 void   frame_switch(void);
 void   ship_draw(int, int, int, int, int, double, uint8_t);
 void   torp_draw(int, int, int);
+void   collision_draw(int, int, int);
 
 
 
index f432bba..32496c2 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: packets.c,v 1.3 2014/06/25 12:53:13 mmondor Exp $ */
+/* $Id: packets.c,v 1.4 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -205,9 +205,12 @@ spacket_torp_handler(uint16_t *ptr)
 static int
 spacket_collision_handler(uint16_t *ptr)
 {
-       /*struct spacket_collision      *p = (struct spacket_collision *)ptr;*/
+       struct spacket_collision        *p = (struct spacket_collision *)ptr;
 
-       /* XXX */
+       p->x = BYTEORDER_HOST16(p->x);
+       p->y = BYTEORDER_HOST16(p->y);
+
+       collision_draw(p->x, p->y, p->radius);
 
        return 0;
 }
index 1c04d4e..c075d1d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: packets_common.h,v 1.4 2014/06/25 12:53:13 mmondor Exp $ */
+/* $Id: packets_common.h,v 1.5 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -109,8 +109,8 @@ struct spacket_torp {
 /* Collision/detonation update to client */
 struct spacket_collision {
        int8_t  packet_type;
-       int8_t  collision_type;
-       int16_t object1_id, object2_id;
+       int8_t  radius;
+       int16_t x, y;
 } __attribute__((__packed__));
 
 struct svpacket_message {
index 0370962..f52821d 100644 (file)
@@ -1,11 +1,11 @@
-# $Id: GNUmakefile,v 1.2 2014/06/19 18:22:03 mmondor Exp $
+# $Id: GNUmakefile,v 1.3 2014/06/25 14:25:42 mmondor Exp $
 
 MMLIB_PATH := ../../mmlib
 
 MMLIBS := $(addprefix $(MMLIB_PATH)/,mmpool.o mmarch.o)
 LIBS := -lc -lm -lz
 OBJS := $(addprefix src/,main.o trigonometry.o net.o kqueue.o sendq.o recvq.o \
-       packets.o daemon.o client.o ships.o torp.o enc.o) \
+       packets.o daemon.o client.o ships.o torp.o collision.o enc.o) \
        $(addprefix ../common/,hmac_sha1.o hmac_rmd160.o sha1.o rmd160.o \
        mmenc.o)
 CFLAGS += -Wall -g
diff --git a/mmsoftware/mystic_ships/server/src/collision.c b/mmsoftware/mystic_ships/server/src/collision.c
new file mode 100644 (file)
index 0000000..c76e1ff
--- /dev/null
@@ -0,0 +1,65 @@
+/* $Id: collision.c,v 1.1 2014/06/25 14:25:42 mmondor Exp $ */
+
+/*
+ * Copyright (c) 2006, 2014, Matthew Mondor
+ * ALL RIGHTS RESERVED.
+ */
+
+
+
+#include <stdlib.h>
+#include <syslog.h>
+
+#include <mmlog.h>
+
+#include <collision.h>
+#include <client.h>
+
+
+
+list_t         collisions_list;
+static pool_t  collisions_pool;
+
+
+void
+collision_init(void)
+{
+
+       DLIST_INIT(&collisions_list);
+       if (!pool_init(&collisions_pool, "collisions_pool", malloc, free,
+           NULL, NULL, sizeof(collision_t), 65536 / sizeof(collision_t), 1,
+           0)) {
+               syslog(LOG_NOTICE, "collision_init() - pool_init()");
+               exit(EXIT_FAILURE);
+       }
+}
+
+void
+collision_create(int radius, int x, int y)
+{
+       collision_t     *c;
+
+       if ((c = (collision_t *)pool_alloc(&collisions_pool, FALSE))
+           != NULL) {
+               c->radius = radius;
+               c->x = x;
+               c->y = y;
+               DLIST_APPEND(&collisions_list, (node_t *)c);
+       } else
+               syslog(LOG_NOTICE,
+                   "collision_create() - pool_alloc(collisions_pool)");
+}
+
+void
+collision_reset(void)
+{
+       collision_t     *c, *next;
+
+       for (c = (collision_t *)DLIST_TOP(&collisions_list); c != NULL;
+           c = next) {
+               next = DLIST_NEXT((node_t *)c);
+               (void) pool_free((pnode_t *)c);
+       }
+       DLIST_INIT(&collisions_list);
+}
+
diff --git a/mmsoftware/mystic_ships/server/src/collision.h b/mmsoftware/mystic_ships/server/src/collision.h
new file mode 100644 (file)
index 0000000..db2846a
--- /dev/null
@@ -0,0 +1,37 @@
+/* $Id: collision.h,v 1.1 2014/06/25 14:25:42 mmondor Exp $ */
+
+/*
+ * Copyright (c) 2006, 2014, Matthew Mondor
+ * ALL RIGHTS RESERVED.
+ */
+
+
+
+#ifndef _COLLISION_H_
+#define _COLLISION_H_
+
+
+
+#include <client.h>
+
+
+
+typedef struct collision {
+       pnode_t pnode;
+       int     radius;
+       int     x, y;
+} collision_t;
+
+
+
+void   collision_init(void);
+void   collision_create(int, int, int);
+void   collision_reset(void);
+
+
+
+extern list_t  collisions_list;
+
+
+
+#endif
index a96c362..236ee01 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.1 2006/12/31 08:32:40 mmondor Exp $ */
+/* $Id: main.c,v 1.2 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -27,6 +27,7 @@
 #include <packets_common.h>
 #include <packets.h>
 #include <torp.h>
+#include <collision.h>
 #include <client.h>
 #include <enc.h>
 #include <conf.h>
@@ -57,6 +58,7 @@ main(int argc, char **argv)
        net_init();
        trigonometry_init();
        torp_init();
+       collision_init();
        noncerand_init();
        kqueue_addlisten(listen_fd);
        kqueue_addsignal(SIGTERM);
index db248a0..5eb78ee 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: packets.c,v 1.4 2014/06/25 12:53:14 mmondor Exp $ */
+/* $Id: packets.c,v 1.5 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -37,6 +37,7 @@
 #include <enc.h>
 #include <ships.h>
 #include <torp.h>
+#include <collision.h>
 #include <sendq.h>
 #include <hmac.h>
 #include <conf.h>
@@ -119,6 +120,14 @@ packets_update(void)
 
                                if (square_intersect(x1, y1, r1, x2, y2, r2) &&
                                    circle_intersect(x1, y1, r1, x2, y2, r2)) {
+                                       /* XXX
+                                        * - If a ship dies, don't kill the
+                                        *   client; delay and reset instead
+                                        */
+                                       collision_create((int)(r1 + r2),
+                                           x1, y1);
+                                       collision_create((int)(r1 + r2),
+                                           x2, y2);
                                        if (ship_hit_ship(s1, s2)) {
                                                client_destroy_mark(c);
                                                client_destroy_mark(c2);
@@ -188,10 +197,11 @@ k:
 
        torps_update();
 
-       /* XXX Send all ships and all torps to all clients for now */
+       /* XXX Send all ships, torps and collisions to all clients for now */
        DLIST_FOREACH(&clients_list, c) {
                client_t        *c2;
                torp_t          *t;
+               collision_t     *col;
 
                if (!c->authenticated)
                        continue;
@@ -214,10 +224,8 @@ k:
                }
 
                DLIST_FOREACH(&clients_list, c2) {
-
                        if (!c2->authenticated)
                                continue;
-
                        if (!c2->ship.cloak_on || c2 == c) {
                                if (spacket_ship_send(c, (uint8_t)c2->fd,
                                    &c2->ship) == -1)
@@ -230,11 +238,18 @@ k:
                                break;
                }
 
+               DLIST_FOREACH(&collisions_list, col) {
+                       if (spacket_collision_send(c, col) == -1)
+                               break;
+               }
+
                if (spacket_eof_send(c) == -1)
                        break;
                (void) sendq_zflush(&c->sendq, NULL, NULL);
                (void) sendq_flush(&c->sendq, -1);
        }
+       /* Collisions are only sent once */
+       collision_reset();
 }
 
 int
@@ -328,6 +343,19 @@ spacket_torp_send(client_t *c, torp_t *t)
        return client_write(c, (uint8_t *)&p, sizeof(p), 1);
 }
 
+int
+spacket_collision_send(client_t *c, collision_t *col)
+{
+       struct spacket_collision        p;
+
+       p.packet_type = SPACKET_COLLISION;
+       p.radius = col->radius;
+       p.x = BYTEORDER_NETWORK16(col->x);
+       p.y = BYTEORDER_NETWORK16(col->y);
+
+       return client_write(c, (uint8_t *)&p, sizeof(p), 1);
+}
+
 
 static int
 cpacket_auth_handler(client_t *c, uint16_t *ptr)
index 64cee78..95179b0 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: packets.h,v 1.2 2014/06/25 12:53:14 mmondor Exp $ */
+/* $Id: packets.h,v 1.3 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) Matthew Mondor
@@ -16,6 +16,7 @@
 #include <packets_common.h>
 #include <ships.h>
 #include <torp.h>
+#include <collision.h>
 
 
 
@@ -42,6 +43,7 @@ int           spacket_eof_send(client_t *);
 int            spacket_ship_send(client_t *, uint8_t, ship_t *);
 int            spacket_shipinfo_send(client_t *);
 int            spacket_torp_send(client_t *, torp_t *);
+int            spacket_collision_send(client_t *, collision_t *);
 
 
 
index b14524b..cb44114 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ships.c,v 1.4 2014/06/25 12:53:14 mmondor Exp $ */
+/* $Id: ships.c,v 1.5 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, 2014, Matthew Mondor
@@ -40,10 +40,10 @@ struct shipdesc ships[SHIP_MAX] = {
                80/*2.0*/,      /* Turn thrust */
                1.0,            /* Turn cost */
                200.0,          /* Torp cost */
-               20.0,           /* Torp damage */
-               2.0,            /* Torp radius */
+               35.0,           /* Torp damage */
+               3.0,            /* Torp radius */
                10.0,           /* Torp speed */
-               40,             /* Torp life */
+               50,             /* Torp life */
                90.0,           /* Phaser cost */
                90.0,           /* Phaser damage */
                5.0,            /* Phaser recharge */
@@ -56,13 +56,13 @@ struct shipdesc ships[SHIP_MAX] = {
                2.0,            /* Hull regen */
                50.0,           /* Shield max */
                5.0,            /* Shield cost */
-               3.0,            /* Shield regen */
+               2.0,            /* Shield regen */
                25.0,           /* Cloak cost */
                5000.0,         /* Fuel max */
                50.0,           /* Fuel regen */
                9.0,            /* Thrust max */
                0.60,           /* Thrust acc */
-               0.60,           /* Thrust dec */
+               0.80,           /* Thrust dec */
                2.0,            /* Thrust acc cost */
                2.0,            /* Thrust dec cost */
                1.0,            /* Thrust cost */
@@ -70,9 +70,9 @@ struct shipdesc ships[SHIP_MAX] = {
                0.5,            /* Turn cost */
                90.0,           /* Torp cost */
                30.0,           /* Torp damage */
-               3.0,            /* Torp radius */
+               2.0,            /* Torp radius */
                12.0,           /* Torp speed */
-               60,             /* Torp life */
+               40,             /* Torp life */
                90.0,           /* Phaser cost */
                90.0,           /* Phaser damage */
                5.0,            /* Phaser recharge */
index e56913d..53c9079 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: torp.c,v 1.2 2014/06/21 17:16:32 mmondor Exp $ */
+/* $Id: torp.c,v 1.3 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -16,6 +16,7 @@
 #include <trigonometry.h>
 #include <client.h>
 #include <ships.h>
+#include <collision.h>
 #include <conf.h>
 
 
@@ -73,7 +74,7 @@ torp_create(client_t *c, int angle)
                s->torps++;
                DLIST_APPEND(&torps_list, (node_t *)t);
        } else
-               syslog(LOG_NOTICE, "torp_create() - pool_alloc(torps_list)");
+               syslog(LOG_NOTICE, "torp_create() - pool_alloc(torps_pool)");
 }
 
 /*
@@ -113,6 +114,12 @@ torps_update(void)
 
                        if (square_intersect(tx, ty, tr, sx, sy, sr) &&
                            circle_intersect(tx, ty, tr, sx, sy, sr)) {
+                               /* XXX
+                                * - If ship dies, don't kill client but
+                                *   instead: delay, reset, continue
+                                */
+                               collision_create((int)(tsd->torp_damage / 5),
+                                   tx, ty);
                                if (ship_hit_torp(s, tsd))
                                        client_destroy_mark(c);
                                torp_destroyed = 1;
index 4fa2aec..c0b369b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: torp.h,v 1.1 2006/12/31 08:32:40 mmondor Exp $ */
+/* $Id: torp.h,v 1.2 2014/06/25 14:25:42 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -37,7 +37,7 @@ void  torps_update(void);
 
 
 
-list_t torps_list;
+extern list_t  torps_list;