Permalink
Browse files

Whoohoo. Funktioniert. Client und Server sind getrennt.

Noch zu tun: 

    och gibt es kein richtiges Handshake, so dass Aufloesung, 
    ersion, etc von Client und Server beim Starten uebereinstimmen
    muss. 

    Der Server benoetigt im Moment noch libSDL (wegen SDL_GetTicks
    und SDL_Delay)

    Der Server sollte auf 20 FPS begrenzt werden um die Paketanzahl
    niedrig zu halten

    Die Position der Kreaturen sollte nicht direkt uebertragen werden, 
    sondern nur das anzulaufende Ziel und die Geschwindigkeit.
    Der Client sollte daraus eine glatte Bewegung erzeugen.

    Testen, sehr viel Testen :)
    


git-svn-id: http://infon.googlecode.com/svn/trunk@14 8171fb75-e542-0410-96e4-03d5dd800671
  • Loading branch information...
1 parent 32503d5 commit 5e2869b4b00622062b495aa8a0ae4543b0958c43 @dividuum committed Jun 29, 2006
Showing with 530 additions and 1,145 deletions.
  1. +7 −6 Makefile
  2. +14 −9 client.c
  3. +5 −3 client.rb
  4. +58 −0 common_creature.h
  5. +12 −0 common_player.h
  6. +31 −91 creature.c
  7. +7 −36 creature.h
  8. +1 −1 global.h
  9. +185 −0 gui_creature.c
  10. +51 −0 gui_creature.h
  11. +36 −829 gui_player.c
  12. +8 −61 gui_player.h
  13. +3 −3 gui_scroller.c
  14. +17 −33 gui_world.c
  15. +21 −8 infon.c
  16. +4 −8 packet.c
  17. +4 −5 packet.h
  18. +25 −25 player.c
  19. +1 −12 player.h
  20. +3 −2 scroller.c
  21. +11 −3 server.c
  22. +7 −0 server.h
  23. +1 −1 sprite.h
  24. +18 −9 world.c
View
@@ -5,19 +5,20 @@ CFLAGS = -I$(LUADIR)/include/ -I$(SDLDIR)/include/SDL -std=gnu99 -Wall
# CFLAGS += -O3 -fexpensive-optimizations -finline-functions -fomit-frame-pointer -DNDEBUG
CFLAGS += -ggdb
-LDFLAGS = -L$(LUADIR)/lib -L$(SDLDIR)/lib -levent -llua -llualib -lm -lSDL
-GUI_LDFLAGS = $(LDFLAGS) -lSDL_image -lSGE -lSDL_gfx
+LDFLAGS = -L$(LUADIR)/lib -levent -lSDL -llua -llualib -lm
+GUI_LDFLAGS = -L$(SDLDIR)/lib -levent -lSDL -lSDL_image -lSGE -lSDL_gfx
all: infond infon
-infond: infond.o server.o listener.o map.o path.o misc.o packet.o player.o world.o creature.o scroller.o
- $(MAKE) -C $(LUADIR)
+infond: lua-5.0.2/lib/liblua.a infond.o server.o listener.o map.o path.o misc.o packet.o player.o world.o creature.o scroller.o
$(CC) $^ $(LDFLAGS) -o $@
-infon: infon.o client.o packet.o gui_player.o gui_world.o gui_creature.o gui_scroller.o
- $(MAKE) -C $(LUADIR)
+infon: lua-5.0.2/lib/liblua.a infon.o client.o packet.o misc.o gui_player.o gui_world.o gui_creature.o gui_scroller.o video.o sprite.o
$(CC) $^ $(GUI_LDFLAGS) -o $@
+lua-5.0.2/lib/liblua.a:
+ $(MAKE) -C $(LUADIR)
+
clean:
$(MAKE) -C $(LUADIR) clean
-rm -f *.o infond infon tags
View
@@ -36,12 +36,12 @@
#include "packet.h"
#include "global.h"
-#include "player.h"
#include "server.h"
-#include "world.h"
#include "misc.h"
-#include "scroller.h"
-#include "creature.h"
+#include "gui_player.h"
+#include "gui_world.h"
+#include "gui_scroller.h"
+#include "gui_creature.h"
static int serverfd;
static struct event rd_event;
@@ -57,14 +57,16 @@ static void server_handle_packet(packet_t *packet) {
//printf("ptype=%d\n", packet->type);
switch (packet->type) {
case PACKET_PLAYER_UPDATE:
- player_from_network(packet);
+ gui_player_from_network(packet);
break;
case PACKET_WORLD_UPDATE:
- world_from_network(packet);
+ gui_world_from_network(packet);
break;
- //case PACKET_CREATURE_UPDATE: creature_f
+ case PACKET_CREATURE_UPDATE:
+ gui_creature_from_network(packet);
+ break;
case PACKET_SCROLLER_MSG:
- scroller_from_network(packet);
+ gui_scroller_from_network(packet);
break;
case PACKET_WELCOME_MSG:
server_writeto("guiclient\n", 10);
@@ -74,6 +76,9 @@ static void server_handle_packet(packet_t *packet) {
packet->len, packet->data);
server_destroy("done");
break;
+ case PACKET_KOTH_UPDATE:
+ gui_player_king_from_network(packet);
+ break;
default:
printf("packet->type %d unknown\n", packet->type);
break;
@@ -95,7 +100,7 @@ static void server_readable(int fd, short event, void *arg) {
packet_t packet;
memcpy(&packet,EVBUFFER_DATA(in_buf), (int)EVBUFFER_DATA(in_buf)[0] + 2);
int len = packet.len + 2;
- packet_reset(&packet);
+ packet_rewind(&packet);
server_handle_packet(&packet);
if (!client_is_connected())
return;
View
@@ -38,7 +38,7 @@ def read32
print "score=%d " % (socket.read16 - 500) if mask & 16 != 0
puts
when 1:
- puts "%d, %d => %d (%d) " % [socket.read8, socket.read8, socket.read8, socket.read16]
+ puts "%d, %d => %d (%d) " % [socket.read8, socket.read8, socket.read8, socket.read8]
when 2:
puts "msg: %s " % socket.read(len).unpack("A*")[0]
when 3:
@@ -55,11 +55,13 @@ def read32
print "message=%s " % socket.read(socket.read8).unpack("A*")[0] if mask &128 != 0
puts
when 4:
- puts "quit msg: %s " % socket.read(len).unpack("A*")[0]
+ puts "quit msg: %s " % socket.read(len).unpack("A*")[0]
break
+ when 5:
+ puts "king: %d " % socket.read8
when 32:
socket.write("guiclient\n")
- puts "welcome: %s" % socket.read(len).delete("\n").strip
+ puts "welcome: %s" % socket.read(len).delete("\n").strip
else
puts "???: #{type}"
end
View
@@ -0,0 +1,58 @@
+/*
+
+ Copyright (c) 2006 Florian Wesch <fw@dividuum.de>. All Rights Reserved.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+*/
+
+#ifndef COMMON_CREATURE_H
+#define COMMON_CREATURE_H
+
+#define MAXCREATURES 256
+
+#define CREATURE_COLORS 16
+#define CREATURE_TYPES 4
+#define CREATURE_DIRECTIONS 32
+#define CREATURE_ANIMS 2
+
+#define CREATURE_STATES 8
+
+#define CREATURE_NETWORK_RESOLUTION 16
+
+typedef enum {
+ CREATURE_IDLE,
+ CREATURE_WALK,
+ CREATURE_HEAL,
+ CREATURE_EAT,
+ CREATURE_ATTACK,
+ CREATURE_CONVERT,
+ CREATURE_SPAWN,
+ CREATURE_FEED,
+} creature_state;
+
+#define CREATURE_DIRTY_ALIVE (1 << 0)
+#define CREATURE_DIRTY_POS (1 << 1)
+#define CREATURE_DIRTY_TYPE (1 << 2)
+#define CREATURE_DIRTY_FOOD (1 << 3)
+#define CREATURE_DIRTY_HEALTH (1 << 4)
+#define CREATURE_DIRTY_STATE (1 << 5)
+#define CREATURE_DIRTY_TARGET (1 << 6)
+#define CREATURE_DIRTY_MESSAGE (1 << 7)
+
+#define CREATURE_DIRTY_ALL 0xFF
+#define CREATURE_DIRTY_NONE 0x00
+
+#endif
View
@@ -21,3 +21,15 @@
#ifndef COMMON_PLAYER_H
#define COMMON_PLAYER_H
+#define MAXPLAYERS 32
+
+#define PLAYER_DIRTY_ALIVE (1 << 0)
+#define PLAYER_DIRTY_NAME (1 << 1)
+#define PLAYER_DIRTY_COLOR (1 << 2)
+#define PLAYER_DIRTY_CPU (1 << 3)
+#define PLAYER_DIRTY_SCORE (1 << 4)
+
+#define PLAYER_DIRTY_ALL 0x1F
+#define PLAYER_DIRTY_NONE 0x00
+
+#endif
View
@@ -122,68 +122,6 @@ creature_t *creature_nearest_enemy(const creature_t *reference, int *distptr) {
return nearest;
}
-#ifdef SERVER_GUI
-void creature_draw() {
- creature_t *creature = &creatures[0];
- for (int i = 0; i < MAXCREATURES; i++, creature++) {
- if (!CREATURE_USED(creature))
- continue;
-
- const int x = X_TO_SCREENX(creature->x) - 7;
- const int y = Y_TO_SCREENY(creature->y) - 7;
- const int hw = creature->network_health * 16 / 100;
- const int fw = creature->network_food * 16 / 100;
-
- if (fw != 16) video_rect(x + fw, y - 4, x + 16, y - 2, 0x00, 0x00, 0x00, 0xB0);
- if (fw != 0) video_rect(x, y - 4, x + fw, y - 2, 0xFF, 0xFF, 0xFF, 0xB0);
-
- if (hw != 16) video_rect(x + hw, y - 2, x + 16, y, 0xFF, 0x00, 0x00, 0xB0);
- if (hw != 0) video_rect(x, y - 2, x + hw, y, 0x00, 0xFF, 0x00, 0xB0);
-
- video_draw(x, y, sprite_get(CREATURE_SPRITE(creature->player->color,
- creature->type,
- creature->dir,
- (game_time >> 7) % 2)));
-
- //if (game_time > creature->last_state_change + 300 &&
- // game_time < creature->last_state_change + 1000)
- video_draw(x + 15, y - 10, sprite_get(SPRITE_THOUGHT + creature->state));
-
- if (game_time < creature->last_msg_set + 2000)
- video_tiny(x - strlen(creature->message) * 6 / 2 + 9, y + 14, creature->message);
-
- /*
- if (creature->path) {
- int lastx = X_TO_SCREENX(creature->x);
- int lasty = Y_TO_SCREENY(creature->y);
- pathnode_t *node = creature->path;
- while (node) {
- int curx = node->x * SPRITE_TILE_SIZE / TILE_WIDTH;
- int cury = node->y * SPRITE_TILE_SIZE / TILE_HEIGHT;
- video_line(lastx, lasty, curx, cury);
- lastx = curx, lasty = cury;
- node = node->next;
- }
- }
- */
-
- if (creature->state == CREATURE_ATTACK && creature->spawn_time != game_time) {
- assert(creature->target >= 0 && creature->target < MAXCREATURES);
- const creature_t *target = &creatures[creature->target];
- if (target) { // Das Angegriffene Vieh koennte in dieser Runde bereits getoetet sein.
- video_line(x + 6, y + 6, X_TO_SCREENX(target->x) - 3, Y_TO_SCREENY(target->y) - 3);
- video_line(x + 6, y + 6, X_TO_SCREENX(target->x) - 3, Y_TO_SCREENY(target->y) + 3);
- video_line(x + 6, y + 6, X_TO_SCREENX(target->x) + 3, Y_TO_SCREENY(target->y) - 3);
- video_line(x + 6, y + 6, X_TO_SCREENX(target->x) + 3, Y_TO_SCREENY(target->y) + 3);
- }
- }
- }
-}
-#else
-void creature_draw() {
-}
-#endif
-
// ------------- Bewegung -------------
@@ -294,8 +232,8 @@ void creature_do_heal(creature_t *creature, int delta) {
if (max_heal > creature->food)
max_heal = creature->food;
- creature->health += max_heal;
- creature->food -= max_heal;
+ creature->health += max_heal;
+ creature->food -= max_heal;
if (max_heal == 0)
creature_set_state(creature, CREATURE_IDLE);
@@ -437,7 +375,9 @@ void creature_do_convert(creature_t *creature, int delta) {
creature->health = creature_max_health(creature);
if (creature->food > creature_max_food(creature))
creature->food = creature_max_food(creature);
- creature->dirtymask |= CREATURE_DIRTY_TYPE;
+ creature->dirtymask |= CREATURE_DIRTY_TYPE |
+ CREATURE_DIRTY_HEALTH |
+ CREATURE_DIRTY_FOOD;
creature_set_state(creature, CREATURE_IDLE);
}
}
@@ -727,31 +667,39 @@ next_creature: ;
for (int i = 0; i < MAXCREATURES; i++, creature++) {
if (!CREATURE_USED(creature))
continue;
-
+
// Leben/Food Prozentangaben aktualisieren
- int newhealth = 100 * creature->health / creature_max_health(creature);
+ int newhealth = 16 * creature->health / creature_max_health(creature);
if (newhealth != creature->network_health) {
creature->network_health = newhealth;
creature->dirtymask |= CREATURE_DIRTY_HEALTH;
}
- int newfood = 100 * creature->food / creature_max_food(creature);
+ int newfood = 16 * creature->food / creature_max_food(creature);
if (newfood != creature->network_food) {
creature->network_food = newfood;
creature->dirtymask |= CREATURE_DIRTY_FOOD;
}
- if (creature->x != creature->network_x ||
- creature->y != creature->network_y ||
- creature->dir != creature->network_dir)
- {
- creature->network_x = creature->x;
- creature->network_y = creature->y;
- creature->network_dir = creature->dir;
- creature->dirtymask |= CREATURE_DIRTY_POS;
+ int newx = creature->x / CREATURE_NETWORK_RESOLUTION;
+ if (newx != creature->network_x) {
+ creature->network_x = newx;
+ creature->dirtymask |= CREATURE_DIRTY_POS;
+ }
+
+ int newy = creature->y / CREATURE_NETWORK_RESOLUTION;
+ if (newy != creature->network_y) {
+ creature->network_y = newy;
+ creature->dirtymask |= CREATURE_DIRTY_POS;
}
- creature_to_network(creature, creature->dirtymask, PACKET_BROADCAST);
+ int newdir= creature->dir;
+ if (newdir != creature->network_dir) {
+ creature->network_dir = newdir;
+ creature->dirtymask |= CREATURE_DIRTY_POS;
+ }
+
+ creature_to_network(creature, creature->dirtymask, SEND_BROADCAST);
creature->dirtymask = CREATURE_DIRTY_NONE;
}
@@ -789,9 +737,8 @@ int creature_set_path(creature_t *creature, int x, int y) {
}
void creature_set_message(creature_t *creature, const char *message) {
- if (strcmp(creature->message, message)) {
+ if (strncmp(creature->message, message, sizeof(creature->message) - 1)) {
snprintf(creature->message, sizeof(creature->message), "%s", message);
- creature->last_msg_set = game_time;
creature->dirtymask |= CREATURE_DIRTY_MESSAGE;
}
}
@@ -818,23 +765,16 @@ creature_t *creature_spawn(player_t *player, int x, int y, int type, int points)
creature->convert_type = type;
creature->spawn_food = 0;
creature->message[0] = '\0';
- creature->last_msg_set = 0;
creature->spawn_time = game_time;
creature->last_state_change = game_time;
creature->age_action_deltas = 0;
creature->dirtymask = 0;
- creature->network_x = x;
- creature->network_y = y;
- creature->network_dir = 0;
- creature->network_health = 100;
- creature->network_food = 0;
-
player_on_creature_spawned(player, creature_num(creature), points);
- creature_to_network(creature, CREATURE_DIRTY_ALL, PACKET_BROADCAST);
+ creature_to_network(creature, CREATURE_DIRTY_ALL, SEND_BROADCAST);
return creature;
}
@@ -854,7 +794,7 @@ void creature_kill(creature_t *creature, creature_t *killer) {
path_delete(creature->path);
creature->player = NULL;
- creature_to_network(creature, CREATURE_DIRTY_ALIVE, PACKET_BROADCAST);
+ creature_to_network(creature, CREATURE_DIRTY_ALIVE, SEND_BROADCAST);
}
void creature_kill_all_players_creatures(player_t *player) {
@@ -883,13 +823,13 @@ void creature_to_network(creature_t *creature, int dirtymask, client_t *client)
return;
packet_t packet;
- packet_reset(&packet);
+ packet_init(&packet, PACKET_CREATURE_UPDATE);
packet_write16(&packet, creature_num(creature));
packet_write08(&packet, dirtymask);
if (dirtymask & CREATURE_DIRTY_ALIVE) {
if (CREATURE_USED(creature))
- packet_write08(&packet, player_num(creature->player));
+ packet_write08(&packet, creature->player->color);
else
packet_write08(&packet, 0xFF);
}
@@ -913,7 +853,7 @@ void creature_to_network(creature_t *creature, int dirtymask, client_t *client)
packet_writeXX(&packet, &creature->message, strlen(creature->message));
}
- packet_send(PACKET_CREATURE_UPDATE, &packet, client);
+ client_send_packet(&packet, client);
}
Oops, something went wrong.

0 comments on commit 5e2869b

Please sign in to comment.