- Added client message when connecting to server
authorMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 21 Jun 2014 17:16:32 +0000 (17:16 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 21 Jun 2014 17:16:32 +0000 (17:16 +0000)
- Added torpedo collisions and damage
- Added intership collisions

Still very basic, but multiuser combats are now possible.

12 files changed:
mmsoftware/mystic_ships/client/GNUmakefile
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/src/packets.c
mmsoftware/mystic_ships/server/src/ships.c
mmsoftware/mystic_ships/server/src/ships.h
mmsoftware/mystic_ships/server/src/torp.c
mmsoftware/mystic_ships/server/src/trigonometry.c
mmsoftware/mystic_ships/server/src/trigonometry.h

index c393a36..d18059d 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: GNUmakefile,v 1.2 2014/06/19 18:22:03 mmondor Exp $
+# $Id: GNUmakefile,v 1.3 2014/06/21 17:16:32 mmondor Exp $
 
 CC := cc
 RM := rm
@@ -45,6 +45,8 @@ SDL_LDFLAGS += -lSDL_image -lSDL_mixer -lSDL_net -lSDL_gfx
 Z_CFLAGS := -I/usr/local/include
 Z_LDFLAGS := -lz
 
+LDFLAGS += -lm
+
 # OS dependent settings follow
 OS := $(shell $(UNAME) -s)
 ifneq (,$(findstring CYGWIN,$(OS)))
@@ -55,8 +57,8 @@ ifneq (,$(findstring CYGWIN,$(OS)))
 #      GL_LDFLAGS := -lopengl32 -lglu32
 else
        # unix
-       CFLAGS += -I/usr/include -I/usr/pkg/include -I/usr/X11R6/include
-       LDFLAGS += -L/usr/lib -L/usr/pkg/lib -L/usr/X11R6/lib
+       CFLAGS += -I/usr/include -I/usr/pkg/include -I/usr/X11R7/include
+       LDFLAGS += -L/usr/lib -L/usr/pkg/lib -L/usr/X11R7/lib
 #      GL_CFLAGS :=
 #      GL_LDFLAGS := -lGL -lGLU
 endif
@@ -83,7 +85,7 @@ endif
 #CFLAGS += $(SDL_CFLAGS) $(GL_CFLAGS)
 #LDFLAGS += $(SDL_LDFLAGS) $(GL_LDFLAGS)
 CFLAGS += $(SDL_CFLAGS) $(Z_CFLAGS)
-LDFLAGS += $(SDL_LDFLAGS) $(Z_LDFLAGS)
+LDFLAGS += $(SDL_LDFLAGS) $(Z_LDFLAGS) -lm
 
 all: $(BIN)
 
@@ -100,6 +102,7 @@ $(RAWOBJS): encode
 
 $(BIN): $(OBJS) $(RAWOBJS)
        $(CC) -o $@ $(OBJS) $(RAWOBJS) $(LDFLAGS)
+#      $(CC) -o $@ -static $(OBJS) $(RAWOBJS) $(LDFLAGS) -lc -lm -lossaudio -L/usr/X11R7/lib/X11/locale/lib/common/ -lxlibi18n -lximcp -lxlcDef -lxlcUTF8Load -lxlocale -lxomGeneric -lX11 -lxcb -lXau -lXdmcp -lXext -lXrandr -lXrender -lusbhid -lpthread -lSDL -lSDL_image -lSDL_mixer -lSDL_net -lSDL_gfx -lz
        $(STRIP) -s -w -R .comment -R .ident -R .debug* $@*
 
 clean:
index 7f471cb..e1027ba 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: conf.h,v 1.2 2014/06/19 18:22:03 mmondor Exp $ */
+/* $Id: conf.h,v 1.3 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -14,9 +14,7 @@
  * Various hardcoded configuration parameters.
  */
 
-#define SERVER_HOST    "192.168.1.15"
-/*#define SERVER_HOST  "tms-play.pulsar-zone.net"*/
-
+#define SERVER_HOST    "mmondor.pulsar-zone.net"
 #define SERVER_PORT    7777
 
 #define CLIENT_VERSION 1
index 77d87fa..9d711d8 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.2 2014/06/19 19:53:34 mmondor Exp $ */
+/* $Id: main.c,v 1.3 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -260,6 +260,9 @@ main(int argc, char **argv)
         * We should display a "connecting to server" or such message to the
         * user during this time.
         */
+       font_blit_string(font, 16, 16,
+           "Connecting to server, press [SPACE] to abort.",
+           0x00, 0xff, 0x00, 0xff);
        {
                struct msg_connect      *cmsg;
 
@@ -662,7 +665,7 @@ frame_switch(void)
 }
 
 void
-ship_draw(int id, int x, int y, double angle, uint8_t flags)
+ship_draw(int id, int x, int y, int radius, double angle, uint8_t flags)
 {
        char    str[8];
 
@@ -671,14 +674,14 @@ ship_draw(int id, int x, int y, double angle, uint8_t flags)
                filledCircleRGBA(screen_surface, x, y, 25,
                    0x00, 0x00, 0x00, 0x80);
        if ((flags & SHIPF_SHIELD) != 0) {
-               aacircleRGBA(screen_surface, x, y, 25,
+               aacircleRGBA(screen_surface, x, y, radius,
                    0xf0, 0xf0, 0x20, 0xb0);
-               filledCircleRGBA(screen_surface, x, y, 25,
+               filledCircleRGBA(screen_surface, x, y, radius,
                    0x20, 0x50, 0xf0, 0x50);
        }
 
-       x -= 24;
-       y -= 24;
+       x -= radius;
+       y -= radius;
        (void) snprintf(str, 8, "%d", id);
        font_blit_string(font, x, y, str, 0x00, 0x00, 0x00, 0xff);
        font_blit_string(font, x + 1, y + 1, str, 0xff, 0xff, 0xff, 0xff);
index 9ec699c..3ec6c01 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: main.h,v 1.1 2006/12/31 08:32:39 mmondor Exp $ */
+/* $Id: main.h,v 1.2 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -23,7 +23,7 @@
 
 
 void   frame_switch(void);
-void   ship_draw(int, int, int, double, uint8_t);
+void   ship_draw(int, int, int, int, double, uint8_t);
 void   torp_draw(int, int, int);
 
 
index 850872a..61105ad 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: packets.c,v 1.1 2006/12/31 08:32:39 mmondor Exp $ */
+/* $Id: packets.c,v 1.2 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -169,7 +169,7 @@ spacket_ship_handler(uint16_t *ptr)
        p->x = BYTEORDER_HOST16(p->x);
        p->y = BYTEORDER_HOST16(p->y);
 
-       ship_draw(p->id, p->x, p->y, p->angle, p->flags);
+       ship_draw(p->id, p->x, p->y, p->radius, p->angle, p->flags);
 
        return 0;
 }
index c92b5ca..836371c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: packets_common.h,v 1.2 2014/06/19 18:22:03 mmondor Exp $ */
+/* $Id: packets_common.h,v 1.3 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -81,6 +81,7 @@ struct spacket_ship {
        int8_t  flags;
        int16_t id;
        int16_t x, y;
+       uint8_t radius;
        uint8_t angle;
 } __attribute__((__packed__));
 
index b25d696..0cb47e8 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: packets.c,v 1.2 2014/06/19 18:22:03 mmondor Exp $ */
+/* $Id: packets.c,v 1.3 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -86,7 +86,7 @@ struct packet_index   cpacket_index[CVPACKET_MAX] = {
 void
 packets_update(void)
 {
-       client_t        *c, *next;
+       client_t        *c, *next, *c2, *next2;
        uint8_t         *data;
        size_t          size, off, len;
 
@@ -106,6 +106,30 @@ packets_update(void)
                if (c->todestroy || c->toclose)
                        continue;
 
+               /* Check for ship/ship collisions */
+               for (c2 = (client_t *)DLIST_TOP(&clients_list); c2 != NULL;
+                    c2 = next2) {
+                       next2 = DLIST_NEXT((node_t *)c2);
+                       if (c2 != c) {
+                               ship_t  *s1 = &c->ship, *s2 = &c2->ship;
+                               int     x1 = s1->x, y1 = s1->y,
+                                       r1 = s1->ship->radius,
+                                       x2 = s2->x, y2 = s2->y,
+                                       r2 = s2->ship->radius;
+
+                               if (square_intersect(x1, y1, r1, x2, y2, r2) &&
+                                   circle_intersect(x1, y1, r1, x2, y2, r2)) {
+                                       if (ship_hit_ship(s1, s2)) {
+                                               client_destroy_mark(c);
+                                               client_destroy_mark(c2);
+                                       }
+                                       break;
+                               }
+                       }
+               }
+               if (c->todestroy)
+                       continue;
+
                /* Reset ping request throttling flag */
                c->askedping = 0;
 
@@ -270,6 +294,7 @@ spacket_ship_send(client_t *c, uint16_t id, ship_t *s)
        p.id = BYTEORDER_NETWORK16(id);
        p.x = BYTEORDER_NETWORK16(s->x);
        p.y = BYTEORDER_NETWORK16(s->y);
+       p.radius = (uint8_t)s->ship->radius;
        p.angle = (uint8_t)s->angle;
 
        return client_write(c, (uint8_t *)&p, sizeof(p), 1);
index e341fce..ac33956 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ships.c,v 1.1 2006/12/31 08:32:40 mmondor Exp $ */
+/* $Id: ships.c,v 1.2 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -27,7 +27,7 @@ struct shipdesc ships[SHIP_MAX] = {
                1.0,            /* Hull regen */
                100.0,          /* Shield max */
                10.0,           /* Shield cost */
-               3.0,            /* Shield regen */
+               2.0,            /* Shield regen */
                50.0,           /* Cloak cost */
                10000.0,        /* Fuel max */
                30.0,           /* Fuel regen */
@@ -39,8 +39,8 @@ struct shipdesc ships[SHIP_MAX] = {
                6.0,            /* Thrust cost */
                80/*2.0*/,      /* Turn thrust */
                0.5,            /* Turn cost */
-               100.0,          /* Torp cost */
-               90.0,           /* Torp damage */
+               200.0,          /* Torp cost */
+               20.0,           /* Torp damage */
                2.0,            /* Torp radius */
                11.0,           /* Torp speed */
                60,             /* Torp life */
@@ -69,7 +69,7 @@ struct shipdesc ships[SHIP_MAX] = {
                1.0,            /* Turn thrust */
                0.5,            /* Turn cost */
                90.0,           /* Torp cost */
-               90.0,           /* Torp damage */
+               20.0,           /* Torp damage */
                3.0,            /* Torp radius */
                12.0,           /* Torp speed */
                60,             /* Torp life */
@@ -215,7 +215,8 @@ ship_update(ship_t *s)
                i = ((int)s->thrust) + 1;
                s->x = VECTOR_X(s->x, s->angle, i);
                s->y = VECTOR_Y(s->y, s->angle, i);
-               /* XXX Observe world limits and block/bounce */
+               /* Observe world limits and block/bounce */
+               /* XXX Should use ship radius trigonometrically */
                if (s->x < r || s->x > WORLD_X_MAX - r - 1 ||
                    s->y < r || s->y > WORLD_Y_MAX - r - 1) {
                        s->x = ox;
@@ -242,3 +243,39 @@ ship_update(ship_t *s)
        } else
                s->cloak_on = 0;
 }
+
+int
+ship_hit_torp(ship_t *s, shipdesc_t *tsd)
+{
+
+       /* XXX */
+       if (s->shield_on) {
+               s->shield -= tsd->torp_damage;
+               if (s->shield <= 0) {
+                       s->hull += s->shield;
+                       s->shield = 0.0;
+                       s->shield_on = 0;
+               } else
+                       return 0;
+       } else
+               s->hull -= tsd->torp_damage;
+       if (s->hull <= 0) {
+               s->hull = 0.0;
+               return 1;
+       }
+
+       return 0;
+}
+
+int
+ship_hit_ship(ship_t *s1, ship_t *s2)
+{
+
+       if (s1->cloak_on || s2->cloak_on)
+               return 0;
+
+       /* XXX */
+
+       return 1;
+}
+
index 2c3b595..6b5c1ce 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ships.h,v 1.1 2006/12/31 08:32:40 mmondor Exp $ */
+/* $Id: ships.h,v 1.2 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -18,7 +18,7 @@ enum ship_types {
        SHIP_MAX
 };
 
-struct shipdesc {
+typedef struct shipdesc {
        double          radius;
        double          weight;
        double          hull_max, hull_regen;
@@ -32,10 +32,10 @@ struct shipdesc {
        int             torp_life;
        double          phaser_cost, phaser_damage, phaser_recharge,
                        phaser_radius;
-};
+} shipdesc_t;
 
 typedef struct ship {
-       struct shipdesc *ship;
+       shipdesc_t      *ship;
        double          hull;
        double          shield;
        double          fuel;
@@ -52,7 +52,8 @@ typedef struct ship {
 
 void                   ship_init(ship_t *, int);
 void                   ship_update(ship_t *);
-
+int                    ship_hit_torp(ship_t *, shipdesc_t *);
+int                    ship_hit_ship(ship_t *, ship_t *);
 
 
 extern struct shipdesc ships[SHIP_MAX];
index 2680216..e56913d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: torp.c,v 1.1 2006/12/31 08:32:40 mmondor Exp $ */
+/* $Id: torp.c,v 1.2 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -14,6 +14,8 @@
 
 #include <torp.h>
 #include <trigonometry.h>
+#include <client.h>
+#include <ships.h>
 #include <conf.h>
 
 
@@ -38,32 +40,37 @@ torp_init(void)
 void
 torp_create(client_t *c, int angle)
 {
-       torp_t  *t;
+       torp_t          *t;
+       ship_t          *s = &c->ship;
+       shipdesc_t      *sd = s->ship;
 
-       if (c->ship.torps > 15)
+       if (s->cloak_on)
+               return;
+       if (s->torps > 15)
                return;
 
-       if (c->ship.fuel < c->ship.ship->torp_cost)
+       if (s->fuel < sd->torp_cost)
                return;
-       c->ship.fuel -= c->ship.ship->torp_cost;
+       s->fuel -= sd->torp_cost;
 
        if ((t = (torp_t *)pool_alloc(&torps_pool, FALSE)) != NULL) {
-               int     r = c->ship.ship->torp_radius;
+               int     tr = sd->torp_radius,
+                       sr = sd->radius + sd->torp_speed + 5;
 
                t->client = c;
-               t->life = t->client->ship.ship->torp_life;
+               t->life = sd->torp_life;
                t->angle = angle;
-               t->x = VECTOR_X(c->ship.x, angle, c->ship.ship->radius);
-               t->y = VECTOR_Y(c->ship.y, angle, c->ship.ship->radius);
-               if (t->x < r)
-                       t->x = r;
-               if (t->y < r)
-                       t->y = r;
-               if (t->x > WORLD_X_MAX - 1 - r)
-                       t->x = WORLD_X_MAX - 1 - r;
-               if (t->y > WORLD_Y_MAX - 1 - r)
-                       t->x = WORLD_Y_MAX - 1 - r;
-               c->ship.torps++;
+               t->x = VECTOR_X(s->x, angle, sr + tr);
+               t->y = VECTOR_Y(s->y, angle, sr + tr);
+               if (t->x < tr)
+                       t->x = tr;
+               if (t->y < tr)
+                       t->y = tr;
+               if (t->x > WORLD_X_MAX - 1 - tr)
+                       t->x = WORLD_X_MAX - 1 - tr;
+               if (t->y > WORLD_Y_MAX - 1 - tr)
+                       t->x = WORLD_Y_MAX - 1 - tr;
+               s->torps++;
                DLIST_APPEND(&torps_list, (node_t *)t);
        } else
                syslog(LOG_NOTICE, "torp_create() - pool_alloc(torps_list)");
@@ -92,6 +99,30 @@ torps_update(void)
 
        for (t = (torp_t *)DLIST_TOP(&torps_list); t != NULL; t = next) {
                next = DLIST_NEXT((node_t *)t);
+               client_t        *c, *cnext;
+               shipdesc_t      *tsd = t->client->ship.ship;
+               int             tx = t->x, ty = t->y,
+                               tr = tsd->torp_radius,
+                               torp_destroyed = 0;
+
+               /* Verify for torp/ship collisions */
+               for (c = (client_t *)DLIST_TOP(&clients_list); c != NULL; c = cnext) {
+                       cnext = DLIST_NEXT((node_t *)c);
+                       ship_t  *s = &c->ship;
+                       int     sx = s->x, sy = s->y, sr = s->ship->radius;
+
+                       if (square_intersect(tx, ty, tr, sx, sy, sr) &&
+                           circle_intersect(tx, ty, tr, sx, sy, sr)) {
+                               if (ship_hit_torp(s, tsd))
+                                       client_destroy_mark(c);
+                               torp_destroyed = 1;
+                               continue;
+                       }
+               }
+               if (torp_destroyed) {
+                       torp_destroy(t);
+                       continue;
+               }
 
                if (--(t->life) == 0) {
                        torp_destroy(t);
@@ -100,8 +131,8 @@ torps_update(void)
 
                ox = t->x;
                oy = t->y;
-               s = t->client->ship.ship->torp_speed;
-               r = t->client->ship.ship->torp_radius;
+               s = tsd->torp_speed;
+               r = tsd->torp_radius;
                t->x = VECTOR_X(t->x, t->angle, s);
                t->y = VECTOR_Y(t->y, t->angle, s);
                t->x += (-4 + (random() % 8));
index bf2fced..d160347 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: trigonometry.c,v 1.1 2006/12/31 08:32:40 mmondor Exp $ */
+/* $Id: trigonometry.c,v 1.2 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -8,6 +8,7 @@
 
 
 #include <math.h>
+#include <stdlib.h>
 
 #include <mmlog.h>
 
@@ -46,5 +47,28 @@ angle_bounce(int angle)
         * Should we be told via another parameter the relative angle of
         * the object we had collision with?  Or the direction?
         */
+       /*
        return ((angle + 128) & 0xff);
+       */
+       /* XXX Debugging */
+       return (random() % 256);
+}
+
+inline int
+square_intersect(int x1, int y1, int r1, int x2, int y2, int r2)
+{
+
+       return (x1 + r1 >= x2 - r2 && x1 - r1 <= x2 + r2 &&
+               y1 + r1 >= y2 - r2 && y1 - r1 <= y2 + r2);
 }
+
+inline int
+circle_intersect(int x1, int y1, int r1, int x2, int y2, int r2)
+{
+       int     dx = x2 - x1, dy = y2 - y1,
+               ms = (dx * dx) + (dy * dy),     /* sqrt(dx * dx + dy * dy) */
+               mr = r1 + r2;
+
+       return (ms < mr * mr);                  /* ms < r1 + r2 */
+}
+
index f34b447..03cabb2 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: trigonometry.h,v 1.1 2006/12/31 08:32:40 mmondor Exp $ */
+/* $Id: trigonometry.h,v 1.2 2014/06/21 17:16:32 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -19,8 +19,8 @@
 
 void                           trigonometry_init(void);
 int                            angle_bounce(int);
-
-
+inline int                     square_intersect(int, int, int, int, int, int);
+inline int                     circle_intersect(int, int, int, int, int, int);
 
 extern double                  sin_table[256], cos_table[256];