From 2d776fcd61be168e1ff0ff2e4a5e71835e7cea19 Mon Sep 17 00:00:00 2001 From: oroborus Date: Sat, 28 Jun 2008 16:38:35 +0000 Subject: - prekopanie adresarovej struktury, prva cast git-svn-id: http://opensvn.csie.org/tuxanci_ng@77 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- Makefile | 2 +- config.h | 5 +- src/Makefile | 223 +-------- src/Makefile-listserver | 27 -- src/Makefile-nosound | 197 +------- src/Makefile-publicServer | 106 ----- src/Makefile-sdlnet | 29 ++ src/Makefile-server | 26 ++ src/arena.c | 233 ---------- src/arenaFile.c | 240 ---------- src/audio.c | 44 -- src/audio/Makefile | 17 + src/audio/audio.c | 44 ++ src/audio/audio.h | 12 + src/audio/music.c | 210 +++++++++ src/audio/music.h | 36 ++ src/audio/sound.c | 137 ++++++ src/audio/sound.h | 33 ++ src/base/Makefile | 67 +++ src/base/arena.c | 233 ++++++++++ src/base/arena.h | 43 ++ src/base/arenaFile.c | 269 +++++++++++ src/base/arenaFile.h | 22 + src/base/bool.h | 13 + src/base/checkFront.c | 136 ++++++ src/base/checkFront.h | 22 + src/base/director.c | 64 +++ src/base/director.h | 18 + src/base/fake_audio.c | 89 ++++ src/base/gun.c | 353 ++++++++++++++ src/base/gun.h | 21 + src/base/homeDirector.c | 42 ++ src/base/homeDirector.h | 13 + src/base/idManager.c | 107 +++++ src/base/idManager.h | 19 + src/base/item.c | 620 +++++++++++++++++++++++++ src/base/item.h | 79 ++++ src/base/list.c | 241 ++++++++++ src/base/list.h | 33 ++ src/base/main.c | 126 +++++ src/base/main.h | 33 ++ src/base/modules.c | 222 +++++++++ src/base/modules.h | 78 ++++ src/base/myTimer.c | 148 ++++++ src/base/myTimer.h | 32 ++ src/base/net_multiplayer.c | 172 +++++++ src/base/net_multiplayer.h | 35 ++ src/base/path.h | 33 ++ src/base/protect.c | 88 ++++ src/base/protect.h | 30 ++ src/base/proto.c | 713 +++++++++++++++++++++++++++++ src/base/proto.h | 81 ++++ src/base/server.c | 984 +++++++++++++++++++++++++++++++++++++++ src/base/server.h | 93 ++++ src/base/shot.c | 369 +++++++++++++++ src/base/shot.h | 61 +++ src/base/space.c | 600 ++++++++++++++++++++++++ src/base/space.h | 38 ++ src/base/storage.c | 138 ++++++ src/base/storage.h | 16 + src/base/string_length.h | 16 + src/base/textFile.c | 163 +++++++ src/base/textFile.h | 21 + src/base/tux.c | 803 ++++++++++++++++++++++++++++++++ src/base/tux.h | 137 ++++++ src/buffer.c | 121 ----- src/checkFront.c | 136 ------ src/client.c | 306 ------------- src/client/Makefile | 36 ++ src/client/client.c | 305 +++++++++++++ src/client/client.h | 35 ++ src/client/configFile.c | 136 ++++++ src/client/configFile.h | 20 + src/client/font.c | 99 ++++ src/client/font.h | 26 ++ src/client/game.c | 120 +++++ src/client/game.h | 9 + src/client/image.c | 149 ++++++ src/client/image.h | 35 ++ src/client/interface.c | 256 +++++++++++ src/client/interface.h | 40 ++ src/client/keytable.c | 136 ++++++ src/client/keytable.h | 27 ++ src/client/language.c | 157 +++++++ src/client/language.h | 12 + src/client/layer.c | 155 +++++++ src/client/layer.h | 53 +++ src/client/panel.c | 190 ++++++++ src/client/panel.h | 27 ++ src/client/screen.c | 137 ++++++ src/client/screen.h | 32 ++ src/client/term.c | 106 +++++ src/client/term.h | 17 + src/configFile.c | 136 ------ src/director.c | 66 --- src/fake_audio.c | 89 ---- src/font.c | 101 ---- src/game.c | 115 ----- src/gun.c | 350 -------------- src/hashTable.c | 221 --------- src/heightScore.c | 98 ---- src/homeDirector.c | 42 -- src/idManager.c | 107 ----- src/image.c | 150 ------ src/interface.c | 256 ----------- src/item.c | 610 ------------------------- src/keytable.c | 135 ------ src/language.c | 155 ------- src/layer.c | 153 ------- src/list.c | 241 ---------- src/listServer.c | 277 ----------- src/logFile.c | 46 -- src/main.c | 125 ----- src/modules.c | 223 --------- src/modules/Makefile | 22 + src/modules/modAI.c | 256 +++++++++++ src/modules/modPipe.c | 453 ++++++++++++++++++ src/modules/modTeleport.c | 506 +++++++++++++++++++++ src/modules/modWall.c | 331 ++++++++++++++ src/music.c | 210 --------- src/myTimer.c | 149 ------ src/net_multiplayer.c | 168 ------- src/net_sdl/Makefile | 9 + src/net_sdl/sdl_udp.c | 219 +++++++++ src/net_sdl/sdl_udp.h | 28 ++ src/net_unix/Makefile | 9 + src/net_unix/udp.c | 358 +++++++++++++++ src/net_unix/udp.h | 31 ++ src/network.c | 101 ---- src/panel.c | 190 -------- src/protect.c | 88 ---- src/proto.c | 714 ----------------------------- src/publicServer.c | 191 -------- src/screen.c | 136 ------ src/screen/Makefile | 29 ++ src/screen/screen_analyze.c | 179 ++++++++ src/screen/screen_analyze.h | 25 + src/screen/screen_choiceArena.c | 172 +++++++ src/screen/screen_choiceArena.h | 17 + src/screen/screen_credits.c | 134 ++++++ src/screen/screen_credits.h | 20 + src/screen/screen_gameType.c | 256 +++++++++++ src/screen/screen_gameType.h | 17 + src/screen/screen_mainMenu.c | 128 ++++++ src/screen/screen_mainMenu.h | 15 + src/screen/screen_setting.c | 532 ++++++++++++++++++++++ src/screen/screen_setting.h | 20 + src/screen/screen_table.c | 240 ++++++++++ src/screen/screen_table.h | 17 + src/screen/screen_world.c | 603 ++++++++++++++++++++++++ src/screen/screen_world.h | 31 ++ src/screen_analyze.c | 173 ------- src/screen_choiceArena.c | 167 ------- src/screen_credits.c | 128 ------ src/screen_gameType.c | 252 ---------- src/screen_mainMenu.c | 121 ----- src/screen_setting.c | 527 --------------------- src/screen_table.c | 235 ---------- src/screen_world.c | 588 ------------------------ src/sdl_udp.c | 219 --------- src/server.c | 986 ---------------------------------------- src/server/Makefile | 15 + src/server/heightScore.c | 98 ++++ src/server/heightScore.h | 13 + src/server/logFile.c | 46 ++ src/server/logFile.h | 18 + src/server/publicServer.c | 192 ++++++++ src/server/publicServer.h | 18 + src/server/serverConfigFile.c | 86 ++++ src/server/serverConfigFile.h | 11 + src/serverConfigFile.c | 86 ---- src/shot.c | 369 --------------- src/sound.c | 137 ------ src/space.c | 600 ------------------------ src/storage.c | 138 ------ src/tcp.c | 235 ---------- src/term.c | 105 ----- src/textFile.c | 164 ------- src/tmp | 10 - src/tux.c | 792 -------------------------------- src/udp.c | 356 --------------- src/widget/Makefile | 28 ++ src/widget/widget_button.c | 84 ++++ src/widget/widget_button.h | 27 ++ src/widget/widget_buttonimage.c | 55 +++ src/widget/widget_buttonimage.h | 27 ++ src/widget/widget_check.c | 84 ++++ src/widget/widget_check.h | 27 ++ src/widget/widget_image.c | 35 ++ src/widget/widget_image.h | 21 + src/widget/widget_label.c | 47 ++ src/widget/widget_label.h | 26 ++ src/widget/widget_select.c | 97 ++++ src/widget/widget_select.h | 26 ++ src/widget/widget_textfield.c | 194 ++++++++ src/widget/widget_textfield.h | 33 ++ src/widget_button.c | 82 ---- src/widget_buttonimage.c | 56 --- src/widget_check.c | 85 ---- src/widget_image.c | 33 -- src/widget_label.c | 47 -- src/widget_select.c | 99 ---- src/widget_textfield.c | 194 -------- src/zal_pipe.c | 256 ----------- src/zal_proto.c | 692 ---------------------------- 205 files changed, 16609 insertions(+), 15164 deletions(-) mode change 100755 => 100644 src/Makefile delete mode 100755 src/Makefile-listserver mode change 100755 => 100644 src/Makefile-nosound delete mode 100755 src/Makefile-publicServer create mode 100644 src/Makefile-sdlnet create mode 100644 src/Makefile-server delete mode 100644 src/arena.c delete mode 100644 src/arenaFile.c delete mode 100644 src/audio.c create mode 100644 src/audio/Makefile create mode 100644 src/audio/audio.c create mode 100644 src/audio/audio.h create mode 100644 src/audio/music.c create mode 100644 src/audio/music.h create mode 100644 src/audio/sound.c create mode 100644 src/audio/sound.h create mode 100644 src/base/Makefile create mode 100644 src/base/arena.c create mode 100644 src/base/arena.h create mode 100644 src/base/arenaFile.c create mode 100644 src/base/arenaFile.h create mode 100644 src/base/bool.h create mode 100644 src/base/checkFront.c create mode 100644 src/base/checkFront.h create mode 100755 src/base/director.c create mode 100644 src/base/director.h create mode 100644 src/base/fake_audio.c create mode 100644 src/base/gun.c create mode 100644 src/base/gun.h create mode 100755 src/base/homeDirector.c create mode 100755 src/base/homeDirector.h create mode 100644 src/base/idManager.c create mode 100644 src/base/idManager.h create mode 100644 src/base/item.c create mode 100644 src/base/item.h create mode 100755 src/base/list.c create mode 100755 src/base/list.h create mode 100644 src/base/main.c create mode 100644 src/base/main.h create mode 100755 src/base/modules.c create mode 100644 src/base/modules.h create mode 100644 src/base/myTimer.c create mode 100644 src/base/myTimer.h create mode 100644 src/base/net_multiplayer.c create mode 100644 src/base/net_multiplayer.h create mode 100644 src/base/path.h create mode 100644 src/base/protect.c create mode 100644 src/base/protect.h create mode 100644 src/base/proto.c create mode 100644 src/base/proto.h create mode 100644 src/base/server.c create mode 100644 src/base/server.h create mode 100644 src/base/shot.c create mode 100644 src/base/shot.h create mode 100644 src/base/space.c create mode 100644 src/base/space.h create mode 100644 src/base/storage.c create mode 100644 src/base/storage.h create mode 100644 src/base/string_length.h create mode 100755 src/base/textFile.c create mode 100755 src/base/textFile.h create mode 100644 src/base/tux.c create mode 100644 src/base/tux.h delete mode 100755 src/buffer.c delete mode 100644 src/checkFront.c delete mode 100644 src/client.c create mode 100644 src/client/Makefile create mode 100644 src/client/client.c create mode 100644 src/client/client.h create mode 100644 src/client/configFile.c create mode 100644 src/client/configFile.h create mode 100755 src/client/font.c create mode 100755 src/client/font.h create mode 100644 src/client/game.c create mode 100644 src/client/game.h create mode 100755 src/client/image.c create mode 100755 src/client/image.h create mode 100755 src/client/interface.c create mode 100755 src/client/interface.h create mode 100644 src/client/keytable.c create mode 100644 src/client/keytable.h create mode 100644 src/client/language.c create mode 100644 src/client/language.h create mode 100755 src/client/layer.c create mode 100755 src/client/layer.h create mode 100644 src/client/panel.c create mode 100644 src/client/panel.h create mode 100644 src/client/screen.c create mode 100644 src/client/screen.h create mode 100644 src/client/term.c create mode 100644 src/client/term.h delete mode 100644 src/configFile.c delete mode 100755 src/director.c delete mode 100644 src/fake_audio.c delete mode 100755 src/font.c delete mode 100644 src/game.c delete mode 100644 src/gun.c delete mode 100644 src/hashTable.c delete mode 100644 src/heightScore.c delete mode 100755 src/homeDirector.c delete mode 100644 src/idManager.c delete mode 100755 src/image.c delete mode 100755 src/interface.c delete mode 100644 src/item.c delete mode 100644 src/keytable.c delete mode 100644 src/language.c delete mode 100755 src/layer.c delete mode 100755 src/list.c delete mode 100644 src/listServer.c delete mode 100644 src/logFile.c delete mode 100644 src/main.c delete mode 100755 src/modules.c create mode 100755 src/modules/Makefile create mode 100755 src/modules/modAI.c create mode 100755 src/modules/modPipe.c create mode 100755 src/modules/modTeleport.c create mode 100755 src/modules/modWall.c delete mode 100644 src/music.c delete mode 100644 src/myTimer.c delete mode 100644 src/net_multiplayer.c create mode 100644 src/net_sdl/Makefile create mode 100644 src/net_sdl/sdl_udp.c create mode 100644 src/net_sdl/sdl_udp.h create mode 100644 src/net_unix/Makefile create mode 100644 src/net_unix/udp.c create mode 100644 src/net_unix/udp.h delete mode 100755 src/network.c delete mode 100644 src/panel.c delete mode 100644 src/protect.c delete mode 100644 src/proto.c delete mode 100644 src/publicServer.c delete mode 100644 src/screen.c create mode 100644 src/screen/Makefile create mode 100644 src/screen/screen_analyze.c create mode 100644 src/screen/screen_analyze.h create mode 100644 src/screen/screen_choiceArena.c create mode 100644 src/screen/screen_choiceArena.h create mode 100644 src/screen/screen_credits.c create mode 100644 src/screen/screen_credits.h create mode 100644 src/screen/screen_gameType.c create mode 100644 src/screen/screen_gameType.h create mode 100644 src/screen/screen_mainMenu.c create mode 100644 src/screen/screen_mainMenu.h create mode 100644 src/screen/screen_setting.c create mode 100644 src/screen/screen_setting.h create mode 100644 src/screen/screen_table.c create mode 100644 src/screen/screen_table.h create mode 100644 src/screen/screen_world.c create mode 100644 src/screen/screen_world.h delete mode 100644 src/screen_analyze.c delete mode 100644 src/screen_choiceArena.c delete mode 100644 src/screen_credits.c delete mode 100644 src/screen_gameType.c delete mode 100644 src/screen_mainMenu.c delete mode 100644 src/screen_setting.c delete mode 100644 src/screen_table.c delete mode 100644 src/screen_world.c delete mode 100644 src/sdl_udp.c delete mode 100644 src/server.c create mode 100644 src/server/Makefile create mode 100644 src/server/heightScore.c create mode 100644 src/server/heightScore.h create mode 100644 src/server/logFile.c create mode 100644 src/server/logFile.h create mode 100644 src/server/publicServer.c create mode 100644 src/server/publicServer.h create mode 100644 src/server/serverConfigFile.c create mode 100644 src/server/serverConfigFile.h delete mode 100644 src/serverConfigFile.c delete mode 100644 src/shot.c delete mode 100644 src/sound.c delete mode 100644 src/space.c delete mode 100644 src/storage.c delete mode 100755 src/tcp.c delete mode 100644 src/term.c delete mode 100755 src/textFile.c delete mode 100644 src/tmp delete mode 100644 src/tux.c delete mode 100644 src/udp.c create mode 100644 src/widget/Makefile create mode 100644 src/widget/widget_button.c create mode 100644 src/widget/widget_button.h create mode 100644 src/widget/widget_buttonimage.c create mode 100644 src/widget/widget_buttonimage.h create mode 100644 src/widget/widget_check.c create mode 100644 src/widget/widget_check.h create mode 100644 src/widget/widget_image.c create mode 100644 src/widget/widget_image.h create mode 100644 src/widget/widget_label.c create mode 100644 src/widget/widget_label.h create mode 100644 src/widget/widget_select.c create mode 100644 src/widget/widget_select.h create mode 100644 src/widget/widget_textfield.c create mode 100644 src/widget/widget_textfield.h delete mode 100644 src/widget_button.c delete mode 100644 src/widget_buttonimage.c delete mode 100644 src/widget_check.c delete mode 100644 src/widget_image.c delete mode 100644 src/widget_label.c delete mode 100644 src/widget_select.c delete mode 100644 src/widget_textfield.c delete mode 100644 src/zal_pipe.c delete mode 100644 src/zal_proto.c diff --git a/Makefile b/Makefile index 1397e3c..6b178c3 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ svnr: svn co http://opensvn.csie.org/tuxanci_ng/ server: - make -C ./src -f Makefile-publicServer + make -C ./src -f Makefile-server nosound: make -C ./src -f Makefile-nosound diff --git a/config.h b/config.h index 88cf9d3..b707e8e 100644 --- a/config.h +++ b/config.h @@ -6,9 +6,6 @@ #define DEBUG_CLIENT_RECV */ -#define TUXANCI_NG_VERSION "svn76" +#define TUXANCI_NG_VERSION "svn77" #define DESTDIR "/usr/local/" -#define SUPPORT_NET_UNIX_UDP -//#define SUPPORT_NET_SDL_UDP -//#define PUBLIC_SERVER diff --git a/src/Makefile b/src/Makefile old mode 100755 new mode 100644 index 13f5ba4..a54b508 --- a/src/Makefile +++ b/src/Makefile @@ -1,202 +1,29 @@ -#CC = /usr/local/gcc/bin/gcc CC = gcc - -#CFLAGS = -O2 -DSUPPORT_NET_SDL_UDP -I../include -Wall `sdl-config --cflags` -CFLAGS = -g -O0 -std=c99 -I../include -Wall `sdl-config --cflags` -#CFLAGS = -O2 -DDESTDIR=\"$(DESTDIR)\" `sdl-config --cflags` -I../include -Wall - -BUILD_DIR = . -LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer -lSDL_net - -FILES = interface.o protect.o space.o idManager.o storage.o font.o list.o modules.o\ - image.o layer.o director.o configFile.o tux.o main.o checkFront.o\ - screen.o screen_world.o shot.o myTimer.o panel.o net_multiplayer.o \ - arena.o arenaFile.o textFile.o audio.o sound.o music.o gun.o \ - item.o widget_label.o screen_mainMenu.o widget_button.o screen_analyze.o widget_textfield.o \ - widget_check.o widget_image.o widget_select.o screen_setting.o screen_gameType.o screen_choiceArena.o \ - widget_buttonimage.o screen_credits.o homeDirector.o screen_table.o \ - sdl_udp.o udp.o proto.o server.o client.o term.o keytable.o language.o game.o - -tuxanci-ng: $(FILES) - $(CC) $(CFLAGS) $(LIBS) -o $(BUILD_DIR)/tuxanci-ng $(BUILD_DIR)/*.o - -main.o: main.c - $(CC) $(CFLAGS) -o $(BUILD_DIR)/main.o -c main.c - -protect.o: protect.c - $(CC) $(CFLAGS) -o $(BUILD_DIR)/protect.o -c protect.c - -hashTable.o: hashTable.c ../include/hashTable.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/hashTable.o -c hashTable.c - -space.o: space.c ../include/space.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/space.o -c space.c - -interface.o: interface.c ../include/interface.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/interface.o -c interface.c - -storage.o: storage.c ../include/storage.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/storage.o -c storage.c - -modules.o: modules.c ../include/modules.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/modules.o -c modules.c - -idManager.o: idManager.c ../include/idManager.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/idManager.o -c idManager.c - -checkFront.o: checkFront.c ../include/checkFront.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/checkFront.o -c checkFront.c - -font.o: font.c ../include/font.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/font.o -c font.c - -list.o: list.c ../include/list.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/list.o -c list.c - -image.o: image.c ../include/image.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/image.o -c image.c - -layer.o: layer.c ../include/layer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/layer.o -c layer.c - -director.o: director.c ../include/director.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/director.o -c director.c - -configFile.o: configFile.c ../include/configFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/configFile.o -c configFile.c - -tux.o: tux.c ../include/tux.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/tux.o -c tux.c - -screen.o: screen.c ../include/screen.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen.o -c screen.c - -screen_world.o: screen_world.c ../include/screen_world.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_world.o -c screen_world.c - -#wall.o: wall.c ../include/wall.h -# $(CC) $(CFLAGS) -o $(BUILD_DIR)/wall.o -c wall.c - -shot.o: shot.c ../include/shot.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/shot.o -c shot.c - -myTimer.o: myTimer.c ../include/myTimer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/myTimer.o -c myTimer.c - -panel.o: panel.c ../include/panel.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/panel.o -c panel.c - -tcp.o: tcp.c ../include/tcp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/tcp.o -c tcp.c - -udp.o: udp.c ../include/udp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/udp.o -c udp.c - -sdl_udp.o: sdl_udp.c ../include/sdl_udp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/sdl_udp.o -c sdl_udp.c - -proto.o: proto.c ../include/proto.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/proto.o -c proto.c - -server.o: server.c ../include/server.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/server.o -c server.c - -client.o: client.c ../include/client.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/client.o -c client.c - -network.o: network.c ../include/network.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/network.o -c network.c - -net_multiplayer.o: net_multiplayer.c ../include/net_multiplayer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/net_multiplayer.o -c net_multiplayer.c - -arena.o: arena.c ../include/arena.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/arena.o -c arena.c - -textFile.o: textFile.c ../include/textFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/textFile.o -c textFile.c - -arenaFile.o: arenaFile.c ../include/arenaFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/arenaFile.o -c arenaFile.c - -audio.o: audio.c ../include/audio.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/audio.o -c audio.c - -sound.o: sound.c ../include/sound.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/sound.o -c sound.c - -music.o: music.c ../include/music.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/music.o -c music.c - -gun.o: gun.c ../include/gun.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/gun.o -c gun.c - -item.o: item.c ../include/item.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/item.o -c item.c - -screen_mainMenu.o: screen_mainMenu.c ../include/screen_mainMenu.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_mainMenu.o -c screen_mainMenu.c - -screen_analyze.o: screen_analyze.c ../include/screen_analyze.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_analyze.o -c screen_analyze.c - -screen_setting.o: screen_setting.c ../include/screen_setting.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_setting.o -c screen_setting.c - -screen_gameType.o: screen_gameType.c ../include/screen_gameType.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_gameType.o -c screen_gameType.c - -screen_choiceArena.o: screen_choiceArena.c ../include/screen_choiceArena.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_choiceArena.o -c screen_choiceArena.c - -screen_credits.o: screen_credits.c ../include/screen_credits.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_credits.o -c screen_credits.c - -widget_label.o: widget_label.c ../include/widget_label.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_label.o -c widget_label.c - -widget_button.o: widget_button.c ../include/widget_button.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_button.o -c widget_button.c - -widget_buttonimage.o: widget_buttonimage.c ../include/widget_buttonimage.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_buttonimage.o -c widget_buttonimage.c - -widget_textfield.o: widget_textfield.c ../include/widget_textfield.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_textfield.o -c widget_textfield.c - -widget_check.o: widget_check.c ../include/widget_check.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_check.o -c widget_check.c - -widget_image.o: widget_image.c ../include/widget_image.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_image.o -c widget_image.c - -widget_select.o: widget_select.c ../include/widget_select.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_select.o -c widget_select.c - -#teleport.o: teleport.c ../include/teleport.h -# $(CC) $(CFLAGS) -o $(BUILD_DIR)/teleport.o -c teleport.c - -#pipe.o: pipe.c ../include/pipe.h -# $(CC) $(CFLAGS) -o $(BUILD_DIR)/pipe.o -c pipe.c - -homeDirector.o: homeDirector.c ../include/homeDirector.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/homeDirector.o -c homeDirector.c - -screen_table.o: screen_table.c ../include/screen_table.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_table.o -c screen_table.c - -term.o: term.c ../include/term.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/term.o -c term.c - -keytable.o: keytable.c ../include/keytable.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/keytable.o -c keytable.c - -language.o: language.c ../include/language.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/language.o -c language.c - -game.o: game.c ../include/game.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/game.o -c game.c +CFLAGS = -g -O0 -std=c99 -I.. -Wall `sdl-config --cflags` -DSUPPORT_NET_UNIX_UDP +LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer + +all: + make -C ./audio -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./net_unix -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./client -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./screen -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./widget -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./base -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./modules -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + $(CC) $(CFLAGS) $(LIBS) -o tuxanci-ng base/*.o client/*.o widget/*.o screen/*.o net_unix/*.o audio/*.o + cp modules/*.so ../modules/ + cp tuxanci-ng ../ clean: - rm -rf *.o *.c~ ../include/*.h~ Makefile~ + make -C ./base -f Makefile clean + make -C ./client -f Makefile clean + make -C ./screen -f Makefile clean + make -C ./widget -f Makefile clean + make -C ./net_unix -f Makefile clean + make -C ./net_sdl -f Makefile clean + make -C ./audio -f Makefile clean + make -C ./modules -f Makefile clean + rm -rf ../modules/*.so + rm tuxanci-ng + rm ../tuxanci-ng diff --git a/src/Makefile-listserver b/src/Makefile-listserver deleted file mode 100755 index c2cba31..0000000 --- a/src/Makefile-listserver +++ /dev/null @@ -1,27 +0,0 @@ - -#CC = /usr/local/gcc/bin/gcc -CC = gcc - -#CFLAGS = -O2 -DSUPPORT_NET_SDL_UDP -I../include -Wall `sdl-config --cflags` -CFLAGS = -g -O0 -std=c99 -D_GNU_SOURCE=1 -I../include -Wall -#CFLAGS = -O2 -DDESTDIR=\"$(DESTDIR)\" `sdl-config --cflags` -I../include -Wall - -BUILD_DIR = . -LIBS = - -FILES = listServer.o udp.o list.o - -listServer: $(FILES) - $(CC) $(CFLAGS) $(LIBS) -o $(BUILD_DIR)/listServer $(BUILD_DIR)/*.o - -listServer.o: listServer.c - $(CC) $(CFLAGS) -o $(BUILD_DIR)/listServer.o -c listServer.c - -list.o: list.c ../include/list.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/list.o -c list.c - -udp.o: udp.c ../include/udp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/udp.o -c udp.c - -clean: - rm -rf *.o *.c~ ../include/*.h~ Makefile~ diff --git a/src/Makefile-nosound b/src/Makefile-nosound old mode 100755 new mode 100644 index dbe518f..e672fe1 --- a/src/Makefile-nosound +++ b/src/Makefile-nosound @@ -1,177 +1,28 @@ -#CC = /usr/local/gcc/bin/gcc CC = gcc - -#CFLAGS = -O2 -DSUPPORT_NET_SDL_UDP -I../include -Wall `sdl-config --cflags` -CFLAGS = -g -O0 -std=c99 -DNO_SOUND -I../include -Wall `sdl-config --cflags` -#CFLAGS = -O2 -DDESTDIR=\"$(DESTDIR)\" `sdl-config --cflags` -I../include -Wall - -BUILD_DIR = . -LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_net - -FILES = interface.o font.o list.o image.o layer.o director.o configFile.o tux.o main.o \ - screen.o screen_world.o wall.o shot.o myTimer.o panel.o net_multiplayer.o \ - buffer.o dynamicInt.o arena.o arenaFile.o textFile.o fake_audio.o gun.o \ - item.o widget_label.o screen_mainMenu.o widget_button.o screen_analyze.o widget_textfield.o \ - widget_check.o widget_image.o screen_setting.o screen_gameType.o screen_choiceArena.o \ - widget_buttonimage.o screen_credits.o teleport.o pipe.o homeDirector.o screen_table.o \ - sdl_udp.o proto.o server.o client.o term.o keytable.o language.o game.o - -tuxanci-ng: $(FILES) - $(CC) $(CFLAGS) $(LIBS) -o $(BUILD_DIR)/tuxanci-ng $(BUILD_DIR)/*.o - -main.o: main.c - $(CC) $(CFLAGS) -o $(BUILD_DIR)/main.o -c main.c - -interface.o: interface.c ../include/interface.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/interface.o -c interface.c - -font.o: font.c ../include/font.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/font.o -c font.c - -list.o: list.c ../include/list.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/list.o -c list.c - -image.o: image.c ../include/image.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/image.o -c image.c - -layer.o: layer.c ../include/layer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/layer.o -c layer.c - -director.o: director.c ../include/director.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/director.o -c director.c - -configFile.o: configFile.c ../include/configFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/configFile.o -c configFile.c - -tux.o: tux.c ../include/tux.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/tux.o -c tux.c - -screen.o: screen.c ../include/screen.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen.o -c screen.c - -screen_world.o: screen_world.c ../include/screen_world.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_world.o -c screen_world.c - -wall.o: wall.c ../include/wall.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/wall.o -c wall.c - -shot.o: shot.c ../include/shot.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/shot.o -c shot.c - -myTimer.o: myTimer.c ../include/myTimer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/myTimer.o -c myTimer.c - -panel.o: panel.c ../include/panel.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/panel.o -c panel.c - -buffer.o: buffer.c ../include/buffer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/buffer.o -c buffer.c - -tcp.o: tcp.c ../include/tcp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/tcp.o -c tcp.c - -udp.o: udp.c ../include/udp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/udp.o -c udp.c - -sdl_udp.o: sdl_udp.c ../include/sdl_udp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/sdl_udp.o -c sdl_udp.c - -proto.o: proto.c ../include/proto.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/proto.o -c proto.c - -server.o: server.c ../include/server.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/server.o -c server.c - -client.o: client.c ../include/client.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/client.o -c client.c - -network.o: network.c ../include/network.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/network.o -c network.c - -net_multiplayer.o: net_multiplayer.c ../include/net_multiplayer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/net_multiplayer.o -c net_multiplayer.c - -dynamicInt.o: dynamicInt.c ../include/dynamicInt.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/dynamicInt.o -c dynamicInt.c - -arena.o: arena.c ../include/arena.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/arena.o -c arena.c - -textFile.o: textFile.c ../include/textFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/textFile.o -c textFile.c - -arenaFile.o: arenaFile.c ../include/arenaFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/arenaFile.o -c arenaFile.c - -gun.o: gun.c ../include/gun.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/gun.o -c gun.c - -item.o: item.c ../include/item.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/item.o -c item.c - -screen_mainMenu.o: screen_mainMenu.c ../include/screen_mainMenu.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_mainMenu.o -c screen_mainMenu.c - -screen_analyze.o: screen_analyze.c ../include/screen_analyze.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_analyze.o -c screen_analyze.c - -screen_setting.o: screen_setting.c ../include/screen_setting.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_setting.o -c screen_setting.c - -screen_gameType.o: screen_gameType.c ../include/screen_gameType.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_gameType.o -c screen_gameType.c - -screen_choiceArena.o: screen_choiceArena.c ../include/screen_choiceArena.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_choiceArena.o -c screen_choiceArena.c - -screen_credits.o: screen_credits.c ../include/screen_credits.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_credits.o -c screen_credits.c - -widget_label.o: widget_label.c ../include/widget_label.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_label.o -c widget_label.c - -widget_button.o: widget_button.c ../include/widget_button.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_button.o -c widget_button.c - -widget_buttonimage.o: widget_buttonimage.c ../include/widget_buttonimage.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_buttonimage.o -c widget_buttonimage.c - -widget_textfield.o: widget_textfield.c ../include/widget_textfield.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_textfield.o -c widget_textfield.c - -widget_check.o: widget_check.c ../include/widget_check.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_check.o -c widget_check.c - -widget_image.o: widget_image.c ../include/widget_image.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/widget_image.o -c widget_image.c - -teleport.o: teleport.c ../include/teleport.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/teleport.o -c teleport.c - -pipe.o: pipe.c ../include/pipe.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/pipe.o -c pipe.c - -homeDirector.o: homeDirector.c ../include/homeDirector.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/homeDirector.o -c homeDirector.c - -screen_table.o: screen_table.c ../include/screen_table.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/screen_table.o -c screen_table.c - -term.o: term.c ../include/term.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/term.o -c term.c - -keytable.o: keytable.c ../include/keytable.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/keytable.o -c keytable.c - -language.o: language.c ../include/language.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/language.o -c language.c - -game.o: game.c ../include/game.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/game.o -c game.c - -fake_audio.o: fake_audio.c ../include/fake_audio.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/fake_audio.o -c fake_audio.c +CFLAGS = -g -O0 -std=c99 -I.. -Wall `sdl-config --cflags` -DNO_SOUND -DSUPPORT_NET_UNIX_UDP +LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf + +all: + make -C ./net_unix -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./client -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./screen -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./widget -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./base -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./modules -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + $(CC) $(CFLAGS) $(LIBS) -o tuxanci-ng base/*.o client/*.o widget/*.o screen/*.o net_unix/*.o + cp modules/*.so ../modules/ + cp tuxanci-ng ../ clean: - rm -rf *.o *.c~ ../include/*.h~ Makefile~ + make -C ./base -f Makefile clean + make -C ./client -f Makefile clean + make -C ./screen -f Makefile clean + make -C ./widget -f Makefile clean + make -C ./net_unix -f Makefile clean + make -C ./net_sdl -f Makefile clean + make -C ./audio -f Makefile clean + make -C ./modules -f Makefile clean + rm -rf ../modules/*.so + rm tuxanci-ng + rm ../tuxanci-ng diff --git a/src/Makefile-publicServer b/src/Makefile-publicServer deleted file mode 100755 index 7f1f30c..0000000 --- a/src/Makefile-publicServer +++ /dev/null @@ -1,106 +0,0 @@ - -#CC = /usr/local/gcc/bin/gcc -CC = gcc - -DESTDIR:=/usr/local - -CFLAGS = -g -O0 -I../include -Wall -#CFLAGS = -O2 -DDESTDIR=\"$(DESTDIR)\" `sdl-config --cflags` -I../include -Wall - -BUILD_DIR = ../build-server -LIBS = -ldl - -FILES = list.o space.o protect.o heightScore.o modules.o director.o configFile.o tux.o shot.o myTimer.o \ - arena.o arenaFile.o textFile.o gun.o item.o idManager.o checkFront.o\ - udp.o proto.o server.o publicServer.o net_multiplayer.o serverConfigFile.o main.o - -$(BUILD_DIR)/publicserver: mkdir_build $(FILES) - $(CC) $(CFLAGS) $(LIBS) -o $(BUILD_DIR)/publicserver $(BUILD_DIR)/*.o - -mkdir_build: - mkdir -p $(BUILD_DIR) - -protect.o: protect.c ../include/protect.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/protect.o -c protect.c - -space.o: space.c ../include/space.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/space.o -c space.c - -heightScore.o: heightScore.c - $(CC) $(CFLAGS) -o $(BUILD_DIR)/heightScore.o -c heightScore.c - -main.o: main.c - $(CC) $(CFLAGS) -o $(BUILD_DIR)/main.o -c main.c - -idManager.o: idManager.c ../include/idManager.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/idManager.o -c idManager.c - -modules.o: modules.c - $(CC) $(CFLAGS) -o $(BUILD_DIR)/modules.o -c modules.c - -checkFront.o: checkFront.c ../include/checkFront.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/checkFront.o -c checkFront.c - -publicServer.o: publicServer.c - $(CC) $(CFLAGS) -o $(BUILD_DIR)/publicServer.o -c publicServer.c - -list.o: list.c ../include/list.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/list.o -c list.c - -director.o: director.c ../include/director.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/director.o -c director.c - -configFile.o: configFile.c ../include/configFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/configFile.o -c configFile.c - -tux.o: tux.c ../include/tux.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/tux.o -c tux.c - -shot.o: shot.c ../include/shot.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/shot.o -c shot.c - -myTimer.o: myTimer.c ../include/myTimer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/myTimer.o -c myTimer.c - -tcp.o: tcp.c ../include/tcp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/tcp.o -c tcp.c - -udp.o: udp.c ../include/udp.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/udp.o -c udp.c - -proto.o: proto.c ../include/proto.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/proto.o -c proto.c - -server.o: server.c ../include/server.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/server.o -c server.c - -net_multiplayer.o: net_multiplayer.c ../include/net_multiplayer.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/net_multiplayer.o -c net_multiplayer.c - -arena.o: arena.c ../include/arena.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/arena.o -c arena.c - -textFile.o: textFile.c ../include/textFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/textFile.o -c textFile.c - -arenaFile.o: arenaFile.c ../include/arenaFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/arenaFile.o -c arenaFile.c - -gun.o: gun.c ../include/gun.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/gun.o -c gun.c - -item.o: item.c ../include/item.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/item.o -c item.c - -#teleport.o: teleport.c ../include/teleport.h -# $(CC) $(CFLAGS) -o $(BUILD_DIR)/teleport.o -c teleport.c - -#pipe.o: pipe.c ../include/pipe.h -# $(CC) $(CFLAGS) -o $(BUILD_DIR)/pipe.o -c pipe.c - -serverConfigFile.o: serverConfigFile.c ../include/serverConfigFile.h - $(CC) $(CFLAGS) -o $(BUILD_DIR)/serverConfigFile.o -c serverConfigFile.c - -clean: - rm -rf $(BUILD_DIR) ../include*.h~ Makefile-publicServer~ - diff --git a/src/Makefile-sdlnet b/src/Makefile-sdlnet new file mode 100644 index 0000000..d278d52 --- /dev/null +++ b/src/Makefile-sdlnet @@ -0,0 +1,29 @@ + +CC = gcc +CFLAGS = -g -O0 -std=c99 -I.. -Wall `sdl-config --cflags` -DSUPPORT_NET_SDL_UDP +LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer -lSDL_net + +all: + make -C ./audio -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./net_sdl -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./client -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./screen -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./widget -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./base -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./modules -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + $(CC) $(CFLAGS) $(LIBS) -o tuxanci-ng base/*.o client/*.o widget/*.o screen/*.o net_sdl/*.o audio/*.o + cp modules/*.so ../modules/ + cp tuxanci-ng ../ + +clean: + make -C ./base -f Makefile clean + make -C ./client -f Makefile clean + make -C ./screen -f Makefile clean + make -C ./widget -f Makefile clean + make -C ./net_unix -f Makefile clean + make -C ./net_sdl -f Makefile clean + make -C ./audio -f Makefile clean + make -C ./modules -f Makefile clean + rm -rf ../modules/*.so + rm tuxanci-ng + rm ../tuxanci-ng diff --git a/src/Makefile-server b/src/Makefile-server new file mode 100644 index 0000000..23ec740 --- /dev/null +++ b/src/Makefile-server @@ -0,0 +1,26 @@ + +CC = gcc +CFLAGS = -g -O0 -std=c99 -I.. -Wall -D_GNU_SOURCE=1 -DSUPPORT_NET_UNIX_UDP -DPUBLIC_SERVER -DNO_SOUND +LIBS = -ldl + +all: + make -C ./base -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./server -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./net_unix -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + make -C ./modules -f Makefile CC="$(CC)" CFLAGS="$(CFLAGS)" + $(CC) $(CFLAGS) $(LIBS) -o publicserver base/*.o server/*.o net_unix/*.o + cp modules/*.so ../modules/ + cp publicserver ../ + +clean: + make -C ./base -f Makefile clean + make -C ./client -f Makefile clean + make -C ./screen -f Makefile clean + make -C ./widget -f Makefile clean + make -C ./net_unix -f Makefile clean + make -C ./net_sdl -f Makefile clean + make -C ./audio -f Makefile clean + make -C ./modules -f Makefile clean + rm -rf ../modules/*.so + rm publicserver + rm ../publicserver diff --git a/src/arena.c b/src/arena.c deleted file mode 100644 index d9df24f..0000000 --- a/src/arena.c +++ /dev/null @@ -1,233 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "arena.h" -#include "list.h" -#include "tux.h" -#include "shot.h" -#include "item.h" -#include "myTimer.h" -#include "modules.h" - -#ifndef PUBLIC_SERVER -#include "screen_world.h" -#include "layer.h" -#endif - -static arena_t *currentArena; - -void setCurrentArena(arena_t *p) -{ - currentArena = p; -} - -arena_t* getCurrentArena() -{ - return currentArena; -} - -arena_t* newArena(int w, int h) -{ - arena_t *new; - int zone_w, zone_h; - - new = malloc( sizeof(arena_t) ); - -#ifndef PUBLIC_SERVER - new->background = NULL; - strcpy(new->music, ""); -#endif - - new->w = w; - new->h = h; - - new->listTimer = newList(); - - if( w > 800 || h > 600) - { - zone_w = 320; - zone_h = 240; - } - else - { - zone_w = 80; - zone_h = 60; - } - - new->spaceTux = newSpace(w, h, zone_w, zone_h, getStatusTux, setStatusTux); - new->spaceItem = newSpace(w, h, zone_w, zone_h, getStatusItem, setStatusItem); - new->spaceShot = newSpace(w, h, zone_w, zone_h, getStatusShot, setStatusShot); - - return new; -} - -int conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2) -{ - return (x1spaceTux, x, y, w, h) )return 0; - if( isConflictWithObjectFromSpace(arena->spaceShot, x, y, w, h) )return 0; - if( isConflictWithObjectFromSpace(arena->spaceItem, x, y, w, h) )return 0; - if( isConflictWithObjectFromSpace(arena->spaceShot, x, y, w, h) )return 0; - - if( isConflictModule(x, y, w, h) )return 0; - - return 1; -} - -void findFreeSpace(arena_t *arena, int *x, int *y, int w, int h) -{ - int z_x; - int z_y; - - assert( x != NULL ); - assert( y != NULL ); - - do{ - z_x = random() % WINDOW_SIZE_X;//arena->w; - z_y = random() % WINDOW_SIZE_Y;//arena->h; - }while( isFreeSpace(arena, z_x, z_y ,w ,h) == 0 ); - - *x = z_x; - *y = z_y; -} - -void getCenterScreen(int *screen_x, int *screen_y, int x, int y) -{ - arena_t *arena; - - arena = getCurrentArena(); - - *screen_x = x - WINDOW_SIZE_X/2; - *screen_y = y - WINDOW_SIZE_Y/2; - - if( *screen_x < 0 ) - { - *screen_x = 0; - } - - if( *screen_y < 0 ) - { - *screen_y = 0; - } - - if( *screen_x + WINDOW_SIZE_X >= arena->w ) - { - *screen_x = arena->w - WINDOW_SIZE_X; - } - - if( *screen_y + WINDOW_SIZE_Y >= arena->h ) - { - *screen_y = arena->h - WINDOW_SIZE_Y; - } -} - -#ifndef PUBLIC_SERVER - -void drawArena(arena_t *arena) -{ - int screen_x, screen_y; - tux_t *tux = NULL; - int i, j; - - tux = getControlTux(TUX_CONTROL_KEYBOARD_RIGHT); - - if( tux == NULL ) - { - return; - } -/* - screen_x = tux->x - WINDOW_SIZE_X/2; - screen_y = tux->y - WINDOW_SIZE_Y/2; -*/ - - getCenterScreen(&screen_x, &screen_y, tux->x, tux->y); - - for( i = screen_y/arena->background->h ; - i <= screen_y/arena->background->h + WINDOW_SIZE_Y/arena->background->h ; i++ ) - { - for( j = screen_x/arena->background->w ; - j <= screen_x/arena->background->w + WINDOW_SIZE_X/arena->background->w ; j++ ) - { - addLayer(arena->background, - j * arena->background->w, - i * arena->background->h, - 0, 0, arena->background->w, arena->background->h, -100); - } - - } - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceTux, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y, listHelp); - drawListTux(listHelp); - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceItem, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y, listHelp); - drawListItem(listHelp); - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceShot, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y, listHelp); - drawListShot(listHelp); - - //printSpace(arena->spaceShot); - - drawModule(screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); -/* - my_time_t t; - - t = getMyTime(); -*/ - if( tux == NULL ) - { - drawLayerCenter(WINDOW_SIZE_X/2, WINDOW_SIZE_Y/2); - } - else - { - drawLayerCenter(tux->x, tux->y); - } - -// printf("time = %d\n", getMyTime() - t ); -} - -#endif - -void eventArena(arena_t *arena) -{ - int i; - - //eventConflictTuxWithTeleport(arena->listTux, arena->listTeleport); - //eventConflictTuxWithShot(arena); - - for( i = 0 ; i < 8 ; i++) - { - eventMoveListShot(arena); - checkShotIsInTuxScreen(arena); - //eventConflictShotWithWall(arena->listWall, arena->listShot); - //eventConflictShotWithTeleport(arena->listTeleport, arena->listShot); - //eventConflictShotWithPipe(arena->listPipe, arena->listShot); - eventConflictShotWithItem(arena); - eventConflictTuxWithShot(arena); - eventModule(); - } - - eventListItem(arena->spaceItem); - - eventListTux(arena->spaceTux->list); - - eventTimer(arena->listTimer); -} - -void destroyArena(arena_t *p) -{ - destroySpaceWithObject(p->spaceTux, destroyTux); - destroySpaceWithObject(p->spaceItem, destroyItem); - destroySpaceWithObject(p->spaceShot, destroyShot); - destroyTimer(p->listTimer); - free(p); -} diff --git a/src/arenaFile.c b/src/arenaFile.c deleted file mode 100644 index a3383e4..0000000 --- a/src/arenaFile.c +++ /dev/null @@ -1,240 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "textFile.h" -#include "configFile.h" -#include "arenaFile.h" -#include "arena.h" -#include "tux.h" -#include "shot.h" -#include "item.h" -#include "director.h" -#include "path.h" -#include "modules.h" -#include "string_length.h" - -#ifndef PUBLIC_SERVER -#include "image.h" -#include "music.h" -#include "fake_audio.h" -#endif - -static list_t *listArenaFile; - -static bool_t isArenaFileInit = FALSE; - -bool_t isAreaFileInicialized() -{ - return isArenaFileInit; -} - -static void cmd_arena(arena_t **arena, char *line) -{ - char str_image[STR_SIZE]; - char str_w[STR_SIZE]; - char str_h[STR_SIZE]; - - if( getValue(line, "background", str_image, STR_SIZE) != 0 )return; - if( getValue(line, "w", str_w, STR_SIZE) != 0 )return; - if( getValue(line, "h", str_h, STR_SIZE) != 0 )return; - - (*arena) = newArena(atoi(str_w), atoi(str_h)); -#ifndef PUBLIC_SERVER - (*arena)->background = getImage(IMAGE_GROUP_USER, str_image); -#endif - //(*arena)->w = atoi(str_w); - //(*arena)->h = atoi(str_h); - setCurrentArena(*arena); - printf("new arena\n"); -} - -static void cmd_loadModule(char *line) -{ - char str_file[STR_SIZE]; - - if( getValue(line, "file", str_file, STR_SIZE) != 0 )return; - - loadModule(str_file); -} - -#ifndef PUBLIC_SERVER - -static void cmd_loadImage(char *line) -{ - char str_file[STR_SIZE]; - char str_name[STR_SIZE]; - char str_alpha[STR_SIZE]; - - if( getValue(line, "file", str_file, STR_SIZE) != 0 )return; - if( getValue(line, "name", str_name, STR_SIZE) != 0 )return; - if( getValue(line, "alpha", str_alpha, STR_SIZE) != 0 )return; - - addImageData(str_file, isYesOrNO(str_alpha), str_name, IMAGE_GROUP_USER); -} - -static void cmd_loadMusic(char *line) -{ - char str_file[STR_SIZE]; - char str_name[STR_SIZE]; - - if( getValue(line, "file", str_file, STR_SIZE) != 0 )return; - if( getValue(line, "name", str_name, STR_SIZE) != 0 )return; - - addMusic(str_file, str_name, MUSIC_GROUP_USER); -} - -static void cmd_playMusic(arena_t *arena, char *line) -{ - char str_music[STR_SIZE]; - - if( getValue(line, "music", str_music, STR_SIZE) != 0 )return; - - strcpy(arena->music, str_music); -} - -#endif - -int getArenaCount() -{ - return listArenaFile->count; -} - -static char *getArenaStatus(int id, char *s) -{ - textFile_t *ts; - int len; - int i; - - ts = (textFile_t *) listArenaFile->list[id]; - len = strlen(s); - - for( i = 0 ; i < ts->text->count ; i++ ) - { - char *line; - line = (char *) (ts->text->list[i]); - - if( strncmp(line, s, len) == 0 ) - { - return line+len+1; - } - } - - return NULL; -} - -char *getArenaName(int id) -{ - char *ret; - - ret = getArenaStatus(id, "name"); - - return ( ret != NULL ? ret : "arena_no_name" ); -} - -char *getArenaNetName(int id) -{ - char *ret; - - ret = getArenaStatus(id, "netName"); - - return ( ret != NULL ? ret : "arena_no_net_name" ); -} - -int getArenaIdFormNetName(char *s) -{ - int i; - - for( i = 0 ; i < listArenaFile->count ; i++ ) - { - if( strcmp(getArenaNetName(i), s) == 0 ) - { - return i; - } - } - - return -1; -} - -char *getArenaImage(int id) -{ - return getArenaStatus(id, "screen"); -} - -arena_t* getArena(int id) -{ - textFile_t *ts; - arena_t *arena; - int i; - - ts = (textFile_t *) listArenaFile->list[id]; - - for( i = 0 ; i < ts->text->count ; i++ ) - { - char *line; - line = (char *) (ts->text->list[i]); - - cmdModule(line); - -#ifndef PUBLIC_SERVER - if( strncmp(line, "loadImage", 9) == 0 )cmd_loadImage(line); - if( strncmp(line, "loadMusic", 9) == 0 )cmd_loadMusic(line); - if( strncmp(line, "playMusic", 9) == 0 )cmd_playMusic(arena, line); -#endif - - if( strncmp(line, "arena", 5) == 0 )cmd_arena(&arena, line); - if( strncmp(line, "loadModule", 10) == 0 )cmd_loadModule(line); - //if( strncmp(line, "wall", 4) == 0 )cmd_wall(arena, line); - //if( strncmp(line, "teleport", 8) == 0 )cmd_teleport(arena, line); - //if( strncmp(line, "pipe", 4) == 0 )cmd_pipe(arena, line); - } - - return arena; -} - -void initArenaFile() -{ - director_t *p; - int i; - -#ifndef PUBLIC_SERVER - assert( isImageInicialized() == TRUE ); -#endif - - isArenaFileInit = TRUE; - listArenaFile = newList(); - - p = loadDirector(PATH_ARENA); - - for( i = 0 ; i < p->list->count ; i++ ) - { - char *line; - - line = (char *)( p->list->list[i] ); - - if( strstr(line, ".map") != NULL && strstr(line, "~") == NULL ) - { - char path[STR_PATH_SIZE]; - - sprintf(path, PATH_ARENA "%s", line); - - printf("load map %s\n", line); - - accessExistFile(path); - addList(listArenaFile, loadTextFile(path)); - } - } - - destroyDirector(p); -} - -void quitArenaFile() -{ - isArenaFileInit = FALSE; - destroyListItem(listArenaFile, destroyTextFile); -} - diff --git a/src/audio.c b/src/audio.c deleted file mode 100644 index 8397788..0000000 --- a/src/audio.c +++ /dev/null @@ -1,44 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "audio.h" -#include "interface.h" - -static bool_t isAudioInit = FALSE; - -bool_t isAudioInicialized() -{ - return isAudioInit; -} - -void initAudio() -{ - if( SDL_Init(SDL_INIT_AUDIO) == -1 ) - { - fprintf(stderr, "Nelze inicializovat audio : %s\n", SDL_GetError()); - return; - } - - if( Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) == -1 ) - { - fprintf(stderr, "Nelze inicializovat audio : %s\n", Mix_GetError()); - return; - } - - Mix_AllocateChannels(16); - Mix_Volume(-1, MIX_MAX_VOLUME); - - printf("init audio..\n"); - isAudioInit = TRUE; -} - -void quitAudio() -{ - Mix_CloseAudio(); - isAudioInit = TRUE; - printf("quit audio..\n"); -} - diff --git a/src/audio/Makefile b/src/audio/Makefile new file mode 100644 index 0000000..a5dff01 --- /dev/null +++ b/src/audio/Makefile @@ -0,0 +1,17 @@ + + +all: audio.o music.o sound.o + +audio.o: audio.c audio.h + $(CC) $(CFLAGS) -o audio.o -c audio.c + +music.o: music.c music.h + $(CC) $(CFLAGS) -o music.o -c music.c + +sound.o: sound.c sound.h + $(CC) $(CFLAGS) -o sound.o -c sound.c + + + +clean: + rm -rf *.o diff --git a/src/audio/audio.c b/src/audio/audio.c new file mode 100644 index 0000000..d7f4a9b --- /dev/null +++ b/src/audio/audio.c @@ -0,0 +1,44 @@ + +#include +#include +#include + +#include "base/main.h" +#include "audio/audio.h" +#include "client/interface.h" + +static bool_t isAudioInit = FALSE; + +bool_t isAudioInicialized() +{ + return isAudioInit; +} + +void initAudio() +{ + if( SDL_Init(SDL_INIT_AUDIO) == -1 ) + { + fprintf(stderr, "Nelze inicializovat audio : %s\n", SDL_GetError()); + return; + } + + if( Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) == -1 ) + { + fprintf(stderr, "Nelze inicializovat audio : %s\n", Mix_GetError()); + return; + } + + Mix_AllocateChannels(16); + Mix_Volume(-1, MIX_MAX_VOLUME); + + printf("init audio..\n"); + isAudioInit = TRUE; +} + +void quitAudio() +{ + Mix_CloseAudio(); + isAudioInit = TRUE; + printf("quit audio..\n"); +} + diff --git a/src/audio/audio.h b/src/audio/audio.h new file mode 100644 index 0000000..04c58e7 --- /dev/null +++ b/src/audio/audio.h @@ -0,0 +1,12 @@ + +#ifndef AUDIO_H + +#define AUDIO_H + +#include "base/main.h" + +extern bool_t isAudioInicialized(); +extern void initAudio(); +extern void quitAudio(); + +#endif diff --git a/src/audio/music.c b/src/audio/music.c new file mode 100644 index 0000000..90bc5a0 --- /dev/null +++ b/src/audio/music.c @@ -0,0 +1,210 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/storage.h" + +#include "client/interface.h" + +#include "audio/audio.h" +#include "audio/music.h" + +static list_t *listStorage; + +static bool_t isMusicInit = FALSE; +static bool_t var_isMusicActive = TRUE; +static Mix_Music *currentMusic; + +bool_t isMusicInicialized() +{ + return isMusicInit; +} + +void initMusic() +{ + if( isAudioInicialized() == FALSE ) + { + isMusicInit = FALSE; + return; + } + + listStorage = newStorage(); + currentMusic = NULL; + + isMusicInit = TRUE; + var_isMusicActive = TRUE; + + if( isParamFlag("--nomusic") ) + { + setMusicActive(FALSE); + } + + if( isParamFlag("--music") ) + { + setMusicActive(TRUE); + } + + printf("init music..\n"); +} + +static Mix_Music* loadMixMusic(char *file) +{ + Mix_Music *new; + char str[STR_PATH_SIZE]; + + + printf("load music %s..\n", file); + sprintf(str, PATH_MUSIC "%s", file); + + accessExistFile(str); + + new = Mix_LoadMUS(str); + + if( new == NULL ) + { + fprintf(stderr, "Nepodarilo sa nacitat hudbu %s\n", file); + return NULL; + } + + return new; +} + +static void playMixMusic() +{ + if( currentMusic != NULL ) + { + Mix_PlayMusic(currentMusic, 1000); + } +} + +static void destroyMusic(void *p) +{ + Mix_FreeMusic((Mix_Music *)p); +} + +void addMusic(char *file, char *name, char *group) +{ + Mix_Music *new; + + if( isMusicInit == FALSE ) + { + return; + } + + assert( file != NULL ); + assert( name != NULL ); + assert( group != NULL ); + + new = loadMixMusic(file); + + addItemToStorage(listStorage, group, name, new ); +} + +void stopMusic() +{ + if( isMusicInit == FALSE || + var_isMusicActive == FALSE ) + { + return; + } + + if( currentMusic != NULL ) + { + printf("stop music..\n"); + Mix_HaltMusic(); + currentMusic = NULL; + } +} + +void playMusic(char *name, char *group) +{ + static char currentMusic_group[STR_SIZE]; + static char currentMusic_name[STR_SIZE]; + static int isStrInit = 0; + + if( isStrInit == 0 ) + { + strcpy(currentMusic_group, "none"); + strcpy(currentMusic_name, "none"); + isStrInit = 1; + } + + if( isMusicInit == FALSE || + var_isMusicActive == FALSE ) + { + return; + } + + if( currentMusic != NULL && + strcmp(currentMusic_group, group) == 0 && + strcmp(currentMusic_name, name) == 0 ) + { + return; + } + + if( currentMusic != NULL ) + { + stopMusic(); + } + + currentMusic = getItemFromStorage(listStorage, group, name); + strcpy(currentMusic_group, group); + strcpy(currentMusic_name, name); + + if( currentMusic != NULL ) + { + printf("play music %s\n", name); + playMixMusic(); + } +} + +void setMusicActive(bool_t n) +{ + static Mix_Music *music = NULL; + + if( n == FALSE ) + { + music = currentMusic; + stopMusic(); + } + + if( n == TRUE ) + { + currentMusic = music; + playMixMusic(); + } + + var_isMusicActive = n; +} + +bool_t isMusicActive() +{ + return var_isMusicActive; +} + +char* getCurrentMusic() +{ + return "unknown"; +} + +void delAllMusicInGroup(char *group) +{ + delAllItemFromStorage(listStorage, group, destroyMusic); +} + +void quitMusic() +{ + if( isMusicInit == FALSE ) + { + return; + } + + stopMusic(); + destroyStorage(listStorage, destroyMusic); + isMusicInit = FALSE; + + printf("quit music..\n"); +} diff --git a/src/audio/music.h b/src/audio/music.h new file mode 100644 index 0000000..d87f382 --- /dev/null +++ b/src/audio/music.h @@ -0,0 +1,36 @@ + +#ifndef MUSIC_H + +#define MUSIC_H + +#ifndef NO_SOUND + +#include +#include "base/main.h" + +#define MUSIC_GROUP_BASE "base" +#define MUSIC_GROUP_USER "user" + +/* +typedef struct music_struct +{ + char *name; + int group; + Mix_Music *music; +} music_t; +*/ + +extern bool_t isMusicInicialized(); +extern void initMusic(); +extern void addMusic(char *file, char *name, char *group); +extern void stopMusic(); +extern void playMusic(char *name, char *group); +extern void setMusicActive(bool_t n); +extern bool_t isMusicActive(); +extern char* getCurrentMusic(); +extern void delAllMusicInGroup(char *group); +extern void quitMusic(); + +#endif + +#endif diff --git a/src/audio/sound.c b/src/audio/sound.c new file mode 100644 index 0000000..37803c4 --- /dev/null +++ b/src/audio/sound.c @@ -0,0 +1,137 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/storage.h" + +#include "client/interface.h" + +#include "audio/audio.h" +#include "audio/sound.h" + +static list_t *listStorage; + +static bool_t isSoundInit = FALSE; +static bool_t var_isSoundActive = TRUE; + +bool_t isSoundInicialized() +{ + return isSoundInit; +} + +void initSound() +{ + if( isAudioInicialized() == FALSE ) + { + isSoundInit = FALSE; + return; + } + + listStorage = newStorage(); + isSoundInit = TRUE; + var_isSoundActive = TRUE; + + if( isParamFlag("--nosound") ) + { + setSoundActive(FALSE); + } + + if( isParamFlag("--sound") ) + { + setSoundActive(TRUE); + } + + printf("init sound..\n"); +} + +static Mix_Chunk* loadMixSound(char *file) +{ + Mix_Chunk *new; + char str[STR_PATH_SIZE]; + + + printf("load sound %s..\n", file); + sprintf(str, PATH_SOUND "%s", file); + + accessExistFile(str); + + new = Mix_LoadWAV(str); + + if( new == NULL ) + { + fprintf(stderr, "Nelze nahrat zvuk %s : %s\n", str, Mix_GetError()); + return NULL; + } + + return new; +} + + +static void playMixSound(Mix_Chunk *p) +{ + if( Mix_PlayChannel(-1, p, 0) == -1 ) + { + fprintf(stderr, "Nelze prehrat zvuk : %s\n", Mix_GetError()); + return; + } +} + +static void destroySound(void *p) +{ + Mix_FreeChunk((Mix_Chunk *)p); +} + +void addSound(char *file, char *name, char *group) +{ + Mix_Chunk *new; + + if( isSoundInit == FALSE ) + { + return; + } + + assert( file != NULL ); + assert( name != NULL ); + assert( group != NULL ); + + new = loadMixSound(file); + + addItemToStorage(listStorage, group, name, new ); +} + +void playSound(char *name, char *group) +{ + if( isSoundInit == FALSE || + var_isSoundActive == FALSE ) + { + return; + } + + playMixSound( getItemFromStorage(listStorage, group, name) ); +} + +void setSoundActive(bool_t n) +{ + var_isSoundActive = n; +} + +bool_t isSoundActive() +{ + return var_isSoundActive; +} + +void quitSound() +{ + if( isSoundInit == FALSE ) + { + return; + } + + destroyStorage(listStorage, destroySound); + isSoundInit = FALSE; + printf("quit sound..\n"); +} + diff --git a/src/audio/sound.h b/src/audio/sound.h new file mode 100644 index 0000000..ec1beb1 --- /dev/null +++ b/src/audio/sound.h @@ -0,0 +1,33 @@ + +#ifndef SOUND_H + +#define SOUND_H + +#ifndef NO_SOUND + +#include +#include "base/main.h" + +#define SOUND_GROUP_BASE "base" +#define SOUND_GROUP_USER "user" + +/* +typedef struct sound_struct +{ + char *name; + int group; + Mix_Chunk *sound; +} sound_t; +*/ + +extern bool_t isSoundInicialized(); +extern void initSound(); +extern void addSound(char *file, char *name, char *group); +extern void playSound(char *name, char *group); +extern void setSoundActive(bool_t n); +extern bool_t isSoundActive(); +extern void quitSound(); + +#endif + +#endif diff --git a/src/base/Makefile b/src/base/Makefile new file mode 100644 index 0000000..6de422d --- /dev/null +++ b/src/base/Makefile @@ -0,0 +1,67 @@ + +all: arena.o arenaFile.o director.o gun.o homeDirector.o checkFront.o idManager.o item.o list.o main.o modules.o myTimer.o net_multiplayer.o protect.o proto.o server.o shot.o space.o storage.o textFile.o tux.o + +arena.o: arena.c arena.h + $(CC) $(CFLAGS) -o arena.o -c arena.c + +arenaFile.o: arenaFile.c arenaFile.h + $(CC) $(CFLAGS) -o arenaFile.o -c arenaFile.c + +director.o: director.c director.h + $(CC) $(CFLAGS) -o director.o -c director.c + +gun.o: gun.c gun.h + $(CC) $(CFLAGS) -o gun.o -c gun.c + +homeDirector.o: homeDirector.c homeDirector.h + $(CC) $(CFLAGS) -o homeDirector.o -c homeDirector.c + +checkFront.o: checkFront.c checkFront.h + $(CC) $(CFLAGS) -o checkFront.o -c checkFront.c + +idManager.o: idManager.c idManager.h + $(CC) $(CFLAGS) -o idManager.o -c idManager.c + +item.o: item.c item.h + $(CC) $(CFLAGS) -o item.o -c item.c + +list.o: list.c list.h + $(CC) $(CFLAGS) -o list.o -c list.c + +main.o: main.c main.h + $(CC) $(CFLAGS) -o main.o -c main.c + +modules.o: modules.c modules.h + $(CC) $(CFLAGS) -o modules.o -c modules.c + +myTimer.o: myTimer.c myTimer.h + $(CC) $(CFLAGS) -o myTimer.o -c myTimer.c + +net_multiplayer.o: net_multiplayer.c net_multiplayer.h + $(CC) $(CFLAGS) -o net_multiplayer.o -c net_multiplayer.c + +protect.o: protect.c protect.h + $(CC) $(CFLAGS) -o protect.o -c protect.c + +server.o: server.c server.h + $(CC) $(CFLAGS) -o server.o -c server.c + +shot.o: shot.c shot.h + $(CC) $(CFLAGS) -o shot.o -c shot.c + +space.o: space.c space.h + $(CC) $(CFLAGS) -o space.o -c space.c + +storage.o: storage.c storage.h + $(CC) $(CFLAGS) -o storage.o -c storage.c + +textFile.o: textFile.c textFile.h + $(CC) $(CFLAGS) -o textFile.o -c textFile.c + +tux.o: tux.c tux.h + $(CC) $(CFLAGS) -o tux.o -c tux.c + +clean: + rm -rf *.o + + diff --git a/src/base/arena.c b/src/base/arena.c new file mode 100644 index 0000000..448ee5b --- /dev/null +++ b/src/base/arena.c @@ -0,0 +1,233 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/arena.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/shot.h" +#include "base/item.h" +#include "base/myTimer.h" +#include "base/modules.h" + +#ifndef PUBLIC_SERVER +#include "screen/screen_world.h" +#include "client/layer.h" +#endif + +static arena_t *currentArena; + +void setCurrentArena(arena_t *p) +{ + currentArena = p; +} + +arena_t* getCurrentArena() +{ + return currentArena; +} + +arena_t* newArena(int w, int h) +{ + arena_t *new; + int zone_w, zone_h; + + new = malloc( sizeof(arena_t) ); + +#ifndef PUBLIC_SERVER + new->background = NULL; + strcpy(new->music, ""); +#endif + + new->w = w; + new->h = h; + + new->listTimer = newList(); + + if( w > 800 || h > 600) + { + zone_w = 320; + zone_h = 240; + } + else + { + zone_w = 80; + zone_h = 60; + } + + new->spaceTux = newSpace(w, h, zone_w, zone_h, getStatusTux, setStatusTux); + new->spaceItem = newSpace(w, h, zone_w, zone_h, getStatusItem, setStatusItem); + new->spaceShot = newSpace(w, h, zone_w, zone_h, getStatusShot, setStatusShot); + + return new; +} + +int conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2) +{ + return (x1spaceTux, x, y, w, h) )return 0; + if( isConflictWithObjectFromSpace(arena->spaceShot, x, y, w, h) )return 0; + if( isConflictWithObjectFromSpace(arena->spaceItem, x, y, w, h) )return 0; + if( isConflictWithObjectFromSpace(arena->spaceShot, x, y, w, h) )return 0; + + if( isConflictModule(x, y, w, h) )return 0; + + return 1; +} + +void findFreeSpace(arena_t *arena, int *x, int *y, int w, int h) +{ + int z_x; + int z_y; + + assert( x != NULL ); + assert( y != NULL ); + + do{ + z_x = random() % WINDOW_SIZE_X;//arena->w; + z_y = random() % WINDOW_SIZE_Y;//arena->h; + }while( isFreeSpace(arena, z_x, z_y ,w ,h) == 0 ); + + *x = z_x; + *y = z_y; +} + +void getCenterScreen(int *screen_x, int *screen_y, int x, int y) +{ + arena_t *arena; + + arena = getCurrentArena(); + + *screen_x = x - WINDOW_SIZE_X/2; + *screen_y = y - WINDOW_SIZE_Y/2; + + if( *screen_x < 0 ) + { + *screen_x = 0; + } + + if( *screen_y < 0 ) + { + *screen_y = 0; + } + + if( *screen_x + WINDOW_SIZE_X >= arena->w ) + { + *screen_x = arena->w - WINDOW_SIZE_X; + } + + if( *screen_y + WINDOW_SIZE_Y >= arena->h ) + { + *screen_y = arena->h - WINDOW_SIZE_Y; + } +} + +#ifndef PUBLIC_SERVER + +void drawArena(arena_t *arena) +{ + int screen_x, screen_y; + tux_t *tux = NULL; + int i, j; + + tux = getControlTux(TUX_CONTROL_KEYBOARD_RIGHT); + + if( tux == NULL ) + { + return; + } +/* + screen_x = tux->x - WINDOW_SIZE_X/2; + screen_y = tux->y - WINDOW_SIZE_Y/2; +*/ + + getCenterScreen(&screen_x, &screen_y, tux->x, tux->y); + + for( i = screen_y/arena->background->h ; + i <= screen_y/arena->background->h + WINDOW_SIZE_Y/arena->background->h ; i++ ) + { + for( j = screen_x/arena->background->w ; + j <= screen_x/arena->background->w + WINDOW_SIZE_X/arena->background->w ; j++ ) + { + addLayer(arena->background, + j * arena->background->w, + i * arena->background->h, + 0, 0, arena->background->w, arena->background->h, -100); + } + + } + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceTux, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y, listHelp); + drawListTux(listHelp); + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceItem, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y, listHelp); + drawListItem(listHelp); + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y, listHelp); + drawListShot(listHelp); + + //printSpace(arena->spaceShot); + + drawModule(screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); +/* + my_time_t t; + + t = getMyTime(); +*/ + if( tux == NULL ) + { + drawLayerCenter(WINDOW_SIZE_X/2, WINDOW_SIZE_Y/2); + } + else + { + drawLayerCenter(tux->x, tux->y); + } + +// printf("time = %d\n", getMyTime() - t ); +} + +#endif + +void eventArena(arena_t *arena) +{ + int i; + + //eventConflictTuxWithTeleport(arena->listTux, arena->listTeleport); + //eventConflictTuxWithShot(arena); + + for( i = 0 ; i < 8 ; i++) + { + eventMoveListShot(arena); + checkShotIsInTuxScreen(arena); + //eventConflictShotWithWall(arena->listWall, arena->listShot); + //eventConflictShotWithTeleport(arena->listTeleport, arena->listShot); + //eventConflictShotWithPipe(arena->listPipe, arena->listShot); + eventConflictShotWithItem(arena); + eventConflictTuxWithShot(arena); + eventModule(); + } + + eventListItem(arena->spaceItem); + + eventListTux(arena->spaceTux->list); + + eventTimer(arena->listTimer); +} + +void destroyArena(arena_t *p) +{ + destroySpaceWithObject(p->spaceTux, destroyTux); + destroySpaceWithObject(p->spaceItem, destroyItem); + destroySpaceWithObject(p->spaceShot, destroyShot); + destroyTimer(p->listTimer); + free(p); +} diff --git a/src/base/arena.h b/src/base/arena.h new file mode 100644 index 0000000..57792c5 --- /dev/null +++ b/src/base/arena.h @@ -0,0 +1,43 @@ + +#ifndef ARENA_H + +#define ARENA_H + +#include "base/main.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#endif + +#include "base/list.h" +#include "base/space.h" + +typedef struct arena_struct +{ +#ifndef PUBLIC_SERVER + char music[STR_SIZE]; + + SDL_Surface *background; +#endif + int w, h; + list_t *listTimer; + //list_t *listTux; + space_t *spaceShot; + space_t *spaceTux; + space_t *spaceItem; +} arena_t; + +extern void setCurrentArena(arena_t *p); +extern arena_t* getCurrentArena(); +extern arena_t* newArena(int w, int h); +extern int conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2); +extern int isFreeSpace(arena_t *arena, int x, int y, int w, int h); +extern void findFreeSpace(arena_t *arena, int *x, int *y, int w, int h); +extern void getCenterScreen(int *screen_x, int *screen_y, int x, int y); +#ifndef PUBLIC_SERVER +extern void drawArena(arena_t *arena); +#endif +extern void eventArena(arena_t *arena); +extern void destroyArena(arena_t *p); + +#endif diff --git a/src/base/arenaFile.c b/src/base/arenaFile.c new file mode 100644 index 0000000..9fe683e --- /dev/null +++ b/src/base/arenaFile.c @@ -0,0 +1,269 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/textFile.h" +#include "base/arenaFile.h" +#include "base/arena.h" +#include "base/tux.h" +#include "base/shot.h" +#include "base/item.h" +#include "base/director.h" +#include "base/modules.h" + +#ifndef PUBLIC_SERVER +#include "client/configFile.h" +#include "client/image.h" +#include "audio/music.h" +#endif + +static list_t *listArenaFile; + +static bool_t isArenaFileInit = FALSE; + +bool_t isAreaFileInicialized() +{ + return isArenaFileInit; +} + +int getArenaValue(char *line, char *env, char *val, int len) +{ + char *offset_env; + char *offset_val_begin; + char *offset_val_end; + char clone_env[STR_SIZE]; + int val_len; + + strcpy(clone_env, " "); + strcat(clone_env, env); + + offset_env = strstr(line, clone_env); + if( offset_env == NULL )return -1; + + offset_val_begin = strchr(offset_env, '"'); + if( offset_val_begin == NULL )return -1; + + offset_val_end = strchr(offset_val_begin+1, '"'); + if( offset_val_end == NULL )return -1; + + val_len = (int)(offset_val_end - ( offset_val_begin + 1) ); + if( val_len > len - 1 ) val_len = len - 1; + + memset(val, 0, len); + memcpy(val, offset_val_begin+1, val_len); + + return 0; +} + + +static void cmd_arena(arena_t **arena, char *line) +{ + char str_image[STR_SIZE]; + char str_w[STR_SIZE]; + char str_h[STR_SIZE]; + + if( getArenaValue(line, "background", str_image, STR_SIZE) != 0 )return; + if( getArenaValue(line, "w", str_w, STR_SIZE) != 0 )return; + if( getArenaValue(line, "h", str_h, STR_SIZE) != 0 )return; + + (*arena) = newArena(atoi(str_w), atoi(str_h)); +#ifndef PUBLIC_SERVER + (*arena)->background = getImage(IMAGE_GROUP_USER, str_image); +#endif + //(*arena)->w = atoi(str_w); + //(*arena)->h = atoi(str_h); + setCurrentArena(*arena); + printf("new arena\n"); +} + +static void cmd_loadModule(char *line) +{ + char str_file[STR_SIZE]; + + if( getArenaValue(line, "file", str_file, STR_SIZE) != 0 )return; + + loadModule(str_file); +} + +#ifndef PUBLIC_SERVER + +static void cmd_loadImage(char *line) +{ + char str_file[STR_SIZE]; + char str_name[STR_SIZE]; + char str_alpha[STR_SIZE]; + + if( getArenaValue(line, "file", str_file, STR_SIZE) != 0 )return; + if( getArenaValue(line, "name", str_name, STR_SIZE) != 0 )return; + if( getArenaValue(line, "alpha", str_alpha, STR_SIZE) != 0 )return; + + addImageData(str_file, isYesOrNO(str_alpha), str_name, IMAGE_GROUP_USER); +} + +static void cmd_loadMusic(char *line) +{ + char str_file[STR_SIZE]; + char str_name[STR_SIZE]; + + if( getArenaValue(line, "file", str_file, STR_SIZE) != 0 )return; + if( getArenaValue(line, "name", str_name, STR_SIZE) != 0 )return; + +#ifndef NO_SOUND + addMusic(str_file, str_name, MUSIC_GROUP_USER); +#endif +} + +static void cmd_playMusic(arena_t *arena, char *line) +{ + char str_music[STR_SIZE]; + + if( getArenaValue(line, "music", str_music, STR_SIZE) != 0 )return; + + strcpy(arena->music, str_music); +} + +#endif + +int getArenaCount() +{ + return listArenaFile->count; +} + +static char *getArenaStatus(int id, char *s) +{ + textFile_t *ts; + int len; + int i; + + ts = (textFile_t *) listArenaFile->list[id]; + len = strlen(s); + + for( i = 0 ; i < ts->text->count ; i++ ) + { + char *line; + line = (char *) (ts->text->list[i]); + + if( strncmp(line, s, len) == 0 ) + { + return line+len+1; + } + } + + return NULL; +} + +char *getArenaName(int id) +{ + char *ret; + + ret = getArenaStatus(id, "name"); + + return ( ret != NULL ? ret : "arena_no_name" ); +} + +char *getArenaNetName(int id) +{ + char *ret; + + ret = getArenaStatus(id, "netName"); + + return ( ret != NULL ? ret : "arena_no_net_name" ); +} + +int getArenaIdFormNetName(char *s) +{ + int i; + + for( i = 0 ; i < listArenaFile->count ; i++ ) + { + if( strcmp(getArenaNetName(i), s) == 0 ) + { + return i; + } + } + + return -1; +} + +char *getArenaImage(int id) +{ + return getArenaStatus(id, "screen"); +} + +arena_t* getArena(int id) +{ + textFile_t *ts; + arena_t *arena; + int i; + + ts = (textFile_t *) listArenaFile->list[id]; + + for( i = 0 ; i < ts->text->count ; i++ ) + { + char *line; + line = (char *) (ts->text->list[i]); + + cmdModule(line); + +#ifndef PUBLIC_SERVER + if( strncmp(line, "loadImage", 9) == 0 )cmd_loadImage(line); + if( strncmp(line, "loadMusic", 9) == 0 )cmd_loadMusic(line); + if( strncmp(line, "playMusic", 9) == 0 )cmd_playMusic(arena, line); +#endif + + if( strncmp(line, "arena", 5) == 0 )cmd_arena(&arena, line); + if( strncmp(line, "loadModule", 10) == 0 )cmd_loadModule(line); + //if( strncmp(line, "wall", 4) == 0 )cmd_wall(arena, line); + //if( strncmp(line, "teleport", 8) == 0 )cmd_teleport(arena, line); + //if( strncmp(line, "pipe", 4) == 0 )cmd_pipe(arena, line); + } + + return arena; +} + +void initArenaFile() +{ + director_t *p; + int i; + +#ifndef PUBLIC_SERVER + assert( isImageInicialized() == TRUE ); +#endif + + isArenaFileInit = TRUE; + listArenaFile = newList(); + + p = loadDirector(PATH_ARENA); + + for( i = 0 ; i < p->list->count ; i++ ) + { + char *line; + + line = (char *)( p->list->list[i] ); + + if( strstr(line, ".map") != NULL && strstr(line, "~") == NULL ) + { + char path[STR_PATH_SIZE]; + + sprintf(path, PATH_ARENA "%s", line); + + printf("load map %s\n", line); + + accessExistFile(path); + addList(listArenaFile, loadTextFile(path)); + } + } + + destroyDirector(p); +} + +void quitArenaFile() +{ + isArenaFileInit = FALSE; + destroyListItem(listArenaFile, destroyTextFile); +} + diff --git a/src/base/arenaFile.h b/src/base/arenaFile.h new file mode 100644 index 0000000..e2e8108 --- /dev/null +++ b/src/base/arenaFile.h @@ -0,0 +1,22 @@ + +#ifndef ARENA_FILE_H + +#define ARENA_FILE_H + +#include "base/main.h" +#include "base/arena.h" + +#define PUBLIC_SERVER_PATH_ARENA "./arena/" + +extern int getArenaValue(char *line, char *env, char *val, int len); +extern bool_t isArenaFileInicialized(); +extern void initArenaFile(); +extern int getArenaCount(); +extern char *getArenaName(int id); +extern char *getArenaNetName(int id); +extern int getArenaIdFormNetName(char *s); +extern char *getArenaImage(int id); +extern arena_t* getArena(int id); +extern void quitArenaFile(); + +#endif diff --git a/src/base/bool.h b/src/base/bool.h new file mode 100644 index 0000000..d122b85 --- /dev/null +++ b/src/base/bool.h @@ -0,0 +1,13 @@ + +#ifndef BOOL_H + +#define BOOL_H + +#include "main.h" + +#define bool_t int + +#define TRUE 1 +#define FALSE 0 + +#endif diff --git a/src/base/checkFront.c b/src/base/checkFront.c new file mode 100644 index 0000000..37b0ace --- /dev/null +++ b/src/base/checkFront.c @@ -0,0 +1,136 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/myTimer.h" +#include "base/checkFront.h" +#include "base/idManager.h" +#include "base/server.h" + +typedef struct struct_checkfront_t +{ + char *msg; + my_time_t time; + int id; + int count; + int type; +} checkfront_t; + +static checkfront_t* newCheck(char *s, int type, int id) +{ + checkfront_t *new; + + assert( s != NULL ); + + new = malloc( sizeof(checkfront_t) ); + memset(new, 0, sizeof(checkfront_t)); + + new->msg = strdup(s); + new->time = getMyTime(); + new->id = id; + new->type = type; + new->count = 0; + + return new; +} + +static void destroyCheck(checkfront_t *p) +{ + assert( p != NULL ); + + free(p->msg); + free(p); +} + +list_t* newCheckFront() +{ + return newList(); +} + +void addMsgInCheckFront(list_t *list, char *msg, int type, int id) +{ + addList(list, newCheck(msg, type, id) ); +} + +void eventMsgInCheckFront(client_t *client) +{ + my_time_t currentTime; + int i; + + currentTime = getMyTime(); + + for( i = 0 ; i < client->listSendMsg->count ; i++ ) + { + checkfront_t *this; + + this = (checkfront_t *)client->listSendMsg->list[i]; + + switch( this->type ) + { + case CHECK_FORNT_TYPE_SIMPLE : + sendClient(client, this->msg); + delListItem(client->listSendMsg, i, destroyCheck); + i--; + break; + + case CHECK_FORNT_TYPE_CHECK : + if( this->count == 0 || currentTime - this->time > CHECK_FRONT_SEND_TIME_INTERVAL ) + { + sendClient(client, this->msg); + this->time = currentTime; + this->count++; + } + + if( this->count > CHECK_FRONT_MAX_COUNT_SEND ) + { + if( isRegisterID(this->id) != -1 ) + { + delID(this->id); + } + + delListItem(client->listSendMsg, i, destroyCheck); + i--; + } + break; + + default : + assert( ! "Zly typ !" ); + break; + } + } +} + +void delMsgInCheckFront(list_t *listCheckFront, int id) +{ + int i; + + for( i = 0 ; i < listCheckFront->count ; i++ ) + { + checkfront_t *this; + + this = (checkfront_t *)listCheckFront->list[i]; + + if( this->id == id ) + { + if( isRegisterID(this->id) != -1 ) + { + delID(this->id); + } + + delListItem(listCheckFront, i, destroyCheck); + return; + } + } +} + +void destroyCheckFront(list_t *p) +{ + assert( p != NULL ); + + destroyListItem(p, destroyCheck); +} + diff --git a/src/base/checkFront.h b/src/base/checkFront.h new file mode 100644 index 0000000..3beb370 --- /dev/null +++ b/src/base/checkFront.h @@ -0,0 +1,22 @@ + +#ifndef CHECK_FRONT_H + +#define CHECK_FRONT_H + +#include "base/list.h" +#include "base/server.h" + +#define CHECK_FRONT_SEND_TIME_INTERVAL 100 +#define CHECK_FRONT_MAX_COUNT_SEND 30 + +#define CHECK_FRONT_ID_NONE -1 +#define CHECK_FORNT_TYPE_SIMPLE 1 +#define CHECK_FORNT_TYPE_CHECK 2 + +extern list_t* newCheckFront(); +extern void addMsgInCheckFront(list_t *list, char *msg, int type, int id); +extern void eventMsgInCheckFront(client_t *client); +extern void delMsgInCheckFront(list_t *listCheckFront, int id); +extern void destroyCheckFront(list_t *p); + +#endif diff --git a/src/base/director.c b/src/base/director.c new file mode 100755 index 0000000..fede2c3 --- /dev/null +++ b/src/base/director.c @@ -0,0 +1,64 @@ + +#include +#include +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" + +typedef struct director_struct +{ + char *path; + list_t *list; +} director_t; + +director_t* loadDirector(char *s) +{ + director_t *new; + DIR *dir; + struct dirent *item; + char path[STR_PATH_SIZE]; + + assert( s != 0 ); + + new = malloc( sizeof(director_t) ); + memset(new, 0, sizeof(director_t) ); + + new->list = newList(); + + if( s[0] == '/' ) + { + strcpy(path, s); + } + else + { + getcwd(path, STR_PATH_SIZE); + strcat(path, s+1); + } + + new->path = strdup(path); + + dir = opendir(new->path); + + while( ( item = readdir(dir) ) != NULL ) + addList(new->list, strdup(item->d_name) ); + + closedir(dir); + + return new; +} + +void destroyDirector(director_t *p) +{ + assert( p != NULL ); + + destroyListItem(p->list, free); + free(p->path); + free(p); +} + + diff --git a/src/base/director.h b/src/base/director.h new file mode 100644 index 0000000..20e577c --- /dev/null +++ b/src/base/director.h @@ -0,0 +1,18 @@ + +#ifndef DIRECTOR_H + +#define DIRECTOR_H + +#include "base/main.h" +#include "base/list.h" + +typedef struct director_struct +{ + char *path; + list_t *list; +} director_t; + +extern director_t* loadDirector(char *s); +extern void destroyDirector(director_t *p); + +#endif diff --git a/src/base/fake_audio.c b/src/base/fake_audio.c new file mode 100644 index 0000000..2149c86 --- /dev/null +++ b/src/base/fake_audio.c @@ -0,0 +1,89 @@ + +#include "base/main.h" +#include "base/fake_audio.h" + +bool_t isAudioInicialized() +{ + return TRUE; +} + +void initAudio() +{ +} + +void quitAudio() +{ +} + +bool_t isSoundInicialized() +{ + return TRUE; +} + +void initSound() +{ +} + +void addSound(char *file, char *name, int group) +{ +} + +void playSound(char *name, int group) +{ +} + +void setSoundActive(bool_t n) +{ +} + +bool_t isSoundActive() +{ + return FALSE; +} + +void quitSound() +{ +} + +bool_t isMusicInicialized() +{ + return FALSE; +} + +void initMusic() +{ +} + +void addMusic(char *file, char *name, int group) +{ +} + +void stopMusic() +{ +} + +void playMusic(char *name, int group) +{ +} + +void setMusicActive(bool_t n) +{ +} + +bool_t isMusicActive() +{ + return FALSE; +} + +char* getCurrentMusic() +{ + return "no_sound"; +} + +void delAllMusicInGroup(int group) +{ +} + +void quitMusic() +{ +} diff --git a/src/base/gun.c b/src/base/gun.c new file mode 100644 index 0000000..3cd6122 --- /dev/null +++ b/src/base/gun.c @@ -0,0 +1,353 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/arena.h" +#include "base/tux.h" +#include "base/shot.h" +#include "base/myTimer.h" +#include "base/item.h" +#include "base/gun.h" +#include "base/proto.h" +#include "base/net_multiplayer.h" + +#ifndef PUBLIC_SERVER +#include "audio/sound.h" + +#include "client/interface.h" +#include "client/term.h" + +#include "screen/screen_world.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +static void modificiationCopuse(int courser, int right_x, int right_y, int *dest_x, int *dest_y) +{ + assert( dest_x != NULL ); + assert( dest_y != NULL ); + + switch( courser ) + { + case TUX_UP : + *dest_x = right_y; + *dest_y = -right_x; + break; + case TUX_RIGHT : + *dest_x = right_x; + *dest_y = right_y; + break; + case TUX_LEFT : + *dest_x = -right_x; + *dest_y = right_y; + break; + case TUX_DOWN : + *dest_x = right_y; + *dest_y = right_x; + break; + default : + assert( ! "V premennej courser je zly smer !" ); + break; + } +} + +static void addShotTrivial(tux_t *tux, int x, int y, int px, int py, int gun) +{ + int dest_x = 0, dest_y = 0; + int dest_px = 0, dest_py = 0; + shot_t *shot; + + modificiationCopuse(tux->position, px, py, &dest_px, &dest_py); + modificiationCopuse(tux->position, x, y, &dest_x, &dest_y); + + if( gun == GUN_LASSER ) + { + switch( tux->position ) + { + case TUX_UP : + dest_y -= GUN_LASSER_HORIZONTAL; + break; + case TUX_RIGHT : + break; + case TUX_LEFT : + dest_x -= GUN_LASSER_HORIZONTAL; + break; + case TUX_DOWN : + break; + } + } + + shot = newShot(tux->x + dest_x, tux->y + dest_y, dest_px, dest_py, gun, tux->id); + +/* + if( isFreeSpace( getCurrentArena(), shot->x, shot->y, shot->w, shot->h ) == 0 ) + { + destroyShot(shot); + return; + } +*/ + //addList( getCurrentArena()->listShot, shot ); + addObjectToSpace(getCurrentArena()->spaceShot, shot); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + client_t *thisClient; + + thisClient = getClientFromTux(tux); + + if( thisClient != NULL ) + { + proto_send_shot_server(PROTO_SEND_ONE, thisClient, shot); + } + + proto_send_shot_server(PROTO_SEND_ALL_SEES_TUX, thisClient, shot); + } + +} + +static void addShot(tux_t *tux,int x, int y, int px, int py) +{ + int gun; + + if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + return; + } + + if( tux->gun == GUN_BOMBBALL ) + { + gun = GUN_BOMBBALL; + } + else if( tux->gun == GUN_LASSER ) + { + gun = GUN_LASSER; + } + else + { + gun = GUN_SIMPLE; + } + + if( tux->bonus == BONUS_4X ) + { + int zal; + int i; + + zal = tux->position; + + for( i = 0 ; i < 4 ; i++ ) + { + switch( i ) + { + case 0 : tux->position = TUX_UP; break; + case 1 : tux->position = TUX_RIGHT; break; + case 2 : tux->position = TUX_LEFT; break; + case 3 : tux->position = TUX_DOWN; break; + } + + addShotTrivial(tux, x, y, px, py, gun); + } + + tux->position = zal; + } + else + { + addShotTrivial(tux, x, y, px, py, gun); + } +} + +static void shotInGunSimpe(tux_t *tux) +{ + addShot(tux, 0, 0, +5, 0); +} + +static void shotInGunDualSimpe(tux_t *tux) +{ + addShot(tux, 0, -10, +6, 0); + addShot(tux, 0, +10, +6, 0); +} + +static void shotInGunScatter(tux_t *tux) +{ + int i; + + for( i = -2 ; i <= +2 ; i++ ) + { + addShot(tux, 0, 0, +8, i); + } +} + +static void timer_addShotTimer(void *p) +{ + tux_t *tux; + int id; + int gun; + + id = * ((int *)p); + free(p); + + tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + + if( tux == NULL )return; + + if( tux->status != TUX_STATUS_ALIVE )return; + + gun = tux->gun; + tux->gun = GUN_SIMPLE; + addShot(tux, 0, 0, +6, 0); + tux->gun = gun; +} + +static void shotInGunTommy(tux_t *tux) +{ + int i; + + for( i = 0 ; i < 10 ; i++ ) + { + addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_addShotTimer, newInt(tux->id), i * 100 ); + } +} + +static void timer_addLaserTimer(void *p) +{ + tux_t *tux; + int id; + int gun; + + id = * ((int *)p); + free(p); + + tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + + if( tux == NULL )return; + + if( tux->status != TUX_STATUS_ALIVE )return; + + gun = tux->gun; + tux->gun = GUN_LASSER; + + addShot(tux, 0, 0, +10, 0); + + tux->gun = gun; +} + +static void shotInGunLasser(tux_t *tux) +{ + int i; + + for( i = 0 ; i < 50 ; i++ ) + { + addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_addLaserTimer, newInt(tux->id), i * 10 ); + } +} + +static void putInGunMine(tux_t *tux) +{ + int x, y; + arena_t *arena; + + x = tux->x; + y = tux->y; + + switch( tux->position ) + { + case TUX_UP : + y += TUX_HEIGHT; + break; + case TUX_RIGHT : + x -= TUX_WIDTH; + break; + case TUX_LEFT : + x += TUX_WIDTH; + break; + case TUX_DOWN : + y -= TUX_HEIGHT; + break; + } + + arena = getCurrentArena(); + + if( isFreeSpace(getCurrentArena(), x, y, ITEM_MINE_WIDTH, ITEM_MINE_HEIGHT) ) + { + item_t *item; + +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#ifndef NO_SOUND + playSound("put_mine", SOUND_GROUP_BASE); +#endif + sprintf(msg, "tux with id %d pu mine\n", tux->id); + appendTextInTerm(msg); +#endif + + tux->shot[tux->gun]--; + + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + item = newItem(x, y, ITEM_MINE, tux->id); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_additem_server(PROTO_SEND_ALL, NULL, item); + } + + addObjectToSpace(arena->spaceItem, item ); + } + } +} + +static void shotInGunBombball(tux_t *tux) +{ + addShot(tux, 0, 0, +10, 0); +} + +void shotInGun(tux_t *tux) +{ + if( tux->bonus != BONUS_SHOT && tux->gun != GUN_MINE ) + { + tux->shot[tux->gun]--; + } + + switch( tux->gun ) + { + case GUN_SIMPLE : +#ifndef NO_SOUND + playSound("gun_revolver", SOUND_GROUP_BASE); +#endif + shotInGunSimpe(tux); + break; + case GUN_DUAL_SIMPLE : +#ifndef NO_SOUND + playSound("gun_revolver", SOUND_GROUP_BASE); +#endif + shotInGunDualSimpe(tux); + break; + case GUN_SCATTER : +#ifndef NO_SOUND + playSound("gun_scatter", SOUND_GROUP_BASE); +#endif + shotInGunScatter(tux); + break; + case GUN_TOMMY : +#ifndef NO_SOUND + playSound("gun_tommy", SOUND_GROUP_BASE); +#endif + shotInGunTommy(tux); + break; + case GUN_LASSER : +#ifndef NO_SOUND + playSound("gun_lasser", SOUND_GROUP_BASE); +#endif + shotInGunLasser(tux); + break; + case GUN_BOMBBALL : + shotInGunBombball(tux); + break; + case GUN_MINE : + putInGunMine(tux); + break; + } +} diff --git a/src/base/gun.h b/src/base/gun.h new file mode 100644 index 0000000..4986660 --- /dev/null +++ b/src/base/gun.h @@ -0,0 +1,21 @@ + +#ifndef GUN_H + +#define GUN_H + +#include "main.h" + +#define GUN_SHOT_WIDTH 5 +#define GUN_SHOT_HEIGHT 5 + +#define GUN_LASSER_HORIZONTAL 50 +#define GUN_SHOT_VERTICAL 5 + +#define GUN_BOMBBALL_WIDTH 20 +#define GUN_BOMBBALL_HEIGHT 20 + +#include "tux.h" + +extern void shotInGun(tux_t *p); + +#endif diff --git a/src/base/homeDirector.c b/src/base/homeDirector.c new file mode 100755 index 0000000..0533d2e --- /dev/null +++ b/src/base/homeDirector.c @@ -0,0 +1,42 @@ + +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/homeDirector.h" + +static char homeDirector[STR_PATH_SIZE]; + +void createHomeDirector() +{ + char *envHome; + + envHome = getenv("HOME"); + + if( envHome == NULL ) + { + fprintf(stderr, "Environment HOME not found !\n"); + exit(0); + } + + sprintf(homeDirector, "%s/%s", envHome, HOMEDIRECTOR_NAME); + + if ( access(homeDirector, F_OK) != 0 ) + { + printf("Create home director %s\n", homeDirector); + + if( mkdir(homeDirector, 0755) != 0 ) + { + fprintf(stderr, "Can't create home directory!\n"); + exit(0); + } + } +} + +char *getHomeDirector() +{ + return homeDirector; +} diff --git a/src/base/homeDirector.h b/src/base/homeDirector.h new file mode 100755 index 0000000..a5c5e0c --- /dev/null +++ b/src/base/homeDirector.h @@ -0,0 +1,13 @@ + +#ifndef HOMEDIRECTOR_H + +#define HOMEDIRECTOR_H + +#include "base/main.h" + +#define HOMEDIRECTOR_NAME ".tuxanci-ng" + +extern void createHomeDirector(); +extern char *getHomeDirector(); + +#endif diff --git a/src/base/idManager.c b/src/base/idManager.c new file mode 100644 index 0000000..fb4a928 --- /dev/null +++ b/src/base/idManager.c @@ -0,0 +1,107 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/idManager.h" + +static list_t *listID; +static int lastID; + +void initListID() +{ + listID = newList(); + lastID = 0; + printf("init ID manger..\n"); +} + +int isRegisterID(int id) +{ + int i; + + assert( listID != NULL ); + + for( i = 0 ; i < listID->count ; i++ ) + { + int *z; + + z = (int *) listID->list[i]; + + if( *z == id ) + { + return i; + } + } + + return -1; +} + +int getNewID() +{ + int ret; + + assert( listID != NULL ); + + do{ + ret = random() % MAX_ID; +/* + ret = ++lastID; + + if( lastID > MAX_ID ) + { + lastID = 0; + } +*/ + }while( isRegisterID(ret) != -1 ); + + addList(listID, newInt(ret) ); + + //printf("new ID -> %d\n", ret); + return ret; +} + +void delID(int id) +{ + int index; + + assert( listID != NULL ); + + index = isRegisterID(id); + + if( index == -1 ) + { + assert( ! "takyto ID nebol nikdy registrovany !" ); + return; // ha ha ha + } + + delListItem(listID, index, free); + + return; +} + +void replaceID(int old_id, int new_id) +{ + if( old_id == new_id ) + { + return; + } + + delID(old_id); + + if( new_id != -1 ) + { + assert( isRegisterID(new_id) == -1 ); + addList(listID, newInt(new_id) ); + } +} + +void quitListID() +{ + printf("quit ID manger..\n"); + + assert( listID != NULL ); + destroyListItem(listID, free); +} + diff --git a/src/base/idManager.h b/src/base/idManager.h new file mode 100644 index 0000000..2b33ac5 --- /dev/null +++ b/src/base/idManager.h @@ -0,0 +1,19 @@ + + +#ifndef ID_MANAGER_H + +#define ID_MANAGER_H + +#define MAX_ID 1000 +#define ID_UNKNOWN -1 + +#include "base/list.h" + +extern void initListID(); +extern int getNewID(); +extern int isRegisterID(int id); +extern void delID(int id); +extern void replaceID(int old_id, int new_id); +extern void quitListID(); + +#endif diff --git a/src/base/item.c b/src/base/item.c new file mode 100644 index 0000000..73bfeaa --- /dev/null +++ b/src/base/item.c @@ -0,0 +1,620 @@ + +#include +#include + +#include "base/main.h" +#include "base/arena.h" +#include "base/tux.h" +#include "base/item.h" +#include "base/shot.h" +#include "base/proto.h" +#include "base/idManager.h" +#include "base/net_multiplayer.h" + +#ifndef PUBLIC_SERVER + +#include "client/interface.h" +#include "client/image.h" +#include "client/layer.h" +#include "client/term.h" + +#include "screen/screen_world.h" +#include "screen/screen_setting.h" + +#ifndef NO_SOUND +#include "audio/sound.h" +#endif + +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +#ifndef PUBLIC_SERVER +static SDL_Surface *g_item[ITEM_COUNT]; +#endif + +static bool_t isItemInit = FALSE; + +bool_t isItemInicialized() +{ + return isItemInit; +} + +void initItem() +{ +#ifndef PUBLIC_SERVER + g_item[GUN_DUAL_SIMPLE] = addImageData("item.dual.png", IMAGE_ALPHA, "item_dual_simple", IMAGE_GROUP_BASE); + g_item[GUN_TOMMY] = addImageData("item.tommy.png", IMAGE_ALPHA, "item_tommy_simple", IMAGE_GROUP_BASE); + g_item[GUN_SCATTER] = addImageData("item.scatter.png", IMAGE_ALPHA, "item_scatter_simple", IMAGE_GROUP_BASE); + g_item[GUN_LASSER] = addImageData("item.lasser.png", IMAGE_ALPHA, "item_lasser_simple", IMAGE_GROUP_BASE); + g_item[GUN_MINE] = addImageData("item.mine.png", IMAGE_ALPHA, "item_mines_simple", IMAGE_GROUP_BASE); + g_item[GUN_BOMBBALL] = addImageData("item.bombball.png", IMAGE_ALPHA, "item_bombball_simple", IMAGE_GROUP_BASE); + + g_item[ITEM_MINE] = addImageData("mine.png", IMAGE_ALPHA, "item_mine_simple", IMAGE_GROUP_BASE); + g_item[ITEM_EXPLOSION] = addImageData("explosion.png", IMAGE_ALPHA, "item_explosion_simple", IMAGE_GROUP_BASE); + g_item[ITEM_BIG_EXPLOSION] = addImageData("big-explosion.png", IMAGE_ALPHA, "item_big-explosion_simple", IMAGE_GROUP_BASE); + + g_item[BONUS_SPEED] = addImageData("item.bonus.speed.png", IMAGE_ALPHA, "item_bonus_speed", IMAGE_GROUP_BASE); + g_item[BONUS_SHOT] = addImageData("item.bonus.shot.png", IMAGE_ALPHA, "item_shot_speed", IMAGE_GROUP_BASE); + g_item[BONUS_TELEPORT] = addImageData("item.bonus.teleport.png", IMAGE_ALPHA, "item_teleport_speed", IMAGE_GROUP_BASE); + g_item[BONUS_GHOST] = addImageData("item.bonus.ghost.png", IMAGE_ALPHA, "item_ghost_speed", IMAGE_GROUP_BASE); + g_item[BONUS_4X] = addImageData("item.bonus.4x.png", IMAGE_ALPHA, "item_4x_speed", IMAGE_GROUP_BASE); + g_item[BONUS_HIDDEN] = addImageData("item.bonus.hidden.png", IMAGE_ALPHA, "item_hidden_speed", IMAGE_GROUP_BASE); +#endif + + isItemInit = TRUE; +} + +item_t* newItem(int x, int y, int type, int author_id) +{ + item_t *new; + + new = malloc( sizeof(item_t) ); + assert( new != NULL ); + + new->type = type; + + new->id = getNewID(); + new->x = x; + new->y = y; + new->frame = 0; + new->count = 0; +#ifndef PUBLIC_SERVER + new->img = g_item[type]; +#endif + new->author_id = author_id; + + switch( type ) + { + case GUN_DUAL_SIMPLE : + case GUN_SCATTER : + case GUN_TOMMY : + case GUN_LASSER : + case GUN_MINE : + case GUN_BOMBBALL : + new->w = ITEM_GUN_WIDTH; + new->h = ITEM_GUN_HEIGHT; + break; + + case ITEM_MINE : + new->w = ITEM_MINE_WIDTH; + new->h = ITEM_MINE_HEIGHT; + break; + + case ITEM_EXPLOSION : +#ifndef NO_SOUND + playSound("explozion", SOUND_GROUP_BASE); +#endif + new->w = ITEM_EXPLOSION_WIDTH; + new->h = ITEM_EXPLOSION_HEIGHT; + break; + + case ITEM_BIG_EXPLOSION : +#ifndef NO_SOUND + playSound("explozion", SOUND_GROUP_BASE); +#endif + new->w = ITEM_BIG_EXPLOSION_WIDTH; + new->h = ITEM_BIG_EXPLOSION_HEIGHT; + break; + + case BONUS_SPEED : + case BONUS_SHOT : + case BONUS_TELEPORT : + case BONUS_GHOST : + case BONUS_4X : + case BONUS_HIDDEN : + new->w = ITEM_BONUS_WIDTH; + new->h = ITEM_BONUS_HEIGHT; + break; + } + + return new; +} + +/* +item_t* getItemID(list_t *listItem, int id) +{ + item_t *thisItem; + int i; + + assert( listItem != NULL ); + + for( i = 0 ; i < listItem->count ; i++ ) + { + thisItem = (item_t *)listItem->list[i]; + assert( thisItem != NULL ); + + if( thisItem->id == id ) + { + return thisItem; + } + } + + return NULL; +} +*/ + +void getStatusItem(void *p, int *id, int *x, int *y ,int *w, int *h) +{ + item_t *item; + + item = p; + + *id = item->id; + *x = item->x; + *y = item->y; + *w = item->w; + *h = item->h; +} + +void setStatusItem(void *p, int x, int y, int w, int h) +{ + item_t *item; + + item = p; + item->x = x; + item->y = y; + item->w = w; + item->h = h; +} + +void replaceItemID(item_t *item, int id) +{ + replaceID(item->id, id); + item->id = id; +} + +void addNewItem(space_t *spaceItem, int author_id) +{ +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#endif + arena_t *arena; + item_t *item; + int new_x, new_y; + int type; + +#ifndef PUBLIC_SERVER + if( isSettingAnyItem() == FALSE || + getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + return; + } +#endif + + arena = getCurrentArena(); + findFreeSpace(getCurrentArena(), &new_x, &new_y, ITEM_GUN_WIDTH, ITEM_GUN_HEIGHT); + + type = GUN_DUAL_SIMPLE; + +#ifndef PUBLIC_SERVER + do{ +#endif + switch( random() % 12 ) + { + case 0 : type = GUN_DUAL_SIMPLE; + break; + case 1 : type = GUN_SCATTER; + break; + case 2 : type = GUN_TOMMY; + break; + case 3 : type = GUN_MINE; + break; + case 4 : type = GUN_LASSER; + break; + case 5 : type = GUN_BOMBBALL; + break; + case 6 : type = BONUS_SPEED; + break; + case 7 : type = BONUS_SHOT; + break; + case 8 : type = BONUS_TELEPORT; + break; + case 9 : type = BONUS_GHOST; + break; + case 10 : type = BONUS_4X; + break; + case 11 : type = BONUS_HIDDEN; + break; + } +#ifndef PUBLIC_SERVER + }while( isSettingItem(type) == FALSE || + (getNetTypeGame() == NET_GAME_TYPE_NONE && type == BONUS_HIDDEN) ); +#endif + + item = newItem(new_x, new_y, type, author_id); + addObjectToSpace(spaceItem, item); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_additem_server(PROTO_SEND_ALL, NULL, item); + } + +#ifndef PUBLIC_SERVER + sprintf(msg, "in arena is new item\n"); + appendTextInTerm(msg); +#endif + +} + +#ifndef PUBLIC_SERVER + +void drawItem(item_t *p) +{ + assert( p != NULL ); + + addLayer(p->img, p->x, p->y, p->frame * p->w, 0, p->w, p->h, TUX_LAYER); +} + +void drawListItem(list_t *listItem) +{ + item_t *thisItem; + int i; + + assert( listItem != NULL ); + + for( i = 0 ; i < listItem->count ; i++ ) + { + thisItem = (item_t *)listItem->list[i]; + assert( thisItem != NULL ); + drawItem(thisItem); + } +} + +#endif + +void eventListItem(space_t *spaceItem) +{ + my_time_t currentTime; + item_t *thisItem; + int i; + + currentTime = getMyTime(); + + assert( spaceItem != NULL ); + + for( i = 0 ; i < spaceItem->list->count ; i++ ) + { + thisItem = (item_t *)spaceItem->list->list[i]; + assert( thisItem != NULL ); + + thisItem->count++; + + if( thisItem->count == ITEM_MAX_COUNT ) + { + thisItem->count = 0; + thisItem->frame++; + } + + switch( thisItem->type ) + { + case GUN_TOMMY : + case GUN_DUAL_SIMPLE : + case GUN_SCATTER : + case GUN_LASSER : + case GUN_MINE : + case GUN_BOMBBALL : + if( thisItem->frame == ITEM_GUN_MAX_FRAME ) + { + thisItem->frame = 0; + } + break; + + case ITEM_MINE : + if( thisItem->frame == ITEM_MINE_MAX_FRAME ) + { + thisItem->frame = 0; + } + break; + + case ITEM_EXPLOSION : + case ITEM_BIG_EXPLOSION : + if( thisItem->frame == ITEM_EXPLOSION_MAX_FRAME ) + { + delObjectFromSpaceWithObject(spaceItem, thisItem, destroyItem); + i--; + } + break; + + case BONUS_SPEED : + case BONUS_SHOT : + case BONUS_TELEPORT : + case BONUS_GHOST : + case BONUS_4X : + case BONUS_HIDDEN : + if( thisItem->frame == ITEM_GUN_MAX_FRAME ) + { + thisItem->frame = 0; + } + break; + } + } +} + +void mineExplosion(space_t *spaceItem, item_t *item) +{ + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + int x, y; + item_t *item_explosion; + + x = ( item->x + item->w/2 ) - ITEM_BIG_EXPLOSION_WIDTH/2; + y = ( item->y + item->h/2 ) - ITEM_BIG_EXPLOSION_HEIGHT/2; + + item_explosion = newItem(x, y, ITEM_BIG_EXPLOSION, item->author_id); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_additem_server(PROTO_SEND_ALL, NULL, item_explosion); + } + + addObjectToSpace(spaceItem, item_explosion ); + } + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); + } + + delObjectFromSpaceWithObject(spaceItem, item, destroyItem); +} + +void eventConflictShotWithItem(arena_t *arena) +{ + shot_t *thisShot; + item_t *thisItem; + int i, j; + + if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + return; + } + + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) + { + bool_t isDelShot; + + isDelShot = FALSE; + thisShot = (shot_t *)arena->spaceShot->list->list[i]; + assert( thisShot != NULL ); + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceItem, thisShot->x, thisShot->y, thisShot->w, thisShot->h, listHelp); + + for( j = 0 ; j < listHelp->count ; j++ ) + { + thisItem = (item_t *)listHelp->list[j]; + assert( thisItem != NULL ); + + switch( thisItem->type ) + { + case ITEM_MINE : + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + mineExplosion(arena->spaceItem, thisItem); + } + break; + + case ITEM_EXPLOSION : + case ITEM_BIG_EXPLOSION : + isDelShot = TRUE; + break; + } + } + + if( isDelShot ) + { + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_del_server(PROTO_SEND_ALL, NULL, thisShot->id); + } + + delObjectFromSpaceWithObject(arena->spaceShot, thisShot, destroyShot); + i--; + } + } +} + +static void eventTuxIsDeadWithItem(tux_t *tux, item_t *item) +{ + if( tux->bonus == BONUS_GHOST ) + { + return; + } + + if( tux->bonus == BONUS_TELEPORT ) + { + tuxTeleport(tux); + return; + } + + if( item->author_id != tux->id ) + { + tux_t *author; + + author = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, item->author_id); + + if( author != NULL ) + { + author->score++; + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(PROTO_SEND_ALL, NULL, author); + } + } + } + + countRoundInc(); + eventTuxIsDead(tux); +} + +static void tuxGiveBonus(tux_t *tux, item_t *item) +{ +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#ifndef NO_SOUND + playSound("item_bonus", SOUND_GROUP_BASE); +#endif + +#endif + tux->bonus = item->type; + tux->bonus_time = TUX_MAX_BONUS; + +#ifndef PUBLIC_SERVER + sprintf(msg, "tux with id %d bonus enabled\n", tux->id); + appendTextInTerm(msg); +#endif + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); + } +} + +static void tuxGiveGun(tux_t *tux, item_t *item) +{ +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#ifndef NO_SOUND + playSound("item_gun", SOUND_GROUP_BASE); +#endif +#endif + + tux->pickup_time = 0; + tux->shot[ item->type ] += GUN_MAX_SHOT; + tux->gun = item->type; + +#ifndef PUBLIC_SERVER + sprintf(msg, "tux with id %d has new gun\n", tux->id); + appendTextInTerm(msg); +#endif + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); + } +} + +void eventGiveTuxItem(tux_t *tux, item_t *item, space_t *spaceItem) +{ + switch( item->type ) + { + case GUN_TOMMY : + case GUN_DUAL_SIMPLE : + case GUN_SCATTER : + case GUN_LASSER : + case GUN_MINE : + case GUN_BOMBBALL : + tuxGiveGun(tux, item); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); + } + + delObjectFromSpaceWithObject(spaceItem, item, destroyItem); + break; + + case ITEM_MINE : + if( tux->bonus != BONUS_GHOST ) + { + mineExplosion(spaceItem, item); + } + break; + + case ITEM_EXPLOSION : + case ITEM_BIG_EXPLOSION : + eventTuxIsDeadWithItem(tux, item); + break; + + case BONUS_SPEED : + case BONUS_SHOT : + case BONUS_TELEPORT : + case BONUS_GHOST : + case BONUS_4X : + case BONUS_HIDDEN : + tuxGiveBonus(tux, item); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); + } + + delObjectFromSpaceWithObject(spaceItem, item, destroyItem); + break; + } +} + +void eventGiveTuxListItem(tux_t *tux, space_t *spaceItem) +{ + item_t *thisItem; + int x, y, w, h; + int i; + + if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + return; + } + + assert( spaceItem != NULL ); + assert( tux != NULL ); + + if( tux->status == TUX_STATUS_DEAD ) + { + return; + } + + getTuxProportion(tux, &x, &y, &w, &h); + + listDoEmpty(listHelp); + getObjectFromSpace(spaceItem, x, y, w, h, listHelp); + + for( i = 0 ; i < listHelp->count ; i++ ) + { + thisItem = (item_t *)listHelp->list[i]; + assert( thisItem != NULL ); + + eventGiveTuxItem(tux, thisItem, spaceItem); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + //proto_send_item_server(PROTO_SEND_ALL, NULL, tux, thisItem); + proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); + } + } +} + +void destroyItem(item_t *p) +{ + assert( p != NULL ); + delID(p->id); + free(p); +} + +void quitItem() +{ + isItemInit = FALSE; +} diff --git a/src/base/item.h b/src/base/item.h new file mode 100644 index 0000000..ec11654 --- /dev/null +++ b/src/base/item.h @@ -0,0 +1,79 @@ + +#ifndef ITEM_H + +#define ITEM_H + +#include "base/main.h" + +#define ITEM_SYNC_TIMEOUT 5000 + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#endif + +#include "base/list.h" +#include "base/tux.h" +#include "base/myTimer.h" + +typedef struct item_struct +{ + int id; + int type; // typ veci + + int x; // poloha veci + int y; + + int w; // rozmery veci + int h; + + int frame; //poradove cislo animacie + int count; + + int author_id; +#ifndef PUBLIC_SERVER + SDL_Surface *img; //obrazok +#endif +} item_t; + +#define ITEM_MAX_COUNT 1 + +#define ITEM_GUN_MAX_FRAME 8 +#define ITEM_MINE_MAX_FRAME 1 +#define ITEM_EXPLOSION_MAX_FRAME 10 +#define ITEM_BONUS_MAX_FRAME 8 + +#define ITEM_GUN_WIDTH 50 +#define ITEM_GUN_HEIGHT 20 + +#define ITEM_MINE_WIDTH 15 +#define ITEM_MINE_HEIGHT 11 + +#define ITEM_EXPLOSION_WIDTH 40 +#define ITEM_EXPLOSION_HEIGHT 40 + +#define ITEM_BIG_EXPLOSION_WIDTH 100 +#define ITEM_BIG_EXPLOSION_HEIGHT 100 + +#define ITEM_BONUS_WIDTH 20 +#define ITEM_BONUS_HEIGHT 20 + +extern bool_t isItemInicialized(); +extern void initItem(); +extern item_t* newItem(int x, int y, int type, int author_id); +extern void getStatusItem(void *p, int *id, int *x, int *y ,int *w, int *h); +extern void setStatusItem(void *p, int x, int y, int w, int h); +extern void replaceItemID(item_t *item, int id); +extern void addNewItem(space_t *spaceItem, int author_id); +#ifndef PUBLIC_SERVER +extern void drawListItem(list_t *listItem); +#endif +extern void eventListItem(space_t *listSpace); +extern void mineExplosion(space_t *spaceItem, item_t *item); +extern void eventConflictShotWithItem(arena_t *arena); +extern void eventGiveTuxItem(tux_t *tux, item_t *item, space_t *spaceItem); +extern void eventGiveTuxListItem(tux_t *tux, space_t *spaceItem); +extern void destroyItem(item_t *p); +extern void quitItem(); + +#endif + diff --git a/src/base/list.c b/src/base/list.c new file mode 100755 index 0000000..d182210 --- /dev/null +++ b/src/base/list.c @@ -0,0 +1,241 @@ + +#include "base/main.h" +#include "base/list.h" + +#include +#include +#include +#include + +list_t* newList() +{ + list_t *new; + + new = malloc( sizeof(list_t) ); + memset(new, 0, sizeof(list_t) ); + + return new; +} + +list_t* cloneList(list_t *p) +{ + list_t *new; + + assert( p != NULL ); + + new = malloc( sizeof(list_t) ); + memcpy(new, p, sizeof(list_t) ); + + if( p->list != NULL ) + { + new->list = malloc( p->alloc * sizeof(void *) ); + memcpy(new->list, p->list, p->alloc * sizeof(void *)); + } + + return new; +} + +list_t* cloneListItem(list_t *p, void *f) +{ + list_t *new; + void* (*fce)(void *); + int i; + + assert( p != NULL ); + assert( f != NULL ); + + new = cloneList(p); + fce = f; + + for(i = 0 ; i < p->count ; i++) + new->list[i] = fce(p->list[i]); + + return new; +} + +void addList(list_t *p, void *item) +{ + assert( p != NULL ); + + if( p->alloc == 0 ) + { + p->alloc = LIST_ALLOC_LIMIT; + p->count = 1; + p->list = malloc(p->alloc * sizeof(void *) ); + p->list[0] = item; + + return; + } + + if( p->count + 1 <= p->alloc ) + { + p->list[p->count] = item; + p->count += 1; + + return; + } + + if( p->count + 1 > p->alloc ) + { + void **new; + +#ifdef DEBUG_LIST + printf("realokujem z %d na %d (count=%d)\n", + p->alloc, p->alloc+LIST_ALLOC_LIMIT, p->count); +#endif + + p->alloc += LIST_ALLOC_LIMIT; + new = malloc(p->alloc * sizeof(void *) ); + memcpy(new, p->list, p->count * sizeof(void *) ); + free(p->list); + p->list = new; + p->list[p->count] = item; + p->count++; + + return; + } +} + +void insList(list_t *p, int n, void *item) +{ + assert( p != NULL ); + + addList(p, NULL); // :) + + assert( n >= 0 || n < p->count ); + + memmove(p->list+n+1, p->list+n, ( (p->count-1) - n ) * sizeof(void *)); + + p->list[n] = item; +} + +void *getList(list_t *p, int n) +{ + assert( p != NULL ); + assert( n >= 0 || n < p->count ); + + return p->list[n]; +} + +int searchListItem(list_t *p, void *n) +{ + int i; + + assert( p != NULL ); + + for( i = 0 ; i < p->count ; i++ ) + if( p->list[i] == n )return i; + + return -1; +} + +void delList(list_t *p, int n) +{ + assert( p != NULL ); + assert( n >= 0 || n < p->count ); + + memmove(p->list+n, p->list+n+1, ( (p->count-1) - n ) * sizeof(void *)); + + p->count--; + + if( p->count + LIST_ALLOC_LIMIT < p->alloc ) + { + void **new; + +#ifdef DEBUG_LIST + printf("realokujem z %d na %d (count=%d)\n", + p->alloc, p->alloc-LIST_ALLOC_LIMIT, p->count); +#endif + + p->alloc -= LIST_ALLOC_LIMIT; + new = malloc(p->alloc * sizeof(void *) ); + memcpy(new, p->list, p->count * sizeof(void *) ); + free(p->list); + p->list = new; + } +} + +void delListItem(list_t *p, int n, void *f) +{ + int (*fce)(void *); + + assert( p != NULL ); + assert( n >= 0 || n < p->count ); + + fce = f; + if( fce != NULL )fce(p->list[n]); + + delList(p, n); +} + +void listDoEmpty(list_t *p) +{ + p->count = 0; +} + +void destroyList(list_t *p) +{ + assert( p != NULL ); + + if( p->list != NULL )free(p->list); + free(p); +} + +void destroyListItem(list_t *p, void *f) +{ + void (*fce)(void *); + int i; + + assert( p != NULL ); + assert( f != NULL ); + + fce = f; + + for(i = 0 ; i < p->count ; i++) + fce(p->list[i]); + + destroyList(p); +} + +/* +typedef struct pokus_str +{ + int x; +} pokus; + +pokus* newPokus(int n) +{ + pokus *new; + new = malloc(sizeof(pokus)); + new->x = n; + return new; +} + +pokus *clonePokus(pokus *p) +{ + pokus *new; + new = malloc(sizeof(pokus)); + memcpy(new, p, sizeof(pokus)); + return new; +} + +void destroyPokus(pokus *p) +{ + free(p); +} + +int main() +{ + list_t *my; + int i; + + my = newList(); + + for(i = 0; i < 100; i++) + addList(my, newPokus(i)); + + destroyListItem(my, destroyPokus); + + return 0; +} +*/ diff --git a/src/base/list.h b/src/base/list.h new file mode 100755 index 0000000..17a8e2d --- /dev/null +++ b/src/base/list.h @@ -0,0 +1,33 @@ + +#ifndef LIST_H + +#define LIST_H + +#include "base/main.h" + +#define LIST_ALLOC_LIMIT 16 + +typedef struct list_str +{ + void **list; + int count; + int alloc; +} list_t; + +extern list_t* newList(); +extern list_t* cloneList(list_t *p); +extern list_t* cloneListItem(list_t *p, void *f); + +extern void addList(list_t *p, void *item); +extern void insList(list_t *p, int n, void *item); +extern void *getList(list_t *p,int n); +extern int searchListItem(list_t *p, void *n); + +extern void delList(list_t *p,int n); +extern void delListItem(list_t *p,int n,void *f); +extern void listDoEmpty(list_t *p); + +extern void destroyList(list_t *p); +extern void destroyListItem(list_t *p,void *f); + +#endif diff --git a/src/base/main.c b/src/base/main.c new file mode 100644 index 0000000..ffebbe0 --- /dev/null +++ b/src/base/main.c @@ -0,0 +1,126 @@ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/tux.h" + +#ifndef PUBLIC_SERVER +#include "client/game.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +static int my_argc; +static char **my_argv; + +list_t *listHelp; + +char* getParam(char *s) +{ + int i; + int len; + + len = strlen(s); + + for( i = 1 ; i < my_argc ; i++ ) + { + //printf("%s %s\n", s, my_argv[i]); + + if( strlen(my_argv[i]) < len ) + { + continue; + } + + if( strncmp(s, my_argv[i], len) == 0 ) + { + return strchr(my_argv[i], '=')+1; + } + } + + return NULL; +} + +char* getParamElse(char *s1, char *s2) +{ + char *ret; + ret = getParam(s1); + + if( ret == NULL) + { + return s2; + } + + return ret; +} + +bool_t isParamFlag(char *s) +{ + int i; + + for( i = 0 ; i < my_argc ; i++ ) + { + if( strcmp(s, my_argv[i]) == 0 ) + { + return TRUE; + } + } + + return FALSE; +} + +char *getString(int n) +{ + char str[STR_NUM_SIZE]; + sprintf(str, "%d", n); + return strdup(str); +} + +int* newInt(int x) +{ + int *new; + new = malloc( sizeof(int) ); + *new = x; + return new; +} + +void accessExistFile(const char *s) +{ + if( access(s, F_OK) != 0 ) + { + fprintf(stderr, "File %s not found !\nProgram shutdown !\n", s); + exit(-1); + } +} + +int main(int argc, char *argv[]) +{ + listHelp = newList(); + + srand( (unsigned) time(NULL) ); + + my_argc = argc; + my_argv = argv; +/* + test_space(); + exit(0); +*/ + +#ifndef PUBLIC_SERVER + startGame(); +#endif + +#ifdef PUBLIC_SERVER + startPublicServer(); +#endif + + return 0; +} diff --git a/src/base/main.h b/src/base/main.h new file mode 100644 index 0000000..b38a1d7 --- /dev/null +++ b/src/base/main.h @@ -0,0 +1,33 @@ + +#ifndef MAIN_H + +#define MAIN_H + +#include "../../config.h" + +#if defined PUBLIC_SERVER + +#undef DESTDIR + +#endif + +#include "base/bool.h" +#include "base/string_length.h" +#include "base/path.h" + +//rozlisenie obrazovky +#define WINDOW_SIZE_X 800 +#define WINDOW_SIZE_Y 600 + +#include "base/list.h" + +extern list_t *listHelp; + +extern char* getParam(char *s); +extern char* getParamElse(char *s1, char *s2); +extern bool_t isParamFlag(char *s); +extern char *getString(int n); +extern int* newInt(int x); +extern void accessExistFile(const char *s); + +#endif diff --git a/src/base/modules.c b/src/base/modules.c new file mode 100755 index 0000000..aa881d5 --- /dev/null +++ b/src/base/modules.c @@ -0,0 +1,222 @@ + +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/shot.h" +#include "base/modules.h" +#include "base/arena.h" +#include "base/arenaFile.h" +#include "base/proto.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#include "client/image.h" +#include "client/layer.h" +#include "client/configFile.h" +#endif + +int pokus(char *s) +{ + printf("s = %s\n", s); + return 0; +} + +static export_fce_t export_fce = + { +#ifndef PUBLIC_SERVER + .fce_addLayer = addLayer, + .fce_getImage = getImage, +#endif + .fce_getValue = getArenaValue, + .fce_getNetTypeGame = getNetTypeGame, + .fce_getTuxProportion = getTuxProportion, + .fce_setTuxProportion = setTuxProportion, + //.fce_getTuxID = getTuxID, + .fce_actionTux = actionTux, + + .fce_getMyTime = getMyTime, + + .fce_getCurrentArena = getCurrentArena, + .fce_conflictSpace = conflictSpace, + .fce_isFreeSpace = isFreeSpace, + .fce_findFreeSpace = findFreeSpace, + .fce_proto_send_newtux_server = proto_send_newtux_server, + .fce_proto_send_shot_server = proto_send_shot_server, + .fce_destroyShot = destroyShot, + .fce_boundBombBall = boundBombBall, + .fce_transformOnlyLasser = transformOnlyLasser + }; + +static list_t *listModule; + +static void* getFce(module_t *p, char *s) +{ + void *ret; + char *error; + + assert( p != NULL ); + assert( s != NULL ); + + ret = dlsym(p->image, s); + + if( ( error = dlerror() ) != NULL) + { + fprintf (stderr, "error = %s\n", error); + return NULL; + } + + return (void *)ret; +} + +static module_t* newModule(char *name) +{ + module_t *ret; + + assert( name != NULL ); + + ret = malloc( sizeof(module_t) ); + + ret->image = dlopen (name, RTLD_LAZY); + + if(!ret->image) + { + fputs (dlerror(), stderr); + free(ret); + return NULL; + } + + ret->file = strdup(name); + + ret->fce_init = getFce(ret, "init"); +#ifndef PUBLIC_SERVER + ret->fce_draw = getFce(ret, "draw"); +#endif + ret->fce_event = getFce(ret, "event"); + ret->fce_destroy = getFce(ret, "destroy"); + ret->fce_isConflict = getFce(ret, "isConflict"); + ret->fce_cmd = getFce(ret, "cmd"); + + printf("load module.. (%s)\n", name); + + if( ret->fce_init(&export_fce) != 0 ) + { + fprintf(stderr, "init module failed !\n"); + + dlclose(ret->image); + free(ret->file); + free(ret); + return NULL; + } + + return ret; +} + +static int destroyModule(module_t *p) +{ + assert( p != NULL ); + + p->fce_destroy(); + + free(p->file); + dlclose(p->image); + free(p); + + printf("destroy module..\n"); + + return 0; +} + +void initModule() +{ + listModule = newList(); +} + +int loadModule(char *name) +{ + char str[STR_PATH_SIZE]; + module_t *module; + + sprintf(str, PATH_MODULES "%s", name); + accessExistFile(str); + + module = newModule(str); + + if( module == NULL ) + { + fprintf(stderr, "load module %s failed !\n", name); + return -1; + } + + addList(listModule, module); + + return 0; +} + +void cmdModule(char *s) +{ + int i; + + for( i = 0 ; i < listModule->count ; i++ ) + { + module_t *this; + this = (module_t *)listModule->list[i]; + this->fce_cmd(s); + } +} + +#ifndef PUBLIC_SERVER + +void drawModule(int x, int y, int w, int h) +{ + int i; + + for( i = 0 ; i < listModule->count ; i++ ) + { + module_t *this; + this = (module_t *)listModule->list[i]; + this->fce_draw(x, y, w, h); + } +} + +#endif + +void eventModule() +{ + int i; + + for( i = 0 ; i < listModule->count ; i++ ) + { + module_t *this; + this = (module_t *)listModule->list[i]; + this->fce_event(); + } +} + +int isConflictModule(int x, int y, int w, int h) +{ + int i; + + for( i = 0 ; i < listModule->count ; i++ ) + { + module_t *this; + this = (module_t *)listModule->list[i]; + + if( this->fce_isConflict(x, y, w, h) == 1 ) + { + return 1; + } + } + + return 0; +} + +void quitModule() +{ + destroyListItem(listModule, destroyModule); +} diff --git a/src/base/modules.h b/src/base/modules.h new file mode 100644 index 0000000..3b50ade --- /dev/null +++ b/src/base/modules.h @@ -0,0 +1,78 @@ + +#ifndef MODULE_H + +#define MODULE_H + +#include "base/tux.h" +#include "base/shot.h" +#include "base/arena.h" +#include "base/proto.h" +#include "base/myTimer.h" + +#ifndef PUBLIC_SERVER +#include "client/image.h" +#endif + +typedef struct export_fce_s +{ + int (*fce_getValue)(char *line, char *env, char *val, int len); + +#ifndef PUBLIC_SERVER + SDL_Surface* (*fce_getImage)(char *group, char *name); + + void (*fce_addLayer)(SDL_Surface *img, + int x,int y, int px,int py, + int w,int h, int player); +#endif + + int (*fce_getNetTypeGame)(); + + void (*fce_getTuxProportion)(tux_t *tux, int *x,int *y, int *w, int *h); + void (*fce_setTuxProportion)(tux_t *tux, int x, int y); + tux_t* (*fce_getTuxID)(list_t *listTux, int id); + void (*fce_actionTux)(tux_t *tux, int action); + + arena_t* (*fce_getCurrentArena)(); + int (*fce_conflictSpace)(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2); + int (*fce_isFreeSpace)(arena_t *arena, int x, int y, int w, int h); + void (*fce_findFreeSpace)(arena_t *arena, int *x, int *y, int w, int h); + void (*fce_proto_send_newtux_server)(int type, client_t *client, tux_t *tux); + void (*fce_proto_send_shot_server)(int type, client_t *client, shot_t *p); + + my_time_t (*fce_getMyTime)(); + + void (*fce_destroyShot)(shot_t *p); + void (*fce_boundBombBall)(shot_t *shot); + void (*fce_transformOnlyLasser)(shot_t *shot); + + int (*fce_pokus)(char *s); + +} export_fce_t; + +typedef struct module_s +{ + char *file; + void *image; + + int (*fce_init)(export_fce_t *p); +#ifndef PUBLIC_SERVER + int (*fce_draw)(int x, int y,int w, int h); +#endif + int (*fce_event)(); + int (*fce_isConflict)(int x, int y, int w, int h); + void (*fce_cmd)(char *line); + int (*fce_destroy)(); + +} module_t; + +extern void initModule(); +extern int loadModule(char *name); +#ifndef PUBLIC_SERVER +extern void drawModule(int x, int y, int w, int h); +#endif +extern void eventModule(); +extern void cmdModule(char *s); +extern int isConflictModule(int x, int y, int w, int h); +extern void quitModule(); + +#endif diff --git a/src/base/myTimer.c b/src/base/myTimer.c new file mode 100644 index 0000000..7913f8b --- /dev/null +++ b/src/base/myTimer.c @@ -0,0 +1,148 @@ + +#include +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/myTimer.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#endif + +static struct timeval start = { .tv_sec = 0, .tv_usec = 0 }; + +list_t* newTimer() +{ + return newList(); +} + +void restartTimer() +{ + gettimeofday(&start, NULL); +} + + +my_time_t getMyTime() +{ + struct timeval now; + my_time_t ticks; + + if( start.tv_sec == 0 && start.tv_usec == 0 ) + { + restartTimer(); + } + + gettimeofday(&now, NULL); + + ticks = (now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000; + + //printf("-> %d\n", ticks); + return ticks; +} + +my_timer_t* newTimerItem(int type, void (*fce)(void *p), void *arg, my_time_t my_time) +{ + static int new_id = 0; + my_timer_t *new; + + assert( fce != NULL ); + + new = malloc( sizeof(my_timer_t) ); + memset(new, 0, sizeof(my_timer_t)); + + new->id = new_id++; + new->type = type; + new->fce = fce; + new->arg = arg; + + new->createTime = getMyTime(); + new->time = my_time; + + return new; +} + +static void destroyTimerItem(my_timer_t *p) +{ + assert( p != NULL ); + free(p); +} + +int addTaskToTimer(list_t *listTimer, int type, void (*fce)(void *p), void *arg, my_time_t my_time) +{ + my_timer_t *new; + + new = newTimerItem(type, fce, arg, my_time); + addList(listTimer, new); + + return new->id; +} + +void eventTimer(list_t *listTimer) +{ + int i; + my_timer_t *thisTimer; + my_time_t currentTime; + + currentTime = getMyTime(); + + for( i = 0 ; i < listTimer->count ; i++ ) + { + thisTimer = (my_timer_t *)listTimer->list[i]; + assert( thisTimer != NULL ); + + switch( thisTimer->type ) + { + case TIMER_ONE: + if( currentTime >= thisTimer->createTime + thisTimer->time ) + { + thisTimer->fce(thisTimer->arg); + + delListItem(listTimer, i, free); + i--; + } + break; + case TIMER_PERIODIC: + if( currentTime >= thisTimer->createTime + thisTimer->time ) + { + thisTimer->fce(thisTimer->arg); + thisTimer->createTime = getMyTime(); + } + break; + default : + assert( ! "bad timer type !" ); + break; + } + + } +} + +void delTimer(list_t *listTimer, int id) +{ + int i; + my_timer_t *thisTimer; + + for( i = 0 ; i < listTimer->count ; i++ ) + { + thisTimer = (my_timer_t *)listTimer->list[i]; + assert( thisTimer != NULL ); + + if( thisTimer->id == id ) + { + delListItem(listTimer, i, free); + return; + } + } + + assert( ! "Uloha s id nenajdena !" ); +} + + +void destroyTimer(list_t *listTimer) +{ + destroyListItem(listTimer, destroyTimerItem); +} diff --git a/src/base/myTimer.h b/src/base/myTimer.h new file mode 100644 index 0000000..2f038da --- /dev/null +++ b/src/base/myTimer.h @@ -0,0 +1,32 @@ + +#ifndef MY_TIME_H + +#define MY_TIME_H + +#include "base/main.h" +#include "base/list.h" + +#define my_time_t unsigned int + +#define TIMER_ONE 0 +#define TIMER_PERIODIC 1 + +typedef struct my_timer_struct +{ + unsigned int id; + void (*fce)(void *p); + void *arg; + int type; + my_time_t createTime; + my_time_t time; +} my_timer_t; + +extern list_t* newTimer(); +extern void restartTimer(); +extern my_time_t getMyTime(); +extern int addTaskToTimer(list_t *listTimer, int type, void (*fce)(void *p), void *arg, my_time_t my_time); +extern void eventTimer(list_t *listTimer); +extern void delTimer(list_t *listTimer, int id); +extern void destroyTimer(list_t *listTimer); + +#endif diff --git a/src/base/net_multiplayer.c b/src/base/net_multiplayer.c new file mode 100644 index 0000000..bc8ecab --- /dev/null +++ b/src/base/net_multiplayer.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/item.h" +#include "base/arenaFile.h" +#include "base/net_multiplayer.h" +#include "base/server.h" + +#ifdef SUPPORT_NET_UNIX_UDP +#include "net_unix/udp.h" +#endif + +#ifdef SUPPORT_NET_SDL_UDP +#include "net_sdl/sdl_udp.h" +#endif + +#ifndef PUBLIC_SERVER +#include "screen/screen_setting.h" +#include "screen/screen_choiceArena.h" + +#include "client/screen.h" +#include "client/client.h" +#endif + +static int netGameType; + +int getNetTypeGame() +{ + return netGameType; +} + +#ifdef PUBLIC_SERVER + +int initNetMulitplayerPublicServer(char *ip4, int port4, char *ip6, int port6) +{ + netGameType = NET_GAME_TYPE_SERVER; + return initUdpPublicServer(ip4, port4, ip6, port6); +} +#endif + +int initNetMuliplayer(int type, char *ip, int port, int proto) +{ + netGameType = type; + + switch( netGameType ) + { + case NET_GAME_TYPE_NONE : + break; + + case NET_GAME_TYPE_SERVER : +#ifdef SUPPORT_NET_UNIX_UDP + if( initUdpServer(ip, port, proto) != 0 ) + { + fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n"); + netGameType = NET_GAME_TYPE_NONE; + return -1; + } +#endif +#ifdef SUPPORT_NET_SDL_UDP + if( initSdlUdpServer(port) != 0 ) + { + fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n"); + netGameType = NET_GAME_TYPE_NONE; + return -1; + } +#endif + break; + +#ifndef PUBLIC_SERVER + case NET_GAME_TYPE_CLIENT : +#ifdef SUPPORT_NET_UNIX_UDP + if( initUdpClient(ip, port, proto) != 0 ) + { + fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n"); + netGameType = NET_GAME_TYPE_NONE; + return -1; + } +#endif +#ifdef SUPPORT_NET_SDL_UDP + if( initSdlUdpClient(ip, port) != 0 ) + { + fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n"); + netGameType = NET_GAME_TYPE_NONE; + return -1; + } +#endif + break; +#endif + default : + assert( ! "Premenna netGameType ma zlu hodnotu !" ); + break; + } + + return 0; +} + +void eventNetMultiplayer() +{ + switch( netGameType ) + { + case NET_GAME_TYPE_NONE : + break; + + case NET_GAME_TYPE_SERVER : +#ifdef SUPPORT_NET_UNIX_UDP + eventServer(); +#endif +#ifdef SUPPORT_NET_SDL_UDP + eventServer(); +#endif + break; + +#ifndef PUBLIC_SERVER + case NET_GAME_TYPE_CLIENT : +#ifdef SUPPORT_NET_UNIX_UDP + eventPingServer(); + selectClientUdpSocket(); + eventServerBuffer(); +#endif +#ifdef SUPPORT_NET_SDL_UDP + eventPingServer(); + selectClientSdlUdpSocket(); + eventServerBuffer(); +#endif +#endif + break; + default : + assert( ! "Premenna netGameType ma zlu hodnotu !" ); + break; + } +} + +void quitNetMultiplayer() +{ + switch( netGameType ) + { + case NET_GAME_TYPE_NONE : + break; + + case NET_GAME_TYPE_SERVER : +#ifdef SUPPORT_NET_UNIX_UDP + quitUdpServer(); +#endif +#ifdef SUPPORT_NET_SDL_UDP + quitSdlUdpServer(); +#endif + break; + +#ifndef PUBLIC_SERVER + case NET_GAME_TYPE_CLIENT : +#ifdef SUPPORT_NET_UNIX_UDP + quitUdpClient(); +#endif +#ifdef SUPPORT_NET_SDL_UDP + quitSdlUdpClient(); +#endif + break; +#endif + + default : + assert( ! "Premenna netGameType ma zlu hodnotu !" ); + break; + } + + netGameType = NET_GAME_TYPE_NONE; +} diff --git a/src/base/net_multiplayer.h b/src/base/net_multiplayer.h new file mode 100644 index 0000000..ad5eb41 --- /dev/null +++ b/src/base/net_multiplayer.h @@ -0,0 +1,35 @@ + +#ifndef NET_MULTIPLAYER_H + +#define NET_MULTIPLAYER_H + +#include "base/main.h" +#include "base/tux.h" +#include "base/item.h" +#include "base/arena.h" + +#define PROTO_UDPv4 0 +#define PROTO_UDPv6 1 + +#define NET_GAME_TYPE_NONE 0 +#define NET_GAME_TYPE_SERVER 1 +#define NET_GAME_TYPE_CLIENT 2 + +#define NET_STATUS_OK 0 +#define NET_STATUS_ZOMBIE 1 + +#define NET_PROTOCOL_TYPE_TCP 0 +#define NET_PROTOCOL_TYPE_UDP 1 + +#define LIMIT_BUFFER 10240 + +#ifdef PUBLIC_SERVER +extern int initNetMulitplayerPublicServer(char *ip4, int port4, char *ip6, int port6); +#endif + +extern int initNetMuliplayer(int type, char *ip, int port, int proto); +extern int getNetTypeGame(); +extern void eventNetMultiplayer(); +extern void quitNetMultiplayer(); + +#endif diff --git a/src/base/path.h b/src/base/path.h new file mode 100644 index 0000000..9c26ea4 --- /dev/null +++ b/src/base/path.h @@ -0,0 +1,33 @@ + +#ifndef PATH_H + +#define PATH_H + +#include "base/main.h" + +#ifdef DESTDIR + +#define PATH_DIR "share/tuxanci-ng/" + +#endif + +#ifndef DESTDIR + +#define PATH_DIR "./" + +#undef DESTDIR +#define DESTDIR "" + +#endif + +#define PATH_IMAGE DESTDIR PATH_DIR "image/" +#define PATH_FONT DESTDIR PATH_DIR "font/" +#define PATH_ARENA DESTDIR PATH_DIR "arena/" +#define PATH_SOUND DESTDIR PATH_DIR "sound/" +#define PATH_MUSIC DESTDIR PATH_DIR "music/" +#define PATH_CONFIG DESTDIR PATH_DIR "conf/" +#define PATH_LANG DESTDIR PATH_DIR "lang/" +#define PATH_DATA DESTDIR PATH_DIR "data/" +#define PATH_MODULES DESTDIR PATH_DIR "modules/" + +#endif diff --git a/src/base/protect.c b/src/base/protect.c new file mode 100644 index 0000000..074873a --- /dev/null +++ b/src/base/protect.c @@ -0,0 +1,88 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/myTimer.h" +#include "base/protect.h" + +protect_t* newProtect() +{ + protect_t *new; + + new = malloc( sizeof(protect_t) ); + new->lastMove = getMyTime(); + new->lastPing = getMyTime(); + new->avarage = 0; + new->count = 0; + new->isDown = FALSE; + return new; +} + +void refreshLastMove(protect_t *p) +{ + my_time_t currentTime; + my_time_t interval; + + currentTime = getMyTime(); + + interval = currentTime - p->lastMove; + p->lastMove = getMyTime(); + + p->avarage += interval; + p->count++; + + if( p->count == PROTECT_SPEED_AVARAGE ) + { + int index; + + index = p->avarage/PROTECT_SPEED_AVARAGE; + p->avarage = 0; + p->count = 0; + + if( index < PROTECT_SPEED_INTERVAL_TIMEOUT ) + { + p->isDown = TRUE; + } + + //printf("speed index = %d\n", index); + } +} + +void rereshLastPing(protect_t *p) +{ + //my_time_t currentTime; + //my_time_t interval; + + //currentTime = getMyTime(); + + //interval = currentTime - p->lastPing; + p->lastPing = getMyTime(); + + //printf("interval = %d\n", interval); +} + +bool_t isDown(protect_t *p) +{ + my_time_t currentTime; + my_time_t interval; + + currentTime = getMyTime(); + + interval = currentTime - p->lastPing; + + if( interval > PROTECT_PING_INTERVAL_TIMEOUT ) + { + p->isDown = TRUE; + } + + return p->isDown; +} + +void destroyProtect(protect_t *p) +{ + assert( p != NULL ); + free(p); +} diff --git a/src/base/protect.h b/src/base/protect.h new file mode 100644 index 0000000..b0db5a9 --- /dev/null +++ b/src/base/protect.h @@ -0,0 +1,30 @@ + +#ifndef PROTECT_H + +#define PROTECT_H + +#include "base/main.h" +#include "base/myTimer.h" + +#define PROTECT_PING_INTERVAL_TIMEOUT 5000 +#define PROTECT_SPEED_INTERVAL_TIMEOUT 40 +#define PROTECT_SPEED_AVARAGE 10 + +typedef struct protect_struct +{ + my_time_t lastPing; + + my_time_t lastMove; + my_time_t avarage; + int count; + + bool_t isDown; +} protect_t; + +extern protect_t* newProtect(); +extern void refreshLastMove(protect_t *p); +extern void rereshLastPing(protect_t *p); +extern bool_t isDown(protect_t *p); +extern void destroyProtect(protect_t *p); + +#endif diff --git a/src/base/proto.c b/src/base/proto.c new file mode 100644 index 0000000..52c9dd6 --- /dev/null +++ b/src/base/proto.c @@ -0,0 +1,713 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/item.h" +#include "base/shot.h" +#include "base/arenaFile.h" +#include "base/myTimer.h" +#include "base/server.h" +#include "base/proto.h" +#include "base/net_multiplayer.h" +#include "base/checkFront.h" +#include "base/idManager.h" +#include "base/protect.h" + +#ifndef PUBLIC_SERVER +#include "client/language.h" +#include "screen/screen_world.h" +#include "screen/screen_setting.h" +#include "screen/screen_choiceArena.h" +#include "screen/screen_analyze.h" +#include "client/client.h" +#include "client/term.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/heightScore.h" +#include "server/publicServer.h" +#endif + +void proto_send_error_server(int type, client_t *client, int errorcode) +{ + char msg[STR_PROTO_SIZE]; + + sprintf(msg, "error %d\n", errorcode); + + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_error_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int errorcode; + + assert( msg != NULL ); + + sscanf(msg, "%s %d", + cmd, &errorcode); + + printf("proto error code %d\n", errorcode); + + switch(errorcode) + { + case PROTO_ERROR_CODE_BAD_VERSION : + setMsgToAnalyze(getMyText("ERROR_BAD_VERSION")); + setWorldEnd(); + break; + } +} + +#endif + +#ifndef PUBLIC_SERVER + +void proto_send_hello_client(char *name) +{ + char msg[STR_PROTO_SIZE]; + + sprintf(msg, "hello %s %s\n", TUXANCI_NG_VERSION, name); + sendServer(msg); +} + +#endif + +void proto_recv_hello_server(client_t *client, char *msg) +{ + char cmd[STR_PROTO_SIZE]; + char version[STR_NAME_SIZE]; + char name[STR_NAME_SIZE]; + + assert( client != NULL ); + + strcpy(version, ""); + strcpy(name, ""); + + sscanf(msg, "%s %s %s", cmd, version, name); + + if( strncmp(version, TUXANCI_NG_VERSION, strlen(TUXANCI_NG_VERSION)) != 0 ) + { + proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_VERSION); + client->status = NET_STATUS_ZOMBIE; + return; + } + + client->tux = newTux(); + client->tux->control = TUX_CONTROL_NET; + client->tux->client = client; + + addObjectToSpace(getCurrentArena()->spaceTux, client->tux); + + if( strlen(name) > STR_NAME_SIZE-1 ) + { + name[STR_NAME_SIZE-1] = '\0'; + } + + strcpy(client->tux->name, name); + sendInfoCreateClient(client); +} + +void proto_send_status_server(int type, client_t *client) +{ + char msg[STR_PROTO_SIZE]; + + sprintf(msg, "version: %s\n" + "clients: %d\n" + "maxclients: %d\n" + "uptime: %d\n" + "arena: %s\n", + TUXANCI_NG_VERSION, getCurrentArena()->spaceTux->list->count, + getServerMaxClients(), (unsigned int)getUpdateServer(), + getArenaNetName( getChoiceArenaId() ) ); + + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); + //proto_send(type, client, msg); +} + +void proto_recv_status_server(client_t *client, char *msg) +{ + proto_send_status_server(PROTO_SEND_ONE, client); +} + +#ifdef PUBLIC_SERVER + +void proto_send_listscore_server(int type, client_t *client, int max) +{ + char *str; + char *msg; + int i; + + msg = malloc( (max * 24) * sizeof(char) ); + strcpy(msg, ""); + + for( i = 0 ; i < max ; i++ ) + { + str = getTableItem(i); + + if( str == NULL ) + { + break; + } + + strcat(msg, str); + strcat(msg, "\n"); + } + + //proto_send(type, client, msg); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); + free(msg); +} + +void proto_recv_listscore_server(client_t *client, char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int max; + int ret; + + assert( msg != NULL ); + + ret = sscanf(msg, "%s %d", + cmd, &max); + + if( ret != 2 ) + { + max = HEIGHTSCORE_MAX_PLAYERS; + } + + proto_send_listscore_server(PROTO_SEND_ONE, client, max); +} + +#endif + +#ifndef PUBLIC_SERVER + +void proto_send_check_client(int id) +{ + char msg[STR_PROTO_SIZE]; + + sprintf(msg, "check %d\n", id); + sendServer(msg); +} + +#endif + +void proto_recv_check_server(client_t *client, char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int id; + + assert( msg != NULL ); + + sscanf(msg, "%s %d", + cmd, &id); + + delMsgInCheckFront(client->listSendMsg, id); +} + +void proto_send_init_server(int type, client_t *client, client_t *client2) +{ + char msg[STR_PROTO_SIZE]; + int n = WORLD_COUNT_ROUND_UNLIMITED; + + assert( client2 != NULL ); + +#ifndef PUBLIC_SERVER + getSettingCountRound(&n); +#endif + + sprintf(msg, "init %d %d %d %d %s\n", + client2->tux->id, client2->tux->x, client2->tux->y, + n, getArenaNetName( getChoiceArenaId() )); + + //proto_send(type, client, msg); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_init_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + char arena_name[STR_PROTO_SIZE]; + tux_t *tux; + int id; + int x, y, n; + + assert( msg != NULL ); + + sscanf(msg, "%s %d %d %d %d %s", + cmd, &id, &x, &y, &n, arena_name); + + setWorldArena( getArenaIdFormNetName(arena_name) ); + setMaxCountRound(n); + + tux = newTux(); + replaceTuxID(tux, id); + tux->x = x; + tux->y = y; + tux->control = TUX_CONTROL_KEYBOARD_RIGHT; + setControlTux(tux, TUX_CONTROL_KEYBOARD_RIGHT); + + getSettingNameRight(tux->name); + + addObjectToSpace(getCurrentArena()->spaceTux, tux); +} + +#endif + +void proto_send_event_server(int type, client_t *client, tux_t *tux, int action) +{ + char msg[STR_PROTO_SIZE]; + + sprintf(msg, "event %d %d\n", tux->id, action); + + //proto_send(type, client, msg); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_event_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int id, action; + tux_t *tux; + + assert( msg != NULL ); + + sscanf(msg, "%s %d %d", cmd, &id, &action); + + tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + + if( tux != NULL ) + { + actionTux(tux, action); + } +} + +#endif + +#ifndef PUBLIC_SERVER + +void proto_send_event_client(int action) +{ + char msg[STR_PROTO_SIZE]; + + sprintf(msg, "event %d\n", action); + + sendServer(msg); +} + +#endif + +/* +static void debug_interval() +{ + static my_time_t lastEvent = 0; + my_time_t curentTime; + + if( lastEvent == 0 ) + { + lastEvent = getMyTime(); + } + + curentTime = getMyTime(); + + printf("time = %d\n", curentTime - lastEvent); + + lastEvent = getMyTime(); +} +*/ + +void proto_recv_event_server(client_t *client, char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int action; + + assert( client != NULL ); + assert( msg != NULL ); + + //debug_interval(); + + sscanf(msg, "%s %d", cmd, &action); + + proto_send_event_server(PROTO_SEND_ALL_SEES_TUX, client, client->tux, action); + + refreshLastMove(client->protect); + actionTux(client->tux, action); +} + +void proto_send_newtux_server(int type, client_t *client, tux_t *tux) +{ + char msg[STR_PROTO_SIZE]; + int x, y; + + assert( tux != NULL ); + + if( tux->bonus == BONUS_HIDDEN ) + { + x = TUX_LOCATE_UNKNOWN; + y = TUX_LOCATE_UNKNOWN; + } + else + { + x = tux->x; + y = tux->y; + } + + getTuxProportion(tux, &x, &y, NULL, NULL); + + sprintf(msg, "newtux %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d\n", + tux->id ,x, y, tux->status, tux->position ,tux->frame, tux->score, tux->name, + tux->gun, tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE], + tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY], tux->shot[GUN_LASSER], + tux->shot[GUN_MINE], tux->shot[GUN_BOMBBALL], + tux->bonus_time, tux->pickup_time); + + //proto_send(type, client, msg); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_newtux_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + char name[STR_NAME_SIZE]; + int id, x, y, status, position, frame, score; + int myGun, myBonus; + int gun1, gun2, gun3, gun4, gun5, gun6, gun7; + int time1, time2; + tux_t *tux; + + assert( msg != NULL ); + + if( getCurrentArena() == NULL ) + { + return; + } + + sscanf(msg, "%s %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d", + cmd, &id, &x, &y, &status, &position, &frame, &score, name, + &myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, &gun7, &time1, &time2); + + tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + + if( tux != NULL ) + { + assert( tux->id == id ); + } + + if( tux == NULL ) + { + char term_msg[STR_SIZE]; + sprintf(term_msg, "connect new client( id = %d)\n", id); + appendTextInTerm(term_msg); + + tux = newTux(); + replaceTuxID(tux, id); + tux->control = TUX_CONTROL_NET; + addObjectToSpace(getCurrentArena()->spaceTux, tux); + } + + moveObjectInSpace(getCurrentArena()->spaceTux, tux, x, y); + tux->status = status; + tux->position = position; + tux->frame = frame; + tux->score = score; + strcpy(tux->name, name); + tux->gun = myGun; + tux->bonus = myBonus; + tux->shot[GUN_SIMPLE] = gun1; + tux->shot[GUN_DUAL_SIMPLE] = gun2; + tux->shot[GUN_SCATTER] = gun3; + tux->shot[GUN_TOMMY] = gun4; + tux->shot[GUN_LASSER] = gun5; + tux->shot[GUN_MINE] = gun6; + tux->shot[GUN_BOMBBALL] = gun7; + tux->bonus_time = time1; + tux->pickup_time = time2; +} + +#endif + +void proto_send_kill_server(int type, client_t *client, tux_t *tux) +{ + char msg[STR_PROTO_SIZE]; + + assert( tux != NULL ); + + sprintf(msg, "kill %d\n", tux->id); + + //proto_send(type, client, msg); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_kill_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int id; + tux_t *tux; + + assert( msg != NULL ); + + sscanf(msg, "%s %d", cmd, &id); + + tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + + if( tux != NULL ) + { + eventTuxIsDead(tux); + } +} + +#endif + +void proto_send_del_server(int type, client_t *client, int id) +{ + char msg[STR_PROTO_SIZE]; + int check_id; + + check_id = getNewID(); + + sprintf(msg, "del %d %d\n", + id, check_id); + + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_CHECK, check_id); + //proto_check(type, client, msg, check_id); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_del_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int id, check_id; + tux_t *tux; + shot_t *shot; + item_t *item; + + assert( msg != NULL ); + + sscanf(msg, "%s %d %d", + cmd, &id, &check_id); + + proto_send_check_client(check_id); + + tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + + if( tux != NULL ) + { + char term_msg[STR_SIZE]; + + //printf("delete tux..\n"); + sprintf(term_msg, "tux with id %d is disconnect\n", tux->id); + appendTextInTerm(term_msg); + + delObjectFromSpaceWithObject(getCurrentArena()->spaceTux, tux, destroyTux); + return; + } + + shot = getObjectFromSpaceWithID(getCurrentArena()->spaceShot, id); + + if( shot != NULL ) + { + //printf("delete shot..\n"); + delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); + return; + } + + item = getObjectFromSpaceWithID(getCurrentArena()->spaceItem, id); + + if( item != NULL ) + { + //printf("delete item..\n"); + delObjectFromSpaceWithObject(getCurrentArena()->spaceItem, item, destroyItem); + return; + } +} + +#endif + +void proto_send_additem_server(int type, client_t *client, item_t *p) +{ + char msg[STR_PROTO_SIZE]; + int check_id; + + assert( p != NULL ); + + check_id = getNewID(); + + sprintf(msg, "additem %d %d %d %d %d %d %d %d\n", + p->id, p->type, p->x, p->y, p->count, p->frame, p->author_id, check_id); + + //proto_send(type, client, msg); + //proto_check(type, client, msg, check_id); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_CHECK, check_id); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_additem_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int id, type, x, y, count, frame, author_id, check_id; + item_t *item; + + assert( msg != NULL ); + + if( getCurrentArena() == NULL ) + { + return; + } + + sscanf(msg, "%s %d %d %d %d %d %d %d %d", + cmd, &id, &type, &x, &y, &count, &frame, &author_id, &check_id); + + proto_send_check_client(check_id); + + if( ( item = getObjectFromSpaceWithID(getCurrentArena()->spaceItem, id) ) != NULL ) + { + return; + } + + item = newItem(x, y, type, author_id); + + replaceItemID(item, id); + item->count = count; + item->frame = frame; + + addObjectToSpace(getCurrentArena()->spaceItem, item); +} + +#endif + +void proto_send_shot_server(int type, client_t *client, shot_t *p) +{ + char msg[STR_PROTO_SIZE]; + + assert( p != NULL ); + + sprintf(msg, "shot %d %d %d %d %d %d %d %d %d\n", + p->id, p->x, p->y, p->px, p->py, p->position, p->gun, p->author_id, p->isCanKillAuthor); + + //proto_send(type, client, msg); + //proto_check(type, client, msg, check_id); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_shot_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int x, y, px, py, position, gun, shot_id, author_id, isCanKillAuthor; + shot_t *shot; + + assert( msg != NULL ); + + sscanf(msg, "%s %d %d %d %d %d %d %d %d %d", + cmd, &shot_id, &x, &y, &px, &py, &position, &gun, &author_id, &isCanKillAuthor); + + + if( ( shot = getObjectFromSpaceWithID(getCurrentArena()->spaceShot, shot_id) ) != NULL ) + { + delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); + + //return; + } + + shot = newShot(x, y, px, py, gun, author_id); + + replaceShotID(shot, shot_id); + shot->isCanKillAuthor = isCanKillAuthor; + shot->position = position; + + if( shot->gun == GUN_LASSER ) + { + transformOnlyLasser(shot); + } + + addObjectToSpace(getCurrentArena()->spaceShot, shot); +} + +#endif + +#ifndef PUBLIC_SERVER + +void proto_send_ping_client() +{ + char msg[STR_PROTO_SIZE]; + sprintf(msg, "ping\n"); + sendServer(msg); +} + +#endif + +void proto_recv_ping_server(client_t *client, char *msg) +{ +} + +void proto_send_ping_server(int type, client_t *client) +{ + char msg[STR_PROTO_SIZE]; + sprintf(msg, "ping\n"); + //proto_send(type, client, msg); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_ping_client(char *msg) +{ +} + +#endif + +void proto_send_end_server(int type, client_t *client) +{ + char msg[STR_PROTO_SIZE]; + strcpy(msg, "end\n"); + //proto_send(type, client, msg); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); +} + +#ifndef PUBLIC_SERVER + +void proto_recv_end_client(char *msg) +{ + assert( msg != NULL ); + + setWorldEnd(); +} + +#endif + +#ifndef PUBLIC_SERVER + +void proto_send_end_client() +{ + char msg[STR_PROTO_SIZE]; + strcpy(msg, "end\n"); + sendServer(msg); +} + +#endif + +void proto_recv_end_server(client_t *client, char *msg) +{ + assert( msg != NULL ); + assert( client != NULL ); + + client->status = NET_STATUS_ZOMBIE; +} + diff --git a/src/base/proto.h b/src/base/proto.h new file mode 100644 index 0000000..9fe9303 --- /dev/null +++ b/src/base/proto.h @@ -0,0 +1,81 @@ + +#ifndef MY_PROTO + +#define MY_PROTO + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/item.h" +#include "base/shot.h" +#include "base/arenaFile.h" +#include "base/myTimer.h" +#include "base/server.h" +#include "base/proto.h" +#include "base/net_multiplayer.h" + +#ifndef PUBLIC_SERVER +#include "screen/screen_world.h" +#include "screen/screen_setting.h" +#include "screen/screen_choiceArena.h" +#include "client/client.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +#define PROTO_SEND_ONE 0 +#define PROTO_SEND_ALL 1 +#define PROTO_SEND_BUT 3 +#define PROTO_SEND_ALL_SEES_TUX 4 + +#define PROTO_ERROR_CODE_UNKNOWN 0 +#define PROTO_ERROR_CODE_BAD_VERSION 1 +#define PROTO_ERROR_CODE_BAD_NAME 2 +#define PROTO_ERROR_CODE_BAD_COMMAND 3 +#define PROTO_ERROR_CODE_TIMEOUT 4 + +extern void proto_send_error_server(int type, client_t *client, int errorcode); +extern void proto_recv_error_client(char *msg); +extern void proto_send_hello_client(char *name); +extern void proto_recv_hello_server(client_t *client, char *msg); +extern void proto_send_status_server(int type, client_t *client); +extern void proto_recv_status_server(client_t *client, char *msg); +extern void proto_send_listscore_server(int type, client_t *client, int max); +extern void proto_recv_listscore_server(client_t *client, char *msg); +extern void proto_send_check_client(int id); +extern void proto_recv_check_server(client_t *client, char *msg); +extern void proto_send_init_server(int type, client_t *client, client_t *client2); +extern void proto_recv_init_client(char *msg); +extern void proto_send_event_server(int type, client_t *client, tux_t *tux, int action); +extern void proto_recv_event_client(char *msg); +extern void proto_send_event_client(int action); +extern void proto_recv_event_server(client_t *client, char *msg); +extern void proto_send_newtux_server(int type, client_t *client, tux_t *tux); +extern void proto_recv_newtux_client(char *msg); +extern void proto_send_kill_server(int type, client_t *client, tux_t *tux); +extern void proto_recv_kill_client(char *msg); +extern void proto_send_score_server(int type, client_t *client, tux_t *tux); +extern void proto_recv_score_client(char *msg); +extern void proto_send_del_server(int type, client_t *client, int id); +extern void proto_recv_del_client(char *msg); +extern void proto_send_additem_server(int type, client_t *client, item_t *p); +extern void proto_recv_additem_client(char *msg); +extern void proto_send_shot_server(int type, client_t *client, shot_t *p); +extern void proto_recv_shot_client(char *msg); +extern void proto_send_ping_client(); +extern void proto_recv_ping_server(client_t *client, char *msg); +extern void proto_send_ping_server(int type, client_t *client); +extern void proto_recv_ping_client(char *msg); +extern void proto_send_end_server(int type, client_t *client); +extern void proto_recv_end_client(char *msg); +extern void proto_send_end_client(); +extern void proto_recv_end_server(client_t *client, char *msg); + +#endif + diff --git a/src/base/server.c b/src/base/server.c new file mode 100644 index 0000000..ae9f170 --- /dev/null +++ b/src/base/server.c @@ -0,0 +1,984 @@ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/proto.h" +#include "base/server.h" +#include "base/myTimer.h" +#include "base/arena.h" +#include "base/net_multiplayer.h" +#include "base/checkFront.h" +#include "base/protect.h" + +#ifndef PUBLIC_SERVER +#include "screen/screen_world.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#include "server/heightScore.h" +#endif + +#ifdef SUPPORT_NET_UNIX_UDP +#include "net_unix/udp.h" +static sock_udp_t *sock_server_udp; +static sock_udp_t *sock_server_udp_second; +#endif + +#ifdef SUPPORT_NET_SDL_UDP +#include "net_sdl/sdl_udp.h" +static sock_sdl_udp_t *sock_server_sdl_udp; +#endif + +static list_t *listClient; +static list_t *listServerTimer; +static time_t timeUpdate; +static int maxClients; + +static void startUpdateServer() +{ + timeUpdate = time(NULL); +} + +time_t getUpdateServer() +{ + return time(NULL) - timeUpdate; +} + +static void delZombieCLient(void *p_nothink) +{ + client_t *thisClient; + my_time_t currentTime; + int i; + + currentTime = getMyTime(); + + for( i = 0 ; i < listClient->count; i++) + { + thisClient = (client_t *) listClient->list[i]; + + if( isDown(thisClient->protect) == TRUE ) + { + //proto_send_error_server(PROTO_SEND_ONE, thisClient, PROTO_ERROR_CODE_TIMEOUT); + proto_send_end_server(PROTO_SEND_ONE, thisClient); + eventMsgInCheckFront(thisClient); + thisClient->status = NET_STATUS_ZOMBIE; + } + + if( thisClient->status == NET_STATUS_ZOMBIE ) + { + if( thisClient->tux != NULL ) + { + proto_send_del_server(PROTO_SEND_ALL, NULL, thisClient->tux->id); + } + + delListItem(listClient, i, destroyClient); + } + } + +} + +static void eventPeriodicSyncClient(void *p_nothink) +{ + client_t *thisClientInfo; + client_t *thisClientSend; + tux_t *thisTux; + int i, j; + + for( i = 0 ; i < listClient->count; i++) + { + thisClientSend = (client_t *) listClient->list[i]; + + if( thisClientSend->tux == NULL ) + { + continue; + } + +#ifndef PUBLIC_SERVER + thisTux = getControlTux(TUX_CONTROL_KEYBOARD_RIGHT); + if( isTuxSeesTux(thisClientSend->tux, thisTux) ) + proto_send_newtux_server(PROTO_SEND_ONE, thisClientSend, thisTux); +#endif + + for( j = 0 ; j < listClient->count; j++) + { + thisClientInfo = (client_t *) listClient->list[j]; + thisTux = thisClientInfo->tux; + + if( thisTux != NULL && + thisClientSend != thisClientInfo && + isTuxSeesTux(thisClientSend->tux, thisTux) ) + { + proto_send_newtux_server(PROTO_SEND_ONE, + thisClientSend, thisTux); + } + } + } +} + +static void eventSendPingClients(void *p_nothink) +{ + proto_send_ping_server(PROTO_SEND_ALL, NULL); +} + +static void setServerTimer() +{ + if( listServerTimer != NULL ) + { + destroyTimer(listServerTimer); + } + + restartTimer(); + listServerTimer = newTimer(); + + addTaskToTimer(listServerTimer, TIMER_PERIODIC, delZombieCLient, NULL, SERVER_TIMEOUT); + addTaskToTimer(listServerTimer, TIMER_PERIODIC, eventPeriodicSyncClient, NULL, SERVER_TIME_SYNC); + addTaskToTimer(listServerTimer, TIMER_PERIODIC, eventSendPingClients, NULL, SERVER_TIME_PING); +} + +void static initServer() +{ + startUpdateServer(); + listServerTimer = NULL; + listClient = newList(); + setServerMaxClients(SERVER_MAX_CLIENTS); + setServerTimer(); +} + +#ifdef SUPPORT_NET_UNIX_UDP +int initUdpServer(char *ip, int port, int proto) +{ + sock_server_udp = bindUdpSocket(ip, port, proto); + sock_server_udp_second = NULL; + + if( sock_server_udp == NULL ) + { + return -1; + } + + initServer(); + + printf("server listen UDP port %s %d\n", ip, port); + + return 0; +} +#endif + +#ifdef PUBLIC_SERVER + +int initUdpPublicServer(char *ip4, int port4, char *ip6, int port6) +{ + sock_server_udp = NULL; + sock_server_udp_second = NULL; + + if( ip4 != NULL ) + { + sock_server_udp = bindUdpSocket(ip4, port4, PROTO_UDPv4); + + if( sock_server_udp == NULL ) + { + return -1; + } + + printf("server listen UDP port %s %d\n", ip4, port4); + } + + if( ip6 != NULL ) + { + sock_server_udp_second = bindUdpSocket(ip6, port6, PROTO_UDPv6); + + if( sock_server_udp_second == NULL ) + { + return -1; + } + + printf("server listen UDP port %s %d\n", ip6, port6); + } + + initServer(); + + return 0; +} + +#endif + +#ifdef SUPPORT_NET_SDL_UDP +int initSdlUdpServer(int port) +{ + sock_server_sdl_udp = bindSdlUdpSocket(port); + + if( sock_server_sdl_udp == NULL ) + { + return -1; + } + + initServer(); + + printf("server listen UDP port %d\n", port); + + return 0; +} +#endif + +static client_t* newAnyClient() +{ + client_t *new; + + new = malloc( sizeof(client_t) ); + memset(new, 0, sizeof(client_t)); + + new->status = NET_STATUS_OK; + new->listRecvMsg = newList(); + new->listSendMsg = newCheckFront(); + new->protect = newProtect(); + + return new; +} + +#ifdef SUPPORT_NET_UNIX_UDP +client_t* newUdpClient(sock_udp_t *sock_udp) +{ + client_t *new; + char str_ip[STR_IP_SIZE]; + + assert( sock_udp != NULL ); + + getSockUdpIp(sock_udp, str_ip, STR_IP_SIZE); + printf("new client from %s %d\n", str_ip, getSockUdpPort(sock_udp)); + + new = newAnyClient(); + new->socket_udp = sock_udp; + + return new; +} +#endif + +#ifdef SUPPORT_NET_SDL_UDP +client_t* newSdlUdpClient(sock_sdl_udp_t *sock_sdl_udp) +{ + client_t *new; + + assert( sock_sdl_udp != NULL ); + + printf("new client from %d\n", getSockSdlUdpPort(sock_sdl_udp)); + new = newAnyClient(); + new->socket_sdl_udp = sock_sdl_udp; + + return new; +} +#endif + +void destroyClient(client_t *p) +{ + char str_ip[STR_IP_SIZE]; + + assert( p != NULL ); + + eventMsgInCheckFront(p); + +#ifdef SUPPORT_NET_UNIX_UDP + getSockUdpIp(p->socket_udp, str_ip, STR_IP_SIZE); + printf("close connect %s %d\n", str_ip, getSockUdpPort(p->socket_udp) ); +#endif + +#ifdef SUPPORT_NET_UNIX_UDP + closeUdpSocket(p->socket_udp); +#endif + +#ifdef SUPPORT_NET_SDL_UDP + closeSdlUdpSocket(p->socket_sdl_udp); +#endif + + destroyListItem(p->listRecvMsg, free); + destroyCheckFront(p->listSendMsg); + + destroyProtect(p->protect); + + if( p->tux != NULL ) + { +#ifdef PUBLIC_SERVER + addPlayerInHighScore(p->tux->name, p->tux->score); +#endif + delObjectFromSpaceWithObject(getCurrentArena()->spaceTux, p->tux, destroyTux); + } + + free(p); +} + +list_t* getListServerClient() +{ + return listClient; +} + +int getServerMaxClients() +{ + return maxClients; +} + +void setServerMaxClients(int n) +{ + maxClients = n; +} + +void sendClient(client_t *p, char *msg) +{ + assert( p != NULL ); + assert( msg != NULL ); + + if( p->status != NET_STATUS_ZOMBIE ) + { + int ret; + +#ifndef PUBLIC_SERVER + if( isParamFlag("--send") ) + { + printf("send -> %s", msg); + } +#endif + +#ifdef SUPPORT_NET_UNIX_UDP + if( p->socket_udp->proto == PROTO_UDPv4 && + sock_server_udp != NULL && + sock_server_udp->proto == PROTO_UDPv4 ) + { + ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg)); + } + + if( p->socket_udp->proto == PROTO_UDPv6 && + sock_server_udp != NULL && + sock_server_udp->proto == PROTO_UDPv6 ) + { + ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg)); + } + + if( p->socket_udp->proto == PROTO_UDPv6 && + sock_server_udp_second != NULL && + sock_server_udp_second->proto == PROTO_UDPv6 ) + { + ret = writeUdpSocket(sock_server_udp_second, p->socket_udp, msg, strlen(msg)); + } +#endif + +#ifdef SUPPORT_NET_SDL_UDP + ret = writeSdlUdpSocket(sock_server_sdl_udp, p->socket_sdl_udp, msg, strlen(msg)); +#endif + } +} + +static void addMsgClient(client_t *p, char *msg, int type, int id) +{ + assert( p != NULL ); + assert( msg != NULL ); + + if( p->status != NET_STATUS_ZOMBIE ) + { + addMsgInCheckFront(p->listSendMsg, msg, type, id); + } +} + +static void addMsgAllClientBut(char *msg, client_t *p, int type, int id) +{ + client_t *thisClient; + int i; + + assert( msg != NULL ); + + for( i = 0 ; i < listClient->count; i++) + { + thisClient = (client_t *) listClient->list[i]; + + if( thisClient->tux != NULL && thisClient != p ) + { + addMsgClient(thisClient, msg, type, id); + } + } +} + +static void addMsgAllClient(char *msg, int type, int id) +{ + assert( msg != NULL ); + + addMsgAllClientBut(msg, NULL, type, id); +} + +static void addMsgAllClientSeesTux(char *msg, tux_t *tux, int type, int id) +{ + int i; + + assert( msg != NULL ); + + for( i = 0 ; i < listClient->count ; i++ ) + { + client_t *thisClient; + tux_t *thisTux; + + thisClient = (client_t *)listClient->list[i]; + thisTux = (tux_t *)thisClient->tux; + + if( thisTux == NULL ) + { + continue; + } + + if( tux != thisTux && + isTuxSeesTux(thisTux, tux) ) + { + addMsgClient(thisClient, msg, type, id); + } + } +} + +void protoSendClient(int type, client_t *client, char *msg, int type2, int id) +{ + assert( msg != NULL ); + + switch( type ) + { + case PROTO_SEND_ONE : + assert( client != NULL ); + addMsgClient(client, msg, type2, id); + break; + case PROTO_SEND_ALL : + assert( client == NULL ); + addMsgAllClient(msg, type2, id); + break; + case PROTO_SEND_BUT : + assert( client != NULL ); + addMsgAllClientBut(msg, client, type2, id); + break; + case PROTO_SEND_ALL_SEES_TUX : +#ifndef PUBLIC_SERVER + if( client != NULL ) + { + addMsgAllClientSeesTux(msg, client->tux, type2, id); + } + else + { + addMsgAllClientSeesTux(msg, getControlTux(TUX_CONTROL_KEYBOARD_RIGHT), type2, id); + } +#endif +#ifdef PUBLIC_SERVER + addMsgAllClientSeesTux(msg, client->tux, type2, id); +#endif + break; + default : + assert( ! "Premenna type ma zlu hodnotu !" ); + break; + } +} + +tux_t *getServerTux() +{ + return NULL; +} + +client_t *getClientFromTux(tux_t *tux) +{ + client_t *thisClient; + int i; + + for( i = 0 ; i < listClient->count; i++) + { + thisClient = (client_t *) listClient->list[i]; + + if( thisClient->tux == tux ) + { + return thisClient; + } + } + + return NULL; +} + +void sendInfoCreateClient(client_t *client) +{ + client_t *thisClient; + tux_t *thisTux; + item_t *thisItem; + int i; + + assert( client != NULL ); + + proto_send_init_server(PROTO_SEND_ONE, client, client); + +#ifndef PUBLIC_SERVER + proto_send_newtux_server(PROTO_SEND_ONE, client, getControlTux(TUX_CONTROL_KEYBOARD_RIGHT) ); +#endif + + for( i = 0 ; i < listClient->count; i++) + { + thisClient = (client_t *) listClient->list[i]; + thisTux = thisClient->tux; + + if( thisTux != NULL && thisTux != client->tux ) + { + proto_send_newtux_server(PROTO_SEND_ONE, thisClient, client->tux); + proto_send_newtux_server(PROTO_SEND_ONE, client, thisTux); + } + } + + for( i = 0 ; i < getCurrentArena()->spaceItem->list->count; i++) + { + thisItem = (item_t *) getCurrentArena()->spaceItem->list->list[i]; + proto_send_additem_server(PROTO_SEND_ONE, client, thisItem); + } +} + +#ifdef SUPPORT_NET_UNIX_UDP + +static void eventCreateNewUdpClient(sock_udp_t *socket_udp) +{ + client_t *client; + + assert( socket_udp != NULL ); + + client = newUdpClient( socket_udp ); + addList(listClient, client); +} + +#endif + +#ifdef SUPPORT_NET_SDL_UDP + +static void eventCreateNewSdlUdpClient(sock_sdl_udp_t *socket_sdl_udp) +{ + client_t *client; + + assert( socket_sdl_udp != NULL ); + + client = newSdlUdpClient( socket_sdl_udp ); + addList(listClient, client); +} + +#endif + +static void eventClientBuffer(client_t *client) +{ + char *line; + int i; + + assert( client != NULL ); + + /* obsluha udalosti od clientov */ + + //while( getBufferLine(client->listRecvMsg, line, STR_PROTO_SIZE) >= 0 ) + for( i = 0 ; i < client->listRecvMsg->count ; i++ ) + { + line = (char *)client->listRecvMsg->list[i]; + +#ifndef PUBLIC_SERVER + if( isParamFlag("--recv") ) + { + printf("recv -> %s", line); + } +#endif + + rereshLastPing(client->protect); + + if( strncmp(line, "hello", 5) == 0 ) + { + proto_recv_hello_server(client, line); + continue; + } + + if( strncmp(line, "status", 6) == 0 ) + { + proto_recv_status_server(client, line); + continue; + } + +#ifdef PUBLIC_SERVER + if( strncmp(line, "list", 4) == 0 ) + { + proto_recv_listscore_server(client, line); + continue; + } +#endif + + if( client->tux != NULL ) + { + if( strncmp(line, "event", 5) == 0 ) + { + proto_recv_event_server(client, line); + continue; + } + + if( strncmp(line, "check", 5) == 0 ) + { + proto_recv_check_server(client, line); + continue; + } + + if( strncmp(line, "ping", 4) == 0 ) + { + proto_recv_ping_server(client, line); + continue; + } + + if( strncmp(line, "end", 3) == 0 ) + { + proto_recv_end_server(client, line); + continue; + } + } + + if( client->tux != NULL ) + { + proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_COMMAND); + } + } + + destroyListItem(client->listRecvMsg, free); + client->listRecvMsg = newList(); +} + +void eventClientListBuffer() +{ + int i; + client_t *thisClient; + + for( i = 0 ; i < listClient->count; i++) + { + thisClient = (client_t *) listClient->list[i]; + eventClientBuffer(thisClient); + eventMsgInCheckFront(thisClient); + } +} + +#ifdef SUPPORT_NET_UNIX_UDP + +static client_t* findUdpClient(sock_udp_t *sock_udp) +{ + int i; + client_t *thisClient; + + for( i = 0 ; i < listClient->count; i++) + { + thisClient = (client_t *) listClient->list[i]; + + if( getSockUdpPort(thisClient->socket_udp) == getSockUdpPort(sock_udp) ) + { + return thisClient; + } + } + + return NULL; +} + +static void eventClientUdpSelect(sock_udp_t *sock_server) +{ + sock_udp_t *sock_client; + client_t *client; + char listRecvMsg[STR_SIZE]; + bool_t isCreateNewClient; + int ret; + + assert( sock_server != NULL ); + + sock_client = newSockUdp(sock_server->proto); + isCreateNewClient = FALSE; + + memset(listRecvMsg, 0, STR_SIZE); + + ret = readUdpSocket(sock_server, sock_client, listRecvMsg, STR_SIZE-1); + + client = findUdpClient(sock_client); + + if( client == NULL ) + { + if( getCurrentArena()->spaceTux->list->count+1 > maxClients ) + { + destroySockUdp(sock_client); + return; + } + + eventCreateNewUdpClient(sock_client); + client = findUdpClient(sock_client); + isCreateNewClient = TRUE; + } + + if( client == NULL ) + { + fprintf(stderr, "Client total not found !\n"); + return; + } + + if( isCreateNewClient == FALSE ) + { + destroySockUdp(sock_client); + } + + if( ret <= 0 ) + { + client->status = NET_STATUS_ZOMBIE; + return; + } + + //printf("add packet >>%s<<\n", listRecvMsg); + addList(client->listRecvMsg, strdup(listRecvMsg) ); +} + +int selectServerUdpSocket() +{ + struct timeval tv; + fd_set readfds; + fd_set errorfds; + int max_fd; + int ret; + int count; + +#ifndef PUBLIC_SERVER + tv.tv_sec = 0; + tv.tv_usec = 0; +#endif + +#ifdef PUBLIC_SERVER + tv.tv_sec = 0; + tv.tv_usec = 5000; +#endif + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + + max_fd = 0; + count = 0; + + if( sock_server_udp != NULL ) + { + FD_SET(sock_server_udp->sock, &errorfds); + FD_SET(sock_server_udp->sock, &readfds); + + if( sock_server_udp->sock > max_fd ) + { + max_fd = sock_server_udp->sock; + } + } + + if( sock_server_udp_second != NULL ) + { + FD_SET(sock_server_udp_second->sock, &readfds); + FD_SET(sock_server_udp_second->sock, &errorfds); + + if( sock_server_udp_second->sock > max_fd ) + { + max_fd = sock_server_udp_second->sock; + } + } + + //printf("select..\n"); + +#ifdef PUBLIC_SERVER + if( listClient->count == 0 ) + { + ret = select(max_fd+1, &readfds, (fd_set *)NULL, &errorfds, NULL); + setServerTimer(); + } + else + { + ret = select(max_fd+1, &readfds, (fd_set *)NULL,&errorfds, &tv); + } +#endif + +#ifndef PUBLIC_SERVER + ret = select(max_fd+1, &readfds, (fd_set *)NULL,&errorfds, &tv); +#endif + + if( ret < 0 ) + { + //printf("select ret = %d\n", ret); + return 0; + } + + if( sock_server_udp != NULL ) + { + if( FD_ISSET(sock_server_udp->sock, &readfds) ) + { + eventClientUdpSelect(sock_server_udp); + count = 1; + } + + if( FD_ISSET(sock_server_udp->sock, &errorfds) ) + { + printf("error\n"); + } + } + + if( sock_server_udp_second != NULL ) + { + if( FD_ISSET(sock_server_udp_second->sock, &readfds) ) + { + eventClientUdpSelect(sock_server_udp_second); + count = 1; + } + + if( FD_ISSET(sock_server_udp_second->sock, &errorfds) ) + { + printf("error\n"); + } + } + + return count; +} + +#endif + +#ifdef SUPPORT_NET_SDL_UDP + +static client_t* findSdlUdpClient(sock_sdl_udp_t *sock_sdl_udp) +{ + int i; + client_t *thisClient; + + for( i = 0 ; i < listClient->count; i++) + { + thisClient = (client_t *) listClient->list[i]; + + if( getSockSdlUdpPort(thisClient->socket_sdl_udp) == getSockSdlUdpPort(sock_sdl_udp) ) + { + return thisClient; + } + } + + return NULL; +} + +void selectServerSdlUdpSocket() +{ + sock_sdl_udp_t *sock_client; + client_t *client; + char listRecvMsg[STR_PROTO_SIZE]; + bool_t isCreateNewClient; + int ret; + + assert( sock_server_sdl_udp != NULL ); + + do{ + sock_client = newSdlSockUdp(); + isCreateNewClient = FALSE; + + memset(listRecvMsg, 0, STR_PROTO_SIZE); + + ret = readSdlUdpSocket(sock_server_sdl_udp, sock_client, listRecvMsg, STR_PROTO_SIZE-1); + + if( ret < 0 ) + { + destroySockSdlUdp(sock_client); + break; + } + + client = findSdlUdpClient(sock_client); + + if( client == NULL ) + { + eventCreateNewSdlUdpClient(sock_client); + client = findSdlUdpClient(sock_client); + isCreateNewClient = TRUE; + } + + if( client == NULL ) + { + fprintf(stderr, "Client total not found !\n"); + return; + } + + if( isCreateNewClient == FALSE ) + { + destroySockSdlUdp(sock_client); + } + + if( ret <= 0 ) + { + client->status = NET_STATUS_ZOMBIE; + return; + } + + addList(client->listRecvMsg, strdup(listRecvMsg) ); + }while( ret > 0 ); +} + +#endif + +void eventServer() +{ +#ifndef PUBLIC_SERVER + + int count; + +#ifdef SUPPORT_NET_SDL_UDP + selectServerSdlUdpSocket(); +#endif + +#ifdef SUPPORT_NET_UNIX_UDP + do{ + count = selectServerUdpSocket(); + }while( count > 0 ); +#endif + +#endif + +#ifdef PUBLIC_SERVER + selectServerUdpSocket(); +#endif + + eventClientListBuffer(); + eventTimer(listServerTimer); +} + +static void quitServer() +{ + proto_send_end_server(PROTO_SEND_ALL, NULL); + + assert( listClient != NULL ); + destroyListItem(listClient, destroyClient); + destroyTimer(listServerTimer); +} + +#ifdef SUPPORT_NET_UNIX_UDP + +void quitUdpServer() +{ + quitServer(); + + if( sock_server_udp != NULL ) + { + printf("close port %d\n", getSockUdpPort(sock_server_udp)); + closeUdpSocket(sock_server_udp); + } + + if( sock_server_udp_second != NULL ) + { + printf("close port %d\n", getSockUdpPort(sock_server_udp_second)); + closeUdpSocket(sock_server_udp_second); + } + + printf("quit UDP port\n"); +} + +#endif + +#ifdef SUPPORT_NET_SDL_UDP + +void quitSdlUdpServer() +{ + quitServer(); + + assert( sock_server_sdl_udp != NULL ); + closeSdlUdpSocket(sock_server_sdl_udp); + + printf("quit UDP port\n"); +} + +#endif diff --git a/src/base/server.h b/src/base/server.h new file mode 100644 index 0000000..4a946ea --- /dev/null +++ b/src/base/server.h @@ -0,0 +1,93 @@ + +#ifndef MY_SERVER + +#define MY_SERVER + +#include + +#include "base/list.h" +#include "base/tux.h" +#include "base/myTimer.h" +#include "base/protect.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#endif + + +#ifdef SUPPORT_NET_SDL_UDP + +#include "net_sdl/sdl_udp.h" + +#endif + +#ifdef SUPPORT_NET_UNIX_UDP + +#include "net_unix/udp.h" + +#endif + +#define SERVER_TIMEOUT 1000 +#define SERVER_TIME_SYNC 1000 +#define SERVER_TIME_PING 1000 +#define SERVER_MAX_CLIENTS 100 + + +#define SERVER_INDEX_ROOT_TUX 0 + +typedef struct client_struct +{ +#ifdef SUPPORT_NET_UNIX_UDP + sock_udp_t *socket_udp; +#endif + +#ifdef SUPPORT_NET_SDL_UDP + sock_sdl_udp_t *socket_sdl_udp; +#endif + int status; + + tux_t *tux; + + protect_t *protect; + + list_t *listSendMsg; + list_t *listRecvMsg; +} client_t; + +#ifdef PUBLIC_SERVER +extern int initUdpPublicServer(char *ip4, int port4, char *ip6, int port6); +#endif + +#ifdef SUPPORT_NET_UNIX_UDP +extern int initUdpServer(char *ip, int port, int proto); +extern int initUdpServerMultiProto(char *ip4, char *ip6, int port4, int port6); +extern client_t* newUdpClient(sock_udp_t *sock_udp); +extern int selectServerUdpSocket(); +extern void quitUdpServer(); +#endif + +#ifdef SUPPORT_NET_SDL_UDP +extern int initSdlUdpServer(int port); +extern client_t* newSdlUdpClient(sock_sdl_udp_t *sock_udp); +extern void selectServerSdlUdpSocket(); +extern void quitSdlUdpServer(); +#endif + +extern void destroyClient(client_t *p); + +extern time_t getUpdateServer(); +extern list_t* getListServerClient(); +extern int getServerMaxClients(); +extern void setServerMaxClients(int n); + +extern void sendClient(client_t *p, char *msg); +extern void protoSendClient(int type, client_t *client, char *msg, int type2, int id); + +extern tux_t *getServerTux(); +extern client_t *getClientFromTux(tux_t *tux); +extern void eventClientListBuffer(); +extern void sendInfoCreateClient(client_t *client); +extern void eventServer(); + +#endif + diff --git a/src/base/shot.c b/src/base/shot.c new file mode 100644 index 0000000..e8cdff9 --- /dev/null +++ b/src/base/shot.c @@ -0,0 +1,369 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/shot.h" +#include "base/gun.h" +#include "base/net_multiplayer.h" +#include "base/proto.h" +#include "base/idManager.h" + +#ifndef PUBLIC_SERVER +#include "client/image.h" +#include "client/layer.h" +#include "screen/screen_world.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +#ifndef PUBLIC_SERVER + +static SDL_Surface *g_shot_simple; +static SDL_Surface *g_shot_lasserX; +static SDL_Surface *g_shot_lasserY; +static SDL_Surface *g_shot_bombball; + +#endif + +static bool_t isShotInit = FALSE; + +bool_t isShotInicialized() +{ + return isShotInit; +} + +void initShot() +{ +#ifndef PUBLIC_SERVER + assert( isImageInicialized() == TRUE ); +#endif + +#ifndef PUBLIC_SERVER + + g_shot_simple = addImageData("shot.png", IMAGE_ALPHA, "shot", IMAGE_GROUP_BASE); + g_shot_lasserX = addImageData("lasserX.png", IMAGE_NO_ALPHA, "lasserX", IMAGE_GROUP_BASE); + g_shot_lasserY = addImageData("lasserY.png", IMAGE_NO_ALPHA, "lasserY", IMAGE_GROUP_BASE); + g_shot_bombball = addImageData("bombball.png", IMAGE_ALPHA, "bombball", IMAGE_GROUP_BASE); + +#endif + + isShotInit = TRUE; +} + +shot_t* newShot(int x,int y, int px, int py, int gun, int author_id) +{ + shot_t *new; + tux_t *author; + + new = malloc( sizeof(shot_t) ); + memset(new, 0, sizeof(shot_t) ); + + new->id = getNewID(); + new->x = x; + new->y = y; + new->px = px; + new->py = py; + new->gun = gun; + new->author_id = author_id; + + new->position = POSITION_UNKNOWN; + + author = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, author_id); + + if( author != NULL ) + { + new->position = author->position; + } + + new->isCanKillAuthor = FALSE; + + switch( gun ) + { + case GUN_SIMPLE : + new->w = GUN_SHOT_WIDTH; + new->h = GUN_SHOT_HEIGHT; +#ifndef PUBLIC_SERVER + new->img = g_shot_simple; +#endif + break; + + case GUN_LASSER : + switch( author-> position ) + { + case TUX_RIGHT : + case TUX_LEFT : + new->w = GUN_LASSER_HORIZONTAL; + new->h = GUN_SHOT_VERTICAL; +#ifndef PUBLIC_SERVER + new->img = g_shot_lasserX; +#endif + break; + case TUX_UP : + case TUX_DOWN : + new->w = GUN_SHOT_VERTICAL; + new->h = GUN_LASSER_HORIZONTAL; +#ifndef PUBLIC_SERVER + new->img = g_shot_lasserY; +#endif + break; + } + break; + + case GUN_BOMBBALL : + new->w = GUN_BOMBBALL_WIDTH; + new->h = GUN_BOMBBALL_HEIGHT; + +#ifndef PUBLIC_SERVER + new->img = g_shot_bombball; +#endif + break; + + default : + assert( ! "Premenna weapon ma zlu hodnotu !" ); + break; + } + + return new; +} + +void replaceShotID(shot_t *shot, int id) +{ + replaceID(shot->id, id); + shot->id = id; +} + + +void getStatusShot(void *p, int *id, int *x,int *y, int *w, int *h) +{ + shot_t *shot; + + shot = p; + *id = shot->id; + *x = shot->x; + *y = shot->y; + *w = shot->w; + *h = shot->h; +} + +void setStatusShot(void *p, int x, int y, int w, int h) +{ + shot_t *shot; + + shot = p; + shot->x = x; + shot->y = y; + shot->w = w; + shot->h = h; +} + +#ifndef PUBLIC_SERVER + +void drawShot(shot_t *p) +{ + assert( p != NULL ); + addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, TUX_LAYER); +} + +void drawListShot(list_t *listShot) +{ + shot_t *thisShot; + int i; + + assert( listShot != NULL ); + + for( i = 0 ; i < listShot->count ; i++ ) + { + thisShot = (shot_t *)listShot->list[i]; + assert( thisShot != NULL ); + drawShot(thisShot); + } +} + +#endif + +static int getRandomCourse(int x, int y) +{ + int ret; + + do{ + switch( random() % 3 ) + { + case 0 : + ret = y; + break; + case 1 : + ret = -y; + break; + case 2 : + ret = 0; + break; + } + }while( ret == x ); + + return ret; +} + +void boundBombBall(shot_t *shot) +{ + if( shot->gun != GUN_BOMBBALL ) + { + return; + } + + shot->px = getRandomCourse(shot->px, +10); + shot->py = getRandomCourse(shot->py, +10); + shot->isCanKillAuthor = 1; + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_shot_server(PROTO_SEND_ALL, NULL, shot); + } +} + +void transformOnlyLasser(shot_t *shot) +{ + switch( shot->position ) + { + case TUX_RIGHT : + case TUX_LEFT : + shot->w = GUN_LASSER_HORIZONTAL; + shot->h = GUN_SHOT_VERTICAL; +#ifndef PUBLIC_SERVER + shot->img = g_shot_lasserX; +#endif + break; + case TUX_UP : + case TUX_DOWN : + shot->w = GUN_SHOT_VERTICAL; + shot->h = GUN_LASSER_HORIZONTAL; +#ifndef PUBLIC_SERVER + shot->img = g_shot_lasserY; +#endif + break; + } +} + +void eventMoveListShot(arena_t *arena) +{ + shot_t *thisShot; + int new_x, new_y; + int i; + + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) + { + thisShot = (shot_t *) arena->spaceShot->list->list[i]; + assert( thisShot != NULL ); + + new_x = thisShot->x + thisShot->px; + new_y = thisShot->y + thisShot->py; + + moveObjectInSpace(getCurrentArena()->spaceShot, thisShot, new_x, new_y); + + if( thisShot->x+thisShot->w < 0 || thisShot->x > arena->w || + thisShot->y+thisShot->h < 0 || thisShot->y > arena->h ) + { + delObjectFromSpaceWithObject(arena->spaceShot, + thisShot, destroyShot); + i--; + continue; + } + } +} + +static int myAbs(int n) +{ + return ( n > 0 ? n : -n ); +} + +static int getSppedShot(shot_t *shot) +{ + return ( myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py) ); +} + + +void checkShotIsInTuxScreen(arena_t *arena) +{ + int screen_x, screen_y; + shot_t *thisShot; + tux_t *thisTux; + int speed; + int i, j; + + if( getNetTypeGame() != NET_GAME_TYPE_SERVER ) + { + return; + } + + for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) + { + thisTux = (tux_t *)arena->spaceTux->list->list[i]; + speed = 25; + + if( thisTux->control != TUX_CONTROL_NET ) + { + continue; + } + + getCenterScreen(&screen_x, &screen_y, thisTux->x, thisTux->y); + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x-speed, screen_y, speed, WINDOW_SIZE_Y, listHelp); + + for( j = 0 ; j count ; j++ ) + { + thisShot = (shot_t *)listHelp->list[j]; + client_t *thisClient; + thisClient = getClientFromTux(thisTux); + proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); + } + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x+WINDOW_SIZE_X, screen_y, speed, WINDOW_SIZE_Y, listHelp); + + for( j = 0 ; j count ; j++ ) + { + thisShot = (shot_t *)listHelp->list[j]; + client_t *thisClient; + thisClient = getClientFromTux(thisTux); + proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); + } + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x, screen_y-speed, WINDOW_SIZE_X, speed, listHelp); + + for( j = 0 ; j count ; j++ ) + { + thisShot = (shot_t *)listHelp->list[j]; + client_t *thisClient; + thisClient = getClientFromTux(thisTux); + proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); + } + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x, screen_y+WINDOW_SIZE_Y, WINDOW_SIZE_X, speed, listHelp); + + for( j = 0 ; j count ; j++ ) + { + thisShot = (shot_t *)listHelp->list[j]; + client_t *thisClient; + thisClient = getClientFromTux(thisTux); + proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); + } + } +} + +void destroyShot(shot_t *p) +{ + assert( p != NULL ); + delID(p->id); + free(p); +} + +void quitShot() +{ + isShotInit = FALSE; +} diff --git a/src/base/shot.h b/src/base/shot.h new file mode 100644 index 0000000..db157cb --- /dev/null +++ b/src/base/shot.h @@ -0,0 +1,61 @@ + +#ifndef SHOT_H + +#define SHOT_H + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/arena.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#endif + +typedef struct shot_struct +{ + int id; + + int x; + int y; + + int w; + int h; + + int px; + int py; + + int position; + int gun; + + int author_id; + bool_t isCanKillAuthor; + +#ifndef PUBLIC_SERVER + SDL_Surface *img; +#endif +} shot_t; + +extern bool_t isShotInicialized(); +extern void initShot(); +extern shot_t* newShot(int x,int y, int px, int py, int gun, int author_id); +extern shot_t* getShotID(list_t *listShot, int id); +extern void replaceShotID(shot_t *shot, int id); +extern void getStatusShot(void *p, int *id, int *x,int *y, int *w, int *h); +extern void setStatusShot(void *p, int x, int y, int w, int h); +#ifndef PUBLIC_SERVER +extern void drawShot(shot_t *p); +extern void drawListShot(list_t *listShot); +#endif +extern int isConflictWithListShot(list_t *listShot, int x, int y, int w, int h); +extern void eventMoveListShot(arena_t *arena); +extern void checkShotIsInTuxScreen(arena_t *arena); + +extern void boundBombBall(shot_t *shot); +extern void transformOnlyLasser(shot_t *shot); + +extern void destroyShot(shot_t *p); +extern void quitShot(); + +#endif + diff --git a/src/base/space.c b/src/base/space.c new file mode 100644 index 0000000..0d3060a --- /dev/null +++ b/src/base/space.c @@ -0,0 +1,600 @@ + +#include +#include +#include +#include + +#include "main.h" +#include "list.h" +#include "space.h" + +#define DEBUG_SPACE +#define ERROR_SUPPORT + +#ifdef DEBUG_SPACE + + +typedef struct recv_struct +{ + int id; + int x, y; + int w, h; +} recv_t; + +recv_t* newRecv(int id, int x , int y, int w, int h) +{ + recv_t *new; + + new = malloc( sizeof(recv_t) ); + memset(new, 0, sizeof(recv_t)); + + new->id = id; + new->x = x; + new->y = y; + new->w = w; + new->h = h; + + return new; +} + +void getStatus(void *p, int *id, int *x, int *y, int *w, int *h) +{ + recv_t *recv; + + recv = p; + + *id = recv->id; + *x = recv->x; + *y = recv->y; + *w = recv->w; + *h = recv->h; +} + +void setStatus(void *p, int x, int y, int w, int h) +{ + recv_t *recv; + + recv = p; + + recv->x = x; + recv->y = y; + recv->w = w; + recv->h = h; + recv->x = x; +} + +void destroyRecv(recv_t *p) +{ + free(p); +} + +#endif + +static int my_conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2) +{ + return (x1w = w / segW + 1; + new->h = h / segH + 1; + new->segW = segW; + new->segH = segH; + new->getStatus = getStatus; + new->setStatus = setStatus; + new->list = newList(); + + new->area = malloc( new->w * sizeof(list_t **) ); + + for( i = 0 ; i < new->w ; i++ ) + { + new->area[i] = malloc( new->h * sizeof(list_t *) ); + } + + for( i = 0 ; i < new->h ; i++ ) + { + for( j = 0 ; j < new->w ; j++ ) + { + new->area[j][i] = newList(); + } + } + + return new; +} + +static void getSegment(space_t *p, int x, int y, int w, int h, + int *segX, int *segY, int *segW, int *segH) +{ + *segX = x / p->segW; + *segY = y / p->segH; + *segW = ( (x+w) / p->segW + 1 ) - *segX; + *segH = ( (y+h) / p->segH + 1 ) - *segY; +} + +#ifdef ERROR_SUPPORT + +void printListID(space_t *space) +{ + int i; + + for( i = 0 ; i < space->list->count ; i++ ) + { + int id, x, y, w, h; + + space->getStatus(space->list->list[i], &id, &x, &y, &w, &h); + printf("%d\n", id); + } + + putchar('\n'); +} + +static void checkList(space_t *space) +{ + int id, x, y, w, h; + int thisId; + int i; + + if( space->list->count == 0 ) + { + printf("nothing\n"); + return; + } + + space->getStatus(space->list->list[0], &thisId, &x, &y, &w, &h); + + for( i = 1 ; i < space->list->count ; i++ ) + { + space->getStatus(space->list->list[i], &id, &x, &y, &w, &h); + + if( id <= thisId ) + { + printListID(space); + assert( ! "error" ); + } + + thisId = id; + } +} + +static void addToListOnTheBaseIndex(space_t *space, void *p) +{ + int id, point_id, inc_point_id; + int x, y, w, h; + int len; + int min, max; + int point; + + + len = space->list->count; + + space->getStatus(p, &id, &x, &y, &w, &h); +/* + printf("addToListOnTheBaseIndex (%d)\n", id); + printListID(space); + assert( id >= 0 ); +*/ + if( len == 0 ) + { + //printf("OK first\n"); + addList(space->list, p); + return; + } + + if( len == 1 ) + { + space->getStatus(space->list->list[0], &point_id, &x, &y, &w, &h); + + if( id > point_id ) + { + addList(space->list, p); + } + + if( id < point_id ) + { + insList(space->list, 0, p); + } + + //printf("OK\n"); + + return; + } + + min = 0; + max = len-1; +/* + if( max < 0 ) + { + max = 0; + } +*/ + for(;;) + { + point = min + ( max - min ) / 2; +/* + space->getStatus(space->list->list[min], &id_min, &x, &y, &w, &h); + space->getStatus(space->list->list[max], &id_max, &x, &y, &w, &h); +*/ + //printf("min = %d max = %d point = %d len = %d id = %d\n", min, max, point, len, id); + + if( max < 0 ) + { + //printf("OK first\n"); + insList(space->list, 0, p); + //checkList(space); + return; + } + + if( point+1 >= len /*|| min >= len*/ ) + { + addList(space->list, p); + //printf("OK height\n"); + //checkList(space); + return; + } + + space->getStatus(space->list->list[point], &point_id, &x, &y, &w, &h); + space->getStatus(space->list->list[point+1], &inc_point_id, &x, &y, &w, &h); +/* + if( min == max ) + { + //printf("OK\n"); + insList(space->list, min, p); + checkList(space); + return; + } +*/ + if( point_id < id && id < inc_point_id ) + { + //printf("OK\n"); + insList(space->list, point+1, p); + //checkList(space); + return; + } + + if( id > inc_point_id ) + { + min = point + 1; + continue; + } + + if( id < point_id ) + { + max = point - 1; + continue; + } + } +} + +#endif + +void addObjectToSpace(space_t *p, void *item) +{ + int segX, segY, segW, segH; + int id, x, y, w, h; + int i, j; + + p->getStatus(item, &id, &x, &y, &w, &h); + getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); + + for( i = segY ; i < segY + segH ; i++ ) + { + for( j = segX ; j < segX + segW ; j++ ) + { + addList(p->area[j][i], item); + } + } + +#ifdef ERROR_SUPPORT + addToListOnTheBaseIndex(p, item); +#endif + +#ifndef ERROR_SUPPORT + addList(p->list, item); +#endif +} + +void getObjectFromSpace(space_t *p, int x, int y, int w, int h, list_t *list) +{ + int segX, segY, segW, segH; + int id, this_x, this_y, this_w, this_h; + int i, j, k; + + getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); + + for( i = segY ; i < segY + segH ; i++ ) + { + for( j = segX ; j < segX + segW ; j++ ) + { + void *this; + + for( k = 0 ; k < p->area[j][i]->count ; k++ ) + { + this = p->area[j][i]->list[k]; + p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); + + if( my_conflictSpace(x, y, w, h, this_x, this_y, this_w, this_h) && + searchListItem(list, this) == -1 ) + { + addList(list, this); + } + } + } + } +} + +int searchObjectInSpace(space_t *space, int id) +{ + int point_id; + int x, y, w, h; + int len; + int min, max; + int point; + +/* + printf("getObjectFromSpaceWithID %d\n", id); + printListID(space); +*/ + len = space->list->count; + + min = 0; + max = len-1; + + for(;;) + { + point = min + ( max - min ) / 2; + + //printf("min = %d max = %d point = %d len = %d id = %d\n", min, max, point, len, id); + + if( max < 0 || point >= len || max < min ) + { + return -1; + } + + space->getStatus(space->list->list[point], &point_id, &x, &y, &w, &h); + + if( min == max ) + { + space->getStatus(space->list->list[min], &point_id, &x, &y, &w, &h); + } + + if( point_id == id ) + { + return point; + } + + if( id > point_id ) + { + min = point + 1; + continue; + } + + if( id < point_id ) + { + max = point - 1; + continue; + } + } +} + +void* getObjectFromSpaceWithID(space_t *space, int id) +{ + int index; + + index = searchObjectInSpace(space, id); + + return ( index != -1 ? space->list->list[index] : NULL ); +} + +int isConflictWithObjectFromSpace(space_t *p, int x, int y, int w, int h) +{ + int segX, segY, segW, segH; + int id, this_x, this_y, this_w, this_h; + int i, j, k; + + getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); + + for( i = segY ; i < segY + segH ; i++ ) + { + for( j = segX ; j < segX + segW ; j++ ) + { + void *this; + + for( k = 0 ; k < p->area[j][i]->count ; k++ ) + { + this = p->area[j][i]->list[k]; + p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); + + if( my_conflictSpace(x, y, w, h, this_x, this_y, this_w, this_h) ) + { + return 1; + } + } + } + } + + return 0; +} + +int isConflictWithObjectFromSpaceBut(space_t *p, int x, int y, int w, int h, void *but) +{ + int segX, segY, segW, segH; + int id, this_x, this_y, this_w, this_h; + int i, j, k; + + getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); + + for( i = segY ; i < segY + segH ; i++ ) + { + for( j = segX ; j < segX + segW ; j++ ) + { + void *this; + + for( k = 0 ; k < p->area[j][i]->count ; k++ ) + { + this = p->area[j][i]->list[k]; + + if( this == but ) + { + continue; + } + + p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); + + if( my_conflictSpace(x, y, w, h, this_x, this_y, this_w, this_h) ) + { + return 1; + } + } + } + } + + return 0; +} + +void delObjectFromSpace(space_t *p, void *item) +{ + int segX, segY, segW, segH; + int id, x, y, w, h; + int index; + int i, j; + + p->getStatus(item, &id, &x, &y, &w, &h); + getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); + + for( i = segY ; i < segY + segH ; i++ ) + { + for( j = segX ; j < segX + segW ; j++ ) + { + index = searchListItem(p->area[j][i], item); + assert( index != -1 ); + delList(p->area[j][i], index); + } + } + + index = searchObjectInSpace(p, id); + assert( index != -1 ); + delList(p->list, index); +} + +void delObjectFromSpaceWithObject(space_t *p, void *item, void *f) +{ + void (*fce)(void *param); + + fce = f; + + delObjectFromSpace(p, item); + fce(item); +} + +void moveObjectInSpace(space_t *p, void *item, int move_x, int move_y) +{ + int old_segX, old_segY, old_segW, old_segH; + int new_segX, new_segY, new_segW, new_segH; + int id, x, y, w, h; + + p->getStatus(item, &id, &x, &y, &w, &h); + getSegment(p, x, y, w, h, &old_segX, &old_segY, &old_segW, &old_segH); + getSegment(p, move_x, move_y, w, h, &new_segX, &new_segY, &new_segW, &new_segH); +/* + printf("%d %d %d %d -> %d %d %d %d \n", + old_segX, old_segY, old_segW, old_segH, + new_segX, new_segY, new_segW, new_segH); +*/ + if( old_segX != new_segX || old_segY != new_segY || + old_segW != new_segW || old_segH != new_segH ) + { + delObjectFromSpace(p, item); + p->setStatus(item, move_x, move_y, w, h); + addObjectToSpace(p, item); + return; + } + + p->setStatus(item, move_x, move_y, w, h); +} + +void printSpace(space_t *p) +{ + int i, j; + + printf("print space : \n"); + + for( i = 0 ; i < p->h ; i++ ) + { + for( j = 0 ; j < p->w ; j++ ) + { + printf("%3d ", p->area[j][i]->count); + } + + putchar('\n'); + } +} + +void destroySpace(space_t *p) +{ + int j, i; + + destroyList(p->list); + + for( i = 0 ; i < p->h ; i++ ) + { + for( j = 0 ; j < p->w ; j++ ) + { + destroyList(p->area[j][i]); + } + } + + for( i = 0 ; i < p->w ; i++ ) + { + free(p->area[i]); + } + + free(p->area); + free(p); +} + +void destroySpaceWithObject(space_t *p, void *f) +{ + void (*fce)(void *param); + int i; + + fce = f; + + for( i = 0 ; i < p->list->count ; i++ ) + fce(p->list->list[i]); + + destroySpace(p); +} + +#ifdef DEBUG_SPACE + +void test_space() +{/* + space_t *space; + recv_t *recv; + + space = newSpace(5000, 2500, 320, 240, getStatus, setStatus); + + addObjectToSpace(space, newRecv(5, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(7, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(8, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(9, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(6, 0, 0, 1, 1) ); +*/ +/* + recv = getObjectFromSpaceWithID(space, 6); + if( recv != NULL )printf("id = %d\n", recv->id); +*/ +// printListID(space); +} + +#endif + diff --git a/src/base/space.h b/src/base/space.h new file mode 100644 index 0000000..de31768 --- /dev/null +++ b/src/base/space.h @@ -0,0 +1,38 @@ + +#ifndef SPACE_H + +#define SPACE_H + +#include "main.h" +#include "list.h" + +typedef struct space_struct +{ + int w; + int h; + int segW; + int segH; + list_t ***area; + list_t *list; + void (*getStatus)(void *p, int *id, int *x, int *y, int *w, int *h); + void (*setStatus)(void *p, int x, int y, int w, int h); +} space_t; + +extern space_t *newSpace(int w, int h, int segW, int segH, + void (*getStatus)(void *p, int *id, int *x, int *y, int *w, int *h), + void (*setStatus)(void *p, int x, int y, int w, int h)); + +extern void addObjectToSpace(space_t *p, void *item); +extern void getObjectFromSpace(space_t *p, int x, int y, int w, int h, list_t *list); +extern void* getObjectFromSpaceWithID(space_t *p, int id); +extern int isConflictWithObjectFromSpace(space_t *p, int x, int y, int w, int h); +extern int isConflictWithObjectFromSpaceBut(space_t *p, int x, int y, int w, int h, void *but); +extern void delObjectFromSpace(space_t *p, void *item); +extern void delObjectFromSpaceWithObject(space_t *p, void *item, void *f); +extern void moveObjectInSpace(space_t *p, void *item, int move_x, int move_y); +extern void printSpace(space_t *p); +extern void destroySpace(space_t *p); +extern void destroySpaceWithObject(space_t *p, void *f); + +#endif + diff --git a/src/base/storage.c b/src/base/storage.c new file mode 100644 index 0000000..e7afa45 --- /dev/null +++ b/src/base/storage.c @@ -0,0 +1,138 @@ + +#include +#include +#include +#include + +#include "main.h" +#include "list.h" + +typedef struct struct_storage_item +{ + char *name; + char *group; + void *data; +} storage_item_t; + +list_t* newStorage() +{ + return newList(); +} + +static storage_item_t* newStorageItem(char *group, char *name, void *data) +{ + storage_item_t *new; + + assert( name != NULL ); + assert( group != NULL ); + assert( data != NULL ); + + new = malloc( sizeof(storage_item_t) ); + new->name = strdup(name); + new->group = strdup(group); + new->data = data; + + return new; +} + +static void destroyStorageItem(storage_item_t *p, void *f) +{ + void(*fce)(void *); + + assert( p != NULL ); + assert( f != NULL ); + + fce = f; + + fce(p->data); + free(p->name); + free(p->group); + free(p); +} + +void addItemToStorage(list_t *list, char *group, char *name, void *data) +{ + addList(list, newStorageItem(group, name, data) ); +} + +void* getItemFromStorage(list_t *list, char *group, char *name) +{ + storage_item_t *this; + int i; + + assert( group != NULL ); + assert( name != NULL ); + + for(i = 0 ; i < list->count; i++) + { + this = (storage_item_t *) list->list[i]; + + if( strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0 ) + { + return this->data; + } + } + + printf("%s %s not found in storage !!!\n", group, name); + return NULL; +} + +void delItemFromStorage(list_t *list, char *group, char *name, void *f) +{ + storage_item_t *this; + int i; + + assert( group != NULL ); + assert( name != NULL ); + + for(i = 0 ; i < list->count; i++) + { + this = (storage_item_t *) list->list[i]; + + if( strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0 ) + { + destroyStorageItem(this, f); + delList(list, i); + return; + } + } + + return; +} + +void delAllItemFromStorage(list_t *list, char *group, void *f) +{ + storage_item_t *this; + int i; + + assert( group != NULL ); + + for(i = 0 ; i < list->count; i++) + { + this = (storage_item_t *) list->list[i]; + + if( strcmp(group, this->group) == 0 ) + { + destroyStorageItem(this, f); + delList(list, i); + i--; + } + } +} + +void destroyStorage(list_t *p, void *f) +{ + storage_item_t *this; + int i; + + assert( p != NULL ); + assert( f != NULL ); + + for( i = 0 ; i < p->count ; i++ ) + { + this = (storage_item_t *)p->list[i]; + destroyStorageItem(this, f); + } + + destroyList(p); +} diff --git a/src/base/storage.h b/src/base/storage.h new file mode 100644 index 0000000..af72ba2 --- /dev/null +++ b/src/base/storage.h @@ -0,0 +1,16 @@ + +#ifndef STORAGE_H + +#define STORAGE_H + +#include "base/main.h" +#include "base/list.h" + +extern list_t* newStorage(); +extern void addItemToStorage(list_t *list, char *group, char *name, void *data); +extern void* getItemFromStorage(list_t *list, char *group, char *name); +extern void delItemFromStorage(list_t *list, char *group, char *name, void *f); +extern void delAllItemFromStorage(list_t *list, char *group, void *f); +extern void destroyStorage(list_t *p, void *f); + +#endif diff --git a/src/base/string_length.h b/src/base/string_length.h new file mode 100644 index 0000000..c453e60 --- /dev/null +++ b/src/base/string_length.h @@ -0,0 +1,16 @@ + +#ifndef STR_LENGTH_H + +#define STR_LENGTH_H + +#include "base/main.h" + +#define STR_SIZE 128 +#define STR_PROTO_SIZE 128 +#define STR_NUM_SIZE 8 +#define STR_NAME_SIZE 32 +#define STR_PATH_SIZE 4096 +#define STR_FILE_NAME_SIZE 256 +#define STR_IP_SIZE 64 + +#endif diff --git a/src/base/textFile.c b/src/base/textFile.c new file mode 100755 index 0000000..5c8c95d --- /dev/null +++ b/src/base/textFile.c @@ -0,0 +1,163 @@ + +#include +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/textFile.h" + +textFile_t* newTextFile(char *s) +{ + textFile_t *new; + char path[STR_PATH_SIZE]; + + assert( s != NULL ); + + if( s[0] == '/' ) + { + strcpy(path, s); + } + else + { + getcwd(path, STR_PATH_SIZE); + strcat(path, "/"); + strcat(path, s); + } + + new = malloc(sizeof(textFile_t)); + new->file = strdup(path); + new->text = newList(); + + return new; +} + +static void createLine(list_t *list, char *p, int len) +{ + char *line; + char *begin_line; + char *end_line; + int length_line = 0; + + assert( list != NULL ); + assert( p != NULL ); + assert( len >= 0 ); + + begin_line = p; + + do{ + end_line = memchr( begin_line, '\n', len); + if( end_line == NULL )break; + + length_line = (int ) (end_line - ( begin_line ) ); + + line = malloc( (length_line+1) * sizeof(char) ); + memset(line, 0, (length_line+1) * sizeof(char) ); + strncpy(line, begin_line, length_line); + + addList(list, line); + len -= (length_line+1); + + begin_line = end_line+1; + + }while( begin_line != NULL ); +} + +/* + * Nacita subor *s a vrati ho f + * formatovany v type textFile_t* + */ +textFile_t* loadTextFile(char *s) +{ + FILE *file; + textFile_t *ret; + char *p; + int file_length = 0; + struct stat buf; + + assert( s != NULL ); + + if( lstat(s, &buf) < 0 ) + { + fprintf(stderr, "CHYBA: nemozem ziskat status suboru %s!\n", s); + return NULL; + } + + file_length = buf.st_size; + if( (file = fopen(s, "rt")) == NULL ) + { + fprintf(stderr, "CHYBA: nemozem otvorit subor %s pre citanie\n", s); + return NULL; + } + + p = malloc(file_length * sizeof(char)); + + if( fread(p, file_length * sizeof(char), 1, file) != 1 ) + { + fprintf(stderr, "CHYBA: nemozem nacitat udaje zo suboru %s\n",s); + fclose(file); + return NULL; + } + + fclose(file); + + ret = newTextFile(s); + createLine(ret->text, p, file_length); + + free(p); + + return ret; +} + +/* + * Zobrazi text ulozeny v *p + * do stdout + */ +void printTextFile(textFile_t *p) +{ + int i; + + assert( p != NULL ); + + printf("file : %s\n\n", p->file); + + for(i = 0; i < p->text->count; i++) + { + printf("%3d >> %s\n", i, (char *)p->text->list[i]); + } +} + +void saveTextFile(textFile_t *p) +{ + int i; + FILE *file; + + assert( p != NULL ); + + file = fopen(p->file, "wt"); + + for(i = 0; i < p->text->count; i++) + { + fprintf(file, "%s\n", (char *)p->text->list[i]); + } + + fclose(file); +} + +/* + * Uvolni text ulozeny v type textFile_t* + * z pamate. + */ +void destroyTextFile(textFile_t *p) +{ + assert( p != NULL ); + + free(p->file); + destroyListItem(p->text, free); + free(p); +} + + + diff --git a/src/base/textFile.h b/src/base/textFile.h new file mode 100755 index 0000000..ea15b4d --- /dev/null +++ b/src/base/textFile.h @@ -0,0 +1,21 @@ + +#ifndef TEXTFILE_H + +#define TEXTFILE_H + +#include "base/list.h" + +typedef struct str_textFile_t +{ + char *file; + list_t *text; +} textFile_t; + +extern textFile_t* newTextFile(char *s); +extern textFile_t* loadTextFile(char *s); +extern void printTextFile(textFile_t *p); +extern void saveTextFile(textFile_t *p); +extern void destroyTextFile(textFile_t *p); + + +#endif diff --git a/src/base/tux.c b/src/base/tux.c new file mode 100644 index 0000000..0c0717c --- /dev/null +++ b/src/base/tux.c @@ -0,0 +1,803 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/tux.h" +#include "base/arena.h" +#include "base/shot.h" +#include "base/gun.h" +#include "base/item.h" +#include "base/arena.h" +#include "base/myTimer.h" +#include "base/net_multiplayer.h" +#include "base/proto.h" +#include "base/modules.h" +#include "base/idManager.h" + +#ifndef PUBLIC_SERVER +#include "client/image.h" +#include "client/layer.h" +#include "client/term.h" + +#include "audio/sound.h" + +#include "screen/screen_world.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +#ifndef PUBLIC_SERVER + +static SDL_Surface *g_tux_up; +static SDL_Surface *g_tux_right; +static SDL_Surface *g_tux_left; +static SDL_Surface *g_tux_down; +static SDL_Surface *g_cross; + +#endif + +static bool_t isTuxInit = FALSE; + +bool_t isTuxInicialized() +{ + return isTuxInit; +} + +void initTux() +{ +#ifndef PUBLIC_SERVER + assert( isImageInicialized() == TRUE ); +#endif + +#ifndef PUBLIC_SERVER + g_tux_up = addImageData("tux8.png", IMAGE_ALPHA, "tux8", IMAGE_GROUP_BASE); + g_tux_right = addImageData("tux6.png", IMAGE_ALPHA, "tux6", IMAGE_GROUP_BASE); + g_tux_left = addImageData("tux4.png", IMAGE_ALPHA, "tux4", IMAGE_GROUP_BASE); + g_tux_down = addImageData("tux2.png", IMAGE_ALPHA, "tux2", IMAGE_GROUP_BASE); + g_cross = addImageData("cross.png", IMAGE_ALPHA, "cross", IMAGE_GROUP_BASE); +#endif + + isTuxInit = TRUE; +} + +tux_t* newTux() +{ + int x, y; + tux_t *new; + + new = malloc( sizeof(tux_t) ); + assert( new != NULL ); + + memset(new, 0, sizeof(tux_t) ); + + new->id = getNewID(); + new->status = TUX_STATUS_ALIVE; + new->control = TUX_CONTROL_NONE; + + findFreeSpace(getCurrentArena(), &x, &y, TUX_WIDTH, TUX_HEIGHT); + setTuxProportion(new, x, y); + + new->position = TUX_DOWN; + new->gun = GUN_SIMPLE; + new->shot[ new->gun ] = GUN_MAX_SHOT; + + sprintf(new->name, "no_name_id_%d", new->id); + new->score = 0; + new->frame = 0; + + new->bonus = BONUS_NONE; + new->bonus_time = 0; + + new->isCanShot = TRUE; + new->isCanSwitchGun = TRUE; + + new->client = NULL; + + return new; +} + +bool_t isTuxAnyGun(tux_t *tux) +{ + int i; + + for( i = 0 ; i < GUN_COUNT ; i++ ) + { + if( tux->shot[i] > 0 ) + { + return TRUE; + } + } + + return FALSE; +} + +void getCourse(int n, int *x, int *y) +{ + assert( x != NULL ); + assert( y != NULL ); + + switch( n ) + { + case TUX_UP : + *x = 0; + *y = -1; + break; + + case TUX_LEFT : + *x = -1; + *y = 0; + break; + + case TUX_RIGHT : + *x = +1; + *y = 0; + break; + + case TUX_DOWN : + *x = 0; + *y = +1; + break; + + default : + assert( ! "premenna n ma zlu hodnotu !" ); + break; + } +} + +#ifndef PUBLIC_SERVER + +void drawTux(tux_t *tux) +{ + SDL_Surface *g_image = NULL; + + assert( tux != NULL ); + + if( tux->bonus == BONUS_HIDDEN && + getNetTypeGame() != NET_GAME_TYPE_NONE && + tux->control == TUX_CONTROL_NET ) + { + return; + } + + switch( tux->position ) + { + case TUX_UP : + g_image = g_tux_up; + break; + + case TUX_LEFT : + g_image = g_tux_left; + break; + + case TUX_RIGHT : + g_image = g_tux_right; + break; + + case TUX_DOWN : + g_image = g_tux_down; + break; + + default : + assert( ! "premenna p->control ma zlu hodnotu !" ); + break; + } + + if( tux->status == TUX_STATUS_DEAD ) + { + g_image = g_cross; + + addLayer(g_cross, + tux->x - g_cross->w / 2, + ( tux->y + g_cross->h / 2 ) - g_cross->h, + 0, 0, + g_cross->w, g_cross->h, TUX_LAYER); + + return; + } + + addLayer(g_image, + tux->x - TUX_IMG_WIDTH / 2, + ( tux->y + TUX_HEIGHT / 2 ) - TUX_IMG_HEIGHT, + (tux->frame/TUX_KEY) * TUX_IMG_WIDTH, 0, + TUX_IMG_WIDTH, TUX_IMG_HEIGHT, TUX_LAYER); +} + +void drawListTux(list_t *listTux) +{ + tux_t *thisTux; + int i; + + assert( listTux != NULL ); + + for( i = 0 ; i < listTux->count ; i++ ) + { + thisTux = (tux_t *)listTux->list[i]; + assert( thisTux != NULL ); + drawTux(thisTux); + } +} + +#endif + +void replaceTuxID(tux_t *tux, int id) +{ + replaceID(tux->id, id); + tux->id = id; +} + +int isTuxSeesTux(tux_t *tux, tux_t *thisTux) +{ + int tux_x, tux_y, tux_w, tux_h; + int thisTux_x, thisTux_y, thisTux_w, thisTux_h; + int screen_x, screen_y; + + getTuxProportion(tux, &tux_x, &tux_y, &tux_w, &tux_h); + getTuxProportion(thisTux, &thisTux_x, &thisTux_y, &thisTux_w, &thisTux_h); + + getCenterScreen(&screen_x, &screen_y, tux->x, tux->y); + + return conflictSpace(screen_x-WINDOW_SIZE_X/4, screen_y-WINDOW_SIZE_Y/4, + WINDOW_SIZE_X*1.25, WINDOW_SIZE_Y*1.25, thisTux_x, thisTux_y, thisTux_w, thisTux_h); +} + +static void timer_spawnTux(void *p) +{ + tux_t *tux; + arena_t *arena; + int x, y; + int id; + + id = * ((int *)p); + free(p); + + arena = getCurrentArena(); + tux = getObjectFromSpaceWithID(arena->spaceTux, id); + + if( tux == NULL )return; + + tux->status = TUX_STATUS_ALIVE; + findFreeSpace(getCurrentArena(), &x, &y, TUX_WIDTH, TUX_HEIGHT); + moveObjectInSpace(getCurrentArena()->spaceTux, tux, x, y); + tux->gun = GUN_SIMPLE; + + addNewItem(arena->spaceItem, ID_UNKNOWN); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); + } +} + +static void timer_tuxCanShot(void *p) +{ + tux_t *tux; + int id; + + id = * ((int *)p); + free(p); + + tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + + if( tux == NULL )return; + + tux->isCanShot = TRUE; +} + +static void timer_tuxCanSwitchGun(void *p) +{ + tux_t *tux; + int id; + + id = * ((int *)p); + free(p); + + tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + + if( tux == NULL )return; + + tux->isCanSwitchGun = TRUE; +} + +void eventTuxIsDead(tux_t *tux) +{ +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#endif + + if( tux->status == TUX_STATUS_DEAD ) + { + return; + } + +#ifndef PUBLIC_SERVER + sprintf(msg, "tux with id %d is dead\n", tux->id); +#ifndef NO_SOUND + playSound("dead", SOUND_GROUP_BASE); +#endif + appendTextInTerm(msg); +#endif + + tux->status = TUX_STATUS_DEAD; + memset(tux->shot, 0, sizeof(int) * GUN_COUNT); + tux->gun = GUN_SIMPLE; + tux->shot[ tux->gun ] = GUN_MAX_SHOT; + + tux->bonus = BONUS_NONE; + tux->bonus_time = 0; + tux->pickup_time = 0; + + tux->isCanShot = TRUE; + tux->isCanSwitchGun = TRUE; + + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_spawnTux, newInt(tux->id), TUX_TIME_SPAWN ); + } + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_kill_server(PROTO_SEND_ALL, NULL, tux); + } +} + +static void eventTuxIsDeadWIthShot(tux_t *tux, shot_t *shot) +{ + if( shot->author_id != tux->id ) + { + tux_t *author; + + author = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, shot->author_id); + + if( author != NULL ) + { + author->score++; + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(PROTO_SEND_ALL, NULL, author); + } + } + + } + + countRoundInc(); + eventTuxIsDead(tux); +} + +void tuxTeleport(tux_t *tux) +{ + int x, y; +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#endif + +#ifndef PUBLIC_SERVER + sprintf(msg, "tux with id %d teleporting\n", tux->id); +#ifndef NO_SOUND + playSound("teleport", SOUND_GROUP_BASE); +#endif + appendTextInTerm(msg); +#endif + + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + findFreeSpace(getCurrentArena(), &x, &y, TUX_WIDTH, TUX_HEIGHT); + moveObjectInSpace(getCurrentArena()->spaceTux, tux, x, y); + //setTuxProportion(tux, x, y); + } + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); + } +} + +static void bombBallExplosion(shot_t *shot) +{ + item_t *item; + int x, y; + + x = ( shot->x + shot->w/2 ) - ITEM_BIG_EXPLOSION_WIDTH/2; + y = ( shot->y + shot->h/2 ) - ITEM_BIG_EXPLOSION_HEIGHT/2; + + item = newItem(x, y, ITEM_BIG_EXPLOSION, shot->author_id ); + + addObjectToSpace(getCurrentArena()->spaceItem, item ); + + delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_del_server(PROTO_SEND_ALL, NULL, shot->id); + proto_send_additem_server(PROTO_SEND_ALL, NULL, item); + } +} + +void eventConflictTuxWithShot(arena_t *arena) +{ + shot_t *thisShot; + tux_t *thisTux; + int i, j; + + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) + { + thisShot = (shot_t *)arena->spaceShot->list->list[i]; + assert( thisShot != NULL ); + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceTux, thisShot->x, thisShot->y, + thisShot->w, thisShot->h, listHelp); + + for( j = 0 ; j < listHelp->count ; j++ ) + { + thisTux = listHelp->list[j]; + + if( thisTux->status == TUX_STATUS_ALIVE ) + { + if( thisShot->author_id == thisTux->id && + thisShot->isCanKillAuthor == FALSE ) + { + continue; + } + + if( thisTux->bonus == BONUS_TELEPORT ) + { + tuxTeleport(thisTux); + continue; + } + + if( thisShot->gun == GUN_BOMBBALL ) + { + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + bombBallExplosion(thisShot); + i--; + } + + continue; + } + + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + eventTuxIsDeadWIthShot(thisTux, thisShot); + } + } + + delObjectFromSpaceWithObject(arena->spaceShot, thisShot, destroyShot); + i--; + + continue; + } + } +} + +void moveTux(tux_t *tux, int n) +{ + int px, py; + int zal_x, zal_y; + int new_x, new_y; + int w, h; + arena_t *arena; + + assert( tux != NULL ); + + arena = getCurrentArena(); + //interval(); + + if( tux->position != n ) + { + tux->position = n; + return; + } + + if( n == TUX_LEFT && tux->x-TUX_STEP <= 0 ) + { + return; + } + + if( n == TUX_RIGHT && tux->x+TUX_STEP >= arena->w ) + { + return; + } + + if( n == TUX_UP && tux->y-TUX_STEP <= 0 ) + { + return; + } + + if( n == TUX_DOWN && tux->y+TUX_STEP >= arena->h ) + { + return; + } + + getTuxProportion(tux, &zal_x, &zal_y, &w, &h); + + getCourse(tux->position, &px, &py); + + new_x = zal_x; + new_y = zal_y; + + new_x += px * TUX_STEP; + new_y += py * TUX_STEP; + + if( tux->bonus == BONUS_SPEED ) + { + new_x += px * TUX_STEP; + new_y += py * TUX_STEP; + } + + if( tux->bonus != BONUS_GHOST && ( + isConflictWithObjectFromSpaceBut(arena->spaceTux, new_x, new_y, w, h, tux) || + isConflictModule(new_x, new_y, w, h) ) ) + { + setTuxProportion(tux, zal_x, zal_y); + return; + } + + setTuxProportion(tux, zal_x, zal_y); + moveObjectInSpace(arena->spaceTux, tux, new_x, new_y); + eventGiveTuxListItem(tux, getCurrentArena()->spaceItem); + tux->frame++; + if( tux->frame == TUX_KEY * TUX_MAX_ANIMATION_FRAME ) tux->frame = 0; +} + +void switchTuxGun(tux_t *tux) +{ + int i; +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#endif + + if( tux->control != TUX_CONTROL_NET && + tux->isCanSwitchGun == FALSE ) + { + return; + } + + for( i = tux->gun+1 ; i < GUN_COUNT ; i++ ) + { + if( tux->shot[i] > 0 ) + { + tux->gun = i; + tux->isCanSwitchGun = FALSE; + addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_tuxCanSwitchGun, + newInt(tux->id), TUX_TIME_CAN_SWITCH_GUN ); +#ifndef PUBLIC_SERVER +#ifndef NO_SOUND + playSound("switch_gun", SOUND_GROUP_BASE); +#endif + sprintf(msg, "tux with id %d switch gun\n", tux->id); + appendTextInTerm(msg); +#endif + return; + } + } + + for( i = 0 ; i < GUN_COUNT ; i++ ) + { + if( tux->shot[i] > 0 ) + { + tux->gun = i; + tux->isCanSwitchGun = FALSE; + addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_tuxCanSwitchGun, + newInt(tux->id), TUX_TIME_CAN_SWITCH_GUN ); + +#ifndef PUBLIC_SERVER +#ifndef NO_SOUND + playSound("switch_gun", SOUND_GROUP_BASE); +#endif + sprintf(msg, "tux with id %d switch gun\n", tux->id); + appendTextInTerm(msg); +#endif + + return; + } + } +} + +void shotTux(tux_t *tux) +{ + assert( tux != NULL ); + + if( tux->isCanShot == FALSE || + tux->shot[tux->gun] == 0 ) + { + return; + } + +#ifndef NO_SOUND + playSound("switch_gun", SOUND_GROUP_BASE); +#endif + + shotInGun(tux); + + tux->isCanShot = FALSE; + + addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_tuxCanShot, + newInt(tux->id), TUX_TIME_CAN_SHOT ); + + if( tux->shot[tux->gun] == 0 ) + { + switchTuxGun(tux); + } +} + +void actionTux(tux_t *tux, int action) +{ + if( tux->status == TUX_STATUS_DEAD ) + { + return; + } + + switch( action ) + { + case TUX_UP : + case TUX_LEFT : + case TUX_RIGHT : + case TUX_DOWN : + moveTux(tux, action); + break; + + case TUX_SHOT : + shotTux(tux); + break; + + case TUX_SWITCH_GUN : + switchTuxGun(tux); + break; + } +} + +static void pickUpGun(tux_t *tux) +{ + if( isTuxAnyGun(tux) == FALSE ) + { + tux->gun = GUN_SIMPLE; + + if( tux->pickup_time < TUX_MAX_PICKUP ) + { + tux->pickup_time++; + } + + if( tux->pickup_time == TUX_MAX_PICKUP && getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#ifndef NO_SOUND + playSound("switch_gun", SOUND_GROUP_BASE); +#endif + sprintf(msg, "tux with id %d pickup\n", tux->id); + appendTextInTerm(msg); +#endif + + tux->gun = GUN_SIMPLE; + tux->shot[ tux->gun ] = GUN_MAX_SHOT; + tux->pickup_time = 0; + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); + } + } + } +} + +static void eventBonus(tux_t *tux) +{ + if( tux->bonus != BONUS_NONE ) + { + if( tux->bonus_time > 0 ) + { + tux->bonus_time--; + } + + if( tux->bonus_time == 0 && getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { +#ifndef PUBLIC_SERVER + char msg[STR_SIZE]; +#endif + + if( tux->bonus == BONUS_GHOST ) + { + int x, y, w, h; + getTuxProportion(tux, &x, &y, &w, &h); + + tux->bonus = BONUS_NONE; + + if( /*isConflictTuxWithListTux(tux, getCurrentArena()->listTux) || */ + isConflictModule(x, y, w, h) ) + { + tux->bonus = BONUS_GHOST; + return; + } + } + + tux->bonus = BONUS_NONE; + +#ifndef PUBLIC_SERVER + sprintf(msg, "tux with id %d bonus disabled\n", tux->id); + appendTextInTerm(msg); +#endif + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); + } + } + } +} + +void eventListTux(list_t *listTux) +{ + tux_t *thisTux; + arena_t *arena; + int i; + + assert( listTux != NULL ); + arena = getCurrentArena(); + + for( i = 0 ; i < listTux->count ; i++ ) + { + thisTux = (tux_t *)listTux->list[i]; + assert( thisTux != NULL ); + +#ifndef PUBLIC_SERVER + tuxControl(thisTux); +#endif + pickUpGun(thisTux); + eventBonus(thisTux); + eventGiveTuxListItem(thisTux, arena->spaceItem); + } +} + +void getTuxProportion(tux_t *tux, int *x,int *y, int *w, int *h) +{ + assert( tux != NULL ); + + if( x != NULL ) *x = tux->x - TUX_WIDTH / 2; + if( y != NULL ) *y = tux->y - TUX_HEIGHT / 2; + if( w != NULL ) *w = TUX_WIDTH; + if( h != NULL ) *h = TUX_HEIGHT; +} + +void setTuxProportion(tux_t *tux, int x, int y) +{ + assert( tux != NULL ); + + tux->x = x + TUX_WIDTH / 2; + tux->y = y + TUX_WIDTH / 2; +} + +void getStatusTux(void *p, int *id, int *x,int *y, int *w, int *h) +{ + tux_t *tux; + + tux = p; + *id = tux->id; + getTuxProportion(tux, x, y, w, h); +} + +void setStatusTux(void *p, int x, int y, int w, int h) +{ + tux_t *tux; + + tux = p; + setTuxProportion(tux, x, y); +} + +void destroyTux(tux_t *tux) +{ + assert( tux != NULL ); + delID(tux->id); + free(tux); +} + +void quitTux() +{ + isTuxInit = FALSE; +} diff --git a/src/base/tux.h b/src/base/tux.h new file mode 100644 index 0000000..8a39cf9 --- /dev/null +++ b/src/base/tux.h @@ -0,0 +1,137 @@ + +#ifndef TUX_H + +#define TUX_H + +#include "base/main.h" +#include "base/list.h" +#include "base/arena.h" + +#define TUX_STEP 7 +#define TUX_LAYER 0 + +#define TUX_MAX_ANIMATION_FRAME 10 +#define TUX_KEY 1 + +#define TUX_LOCATE_UNKNOWN -100 + +#define TUX_MAX_PICKUP 50 +#define TUX_MAX_BONUS 500 + +#define TUX_TIME_SPAWN 2000 +#define TUX_TIME_CAN_SHOT 500 +#define TUX_TIME_CAN_SWITCH_GUN 100 + +#define TUX_STATUS_ALIVE 0 +#define TUX_STATUS_DEAD 1 +#define TUX_STATUS_PAUZED 2 + +#define TUX_CONTROL_NONE 0 +#define TUX_CONTROL_KEYBOARD_LEFT 1 +#define TUX_CONTROL_KEYBOARD_RIGHT 2 +#define TUX_CONTROL_NET 3 +#define TUX_CONTROL_AI 4 + +#define TUX_WIDTH 30 +#define TUX_HEIGHT 30 + +#define TUX_IMG_WIDTH 40 +#define TUX_IMG_HEIGHT 54 + +#define TUX_UP 8 +#define TUX_LEFT 4 +#define TUX_RIGHT 6 +#define TUX_DOWN 2 +#define POSITION_UNKNOWN -1 + +#define TUX_SHOT 5 +#define TUX_SWITCH_GUN 0 + + +#define GUN_NONE -1 + +#define GUN_COUNT 7 +#define GUN_MAX_SHOT 5 + +#define BONUS_COUNT 6 +#define ITEM_COUNT 16 + + +#define SHOT_CHECK_AREA 25 + +#define GUN_SIMPLE 0 +#define GUN_DUAL_SIMPLE 1 +#define GUN_SCATTER 2 +#define GUN_TOMMY 3 +#define GUN_LASSER 4 +#define GUN_MINE 5 +#define GUN_BOMBBALL 6 + +#define ITEM_MINE 7 +#define ITEM_EXPLOSION 8 +#define ITEM_BIG_EXPLOSION 9 + +#define BONUS_NONE -1 +#define BONUS_SPEED 10 +#define BONUS_SHOT 11 +#define BONUS_TELEPORT 12 +#define BONUS_GHOST 13 +#define BONUS_4X 14 +#define BONUS_HIDDEN 15 + +typedef struct tux_struct +{ + int id; + + int x; + int y; + + char name[STR_NAME_SIZE]; + + int status; + int control; + int position; + int score; + + int gun; + int shot[GUN_COUNT]; + int round; + int bonus; + + int bonus_time; + int pickup_time; + + bool_t isCanShot; + bool_t isCanSwitchGun; + + int frame; + + void *client; +} tux_t; + +extern bool_t isTuxInicialized(); +extern void initTux(); +extern tux_t* newTux(); +extern bool_t isTuxAnyGun(tux_t *tux); +extern void getCourse(int n, int *x, int *y); +extern void drawTux(tux_t *tux); +extern void drawListTux(list_t *listTux); +extern void eventTuxIsDead(tux_t *tux); +extern tux_t* isConflictWithListTux(list_t *listTux, int x, int y, int w, int h); +extern int isConflictTuxWithListTux(tux_t *tux, list_t *listTux); +extern void eventConflictTuxWithShot(arena_t *arena); +extern void eventConflictTuxWithTeleport(list_t *listTux, list_t *listTeleport); +extern void tuxTeleport(tux_t *tux); +extern void actionTux(tux_t *tux, int action); +extern void eventListTux(list_t *listTux); +extern tux_t* getTuxID(list_t *listTux, int id); +extern void getTuxProportion(tux_t *tux, int *x,int *y, int *w, int *h); +extern void replaceTuxID(tux_t *tux, int id); +extern int isTuxSeesTux(tux_t *tux, tux_t *thisTux); +extern void setTuxProportion(tux_t *tux, int x, int y); +extern void getStatusTux(void *p, int *id, int *x,int *y, int *w, int *h); +extern void setStatusTux(void *p, int x, int y, int w, int h); +extern void destroyTux(tux_t *tux); +extern void quitTux(); + +#endif diff --git a/src/buffer.c b/src/buffer.c deleted file mode 100755 index 37cbdfc..0000000 --- a/src/buffer.c +++ /dev/null @@ -1,121 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "buffer.h" - -buffer_t * newBuffer(int n) -{ - buffer_t *new; - - new = malloc(sizeof(buffer_t)); - memset(new, 0, sizeof(buffer_t)); - new->alloc = n; - new->data = malloc(n); - memset(new->data , 0, n); - - return new; -} - -int addBuffer(buffer_t *p, char *data, int len) -{ - assert( p != NULL ); - assert( data != NULL ); - assert( len >= 0 ); - - if( p->size + len <= p->alloc ) - { - memcpy(p->data + p->size, data, len); - p->size += len; - return 0; - } - - return 1; -} - -int cutBuffer(buffer_t *p, int len) -{ - assert( p != NULL ); - assert( len >= 0 ); - - if( p->size - len >= 0 ) - { - memmove(p->data, p->data + len, p->size - len); - p->size -= len; - return 0; - } - - return 1; -} - -int getBufferLine(buffer_t *p, char *line, int len) -{ - char *end; - int ret_len; - - assert( p != NULL ); - assert( line != NULL ); - assert( len >= 0 ); - - end = NULL; - - end = memchr(p->data, '\n', p->size); - - if( end == NULL ) return -1; - - ret_len = (int) ( (end - p->data) + 1); - if( ret_len > len-1 ) ret_len = len-1; - - memset(line, 0, len); - memcpy(line, p->data, ret_len); - - cutBuffer(p, ret_len); - - return ret_len; -} - -int getData(buffer_t *p, char *line, int len) -{ - assert( p != NULL ); - assert( line != NULL ); - assert( len >= 0 ); - - if( p->size < len)return -1; - memcpy(line, p->data, len); - cutBuffer(p, len); - return len; -} - -void destroyBuffer(buffer_t *p) -{ - assert( p != NULL ); - - if( p->data != NULL )free(p->data); - free(p); -} - -/* -int main() -{ - buffer_t *p; - char str[64]; - char *s[] = {"hello world\n", "dalsi riadok\n", "koniec\n", NULL}; - int i; - - p = newBuffer(1024 * 10); - - for( i = 0 ; s[i] != NULL ; i++) - addBuffer(p, s[i], strlen(s[i])); - - while( getBufferLine(p, str, 64) > 0 ) - { - printf("str = %s\n", str); - } - - destroyBuffer(p); - return 0; -} -*/ diff --git a/src/checkFront.c b/src/checkFront.c deleted file mode 100644 index f6164f3..0000000 --- a/src/checkFront.c +++ /dev/null @@ -1,136 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "myTimer.h" -#include "checkFront.h" -#include "idManager.h" -#include "server.h" - -typedef struct struct_checkfront_t -{ - char *msg; - my_time_t time; - int id; - int count; - int type; -} checkfront_t; - -static checkfront_t* newCheck(char *s, int type, int id) -{ - checkfront_t *new; - - assert( s != NULL ); - - new = malloc( sizeof(checkfront_t) ); - memset(new, 0, sizeof(checkfront_t)); - - new->msg = strdup(s); - new->time = getMyTime(); - new->id = id; - new->type = type; - new->count = 0; - - return new; -} - -static void destroyCheck(checkfront_t *p) -{ - assert( p != NULL ); - - free(p->msg); - free(p); -} - -list_t* newCheckFront() -{ - return newList(); -} - -void addMsgInCheckFront(list_t *list, char *msg, int type, int id) -{ - addList(list, newCheck(msg, type, id) ); -} - -void eventMsgInCheckFront(client_t *client) -{ - my_time_t currentTime; - int i; - - currentTime = getMyTime(); - - for( i = 0 ; i < client->listSendMsg->count ; i++ ) - { - checkfront_t *this; - - this = (checkfront_t *)client->listSendMsg->list[i]; - - switch( this->type ) - { - case CHECK_FORNT_TYPE_SIMPLE : - sendClient(client, this->msg); - delListItem(client->listSendMsg, i, destroyCheck); - i--; - break; - - case CHECK_FORNT_TYPE_CHECK : - if( this->count == 0 || currentTime - this->time > CHECK_FRONT_SEND_TIME_INTERVAL ) - { - sendClient(client, this->msg); - this->time = currentTime; - this->count++; - } - - if( this->count > CHECK_FRONT_MAX_COUNT_SEND ) - { - if( isRegisterID(this->id) != -1 ) - { - delID(this->id); - } - - delListItem(client->listSendMsg, i, destroyCheck); - i--; - } - break; - - default : - assert( ! "Zly typ !" ); - break; - } - } -} - -void delMsgInCheckFront(list_t *listCheckFront, int id) -{ - int i; - - for( i = 0 ; i < listCheckFront->count ; i++ ) - { - checkfront_t *this; - - this = (checkfront_t *)listCheckFront->list[i]; - - if( this->id == id ) - { - if( isRegisterID(this->id) != -1 ) - { - delID(this->id); - } - - delListItem(listCheckFront, i, destroyCheck); - return; - } - } -} - -void destroyCheckFront(list_t *p) -{ - assert( p != NULL ); - - destroyListItem(p, destroyCheck); -} - diff --git a/src/client.c b/src/client.c deleted file mode 100644 index f49fefb..0000000 --- a/src/client.c +++ /dev/null @@ -1,306 +0,0 @@ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "network.h" -#include "buffer.h" -#include "net_multiplayer.h" -#include "screen_world.h" -#include "proto.h" -#include "client.h" -#include "language.h" -#include "screen_analyze.h" - -static int protocolType; - -#ifdef SUPPORT_NET_UNIX_UDP -#include "udp.h" -static sock_udp_t *sock_server_udp; -#endif - -#ifdef SUPPORT_NET_SDL_UDP -#include "sdl_udp.h" -static sock_sdl_udp_t *sock_server_sdl_udp; -#endif - -static list_t *clientBuffer; -static my_time_t lastPing; -static my_time_t lastPingServerAlive; - -static void initClient() -{ - char name[STR_NAME_SIZE]; - - clientBuffer = newList(); - lastPing = getMyTime(); - lastPingServerAlive = getMyTime(); - - getSettingNameRight(name); - proto_send_hello_client(name); -} - -#ifdef SUPPORT_NET_UNIX_UDP - -int initUdpClient(char *ip, int port, int proto) -{ - sock_server_udp = connectUdpSocket(ip, port, proto); - - if( sock_server_udp == NULL ) - { - return -1; - } - - printf("connect UDP %s %d\n", ip, port); - - protocolType = NET_PROTOCOL_TYPE_UDP; - initClient(); - - return 0; -} - -#endif - -#ifdef SUPPORT_NET_SDL_UDP - -int initSdlUdpClient(char *ip, int port) -{ - sock_server_sdl_udp = connectSdlUdpSocket(ip, port); - - if( sock_server_sdl_udp == NULL ) - { - return -1; - } - - printf("connect UDP %s %d\n", ip, port); - - protocolType = NET_PROTOCOL_TYPE_UDP; - initClient(); - - return 0; -} - -#endif - -void sendServer(char *msg) -{ - int ret; - - assert( msg != NULL ); - -#ifndef PUBLIC_SERVER - if( isParamFlag("--send") ) - { - printf("send -> %s", msg); - } -#endif - -#ifdef SUPPORT_NET_UNIX_UDP - ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg)); -#endif - -#ifdef SUPPORT_NET_SDL_UDP - ret = writeSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, msg, strlen(msg)); -#endif -} - -static int eventServerSelect() -{ - char buffer[STR_PROTO_SIZE]; - int ret; - - memset(buffer,0 ,STR_PROTO_SIZE); - -#ifdef SUPPORT_NET_UNIX_UDP - ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE-1); -#endif - -#ifdef SUPPORT_NET_SDL_UDP - ret = readSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, buffer, STR_PROTO_SIZE-1); - - if( ret < 0 ) - { - return ret; - } -#endif - - addList(clientBuffer, strdup(buffer) ); -/* - if( addBuffer(clientBuffer, buffer, ret) != 0 ) - { - fprintf(stderr, "chyba, nemozem zapisovat do mojho buffera !\n"); - setWorldEnd(); - return ret; - } -*/ - return ret; -} - -void eventServerBuffer() -{ - char *line; - int i; - - /* obsluha udalosti od servera */ - - assert( clientBuffer != NULL ); - - for( i = 0 ; i < clientBuffer->count ; i++ ) - { - line = (char *)clientBuffer->list[i]; - -#ifndef PUBLIC_SERVER - if( isParamFlag("--recv") ) - { - printf("recv -> %s", line); - } -#endif - if( strncmp(line, "error", 5) == 0 )proto_recv_error_client(line); - if( strncmp(line, "init", 4) == 0 )proto_recv_init_client(line); - if( strncmp(line, "event", 5) == 0 )proto_recv_event_client(line); - if( strncmp(line, "newtux", 6) == 0 )proto_recv_newtux_client(line); - if( strncmp(line, "del", 3) == 0 )proto_recv_del_client(line); - if( strncmp(line, "additem", 7) == 0 )proto_recv_additem_client(line); - if( strncmp(line, "shot", 4) == 0 )proto_recv_shot_client(line); - if( strncmp(line, "kill", 4) == 0 )proto_recv_kill_client(line); - if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_client(line); - if( strncmp(line, "end", 3) == 0 )proto_recv_end_client(line); - - lastPingServerAlive = getMyTime(); - } - - destroyListItem(clientBuffer, free); - clientBuffer = newList(); -} - -void eventPingServer() -{ - my_time_t currentTime; - - currentTime = getMyTime(); - - if( currentTime - lastPing > CLIENT_TIMEOUT ) - { - proto_send_ping_client(); - lastPing = getMyTime(); - } -} - -bool_t isServerAlive() -{ - my_time_t currentTime; - - currentTime = getMyTime(); - - if( currentTime - lastPingServerAlive > SERVER_TIMEOUT_ALIVE ) - { - return FALSE; - } - - return TRUE; -} - -#ifdef SUPPORT_NET_UNIX_UDP - -void selectClientUdpSocket() -{ - fd_set readfds; - struct timeval tv; - int max_fd; - bool_t isNext; - - if( isServerAlive() == FALSE ) - { - fprintf(stderr, "server neodpoveda !\n"); - setWorldEnd(); - return; - } - - do{ - isNext = FALSE; - - tv.tv_sec = 0; - tv.tv_usec = 0; - - FD_ZERO(&readfds); - FD_SET(sock_server_udp->sock, &readfds); - max_fd = sock_server_udp->sock; - - select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv); - - if( FD_ISSET(sock_server_udp->sock, &readfds) ) - { - eventServerSelect(); - isNext = TRUE; - } - - }while( isNext == TRUE ); - -} - -#endif - -#ifdef SUPPORT_NET_SDL_UDP - -void selectClientSdlUdpSocket() -{ - int ret; - - if( isServerAlive() == FALSE ) - { - setMsgToAnalyze(getMyText("ERROR_SERVER_DONT_ALIVE")); - setWorldEnd(); - return; - } - - do{ - ret = eventServerSelect(); - }while( ret > 0); -} - -#endif - -static void quitClient() -{ - proto_send_end_client(); - assert( clientBuffer != NULL ); - destroyListItem(clientBuffer, free); -} - -#ifdef SUPPORT_NET_UNIX_UDP - -void quitUdpClient() -{ - quitClient(); - - assert( sock_server_udp != NULL ); - closeUdpSocket(sock_server_udp); - - printf("quit UDP conenct\n"); -} - -#endif - -#ifdef SUPPORT_NET_SDL_UDP - -void quitSdlUdpClient() -{ - quitClient(); - - assert( sock_server_sdl_udp != NULL ); - closeSdlUdpSocket(sock_server_sdl_udp); - - printf("quit UDP conenct\n"); -} - -#endif diff --git a/src/client/Makefile b/src/client/Makefile new file mode 100644 index 0000000..c5637ea --- /dev/null +++ b/src/client/Makefile @@ -0,0 +1,36 @@ + + +all: client.o configFile.o font.o game.o image.o interface.o keytable.o language.o layer.o panel.o screen.o term.o + +client.o: client.c client.h + $(CC) $(CFLAGS) -o client.o -c client.c + +configFile.o: configFile.c configFile.h + $(CC) $(CFLAGS) -o configFile.o -c configFile.c + +font.o: font.c font.h + $(CC) $(CFLAGS) -o font.o -c font.c + +game.o: game.c game.h + $(CC) $(CFLAGS) -o game.o -c game.c + +image.o: image.c image.h + $(CC) $(CFLAGS) -o image.o -c image.c + +interface.o: interface.c interface.h + $(CC) $(CFLAGS) -o interface.o -c interface.c + +layer.o: layer.c layer.h + $(CC) $(CFLAGS) -o layer.o -c layer.c + +panel.o: panel.c panel.h + $(CC) $(CFLAGS) -o panel.o -c panel.c + +screen.o: screen.c screen.h + $(CC) $(CFLAGS) -o screen.o -c screen.c + +term.o: term.c term.h + $(CC) $(CFLAGS) -o term.o -c term.c + +clean: + rm -rf *.o diff --git a/src/client/client.c b/src/client/client.c new file mode 100644 index 0000000..975c9da --- /dev/null +++ b/src/client/client.c @@ -0,0 +1,305 @@ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/proto.h" + +#include "client/client.h" +#include "client/language.h" + +#include "screen/screen_analyze.h" +#include "screen/screen_world.h" + +static int protocolType; + +#ifdef SUPPORT_NET_UNIX_UDP +#include "net_unix/udp.h" +static sock_udp_t *sock_server_udp; +#endif + +#ifdef SUPPORT_NET_SDL_UDP +#include "net_sdl/sdl_udp.h" +static sock_sdl_udp_t *sock_server_sdl_udp; +#endif + +static list_t *clientBuffer; +static my_time_t lastPing; +static my_time_t lastPingServerAlive; + +static void initClient() +{ + char name[STR_NAME_SIZE]; + + clientBuffer = newList(); + lastPing = getMyTime(); + lastPingServerAlive = getMyTime(); + + getSettingNameRight(name); + proto_send_hello_client(name); +} + +#ifdef SUPPORT_NET_UNIX_UDP + +int initUdpClient(char *ip, int port, int proto) +{ + sock_server_udp = connectUdpSocket(ip, port, proto); + + if( sock_server_udp == NULL ) + { + return -1; + } + + printf("connect UDP %s %d\n", ip, port); + + protocolType = NET_PROTOCOL_TYPE_UDP; + initClient(); + + return 0; +} + +#endif + +#ifdef SUPPORT_NET_SDL_UDP + +int initSdlUdpClient(char *ip, int port) +{ + sock_server_sdl_udp = connectSdlUdpSocket(ip, port); + + if( sock_server_sdl_udp == NULL ) + { + return -1; + } + + printf("connect UDP %s %d\n", ip, port); + + protocolType = NET_PROTOCOL_TYPE_UDP; + initClient(); + + return 0; +} + +#endif + +void sendServer(char *msg) +{ + int ret; + + assert( msg != NULL ); + +#ifndef PUBLIC_SERVER + if( isParamFlag("--send") ) + { + printf("send -> %s", msg); + } +#endif + +#ifdef SUPPORT_NET_UNIX_UDP + ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg)); +#endif + +#ifdef SUPPORT_NET_SDL_UDP + ret = writeSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, msg, strlen(msg)); +#endif +} + +static int eventServerSelect() +{ + char buffer[STR_PROTO_SIZE]; + int ret; + + memset(buffer,0 ,STR_PROTO_SIZE); + +#ifdef SUPPORT_NET_UNIX_UDP + ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE-1); +#endif + +#ifdef SUPPORT_NET_SDL_UDP + ret = readSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, buffer, STR_PROTO_SIZE-1); + + if( ret < 0 ) + { + return ret; + } +#endif + + addList(clientBuffer, strdup(buffer) ); +/* + if( addBuffer(clientBuffer, buffer, ret) != 0 ) + { + fprintf(stderr, "chyba, nemozem zapisovat do mojho buffera !\n"); + setWorldEnd(); + return ret; + } +*/ + return ret; +} + +void eventServerBuffer() +{ + char *line; + int i; + + /* obsluha udalosti od servera */ + + assert( clientBuffer != NULL ); + + for( i = 0 ; i < clientBuffer->count ; i++ ) + { + line = (char *)clientBuffer->list[i]; + +#ifndef PUBLIC_SERVER + if( isParamFlag("--recv") ) + { + printf("recv -> %s", line); + } +#endif + if( strncmp(line, "error", 5) == 0 )proto_recv_error_client(line); + if( strncmp(line, "init", 4) == 0 )proto_recv_init_client(line); + if( strncmp(line, "event", 5) == 0 )proto_recv_event_client(line); + if( strncmp(line, "newtux", 6) == 0 )proto_recv_newtux_client(line); + if( strncmp(line, "del", 3) == 0 )proto_recv_del_client(line); + if( strncmp(line, "additem", 7) == 0 )proto_recv_additem_client(line); + if( strncmp(line, "shot", 4) == 0 )proto_recv_shot_client(line); + if( strncmp(line, "kill", 4) == 0 )proto_recv_kill_client(line); + if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_client(line); + if( strncmp(line, "end", 3) == 0 )proto_recv_end_client(line); + + lastPingServerAlive = getMyTime(); + } + + destroyListItem(clientBuffer, free); + clientBuffer = newList(); +} + +void eventPingServer() +{ + my_time_t currentTime; + + currentTime = getMyTime(); + + if( currentTime - lastPing > CLIENT_TIMEOUT ) + { + proto_send_ping_client(); + lastPing = getMyTime(); + } +} + +bool_t isServerAlive() +{ + my_time_t currentTime; + + currentTime = getMyTime(); + + if( currentTime - lastPingServerAlive > SERVER_TIMEOUT_ALIVE ) + { + return FALSE; + } + + return TRUE; +} + +#ifdef SUPPORT_NET_UNIX_UDP + +void selectClientUdpSocket() +{ + fd_set readfds; + struct timeval tv; + int max_fd; + bool_t isNext; + + if( isServerAlive() == FALSE ) + { + fprintf(stderr, "server neodpoveda !\n"); + setWorldEnd(); + return; + } + + do{ + isNext = FALSE; + + tv.tv_sec = 0; + tv.tv_usec = 0; + + FD_ZERO(&readfds); + FD_SET(sock_server_udp->sock, &readfds); + max_fd = sock_server_udp->sock; + + select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv); + + if( FD_ISSET(sock_server_udp->sock, &readfds) ) + { + eventServerSelect(); + isNext = TRUE; + } + + }while( isNext == TRUE ); + +} + +#endif + +#ifdef SUPPORT_NET_SDL_UDP + +void selectClientSdlUdpSocket() +{ + int ret; + + if( isServerAlive() == FALSE ) + { + setMsgToAnalyze(getMyText("ERROR_SERVER_DONT_ALIVE")); + setWorldEnd(); + return; + } + + do{ + ret = eventServerSelect(); + }while( ret > 0); +} + +#endif + +static void quitClient() +{ + proto_send_end_client(); + assert( clientBuffer != NULL ); + destroyListItem(clientBuffer, free); +} + +#ifdef SUPPORT_NET_UNIX_UDP + +void quitUdpClient() +{ + quitClient(); + + assert( sock_server_udp != NULL ); + closeUdpSocket(sock_server_udp); + + printf("quit UDP conenct\n"); +} + +#endif + +#ifdef SUPPORT_NET_SDL_UDP + +void quitSdlUdpClient() +{ + quitClient(); + + assert( sock_server_sdl_udp != NULL ); + closeSdlUdpSocket(sock_server_sdl_udp); + + printf("quit UDP conenct\n"); +} + +#endif diff --git a/src/client/client.h b/src/client/client.h new file mode 100644 index 0000000..639f924 --- /dev/null +++ b/src/client/client.h @@ -0,0 +1,35 @@ + +#ifndef MY_CLIENT + +#define MY_CLIENT + +#include "base/main.h" + +#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP + +#define CLIENT_TIMEOUT 1000 +#define SERVER_TIMEOUT_ALIVE 5000 + +#endif + +#ifdef SUPPORT_NET_UNIX_UDP +extern int initUdpClient(char *ip, int port, int proto); +extern void eventPingServer(); +extern void selectClientUdpSocket(); +extern void refreshPingServerAlive(); +extern void quitUdpClient(); +#endif + +#ifdef SUPPORT_NET_SDL_UDP +extern int initSdlUdpClient(char *ip, int port); +extern void eventPingServer(); +extern void selectClientSdlUdpSocket(); +extern void refreshPingServerAlive(); +extern bool_t isServerAlive(); +extern void quitSdlUdpClient(); +#endif + +extern void sendServer(char *msg); +extern void eventServerBuffer(); + +#endif diff --git a/src/client/configFile.c b/src/client/configFile.c new file mode 100644 index 0000000..acf7a1f --- /dev/null +++ b/src/client/configFile.c @@ -0,0 +1,136 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "client/configFile.h" + +int isYesOrNO(char *s) +{ + if( s[0] == 'Y' || s[0] == 'y' )return 1; + return 0; +} + +char* getYesOrNo(int n) +{ + return ( n != 0 ? "YES" : "NO" ); +} + +int getValue(char *line, char *env, char *val, int len) +{ + char *offset_env; + char *offset_val_begin; + char *offset_val_end; + char clone_env[STR_SIZE]; + int val_len; + + strcpy(clone_env, " "); + strcat(clone_env, env); + + offset_env = strstr(line, clone_env); + if( offset_env == NULL )return -1; + + offset_val_begin = strchr(offset_env, '"'); + if( offset_val_begin == NULL )return -1; + + offset_val_end = strchr(offset_val_begin+1, '"'); + if( offset_val_end == NULL )return -1; + + val_len = (int)(offset_val_end - ( offset_val_begin + 1) ); + if( val_len > len - 1 ) val_len = len - 1; + + memset(val, 0, len); + memcpy(val, offset_val_begin+1, val_len); + + return 0; +} + +char* setValue(char *line, char *env, char *val) +{ + char *offset_env; + char *offset_val_begin; + char *offset_val_end; + char clone_env[STR_SIZE]; + char clone[STR_SIZE]; + + //strcpy(clone_env, " "); + strcpy(clone_env, env); + + offset_env = strstr(line, clone_env); + if( offset_env == NULL )return NULL; + + offset_val_begin = strchr(offset_env, '"'); + if( offset_val_begin == NULL )return NULL; + + offset_val_end = strchr(offset_val_begin+1, '"'); + if( offset_val_end == NULL )return NULL; + + memset(clone, 0, STR_SIZE); + strncpy(clone, line, (int) ( (offset_val_begin - line) + 1 ) ); + strcat(clone, val); + strcat(clone, offset_val_end); + + return strdup( clone ); +} + +int getValueInConfigFile(textFile_t *textFile, char *env, char *val, int len) +{ + int i; + char *line; + + for( i = 0 ; i < textFile->text->count ; i++ ) + { + line = (char *) (textFile->text->list[i]); + + if( strstr(line, env) != NULL ) + { + return getValue(line, env, val, len); + } + } + + return -1; +} + +int setValueInConfigFile(textFile_t *textFile, char *env, char *val) +{ + int i; + char *line; + char *ret; + + for( i = 0 ; i < textFile->text->count ; i++ ) + { + line = (char *) (textFile->text->list[i]); + + if( strstr(line, env) != NULL ) + { + ret = setValue(line, env, val); + + if( ret != NULL ) + { + delListItem(textFile->text, i, free); + insList(textFile->text, i, ret); + + return 0; + } + } + } + + return -1; +} + +void loadValueFromConfigFile(textFile_t *textFile, char *env, char *val, int len, char *butVal) +{ + strcpy(val, butVal); + + if( getValueInConfigFile(textFile, env, val, len) != 0 ) + { + char line[STR_SIZE]; + + sprintf(line, " %s=\"%s\"", env, butVal); + addList(textFile->text, strdup(line) ); + + printf("Add line \"%s\" in config file.\n", line); + } +} diff --git a/src/client/configFile.h b/src/client/configFile.h new file mode 100644 index 0000000..67898e9 --- /dev/null +++ b/src/client/configFile.h @@ -0,0 +1,20 @@ + +#ifndef CONFIGFILE_H + +#define CONFIGFILE_H + +#include "base/main.h" +#include "base/textFile.h" + +extern int isYesOrNO(char *s); +extern char* getYesOrNo(int n); + +extern int getValue(char *line, char *env, char *val, int len); +extern char* setValue(char *line, char *env, char *val); + +extern int getValueInConfigFile(textFile_t *textFile, char *env, char *val, int len); +extern int setValueInConfigFile(textFile_t *textFile, char *env, char *val); + +extern void loadValueFromConfigFile(textFile_t *textFile, char *env, char *val, int len, char *butVal); + +#endif diff --git a/src/client/font.c b/src/client/font.c new file mode 100755 index 0000000..91d15bb --- /dev/null +++ b/src/client/font.c @@ -0,0 +1,99 @@ + +#include "base/main.h" +#include "client/interface.h" +#include "client/font.h" + +static TTF_Font *g_font; +static int fontSize; + +static bool_t isFontInit = FALSE; + +bool_t isFontInicialized() +{ + return isFontInit; +} + +/* + * Inicialuzuje font. + * nacita font zo suboru *file o velkosti size + * Automaticky sa styk nastavy ako TTF_STYLE_BOLD + */ +void initFont(char *file,int size) +{ + char str[STR_PATH_SIZE]; + + assert( file != NULL ); + assert( size > 0 ); + + assert( isInterfaceInicialized() == TRUE ); + + sprintf(str, PATH_FONT "%s", file); + + if( TTF_Init() == -1 ) + { + fprintf(stderr, "%s\n", SDL_GetError()); + return; + } + + accessExistFile(str); + + g_font = TTF_OpenFont(str, size); + TTF_SetFontStyle(g_font, TTF_STYLE_NORMAL); + + fontSize = size; + + printf("init font.. (%s)\n", file); + isFontInit = TRUE; +} + +/* + * Zobrazi text *s na suradnicu x y s farbou RGB + */ +void drawFont(char *s, int x, int y, int r, int g, int b) +{ + SDL_Rect dst_rect; + SDL_Surface *text; + SDL_Surface *p; + SDL_Color farba_pisma = {r, g, b, 0}; + + assert( s != NULL ); + + p = getSDL_Screen(); + + text = TTF_RenderUTF8_Blended(g_font, s, farba_pisma); + + dst_rect.x = x; + dst_rect.y = y; + + SDL_BlitSurface(text, NULL, p, &dst_rect); + SDL_FreeSurface(text); +} + +/* + * Vrati velkost daneho fontu. + */ +int getFontSize() +{ + return fontSize; +} + +void getTextSize(char *s, int *w, int *h) +{ + assert( s != NULL ); + assert( w != NULL ); + assert( h != NULL ); + + TTF_SizeUTF8(g_font, s, w, h); +} + +/* + * Uvolni font z pamete. + */ +void quitFont() +{ + TTF_CloseFont(g_font); + TTF_Quit(); + + printf("quit font\n"); + isFontInit = FALSE; +} diff --git a/src/client/font.h b/src/client/font.h new file mode 100755 index 0000000..50b1494 --- /dev/null +++ b/src/client/font.h @@ -0,0 +1,26 @@ + +#ifndef FONT_H + +#define FONT_H + +#include +#include +#include "base/main.h" + +#define COLOR_WHITE 255, 255, 255 +#define COLOR_BLACK 0, 0, 0 + +#define COLOR_RED 255, 0, 0 +#define COLOR_GREEN 0, 255, 0 +#define COLOR_BLUE 0, 0, 255 + +#define COLOR_YELLOW 255, 255, 0 + +extern bool_t isFontInicialized(); +extern void initFont(char *file,int size); +extern void drawFont(char *s,int x,int y,int r,int g,int b); +extern int getFontSize(); +extern void getTextSize(char *s, int *w, int *h); +extern void quitFont(); + +#endif diff --git a/src/client/game.c b/src/client/game.c new file mode 100644 index 0000000..9ea50ab --- /dev/null +++ b/src/client/game.c @@ -0,0 +1,120 @@ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/tux.h" +#include "base/homeDirector.h" +#include "base/item.h" +#include "base/shot.h" +#include "base/arenaFile.h" + +#include "client/image.h" +#include "client/layer.h" +#include "client/interface.h" +#include "client/screen.h" +#include "client/font.h" +#include "client/keytable.h" +#include "client/language.h" +#include "client/panel.h" + +#include "audio/audio.h" +#include "audio/sound.h" +#include "audio/music.h" + +#include "screen/screen_world.h" +#include "screen/screen_mainMenu.h" +#include "screen/screen_analyze.h" +#include "screen/screen_setting.h" +#include "screen/screen_gameType.h" +#include "screen/screen_choiceArena.h" +#include "screen/screen_table.h" +#include "screen/screen_credits.h" + +static void initGame() +{ + createHomeDirector(); + + if( initLanguage() == -1 ) + { + printf("fatal error !\n"); + exit(-1); + } + + initSDL(); + + initLayer(); + initFont(getLanguageFont(), getLanguageSize()); + initKeyTable(); + initImageData(); +#ifndef NO_SOUND + initAudio(); + initSound(); + initMusic(); +#endif + initScreen(); + initArenaFile(); + initTux(); + initItem(); + initShot(); + initPanel(); + initWorld(); + initScreenMainMenu(); + initScreenAnalyze(); + initScreenSetting(); + initScreenGameType(); + initScreenChoiceArena(); + initScreenCredits(); + initScreenTable(); +} + +void quitGame() +{ + quitSDL(); + quitLayer(); + quitFont(); + quitKeyTable(); + quitLanguage(); + quitScreen(); + quitArenaFile(); + quitItem(); + quitTux(); + quitShot(); + quitPanel(); + quitWorld(); + quitImageData(); +#ifndef NO_SOUND + quitSound(); + quitMusic(); + quitAudio(); +#endif + quitScreenMainMenu(); + quitScreenAnalyze(); + quitScreenSetting(); + quitScreenGameType(); + quitScreenChoiceArena(); + quitScreenCredits(); + quitScreenTable(); + + destroyList(listHelp); + + printf("quit..\n"); + + exit(0); +} + +void startGame() +{ + initGame(); + + setScreen("mainMenu"); + + eventSDL(); + quitGame(); +} diff --git a/src/client/game.h b/src/client/game.h new file mode 100644 index 0000000..6ce694d --- /dev/null +++ b/src/client/game.h @@ -0,0 +1,9 @@ + +#ifndef GAME_H + +#define GAME_H + +extern void quitGame(); +extern void startGame(); + +#endif diff --git a/src/client/image.c b/src/client/image.c new file mode 100755 index 0000000..98b2b8b --- /dev/null +++ b/src/client/image.c @@ -0,0 +1,149 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/storage.h" + +#include "client/interface.h" +#include "client/image.h" + +static list_t *listStorage; + +static bool_t isImageDataInit = FALSE; + +bool_t isImageInicialized() +{ + return isImageDataInit; +} + +/* + * Inicialuzje globalny zoznam obrazkov + */ +void initImageData() +{ + assert( isInterfaceInicialized() == TRUE ); + + printf("init image database..\n"); + listStorage = newStorage(); + isImageDataInit = TRUE; +} + +static SDL_Surface *loadImage(const char *filename, int alpha) +{ + SDL_Surface *tmp; + SDL_Surface *ret; + + char str[STR_PATH_SIZE]; + + + sprintf(str, PATH_IMAGE "%s", filename); + + accessExistFile(str); + + if( ( tmp = IMG_Load(str) ) == NULL ) + { + fprintf(stderr, "%s\n", SDL_GetError()); + return NULL; + } + + if( ( ret = (alpha) ? SDL_DisplayFormatAlpha(tmp) : SDL_DisplayFormat(tmp) ) == NULL ) + { + fprintf(stderr, "%s\n", SDL_GetError()); + SDL_FreeSurface(tmp); + return NULL; + } + + SDL_FreeSurface(tmp); + return ret; +} + +static void destroySDLSurface(void *p) +{ + assert( p != NULL ); + SDL_FreeSurface((SDL_Surface *)p); +} + +/* + * Prida obrazok do globalneho zoznamu obrazkov + * *file - nazov suboru v ktorom je obrazok ulozeny + * *name - nazov obrazku ( pouzije sa ako vyhadavaci retazec v zazanem ) + * alpha - 0 - nema alpha kanal | 1 - ma alpha kanal + */ +SDL_Surface* addImageData(char *file, int alpha, char *name, char *group) +{ + SDL_Surface *new; + + assert( file != NULL ); + assert( name != NULL ); + assert( group != NULL ); + + new = loadImage(file, alpha); + + addItemToStorage(listStorage, group, name, new ); + + printf("load image %s\n", file); + + return new; +} + +/* + * Vrati odkaz na image_data v globalnom zozname obrazkov + * a nazvom *s + */ +SDL_Surface* getImage(char *group, char *name) +{ + assert( group != NULL ); + assert( name != NULL ); + + return getItemFromStorage(listStorage, group, name); +} + +void delImage(char *group, char *name) +{ + assert( group != NULL ); + assert( name != NULL ); + + delItemFromStorage(listStorage, group, name, destroySDLSurface); +} + +void delAllImageInGroup(char *group) +{ + assert( group != NULL ); + + delAllItemFromStorage(listStorage, group, destroySDLSurface); +} + +void drawImage(SDL_Surface *p, int x,int y, int px, int py, int w, int h) +{ + static SDL_Surface *screen = NULL; + SDL_Rect dst_rect, src_rect; + + if( screen == NULL ) + { + screen = getSDL_Screen(); + } + + dst_rect.x = x; + dst_rect.y = y; + + src_rect.x = px; + src_rect.y = py; + src_rect.w = w; + src_rect.h = h; + + SDL_BlitSurface(p, &src_rect, screen, &dst_rect); +} + +/* + * Odstrani zoznam obrazkov z pamate + */ +void quitImageData() +{ + printf("quit image database..\n"); + destroyStorage(listStorage, destroySDLSurface); + isImageDataInit = FALSE; +} + diff --git a/src/client/image.h b/src/client/image.h new file mode 100755 index 0000000..5de6345 --- /dev/null +++ b/src/client/image.h @@ -0,0 +1,35 @@ + +#ifndef IMAGE_H + +#define IMAGE_H + +#include "client/image.h" +#include "client/interface.h" + +#define IMAGE_NO_ALPHA 0 +#define IMAGE_ALPHA 1 + +#define IMAGE_GROUP_BASE "base" +#define IMAGE_GROUP_EXTENDSIO "extension" +#define IMAGE_GROUP_USER "user" +#define IMAGE_GROUP_OTHER "other" + +/* +typedef struct image_struct +{ + char *name; + char *group; + SDL_Surface *image; +} image_data_t; +*/ + +extern bool_t isImageInicialized(); +extern void initImageData(); +extern SDL_Surface* addImageData(char *file, int alpha, char *name, char *group); +extern SDL_Surface* getImage(char *group, char *name); +extern void delImage(char *group, char *name); +extern void delAllImageInGroup(char *group); +extern void drawImage(SDL_Surface *p, int x,int y, int px, int py, int w, int h); +extern void quitImageData(); + +#endif diff --git a/src/client/interface.c b/src/client/interface.c new file mode 100755 index 0000000..570e3fd --- /dev/null +++ b/src/client/interface.c @@ -0,0 +1,256 @@ + +#include + +#include "base/main.h" +#include "base/myTimer.h" + +#include "client/interface.h" +#include "client/screen.h" + +//povrch okna +static SDL_Surface *screen; +//static SDL_Surface *my_surface; + +//casovac +static SDL_TimerID timer; + +//priznaky okna +static Uint32 g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF; + +static bool_t isInterfaceInit = FALSE; + +bool_t isInterfaceInicialized() +{ + return isInterfaceInit; +} + +static Uint32 TimerCallback(Uint32 interval, void *param) +{ + SDL_Event event; + event.type = SDL_USEREVENT; + event.user.code = USR_EVT_TIMER; + event.user.data1 = NULL; + event.user.data2 = NULL; + + SDL_PushEvent(&event); + + return interval; +} + +int initSDL() +{ + printf("init SDL..\n"); + + //inicializujem SDL + if( SDL_Init(SDL_SUBSYSTEMS) == -1 ) + { + fprintf(stderr, "%s\n", SDL_GetError()); + SDL_Quit(); + return -1; + } + + //inicializujem okno + //screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags); + screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags); + +/* + my_surface = SDL_CreateRGBSurface(screen->flags, WINDOW_SIZE_X, WINDOW_SIZE_Y, + screen->format->BitsPerPixel, + screen->format->Rmask, screen->format->Gmask, + screen->format->Bmask, screen->format->Amask); + + memset(my_surface->pixels, 128, 200); +*/ + if (screen == NULL ) + { + fprintf(stderr, "%s\n", SDL_GetError()); + SDL_Quit(); + return -1; + } + + SDL_WM_SetCaption(WINDOW_TITLE, NULL); + //SDL_ShowCursor(0); + SDL_EnableKeyRepeat(1,1); + timer = SDL_AddTimer(INTERVAL, TimerCallback, NULL); + + srand((unsigned)time(NULL)); + isInterfaceInit = TRUE; + + return 0; +} + +SDL_Surface* getSDL_Screen() +{ + //return my_surface; + return screen; +} + +void interfaceRefresh() +{ + //SDL_BlitSurface(my_surface, NULL, screen, NULL); + //SDL_UpdateRect(screen, 0, 0, screen->w, screen->h); + + //drawImage(my_surface, 200, 100, 0, 0, 200, 200); + SDL_Flip(screen); +} + +int toggleFullscreen() +{ + if(g_win_flags & SDL_FULLSCREEN) + g_win_flags &= ~SDL_FULLSCREEN; + else + g_win_flags |= SDL_FULLSCREEN; + + if( SDL_WM_ToggleFullScreen(screen) == 0 ) + { + fprintf(stderr, "Nemozem prepnut do fullscreenu!\n"); + + SDL_FreeSurface(screen); + screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, WIN_BPP, g_win_flags); + + if(screen == NULL) + { + fprintf(stderr, "Nemozem premnut do okna:%s\n", SDL_GetError()); + return 0; + } + } + + return 1; +} + +void getMousePosition(int *x, int *y) +{ + SDL_GetMouseState(x, y); +} + +int isMouseClicked() +{ + return SDL_GetMouseState(NULL,NULL) & SDL_BUTTON(SDL_BUTTON_LEFT); +} + +int isPessAnyKey() +{ + Uint8 *mapa; + int i; + + mapa = SDL_GetKeyState(NULL); + + for( i = SDLK_BACKSPACE ; i < SDLK_KP9 ; i++ ) + if( mapa[i] == SDL_PRESSED ) + { + return 1; + } + + return 0; +} + +void printPessAnyKey() +{ + Uint8 *mapa; + int i; + + mapa = SDL_GetKeyState(NULL); + + for( i = SDLK_BACKSPACE ; i < SDLK_KP9 ; i++ ) + if( mapa[i] == SDL_PRESSED ) + { + printf("press key with code : %d\n", i); + } + +} + + +/* +static void action_esc() +{ + if( strcmp(getScreen(), "world") == 0 ) + { + setScreen("analyze"); + } + + if( strcmp(getScreen(), "mainMenu") == 0 ) + { + quit(); + } + +} +*/ + +int eventAction() +{ + SDL_Event event; + + + while(SDL_WaitEvent(&event)) + { + switch(event.type) + { + case SDL_KEYDOWN: + switch(event.key.keysym.sym) + { +/* + case SDLK_ESCAPE: + action_esc(); + return 0; + break; +*/ + case SDLK_F1: + if(!toggleFullscreen())return 0; + break; + + default: + break; + } + break; + + case SDL_USEREVENT: + switch(event.user.code) + { + case USR_EVT_TIMER: + drawScreen(); + eventScreen(); + break; + + default: + break; + } + break; + + // Zmena velikosti okna + case SDL_VIDEORESIZE: + screen = SDL_SetVideoMode(event.resize.w,event.resize.h, WIN_BPP, g_win_flags); + + if(screen == NULL) + { + fprintf(stderr, "Nemozem zmenit rozlisene okna: %s\n",SDL_GetError()); + return 0; + } + break; + + // Pozadavek na ukonceni + case SDL_QUIT: + return -1; + break; + + default: + break; + } + } + + return 0; +} + +void eventSDL() +{ + while(1) + { + if ( eventAction() == -1 )return; + } +} + +void quitSDL() +{ + printf("quit SDL..\n"); + SDL_Quit(); + isInterfaceInit = FALSE; +} diff --git a/src/client/interface.h b/src/client/interface.h new file mode 100755 index 0000000..5804676 --- /dev/null +++ b/src/client/interface.h @@ -0,0 +1,40 @@ + +#ifndef INTERFACE_H + +#define INTERFACE_H + +#include +#include +#include + +#include +#include +#include +#include "base/main.h" + +/* +#include +*/ + +#define SDL_SUBSYSTEMS SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_DOUBLEBUF + +//napis na okne +#define WINDOW_TITLE "Tuxanci next generation " TUXANCI_NG_VERSION +#define WIN_BPP 0 +#define USR_EVT_TIMER 0 + +//interval pri ktorom sa spusta akcia(); +#define INTERVAL 50 + +extern bool_t isInterfaceInicialized(); +extern int initSDL(); +extern SDL_Surface* getSDL_Screen(); +extern void getMousePosition(int *x, int *y); +extern int isMouseClicked(); +extern int isPessAnyKey(); +extern void interfaceRefresh(); +extern void eventSDL(); +extern void quitSDL(); + + +#endif diff --git a/src/client/keytable.c b/src/client/keytable.c new file mode 100644 index 0000000..67811a7 --- /dev/null +++ b/src/client/keytable.c @@ -0,0 +1,136 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/homeDirector.h" +#include "base/textFile.h" + +#include "client/keytable.h" +#include "client/configFile.h" + +static textFile_t *keytableFile; +static textFile_t *keycontrolFile; +static int keytable[KEY_LENGTH]; + +static int getKeyCode(char *s) +{ + char str[STR_SIZE]; + char *find; + int i; + + sprintf(str, "keycode %s = ", s); + + for( i = 0 ; i < keytableFile->text->count ; i++ ) + { + char *line; + line = (char *) (keytableFile->text->list[i]); + + if( ( find = strstr(line, str) ) != NULL ) + { + int ret; + ret = atoi( strchr(line, '=')+1 ); + return ret; + } + } + + return -1; +} + +void initKeyTable() +{ + char path[STR_PATH_SIZE]; + char val[STR_SIZE]; +/* + int i; +*/ + sprintf(path, PATH_CONFIG "keytable.conf"); + keytableFile = loadTextFile(path); + + if( keytableFile == NULL ) + { + fprintf(stderr, "Don't load %s\n", path); + return; + } + + sprintf(path, "%s/keycontrol.conf", getHomeDirector()); + keycontrolFile = loadTextFile(path); + + if( keycontrolFile == NULL ) + { + fprintf(stderr, "Don't load %s\n", path); + fprintf(stderr, "I create %s\n", path); + + keycontrolFile = newTextFile(path); + } + + if( keycontrolFile == NULL ) + { + fprintf(stderr, "Don't create %s\n", path); + return; + } + + loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_MOVE_UP", val, STR_SIZE, "SDLK_UP"); + keytable[ KEY_TUX_RIGHT_MOVE_UP ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_MOVE_RIGHT", val, STR_SIZE, "SDLK_RIGHT"); + keytable[ KEY_TUX_RIGHT_MOVE_RIGHT ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_MOVE_LEFT", val, STR_SIZE, "SDLK_LEFT"); + keytable[ KEY_TUX_RIGHT_MOVE_LEFT ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_MOVE_DOWN", val, STR_SIZE, "SDLK_DOWN"); + keytable[ KEY_TUX_RIGHT_MOVE_DOWN ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_SHOOT", val, STR_SIZE, "SDLK_KP0"); + keytable[ KEY_TUX_RIGHT_SHOOT ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_SWITCH_WEAPON", val, STR_SIZE, "SDLK_KP1"); + keytable[ KEY_TUX_RIGHT_SWITCH_WEAPON] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_MOVE_UP", val, STR_SIZE, "SDLK_w"); + keytable[ KEY_TUX_LEFT_MOVE_UP ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_MOVE_RIGHT", val, STR_SIZE, "SDLK_d"); + keytable[ KEY_TUX_LEFT_MOVE_RIGHT ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_MOVE_LEFT", val, STR_SIZE, "SDLK_a"); + keytable[ KEY_TUX_LEFT_MOVE_LEFT ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_MOVE_DOWN", val, STR_SIZE, "SDLK_s"); + keytable[ KEY_TUX_LEFT_MOVE_DOWN ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_SHOOT", val, STR_SIZE, "SDLK_q"); + keytable[ KEY_TUX_LEFT_SHOOT ] = getKeyCode(val); + + loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_SWITCH_WEAPON", val, STR_SIZE, "SDLK_TAB"); + keytable[ KEY_TUX_LEFT_SWITCH_WEAPON ] = getKeyCode(val); + +/* + for( i = 0 ; i < KEY_LENGTH ; i++) + { + printf("keytable[%d] = %d\n", i, keytable[i]); + } +*/ + + saveTextFile(keycontrolFile); +} + +int getKey(int n) +{ + assert( n >= 0 && n < KEY_LENGTH ); +/* + printf("keytable[n] = %d\n", keytable[n]); +*/ + return keytable[n]; +} + +void quitKeyTable() +{ + destroyTextFile(keycontrolFile); + destroyTextFile(keytableFile); +} + diff --git a/src/client/keytable.h b/src/client/keytable.h new file mode 100644 index 0000000..80ca258 --- /dev/null +++ b/src/client/keytable.h @@ -0,0 +1,27 @@ + +#ifndef KEYTABLE_H + +#define KEYTABLE_H + +#define KEY_LENGTH 12 + +#define KEY_TUX_RIGHT_MOVE_UP 0 +#define KEY_TUX_RIGHT_MOVE_RIGHT 1 +#define KEY_TUX_RIGHT_MOVE_LEFT 2 +#define KEY_TUX_RIGHT_MOVE_DOWN 3 +#define KEY_TUX_RIGHT_SHOOT 4 +#define KEY_TUX_RIGHT_SWITCH_WEAPON 5 +#define KEY_TUX_LEFT_MOVE_UP 6 +#define KEY_TUX_LEFT_MOVE_RIGHT 7 +#define KEY_TUX_LEFT_MOVE_LEFT 8 +#define KEY_TUX_LEFT_MOVE_DOWN 9 +#define KEY_TUX_LEFT_SHOOT 10 +#define KEY_TUX_LEFT_SWITCH_WEAPON 11 + +extern void initKeyTable(); +extern int getKey(int n); +extern void quitKeyTable(); + +#endif + + diff --git a/src/client/language.c b/src/client/language.c new file mode 100644 index 0000000..5444253 --- /dev/null +++ b/src/client/language.c @@ -0,0 +1,157 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/homeDirector.h" +#include "base/textFile.h" + +#include "client/configFile.h" +#include "client/language.h" + +static textFile_t *languageFile; +static char fontFile[STR_FILE_NAME_SIZE]; +static int fontSize; + +char* getHead(textFile_t *ts) +{ + int i; + + for( i = 0 ; i < ts->text->count ; i++ ) + { + char *line; + line = (char *) (ts->text->list[i]); + + if( strncmp(line, "HEAD", 4) == 0 ) + { + return line; + } + } + + return NULL; +} + + +int initLanguage() +{ + textFile_t *languageTypeFile; + char path[STR_PATH_SIZE]; + char *lang; + char *head; + + sprintf(path, "%s/lang.conf", getHomeDirector()); + printf("open %s\n", path); + languageTypeFile = loadTextFile(path); + + if( languageTypeFile == NULL ) + { + fprintf(stderr, "Don't load %s\n", path); + fprintf(stderr, "I create %s\n", path); + + languageTypeFile = newTextFile(path); + addList(languageTypeFile->text, strdup("en") ); + saveTextFile(languageTypeFile); + } + + if( languageTypeFile == NULL ) + { + fprintf(stderr, "Don't create %s\n", path); + return -1; + } + + if( languageTypeFile->text->count == 0 ) + { + fprintf(stderr, "File %s empty\n", path); + return -1; + } + + lang = ((char *)languageTypeFile->text->list[0]); + printf("select lang %s\n", lang); + + sprintf(path, PATH_LANG "%s.lang", lang); + + destroyTextFile(languageTypeFile); + + accessExistFile(path); + languageFile = loadTextFile(path); + + if( languageFile == NULL ) + { + fprintf(stderr, "Don't load %s\n", path); + return -1; + } + + head = getHead(languageFile); + + if( head != NULL ) + { + char val[STR_SIZE]; + + if( getValue(head, "fontfile", val, STR_SIZE) == 0 ) + { + strcpy(fontFile, val); + } + else + { + fprintf(stderr, "fontfile in lang file %s not found\n", path); + return -1; + } + + if( getValue(head, "fontsize", val, STR_SIZE) == 0 ) + { + fontSize = atoi(val); + } + else + { + fprintf(stderr, "fontsize in lang file %s not found\n", path); + return -1; + } + } + else + { + fprintf(stderr, "head in lang file %s not found\n", path); + return -1; + } + + return 0; +} + +char* getMyText(char *key) +{ + int i; + int len; + + len = strlen(key); + + for( i = 0 ; i < languageFile->text->count ; i++ ) + { + char *line; + line = (char *) (languageFile->text->list[i]); + + if( strncmp(key, line, len) == 0 ) + { + return line+strlen(key)+1; + } + } + + return NULL; +} + +char* getLanguageFont() +{ + return fontFile; +} + +int getLanguageSize() +{ + return fontSize; +} + +void quitLanguage() +{ + destroyTextFile(languageFile); +} + diff --git a/src/client/language.h b/src/client/language.h new file mode 100644 index 0000000..23fc1a4 --- /dev/null +++ b/src/client/language.h @@ -0,0 +1,12 @@ + +#ifndef LANGUAGE_H + +#define LANGUAGE_H + +extern int initLanguage(); +extern char* getMyText(char *key); +extern char* getLanguageFont(); +extern int getLanguageSize(); +extern void quitLanguage(); + +#endif diff --git a/src/client/layer.c b/src/client/layer.c new file mode 100755 index 0000000..707184b --- /dev/null +++ b/src/client/layer.c @@ -0,0 +1,155 @@ + +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" + +#include "client/interface.h" +#include "client/layer.h" +#include "client/image.h" + +#include "screen/screen_world.h" + +static list_t *listLayer; + +static bool_t isLayerInit = FALSE; + +bool_t isLayerInicialized() +{ + return isLayerInit; +} + +/* + * Inicializuje globalny zoznam vsrtiev + */ +void initLayer() +{ + listLayer = newList(); +} + +/* + * Prida novu polozku do zoznamu vrstiev + * *img - obrazok, ktory obsahuje polozka + * x y w h - suradnca kam sa obrazok vykresli a jeho rozmery + * px py pw ph - suradnica casti obrazka *img ktora sa ma zobrazit + * layer - vrstva na ktorej sa zobrazi obrazok + */ +void addLayer(SDL_Surface *img, + int x,int y, int px,int py, + int w,int h, int player) +{ + layer_t *new; + layer_t *this; + int i; + + assert( img != NULL ); + + new = malloc( sizeof(layer_t) ); + new->x = x; + new->y = y; + new->w = w; + new->h = h; + new->px = px; + new->py = py; + new->layer = player; + new->image = img; + + if( listLayer->count == 0 ) + { + addList(listLayer, new); + return; + } + + i = 0; + this = (layer_t *)listLayer->list[0]; + + //vyhladaj svoju vrstvu + while( i < listLayer->count && this->layer < new->layer ) + { + this = (layer_t *)listLayer->list[++i]; + } + + //porovnaj s vyskami na tvojej vrstve, + //ak neexistuje, rovno to tam hod + this = (layer_t *)listLayer->list[i]; + + while( i < listLayer->count && this->y+this->h < new->y+new->h && new->layer == this->layer ) + { + this = (layer_t *)listLayer->list[++i]; + } + + insList(listLayer, i, new); + isLayerInit = TRUE; +} + +/* + * Vykresli vsetky polozky na obrazovku + * poukadane podla vrstvy a suradnice Y+H + * po dokresleni sa zoznam uvolni. + */ +void drawLayer() +{ + layer_t *this; + int i; + + for( i = 0 ; i < listLayer->count ; i++ ) + { + this = (layer_t *)listLayer->list[i]; + + drawImage(this->image, + this->x, this->y, + this->px, this->py, + this->w, this->h); + } + + destroyListItem(listLayer, free); + listLayer = newList(); +} + +void drawLayerCenter(int x, int y) +{ + layer_t *this; + int screen_x, screen_y; + int i; + + getCenterScreen(&screen_x, &screen_y, x, y); + + for( i = 0 ; i < listLayer->count ; i++ ) + { + this = (layer_t *)listLayer->list[i]; + + if( (this->x - screen_x) + this->w < 0 || (this->x - screen_x) > WINDOW_SIZE_X || + (this->y - screen_y) + this->h < 0 || (this->y - screen_y) > WINDOW_SIZE_Y ) + { + continue; + } + + drawImage(this->image, + this->x - screen_x, this->y - screen_y, + this->px, this->py, + this->w, this->h); + + //printf("%d %d\n", this->x - screen_x, this->y - screen_y); + } + + destroyListItem(listLayer, free); + listLayer = newList(); +} + +void flushLayer() +{ + destroyListItem(listLayer, free); + listLayer = newList(); +} + +/* + * Odstrani zoznam vrtsiev z pamete. + */ +void quitLayer() +{ + destroyListItem(listLayer, free); + isLayerInit = FALSE; +} + diff --git a/src/client/layer.h b/src/client/layer.h new file mode 100755 index 0000000..3564ef8 --- /dev/null +++ b/src/client/layer.h @@ -0,0 +1,53 @@ + +#ifndef MY_LAYER_H + +#define MY_LAYER_H + +#include "base/main.h" + +typedef struct layer_str +{ + int x; //suradnice obrazka na obrazovke + int y; + + int w; //rozmery obrazka na obrazovke + int h; + + int px; //suradnice casti surfrace + int py; + + int pw; //rozmeri casti surfrace + int ph; + + int layer; + + SDL_Surface *image; //dane surfrace +} layer_t; + +extern bool_t isLayerInicialized(); +extern void initLayer(); + +/* + * Prida obrazok na vykreslenie do zoznamu + * *img - ponter na urfrace obrazka + * x,y - suradnice kam sa ma obrazok nakreslit + * w,h - rozmeri obrazka + * px,py - suradnice casti *img z ktorej sa bude obrazok vykreslovat + * ph,ph - rozmeri casti obrazka *img z ktorej sa bude obrazok vykreslovat + * layer - layer, na ktorej bude obrazok + * (0 - pod tuxancami | 1 - na tej istej ako tuxanci | 2 - nad tuxancami) + */ + +extern void addLayer(SDL_Surface *img, + int x,int y, int px,int py, + int w,int h, int player); + +/* + * Vykresli zoznam na obrazovku + */ +extern void drawLayer(); +extern void drawLayerCenter(int x, int y); +extern void flushLayer(); +extern void quitLayer(); + +#endif diff --git a/src/client/panel.c b/src/client/panel.c new file mode 100644 index 0000000..af9cccb --- /dev/null +++ b/src/client/panel.c @@ -0,0 +1,190 @@ + +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" + +#include "client/interface.h" +#include "client/image.h" +#include "client/panel.h" +#include "client/font.h" + +static SDL_Surface *g_panel; +static SDL_Surface *g_shot; +static SDL_Surface *g_icon[ITEM_COUNT]; +static SDL_Surface *g_bonus; + +static bool_t isPanelInit = FALSE; + +bool_t isPanelInicialized() +{ + return isPanelInit; +} + +void initPanel() +{ + assert( isImageInicialized() == TRUE ); + assert( isInterfaceInicialized() == TRUE ); + + g_panel = addImageData("panel.png", IMAGE_ALPHA, "panel", IMAGE_GROUP_BASE); + g_shot = addImageData("panel_shot.png", IMAGE_ALPHA, "panel_image_shot", IMAGE_GROUP_BASE); + g_bonus = addImageData("graf.bonus.png", IMAGE_ALPHA, "panel_graf_bonus", IMAGE_GROUP_BASE); + + g_icon[GUN_SIMPLE] = addImageData("icon.gun.simple.png", IMAGE_ALPHA, "panel_gun", IMAGE_GROUP_BASE); + g_icon[GUN_DUAL_SIMPLE] = addImageData("icon.gun.dual.simple.png", IMAGE_ALPHA, "panel_dual", IMAGE_GROUP_BASE); + g_icon[GUN_TOMMY] = addImageData("icon.gun.tommy.png", IMAGE_ALPHA, "panel_tommy", IMAGE_GROUP_BASE); + g_icon[GUN_SCATTER] = addImageData("icon.gun.scatter.png", IMAGE_ALPHA, "panel_scatter", IMAGE_GROUP_BASE); + g_icon[GUN_LASSER] = addImageData("icon.gun.lasser.png", IMAGE_ALPHA, "panel_lasser", IMAGE_GROUP_BASE); + g_icon[GUN_MINE] = addImageData("icon.gun.mine.png", IMAGE_ALPHA, "panel_mine", IMAGE_GROUP_BASE); + g_icon[GUN_BOMBBALL] = addImageData("icon.gun.bombball.png", IMAGE_ALPHA, "panel_bombball", IMAGE_GROUP_BASE); + + g_icon[BONUS_SPEED] = addImageData("icon.bonus.speed.png", IMAGE_ALPHA, "panel_speed", IMAGE_GROUP_BASE); + g_icon[BONUS_SHOT] = addImageData("icon.bonus.shot.png", IMAGE_ALPHA, "panel_shot", IMAGE_GROUP_BASE); + g_icon[BONUS_TELEPORT] = addImageData("icon.bonus.teleport.png", IMAGE_ALPHA, "panel_teleport", IMAGE_GROUP_BASE); + g_icon[BONUS_GHOST] = addImageData("icon.bonus.ghost.png", IMAGE_ALPHA, "panel_ghost", IMAGE_GROUP_BASE); + g_icon[BONUS_4X] = addImageData("icon.bonus.4x.png", IMAGE_ALPHA, "panel_4x", IMAGE_GROUP_BASE); + g_icon[BONUS_HIDDEN] = addImageData("icon.bonus.hidden.png", IMAGE_ALPHA, "panel_hidden", IMAGE_GROUP_BASE); + + isPanelInit = TRUE; +} + +static void setScoreline(list_t *listTux, char *str) +{ + tux_t *thisTux; + int i; + + assert( listTux != NULL ); + assert( str != NULL ); + + strcpy(str, ""); + + for( i = 0 ; i < listTux->count ; i++ ) + { + char num[STR_SIZE]; + + thisTux = (tux_t *)listTux->list[i]; + assert( thisTux != NULL ); + + sprintf(num, "%s%d", ( i == 0 ? "" : " : " ), thisTux->score); + strcat(str, num); + } +} + +static void drawScore(list_t *listTux) +{ + char strScoreLine[STR_SIZE]; + int w, h; + + assert( listTux != NULL ); + + setScoreline(listTux, strScoreLine); + getTextSize(strScoreLine, &w , &h); + drawFont(strScoreLine, PANEL_SCORE_LOCATION_X, PANEL_SCORE_LOCATION_Y, COLOR_WHITE); +} + +static void drawShot(int n, int x, int y) +{ + drawImage(g_shot, x, y,n * PANEL_SHOT_WIDTH, 0, PANEL_SHOT_WIDTH, PANEL_SHOT_HEIGHT); +} + +static void drawShotInfo(tux_t *tux,int x, int y) +{ + int i; + + if( tux->pickup_time > 0 ) + { + for( i = 0 ; i < tux->pickup_time / 3 ; i++ ) + { + drawShot(PANEL_SHOT_LINE, x + i * PANEL_SHOT_WIDTH, y); + } + + return; + } + + for( i = 0 ; i < GUN_MAX_SHOT ; i++ ) + { + if( tux->shot[tux->gun] > i ) + { + drawShot(PANEL_SHOT_FILL, x + i * ( PANEL_SHOT_WIDTH + 2 ), y); + } + else + { + drawShot(PANEL_SHOT_EMPTY, x + i * ( PANEL_SHOT_WIDTH + 2 ) , y); + } + } +} + +static void drawGunInfo(tux_t *tux,int x, int y) +{ + drawImage(g_icon[ tux->gun ], x, y, 0, 0, + g_icon[ tux->gun ]->w, g_icon[ tux->gun ]->h); +} + +static void drawBonusInfo(tux_t *tux,int x, int y) +{ + drawImage(g_icon[ tux->bonus ], x, y, 0, 0, g_icon[ tux->bonus ]->w, g_icon[ tux->bonus ]->h); +} + +static void drawGrafBonus(tux_t *tux,int x, int y) +{ + int offset; + + offset = tux->bonus_time / 5; + drawImage(g_bonus, x, y, 0, 0, g_bonus->w, g_bonus->h/2); + drawImage(g_bonus, x+1, y, 0, g_bonus->h/2, offset, g_bonus->h/2); +} + +void drawPanel(list_t *listTux) +{ + int i; + + assert( listTux != NULL ); + + drawImage(g_panel, PANEL_LOCATION_X, PANEL_LOCATION_Y, 0, 0, g_panel->w, g_panel->h); + drawScore(listTux); + + for( i = 0 ; i < listTux->count ; i++ ) + { + tux_t *thisTux; + int x, y; + + thisTux = (tux_t *)listTux->list[i]; + assert( thisTux != NULL ); + + x = PANEL_LOCATION_X; + y = PANEL_LOCATION_Y; + + switch( i ) + { + case 0 : + drawShotInfo(thisTux, x+8, y+34); + drawGunInfo(thisTux, x+130, y+30); + + if( thisTux->bonus != BONUS_NONE ) + { + drawGrafBonus(thisTux,x+200, y+50); + drawBonusInfo(thisTux,x+200, y+30); + } + break; + + case 1 : + drawShotInfo(thisTux, x+740, y+34); + drawGunInfo(thisTux, x+620, y+30); + + if( thisTux->bonus != BONUS_NONE ) + { + drawGrafBonus(thisTux,x+460, y+50); + drawBonusInfo(thisTux,x+460, y+30); + } + break; + } + } +} + +void quitPanel() +{ + isPanelInit = FALSE; +} + diff --git a/src/client/panel.h b/src/client/panel.h new file mode 100644 index 0000000..144ea02 --- /dev/null +++ b/src/client/panel.h @@ -0,0 +1,27 @@ + +#ifndef PANEL_H + +#define PANEL_H + +#include "base/main.h" +#include "base/list.h" + +#define PANEL_SCORE_LOCATION_X ( WINDOW_SIZE_X / 2 - w / 2 ) +#define PANEL_SCORE_LOCATION_Y ( WINDOW_SIZE_Y - 40 ) + +#define PANEL_SHOT_WIDTH 4 +#define PANEL_SHOT_HEIGHT 8 + +#define PANEL_LOCATION_X 0 +#define PANEL_LOCATION_Y (WINDOW_SIZE_Y-g_panel->h) + +#define PANEL_SHOT_FILL 0 +#define PANEL_SHOT_EMPTY 1 +#define PANEL_SHOT_LINE 2 + +extern bool_t isPanelInicialized(); +extern void initPanel(); +extern void drawPanel(list_t *listTux); +extern void quitPanel(); + +#endif diff --git a/src/client/screen.c b/src/client/screen.c new file mode 100644 index 0000000..730476e --- /dev/null +++ b/src/client/screen.c @@ -0,0 +1,137 @@ + +#include +#include + +#include "base/main.h" +#include "base/list.h" + +#include "client/interface.h" +#include "client/screen.h" +#include "client/layer.h" + +static screen_t *currentScreen; +static list_t *listScreen; + +static bool_t isScreenInit = FALSE; + +bool_t isScreenInicialized() +{ + return isScreenInit; +} + +screen_t* newScreen(char *name, + void (*fce_start)(), void (*fce_event)(), + void (*fce_draw)(), void (*fce_stop)()) +{ + screen_t *new; + + assert( name != NULL ); + assert( fce_start != NULL ); + assert( fce_event != NULL ); + assert( fce_draw != NULL ); + assert( fce_stop != NULL ); + + new = malloc( sizeof(screen_t) ); + new->name = strdup(name); + new->fce_start = fce_start; + new->fce_event = fce_event; + new->fce_draw = fce_draw; + new->fce_stop = fce_stop; + + return new; +} + +void destroyScreen(screen_t *p) +{ + assert( p != NULL ); + + free(p->name); + free(p); +} + +void registerScreen(screen_t *p) +{ + assert( p != NULL ); + + printf("register screen %s..\n", p->name); + addList(listScreen, p); +} + +void initScreen() +{ + listScreen = newList(); + currentScreen = NULL; + isScreenInit = TRUE; +} + +void setScreen(char *name) +{ + int i; + screen_t *this; + + assert( name != NULL ); + + if( currentScreen != NULL && + strcmp(currentScreen->name, name) == 0 ) + { + return; + } + + printf("switch screen %s..\n", name); + + for( i = 0 ; i < listScreen->count ; i++ ) + { + this = (screen_t *) (listScreen->list[i]); + assert( this != NULL ); + + if( strcmp(name, this->name) == 0 ) + { + flushLayer(); + + if( currentScreen != NULL ) + { + printf("stop screen %s..\n", currentScreen->name); + currentScreen->fce_stop(); + } + + currentScreen = this; + printf("start screen %s..\n", currentScreen->name); + currentScreen->fce_start(); + + return; + } + } + + assert( ! "screen sa nenasiel !" ); +} + +char* getScreen() +{ + assert( currentScreen != NULL ); + return currentScreen->name; +} + +void drawScreen() +{ + assert( currentScreen != NULL ); + + currentScreen->fce_draw(); + + interfaceRefresh(); +} + +void eventScreen() +{ + assert( currentScreen != NULL ); + + currentScreen->fce_event(); +} + +void quitScreen() +{ + assert( listScreen != NULL ); + + destroyListItem(listScreen, destroyScreen); + isScreenInit = FALSE; +} + diff --git a/src/client/screen.h b/src/client/screen.h new file mode 100644 index 0000000..8049147 --- /dev/null +++ b/src/client/screen.h @@ -0,0 +1,32 @@ + +#ifndef SCREEN_H + +#define SCREEN_H + +#include "base/main.h" + +typedef struct screen_struct +{ + char *name; + void (*fce_start)(); + void (*fce_event)(); + void (*fce_draw)(); + void (*fce_stop)(); +} screen_t; + +extern bool_t isScreenInicialized(); + +extern screen_t* newScreen(char *name, + void (*fce_start)(), void (*fce_event)(), + void (*fce_draw)(), void (*fce_stop)()); + +extern void destroyScreen(screen_t *p); +extern void registerScreen(screen_t *p); +extern void initScreen(); +extern void setScreen(char *name); +extern char* getScreen(); +extern void drawScreen(); +extern void eventScreen(); +extern void quitScreen(); + +#endif diff --git a/src/client/term.c b/src/client/term.c new file mode 100644 index 0000000..3357f0b --- /dev/null +++ b/src/client/term.c @@ -0,0 +1,106 @@ + +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/myTimer.h" + +#include "client/interface.h" +#include "client/term.h" +#include "client/font.h" + +typedef struct struct_term_t +{ + char *text; + my_time_t delTime; +} term_t; + +static list_t *listText; + +static term_t* newTermItem(char *s) +{ + term_t *new; + char *line; + int len; + + assert( s != NULL ); + + line = strdup(s); + len = strlen(line); + line[len-1] = '\0'; + + new = malloc( sizeof(term_t) ); + memset(new, 0, sizeof(term_t)); + + new->text = line; + new->delTime = getMyTime() + TERM_DEL_TEXT_TIMEOUT; + + return new; +} + +static void destroyTermItem(term_t *p) +{ + assert( p != NULL ); + + free(p->text); + free(p); +} + +void initTerm() +{ + listText = newList(); +} + +void drawTerm() +{ + int i; + + for( i = 0 ; i < listText->count ; i++ ) + { + char *line; + + line = (char *)( (term_t *)listText->list[i] )->text; + drawFont(line, 10, 10 + i*20, COLOR_WHITE); + } +} + +void eventTerm() +{ + my_time_t currentTime; + int i; + + currentTime = getMyTime(); + + + for( i = 0 ; i < listText->count ; i++ ) + { + my_time_t thisTime; + + thisTime = ( (term_t *)listText->list[i] )->delTime; + + if( currentTime > thisTime ) + { + delListItem(listText, i, destroyTermItem); + i--; + } + } + +} + +void appendTextInTerm(char *s) +{ + + addList(listText, newTermItem(s) ); + + if( listText->count > TERM_MAX_LINES ) + { + delListItem(listText, 0, destroyTermItem); + } +} + +void quitTerm() +{ + assert( listText != NULL ); + destroyListItem(listText, destroyTermItem); +} diff --git a/src/client/term.h b/src/client/term.h new file mode 100644 index 0000000..548fda6 --- /dev/null +++ b/src/client/term.h @@ -0,0 +1,17 @@ + +#ifndef TERM_H + +#define TERM_H + +#include "base/main.h" + +#define TERM_MAX_LINES 10 +#define TERM_DEL_TEXT_TIMEOUT 2000 + +extern void initTerm(); +extern void drawTerm(); +extern void eventTerm(); +extern void appendTextInTerm(char *s); +extern void quitTerm(); + +#endif diff --git a/src/configFile.c b/src/configFile.c deleted file mode 100644 index 1c19e3f..0000000 --- a/src/configFile.c +++ /dev/null @@ -1,136 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "configFile.h" - -int isYesOrNO(char *s) -{ - if( s[0] == 'Y' || s[0] == 'y' )return 1; - return 0; -} - -char* getYesOrNo(int n) -{ - return ( n != 0 ? "YES" : "NO" ); -} - -int getValue(char *line, char *env, char *val, int len) -{ - char *offset_env; - char *offset_val_begin; - char *offset_val_end; - char clone_env[STR_SIZE]; - int val_len; - - strcpy(clone_env, " "); - strcat(clone_env, env); - - offset_env = strstr(line, clone_env); - if( offset_env == NULL )return -1; - - offset_val_begin = strchr(offset_env, '"'); - if( offset_val_begin == NULL )return -1; - - offset_val_end = strchr(offset_val_begin+1, '"'); - if( offset_val_end == NULL )return -1; - - val_len = (int)(offset_val_end - ( offset_val_begin + 1) ); - if( val_len > len - 1 ) val_len = len - 1; - - memset(val, 0, len); - memcpy(val, offset_val_begin+1, val_len); - - return 0; -} - -char* setValue(char *line, char *env, char *val) -{ - char *offset_env; - char *offset_val_begin; - char *offset_val_end; - char clone_env[STR_SIZE]; - char clone[STR_SIZE]; - - //strcpy(clone_env, " "); - strcpy(clone_env, env); - - offset_env = strstr(line, clone_env); - if( offset_env == NULL )return NULL; - - offset_val_begin = strchr(offset_env, '"'); - if( offset_val_begin == NULL )return NULL; - - offset_val_end = strchr(offset_val_begin+1, '"'); - if( offset_val_end == NULL )return NULL; - - memset(clone, 0, STR_SIZE); - strncpy(clone, line, (int) ( (offset_val_begin - line) + 1 ) ); - strcat(clone, val); - strcat(clone, offset_val_end); - - return strdup( clone ); -} - -int getValueInConfigFile(textFile_t *textFile, char *env, char *val, int len) -{ - int i; - char *line; - - for( i = 0 ; i < textFile->text->count ; i++ ) - { - line = (char *) (textFile->text->list[i]); - - if( strstr(line, env) != NULL ) - { - return getValue(line, env, val, len); - } - } - - return -1; -} - -int setValueInConfigFile(textFile_t *textFile, char *env, char *val) -{ - int i; - char *line; - char *ret; - - for( i = 0 ; i < textFile->text->count ; i++ ) - { - line = (char *) (textFile->text->list[i]); - - if( strstr(line, env) != NULL ) - { - ret = setValue(line, env, val); - - if( ret != NULL ) - { - delListItem(textFile->text, i, free); - insList(textFile->text, i, ret); - - return 0; - } - } - } - - return -1; -} - -void loadValueFromConfigFile(textFile_t *textFile, char *env, char *val, int len, char *butVal) -{ - strcpy(val, butVal); - - if( getValueInConfigFile(textFile, env, val, len) != 0 ) - { - char line[STR_SIZE]; - - sprintf(line, " %s=\"%s\"", env, butVal); - addList(textFile->text, strdup(line) ); - - printf("Add line \"%s\" in config file.\n", line); - } -} diff --git a/src/director.c b/src/director.c deleted file mode 100755 index 6d2fe8c..0000000 --- a/src/director.c +++ /dev/null @@ -1,66 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "string_length.h" - -typedef struct director_struct -{ - char *path; - list_t *list; -} director_t; - - -director_t* loadDirector(char *s) -{ - director_t *new; - DIR *dir; - struct dirent *item; - char path[STR_PATH_SIZE]; - - assert( s != 0 ); - - new = malloc( sizeof(director_t) ); - memset(new, 0, sizeof(director_t) ); - - new->list = newList(); - - if( s[0] == '/' ) - { - strcpy(path, s); - } - else - { - getcwd(path, STR_PATH_SIZE); - strcat(path, s+1); - } - - new->path = strdup(path); - - dir = opendir(new->path); - - while( ( item = readdir(dir) ) != NULL ) - addList(new->list, strdup(item->d_name) ); - - closedir(dir); - - return new; -} - -void destroyDirector(director_t *p) -{ - assert( p != NULL ); - - destroyListItem(p->list, free); - free(p->path); - free(p); -} - - diff --git a/src/fake_audio.c b/src/fake_audio.c deleted file mode 100644 index 7bccbde..0000000 --- a/src/fake_audio.c +++ /dev/null @@ -1,89 +0,0 @@ - -#include "main.h" -#include "fake_audio.h" - -bool_t isAudioInicialized() -{ - return TRUE; -} - -void initAudio() -{ -} - -void quitAudio() -{ -} - -bool_t isSoundInicialized() -{ - return TRUE; -} - -void initSound() -{ -} - -void addSound(char *file, char *name, int group) -{ -} - -void playSound(char *name, int group) -{ -} - -void setSoundActive(bool_t n) -{ -} - -bool_t isSoundActive() -{ - return FALSE; -} - -void quitSound() -{ -} - -bool_t isMusicInicialized() -{ - return FALSE; -} - -void initMusic() -{ -} - -void addMusic(char *file, char *name, int group) -{ -} - -void stopMusic() -{ -} - -void playMusic(char *name, int group) -{ -} - -void setMusicActive(bool_t n) -{ -} - -bool_t isMusicActive() -{ - return FALSE; -} - -char* getCurrentMusic() -{ - return "no_sound"; -} - -void delAllMusicInGroup(int group) -{ -} - -void quitMusic() -{ -} diff --git a/src/font.c b/src/font.c deleted file mode 100755 index 0f1c200..0000000 --- a/src/font.c +++ /dev/null @@ -1,101 +0,0 @@ - -#include "main.h" -#include "interface.h" -#include "string_length.h" -#include "font.h" -#include "path.h" - -static TTF_Font *g_font; -static int fontSize; - -static bool_t isFontInit = FALSE; - -bool_t isFontInicialized() -{ - return isFontInit; -} - -/* - * Inicialuzuje font. - * nacita font zo suboru *file o velkosti size - * Automaticky sa styk nastavy ako TTF_STYLE_BOLD - */ -void initFont(char *file,int size) -{ - char str[STR_PATH_SIZE]; - - assert( file != NULL ); - assert( size > 0 ); - - assert( isInterfaceInicialized() == TRUE ); - - sprintf(str, PATH_FONT "%s", file); - - if( TTF_Init() == -1 ) - { - fprintf(stderr, "%s\n", SDL_GetError()); - return; - } - - accessExistFile(str); - - g_font = TTF_OpenFont(str, size); - TTF_SetFontStyle(g_font, TTF_STYLE_NORMAL); - - fontSize = size; - - printf("init font.. (%s)\n", file); - isFontInit = TRUE; -} - -/* - * Zobrazi text *s na suradnicu x y s farbou RGB - */ -void drawFont(char *s, int x, int y, int r, int g, int b) -{ - SDL_Rect dst_rect; - SDL_Surface *text; - SDL_Surface *p; - SDL_Color farba_pisma = {r, g, b, 0}; - - assert( s != NULL ); - - p = getSDL_Screen(); - - text = TTF_RenderUTF8_Blended(g_font, s, farba_pisma); - - dst_rect.x = x; - dst_rect.y = y; - - SDL_BlitSurface(text, NULL, p, &dst_rect); - SDL_FreeSurface(text); -} - -/* - * Vrati velkost daneho fontu. - */ -int getFontSize() -{ - return fontSize; -} - -void getTextSize(char *s, int *w, int *h) -{ - assert( s != NULL ); - assert( w != NULL ); - assert( h != NULL ); - - TTF_SizeUTF8(g_font, s, w, h); -} - -/* - * Uvolni font z pamete. - */ -void quitFont() -{ - TTF_CloseFont(g_font); - TTF_Quit(); - - printf("quit font\n"); - isFontInit = FALSE; -} diff --git a/src/game.c b/src/game.c deleted file mode 100644 index 0c4903f..0000000 --- a/src/game.c +++ /dev/null @@ -1,115 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "main.h" -#include "tux.h" - -#include "font.h" -#include "keytable.h" -#include "language.h" -#include "image.h" -#include "layer.h" -#include "interface.h" -#include "screen.h" -#include "screen_world.h" -#include "screen_mainMenu.h" -#include "screen_analyze.h" -#include "screen_setting.h" -#include "screen_gameType.h" -#include "screen_choiceArena.h" -#include "screen_table.h" -#include "screen_credits.h" -#include "homeDirector.h" -#include "item.h" -#include "shot.h" -#include "arenaFile.h" -#include "panel.h" -#include "audio.h" -#include "sound.h" -#include "music.h" -#include "fake_audio.h" - -static void initGame() -{ - createHomeDirector(); - - if( initLanguage() == -1 ) - { - printf("fatal error !\n"); - exit(-1); - } - - initSDL(); - - initLayer(); - initFont(getLanguageFont(), getLanguageSize()); - initKeyTable(); - initImageData(); - initAudio(); - initSound(); - initMusic(); - initScreen(); - initArenaFile(); - initTux(); - initItem(); - initShot(); - initPanel(); - initWorld(); - initScreenMainMenu(); - initScreenAnalyze(); - initScreenSetting(); - initScreenGameType(); - initScreenChoiceArena(); - initScreenCredits(); - initScreenTable(); -} - -void quitGame() -{ - quitSDL(); - quitLayer(); - quitFont(); - quitKeyTable(); - quitLanguage(); - quitScreen(); - quitArenaFile(); - quitItem(); - quitTux(); - quitShot(); - quitPanel(); - quitWorld(); - quitImageData(); - quitSound(); - quitMusic(); - quitAudio(); - quitScreenMainMenu(); - quitScreenAnalyze(); - quitScreenSetting(); - quitScreenGameType(); - quitScreenChoiceArena(); - quitScreenCredits(); - quitScreenTable(); - - destroyList(listHelp); - - printf("quit..\n"); - - exit(0); -} - -void startGame() -{ - initGame(); - - setScreen("mainMenu"); - - eventSDL(); - quitGame(); -} diff --git a/src/gun.c b/src/gun.c deleted file mode 100644 index dceec63..0000000 --- a/src/gun.c +++ /dev/null @@ -1,350 +0,0 @@ -// -#include -#include -#include - -#include "main.h" -#include "arena.h" -#include "tux.h" -#include "shot.h" -#include "myTimer.h" -#include "item.h" -#include "gun.h" -#include "proto.h" -#include "net_multiplayer.h" - -#ifndef PUBLIC_SERVER -#include "sound.h" -#include "fake_audio.h" -#include "screen_world.h" -#include "interface.h" -#include "term.h" -#endif - -#ifdef PUBLIC_SERVER -#include "publicServer.h" -#endif - -static void modificiationCopuse(int courser, int right_x, int right_y, int *dest_x, int *dest_y) -{ - assert( dest_x != NULL ); - assert( dest_y != NULL ); - - switch( courser ) - { - case TUX_UP : - *dest_x = right_y; - *dest_y = -right_x; - break; - case TUX_RIGHT : - *dest_x = right_x; - *dest_y = right_y; - break; - case TUX_LEFT : - *dest_x = -right_x; - *dest_y = right_y; - break; - case TUX_DOWN : - *dest_x = right_y; - *dest_y = right_x; - break; - default : - assert( ! "V premennej courser je zly smer !" ); - break; - } -} - -static void addShotTrivial(tux_t *tux, int x, int y, int px, int py, int gun) -{ - int dest_x = 0, dest_y = 0; - int dest_px = 0, dest_py = 0; - shot_t *shot; - - modificiationCopuse(tux->position, px, py, &dest_px, &dest_py); - modificiationCopuse(tux->position, x, y, &dest_x, &dest_y); - - if( gun == GUN_LASSER ) - { - switch( tux->position ) - { - case TUX_UP : - dest_y -= GUN_LASSER_HORIZONTAL; - break; - case TUX_RIGHT : - break; - case TUX_LEFT : - dest_x -= GUN_LASSER_HORIZONTAL; - break; - case TUX_DOWN : - break; - } - } - - shot = newShot(tux->x + dest_x, tux->y + dest_y, dest_px, dest_py, gun, tux->id); - -/* - if( isFreeSpace( getCurrentArena(), shot->x, shot->y, shot->w, shot->h ) == 0 ) - { - destroyShot(shot); - return; - } -*/ - //addList( getCurrentArena()->listShot, shot ); - addObjectToSpace(getCurrentArena()->spaceShot, shot); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - client_t *thisClient; - - thisClient = getClientFromTux(tux); - - if( thisClient != NULL ) - { - proto_send_shot_server(PROTO_SEND_ONE, thisClient, shot); - } - - proto_send_shot_server(PROTO_SEND_ALL_SEES_TUX, thisClient, shot); - } - -} - -static void addShot(tux_t *tux,int x, int y, int px, int py) -{ - int gun; - - if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) - { - return; - } - - if( tux->gun == GUN_BOMBBALL ) - { - gun = GUN_BOMBBALL; - } - else if( tux->gun == GUN_LASSER ) - { - gun = GUN_LASSER; - } - else - { - gun = GUN_SIMPLE; - } - - if( tux->bonus == BONUS_4X ) - { - int zal; - int i; - - zal = tux->position; - - for( i = 0 ; i < 4 ; i++ ) - { - switch( i ) - { - case 0 : tux->position = TUX_UP; break; - case 1 : tux->position = TUX_RIGHT; break; - case 2 : tux->position = TUX_LEFT; break; - case 3 : tux->position = TUX_DOWN; break; - } - - addShotTrivial(tux, x, y, px, py, gun); - } - - tux->position = zal; - } - else - { - addShotTrivial(tux, x, y, px, py, gun); - } -} - -static void shotInGunSimpe(tux_t *tux) -{ - addShot(tux, 0, 0, +5, 0); -} - -static void shotInGunDualSimpe(tux_t *tux) -{ - addShot(tux, 0, -10, +6, 0); - addShot(tux, 0, +10, +6, 0); -} - -static void shotInGunScatter(tux_t *tux) -{ - int i; - - for( i = -2 ; i <= +2 ; i++ ) - { - addShot(tux, 0, 0, +8, i); - } -} - -static void timer_addShotTimer(void *p) -{ - tux_t *tux; - int id; - int gun; - - id = * ((int *)p); - free(p); - - tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); - - if( tux == NULL )return; - - if( tux->status != TUX_STATUS_ALIVE )return; - - gun = tux->gun; - tux->gun = GUN_SIMPLE; - addShot(tux, 0, 0, +6, 0); - tux->gun = gun; -} - -static void shotInGunTommy(tux_t *tux) -{ - int i; - - for( i = 0 ; i < 10 ; i++ ) - { - addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_addShotTimer, newInt(tux->id), i * 100 ); - } -} - -static void timer_addLaserTimer(void *p) -{ - tux_t *tux; - int id; - int gun; - - id = * ((int *)p); - free(p); - - tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); - - if( tux == NULL )return; - - if( tux->status != TUX_STATUS_ALIVE )return; - - gun = tux->gun; - tux->gun = GUN_LASSER; - - addShot(tux, 0, 0, +10, 0); - - tux->gun = gun; -} - -static void shotInGunLasser(tux_t *tux) -{ - int i; - - for( i = 0 ; i < 50 ; i++ ) - { - addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_addLaserTimer, newInt(tux->id), i * 10 ); - } -} - -static void putInGunMine(tux_t *tux) -{ - int x, y; - arena_t *arena; - - x = tux->x; - y = tux->y; - - switch( tux->position ) - { - case TUX_UP : - y += TUX_HEIGHT; - break; - case TUX_RIGHT : - x -= TUX_WIDTH; - break; - case TUX_LEFT : - x += TUX_WIDTH; - break; - case TUX_DOWN : - y -= TUX_HEIGHT; - break; - } - - arena = getCurrentArena(); - - if( isFreeSpace(getCurrentArena(), x, y, ITEM_MINE_WIDTH, ITEM_MINE_HEIGHT) ) - { - item_t *item; - -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; - playSound("put_mine", SOUND_GROUP_BASE); - sprintf(msg, "tux with id %d pu mine\n", tux->id); - appendTextInTerm(msg); -#endif - - tux->shot[tux->gun]--; - - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - item = newItem(x, y, ITEM_MINE, tux->id); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_additem_server(PROTO_SEND_ALL, NULL, item); - } - - addObjectToSpace(arena->spaceItem, item ); - } - } -} - -static void shotInGunBombball(tux_t *tux) -{ - addShot(tux, 0, 0, +10, 0); -} - -void shotInGun(tux_t *tux) -{ - if( tux->bonus != BONUS_SHOT && tux->gun != GUN_MINE ) - { - tux->shot[tux->gun]--; - } - - switch( tux->gun ) - { - case GUN_SIMPLE : -#ifndef PUBLIC_SERVER - playSound("gun_revolver", SOUND_GROUP_BASE); -#endif - shotInGunSimpe(tux); - break; - case GUN_DUAL_SIMPLE : -#ifndef PUBLIC_SERVER - playSound("gun_revolver", SOUND_GROUP_BASE); -#endif - shotInGunDualSimpe(tux); - break; - case GUN_SCATTER : -#ifndef PUBLIC_SERVER - playSound("gun_scatter", SOUND_GROUP_BASE); -#endif - shotInGunScatter(tux); - break; - case GUN_TOMMY : -#ifndef PUBLIC_SERVER - playSound("gun_tommy", SOUND_GROUP_BASE); -#endif - shotInGunTommy(tux); - break; - case GUN_LASSER : -#ifndef PUBLIC_SERVER - playSound("gun_lasser", SOUND_GROUP_BASE); -#endif - shotInGunLasser(tux); - break; - case GUN_BOMBBALL : - shotInGunBombball(tux); - break; - case GUN_MINE : - putInGunMine(tux); - break; - } -} diff --git a/src/hashTable.c b/src/hashTable.c deleted file mode 100644 index cc67225..0000000 --- a/src/hashTable.c +++ /dev/null @@ -1,221 +0,0 @@ - -#include -#include -#include - -#include "hashTable.h" - -hashtable_t* newHashTable() -{ - hashtable_t *new; - - new = malloc( sizeof(hashtable_t) ); - memset(new, 0, sizeof(hashtable_t) ); - - return new; -} - -void addHashTable(hashtable_t *p, char *key, void *item) -{ - hashtable_t *this; - hashtable_t *next; - int len; - int i; - - this = p; - next = p; - - len = strlen(key); - - for( i = 0 ; i < len ; i++ ) - { - //printf("addHashTable key[%d] = %c\n", i , key[i]); - - next = this->table[ (int)key[i] ]; - - if( next == NULL ) - { - next = newHashTable(); - this->table[ (int)key[i] ] = next; - } - - this = next; - } - - next->data = item; -} - -void addHashTableWithIndex(hashtable_t *p, int index, void *item) -{ - char str[16]; - sprintf(str, "%d", index); - addHashTable(p, str, item); -} - -void* getHashTable(hashtable_t *p, char *key) -{ - hashtable_t *this; - hashtable_t *next; - int len; - int i; - - this = p; - next = p; - - len = strlen(key); - - for( i = 0 ; i < len ; i++ ) - { - //printf("getHashTable key[%d] = %c\n", i , key[i]); - - next = this->table[ (int)key[i] ]; - - //printf("next = %p\n", next); - - if( next == NULL ) - { - return NULL; - } - - this = next; - } - - return next->data; -} - -void* getHashTableWithIndex(hashtable_t *p, int index) -{ - char str[16]; - sprintf(str, "%d", index); - return getHashTable(p, str); -} - -void delHashTable(hashtable_t *p, char *key) -{ - hashtable_t *this; - hashtable_t *next; - int len; - int i; - - this = p; - next = p; - - len = strlen(key); - - for( i = 0 ; i < len ; i++ ) - { - //printf("getHashTable key[%d] = %c\n", i , key[i]); - - next = this->table[ (int)key[i] ]; - - //printf("next = %p\n", next); - - if( next == NULL ) - { - return; - } - - this = next; - } - - next->data = NULL; - -} - -void delHashTableWithIndex(hashtable_t *p, int index) -{ - char str[16]; - sprintf(str, "%d", index); - delHashTable(p, str); -} - -void delHashTableWithMem(hashtable_t *p, char *key, void *f) -{ - void (*fce)(void *p); - - delHashTable(p, key); - - fce = f; - fce(key); -} - -void delHashTableWithMemWithIndex(hashtable_t *p, int index) -{ - char str[16]; - sprintf(str, "%d", index); - delHashTable(p, str); -} - -void destroyHashTable(hashtable_t *p) -{ - hashtable_t *this; - hashtable_t *next; - int i; - - this = p; - - for( i = 0 ; i < 256 ; i++ ) - { - next = this->table[i]; - - if( next != NULL ) - { - destroyHashTable(next); - } - } - - free(p); -} - -void destroyHashTableWithMem(hashtable_t *p, void *f) -{ - void (*fce)(void *p); - hashtable_t *this; - hashtable_t *next; - int i; - - this = p; - fce = f; - - for( i = 0 ; i < 256 ; i++ ) - { - next = this->table[i]; - - if( next != NULL ) - { - destroyHashTableWithMem(next, f); - } - } - - if( p->data != NULL ) - { - fce(p->data); - } - - free(p); -} - -int test_main(int argc, char **argv) -{ - hashtable_t *p; - int max = 10000; - int i; - - p = newHashTable(); - - for( i = 0 ; i < max ; i++ ) - { - addHashTableWithIndex(p, i, NULL); - } - - for( i = 0 ; i < max ; i++ ) - { - printf("%d -> %s\n", i, (char *)getHashTableWithIndex(p, i) ); - } - - //printHashTable(p); - - destroyHashTableWithMem(p, free); - - return 0; -} diff --git a/src/heightScore.c b/src/heightScore.c deleted file mode 100644 index fa7402e..0000000 --- a/src/heightScore.c +++ /dev/null @@ -1,98 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "textFile.h" -#include "heightScore.h" - -static textFile_t *textFile; - -void initHeightScore(char *file) -{ - int i; - - textFile = loadTextFile(file); - - if( textFile == NULL ) - { - fprintf(stderr, "I can't load %s !\n", file); - fprintf(stderr, "I create %s\n", file); - textFile = newTextFile(file); - } - else - { - return; - } - - if( textFile == NULL ) - { - fprintf(stderr, "I can't create %s!\n", file); - return; - } - - for( i = 0 ; i < HEIGHTSCORE_MAX_PLAYERS ; i++ ) - { - addList(textFile->text, strdup("no_name 0") ); - } - - saveTextFile(textFile); -} - -int addPlayerInHighScore(char *name, int score) -{ - int i; - - if( score <= 0 ) - { - return -1; // Ha ha ha - } - - for( i = 0 ; i < HEIGHTSCORE_MAX_PLAYERS ; i++ ) - { - char *line; - char thisName[STR_NAME_SIZE]; - int thisCore; - - line = (char *) textFile->text->list[i]; - sscanf(line, "%s %d", thisName, &thisCore); - - if( score >= thisCore ) - { - char new[STR_SIZE]; - - sprintf(new, "%s %d", name, score); - insList(textFile->text, i, strdup(new) ); - delListItem(textFile->text, HEIGHTSCORE_MAX_PLAYERS, free); - saveTextFile(textFile); - - return 0; - } - } - - return -1; -} - -char* getTableItem(int index) -{ - if( textFile != NULL && - index >= 0 && - index < textFile->text->count ) - { - return (char *)textFile->text->list[index]; - } - - return NULL; -} - -void quitHeightScore() -{ - if( textFile != NULL ) - { - saveTextFile(textFile); - destroyTextFile(textFile); - } -} diff --git a/src/homeDirector.c b/src/homeDirector.c deleted file mode 100755 index e90caae..0000000 --- a/src/homeDirector.c +++ /dev/null @@ -1,42 +0,0 @@ - -#include "main.h" -#include "homeDirector.h" - -#include -#include -#include -#include -#include - -static char homeDirector[STR_PATH_SIZE]; - -void createHomeDirector() -{ - char *envHome; - - envHome = getenv("HOME"); - - if( envHome == NULL ) - { - fprintf(stderr, "Environment HOME not found !\n"); - exit(0); - } - - sprintf(homeDirector, "%s/%s", envHome, HOMEDIRECTOR_NAME); - - if ( access(homeDirector, F_OK) != 0 ) - { - printf("Create home director %s\n", homeDirector); - - if( mkdir(homeDirector, 0755) != 0 ) - { - fprintf(stderr, "Can't create home directory!\n"); - exit(0); - } - } -} - -char *getHomeDirector() -{ - return homeDirector; -} diff --git a/src/idManager.c b/src/idManager.c deleted file mode 100644 index ea7e679..0000000 --- a/src/idManager.c +++ /dev/null @@ -1,107 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "idManager.h" - -static list_t *listID; -static int lastID; - -void initListID() -{ - listID = newList(); - lastID = 0; - printf("init ID manger..\n"); -} - -int isRegisterID(int id) -{ - int i; - - assert( listID != NULL ); - - for( i = 0 ; i < listID->count ; i++ ) - { - int *z; - - z = (int *) listID->list[i]; - - if( *z == id ) - { - return i; - } - } - - return -1; -} - -int getNewID() -{ - int ret; - - assert( listID != NULL ); - - do{ - ret = random() % MAX_ID; -/* - ret = ++lastID; - - if( lastID > MAX_ID ) - { - lastID = 0; - } -*/ - }while( isRegisterID(ret) != -1 ); - - addList(listID, newInt(ret) ); - - //printf("new ID -> %d\n", ret); - return ret; -} - -void delID(int id) -{ - int index; - - assert( listID != NULL ); - - index = isRegisterID(id); - - if( index == -1 ) - { - assert( ! "takyto ID nebol nikdy registrovany !" ); - return; // ha ha ha - } - - delListItem(listID, index, free); - - return; -} - -void replaceID(int old_id, int new_id) -{ - if( old_id == new_id ) - { - return; - } - - delID(old_id); - - if( new_id != -1 ) - { - assert( isRegisterID(new_id) == -1 ); - addList(listID, newInt(new_id) ); - } -} - -void quitListID() -{ - printf("quit ID manger..\n"); - - assert( listID != NULL ); - destroyListItem(listID, free); -} - diff --git a/src/image.c b/src/image.c deleted file mode 100755 index 733d480..0000000 --- a/src/image.c +++ /dev/null @@ -1,150 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "interface.h" -#include "image.h" -#include "list.h" -#include "path.h" -#include "storage.h" -#include "string_length.h" - -static list_t *listStorage; - -static bool_t isImageDataInit = FALSE; - -bool_t isImageInicialized() -{ - return isImageDataInit; -} - -/* - * Inicialuzje globalny zoznam obrazkov - */ -void initImageData() -{ - assert( isInterfaceInicialized() == TRUE ); - - printf("init image database..\n"); - listStorage = newStorage(); - isImageDataInit = TRUE; -} - -static SDL_Surface *loadImage(const char *filename, int alpha) -{ - SDL_Surface *tmp; - SDL_Surface *ret; - - char str[STR_PATH_SIZE]; - - - sprintf(str, PATH_IMAGE "%s", filename); - - accessExistFile(str); - - if( ( tmp = IMG_Load(str) ) == NULL ) - { - fprintf(stderr, "%s\n", SDL_GetError()); - return NULL; - } - - if( ( ret = (alpha) ? SDL_DisplayFormatAlpha(tmp) : SDL_DisplayFormat(tmp) ) == NULL ) - { - fprintf(stderr, "%s\n", SDL_GetError()); - SDL_FreeSurface(tmp); - return NULL; - } - - SDL_FreeSurface(tmp); - return ret; -} - -static void destroySDLSurface(void *p) -{ - assert( p != NULL ); - SDL_FreeSurface((SDL_Surface *)p); -} - -/* - * Prida obrazok do globalneho zoznamu obrazkov - * *file - nazov suboru v ktorom je obrazok ulozeny - * *name - nazov obrazku ( pouzije sa ako vyhadavaci retazec v zazanem ) - * alpha - 0 - nema alpha kanal | 1 - ma alpha kanal - */ -SDL_Surface* addImageData(char *file, int alpha, char *name, char *group) -{ - SDL_Surface *new; - - assert( file != NULL ); - assert( name != NULL ); - assert( group != NULL ); - - new = loadImage(file, alpha); - - addItemToStorage(listStorage, group, name, new ); - - printf("load image %s\n", file); - - return new; -} - -/* - * Vrati odkaz na image_data v globalnom zozname obrazkov - * a nazvom *s - */ -SDL_Surface* getImage(char *group, char *name) -{ - assert( group != NULL ); - assert( name != NULL ); - - return getItemFromStorage(listStorage, group, name); -} - -void delImage(char *group, char *name) -{ - assert( group != NULL ); - assert( name != NULL ); - - delItemFromStorage(listStorage, group, name, destroySDLSurface); -} - -void delAllImageInGroup(char *group) -{ - assert( group != NULL ); - - delAllItemFromStorage(listStorage, group, destroySDLSurface); -} - -void drawImage(SDL_Surface *p, int x,int y, int px, int py, int w, int h) -{ - static SDL_Surface *screen = NULL; - SDL_Rect dst_rect, src_rect; - - if( screen == NULL ) - { - screen = getSDL_Screen(); - } - - dst_rect.x = x; - dst_rect.y = y; - - src_rect.x = px; - src_rect.y = py; - src_rect.w = w; - src_rect.h = h; - - SDL_BlitSurface(p, &src_rect, screen, &dst_rect); -} - -/* - * Odstrani zoznam obrazkov z pamate - */ -void quitImageData() -{ - printf("quit image database..\n"); - destroyStorage(listStorage, destroySDLSurface); - isImageDataInit = FALSE; -} - diff --git a/src/interface.c b/src/interface.c deleted file mode 100755 index 0b3772e..0000000 --- a/src/interface.c +++ /dev/null @@ -1,256 +0,0 @@ - -#include - -#include "main.h" -#include "interface.h" -#include "screen.h" -#include "myTimer.h" - - -//povrch okna -static SDL_Surface *screen; -//static SDL_Surface *my_surface; - -//casovac -static SDL_TimerID timer; - -//priznaky okna -static Uint32 g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF; - -static bool_t isInterfaceInit = FALSE; - -bool_t isInterfaceInicialized() -{ - return isInterfaceInit; -} - -static Uint32 TimerCallback(Uint32 interval, void *param) -{ - SDL_Event event; - event.type = SDL_USEREVENT; - event.user.code = USR_EVT_TIMER; - event.user.data1 = NULL; - event.user.data2 = NULL; - - SDL_PushEvent(&event); - - return interval; -} - -int initSDL() -{ - printf("init SDL..\n"); - - //inicializujem SDL - if( SDL_Init(SDL_SUBSYSTEMS) == -1 ) - { - fprintf(stderr, "%s\n", SDL_GetError()); - SDL_Quit(); - return -1; - } - - //inicializujem okno - //screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags); - screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags); - -/* - my_surface = SDL_CreateRGBSurface(screen->flags, WINDOW_SIZE_X, WINDOW_SIZE_Y, - screen->format->BitsPerPixel, - screen->format->Rmask, screen->format->Gmask, - screen->format->Bmask, screen->format->Amask); - - memset(my_surface->pixels, 128, 200); -*/ - if (screen == NULL ) - { - fprintf(stderr, "%s\n", SDL_GetError()); - SDL_Quit(); - return -1; - } - - SDL_WM_SetCaption(WINDOW_TITLE, NULL); - //SDL_ShowCursor(0); - SDL_EnableKeyRepeat(1,1); - timer = SDL_AddTimer(INTERVAL, TimerCallback, NULL); - - srand((unsigned)time(NULL)); - isInterfaceInit = TRUE; - - return 0; -} - -SDL_Surface* getSDL_Screen() -{ - //return my_surface; - return screen; -} - -void interfaceRefresh() -{ - //SDL_BlitSurface(my_surface, NULL, screen, NULL); - //SDL_UpdateRect(screen, 0, 0, screen->w, screen->h); - - //drawImage(my_surface, 200, 100, 0, 0, 200, 200); - SDL_Flip(screen); -} - -int toggleFullscreen() -{ - if(g_win_flags & SDL_FULLSCREEN) - g_win_flags &= ~SDL_FULLSCREEN; - else - g_win_flags |= SDL_FULLSCREEN; - - if( SDL_WM_ToggleFullScreen(screen) == 0 ) - { - fprintf(stderr, "Nemozem prepnut do fullscreenu!\n"); - - SDL_FreeSurface(screen); - screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, WIN_BPP, g_win_flags); - - if(screen == NULL) - { - fprintf(stderr, "Nemozem premnut do okna:%s\n", SDL_GetError()); - return 0; - } - } - - return 1; -} - -void getMousePosition(int *x, int *y) -{ - SDL_GetMouseState(x, y); -} - -int isMouseClicked() -{ - return SDL_GetMouseState(NULL,NULL) & SDL_BUTTON(SDL_BUTTON_LEFT); -} - -int isPessAnyKey() -{ - Uint8 *mapa; - int i; - - mapa = SDL_GetKeyState(NULL); - - for( i = SDLK_BACKSPACE ; i < SDLK_KP9 ; i++ ) - if( mapa[i] == SDL_PRESSED ) - { - return 1; - } - - return 0; -} - -void printPessAnyKey() -{ - Uint8 *mapa; - int i; - - mapa = SDL_GetKeyState(NULL); - - for( i = SDLK_BACKSPACE ; i < SDLK_KP9 ; i++ ) - if( mapa[i] == SDL_PRESSED ) - { - printf("press key with code : %d\n", i); - } - -} - - -/* -static void action_esc() -{ - if( strcmp(getScreen(), "world") == 0 ) - { - setScreen("analyze"); - } - - if( strcmp(getScreen(), "mainMenu") == 0 ) - { - quit(); - } - -} -*/ - -int eventAction() -{ - SDL_Event event; - - - while(SDL_WaitEvent(&event)) - { - switch(event.type) - { - case SDL_KEYDOWN: - switch(event.key.keysym.sym) - { -/* - case SDLK_ESCAPE: - action_esc(); - return 0; - break; -*/ - case SDLK_F1: - if(!toggleFullscreen())return 0; - break; - - default: - break; - } - break; - - case SDL_USEREVENT: - switch(event.user.code) - { - case USR_EVT_TIMER: - drawScreen(); - eventScreen(); - break; - - default: - break; - } - break; - - // Zmena velikosti okna - case SDL_VIDEORESIZE: - screen = SDL_SetVideoMode(event.resize.w,event.resize.h, WIN_BPP, g_win_flags); - - if(screen == NULL) - { - fprintf(stderr, "Nemozem zmenit rozlisene okna: %s\n",SDL_GetError()); - return 0; - } - break; - - // Pozadavek na ukonceni - case SDL_QUIT: - return -1; - break; - - default: - break; - } - } - - return 0; -} - -void eventSDL() -{ - while(1) - { - if ( eventAction() == -1 )return; - } -} - -void quitSDL() -{ - printf("quit SDL..\n"); - SDL_Quit(); - isInterfaceInit = FALSE; -} diff --git a/src/item.c b/src/item.c deleted file mode 100644 index 7100e5c..0000000 --- a/src/item.c +++ /dev/null @@ -1,610 +0,0 @@ - -#include -#include - -#include "main.h" -#include "arena.h" -#include "tux.h" -#include "item.h" -#include "shot.h" -#include "proto.h" -#include "idManager.h" -#include "net_multiplayer.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#include "sound.h" -#include "fake_audio.h" -#include "layer.h" -#include "screen_world.h" -#include "screen_setting.h" -#include "term.h" -#endif - -#ifdef PUBLIC_SERVER -#include "publicServer.h" -#endif - -#ifndef PUBLIC_SERVER -static SDL_Surface *g_item[ITEM_COUNT]; -#endif - -static bool_t isItemInit = FALSE; - -bool_t isItemInicialized() -{ - return isItemInit; -} - -void initItem() -{ -#ifndef PUBLIC_SERVER - g_item[GUN_DUAL_SIMPLE] = addImageData("item.dual.png", IMAGE_ALPHA, "item_dual_simple", IMAGE_GROUP_BASE); - g_item[GUN_TOMMY] = addImageData("item.tommy.png", IMAGE_ALPHA, "item_tommy_simple", IMAGE_GROUP_BASE); - g_item[GUN_SCATTER] = addImageData("item.scatter.png", IMAGE_ALPHA, "item_scatter_simple", IMAGE_GROUP_BASE); - g_item[GUN_LASSER] = addImageData("item.lasser.png", IMAGE_ALPHA, "item_lasser_simple", IMAGE_GROUP_BASE); - g_item[GUN_MINE] = addImageData("item.mine.png", IMAGE_ALPHA, "item_mines_simple", IMAGE_GROUP_BASE); - g_item[GUN_BOMBBALL] = addImageData("item.bombball.png", IMAGE_ALPHA, "item_bombball_simple", IMAGE_GROUP_BASE); - - g_item[ITEM_MINE] = addImageData("mine.png", IMAGE_ALPHA, "item_mine_simple", IMAGE_GROUP_BASE); - g_item[ITEM_EXPLOSION] = addImageData("explosion.png", IMAGE_ALPHA, "item_explosion_simple", IMAGE_GROUP_BASE); - g_item[ITEM_BIG_EXPLOSION] = addImageData("big-explosion.png", IMAGE_ALPHA, "item_big-explosion_simple", IMAGE_GROUP_BASE); - - g_item[BONUS_SPEED] = addImageData("item.bonus.speed.png", IMAGE_ALPHA, "item_bonus_speed", IMAGE_GROUP_BASE); - g_item[BONUS_SHOT] = addImageData("item.bonus.shot.png", IMAGE_ALPHA, "item_shot_speed", IMAGE_GROUP_BASE); - g_item[BONUS_TELEPORT] = addImageData("item.bonus.teleport.png", IMAGE_ALPHA, "item_teleport_speed", IMAGE_GROUP_BASE); - g_item[BONUS_GHOST] = addImageData("item.bonus.ghost.png", IMAGE_ALPHA, "item_ghost_speed", IMAGE_GROUP_BASE); - g_item[BONUS_4X] = addImageData("item.bonus.4x.png", IMAGE_ALPHA, "item_4x_speed", IMAGE_GROUP_BASE); - g_item[BONUS_HIDDEN] = addImageData("item.bonus.hidden.png", IMAGE_ALPHA, "item_hidden_speed", IMAGE_GROUP_BASE); -#endif - - isItemInit = TRUE; -} - -item_t* newItem(int x, int y, int type, int author_id) -{ - item_t *new; - - new = malloc( sizeof(item_t) ); - assert( new != NULL ); - - new->type = type; - - new->id = getNewID(); - new->x = x; - new->y = y; - new->frame = 0; - new->count = 0; -#ifndef PUBLIC_SERVER - new->img = g_item[type]; -#endif - new->author_id = author_id; - - switch( type ) - { - case GUN_DUAL_SIMPLE : - case GUN_SCATTER : - case GUN_TOMMY : - case GUN_LASSER : - case GUN_MINE : - case GUN_BOMBBALL : - new->w = ITEM_GUN_WIDTH; - new->h = ITEM_GUN_HEIGHT; - break; - - case ITEM_MINE : - new->w = ITEM_MINE_WIDTH; - new->h = ITEM_MINE_HEIGHT; - break; - - case ITEM_EXPLOSION : -#ifndef PUBLIC_SERVER - playSound("explozion", SOUND_GROUP_BASE); -#endif - new->w = ITEM_EXPLOSION_WIDTH; - new->h = ITEM_EXPLOSION_HEIGHT; - break; - - case ITEM_BIG_EXPLOSION : -#ifndef PUBLIC_SERVER - playSound("explozion", SOUND_GROUP_BASE); -#endif - new->w = ITEM_BIG_EXPLOSION_WIDTH; - new->h = ITEM_BIG_EXPLOSION_HEIGHT; - break; - - case BONUS_SPEED : - case BONUS_SHOT : - case BONUS_TELEPORT : - case BONUS_GHOST : - case BONUS_4X : - case BONUS_HIDDEN : - new->w = ITEM_BONUS_WIDTH; - new->h = ITEM_BONUS_HEIGHT; - break; - } - - return new; -} - -/* -item_t* getItemID(list_t *listItem, int id) -{ - item_t *thisItem; - int i; - - assert( listItem != NULL ); - - for( i = 0 ; i < listItem->count ; i++ ) - { - thisItem = (item_t *)listItem->list[i]; - assert( thisItem != NULL ); - - if( thisItem->id == id ) - { - return thisItem; - } - } - - return NULL; -} -*/ - -void getStatusItem(void *p, int *id, int *x, int *y ,int *w, int *h) -{ - item_t *item; - - item = p; - - *id = item->id; - *x = item->x; - *y = item->y; - *w = item->w; - *h = item->h; -} - -void setStatusItem(void *p, int x, int y, int w, int h) -{ - item_t *item; - - item = p; - item->x = x; - item->y = y; - item->w = w; - item->h = h; -} - -void replaceItemID(item_t *item, int id) -{ - replaceID(item->id, id); - item->id = id; -} - -void addNewItem(space_t *spaceItem, int author_id) -{ -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; -#endif - arena_t *arena; - item_t *item; - int new_x, new_y; - int type; - -#ifndef PUBLIC_SERVER - if( isSettingAnyItem() == FALSE || - getNetTypeGame() == NET_GAME_TYPE_CLIENT ) - { - return; - } -#endif - - arena = getCurrentArena(); - findFreeSpace(getCurrentArena(), &new_x, &new_y, ITEM_GUN_WIDTH, ITEM_GUN_HEIGHT); - - type = GUN_DUAL_SIMPLE; - -#ifndef PUBLIC_SERVER - do{ -#endif - switch( random() % 12 ) - { - case 0 : type = GUN_DUAL_SIMPLE; - break; - case 1 : type = GUN_SCATTER; - break; - case 2 : type = GUN_TOMMY; - break; - case 3 : type = GUN_MINE; - break; - case 4 : type = GUN_LASSER; - break; - case 5 : type = GUN_BOMBBALL; - break; - case 6 : type = BONUS_SPEED; - break; - case 7 : type = BONUS_SHOT; - break; - case 8 : type = BONUS_TELEPORT; - break; - case 9 : type = BONUS_GHOST; - break; - case 10 : type = BONUS_4X; - break; - case 11 : type = BONUS_HIDDEN; - break; - } -#ifndef PUBLIC_SERVER - }while( isSettingItem(type) == FALSE || - (getNetTypeGame() == NET_GAME_TYPE_NONE && type == BONUS_HIDDEN) ); -#endif - - item = newItem(new_x, new_y, type, author_id); - addObjectToSpace(spaceItem, item); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_additem_server(PROTO_SEND_ALL, NULL, item); - } - -#ifndef PUBLIC_SERVER - sprintf(msg, "in arena is new item\n"); - appendTextInTerm(msg); -#endif - -} - -#ifndef PUBLIC_SERVER - -void drawItem(item_t *p) -{ - assert( p != NULL ); - - addLayer(p->img, p->x, p->y, p->frame * p->w, 0, p->w, p->h, TUX_LAYER); -} - -void drawListItem(list_t *listItem) -{ - item_t *thisItem; - int i; - - assert( listItem != NULL ); - - for( i = 0 ; i < listItem->count ; i++ ) - { - thisItem = (item_t *)listItem->list[i]; - assert( thisItem != NULL ); - drawItem(thisItem); - } -} - -#endif - -void eventListItem(space_t *spaceItem) -{ - my_time_t currentTime; - item_t *thisItem; - int i; - - currentTime = getMyTime(); - - assert( spaceItem != NULL ); - - for( i = 0 ; i < spaceItem->list->count ; i++ ) - { - thisItem = (item_t *)spaceItem->list->list[i]; - assert( thisItem != NULL ); - - thisItem->count++; - - if( thisItem->count == ITEM_MAX_COUNT ) - { - thisItem->count = 0; - thisItem->frame++; - } - - switch( thisItem->type ) - { - case GUN_TOMMY : - case GUN_DUAL_SIMPLE : - case GUN_SCATTER : - case GUN_LASSER : - case GUN_MINE : - case GUN_BOMBBALL : - if( thisItem->frame == ITEM_GUN_MAX_FRAME ) - { - thisItem->frame = 0; - } - break; - - case ITEM_MINE : - if( thisItem->frame == ITEM_MINE_MAX_FRAME ) - { - thisItem->frame = 0; - } - break; - - case ITEM_EXPLOSION : - case ITEM_BIG_EXPLOSION : - if( thisItem->frame == ITEM_EXPLOSION_MAX_FRAME ) - { - delObjectFromSpaceWithObject(spaceItem, thisItem, destroyItem); - i--; - } - break; - - case BONUS_SPEED : - case BONUS_SHOT : - case BONUS_TELEPORT : - case BONUS_GHOST : - case BONUS_4X : - case BONUS_HIDDEN : - if( thisItem->frame == ITEM_GUN_MAX_FRAME ) - { - thisItem->frame = 0; - } - break; - } - } -} - -void mineExplosion(space_t *spaceItem, item_t *item) -{ - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - int x, y; - item_t *item_explosion; - - x = ( item->x + item->w/2 ) - ITEM_BIG_EXPLOSION_WIDTH/2; - y = ( item->y + item->h/2 ) - ITEM_BIG_EXPLOSION_HEIGHT/2; - - item_explosion = newItem(x, y, ITEM_BIG_EXPLOSION, item->author_id); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_additem_server(PROTO_SEND_ALL, NULL, item_explosion); - } - - addObjectToSpace(spaceItem, item_explosion ); - } - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); - } - - delObjectFromSpaceWithObject(spaceItem, item, destroyItem); -} - -void eventConflictShotWithItem(arena_t *arena) -{ - shot_t *thisShot; - item_t *thisItem; - int i, j; - - if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) - { - return; - } - - for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) - { - bool_t isDelShot; - - isDelShot = FALSE; - thisShot = (shot_t *)arena->spaceShot->list->list[i]; - assert( thisShot != NULL ); - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceItem, thisShot->x, thisShot->y, thisShot->w, thisShot->h, listHelp); - - for( j = 0 ; j < listHelp->count ; j++ ) - { - thisItem = (item_t *)listHelp->list[j]; - assert( thisItem != NULL ); - - switch( thisItem->type ) - { - case ITEM_MINE : - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - mineExplosion(arena->spaceItem, thisItem); - } - break; - - case ITEM_EXPLOSION : - case ITEM_BIG_EXPLOSION : - isDelShot = TRUE; - break; - } - } - - if( isDelShot ) - { - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_del_server(PROTO_SEND_ALL, NULL, thisShot->id); - } - - delObjectFromSpaceWithObject(arena->spaceShot, thisShot, destroyShot); - i--; - } - } -} - -static void eventTuxIsDeadWithItem(tux_t *tux, item_t *item) -{ - if( tux->bonus == BONUS_GHOST ) - { - return; - } - - if( tux->bonus == BONUS_TELEPORT ) - { - tuxTeleport(tux); - return; - } - - if( item->author_id != tux->id ) - { - tux_t *author; - - author = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, item->author_id); - - if( author != NULL ) - { - author->score++; - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, author); - } - } - } - - countRoundInc(); - eventTuxIsDead(tux); -} - -static void tuxGiveBonus(tux_t *tux, item_t *item) -{ -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; - playSound("item_bonus", SOUND_GROUP_BASE); -#endif - tux->bonus = item->type; - tux->bonus_time = TUX_MAX_BONUS; - -#ifndef PUBLIC_SERVER - sprintf(msg, "tux with id %d bonus enabled\n", tux->id); - appendTextInTerm(msg); -#endif - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } -} - -static void tuxGiveGun(tux_t *tux, item_t *item) -{ -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; - playSound("item_gun", SOUND_GROUP_BASE); -#endif - - tux->pickup_time = 0; - tux->shot[ item->type ] += GUN_MAX_SHOT; - tux->gun = item->type; - -#ifndef PUBLIC_SERVER - sprintf(msg, "tux with id %d has new gun\n", tux->id); - appendTextInTerm(msg); -#endif - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } -} - -void eventGiveTuxItem(tux_t *tux, item_t *item, space_t *spaceItem) -{ - switch( item->type ) - { - case GUN_TOMMY : - case GUN_DUAL_SIMPLE : - case GUN_SCATTER : - case GUN_LASSER : - case GUN_MINE : - case GUN_BOMBBALL : - tuxGiveGun(tux, item); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); - } - - delObjectFromSpaceWithObject(spaceItem, item, destroyItem); - break; - - case ITEM_MINE : - if( tux->bonus != BONUS_GHOST ) - { - mineExplosion(spaceItem, item); - } - break; - - case ITEM_EXPLOSION : - case ITEM_BIG_EXPLOSION : - eventTuxIsDeadWithItem(tux, item); - break; - - case BONUS_SPEED : - case BONUS_SHOT : - case BONUS_TELEPORT : - case BONUS_GHOST : - case BONUS_4X : - case BONUS_HIDDEN : - tuxGiveBonus(tux, item); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); - } - - delObjectFromSpaceWithObject(spaceItem, item, destroyItem); - break; - } -} - -void eventGiveTuxListItem(tux_t *tux, space_t *spaceItem) -{ - item_t *thisItem; - int x, y, w, h; - int i; - - if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) - { - return; - } - - assert( spaceItem != NULL ); - assert( tux != NULL ); - - if( tux->status == TUX_STATUS_DEAD ) - { - return; - } - - getTuxProportion(tux, &x, &y, &w, &h); - - listDoEmpty(listHelp); - getObjectFromSpace(spaceItem, x, y, w, h, listHelp); - - for( i = 0 ; i < listHelp->count ; i++ ) - { - thisItem = (item_t *)listHelp->list[i]; - assert( thisItem != NULL ); - - eventGiveTuxItem(tux, thisItem, spaceItem); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - //proto_send_item_server(PROTO_SEND_ALL, NULL, tux, thisItem); - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - } -} - -void destroyItem(item_t *p) -{ - assert( p != NULL ); - delID(p->id); - free(p); -} - -void quitItem() -{ - isItemInit = FALSE; -} diff --git a/src/keytable.c b/src/keytable.c deleted file mode 100644 index db4aa02..0000000 --- a/src/keytable.c +++ /dev/null @@ -1,135 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "homeDirector.h" -#include "textFile.h" -#include "configFile.h" -#include "keytable.h" - -static textFile_t *keytableFile; -static textFile_t *keycontrolFile; -static int keytable[KEY_LENGTH]; - -static int getKeyCode(char *s) -{ - char str[STR_SIZE]; - char *find; - int i; - - sprintf(str, "keycode %s = ", s); - - for( i = 0 ; i < keytableFile->text->count ; i++ ) - { - char *line; - line = (char *) (keytableFile->text->list[i]); - - if( ( find = strstr(line, str) ) != NULL ) - { - int ret; - ret = atoi( strchr(line, '=')+1 ); - return ret; - } - } - - return -1; -} - -void initKeyTable() -{ - char path[STR_PATH_SIZE]; - char val[STR_SIZE]; -/* - int i; -*/ - sprintf(path, PATH_CONFIG "keytable.conf"); - keytableFile = loadTextFile(path); - - if( keytableFile == NULL ) - { - fprintf(stderr, "Don't load %s\n", path); - return; - } - - sprintf(path, "%s/keycontrol.conf", getHomeDirector()); - keycontrolFile = loadTextFile(path); - - if( keycontrolFile == NULL ) - { - fprintf(stderr, "Don't load %s\n", path); - fprintf(stderr, "I create %s\n", path); - - keycontrolFile = newTextFile(path); - } - - if( keycontrolFile == NULL ) - { - fprintf(stderr, "Don't create %s\n", path); - return; - } - - loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_MOVE_UP", val, STR_SIZE, "SDLK_UP"); - keytable[ KEY_TUX_RIGHT_MOVE_UP ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_MOVE_RIGHT", val, STR_SIZE, "SDLK_RIGHT"); - keytable[ KEY_TUX_RIGHT_MOVE_RIGHT ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_MOVE_LEFT", val, STR_SIZE, "SDLK_LEFT"); - keytable[ KEY_TUX_RIGHT_MOVE_LEFT ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_MOVE_DOWN", val, STR_SIZE, "SDLK_DOWN"); - keytable[ KEY_TUX_RIGHT_MOVE_DOWN ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_SHOOT", val, STR_SIZE, "SDLK_KP0"); - keytable[ KEY_TUX_RIGHT_SHOOT ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_RIGHT_SWITCH_WEAPON", val, STR_SIZE, "SDLK_KP1"); - keytable[ KEY_TUX_RIGHT_SWITCH_WEAPON] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_MOVE_UP", val, STR_SIZE, "SDLK_w"); - keytable[ KEY_TUX_LEFT_MOVE_UP ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_MOVE_RIGHT", val, STR_SIZE, "SDLK_d"); - keytable[ KEY_TUX_LEFT_MOVE_RIGHT ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_MOVE_LEFT", val, STR_SIZE, "SDLK_a"); - keytable[ KEY_TUX_LEFT_MOVE_LEFT ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_MOVE_DOWN", val, STR_SIZE, "SDLK_s"); - keytable[ KEY_TUX_LEFT_MOVE_DOWN ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_SHOOT", val, STR_SIZE, "SDLK_q"); - keytable[ KEY_TUX_LEFT_SHOOT ] = getKeyCode(val); - - loadValueFromConfigFile(keycontrolFile, "TUX_LEFT_SWITCH_WEAPON", val, STR_SIZE, "SDLK_TAB"); - keytable[ KEY_TUX_LEFT_SWITCH_WEAPON ] = getKeyCode(val); - -/* - for( i = 0 ; i < KEY_LENGTH ; i++) - { - printf("keytable[%d] = %d\n", i, keytable[i]); - } -*/ - - saveTextFile(keycontrolFile); -} - -int getKey(int n) -{ - assert( n >= 0 && n < KEY_LENGTH ); -/* - printf("keytable[n] = %d\n", keytable[n]); -*/ - return keytable[n]; -} - -void quitKeyTable() -{ - destroyTextFile(keycontrolFile); - destroyTextFile(keytableFile); -} - diff --git a/src/language.c b/src/language.c deleted file mode 100644 index 06cb9c1..0000000 --- a/src/language.c +++ /dev/null @@ -1,155 +0,0 @@ -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "homeDirector.h" -#include "textFile.h" -#include "configFile.h" -#include "language.h" - -static textFile_t *languageFile; -static char fontFile[STR_FILE_NAME_SIZE]; -static int fontSize; - -char* getHead(textFile_t *ts) -{ - int i; - - for( i = 0 ; i < ts->text->count ; i++ ) - { - char *line; - line = (char *) (ts->text->list[i]); - - if( strncmp(line, "HEAD", 4) == 0 ) - { - return line; - } - } - - return NULL; -} - - -int initLanguage() -{ - textFile_t *languageTypeFile; - char path[STR_PATH_SIZE]; - char *lang; - char *head; - - sprintf(path, "%s/lang.conf", getHomeDirector()); - printf("open %s\n", path); - languageTypeFile = loadTextFile(path); - - if( languageTypeFile == NULL ) - { - fprintf(stderr, "Don't load %s\n", path); - fprintf(stderr, "I create %s\n", path); - - languageTypeFile = newTextFile(path); - addList(languageTypeFile->text, strdup("en") ); - saveTextFile(languageTypeFile); - } - - if( languageTypeFile == NULL ) - { - fprintf(stderr, "Don't create %s\n", path); - return -1; - } - - if( languageTypeFile->text->count == 0 ) - { - fprintf(stderr, "File %s empty\n", path); - return -1; - } - - lang = ((char *)languageTypeFile->text->list[0]); - printf("select lang %s\n", lang); - - sprintf(path, PATH_LANG "%s.lang", lang); - - destroyTextFile(languageTypeFile); - - accessExistFile(path); - languageFile = loadTextFile(path); - - if( languageFile == NULL ) - { - fprintf(stderr, "Don't load %s\n", path); - return -1; - } - - head = getHead(languageFile); - - if( head != NULL ) - { - char val[STR_SIZE]; - - if( getValue(head, "fontfile", val, STR_SIZE) == 0 ) - { - strcpy(fontFile, val); - } - else - { - fprintf(stderr, "fontfile in lang file %s not found\n", path); - return -1; - } - - if( getValue(head, "fontsize", val, STR_SIZE) == 0 ) - { - fontSize = atoi(val); - } - else - { - fprintf(stderr, "fontsize in lang file %s not found\n", path); - return -1; - } - } - else - { - fprintf(stderr, "head in lang file %s not found\n", path); - return -1; - } - - return 0; -} - -char* getMyText(char *key) -{ - int i; - int len; - - len = strlen(key); - - for( i = 0 ; i < languageFile->text->count ; i++ ) - { - char *line; - line = (char *) (languageFile->text->list[i]); - - if( strncmp(key, line, len) == 0 ) - { - return line+strlen(key)+1; - } - } - - return NULL; -} - -char* getLanguageFont() -{ - return fontFile; -} - -int getLanguageSize() -{ - return fontSize; -} - -void quitLanguage() -{ - destroyTextFile(languageFile); -} - diff --git a/src/layer.c b/src/layer.c deleted file mode 100755 index 07ec40f..0000000 --- a/src/layer.c +++ /dev/null @@ -1,153 +0,0 @@ - -#include -#include - -#include "main.h" -#include "interface.h" -#include "list.h" -#include "layer.h" -#include "image.h" -#include "tux.h" -#include "screen_world.h" - -static list_t *listLayer; - -static bool_t isLayerInit = FALSE; - -bool_t isLayerInicialized() -{ - return isLayerInit; -} - -/* - * Inicializuje globalny zoznam vsrtiev - */ -void initLayer() -{ - listLayer = newList(); -} - -/* - * Prida novu polozku do zoznamu vrstiev - * *img - obrazok, ktory obsahuje polozka - * x y w h - suradnca kam sa obrazok vykresli a jeho rozmery - * px py pw ph - suradnica casti obrazka *img ktora sa ma zobrazit - * layer - vrstva na ktorej sa zobrazi obrazok - */ -void addLayer(SDL_Surface *img, - int x,int y, int px,int py, - int w,int h, int player) -{ - layer_t *new; - layer_t *this; - int i; - - assert( img != NULL ); - - new = malloc( sizeof(layer_t) ); - new->x = x; - new->y = y; - new->w = w; - new->h = h; - new->px = px; - new->py = py; - new->layer = player; - new->image = img; - - if( listLayer->count == 0 ) - { - addList(listLayer, new); - return; - } - - i = 0; - this = (layer_t *)listLayer->list[0]; - - //vyhladaj svoju vrstvu - while( i < listLayer->count && this->layer < new->layer ) - { - this = (layer_t *)listLayer->list[++i]; - } - - //porovnaj s vyskami na tvojej vrstve, - //ak neexistuje, rovno to tam hod - this = (layer_t *)listLayer->list[i]; - - while( i < listLayer->count && this->y+this->h < new->y+new->h && new->layer == this->layer ) - { - this = (layer_t *)listLayer->list[++i]; - } - - insList(listLayer, i, new); - isLayerInit = TRUE; -} - -/* - * Vykresli vsetky polozky na obrazovku - * poukadane podla vrstvy a suradnice Y+H - * po dokresleni sa zoznam uvolni. - */ -void drawLayer() -{ - layer_t *this; - int i; - - for( i = 0 ; i < listLayer->count ; i++ ) - { - this = (layer_t *)listLayer->list[i]; - - drawImage(this->image, - this->x, this->y, - this->px, this->py, - this->w, this->h); - } - - destroyListItem(listLayer, free); - listLayer = newList(); -} - -void drawLayerCenter(int x, int y) -{ - layer_t *this; - int screen_x, screen_y; - int i; - - getCenterScreen(&screen_x, &screen_y, x, y); - - for( i = 0 ; i < listLayer->count ; i++ ) - { - this = (layer_t *)listLayer->list[i]; - - if( (this->x - screen_x) + this->w < 0 || (this->x - screen_x) > WINDOW_SIZE_X || - (this->y - screen_y) + this->h < 0 || (this->y - screen_y) > WINDOW_SIZE_Y ) - { - continue; - } - - drawImage(this->image, - this->x - screen_x, this->y - screen_y, - this->px, this->py, - this->w, this->h); - - //printf("%d %d\n", this->x - screen_x, this->y - screen_y); - } - - destroyListItem(listLayer, free); - listLayer = newList(); -} - -void flushLayer() -{ - destroyListItem(listLayer, free); - listLayer = newList(); -} - -/* - * Odstrani zoznam vrtsiev z pamete. - */ -void quitLayer() -{ - destroyListItem(listLayer, free); - isLayerInit = FALSE; -} - diff --git a/src/list.c b/src/list.c deleted file mode 100755 index 27b0ed8..0000000 --- a/src/list.c +++ /dev/null @@ -1,241 +0,0 @@ - -#include "main.h" -#include "list.h" - -#include -#include -#include -#include - -list_t* newList() -{ - list_t *new; - - new = malloc( sizeof(list_t) ); - memset(new, 0, sizeof(list_t) ); - - return new; -} - -list_t* cloneList(list_t *p) -{ - list_t *new; - - assert( p != NULL ); - - new = malloc( sizeof(list_t) ); - memcpy(new, p, sizeof(list_t) ); - - if( p->list != NULL ) - { - new->list = malloc( p->alloc * sizeof(void *) ); - memcpy(new->list, p->list, p->alloc * sizeof(void *)); - } - - return new; -} - -list_t* cloneListItem(list_t *p, void *f) -{ - list_t *new; - void* (*fce)(void *); - int i; - - assert( p != NULL ); - assert( f != NULL ); - - new = cloneList(p); - fce = f; - - for(i = 0 ; i < p->count ; i++) - new->list[i] = fce(p->list[i]); - - return new; -} - -void addList(list_t *p, void *item) -{ - assert( p != NULL ); - - if( p->alloc == 0 ) - { - p->alloc = LIST_ALLOC_LIMIT; - p->count = 1; - p->list = malloc(p->alloc * sizeof(void *) ); - p->list[0] = item; - - return; - } - - if( p->count + 1 <= p->alloc ) - { - p->list[p->count] = item; - p->count += 1; - - return; - } - - if( p->count + 1 > p->alloc ) - { - void **new; - -#ifdef DEBUG_LIST - printf("realokujem z %d na %d (count=%d)\n", - p->alloc, p->alloc+LIST_ALLOC_LIMIT, p->count); -#endif - - p->alloc += LIST_ALLOC_LIMIT; - new = malloc(p->alloc * sizeof(void *) ); - memcpy(new, p->list, p->count * sizeof(void *) ); - free(p->list); - p->list = new; - p->list[p->count] = item; - p->count++; - - return; - } -} - -void insList(list_t *p, int n, void *item) -{ - assert( p != NULL ); - - addList(p, NULL); // :) - - assert( n >= 0 || n < p->count ); - - memmove(p->list+n+1, p->list+n, ( (p->count-1) - n ) * sizeof(void *)); - - p->list[n] = item; -} - -void *getList(list_t *p, int n) -{ - assert( p != NULL ); - assert( n >= 0 || n < p->count ); - - return p->list[n]; -} - -int searchListItem(list_t *p, void *n) -{ - int i; - - assert( p != NULL ); - - for( i = 0 ; i < p->count ; i++ ) - if( p->list[i] == n )return i; - - return -1; -} - -void delList(list_t *p, int n) -{ - assert( p != NULL ); - assert( n >= 0 || n < p->count ); - - memmove(p->list+n, p->list+n+1, ( (p->count-1) - n ) * sizeof(void *)); - - p->count--; - - if( p->count + LIST_ALLOC_LIMIT < p->alloc ) - { - void **new; - -#ifdef DEBUG_LIST - printf("realokujem z %d na %d (count=%d)\n", - p->alloc, p->alloc-LIST_ALLOC_LIMIT, p->count); -#endif - - p->alloc -= LIST_ALLOC_LIMIT; - new = malloc(p->alloc * sizeof(void *) ); - memcpy(new, p->list, p->count * sizeof(void *) ); - free(p->list); - p->list = new; - } -} - -void delListItem(list_t *p, int n, void *f) -{ - int (*fce)(void *); - - assert( p != NULL ); - assert( n >= 0 || n < p->count ); - - fce = f; - if( fce != NULL )fce(p->list[n]); - - delList(p, n); -} - -void listDoEmpty(list_t *p) -{ - p->count = 0; -} - -void destroyList(list_t *p) -{ - assert( p != NULL ); - - if( p->list != NULL )free(p->list); - free(p); -} - -void destroyListItem(list_t *p, void *f) -{ - void (*fce)(void *); - int i; - - assert( p != NULL ); - assert( f != NULL ); - - fce = f; - - for(i = 0 ; i < p->count ; i++) - fce(p->list[i]); - - destroyList(p); -} - -/* -typedef struct pokus_str -{ - int x; -} pokus; - -pokus* newPokus(int n) -{ - pokus *new; - new = malloc(sizeof(pokus)); - new->x = n; - return new; -} - -pokus *clonePokus(pokus *p) -{ - pokus *new; - new = malloc(sizeof(pokus)); - memcpy(new, p, sizeof(pokus)); - return new; -} - -void destroyPokus(pokus *p) -{ - free(p); -} - -int main() -{ - list_t *my; - int i; - - my = newList(); - - for(i = 0; i < 100; i++) - addList(my, newPokus(i)); - - destroyListItem(my, destroyPokus); - - return 0; -} -*/ diff --git a/src/listServer.c b/src/listServer.c deleted file mode 100644 index 30dba93..0000000 --- a/src/listServer.c +++ /dev/null @@ -1,277 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include - -#include "list.h" -#include "udp.h" - -#define STR_MSG_SIZE 512 -#define STR_IP4_SIZE 20 - -#define LIMIT_REGISTER "100" -#define LIMIT_TIME "10" - -static sock_udp_t *socketServer; -static sock_udp_t *socketClient; -static list_t *listClient; - -static int limitRegisterGameServer; -static int limitTime; - -typedef struct client_struct -{ - char *msg; - char *ip; - time_t time; -} client_t; - -static int my_argc; -static char **my_argv; - -char* getParam(char *s) -{ - int i; - int len; - - len = strlen(s); - - for( i = 1 ; i < my_argc ; i++ ) - { - //printf("%s %s\n", s, my_argv[i]); - - if( strlen(my_argv[i]) < len ) - { - continue; - } - - if( strncmp(s, my_argv[i], len) == 0 ) - { - return strchr(my_argv[i], '=')+1; - } - } - - return NULL; -} - -char* getParamElse(char *s1, char *s2) -{ - char *ret; - ret = getParam(s1); - - if( ret == NULL) - { - return s2; - } - - return ret; -} - -bool_t isParamFlag(char *s) -{ - int i; - - for( i = 0 ; i < my_argc ; i++ ) - { - if( strcmp(s, my_argv[i]) == 0 ) - { - return TRUE; - } - } - - return FALSE; -} - -client_t* newClient(char *str_ip, char *msg) -{ - client_t *new; - - new = malloc( sizeof(client_t) ); - memset(new, 0, sizeof(client_t)); - new->ip = strdup(str_ip); - new->msg = strdup(msg); - new->time = time(NULL); - - return new; -} - -void replaceClient(client_t *p, char *msg) -{ - assert( p != NULL ); - - free(p->msg); - p->msg = strdup(msg); - p->time = time(NULL); -} - -void destroyClient(client_t *p) -{ - assert( p != NULL ); - - free(p->ip); - free(p->msg); - free(p); -} - -void cmd_register(sock_udp_t *client, char *msg) -{ - char str_ip[STR_IP4_SIZE]; - int len; - int i; - - if( listClient->count > limitRegisterGameServer ) - { - return; - } - - getSockUdpIp(client, str_ip); - - len = strlen(msg); - - if( msg[len-1] == '\n' ) - { - msg[len-1] = '\0'; - len--; - } - - for( i = 0 ; i < len ; i++ ) - { - if( msg[i] < ' ' || msg[i] > '~' ) - { - msg[i] = '?'; - } - } - - for( i = 0 ; i < listClient->count ; i++ ) - { - client_t *client; - - client = (client_t *)listClient->list[i]; - - if( strcmp(client->ip, str_ip) == 0 ) - { - replaceClient(client, msg+9); - return; - } - } - - addList(listClient, newClient(str_ip, msg+9) ); -} - -void cmd_list(sock_udp_t *socketClient, char *msg) -{ - time_t currnetTime; - char *out_msg; - int len_out_msg; - int i; - - if( listClient->count == 0 ) - { - writeUdpSocket(socketServer, socketClient, "<|>\n", 4); - return; - } - - currnetTime = time(NULL); - - len_out_msg = listClient->count * STR_MSG_SIZE; - out_msg = malloc(len_out_msg); - memset(out_msg, 0, len_out_msg); - - len_out_msg--; - - for( i = 0 ; i < listClient->count ; i++ ) - { - client_t *client; - char line[STR_MSG_SIZE*2]; - int len_new_line; - - client = (client_t *)listClient->list[i]; - - if( currnetTime - client->time > limitTime ) - { - delListItem(listClient, i, destroyClient); - i--; - continue; - } - - sprintf(line, "ip=\"%s\" %s\n", client->ip, client->msg); - len_new_line = strlen(line); - - if( len_out_msg > len_new_line ) - { - strcat(out_msg, line); - len_out_msg -= len_new_line; - } - else - { - break; - } - } - - if( listClient->count == 0 ) - { - writeUdpSocket(socketServer, socketClient, "<|>\n", 4); - free(out_msg); - return; - } - - writeUdpSocket(socketServer, socketClient, out_msg, strlen(out_msg) ); - free(out_msg); -} - -void my_handler_quit(int n) -{ - printf("my_handler_quit\n"); - - destroyListItem(listClient, destroyClient); - closeUdpSocket(socketServer); - destroySockUdp(socketClient); - - exit(0); -} - -int main(int argc, char **argv) -{ - my_argc = argc; - my_argv = argv; - - signal(SIGPIPE, SIG_IGN); - signal(SIGINT, my_handler_quit); - signal(SIGTERM, my_handler_quit); - signal(SIGQUIT, my_handler_quit); - - listClient = newList(); - - limitRegisterGameServer = atoi( getParamElse("--limitclient", LIMIT_REGISTER) ); - limitTime = atoi( getParamElse("--limittime", LIMIT_TIME) ); - - socketServer = bindUdpSocket( - getParamElse("--ip", "0.0.0.0"), - atoi(getParamElse("--port", "8400")) ); - - socketClient = newSockUdp(); - - for(;;) - { - char msg[STR_MSG_SIZE]; - - memset(msg, 0, STR_MSG_SIZE); - readUdpSocket(socketServer, socketClient, msg, STR_MSG_SIZE); - - if( strncmp(msg, "register", 8) == 0 ) - { - cmd_register(socketClient, msg); - } - - if( strncmp(msg, "list", 4) == 0 ) - { - cmd_list(socketClient, msg); - } - } - - return 0; -} diff --git a/src/logFile.c b/src/logFile.c deleted file mode 100644 index 12a4df8..0000000 --- a/src/logFile.c +++ /dev/null @@ -1,46 +0,0 @@ - -#include -#include -#include -#include -#include - -typedef struct logFile_struct -{ - char *name; - FILE *file; -} logFile_t; - -logFile_t* newLogFile(char *s) -{ - logFile_t *new; - - assert( s != NULL ); - - new = malloc( sizeof(logFile_t) ); - memset(new, 0, sizeof(logFile_t) ); - - new->name = strdup(s); - new->file = fopen(new->name, "a+"); - - assert( new->file != NULL ); - - return new; -} - -int writeLog(logFile_t *p, char *s) -{ - assert( p != NULL ); - assert( s != NULL ); - - return fprintf(p->file, s); -} - -void destroyLogFile(logFile_t *p) -{ - assert( p != NULL ); - - free(p->name); - fclose(p->file); - free(p); -} diff --git a/src/main.c b/src/main.c deleted file mode 100644 index f2482f3..0000000 --- a/src/main.c +++ /dev/null @@ -1,125 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "main.h" -#include "tux.h" -#include "game.h" - -#ifdef PUBLIC_SERVER - -#include "publicServer.h" - -#endif - -static int my_argc; -static char **my_argv; - -list_t *listHelp; - -char* getParam(char *s) -{ - int i; - int len; - - len = strlen(s); - - for( i = 1 ; i < my_argc ; i++ ) - { - //printf("%s %s\n", s, my_argv[i]); - - if( strlen(my_argv[i]) < len ) - { - continue; - } - - if( strncmp(s, my_argv[i], len) == 0 ) - { - return strchr(my_argv[i], '=')+1; - } - } - - return NULL; -} - -char* getParamElse(char *s1, char *s2) -{ - char *ret; - ret = getParam(s1); - - if( ret == NULL) - { - return s2; - } - - return ret; -} - -bool_t isParamFlag(char *s) -{ - int i; - - for( i = 0 ; i < my_argc ; i++ ) - { - if( strcmp(s, my_argv[i]) == 0 ) - { - return TRUE; - } - } - - return FALSE; -} - -char *getString(int n) -{ - char str[STR_NUM_SIZE]; - sprintf(str, "%d", n); - return strdup(str); -} - -int* newInt(int x) -{ - int *new; - new = malloc( sizeof(int) ); - *new = x; - return new; -} - -void accessExistFile(const char *s) -{ - if( access(s, F_OK) != 0 ) - { - fprintf(stderr, "File %s not found !\nProgram shutdown !\n", s); - exit(-1); - } -} - -int main(int argc, char *argv[]) -{ - listHelp = newList(); - - srand( (unsigned) time(NULL) ); - - my_argc = argc; - my_argv = argv; -/* - test_space(); - exit(0); -*/ - -#ifndef PUBLIC_SERVER - startGame(); -#endif - -#ifdef PUBLIC_SERVER - startPublicServer(); -#endif - - return 0; -} diff --git a/src/modules.c b/src/modules.c deleted file mode 100755 index 34224a4..0000000 --- a/src/modules.c +++ /dev/null @@ -1,223 +0,0 @@ - -#include -#include -#include -#include -#include - -#include "main.h" -#include "configFile.h" -#include "list.h" -#include "path.h" -#include "tux.h" -#include "shot.h" -#include "modules.h" -#include "tux.h" -#include "arena.h" -#include "proto.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#include "layer.h" -#endif - -int pokus(char *s) -{ - printf("s = %s\n", s); - return 0; -} - -static export_fce_t export_fce = - { -#ifndef PUBLIC_SERVER - .fce_addLayer = addLayer, - .fce_getImage = getImage, -#endif - .fce_getValue = getValue, - .fce_getNetTypeGame = getNetTypeGame, - .fce_getTuxProportion = getTuxProportion, - .fce_setTuxProportion = setTuxProportion, - //.fce_getTuxID = getTuxID, - .fce_actionTux = actionTux, - - .fce_getMyTime = getMyTime, - - .fce_getCurrentArena = getCurrentArena, - .fce_conflictSpace = conflictSpace, - .fce_isFreeSpace = isFreeSpace, - .fce_findFreeSpace = findFreeSpace, - .fce_proto_send_newtux_server = proto_send_newtux_server, - .fce_proto_send_shot_server = proto_send_shot_server, - .fce_destroyShot = destroyShot, - .fce_boundBombBall = boundBombBall, - .fce_transformOnlyLasser = transformOnlyLasser - }; - -static list_t *listModule; - -static void* getFce(module_t *p, char *s) -{ - void *ret; - char *error; - - assert( p != NULL ); - assert( s != NULL ); - - ret = dlsym(p->image, s); - - if( ( error = dlerror() ) != NULL) - { - fprintf (stderr, "error = %s\n", error); - return NULL; - } - - return (void *)ret; -} - -static module_t* newModule(char *name) -{ - module_t *ret; - - assert( name != NULL ); - - ret = malloc( sizeof(module_t) ); - - ret->image = dlopen (name, RTLD_LAZY); - - if(!ret->image) - { - fputs (dlerror(), stderr); - free(ret); - return NULL; - } - - ret->file = strdup(name); - - ret->fce_init = getFce(ret, "init"); -#ifndef PUBLIC_SERVER - ret->fce_draw = getFce(ret, "draw"); -#endif - ret->fce_event = getFce(ret, "event"); - ret->fce_destroy = getFce(ret, "destroy"); - ret->fce_isConflict = getFce(ret, "isConflict"); - ret->fce_cmd = getFce(ret, "cmd"); - - printf("load module.. (%s)\n", name); - - if( ret->fce_init(&export_fce) != 0 ) - { - fprintf(stderr, "init module failed !\n"); - - dlclose(ret->image); - free(ret->file); - free(ret); - return NULL; - } - - return ret; -} - -static int destroyModule(module_t *p) -{ - assert( p != NULL ); - - p->fce_destroy(); - - free(p->file); - dlclose(p->image); - free(p); - - printf("destroy module..\n"); - - return 0; -} - -void initModule() -{ - listModule = newList(); -} - -int loadModule(char *name) -{ - char str[STR_PATH_SIZE]; - module_t *module; - - sprintf(str, PATH_MODULES "%s", name); - accessExistFile(str); - - module = newModule(str); - - if( module == NULL ) - { - fprintf(stderr, "load module %s failed !\n", name); - return -1; - } - - addList(listModule, module); - - return 0; -} - -void cmdModule(char *s) -{ - int i; - - for( i = 0 ; i < listModule->count ; i++ ) - { - module_t *this; - this = (module_t *)listModule->list[i]; - this->fce_cmd(s); - } -} - -#ifndef PUBLIC_SERVER - -void drawModule(int x, int y, int w, int h) -{ - int i; - - for( i = 0 ; i < listModule->count ; i++ ) - { - module_t *this; - this = (module_t *)listModule->list[i]; - this->fce_draw(x, y, w, h); - } -} - -#endif - -void eventModule() -{ - int i; - - for( i = 0 ; i < listModule->count ; i++ ) - { - module_t *this; - this = (module_t *)listModule->list[i]; - this->fce_event(); - } -} - -int isConflictModule(int x, int y, int w, int h) -{ - int i; - - for( i = 0 ; i < listModule->count ; i++ ) - { - module_t *this; - this = (module_t *)listModule->list[i]; - - if( this->fce_isConflict(x, y, w, h) == 1 ) - { - return 1; - } - } - - return 0; -} - -void quitModule() -{ - destroyListItem(listModule, destroyModule); -} diff --git a/src/modules/Makefile b/src/modules/Makefile new file mode 100755 index 0000000..39e4388 --- /dev/null +++ b/src/modules/Makefile @@ -0,0 +1,22 @@ + +all: modWall.so modPipe.so modTeleport.so modAI.so + +modWall.so: + gcc $(CFLAGS) -fPIC -c modWall.c -o modWall.o + gcc $(CFLAGS) -I../include -shared -fPIC -o modWall.so modWall.o ../base/list.o ../base/space.o + +modPipe.so: + gcc $(CFLAGS) -fPIC -c modPipe.c -o modPipe.o + gcc $(CFLAGS) -I../include -shared -fPIC -o modPipe.so modPipe.o ../base/list.o ../base/space.o + +modTeleport.so: + gcc $(CFLAGS) -fPIC -c modTeleport.c -o modTeleport.o + gcc $(CFLAGS) -I../include -shared -fPIC -o modTeleport.so modTeleport.o ../base/list.o ../base/space.o + +modAI.so: + gcc $(CFLAGS) -fPIC -c modAI.c -o modAI.o + gcc $(CFLAGS) -I../include -shared -fPIC -o modAI.so modAI.o ../base/list.o ../base/space.o + +clean: + rm -rf *.o + rm -rf *.so diff --git a/src/modules/modAI.c b/src/modules/modAI.c new file mode 100755 index 0000000..d3a9f78 --- /dev/null +++ b/src/modules/modAI.c @@ -0,0 +1,256 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/modules.h" +#include "base/tux.h" +#include "base/shot.h" +#include "base/list.h" +#include "base/gun.h" +#include "base/space.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#include "client/image.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +static export_fce_t *export_fce; + +static void cmd_ai(char *line) +{ +} + +int init(export_fce_t *p) +{ + export_fce = p; + + return 0; +} + +#ifndef PUBLIC_SERVER + +int draw(int x, int y, int w, int h) +{ + return 0; +} +#endif + +///////////////// + +int colisionAll(tux_t *tux, int px, int py) +{ + arena_t *arena; + int x, y, w, h; + + arena = export_fce->fce_getCurrentArena(); + + export_fce->fce_getTuxProportion(tux, &x, &y, &w, &h); + x += px; + y += py; + + return export_fce->fce_isFreeSpace(arena, x, y, w, h); +} + +void gotoTux(tux_t *tux, int x, int y, int w, int h) +{ + int px = 0,py = 0; + int smer = TUX_DOWN; + + if(tux->x > x+w ) px = -TUX_STEP; + if(tux->x < x ) px = +TUX_STEP; + if(tux->y > y+h ) py = -TUX_STEP; + if(tux->y < y ) py = +TUX_STEP; + + if( px != 0 && py !=0 ) px = 0; + + while( colisionAll(tux, px, 0 ) ) + { + px=0; + if(py==0) py = +TUX_STEP; + goto sem; + } + + while( colisionAll(tux, 0, py ) ) + { + py = 0; + if( px == 0 ) px = +TUX_STEP; + } + + sem: + if( px != 0 && py != 0 ) px = 0; + + if( py== -TUX_STEP )smer = TUX_UP; + if( px== +TUX_STEP )smer = TUX_RIGHT; + if( px== -TUX_STEP )smer = TUX_LEFT; + if( py== +TUX_STEP )smer = TUX_DOWN; + + //printf("smer=%d\n",smer); + export_fce->fce_actionTux(tux, smer); +} + +void gotoTuxShot(tux_t *tux, int x, int y, int w, int h) +{ + if( tux->x > x && tux->x < x+w ) + { + if( tux->y > y+h )tux->position = TUX_UP; + if( tux->y < y )tux->position = TUX_DOWN; + + if( colisionAll(tux, 0, 0) == 0 ) + { + if( tux->shot[GUN_SIMPLE] > 0 )tux->gun = GUN_SIMPLE; + if( tux->shot[GUN_DUAL_SIMPLE] > 0 )tux->gun = GUN_DUAL_SIMPLE; + if( tux->shot[GUN_SCATTER] > 0 )tux->gun = GUN_SCATTER; + if( tux->shot[GUN_TOMMY] > 0 )tux->gun = GUN_TOMMY; + if( tux->shot[GUN_LASSER] > 0 )tux->gun = GUN_LASSER; + if( tux->shot[GUN_BOMBBALL] > 0 )tux->gun = GUN_BOMBBALL; + + export_fce->fce_actionTux(tux, TUX_SHOT); + } + } + + if( tux->y > y && tux->y < y+h ) + { + if( tux->x > x+w )tux->position=TUX_LEFT; + if( tux->x < x )tux->position=TUX_RIGHT; + + if( colisionAll(tux, 0 , 0) == 0 ) + { + if( tux->shot[GUN_SIMPLE] > 0 )tux->gun = GUN_SIMPLE; + if( tux->shot[GUN_DUAL_SIMPLE] > 0 )tux->gun = GUN_DUAL_SIMPLE; + if( tux->shot[GUN_SCATTER] > 0 )tux->gun = GUN_SCATTER; + if( tux->shot[GUN_TOMMY] > 0 )tux->gun = GUN_TOMMY; + if( tux->shot[GUN_LASSER] > 0 )tux->gun = GUN_LASSER; + if( tux->shot[GUN_BOMBBALL] > 0 )tux->gun = GUN_BOMBBALL; + + export_fce->fce_actionTux(tux, TUX_SHOT); + } + } +} + +void killTux(tux_t *tux, int active) +{ + arena_t *arena; + int i; + + arena = export_fce->fce_getCurrentArena(); + + for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) + { + tux_t *thisTux; + + thisTux = arena->spaceTux->list->list[i]; + + if( thisTux->control != TUX_CONTROL_KEYBOARD_LEFT && + thisTux->status == TUX_STATUS_ALIVE ) + { + gotoTuxShot(tux, thisTux->x, thisTux->y, + TUX_WIDTH, TUX_HEIGHT); + + if( active ) + { + gotoTux(tux, thisTux->x, thisTux->y, + TUX_WIDTH, TUX_HEIGHT); + + gotoTuxShot(tux, thisTux->x, thisTux->y, + TUX_WIDTH, TUX_HEIGHT); + + break; + } + } + } +} + +///////////////// + +static void eventTux(tux_t *tux) +{ + arena_t *arena; + item_t *item; + + arena = export_fce->fce_getCurrentArena(); + + killTux(tux, 0); + + item = NULL; + + if( arena->spaceItem->list->count > 0 ) + { + item = (item_t *) arena->spaceItem->list->list[0]; + } + + if( item != NULL && + item->type != GUN_MINE ) + { + gotoTux(tux, item->x, item->y, + item->w, item->h); + } + else + { + killTux(tux, 1); + } +} + +int event() +{ + static my_time_t lastEvent = 0; + my_time_t curentTime; + arena_t *arena; + int countTuxAI; + int i; + + if( lastEvent == 0 ) + { + lastEvent = export_fce->fce_getMyTime(); + } + + curentTime = export_fce->fce_getMyTime(); + + if( curentTime - lastEvent < 25 ) + { + return 0; + } + + lastEvent = export_fce->fce_getMyTime(); + //printf("event AI\n"); + + arena = export_fce->fce_getCurrentArena(); + countTuxAI = 0; + + for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) + { + tux_t *thisTux; + + thisTux = arena->spaceTux->list->list[i]; + + if( thisTux->control == TUX_CONTROL_AI && + thisTux->status == TUX_STATUS_ALIVE ) + { + eventTux(thisTux); + countTuxAI = 0; + } + } + + return 0; +} + +int isConflict(int x, int y, int w, int h) +{ + return 0; +} + +void cmd(char *line) +{ + if( strncmp(line, "ai", 2) == 0 )cmd_ai(line); +} + +int destroy() +{ + return 0; +} diff --git a/src/modules/modPipe.c b/src/modules/modPipe.c new file mode 100755 index 0000000..77f2c05 --- /dev/null +++ b/src/modules/modPipe.c @@ -0,0 +1,453 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/modules.h" +#include "base/tux.h" +#include "base/shot.h" +#include "base/list.h" +#include "base/gun.h" +#include "base/space.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#include "client/image.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +typedef struct pipe_struct +{ + int x; // poloha steny + int y; + + int w; // rozmery steny + int h; + + int id; + int id_out; + int position; + + int layer; // vrstva + +#ifndef PUBLIC_SERVER + SDL_Surface *img; //obrazok +#endif +} pipe_t; + +static export_fce_t *export_fce; + +static list_t *listPipe; +static space_t *spacePipe; + +#ifndef PUBLIC_SERVER +static pipe_t* newPipe(int x, int y, int w, int h, int layer, int id, int id_out, int position, SDL_Surface *img) +#endif + +#ifdef PUBLIC_SERVER +static pipe_t* newPipe(int x, int y, int w, int h, int layer, int id, int id_out, int position) +#endif + +{ + pipe_t *new; + +#ifndef PUBLIC_SERVER + assert( img != NULL ); +#endif + + new = malloc( sizeof(pipe_t) ); + assert( new != NULL ); + + new->x = x; + new->y = y; + new->w = w; + new->h = h; + new->layer = layer; + new->id = id; + new->id_out = id_out; + new->position = position; +#ifndef PUBLIC_SERVER + new->img = img; +#endif + return new; +} + +static void setStatusPipe(void *p, int x, int y, int w, int h) +{ + pipe_t *pipe; + + pipe = p; + + pipe->x = x; + pipe->y = y; + pipe->w = w; + pipe->h = h; +} + +static void getStatusPipe(void *p, int *id, int *x, int *y, int *w, int *h) +{ + pipe_t *pipe; + + pipe = p; + + *id = pipe->id; + *x = pipe->x; + *y = pipe->y; + *w = pipe->w; + *h = pipe->h; +} + +#ifndef PUBLIC_SERVER + +static void drawPipe(pipe_t *p) +{ + assert( p != NULL ); + + export_fce->fce_addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, p->layer); +} + +#endif + +static void destroyPipe(pipe_t *p) +{ + assert( p != NULL ); + free(p); +} + + +static void cmd_teleport(char *line) +{ + char str_x[STR_NUM_SIZE]; + char str_y[STR_NUM_SIZE]; + char str_w[STR_NUM_SIZE]; + char str_h[STR_NUM_SIZE]; + char str_id[STR_NUM_SIZE]; + char str_id_out[STR_NUM_SIZE]; + char str_position[STR_NUM_SIZE]; + char str_layer[STR_NUM_SIZE]; + char str_image[STR_SIZE]; + pipe_t *new; + + if( export_fce->fce_getValue(line, "x", str_x, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "y", str_y, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "w", str_w, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "h", str_h, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "id", str_id, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "id_out", str_id_out, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "position", str_position, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "image", str_image, STR_SIZE) != 0 )return; + +#ifndef PUBLIC_SERVER + new = newPipe(atoi(str_x), atoi(str_y), + atoi(str_w), atoi(str_h), + atoi(str_layer), atoi(str_id), atoi(str_id_out), atoi(str_position), + export_fce->fce_getImage(IMAGE_GROUP_USER, str_image) ); +#endif + +#ifdef PUBLIC_SERVER + new = newPipe(atoi(str_x), atoi(str_y), + atoi(str_w), atoi(str_h), + atoi(str_layer), atoi(str_id), atoi(str_id_out), atoi(str_position) ); +#endif + + if( spacePipe == NULL ) + { + spacePipe = newSpace(export_fce->fce_getCurrentArena()->w, export_fce->fce_getCurrentArena()->h, + 320, 240, getStatusPipe, setStatusPipe); + } + + addObjectToSpace(spacePipe, new); +} + +static int myAbs(int n) +{ + return ( n > 0 ? n : -n ); +} + + +static int getSppedShot(shot_t *shot) +{ + return ( myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py) ); +} + +static void transformShot(shot_t *shot, int position) +{ + int speed; + + speed = getSppedShot(shot); + + switch( position ) + { + case TUX_UP : + shot->px = 0; + shot->py = -speed; + break; + + case TUX_LEFT : + shot->px = -speed; + shot->py = 0; + break; + + case TUX_RIGHT : + shot->px = speed; + shot->py = 0; + break; + + case TUX_DOWN : + shot->px = 0; + shot->py = +speed; + break; + } + + shot->position = position; + shot->isCanKillAuthor = TRUE; + + if( shot->gun == GUN_LASSER ) + { + export_fce->fce_transformOnlyLasser(shot); + } +} + +static void moveShot(shot_t *shot, int position, int src_x, int src_y, + int dist_x, int dist_y, int dist_w, int dist_h) +{ + int offset = 0; + int new_x, new_y; + + switch( shot->position ) + { + case TUX_UP : + case TUX_DOWN : + offset = shot->x - src_x; + break; + + case TUX_RIGHT : + case TUX_LEFT : + offset = shot->y - src_y; + break; + } + + transformShot(shot, position); + + switch( shot->position ) + { + case TUX_UP : + new_x = dist_x + offset; + new_y = dist_y; + break; + + case TUX_LEFT : + new_x = dist_x; + new_y = dist_y + offset; + break; + + case TUX_RIGHT : + new_x = dist_x + dist_w; + new_y = dist_y + offset; + break; + + case TUX_DOWN : + new_x = dist_x + offset; + new_y = dist_y + dist_h; + break; + } + + new_y += shot->px; + new_y += shot->py; + + moveObjectInSpace(export_fce->fce_getCurrentArena()->spaceShot, shot, new_x, new_y); + + if( export_fce->fce_getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + export_fce->fce_proto_send_shot_server(PROTO_SEND_ALL, NULL, shot); + } +} + +static void moveShotFromPipe(shot_t *shot, pipe_t *pipe) +{ + pipe_t *distPipe; + + distPipe = getObjectFromSpaceWithID(spacePipe, pipe->id_out); + + if( distPipe == NULL ) + { + fprintf(stderr, "Pipe %d ID not found\n", pipe->id); + return; + } + + moveShot(shot, distPipe->position, pipe->x, pipe->y, + distPipe->x, distPipe->y, distPipe->w, distPipe->h); +} + +int init(export_fce_t *p) +{ + export_fce = p; + + listPipe = newList(); + + return 0; +} + +#ifndef PUBLIC_SERVER + +int draw(int x, int y, int w, int h) +{ + pipe_t *thisPipe; + int i; + + if( spacePipe == NULL ) + { + return 0; + } + + listDoEmpty(listPipe); + getObjectFromSpace(spacePipe, x, y, w, h, listPipe); + //printSpace(spacePipe); + + for( i = 0 ; i < listPipe->count ; i++ ) + { + thisPipe = (pipe_t *)listPipe->list[i]; + assert( thisPipe != NULL ); + drawPipe(thisPipe); + } + + return 0; +} +#endif + +static int negPosition(int n) +{ + switch( n ) + { + case TUX_UP : + return TUX_DOWN; + + case TUX_LEFT : + return TUX_RIGHT; + + case TUX_RIGHT : + return TUX_LEFT; + + case TUX_DOWN : + return TUX_UP; + + default : + assert( ! "premenna n ma zlu hodnotu !" ); + break; + } +} + +int event() +{ + pipe_t *thisPipe; + arena_t *arena; + int i; + + if( spacePipe == NULL ) + { + return 0; + } + +/* + if( export_fce->fce_getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + return; + } +*/ + arena = export_fce->fce_getCurrentArena(); + + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) + { + shot_t *thisShot; + int j; + + thisShot = (shot_t *) arena->spaceShot->list->list[i]; + assert( thisShot != NULL ); + + listDoEmpty(listPipe); + getObjectFromSpace(spacePipe, thisShot->x, thisShot->y, thisShot->w, thisShot->h, listPipe); + + for( j = 0 ; j < listPipe->count ; j++ ) + { + tux_t *author; + + thisPipe = (pipe_t *)listPipe->list[j]; + + author = getObjectFromSpaceWithID(arena->spaceTux, thisShot->author_id); + + if( author != NULL && + author->bonus == BONUS_GHOST && + author->bonus_time > 0 ) + { + continue; + } + + if( negPosition( thisShot->position ) == thisPipe->position ) + { + //moveShotFromPipe(thisShot, thisPipe, listPipe); + + if( export_fce->fce_getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + if( thisShot->gun != GUN_BOMBBALL ) + { + delObjectFromSpaceWithObject(export_fce->fce_getCurrentArena()->spaceShot, + thisShot, export_fce->fce_destroyShot); + //delListItem(arena->listShot, i, export_fce->fce_destroyShot); + i--; + } + } + else + { + moveShotFromPipe(thisShot, thisPipe); + } + + } + else + { + if( thisShot->gun == GUN_BOMBBALL && + export_fce->fce_getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + export_fce->fce_boundBombBall(thisShot); + continue; + } + else + { + delObjectFromSpaceWithObject(export_fce->fce_getCurrentArena()->spaceShot, + thisShot, export_fce->fce_destroyShot); + i--; + } + } + } + } + + return 0; +} + +int isConflict(int x, int y, int w, int h) +{ + if( spacePipe == NULL ) + { + return 0; + } + + return isConflictWithObjectFromSpace(spacePipe, x, y, w, h); +} + +void cmd(char *line) +{ + if( strncmp(line, "pipe", 4) == 0 )cmd_teleport(line); +} + +int destroy() +{ + destroySpaceWithObject(spacePipe, destroyPipe); + destroyList(listPipe); + + return 0; +} diff --git a/src/modules/modTeleport.c b/src/modules/modTeleport.c new file mode 100755 index 0000000..724e371 --- /dev/null +++ b/src/modules/modTeleport.c @@ -0,0 +1,506 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/modules.h" +#include "base/tux.h" +#include "base/shot.h" +#include "base/list.h" +#include "base/gun.h" +#include "base/space.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#include "client/image.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +typedef struct teleport_struct +{ + int id; + + int x; // poloha steny + int y; + + int w; // rozmery steny + int h; + + int layer; // vrstva + +#ifndef PUBLIC_SERVER + SDL_Surface *img; //obrazok +#endif +} teleport_t; + +static export_fce_t *export_fce; + +static space_t *spaceTeleport; +static list_t *listTeleport; + +#ifndef PUBLIC_SERVER +teleport_t* newTeleport(int x, int y, int w, int h, int layer, SDL_Surface *img) +#endif + +#ifdef PUBLIC_SERVER +teleport_t* newTeleport(int x, int y, int w, int h, int layer) +#endif +{ + static int last_id = 0; + + teleport_t *new; + +#ifndef PUBLIC_SERVER + assert( img != NULL ); +#endif + + new = malloc( sizeof(teleport_t) ); + assert( new != NULL ); + + new->id = ++last_id; + new->x = x; + new->y = y; + new->w = w; + new->h = h; + new->layer = layer; +#ifndef PUBLIC_SERVER + new->img = img; +#endif + + return new; +} + +static void setStatusTeleport(void *p, int x, int y, int w, int h) +{ + teleport_t *teleport; + + teleport = p; + + teleport->x = x; + teleport->y = y; + teleport->w = w; + teleport->h = h; +} + +static void getStatusTeleport(void *p, int *id, int *x, int *y, int *w, int *h) +{ + teleport_t *teleport; + teleport = p; + + *id = teleport->id; + *x = teleport->x; + *y = teleport->y; + *w = teleport->w; + *h = teleport->h; +} + +#ifndef PUBLIC_SERVER + +static void drawTeleport(teleport_t *p) +{ + assert( p != NULL ); + + export_fce->fce_addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, p->layer); +} + +#endif + +static void destroyTeleport(teleport_t *p) +{ + assert( p != NULL ); + free(p); +} + + +static void cmd_teleport(char *line) +{ + char str_x[STR_NUM_SIZE]; + char str_y[STR_NUM_SIZE]; + char str_w[STR_NUM_SIZE]; + char str_h[STR_NUM_SIZE]; + char str_layer[STR_NUM_SIZE]; + char str_image[STR_SIZE]; + teleport_t *new; + + if( export_fce->fce_getValue(line, "x", str_x, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "y", str_y, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "w", str_w, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "h", str_h, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "image", str_image, STR_SIZE) != 0 )return; + +#ifndef PUBLIC_SERVER + new = newTeleport(atoi(str_x), atoi(str_y), + atoi(str_w), atoi(str_h), + atoi(str_layer), export_fce->fce_getImage(IMAGE_GROUP_USER, str_image) ); +#endif + +#ifdef PUBLIC_SERVER + new = newTeleport(atoi(str_x), atoi(str_y), + atoi(str_w), atoi(str_h), + atoi(str_layer) ); +#endif + + if( spaceTeleport == NULL ) + { + spaceTeleport = newSpace(export_fce->fce_getCurrentArena()->w, export_fce->fce_getCurrentArena()->h, + 320, 240, getStatusTeleport, setStatusTeleport); + } + + addObjectToSpace(spaceTeleport, new); +} + +static teleport_t* getRandomTeleportBut(list_t *list, teleport_t *teleport) +{ + int x; + + do{ + x = random() % list->count; + }while( list->list[x] == teleport ); + + return (teleport_t *) list->list[x]; +} + +static int getRandomPosition() +{ + switch( random() % 4 ) + { + case 0 : + return TUX_UP; + + case 1 : + return TUX_LEFT; + + case 2 : + return TUX_RIGHT; + + case 3 : + return TUX_DOWN; + } + + assert( ! "Stupid error ! " ); +} + +static void teleportTux(tux_t *tux, teleport_t *teleport) +{ + teleport_t *distTeleport; + int dist_x, dist_y; + + if( tux->bonus == BONUS_GHOST || + export_fce->fce_getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + return; + } + + distTeleport = getRandomTeleportBut(spaceTeleport->list, teleport); + + //export_fce->fce_getTuxProportion(tux, ¤t_x, ¤t_y, NULL, NULL); + + switch( tux->position ) + { + case TUX_UP : + dist_x = distTeleport->x + distTeleport->w/2; + dist_y = distTeleport->y - ( TUX_HEIGHT + 20 ); + break; + case TUX_LEFT : + dist_x = distTeleport->x - ( TUX_WIDTH + 20 ); + dist_y = distTeleport->y + distTeleport->h/2; + break; + + case TUX_RIGHT : + dist_x = distTeleport->x + distTeleport->w + 20; + dist_y = distTeleport->y + distTeleport->h/2; + break; + + case TUX_DOWN : + dist_x = distTeleport->x + distTeleport->w/2; + dist_y = distTeleport->y + distTeleport->h + 20; + break; + + default : + assert( ! "premenna p->control ma zlu hodnotu !" ); + break; + } + + if( export_fce->fce_isFreeSpace(export_fce->fce_getCurrentArena(), dist_x, dist_y, TUX_WIDTH, TUX_HEIGHT) ) + { + moveObjectInSpace(export_fce->fce_getCurrentArena()->spaceTux, tux, dist_x, dist_y); + //export_fce->fce_setTuxProportion(tux, dist_x, dist_y); +#ifndef PUBLIC_SERVER + //playSound("teleport", SOUND_GROUP_BASE); +#endif + if( export_fce->fce_getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + export_fce->fce_proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); + } + } +} + +static int myAbs(int n) +{ + return ( n > 0 ? n : -n ); +} + + +static int getSppedShot(shot_t *shot) +{ + return ( myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py) ); +} + +/* +static void transformOnlyLasser(shot_t *shot) +{ + switch( shot->position ) + { + case TUX_RIGHT : + case TUX_LEFT : + shot->w = GUN_LASSER_HORIZONTAL; + shot->h = GUN_SHOT_VERTICAL; +#ifndef PUBLIC_SERVER + shot->img = g_shot_lasserX; +#endif + break; + case TUX_UP : + case TUX_DOWN : + shot->w = GUN_SHOT_VERTICAL; + shot->h = GUN_LASSER_HORIZONTAL; +#ifndef PUBLIC_SERVER + shot->img = g_shot_lasserY; +#endif + break; + } +} +*/ + +static void transformShot(shot_t *shot, int position) +{ + int speed; + + speed = getSppedShot(shot); + + switch( position ) + { + case TUX_UP : + shot->px = 0; + shot->py = -speed; + break; + + case TUX_LEFT : + shot->px = -speed; + shot->py = 0; + break; + + case TUX_RIGHT : + shot->px = speed; + shot->py = 0; + break; + + case TUX_DOWN : + shot->px = 0; + shot->py = +speed; + break; + } + + shot->position = position; + shot->isCanKillAuthor = TRUE; + + if( shot->gun == GUN_LASSER ) + { + export_fce->fce_transformOnlyLasser(shot); + } +} + +static void moveShot(shot_t *shot, int position, int src_x, int src_y, + int dist_x, int dist_y, int dist_w, int dist_h) +{ + int offset = 0; + int new_x, new_y; + + switch( shot->position ) + { + case TUX_UP : + case TUX_DOWN : + offset = shot->x - src_x; + break; + + case TUX_RIGHT : + case TUX_LEFT : + offset = shot->y - src_y; + break; + } + + transformShot(shot, position); + + switch( shot->position ) + { + case TUX_UP : + new_x = dist_x + offset; + new_y = dist_y; + break; + + case TUX_LEFT : + new_x = dist_x; + new_y = dist_y + offset; + break; + + case TUX_RIGHT : + new_x = dist_x + dist_w; + new_y = dist_y + offset; + break; + + case TUX_DOWN : + new_x = dist_x + offset; + new_y = dist_y + dist_h; + break; + } + + new_y += shot->px; + new_y += shot->py; + + moveObjectInSpace(export_fce->fce_getCurrentArena()->spaceShot, shot, new_x, new_y); + + if( export_fce->fce_getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + export_fce->fce_proto_send_shot_server(PROTO_SEND_ALL, NULL, shot); + } +} + +static void moveShotFromTeleport(shot_t *shot, teleport_t *teleport, list_t *list) +{ + teleport_t *distTeleport; + + distTeleport = getRandomTeleportBut(list, teleport); + + moveShot(shot, getRandomPosition(), teleport->x, teleport->y, + distTeleport->x, distTeleport->y, distTeleport->w, distTeleport->h); +} + +int init(export_fce_t *p) +{ + export_fce = p; + + listTeleport = newList(); + + return 0; +} + +#ifndef PUBLIC_SERVER +int draw(int x, int y, int w, int h) +{ + teleport_t *thisTeleport; + int i; + + if( spaceTeleport == NULL ) + { + return 0; + } + + listDoEmpty(listTeleport); + getObjectFromSpace(spaceTeleport, x, y, w, h, listTeleport); + + for( i = 0 ; i < listTeleport->count ; i++ ) + { + thisTeleport = (teleport_t *)listTeleport->list[i]; + assert( thisTeleport != NULL ); + drawTeleport(thisTeleport); + } + + return 0; +} +#endif + +int event() +{ + teleport_t *thisTeleport; + arena_t *arena; + int i; + + if( spaceTeleport == NULL ) + { + return 0; + } + + if( export_fce->fce_getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + return 0; + } + + arena = export_fce->fce_getCurrentArena(); + + for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) + { + tux_t *thisTux; + int x, y, w, h; + int j; + + thisTux = (tux_t *)arena->spaceTux->list->list[i]; + export_fce->fce_getTuxProportion(thisTux, &x, &y, &w, &h); + + listDoEmpty(listTeleport); + getObjectFromSpace(spaceTeleport, x, y, w, h, listTeleport); + + for( j = 0 ; j < listTeleport->count ; j++ ) + { + thisTeleport = (teleport_t *)listTeleport->list[j]; + teleportTux(thisTux, thisTeleport); + } + } + + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) + { + shot_t *thisShot; + int j; + + thisShot = (shot_t *)arena->spaceShot->list->list[i]; + assert( thisShot != NULL ); + + listDoEmpty(listTeleport); + getObjectFromSpace(spaceTeleport, thisShot->x, thisShot->y, thisShot->w, thisShot->h, listTeleport); + + for( j = 0 ; j < listTeleport->count ; j++ ) + { + tux_t *author; + + thisTeleport = (teleport_t *)listTeleport->list[j]; + author = getObjectFromSpaceWithID(arena->spaceTux, thisShot->author_id); + + if( author != NULL && + author->bonus == BONUS_GHOST && + author->bonus_time > 0 ) + { + continue; + } + + moveShotFromTeleport(thisShot, thisTeleport, spaceTeleport->list); + } + } + + return 0; +} + +int isConflict(int x, int y, int w, int h) +{ + if( spaceTeleport == NULL ) + { + return 0; + } + + return 0; +} + +void cmd(char *line) +{ + if( strncmp(line, "teleport", 8) == 0 )cmd_teleport(line); +} + +int destroy() +{ + destroySpaceWithObject(spaceTeleport, destroyTeleport); + destroyList(listTeleport); + return 0; +} diff --git a/src/modules/modWall.c b/src/modules/modWall.c new file mode 100755 index 0000000..c5e01ce --- /dev/null +++ b/src/modules/modWall.c @@ -0,0 +1,331 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/modules.h" +#include "base/tux.h" +#include "base/shot.h" +#include "base/list.h" +#include "base/gun.h" +#include "base/space.h" + +#ifndef PUBLIC_SERVER +#include "client/interface.h" +#include "client/image.h" +#endif + +#ifdef PUBLIC_SERVER +#include "server/publicServer.h" +#endif + +typedef struct wall_struct +{ + int id; + + int x; // poloha steny + int y; + + int w; // rozmery steny + int h; + + int img_x; // poloha obrazka + int img_y; + + int layer; // vrstva + +#ifndef PUBLIC_SERVER + SDL_Surface *img; //obrazok +#endif +} wall_t; + +static export_fce_t *export_fce; + +static space_t *spaceWall; + +#ifndef PUBLIC_SERVER +static space_t *spaceImgWall; +#endif + +static list_t *listWall; + +#ifndef PUBLIC_SERVER +wall_t* newWall(int x, int y, int w, int h, + int img_x, int img_y, int layer, SDL_Surface *img) +#endif + +#ifdef PUBLIC_SERVER +wall_t* newWall(int x, int y, int w, int h, + int img_x, int img_y, int layer) +#endif +{ + static int last_id = 0; + wall_t *new; + +#ifndef PUBLIC_SERVER + assert( img != NULL ); +#endif + new = malloc( sizeof(wall_t) ); + assert( new != NULL ); + + new->id = ++last_id; + new->x = x; + new->y = y; + new->w = w; + new->h = h; + new->img_x = img_x; + new->img_y = img_y; + new->layer = layer; +#ifndef PUBLIC_SERVER + new->img = img; +#endif + + return new; +} + +#ifndef PUBLIC_SERVER + +void drawWall(wall_t *p) +{ + assert( p != NULL ); + + export_fce->fce_addLayer(p->img, p->img_x, p->img_y, 0, 0, p->img->w, p->img->h, p->layer); +} + +void drawListWall(list_t *list) +{ + wall_t *thisWall; + int i; + + assert( list != NULL ); + + for( i = 0 ; i < list->count ; i++ ) + { + thisWall = (wall_t *)list->list[i]; + assert( thisWall != NULL ); + drawWall(thisWall); + } +} + +#endif + +void eventConflictShotWithWall() +{ + arena_t *arena; + shot_t *thisShot; + tux_t *author; + int i; + + arena = export_fce->fce_getCurrentArena(); + + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) + { + thisShot = (shot_t *)arena->spaceShot->list->list[i]; + assert( thisShot != NULL ); + + if( isConflictWithObjectFromSpace(spaceWall, thisShot->x, thisShot->y, thisShot->w, thisShot->h) ) + { + author = getObjectFromSpaceWithID( + export_fce->fce_getCurrentArena()->spaceTux, thisShot->author_id ); + + if( author != NULL && + author->bonus == BONUS_GHOST && + author->bonus_time > 0 ) + { + continue; + } + + if( thisShot->gun == GUN_BOMBBALL && export_fce->fce_getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + export_fce->fce_boundBombBall(thisShot); + continue; + } + + delObjectFromSpaceWithObject(export_fce->fce_getCurrentArena()->spaceShot, + thisShot, export_fce->fce_destroyShot); + + //delListItem(listShot, i, export_fce->fce_destroyShot); + i--; + } + } +} + +void destroyWall(wall_t *p) +{ + assert( p != NULL ); + free(p); +} + +static void getStatusWall(void *p, int *id, int *x, int *y, int *w, int *h) +{ + wall_t *wall; + + wall = p; + + *id = wall->id; + *x = wall->x; + *y = wall->y; + *w = wall->w; + *h = wall->h; +} + +static void setStatusWall(void *p, int x, int y, int w, int h) +{ + wall_t *wall; + + wall = p; + + wall->x = x; + wall->y = y; + wall->w = w; + wall->h = h; +} + +#ifndef PUBLIC_SERVER + +static void getStatusImgWall(void *p, int *id, int *x, int *y, int *w, int *h) +{ + wall_t *wall; + + wall = p; + + *id = wall->id; + *x = wall->img_x; + *y = wall->img_y; + *w = wall->img->w; + *h = wall->img->h; +} + +static void setStatusImgWall(void *p, int x, int y, int w, int h) +{ +} + +#endif + +static void cmd_wall(char *line) +{ + char str_x[STR_NUM_SIZE]; + char str_y[STR_NUM_SIZE]; + char str_img_x[STR_NUM_SIZE]; + char str_img_y[STR_NUM_SIZE]; + char str_w[STR_NUM_SIZE]; + char str_h[STR_NUM_SIZE]; + char str_layer[STR_NUM_SIZE]; + char str_image[STR_SIZE]; + wall_t *new; + + if( export_fce->fce_getValue(line, "x", str_x, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "y", str_y, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "w", str_w, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "h", str_h, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "img_x", str_img_x, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "img_y", str_img_y, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 )return; + if( export_fce->fce_getValue(line, "image", str_image, STR_SIZE) != 0 )return; + +#ifndef PUBLIC_SERVER + new = newWall(atoi(str_x), atoi(str_y), + atoi(str_w), atoi(str_h), + atoi(str_img_x), atoi(str_img_y), + atoi(str_layer), export_fce->fce_getImage(IMAGE_GROUP_USER, str_image) ); +#endif + +#ifdef PUBLIC_SERVER + new = newWall(atoi(str_x), atoi(str_y), + atoi(str_w), atoi(str_h), + atoi(str_img_x), atoi(str_img_y), + atoi(str_layer) ); +#endif + + if( spaceWall == NULL ) + { + spaceWall = newSpace(export_fce->fce_getCurrentArena()->w, export_fce->fce_getCurrentArena()->h, + 320, 240, getStatusWall, setStatusWall); + +#ifndef PUBLIC_SERVER + spaceImgWall = newSpace(export_fce->fce_getCurrentArena()->w, export_fce->fce_getCurrentArena()->h, + 320, 240, getStatusImgWall, setStatusImgWall); +#endif + } + + addObjectToSpace(spaceWall, new); + +#ifndef PUBLIC_SERVER + addObjectToSpace(spaceImgWall, new); +#endif +} + +int init(export_fce_t *p) +{ + export_fce = p; + listWall = newList(); + + return 0; +} + +#ifndef PUBLIC_SERVER + +int draw(int x, int y, int w, int h) +{ + wall_t *thisWall; + int i; + + if( spaceWall == NULL ) + { + return 0; + } + + listDoEmpty(listWall); + getObjectFromSpace(spaceImgWall, x, y, w, h, listWall); + + for( i = 0 ; i < listWall->count ; i++ ) + { + thisWall = (wall_t *)listWall->list[i]; + assert( thisWall != NULL ); + drawWall(thisWall); + } + + //printSpace(spaceWall); + + return 0; +} +#endif + + +int event() +{ + if( spaceWall == NULL ) + { + return 0; + } + + eventConflictShotWithWall(export_fce->fce_getCurrentArena()->spaceShot->list); + return 0; +} + +int isConflict(int x, int y, int w, int h) +{ + if( spaceWall == NULL ) + { + return 0; + } + + return isConflictWithObjectFromSpace(spaceWall, x, y, w, h); +} + +void cmd(char *line) +{ + if( strncmp(line, "wall", 4) == 0 )cmd_wall(line); +} + +int destroy() +{ + destroySpaceWithObject(spaceWall, destroyWall); +#ifndef PUBLIC_SERVER + destroySpace(spaceImgWall); +#endif + destroyList(listWall); + return 0; +} diff --git a/src/music.c b/src/music.c deleted file mode 100644 index e4fb812..0000000 --- a/src/music.c +++ /dev/null @@ -1,210 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "interface.h" -#include "list.h" -#include "path.h" -#include "string_length.h" -#include "audio.h" -#include "music.h" -#include "storage.h" - -static list_t *listStorage; - -static bool_t isMusicInit = FALSE; -static bool_t var_isMusicActive = TRUE; -static Mix_Music *currentMusic; - -bool_t isMusicInicialized() -{ - return isMusicInit; -} - -void initMusic() -{ - if( isAudioInicialized() == FALSE ) - { - isMusicInit = FALSE; - return; - } - - listStorage = newStorage(); - currentMusic = NULL; - - isMusicInit = TRUE; - var_isMusicActive = TRUE; - - if( isParamFlag("--nomusic") ) - { - setMusicActive(FALSE); - } - - if( isParamFlag("--music") ) - { - setMusicActive(TRUE); - } - - printf("init music..\n"); -} - -static Mix_Music* loadMixMusic(char *file) -{ - Mix_Music *new; - char str[STR_PATH_SIZE]; - - - printf("load music %s..\n", file); - sprintf(str, PATH_MUSIC "%s", file); - - accessExistFile(str); - - new = Mix_LoadMUS(str); - - if( new == NULL ) - { - fprintf(stderr, "Nepodarilo sa nacitat hudbu %s\n", file); - return NULL; - } - - return new; -} - -static void playMixMusic() -{ - if( currentMusic != NULL ) - { - Mix_PlayMusic(currentMusic, 1000); - } -} - -static void destroyMusic(void *p) -{ - Mix_FreeMusic((Mix_Music *)p); -} - -void addMusic(char *file, char *name, char *group) -{ - Mix_Music *new; - - if( isMusicInit == FALSE ) - { - return; - } - - assert( file != NULL ); - assert( name != NULL ); - assert( group != NULL ); - - new = loadMixMusic(file); - - addItemToStorage(listStorage, group, name, new ); -} - -void stopMusic() -{ - if( isMusicInit == FALSE || - var_isMusicActive == FALSE ) - { - return; - } - - if( currentMusic != NULL ) - { - printf("stop music..\n"); - Mix_HaltMusic(); - currentMusic = NULL; - } -} - -void playMusic(char *name, char *group) -{ - static char currentMusic_group[STR_SIZE]; - static char currentMusic_name[STR_SIZE]; - static int isStrInit = 0; - - if( isStrInit == 0 ) - { - strcpy(currentMusic_group, "none"); - strcpy(currentMusic_name, "none"); - isStrInit = 1; - } - - if( isMusicInit == FALSE || - var_isMusicActive == FALSE ) - { - return; - } - - if( currentMusic != NULL && - strcmp(currentMusic_group, group) == 0 && - strcmp(currentMusic_name, name) == 0 ) - { - return; - } - - if( currentMusic != NULL ) - { - stopMusic(); - } - - currentMusic = getItemFromStorage(listStorage, group, name); - strcpy(currentMusic_group, group); - strcpy(currentMusic_name, name); - - if( currentMusic != NULL ) - { - printf("play music %s\n", name); - playMixMusic(); - } -} - -void setMusicActive(bool_t n) -{ - static Mix_Music *music = NULL; - - if( n == FALSE ) - { - music = currentMusic; - stopMusic(); - } - - if( n == TRUE ) - { - currentMusic = music; - playMixMusic(); - } - - var_isMusicActive = n; -} - -bool_t isMusicActive() -{ - return var_isMusicActive; -} - -char* getCurrentMusic() -{ - return "unknown"; -} - -void delAllMusicInGroup(char *group) -{ - delAllItemFromStorage(listStorage, group, destroyMusic); -} - -void quitMusic() -{ - if( isMusicInit == FALSE ) - { - return; - } - - stopMusic(); - destroyStorage(listStorage, destroyMusic); - isMusicInit = FALSE; - - printf("quit music..\n"); -} diff --git a/src/myTimer.c b/src/myTimer.c deleted file mode 100644 index 89696e6..0000000 --- a/src/myTimer.c +++ /dev/null @@ -1,149 +0,0 @@ - -#include -#include -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "myTimer.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#endif - - -static struct timeval start = { .tv_sec = 0, .tv_usec = 0 }; - -list_t* newTimer() -{ - return newList(); -} - -void restartTimer() -{ - gettimeofday(&start, NULL); -} - - -my_time_t getMyTime() -{ - struct timeval now; - my_time_t ticks; - - if( start.tv_sec == 0 && start.tv_usec == 0 ) - { - restartTimer(); - } - - gettimeofday(&now, NULL); - - ticks = (now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000; - - //printf("-> %d\n", ticks); - return ticks; -} - -my_timer_t* newTimerItem(int type, void (*fce)(void *p), void *arg, my_time_t my_time) -{ - static int new_id = 0; - my_timer_t *new; - - assert( fce != NULL ); - - new = malloc( sizeof(my_timer_t) ); - memset(new, 0, sizeof(my_timer_t)); - - new->id = new_id++; - new->type = type; - new->fce = fce; - new->arg = arg; - - new->createTime = getMyTime(); - new->time = my_time; - - return new; -} - -static void destroyTimerItem(my_timer_t *p) -{ - assert( p != NULL ); - free(p); -} - -int addTaskToTimer(list_t *listTimer, int type, void (*fce)(void *p), void *arg, my_time_t my_time) -{ - my_timer_t *new; - - new = newTimerItem(type, fce, arg, my_time); - addList(listTimer, new); - - return new->id; -} - -void eventTimer(list_t *listTimer) -{ - int i; - my_timer_t *thisTimer; - my_time_t currentTime; - - currentTime = getMyTime(); - - for( i = 0 ; i < listTimer->count ; i++ ) - { - thisTimer = (my_timer_t *)listTimer->list[i]; - assert( thisTimer != NULL ); - - switch( thisTimer->type ) - { - case TIMER_ONE: - if( currentTime >= thisTimer->createTime + thisTimer->time ) - { - thisTimer->fce(thisTimer->arg); - - delListItem(listTimer, i, free); - i--; - } - break; - case TIMER_PERIODIC: - if( currentTime >= thisTimer->createTime + thisTimer->time ) - { - thisTimer->fce(thisTimer->arg); - thisTimer->createTime = getMyTime(); - } - break; - default : - assert( ! "bad timer type !" ); - break; - } - - } -} - -void delTimer(list_t *listTimer, int id) -{ - int i; - my_timer_t *thisTimer; - - for( i = 0 ; i < listTimer->count ; i++ ) - { - thisTimer = (my_timer_t *)listTimer->list[i]; - assert( thisTimer != NULL ); - - if( thisTimer->id == id ) - { - delListItem(listTimer, i, free); - return; - } - } - - assert( ! "Uloha s id nenajdena !" ); -} - - -void destroyTimer(list_t *listTimer) -{ - destroyListItem(listTimer, destroyTimerItem); -} diff --git a/src/net_multiplayer.c b/src/net_multiplayer.c deleted file mode 100644 index a9132e3..0000000 --- a/src/net_multiplayer.c +++ /dev/null @@ -1,168 +0,0 @@ -#include -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "item.h" -#include "buffer.h" -#include "string_length.h" -#include "arenaFile.h" -#include "net_multiplayer.h" -#include "server.h" -#include "udp.h" - -#ifndef PUBLIC_SERVER -#include "sdl_udp.h" -#include "tcp.h" -#include "screen.h" -#include "screen_setting.h" -#include "screen_choiceArena.h" -#include "client.h" -#endif - -static int netGameType; - -int getNetTypeGame() -{ - return netGameType; -} - -#ifdef PUBLIC_SERVER - -int initNetMulitplayerPublicServer(char *ip4, int port4, char *ip6, int port6) -{ - netGameType = NET_GAME_TYPE_SERVER; - return initUdpPublicServer(ip4, port4, ip6, port6); -} -#endif - -int initNetMuliplayer(int type, char *ip, int port, int proto) -{ - netGameType = type; - - switch( netGameType ) - { - case NET_GAME_TYPE_NONE : - break; - - case NET_GAME_TYPE_SERVER : -#ifdef SUPPORT_NET_UNIX_UDP - if( initUdpServer(ip, port, proto) != 0 ) - { - fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n"); - netGameType = NET_GAME_TYPE_NONE; - return -1; - } -#endif -#ifdef SUPPORT_NET_SDL_UDP - if( initSdlUdpServer(port) != 0 ) - { - fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n"); - netGameType = NET_GAME_TYPE_NONE; - return -1; - } -#endif - break; - -#ifndef PUBLIC_SERVER - case NET_GAME_TYPE_CLIENT : -#ifdef SUPPORT_NET_UNIX_UDP - if( initUdpClient(ip, port, proto) != 0 ) - { - fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n"); - netGameType = NET_GAME_TYPE_NONE; - return -1; - } -#endif -#ifdef SUPPORT_NET_SDL_UDP - if( initSdlUdpClient(ip, port) != 0 ) - { - fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n"); - netGameType = NET_GAME_TYPE_NONE; - return -1; - } -#endif - break; -#endif - default : - assert( ! "Premenna netGameType ma zlu hodnotu !" ); - break; - } - - return 0; -} - -void eventNetMultiplayer() -{ - switch( netGameType ) - { - case NET_GAME_TYPE_NONE : - break; - - case NET_GAME_TYPE_SERVER : -#ifdef SUPPORT_NET_UNIX_UDP - eventServer(); -#endif -#ifdef SUPPORT_NET_SDL_UDP - eventServer(); -#endif - break; - -#ifndef PUBLIC_SERVER - case NET_GAME_TYPE_CLIENT : -#ifdef SUPPORT_NET_UNIX_UDP - eventPingServer(); - selectClientUdpSocket(); - eventServerBuffer(); -#endif -#ifdef SUPPORT_NET_SDL_UDP - eventPingServer(); - selectClientSdlUdpSocket(); - eventServerBuffer(); -#endif -#endif - break; - default : - assert( ! "Premenna netGameType ma zlu hodnotu !" ); - break; - } -} - -void quitNetMultiplayer() -{ - switch( netGameType ) - { - case NET_GAME_TYPE_NONE : - break; - - case NET_GAME_TYPE_SERVER : -#ifdef SUPPORT_NET_UNIX_UDP - quitUdpServer(); -#endif -#ifdef SUPPORT_NET_SDL_UDP - quitSdlUdpServer(); -#endif - break; - -#ifndef PUBLIC_SERVER - case NET_GAME_TYPE_CLIENT : -#ifdef SUPPORT_NET_UNIX_UDP - quitUdpClient(); -#endif -#ifdef SUPPORT_NET_SDL_UDP - quitSdlUdpClient(); -#endif - break; -#endif - - default : - assert( ! "Premenna netGameType ma zlu hodnotu !" ); - break; - } - - netGameType = NET_GAME_TYPE_NONE; -} diff --git a/src/net_sdl/Makefile b/src/net_sdl/Makefile new file mode 100644 index 0000000..f0ffb5b --- /dev/null +++ b/src/net_sdl/Makefile @@ -0,0 +1,9 @@ + + +all: sdl_udp.o + +sdl_udp.o: sdl_udp.c sdl_udp.h + $(CC) $(CFLAGS) -o sdl_udp.o -c sdl_udp.c + +clean: + rm -rf *.o diff --git a/src/net_sdl/sdl_udp.c b/src/net_sdl/sdl_udp.c new file mode 100644 index 0000000..f30ba75 --- /dev/null +++ b/src/net_sdl/sdl_udp.c @@ -0,0 +1,219 @@ + + +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "net_sdl/sdl_udp.h" + +#define BUFSIZE 1000 + + +sock_sdl_udp_t* newSdlSockUdp() +{ + sock_sdl_udp_t *new; + + new = malloc( sizeof(sock_sdl_udp_t) ); + memset(new, 0, sizeof(sock_sdl_udp_t)); + + if( !( new->packet = SDLNet_AllocPacket(512) ) ) + { + fprintf(stderr, "nemozem alokovat pamat pre packet !\n"); + destroySockSdlUdp(new); + } + + return new; +} + +void destroySockSdlUdp(sock_sdl_udp_t *p) +{ + assert( p != NULL ); + + SDLNet_FreePacket(p->packet); + free(p); +} + +sock_sdl_udp_t* bindSdlUdpSocket(int port) +{ + sock_sdl_udp_t *new; + + assert( port > 0 && port < 65535 ); + + new = newSdlSockUdp(); + + assert( new != NULL ); + + if( SDLNet_Init() < 0 ) + { + fprintf(stderr, "nemozem initcializovat SDLnet !\n"); + destroySockSdlUdp(new); + return NULL; + } + + if( !( new->sock = SDLNet_UDP_Open(port) ) ) + { + fprintf(stderr, "nemozem otovrit socket !\n"); + destroySockSdlUdp(new); + return NULL; + } + + new->type = SDL_UDP_SERVER; + + return new; +} + +sock_sdl_udp_t* connectSdlUdpSocket(char *address, int port) +{ + sock_sdl_udp_t *new; + + assert( address != NULL ); + assert( port > 0 && port < 65536 ); + + new = newSdlSockUdp(); + + assert( new != NULL ); + + /* Initialize SDL_net */ + if( SDLNet_Init() < 0 ) + { + fprintf(stderr, "nemozem initcializovat SDLnet !\n"); + destroySockSdlUdp(new); + return NULL; + } + + /* Open a socket on random port */ + if( !( new->sock = SDLNet_UDP_Open(0) ) ) + { + fprintf(stderr, "nemozem otovrit socket !\n"); + destroySockSdlUdp(new); + return NULL; + } + + /* Resolve server name */ + if( SDLNet_ResolveHost(&(new->srvadd), address, port) ) + { + fprintf(stderr, "nemozem sa pripojit na sokcet !\n"); + destroySockSdlUdp(new); + return NULL; + } + + new->type = SDL_UDP_CLIENT; + + return new; +} + +int readSdlUdpSocket(sock_sdl_udp_t *src, sock_sdl_udp_t *dst, void *address, int len) +{ + int x; + + if( SDLNet_UDP_Recv(src->sock, dst->packet) ) + { + x = (dst->packet->len > len ? len : dst->packet->len); + memcpy(address, dst->packet->data, x); + return x; + } + + return -1; +} + +int writeSdlUdpSocket(sock_sdl_udp_t *src, sock_sdl_udp_t *dst, void *address, int len) +{ + assert( src != NULL ); + assert( address != NULL ); + +#ifdef DEBUG_LOST_PACKET + if( rand() % DEBUG_LOST_PACKET == 0 ) + { + printf("debug lost packet %s\n", address); + return size; + } +#endif + + memcpy(dst->packet->data, address, len); + dst->packet->len = len; + + if( src->type == SDL_UDP_CLIENT ) + { + dst->packet->address.host = src->srvadd.host; + dst->packet->address.port = src->srvadd.port; + } + + SDLNet_UDP_Send(src->sock, -1, dst->packet); + + return len; +} + +int getSockSdlUdpPort(sock_sdl_udp_t *p) +{ + assert( p != NULL ); + + return p->packet->address.port; +} + + +void closeSdlUdpSocket(sock_sdl_udp_t *p) +{ + assert( p != NULL ); + + SDLNet_UDP_Close(p->sock); + + printf("close connect\n"); + + destroySockSdlUdp(p); +} + +#if 0 +int main(int argc, char *argv[]) +{ + sock_sdl_udp_t *sock; + char buf[BUFSIZE]; + int ret; + + if( strcmp(argv[1], "s") == 0 ) + { + sock_sdl_udp_t *cli; + + sock = bindSdlUdpSocket( atoi(argv[2]) ); + cli = newSdlSockUdp(); + + while(1) + { + memset(buf, 0, BUFSIZE); + + ret = readSdlUdpSocket(sock, cli, buf, BUFSIZE); + + if( ret > 0 ) + { + writeSdlUdpSocket(sock, cli, buf, ret); + } + } + } + + if( strcmp(argv[1], "c") == 0 ) + { + sock = connectSdlUdpSocket("127.0.0.1", atoi(argv[2])); + + strcpy(buf, "Hello world !\n"); + writeSdlUdpSocket(sock, sock, buf, strlen(buf)); + + while(1) + { + memset(buf, 0, BUFSIZE); + ret = readSdlUdpSocket(sock, sock, buf, BUFSIZE); + + if( ret > 0 ) + { + printf("buf = %s", buf); + } + } + + closeSdlUdpSocket(sock); + } + + return 0; +} + +#endif diff --git a/src/net_sdl/sdl_udp.h b/src/net_sdl/sdl_udp.h new file mode 100644 index 0000000..91a3c71 --- /dev/null +++ b/src/net_sdl/sdl_udp.h @@ -0,0 +1,28 @@ + +#ifndef MY_SDL_UDP + +#include + +#define MY_SDL_UDP + +#define SDL_UDP_SERVER 0 +#define SDL_UDP_CLIENT 1 + +typedef struct struct_sock_sdl_udp_t +{ + int type; + UDPsocket sock; + IPaddress srvadd; + UDPpacket *packet; +} sock_sdl_udp_t; + +sock_sdl_udp_t* newSdlSockUdp(); +void destroySockSdlUdp(sock_sdl_udp_t *p); +sock_sdl_udp_t* bindSdlUdpSocket(int port); +sock_sdl_udp_t* connectSdlUdpSocket(char *address, int port); +int readSdlUdpSocket(sock_sdl_udp_t *src, sock_sdl_udp_t *dst, void *address, int len); +int writeSdlUdpSocket(sock_sdl_udp_t *src, sock_sdl_udp_t *dst, void *address, int len); +int getSockSdlUdpPort(sock_sdl_udp_t *p); +void closeSdlUdpSocket(sock_sdl_udp_t *p); + +#endif diff --git a/src/net_unix/Makefile b/src/net_unix/Makefile new file mode 100644 index 0000000..648ca88 --- /dev/null +++ b/src/net_unix/Makefile @@ -0,0 +1,9 @@ + + +all: udp.o + +udp.o: udp.c udp.h + $(CC) $(CFLAGS) -o udp.o -c udp.c + +clean: + rm -rf *.o diff --git a/src/net_unix/udp.c b/src/net_unix/udp.c new file mode 100644 index 0000000..a701d48 --- /dev/null +++ b/src/net_unix/udp.c @@ -0,0 +1,358 @@ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFSIZE 1000 + +#include "base/main.h" +#include "base/net_multiplayer.h" + +#include "net_unix/udp.h" + +sock_udp_t* newSockUdp(int proto) +{ + sock_udp_t *new; + + new = malloc( sizeof(sock_udp_t) ); + memset(new, 0, sizeof(sock_udp_t)); + new->proto = proto; + + return new; +} + +void destroySockUdp(sock_udp_t *p) +{ + assert( p != NULL ); + free(p); +} + +sock_udp_t* bindUdpSocket(char *address, int port, int proto) +{ + sock_udp_t *new; + int res; + + assert( port > 0 && port < 65535 ); + + new = newSockUdp(proto); + + assert( new != NULL ); + + if( new->proto == PROTO_UDPv4 ) + { + new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_UDPv6 ) + { + new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); + } +#endif + + if( new->sock < 0 ) + { + fprintf(stderr, "nemozem vytvorit socket !\n"); + destroySockUdp(new); + return NULL; + } + + memset(&(new->sockAddr), 0, sizeof(new->sockAddr)); + + if( new->proto == PROTO_UDPv4 ) + { + new->sockAddr.sin_family = AF_INET; + new->sockAddr.sin_port = htons(port); + new->sockAddr.sin_addr.s_addr = inet_addr(address); + + res = bind(new->sock, (struct sockaddr *)&(new->sockAddr), sizeof(new->sockAddr)); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_UDPv6 ) + { + new->sockAddr6.sin6_family = AF_INET6; + new->sockAddr6.sin6_port = htons(port); + inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr)); + + res = bind(new->sock, (struct sockaddr *)&(new->sockAddr6), sizeof(new->sockAddr6)); + } +#endif + + if( res < 0 ) + { + fprintf(stderr, "nemozem nastavit sokcet %s %d !\n", address, port); + destroySockUdp(new); + return NULL; + } + + return new; +} + +sock_udp_t* connectUdpSocket(char *address, int port, int proto) +{ + sock_udp_t *new; + + assert( address != NULL ); + assert( port > 0 && port < 65536 ); + + new = newSockUdp(proto); + + assert( new != NULL ); + + if( new->proto == PROTO_UDPv4 ) + { + new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_UDPv6 ) + { + new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); + } +#endif + + if( new->sock < 0 ) + { + fprintf(stderr, "nemozem vytvorit socket !\n"); + destroySockUdp(new); + return NULL; + } + + memset(&(new->sockAddr), 0, sizeof(new->sockAddr)); + + if( new->proto == PROTO_UDPv4 ) + { + new->sockAddr.sin_family = AF_INET; + new->sockAddr.sin_port = htons(port); + new->sockAddr.sin_addr.s_addr = inet_addr(address); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_UDPv6 ) + { + new->sockAddr6.sin6_family = AF_INET6; + new->sockAddr6.sin6_port = htons(port); + inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr)); + } +#endif + return new; +} + +int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) +{ + int addrlen; + int size; + + assert( src != NULL ); + assert( dst != NULL ); + assert( address != NULL ); + + if( dst->proto == PROTO_UDPv4 ) + { + addrlen = sizeof(src->sockAddr); + + size = recvfrom(src->sock, address, len, 0, + (struct sockaddr *)&dst->sockAddr, (socklen_t *)&addrlen); + } + +#ifdef SUPPORT_IPv6 + if( dst->proto == PROTO_UDPv6 ) + { + addrlen = sizeof(src->sockAddr6); + + size = recvfrom(src->sock, address, len, 0, + (struct sockaddr *)&dst->sockAddr6, (socklen_t *)&addrlen); + } +#endif + + if( size < 0 ) + { + char str_ip[STR_IP_SIZE]; + + getSockUdpIp(dst, str_ip, STR_IP_SIZE); + + fprintf(stderr, "nemozem zapisat zo socketu %d %s %d!\n", size, str_ip, getSockUdpPort(dst)); + return -1; + } + + return size; +} + +int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) +{ + int addrlen; + int size; + + assert( src != NULL ); + assert( dst != NULL ); + assert( address != NULL ); + + if( dst->proto == PROTO_UDPv4 ) + { + addrlen = sizeof(src->sockAddr); + + size = sendto(src->sock, address, len, 0, + (struct sockaddr *)&dst->sockAddr, (socklen_t)addrlen); + } + +#ifdef SUPPORT_IPv6 + if( dst->proto == PROTO_UDPv6 ) + { + addrlen = sizeof(src->sockAddr6); + + size = sendto(src->sock, address, len, 0, + (struct sockaddr *)&dst->sockAddr6, (socklen_t)addrlen); + } +#endif + + if( size < 0 ) + { + char str_ip[STR_IP_SIZE]; + + getSockUdpIp(dst, str_ip, STR_IP_SIZE); + + fprintf(stderr, "nemozem zapisat na socket %d %s %d!\n", size, str_ip, getSockUdpPort(dst)); + return -1; + } + + return size; +} + +void getSockUdpIp(sock_udp_t *p, char *str_ip, int len) +{ + assert( p != NULL ); + assert( str_ip != NULL ); + + //strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr)); + + if( p->proto == PROTO_UDPv4 ) + { + inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len); + } + +#ifdef SUPPORT_IPv6 + if( p->proto == PROTO_UDPv6 ) + { + inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len); + } +#endif +} + +int getSockUdpPort(sock_udp_t *p) +{ + assert( p != NULL ); + + if( p->proto == PROTO_UDPv4 ) + { + return htons(p->sockAddr.sin_port); + } + +#ifdef SUPPORT_IPv6 + if( p->proto == PROTO_UDPv6 ) + { + return htons(p->sockAddr6.sin6_port); + } +#endif + + assert( ! "bad proto !" ); + + return -1; +} + + +void closeUdpSocket(sock_udp_t *p) +{ + assert( p != NULL ); + + close(p->sock); + destroySockUdp(p); +} + +#if 0 + +static int myWait(sock_udp_t *p) +{ + fd_set readfds; + fd_set errorfds; + struct timeval tv; + int max_fd; + + tv.tv_sec = 1; + tv.tv_usec = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + max_fd = p->sock; + + FD_SET(p->sock, &readfds); + FD_SET(p->sock, &errorfds); + + select(max_fd+1, &readfds, (fd_set *)0, &errorfds, &tv); + + if( FD_ISSET(p->sock, &readfds) ) + { + FD_CLR(p->sock, &readfds); + return 1; + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + sock_udp_t *sock; + char buf[BUFSIZE]; + int ret; + + if( strcmp(argv[1], "s") == 0 ) + { + sock_udp_t *cli; + + //sock = bindUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6 ); + sock = bindUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4 ); + + //cli = newSockUdp(PROTO_UDPv6); + cli = newSockUdp(PROTO_UDPv4); + + while(1) + { + while( myWait(sock) == 0 ) + ; + + memset(buf, 0, BUFSIZE); + ret = readUdpSocket(sock, cli, buf, BUFSIZE); + writeUdpSocket(sock, cli, buf, ret); + } + } + + if( strcmp(argv[1], "c") == 0 ) + { + //sock = connectUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6); + sock = connectUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4); + + strcpy(buf, "Hello world !\n"); + writeUdpSocket(sock, sock, buf, strlen(buf)); + + memset(buf, 0, BUFSIZE); + readUdpSocket(sock, sock, buf, BUFSIZE); + + printf("buf = %s", buf); + //while(1)sleep(1); + closeUdpSocket(sock); + } + + return 0; +} +#endif + diff --git a/src/net_unix/udp.h b/src/net_unix/udp.h new file mode 100644 index 0000000..e5fd648 --- /dev/null +++ b/src/net_unix/udp.h @@ -0,0 +1,31 @@ + +#ifndef MY_UDP + +#include + +#define MY_UDP + +#define SUPPORT_IPv6 + +typedef struct struct_sock_udp_t +{ + int sock; + int proto; + +#ifdef SUPPORT_IPv6 + struct sockaddr_in6 sockAddr6; +#endif + struct sockaddr_in sockAddr; +} sock_udp_t; + +extern sock_udp_t* newSockUdp(int proto); +extern void destroySockUdp(sock_udp_t *p); +extern sock_udp_t* bindUdpSocket(char *address, int port, int proto); +extern sock_udp_t* connectUdpSocket(char *address, int port, int proto); +extern int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len); +extern int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len); +extern void getSockUdpIp(sock_udp_t *p, char *str_ip, int len); +extern int getSockUdpPort(sock_udp_t *p); +extern void closeUdpSocket(sock_udp_t *p); + +#endif diff --git a/src/network.c b/src/network.c deleted file mode 100755 index dee3c73..0000000 --- a/src/network.c +++ /dev/null @@ -1,101 +0,0 @@ - -#include "network.h" -#include "tcp.h" -#include "udp.h" - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -int newSocket(int port,const char *ip) -{ - struct sockaddr_in address; - int sockfd; - int len; - - assert( port > 0 && port < 65535 ); - - sockfd = socket(AF_INET, SOCK_STREAM, 0); - - if( sockfd < 0 ) - { - fprintf(stderr, "Nemozem vytvorit sietovy socket\n"); - return -1; - } - - address.sin_family = AF_INET; - - if( ip != NULL ) - address.sin_addr.s_addr = inet_addr(ip); - else - address.sin_addr.s_addr = htonl(INADDR_ANY); - - address.sin_port = htons(port); - - len = sizeof(address); - - if( ip == NULL ) - { - if ( bind(sockfd, (struct sockaddr *)&address, len) < 0 ) - { - fprintf(stderr, "Nemozem obsadit sietovy port %d\n", port); - return -1; - } - - listen(sockfd, 5); - } - else - { - if ( connect(sockfd, (struct sockaddr *)&address, len) < 0 ) - { - fprintf(stderr, "Nemozem sa pripojit na %s %d\n", ip, port); - return -1; - } - } - - return sockfd; -} - -int getNewClient(int sockfd) -{ - struct sockaddr_in client_address; - int client_sockfd; - int client_len; - - assert( sockfd >= 0 ); - - client_len = sizeof(client_address); - - client_sockfd = accept(sockfd, (struct sockaddr *)&client_address, - (socklen_t *)&client_len); - - //printf("ip = %s\n",inet_ntoa(client_address.sin_addr)); - - return client_sockfd; -} - -int getNewClientIP(int sockfd,char **str_ip) -{ - struct sockaddr_in client_address; - int client_sockfd; - int client_len; - - assert( sockfd >= 0 ); - - client_len = sizeof(client_address); - - client_sockfd = accept(sockfd, (struct sockaddr *)&client_address, - (socklen_t *)&client_len); - - *str_ip = strdup( inet_ntoa(client_address.sin_addr) ); - - return client_sockfd; -} diff --git a/src/panel.c b/src/panel.c deleted file mode 100644 index 079d753..0000000 --- a/src/panel.c +++ /dev/null @@ -1,190 +0,0 @@ - -#include -#include - -#include "main.h" -#include "interface.h" -#include "string_length.h" -#include "image.h" -#include "list.h" -#include "font.h" -#include "tux.h" -#include "panel.h" - -static SDL_Surface *g_panel; -static SDL_Surface *g_shot; -static SDL_Surface *g_icon[ITEM_COUNT]; -static SDL_Surface *g_bonus; - -static bool_t isPanelInit = FALSE; - -bool_t isPanelInicialized() -{ - return isPanelInit; -} - -void initPanel() -{ - assert( isImageInicialized() == TRUE ); - assert( isInterfaceInicialized() == TRUE ); - - g_panel = addImageData("panel.png", IMAGE_ALPHA, "panel", IMAGE_GROUP_BASE); - g_shot = addImageData("panel_shot.png", IMAGE_ALPHA, "panel_image_shot", IMAGE_GROUP_BASE); - g_bonus = addImageData("graf.bonus.png", IMAGE_ALPHA, "panel_graf_bonus", IMAGE_GROUP_BASE); - - g_icon[GUN_SIMPLE] = addImageData("icon.gun.simple.png", IMAGE_ALPHA, "panel_gun", IMAGE_GROUP_BASE); - g_icon[GUN_DUAL_SIMPLE] = addImageData("icon.gun.dual.simple.png", IMAGE_ALPHA, "panel_dual", IMAGE_GROUP_BASE); - g_icon[GUN_TOMMY] = addImageData("icon.gun.tommy.png", IMAGE_ALPHA, "panel_tommy", IMAGE_GROUP_BASE); - g_icon[GUN_SCATTER] = addImageData("icon.gun.scatter.png", IMAGE_ALPHA, "panel_scatter", IMAGE_GROUP_BASE); - g_icon[GUN_LASSER] = addImageData("icon.gun.lasser.png", IMAGE_ALPHA, "panel_lasser", IMAGE_GROUP_BASE); - g_icon[GUN_MINE] = addImageData("icon.gun.mine.png", IMAGE_ALPHA, "panel_mine", IMAGE_GROUP_BASE); - g_icon[GUN_BOMBBALL] = addImageData("icon.gun.bombball.png", IMAGE_ALPHA, "panel_bombball", IMAGE_GROUP_BASE); - - g_icon[BONUS_SPEED] = addImageData("icon.bonus.speed.png", IMAGE_ALPHA, "panel_speed", IMAGE_GROUP_BASE); - g_icon[BONUS_SHOT] = addImageData("icon.bonus.shot.png", IMAGE_ALPHA, "panel_shot", IMAGE_GROUP_BASE); - g_icon[BONUS_TELEPORT] = addImageData("icon.bonus.teleport.png", IMAGE_ALPHA, "panel_teleport", IMAGE_GROUP_BASE); - g_icon[BONUS_GHOST] = addImageData("icon.bonus.ghost.png", IMAGE_ALPHA, "panel_ghost", IMAGE_GROUP_BASE); - g_icon[BONUS_4X] = addImageData("icon.bonus.4x.png", IMAGE_ALPHA, "panel_4x", IMAGE_GROUP_BASE); - g_icon[BONUS_HIDDEN] = addImageData("icon.bonus.hidden.png", IMAGE_ALPHA, "panel_hidden", IMAGE_GROUP_BASE); - - isPanelInit = TRUE; -} - -static void setScoreline(list_t *listTux, char *str) -{ - tux_t *thisTux; - int i; - - assert( listTux != NULL ); - assert( str != NULL ); - - strcpy(str, ""); - - for( i = 0 ; i < listTux->count ; i++ ) - { - char num[STR_SIZE]; - - thisTux = (tux_t *)listTux->list[i]; - assert( thisTux != NULL ); - - sprintf(num, "%s%d", ( i == 0 ? "" : " : " ), thisTux->score); - strcat(str, num); - } -} - -static void drawScore(list_t *listTux) -{ - char strScoreLine[STR_SIZE]; - int w, h; - - assert( listTux != NULL ); - - setScoreline(listTux, strScoreLine); - getTextSize(strScoreLine, &w , &h); - drawFont(strScoreLine, PANEL_SCORE_LOCATION_X, PANEL_SCORE_LOCATION_Y, COLOR_WHITE); -} - -static void drawShot(int n, int x, int y) -{ - drawImage(g_shot, x, y,n * PANEL_SHOT_WIDTH, 0, PANEL_SHOT_WIDTH, PANEL_SHOT_HEIGHT); -} - -static void drawShotInfo(tux_t *tux,int x, int y) -{ - int i; - - if( tux->pickup_time > 0 ) - { - for( i = 0 ; i < tux->pickup_time / 3 ; i++ ) - { - drawShot(PANEL_SHOT_LINE, x + i * PANEL_SHOT_WIDTH, y); - } - - return; - } - - for( i = 0 ; i < GUN_MAX_SHOT ; i++ ) - { - if( tux->shot[tux->gun] > i ) - { - drawShot(PANEL_SHOT_FILL, x + i * ( PANEL_SHOT_WIDTH + 2 ), y); - } - else - { - drawShot(PANEL_SHOT_EMPTY, x + i * ( PANEL_SHOT_WIDTH + 2 ) , y); - } - } -} - -static void drawGunInfo(tux_t *tux,int x, int y) -{ - drawImage(g_icon[ tux->gun ], x, y, 0, 0, - g_icon[ tux->gun ]->w, g_icon[ tux->gun ]->h); -} - -static void drawBonusInfo(tux_t *tux,int x, int y) -{ - drawImage(g_icon[ tux->bonus ], x, y, 0, 0, g_icon[ tux->bonus ]->w, g_icon[ tux->bonus ]->h); -} - -static void drawGrafBonus(tux_t *tux,int x, int y) -{ - int offset; - - offset = tux->bonus_time / 5; - drawImage(g_bonus, x, y, 0, 0, g_bonus->w, g_bonus->h/2); - drawImage(g_bonus, x+1, y, 0, g_bonus->h/2, offset, g_bonus->h/2); -} - -void drawPanel(list_t *listTux) -{ - int i; - - assert( listTux != NULL ); - - drawImage(g_panel, PANEL_LOCATION_X, PANEL_LOCATION_Y, 0, 0, g_panel->w, g_panel->h); - drawScore(listTux); - - for( i = 0 ; i < listTux->count ; i++ ) - { - tux_t *thisTux; - int x, y; - - thisTux = (tux_t *)listTux->list[i]; - assert( thisTux != NULL ); - - x = PANEL_LOCATION_X; - y = PANEL_LOCATION_Y; - - switch( i ) - { - case 0 : - drawShotInfo(thisTux, x+8, y+34); - drawGunInfo(thisTux, x+130, y+30); - - if( thisTux->bonus != BONUS_NONE ) - { - drawGrafBonus(thisTux,x+200, y+50); - drawBonusInfo(thisTux,x+200, y+30); - } - break; - - case 1 : - drawShotInfo(thisTux, x+740, y+34); - drawGunInfo(thisTux, x+620, y+30); - - if( thisTux->bonus != BONUS_NONE ) - { - drawGrafBonus(thisTux,x+460, y+50); - drawBonusInfo(thisTux,x+460, y+30); - } - break; - } - } -} - -void quitPanel() -{ - isPanelInit = FALSE; -} - diff --git a/src/protect.c b/src/protect.c deleted file mode 100644 index fe9d29d..0000000 --- a/src/protect.c +++ /dev/null @@ -1,88 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "myTimer.h" -#include "protect.h" - -protect_t* newProtect() -{ - protect_t *new; - - new = malloc( sizeof(protect_t) ); - new->lastMove = getMyTime(); - new->lastPing = getMyTime(); - new->avarage = 0; - new->count = 0; - new->isDown = FALSE; - return new; -} - -void refreshLastMove(protect_t *p) -{ - my_time_t currentTime; - my_time_t interval; - - currentTime = getMyTime(); - - interval = currentTime - p->lastMove; - p->lastMove = getMyTime(); - - p->avarage += interval; - p->count++; - - if( p->count == PROTECT_SPEED_AVARAGE ) - { - int index; - - index = p->avarage/PROTECT_SPEED_AVARAGE; - p->avarage = 0; - p->count = 0; - - if( index < PROTECT_SPEED_INTERVAL_TIMEOUT ) - { - p->isDown = TRUE; - } - - //printf("speed index = %d\n", index); - } -} - -void rereshLastPing(protect_t *p) -{ - //my_time_t currentTime; - //my_time_t interval; - - //currentTime = getMyTime(); - - //interval = currentTime - p->lastPing; - p->lastPing = getMyTime(); - - //printf("interval = %d\n", interval); -} - -bool_t isDown(protect_t *p) -{ - my_time_t currentTime; - my_time_t interval; - - currentTime = getMyTime(); - - interval = currentTime - p->lastPing; - - if( interval > PROTECT_PING_INTERVAL_TIMEOUT ) - { - p->isDown = TRUE; - } - - return p->isDown; -} - -void destroyProtect(protect_t *p) -{ - assert( p != NULL ); - free(p); -} diff --git a/src/proto.c b/src/proto.c deleted file mode 100644 index 4f71531..0000000 --- a/src/proto.c +++ /dev/null @@ -1,714 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "item.h" -#include "shot.h" -#include "arenaFile.h" -#include "myTimer.h" -#include "server.h" -#include "proto.h" -#include "net_multiplayer.h" -#include "checkFront.h" -#include "idManager.h" -#include "protect.h" - -#ifndef PUBLIC_SERVER -#include "language.h" -#include "network.h" -#include "screen_world.h" -#include "screen_setting.h" -#include "screen_choiceArena.h" -#include "screen_analyze.h" -#include "client.h" -#include "term.h" -#endif - -#ifdef PUBLIC_SERVER -#include "heightScore.h" -#include "publicServer.h" -#endif - -void proto_send_error_server(int type, client_t *client, int errorcode) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "error %d\n", errorcode); - - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_error_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int errorcode; - - assert( msg != NULL ); - - sscanf(msg, "%s %d", - cmd, &errorcode); - - printf("proto error code %d\n", errorcode); - - switch(errorcode) - { - case PROTO_ERROR_CODE_BAD_VERSION : - setMsgToAnalyze(getMyText("ERROR_BAD_VERSION")); - setWorldEnd(); - break; - } -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_hello_client(char *name) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "hello %s %s\n", TUXANCI_NG_VERSION, name); - sendServer(msg); -} - -#endif - -void proto_recv_hello_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - char version[STR_NAME_SIZE]; - char name[STR_NAME_SIZE]; - - assert( client != NULL ); - - strcpy(version, ""); - strcpy(name, ""); - - sscanf(msg, "%s %s %s", cmd, version, name); - - if( strncmp(version, TUXANCI_NG_VERSION, strlen(TUXANCI_NG_VERSION)) != 0 ) - { - proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_VERSION); - client->status = NET_STATUS_ZOMBIE; - return; - } - - client->tux = newTux(); - client->tux->control = TUX_CONTROL_NET; - client->tux->client = client; - - addObjectToSpace(getCurrentArena()->spaceTux, client->tux); - - if( strlen(name) > STR_NAME_SIZE-1 ) - { - name[STR_NAME_SIZE-1] = '\0'; - } - - strcpy(client->tux->name, name); - sendInfoCreateClient(client); -} - -void proto_send_status_server(int type, client_t *client) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "version: %s\n" - "clients: %d\n" - "maxclients: %d\n" - "uptime: %d\n" - "arena: %s\n", - TUXANCI_NG_VERSION, getCurrentArena()->spaceTux->list->count, - getServerMaxClients(), (unsigned int)getUpdateServer(), - getArenaNetName( getChoiceArenaId() ) ); - - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); - //proto_send(type, client, msg); -} - -void proto_recv_status_server(client_t *client, char *msg) -{ - proto_send_status_server(PROTO_SEND_ONE, client); -} - -#ifdef PUBLIC_SERVER - -void proto_send_listscore_server(int type, client_t *client, int max) -{ - char *str; - char *msg; - int i; - - msg = malloc( (max * 24) * sizeof(char) ); - strcpy(msg, ""); - - for( i = 0 ; i < max ; i++ ) - { - str = getTableItem(i); - - if( str == NULL ) - { - break; - } - - strcat(msg, str); - strcat(msg, "\n"); - } - - //proto_send(type, client, msg); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); - free(msg); -} - -void proto_recv_listscore_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int max; - int ret; - - assert( msg != NULL ); - - ret = sscanf(msg, "%s %d", - cmd, &max); - - if( ret != 2 ) - { - max = HEIGHTSCORE_MAX_PLAYERS; - } - - proto_send_listscore_server(PROTO_SEND_ONE, client, max); -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_check_client(int id) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "check %d\n", id); - sendServer(msg); -} - -#endif - -void proto_recv_check_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id; - - assert( msg != NULL ); - - sscanf(msg, "%s %d", - cmd, &id); - - delMsgInCheckFront(client->listSendMsg, id); -} - -void proto_send_init_server(int type, client_t *client, client_t *client2) -{ - char msg[STR_PROTO_SIZE]; - int n = WORLD_COUNT_ROUND_UNLIMITED; - - assert( client2 != NULL ); - -#ifndef PUBLIC_SERVER - getSettingCountRound(&n); -#endif - - sprintf(msg, "init %d %d %d %d %s\n", - client2->tux->id, client2->tux->x, client2->tux->y, - n, getArenaNetName( getChoiceArenaId() )); - - //proto_send(type, client, msg); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_init_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - char arena_name[STR_PROTO_SIZE]; - tux_t *tux; - int id; - int x, y, n; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d %d %d %s", - cmd, &id, &x, &y, &n, arena_name); - - setWorldArena( getArenaIdFormNetName(arena_name) ); - setMaxCountRound(n); - - tux = newTux(); - replaceTuxID(tux, id); - tux->x = x; - tux->y = y; - tux->control = TUX_CONTROL_KEYBOARD_RIGHT; - setControlTux(tux, TUX_CONTROL_KEYBOARD_RIGHT); - - getSettingNameRight(tux->name); - - addObjectToSpace(getCurrentArena()->spaceTux, tux); -} - -#endif - -void proto_send_event_server(int type, client_t *client, tux_t *tux, int action) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "event %d %d\n", tux->id, action); - - //proto_send(type, client, msg); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_event_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, action; - tux_t *tux; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d", cmd, &id, &action); - - tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); - - if( tux != NULL ) - { - actionTux(tux, action); - } -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_event_client(int action) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "event %d\n", action); - - sendServer(msg); -} - -#endif - -/* -static void debug_interval() -{ - static my_time_t lastEvent = 0; - my_time_t curentTime; - - if( lastEvent == 0 ) - { - lastEvent = getMyTime(); - } - - curentTime = getMyTime(); - - printf("time = %d\n", curentTime - lastEvent); - - lastEvent = getMyTime(); -} -*/ - -void proto_recv_event_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int action; - - assert( client != NULL ); - assert( msg != NULL ); - - //debug_interval(); - - sscanf(msg, "%s %d", cmd, &action); - - proto_send_event_server(PROTO_SEND_ALL_SEES_TUX, client, client->tux, action); - - refreshLastMove(client->protect); - actionTux(client->tux, action); -} - -void proto_send_newtux_server(int type, client_t *client, tux_t *tux) -{ - char msg[STR_PROTO_SIZE]; - int x, y; - - assert( tux != NULL ); - - if( tux->bonus == BONUS_HIDDEN ) - { - x = TUX_LOCATE_UNKNOWN; - y = TUX_LOCATE_UNKNOWN; - } - else - { - x = tux->x; - y = tux->y; - } - - getTuxProportion(tux, &x, &y, NULL, NULL); - - sprintf(msg, "newtux %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d\n", - tux->id ,x, y, tux->status, tux->position ,tux->frame, tux->score, tux->name, - tux->gun, tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE], - tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY], tux->shot[GUN_LASSER], - tux->shot[GUN_MINE], tux->shot[GUN_BOMBBALL], - tux->bonus_time, tux->pickup_time); - - //proto_send(type, client, msg); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_newtux_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - char name[STR_NAME_SIZE]; - int id, x, y, status, position, frame, score; - int myGun, myBonus; - int gun1, gun2, gun3, gun4, gun5, gun6, gun7; - int time1, time2; - tux_t *tux; - - assert( msg != NULL ); - - if( getCurrentArena() == NULL ) - { - return; - } - - sscanf(msg, "%s %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d", - cmd, &id, &x, &y, &status, &position, &frame, &score, name, - &myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, &gun7, &time1, &time2); - - tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); - - if( tux != NULL ) - { - assert( tux->id == id ); - } - - if( tux == NULL ) - { - char term_msg[STR_SIZE]; - sprintf(term_msg, "connect new client( id = %d)\n", id); - appendTextInTerm(term_msg); - - tux = newTux(); - replaceTuxID(tux, id); - tux->control = TUX_CONTROL_NET; - addObjectToSpace(getCurrentArena()->spaceTux, tux); - } - - moveObjectInSpace(getCurrentArena()->spaceTux, tux, x, y); - tux->status = status; - tux->position = position; - tux->frame = frame; - tux->score = score; - strcpy(tux->name, name); - tux->gun = myGun; - tux->bonus = myBonus; - tux->shot[GUN_SIMPLE] = gun1; - tux->shot[GUN_DUAL_SIMPLE] = gun2; - tux->shot[GUN_SCATTER] = gun3; - tux->shot[GUN_TOMMY] = gun4; - tux->shot[GUN_LASSER] = gun5; - tux->shot[GUN_MINE] = gun6; - tux->shot[GUN_BOMBBALL] = gun7; - tux->bonus_time = time1; - tux->pickup_time = time2; -} - -#endif - -void proto_send_kill_server(int type, client_t *client, tux_t *tux) -{ - char msg[STR_PROTO_SIZE]; - - assert( tux != NULL ); - - sprintf(msg, "kill %d\n", tux->id); - - //proto_send(type, client, msg); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_kill_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id; - tux_t *tux; - - assert( msg != NULL ); - - sscanf(msg, "%s %d", cmd, &id); - - tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); - - if( tux != NULL ) - { - eventTuxIsDead(tux); - } -} - -#endif - -void proto_send_del_server(int type, client_t *client, int id) -{ - char msg[STR_PROTO_SIZE]; - int check_id; - - check_id = getNewID(); - - sprintf(msg, "del %d %d\n", - id, check_id); - - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_CHECK, check_id); - //proto_check(type, client, msg, check_id); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_del_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, check_id; - tux_t *tux; - shot_t *shot; - item_t *item; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d", - cmd, &id, &check_id); - - proto_send_check_client(check_id); - - tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); - - if( tux != NULL ) - { - char term_msg[STR_SIZE]; - - //printf("delete tux..\n"); - sprintf(term_msg, "tux with id %d is disconnect\n", tux->id); - appendTextInTerm(term_msg); - - delObjectFromSpaceWithObject(getCurrentArena()->spaceTux, tux, destroyTux); - return; - } - - shot = getObjectFromSpaceWithID(getCurrentArena()->spaceShot, id); - - if( shot != NULL ) - { - //printf("delete shot..\n"); - delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); - return; - } - - item = getObjectFromSpaceWithID(getCurrentArena()->spaceItem, id); - - if( item != NULL ) - { - //printf("delete item..\n"); - delObjectFromSpaceWithObject(getCurrentArena()->spaceItem, item, destroyItem); - return; - } -} - -#endif - -void proto_send_additem_server(int type, client_t *client, item_t *p) -{ - char msg[STR_PROTO_SIZE]; - int check_id; - - assert( p != NULL ); - - check_id = getNewID(); - - sprintf(msg, "additem %d %d %d %d %d %d %d %d\n", - p->id, p->type, p->x, p->y, p->count, p->frame, p->author_id, check_id); - - //proto_send(type, client, msg); - //proto_check(type, client, msg, check_id); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_CHECK, check_id); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_additem_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, type, x, y, count, frame, author_id, check_id; - item_t *item; - - assert( msg != NULL ); - - if( getCurrentArena() == NULL ) - { - return; - } - - sscanf(msg, "%s %d %d %d %d %d %d %d %d", - cmd, &id, &type, &x, &y, &count, &frame, &author_id, &check_id); - - proto_send_check_client(check_id); - - if( ( item = getObjectFromSpaceWithID(getCurrentArena()->spaceItem, id) ) != NULL ) - { - return; - } - - item = newItem(x, y, type, author_id); - - replaceItemID(item, id); - item->count = count; - item->frame = frame; - - addObjectToSpace(getCurrentArena()->spaceItem, item); -} - -#endif - -void proto_send_shot_server(int type, client_t *client, shot_t *p) -{ - char msg[STR_PROTO_SIZE]; - - assert( p != NULL ); - - sprintf(msg, "shot %d %d %d %d %d %d %d %d %d\n", - p->id, p->x, p->y, p->px, p->py, p->position, p->gun, p->author_id, p->isCanKillAuthor); - - //proto_send(type, client, msg); - //proto_check(type, client, msg, check_id); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_shot_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int x, y, px, py, position, gun, shot_id, author_id, isCanKillAuthor; - shot_t *shot; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d %d %d %d %d %d %d %d", - cmd, &shot_id, &x, &y, &px, &py, &position, &gun, &author_id, &isCanKillAuthor); - - - if( ( shot = getObjectFromSpaceWithID(getCurrentArena()->spaceShot, shot_id) ) != NULL ) - { - delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); - - //return; - } - - shot = newShot(x, y, px, py, gun, author_id); - - replaceShotID(shot, shot_id); - shot->isCanKillAuthor = isCanKillAuthor; - shot->position = position; - - if( shot->gun == GUN_LASSER ) - { - transformOnlyLasser(shot); - } - - addObjectToSpace(getCurrentArena()->spaceShot, shot); -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_ping_client() -{ - char msg[STR_PROTO_SIZE]; - sprintf(msg, "ping\n"); - sendServer(msg); -} - -#endif - -void proto_recv_ping_server(client_t *client, char *msg) -{ -} - -void proto_send_ping_server(int type, client_t *client) -{ - char msg[STR_PROTO_SIZE]; - sprintf(msg, "ping\n"); - //proto_send(type, client, msg); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_ping_client(char *msg) -{ -} - -#endif - -void proto_send_end_server(int type, client_t *client) -{ - char msg[STR_PROTO_SIZE]; - strcpy(msg, "end\n"); - //proto_send(type, client, msg); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_end_client(char *msg) -{ - assert( msg != NULL ); - - setWorldEnd(); -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_end_client() -{ - char msg[STR_PROTO_SIZE]; - strcpy(msg, "end\n"); - sendServer(msg); -} - -#endif - -void proto_recv_end_server(client_t *client, char *msg) -{ - assert( msg != NULL ); - assert( client != NULL ); - - client->status = NET_STATUS_ZOMBIE; -} - diff --git a/src/publicServer.c b/src/publicServer.c deleted file mode 100644 index 4253e17..0000000 --- a/src/publicServer.c +++ /dev/null @@ -1,191 +0,0 @@ - -#include -#include -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "idManager.h" -#include "myTimer.h" -#include "arena.h" -#include "item.h" -#include "shot.h" -#include "arenaFile.h" -#include "proto.h" -#include "publicServer.h" -#include "net_multiplayer.h" -#include "serverConfigFile.h" -#include "heightScore.h" -#include "modules.h" - -static int arenaId; -static arena_t *arena; -static bool_t isSignalEnd; - -void countRoundInc() -{ -} - -static char *getSetting(char *env, char *param, char *default_val) -{ - return getParamElse(param, getServerConfigFileValue(env, default_val) ); -} - -static int initPublicServerNetwork() -{ - char ip4[STR_IP_SIZE]; - char ip6[STR_IP_SIZE]; - char *p_ip4, *p_ip6; - int port4; - int port6; - int ret; - - ret = -1; - - strcpy(ip4, getSetting("IP4", "--ip4", "none" ) ); - strcpy(ip6, getSetting("IP6", "--ip6", "none" ) ); - - p_ip4 = ip4; - - if( strcmp(ip4, "none") == 0 ) - { - p_ip4 = NULL; - } - - p_ip6 = ip6; - - if( strcmp(ip6, "none") == 0 ) - { - p_ip6= NULL; - } - - port4 = atoi( getSetting("PORTv4", "--port", "2200") ); - port6 = atoi( getSetting("PORTv6", "--port", "2200") ); - - ret = initNetMulitplayerPublicServer(p_ip4, port4, p_ip6, port6); - - return ret; -} - -int initPublicServer() -{ - int ret; - - initListID(); - initModule(); - initArenaFile(); - initTux(); - initItem(); - initShot(); - initServerConfigFile(); - initHeightScore( getSetting("SCOREFILE", "--scorefile", "./heightscore") ); - - arenaId = getArenaIdFormNetName( getSetting("ARENA", "--arena", "FAGN") ); - arena = getArena(arenaId); - setCurrentArena(arena); - - addNewItem(arena->spaceItem, ID_UNKNOWN); - isSignalEnd = FALSE; - - ret = initPublicServerNetwork(); - - if( ret < 0 ) - { - printf("Nemozem inicalizovat sietovy socket !\n"); - return -1; - } - - setServerMaxClients( atoi( getSetting("MAX_CLIENTS", "--maxclients", "32") )); - - return 0; -} - -int getChoiceArenaId() -{ - return arenaId; -} - -void eventPublicServer() -{ - static my_time_t lastActive = 0; - my_time_t interval; - - eventNetMultiplayer(); - - if( isSignalEnd == TRUE ) - { - quitPublicServer(); - } - - if( lastActive == 0 ) - { - lastActive = getMyTime(); - } - - interval = getMyTime() - lastActive; - - if( interval < 50 ) - { - return; - } - -/* - printf("interval = %d\n", interval); -*/ - lastActive = getMyTime(); - - //fce_interval(); - eventArena(arena); -} - -void my_handler_quit(int n) -{ - printf("my_handler_quit\n"); - isSignalEnd = TRUE; -} - -void quitPublicServer() -{ - printf("quit public server\n"); - - quitNetMultiplayer(); - destroyArena(arena); - - quitTux(); - quitItem(); - quitShot(); - - quitArenaFile(); - quitServerConfigFile(); - quitModule(); - quitListID(); - quitHeightScore(); - destroyList(listHelp); - - exit(0); -} - -int startPublicServer() -{ - signal(SIGPIPE, SIG_IGN); - signal(SIGINT, my_handler_quit); - signal(SIGTERM, my_handler_quit); - signal(SIGQUIT, my_handler_quit); - - if( initPublicServer() < 0 ) - { - quitPublicServer(); - return -1; - } - - while(1) - { - eventPublicServer(); - } - - return 0; -} diff --git a/src/screen.c b/src/screen.c deleted file mode 100644 index 07a30a2..0000000 --- a/src/screen.c +++ /dev/null @@ -1,136 +0,0 @@ - -#include -#include - -#include "main.h" -#include "interface.h" -#include "screen.h" -#include "list.h" -#include "layer.h" - -static screen_t *currentScreen; -static list_t *listScreen; - -static bool_t isScreenInit = FALSE; - -bool_t isScreenInicialized() -{ - return isScreenInit; -} - -screen_t* newScreen(char *name, - void (*fce_start)(), void (*fce_event)(), - void (*fce_draw)(), void (*fce_stop)()) -{ - screen_t *new; - - assert( name != NULL ); - assert( fce_start != NULL ); - assert( fce_event != NULL ); - assert( fce_draw != NULL ); - assert( fce_stop != NULL ); - - new = malloc( sizeof(screen_t) ); - new->name = strdup(name); - new->fce_start = fce_start; - new->fce_event = fce_event; - new->fce_draw = fce_draw; - new->fce_stop = fce_stop; - - return new; -} - -void destroyScreen(screen_t *p) -{ - assert( p != NULL ); - - free(p->name); - free(p); -} - -void registerScreen(screen_t *p) -{ - assert( p != NULL ); - - printf("register screen %s..\n", p->name); - addList(listScreen, p); -} - -void initScreen() -{ - listScreen = newList(); - currentScreen = NULL; - isScreenInit = TRUE; -} - -void setScreen(char *name) -{ - int i; - screen_t *this; - - assert( name != NULL ); - - if( currentScreen != NULL && - strcmp(currentScreen->name, name) == 0 ) - { - return; - } - - printf("switch screen %s..\n", name); - - for( i = 0 ; i < listScreen->count ; i++ ) - { - this = (screen_t *) (listScreen->list[i]); - assert( this != NULL ); - - if( strcmp(name, this->name) == 0 ) - { - flushLayer(); - - if( currentScreen != NULL ) - { - printf("stop screen %s..\n", currentScreen->name); - currentScreen->fce_stop(); - } - - currentScreen = this; - printf("start screen %s..\n", currentScreen->name); - currentScreen->fce_start(); - - return; - } - } - - assert( ! "screen sa nenasiel !" ); -} - -char* getScreen() -{ - assert( currentScreen != NULL ); - return currentScreen->name; -} - -void drawScreen() -{ - assert( currentScreen != NULL ); - - currentScreen->fce_draw(); - - interfaceRefresh(); -} - -void eventScreen() -{ - assert( currentScreen != NULL ); - - currentScreen->fce_event(); -} - -void quitScreen() -{ - assert( listScreen != NULL ); - - destroyListItem(listScreen, destroyScreen); - isScreenInit = FALSE; -} - diff --git a/src/screen/Makefile b/src/screen/Makefile new file mode 100644 index 0000000..41c8ae1 --- /dev/null +++ b/src/screen/Makefile @@ -0,0 +1,29 @@ + +all: screen_analyze.o screen_credits.o screen_gameType.o screen_choiceArena.o screen_mainMenu.o screen_setting.o screen_table.o screen_world.o + +screen_analyze.o: screen_analyze.c screen_analyze.h + $(CC) $(CFLAGS) -o screen_analyze.o -c screen_analyze.c + +screen_credits.o: screen_credits.c screen_credits.h + $(CC) $(CFLAGS) -o screen_credits.o -c screen_credits.c + +screen_gameType.o: screen_gameType.c screen_gameType.h + $(CC) $(CFLAGS) -o screen_gameType.o -c screen_gameType.c + +screen_mainMenu.o: screen_mainMenu.c screen_mainMenu.h + $(CC) $(CFLAGS) -o screen_mainMenu.o -c screen_mainMenu.c + +screen_setting.o: screen_setting.c screen_setting.h + $(CC) $(CFLAGS) -o screen_setting.o -c screen_setting.c + +screen_table.o: screen_table.c screen_table.h + $(CC) $(CFLAGS) -o screen_table.o -c screen_table.c + +screen_world.o: screen_world.c screen_world.h + $(CC) $(CFLAGS) -o screen_world.o -c screen_world.c + + +clean: + rm -rf *.o + + diff --git a/src/screen/screen_analyze.c b/src/screen/screen_analyze.c new file mode 100644 index 0000000..c1b8258 --- /dev/null +++ b/src/screen/screen_analyze.c @@ -0,0 +1,179 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" + +#include "client/interface.h" +#include "client/screen.h" +#include "client/image.h" + +#ifndef NO_SOUND +#include "audio/music.h" +#endif + +#include "screen/screen_mainMenu.h" +#include "screen/screen_analyze.h" + +#include "widget/widget_label.h" +#include "widget/widget_button.h" +#include "widget/widget_image.h" + +static widget_image_t *image_backgorund; + +static list_t *listWidgetLabelName; +static list_t *listWidgetLabelScore; +static list_t *listAnalyze; +static widget_label_t *widgetLabelMsg; + +static widget_button_t *button_ok; + +static analyze_t* newAnalyze(char *name, int score) +{ + analyze_t *new; + + new = malloc( sizeof(analyze_t) ); + new->name = strdup(name); + new->score = score; + + return new; +} + +static void destroyAnalyze(analyze_t *p) +{ + free(p->name); + free(p); +} + +void startScreenAnalyze() +{ +#ifndef NO_SOUND + playMusic("menu", MUSIC_GROUP_BASE); +#endif +} + +void drawScreenAnalyze() +{ + widget_label_t *this; + int i; + + drawWidgetImage(image_backgorund); + + for( i = 0 ; i < listWidgetLabelName->count ; i++ ) + { + this = (widget_label_t *)(listWidgetLabelName->list[i]); + drawWidgetLabel(this); + } + + for( i = 0 ; i < listWidgetLabelScore->count ; i++ ) + { + this = (widget_label_t *)(listWidgetLabelScore->list[i]); + drawWidgetLabel(this); + } + + drawWidgetLabel(widgetLabelMsg); + drawWidgetButton(button_ok); +} + +void eventScreenAnalyze() +{ + eventWidgetButton(button_ok); +} + +void stopScreenAnalyze() +{ + destroyWidgetLabel(widgetLabelMsg); + widgetLabelMsg = newWidgetLabel("", WINDOW_SIZE_X / 2 , 250, WIDGET_LABEL_CENTER); +} + +static void eventWidget(void *p) +{ + widget_button_t *button; + + button = (widget_button_t *)(p); + + if( button == button_ok ) + { + setScreen("mainMenu"); + } +} + +void restartAnalyze() +{ + destroyListItem(listWidgetLabelName, destroyWidgetLabel); + destroyListItem(listWidgetLabelScore, destroyWidgetLabel); + destroyListItem(listAnalyze, destroyAnalyze); + + listWidgetLabelName = newList(); + listWidgetLabelScore = newList(); + listAnalyze = newList(); +} + +void addAnalyze(char *name, int score) +{ + addList(listAnalyze, newAnalyze(name, score) ); +} + +void setMsgToAnalyze(char *msg) +{ + destroyWidgetLabel(widgetLabelMsg); + widgetLabelMsg = newWidgetLabel(msg, WINDOW_SIZE_X / 2 , 250, WIDGET_LABEL_CENTER); +} + +void endAnalyze() +{ + int i; + + for( i = 0 ; i < listAnalyze->count ; i++ ) + { + analyze_t *this; + char *str; + + this = (analyze_t *)(listAnalyze->list[i]); + + addList(listWidgetLabelName, newWidgetLabel(this->name, + 100, 200 + 20*i, WIDGET_LABEL_LEFT) ); + + str = getString(this->score); + + addList(listWidgetLabelScore, newWidgetLabel(str, + WINDOW_SIZE_X - 100, 200 + 20*i, WIDGET_LABEL_RIGHT) ); + + free(str); + } +} + +void initScreenAnalyze() +{ + SDL_Surface *image; + + image = getImage(IMAGE_GROUP_BASE, "screen_main"); + image_backgorund = newWidgetImage(0, 0, image); + + listWidgetLabelName = newList(); + listWidgetLabelScore = newList(); + listAnalyze = newList(); + + button_ok = newWidgetButton("OK", WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, + WINDOW_SIZE_Y-100, eventWidget); + + registerScreen( newScreen("analyze", startScreenAnalyze, eventScreenAnalyze, + drawScreenAnalyze, stopScreenAnalyze) ); + + widgetLabelMsg = newWidgetLabel("", WINDOW_SIZE_X / 2 , 250, WIDGET_LABEL_CENTER); +} + +void quitScreenAnalyze() +{ + destroyWidgetImage(image_backgorund); + + destroyListItem(listWidgetLabelName, destroyWidgetLabel); + destroyListItem(listWidgetLabelScore, destroyWidgetLabel); + destroyListItem(listAnalyze, destroyAnalyze); + + destroyWidgetButton(button_ok); + destroyWidgetLabel(widgetLabelMsg); +} + diff --git a/src/screen/screen_analyze.h b/src/screen/screen_analyze.h new file mode 100644 index 0000000..b6a9875 --- /dev/null +++ b/src/screen/screen_analyze.h @@ -0,0 +1,25 @@ + +#ifndef SCREEN_ANALYZE_H + +#define SCREEN_ANALYZE_H + +#include "base/main.h" + +typedef struct analyze_struct +{ + char *name; + int score; +} analyze_t; + +extern void startScreenAnalyze(); +extern void drawScreenAnalyze(); +extern void eventScreenAnalyze(); +extern void stopScreenAnalyze(); +extern void restartAnalyze(); +extern void addAnalyze(char *name, int score); +extern void setMsgToAnalyze(char *msg); +extern void endAnalyze(); +extern void initScreenAnalyze(); +extern void quitScreenAnalyze(); + +#endif diff --git a/src/screen/screen_choiceArena.c b/src/screen/screen_choiceArena.c new file mode 100644 index 0000000..629f1ba --- /dev/null +++ b/src/screen/screen_choiceArena.c @@ -0,0 +1,172 @@ + +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/arenaFile.h" + +#include "client/language.h" +#include "client/interface.h" +#include "client/screen.h" +#include "client/image.h" + +#ifndef NO_SOUND +#include "audio/music.h" +#endif + +#include "screen/screen_mainMenu.h" +#include "screen/screen_choiceArena.h" + +#include "widget/widget_label.h" +#include "widget/widget_button.h" +#include "widget/widget_image.h" +#include "widget/widget_buttonimage.h" + +static widget_image_t *image_backgorund; + +static widget_button_t *button_play; +static widget_button_t *button_back; + +static list_t *listWidgetButtonimage; +static int choiceArenaId; + +void startScreenChoiceArena() +{ +#ifndef NO_SOUND + playMusic("menu", MUSIC_GROUP_BASE); +#endif +} + +void drawScreenChoiceArena() +{ + int i; + + drawWidgetImage(image_backgorund); + + drawWidgetButton(button_play); + drawWidgetButton(button_back); + + for( i = 0 ; i < listWidgetButtonimage->count ; i++ ) + { + widget_buttonimage_t *this; + this = (widget_buttonimage_t *) listWidgetButtonimage->list[i]; + drawWidgetButtonimage(this); + } +} + +void eventScreenChoiceArena() +{ + int i; + + eventWidgetButton(button_play); + eventWidgetButton(button_back); + + for( i = 0 ; i < listWidgetButtonimage->count ; i++ ) + { + widget_buttonimage_t *this; + this = (widget_buttonimage_t *) listWidgetButtonimage->list[i]; + eventWidgetButtonimage(this); + } +} + +void stopScreenChoiceArena() +{ +} + +static void eventWidgetButtonImage(void *p) +{ + widget_buttonimage_t *buttonimage; + int i; + + buttonimage = (widget_buttonimage_t *)(p); + + for( i = 0 ; i < listWidgetButtonimage->count ; i++ ) + { + widget_buttonimage_t *this; + + this = (widget_buttonimage_t *)(listWidgetButtonimage->list[i]); + + if( buttonimage == this ) + { + this->active = 1; + choiceArenaId = i; + } + else + { + this->active = 0; + } + } +} + +int getChoiceArenaId() +{ + return choiceArenaId; +} + +static void eventWidget(void *p) +{ + widget_button_t *button; + + button = (widget_button_t *)(p); + + if( button == button_play ) + { + setScreen("world"); + } + + if( button == button_back ) + { + setScreen("gameType"); + } +} + +void initScreenChoiceArena() +{ + SDL_Surface *image; + int i; + + image = getImage(IMAGE_GROUP_BASE, "screen_main"); + image_backgorund = newWidgetImage(0, 0, image); + + button_back = newWidgetButton(getMyText("BACK"), 100, WINDOW_SIZE_Y-100, eventWidget); + button_play = newWidgetButton(getMyText("PLAY"), WINDOW_SIZE_X-200, WINDOW_SIZE_Y-100, eventWidget); + + listWidgetButtonimage = newList(); + choiceArenaId = 0; + + for( i = 0 ; i < getArenaCount() ; i++ ) + { + SDL_Surface *image; + widget_buttonimage_t *widget_buttonimage; + int x, y; + + image = addImageData(getArenaImage(i), IMAGE_NO_ALPHA, "none", IMAGE_GROUP_BASE); + + x = 100 + 200 * ( i - 3 * ( i/3 ) ); + y = 150 + 200 * ( i/3 ); + + widget_buttonimage = newWidgetButtonimage(image, + x, y, eventWidgetButtonImage); + + if( i == choiceArenaId ) + { + widget_buttonimage->active = 1; + } + + addList( listWidgetButtonimage, widget_buttonimage); + } + + registerScreen( newScreen("chiceArena", startScreenChoiceArena, eventScreenChoiceArena, + drawScreenChoiceArena, stopScreenChoiceArena) ); +} + +void quitScreenChoiceArena() +{ + destroyWidgetImage(image_backgorund); + + destroyWidgetButton(button_play); + destroyWidgetButton(button_back); + + destroyListItem(listWidgetButtonimage, destroyWidgetButtonimage); +} diff --git a/src/screen/screen_choiceArena.h b/src/screen/screen_choiceArena.h new file mode 100644 index 0000000..0437aa1 --- /dev/null +++ b/src/screen/screen_choiceArena.h @@ -0,0 +1,17 @@ + +#ifndef SCREEN_CHOICE_ARENA + +#define SCREEN_CHOICE_ARENA + +#include "base/main.h" + +#define SCREENSHOT_ARENA_WIDTH 160 +#define SCREENSHOT_ARENA_HEIGHT 107 + +extern void drawScreenChoiceArena(); +extern void eventScreenChoiceArena(); +extern int getChoiceArenaId(); +extern void initScreenChoiceArena(); +extern void quitScreenChoiceArena(); + +#endif diff --git a/src/screen/screen_credits.c b/src/screen/screen_credits.c new file mode 100644 index 0000000..3f6cf84 --- /dev/null +++ b/src/screen/screen_credits.c @@ -0,0 +1,134 @@ + +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/textFile.h" + +#include "client/language.h" +#include "client/interface.h" +#include "client/screen.h" +#include "client/image.h" + +#ifndef NO_SOUND +#include "audio/music.h" +#endif + +#include "screen/screen_credits.h" + +#include "widget/widget_image.h" +#include "widget/widget_label.h" +#include "widget/widget_button.h" + +static widget_image_t *image_backgorund; +static widget_button_t *button_back; +static list_t *listWidgetLabel; +static textFile_t *textFile; +static int offset; + +void startScreenCredits() +{ +#ifndef NO_SOUND + playMusic("menu", MUSIC_GROUP_BASE); +#endif + offset = 0; +} + +void drawScreenCredits() +{ + int i; + + drawWidgetImage(image_backgorund); + + drawWidgetButton(button_back); + + for( i = 0 ; i < listWidgetLabel->count ; i++ ) + { + int z; + + widget_label_t *this; + this = (widget_label_t *)listWidgetLabel->list[i]; + + z = this->y; + this->y += offset; + + if( this->y > SCREEN_CREDITS_OFFSET_MIN && this->y < SCREEN_CREDITS_OFFSET_MAX ) + { + drawWidgetLabel(this); + } + + this->y = z; + } + +} + +void eventScreenCredits() +{ + eventWidgetButton(button_back); + + offset -= SCREEN_CREDITS_OFFSET_SPEED; + + if( offset < SCREEN_CREDITS_OFFSET_RESTART ) + { + offset = 0; + } + + //printf("offset = %d\n", offset); +} + +void stopScreenCredits() +{ +} + +static void eventWidget(void *p) +{ + widget_button_t *button; + + button = (widget_button_t *)(p); + + if( button == button_back ) + { + setScreen("mainMenu"); + } +} + +void initScreenCredits() +{ + SDL_Surface *image; + int i; + + image = getImage(IMAGE_GROUP_BASE, "screen_main"); + image_backgorund = newWidgetImage(0, 0, image); + + button_back = newWidgetButton(getMyText("BACK"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, WINDOW_SIZE_Y-80, eventWidget); + + listWidgetLabel = newList(); + accessExistFile(PATH_DATA SCREEN_CREDITS_FILE); + textFile = loadTextFile(PATH_DATA SCREEN_CREDITS_FILE); + + for( i = 0 ; i < textFile->text->count ; i++ ) + { + widget_label_t *label; + char *line; + + line = (char *)textFile->text->list[i]; + label = newWidgetLabel(line, WINDOW_SIZE_X/2, (WINDOW_SIZE_Y-100)+i*20, + WIDGET_LABEL_CENTER); + + addList(listWidgetLabel, label); + } + + registerScreen( newScreen("credits", startScreenCredits, eventScreenCredits, + drawScreenCredits, stopScreenCredits) ); +} + +void quitScreenCredits() +{ + destroyWidgetImage(image_backgorund); + + destroyWidgetButton(button_back); + destroyListItem(listWidgetLabel, destroyWidgetLabel); + destroyTextFile(textFile); +} + diff --git a/src/screen/screen_credits.h b/src/screen/screen_credits.h new file mode 100644 index 0000000..2bbce80 --- /dev/null +++ b/src/screen/screen_credits.h @@ -0,0 +1,20 @@ + +#ifndef SCREEN_CREDITS_H + +#define SCREEN_CREDITS_H + +#define SCREEN_CREDITS_FILE "credits.en" + +#define SCREEN_CREDITS_OFFSET_MIN 130 +#define SCREEN_CREDITS_OFFSET_MAX WINDOW_SIZE_Y-120 +#define SCREEN_CREDITS_OFFSET_RESTART -1000 +#define SCREEN_CREDITS_OFFSET_SPEED 2 + +extern void startScreenCredits(); +extern void drawScreenCredits(); +extern void eventScreenCredits(); +extern void stopScreenCredits(); +extern void initScreenCredits(); +extern void quitScreenCredits(); + +#endif diff --git a/src/screen/screen_gameType.c b/src/screen/screen_gameType.c new file mode 100644 index 0000000..5545fc1 --- /dev/null +++ b/src/screen/screen_gameType.c @@ -0,0 +1,256 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/net_multiplayer.h" +#include "base/tux.h" + +#include "client/language.h" +#include "client/interface.h" +#include "client/screen.h" +#include "client/image.h" + +#ifndef NO_SOUND +#include "audio/music.h" +#endif + +#include "screen/screen_gameType.h" +#include "screen/screen_setting.h" + +#include "widget/widget_label.h" +#include "widget/widget_button.h" +#include "widget/widget_image.h" +#include "widget/widget_textfield.h" +#include "widget/widget_check.h" + +static widget_image_t *image_backgorund; + +static widget_button_t *button_back; +static widget_button_t *button_play; + +static widget_label_t *label_none; +static widget_label_t *label_server; +static widget_label_t *label_client; + +static widget_label_t *label_ip; +static widget_label_t *label_port; + +static widget_check_t *check_none; +static widget_check_t *check_server; +static widget_check_t *check_client; + +static widget_textfield_t *textfield_ip; +static widget_textfield_t *textfield_port; + +void startScreenGameType() +{ +#ifndef NO_SOUND + playMusic("menu", MUSIC_GROUP_BASE); +#endif +} + +void drawScreenGameType() +{ + drawWidgetImage(image_backgorund); + + drawWidgetLabel(label_none); + drawWidgetLabel(label_server); + drawWidgetLabel(label_client); + + if( check_none->status == FALSE ) + { + drawWidgetLabel(label_port); + drawWidgetTextfield(textfield_port); + drawWidgetLabel(label_ip); + drawWidgetTextfield(textfield_ip); + } + + drawWidgetCheck(check_none); + drawWidgetCheck(check_server); + drawWidgetCheck(check_client); + + drawWidgetButton(button_back); + drawWidgetButton(button_play); +} + +void eventScreenGameType() +{ + eventWidgetCheck(check_none); + eventWidgetCheck(check_server); + eventWidgetCheck(check_client); + + eventWidgetTextfield(textfield_ip); + eventWidgetTextfield(textfield_port); + + eventWidgetButton(button_back); + eventWidgetButton(button_play); +} + +void stopScreenGameType() +{ +} + +static void eventWidget(void *p) +{ + widget_button_t *button; + widget_check_t *check; + + button = (widget_button_t *)(p); + check = (widget_check_t *)(p); + + if( check == check_none ) + { + check_none->status = TRUE; + check_server->status = FALSE; + check_client->status = FALSE; + return; + } + + if( check == check_server ) + { + check_none->status = FALSE; + check_server->status = TRUE; + check_client->status = FALSE; + return; + } + + if( check == check_client ) + { + check_none->status = FALSE; + check_server->status = FALSE; + check_client->status = TRUE; + return; + } + + if( button == button_back ) + { + setScreen("mainMenu"); + } + + if( button == button_play ) + { + if( getSettingGameType() == NET_GAME_TYPE_CLIENT ) + { + setScreen("world"); + } + else + { + setScreen("chiceArena"); + } + } +} + +void initScreenGameType() +{ + SDL_Surface *image; + + image = getImage(IMAGE_GROUP_BASE, "screen_main"); + image_backgorund = newWidgetImage(0, 0, image); + + button_back = newWidgetButton(getMyText("BACK"), 100, WINDOW_SIZE_Y-100, eventWidget); + button_play = newWidgetButton(getMyText("PLAY"), WINDOW_SIZE_X-200, WINDOW_SIZE_Y-100, eventWidget); + + check_none = newWidgetCheck(100, 150, FALSE, eventWidget); + check_server = newWidgetCheck(100, 200, FALSE, eventWidget); + check_client = newWidgetCheck(100, 250, FALSE, eventWidget); + + if( isParamFlag("--server") ) + { + check_server->status = TRUE; + } + else if( isParamFlag("--client") ) + { + check_client->status = TRUE; + } + else + { + check_none->status = TRUE; + } + + label_none = newWidgetLabel(getMyText("NONE"), 130, 145, WIDGET_LABEL_LEFT); + label_server = newWidgetLabel(getMyText("SERVER"), 130, 195, WIDGET_LABEL_LEFT); + label_client = newWidgetLabel(getMyText("CLIENT"), 130, 245, WIDGET_LABEL_LEFT); + + label_ip = newWidgetLabel(getMyText("IP_ADDR"), 300, 145, WIDGET_LABEL_LEFT); + label_port = newWidgetLabel(getMyText("NET_PORT"), 300, 245, WIDGET_LABEL_LEFT); + + textfield_ip = newWidgetTextfield(getParamElse("--ip", "127.0.0.1"), 300, 180); + textfield_port = newWidgetTextfield(getParamElse("--port", "2200"), 300, 280); + + registerScreen( newScreen("gameType", startScreenGameType, eventScreenGameType, + drawScreenGameType, stopScreenGameType) ); +} + +int getSettingGameType() +{ + if( check_none->status == TRUE ) + { + return NET_GAME_TYPE_NONE; + } + + if( check_server->status == TRUE ) + { + return NET_GAME_TYPE_SERVER; + } + + if( check_client->status == TRUE ) + { + return NET_GAME_TYPE_CLIENT; + } + + assert( ! "Chyba pri zistvani type hry !" ); + + return -1; +} + +char* getSettingIP() +{ + return textfield_ip->text; +} + +int getSettingPort() +{ + return atoi( textfield_port->text ); +} + +int getSettingProto() +{ + if( strstr(textfield_ip->text, ".") != NULL ) + { + return PROTO_UDPv4; + } + + if( strstr(textfield_ip->text, ":") != NULL ) + { + return PROTO_UDPv6; + } + + printf("IP protokol unknown !\n"); + return -1; +} + +void quitScreenGameType() +{ + destroyWidgetImage(image_backgorund); + + destroyWidgetLabel(label_none); + destroyWidgetLabel(label_server); + destroyWidgetLabel(label_client); + + destroyWidgetLabel(label_ip); + destroyWidgetLabel(label_port); + + destroyWidgetTextfield(textfield_ip); + destroyWidgetTextfield(textfield_port); + + destroyWidgetButton(button_back); + destroyWidgetButton(button_play); + + destroyWidgetCheck(check_none); + destroyWidgetCheck(check_server); + destroyWidgetCheck(check_client); +} + diff --git a/src/screen/screen_gameType.h b/src/screen/screen_gameType.h new file mode 100644 index 0000000..93d38ee --- /dev/null +++ b/src/screen/screen_gameType.h @@ -0,0 +1,17 @@ + +#ifndef SCREEN_GAME_TYPE + +#define SCREEN_GAME_TYPE + +#include "base/main.h" + +extern void drawScreenGameType(); +extern void eventScreenGameType(); +extern void initScreenGameType(); +extern int getSettingGameType(); +extern char* getSettingIP(); +extern int getSettingPort(); +extern int getSettingProto(); +extern void quitScreenGameType(); + +#endif diff --git a/src/screen/screen_mainMenu.c b/src/screen/screen_mainMenu.c new file mode 100644 index 0000000..bd3886f --- /dev/null +++ b/src/screen/screen_mainMenu.c @@ -0,0 +1,128 @@ + +#include +#include + +#include "base/main.h" + +#include "client/game.h" +#include "client/interface.h" +#include "client/language.h" +#include "client/screen.h" +#include "client/image.h" + +#ifndef NO_SOUND +#include "audio/music.h" +#endif + +#include "screen/screen_mainMenu.h" +#include "screen/screen_setting.h" + +#include "widget/widget_label.h" +#include "widget/widget_button.h" +#include "widget/widget_textfield.h" +#include "widget/widget_check.h" +#include "widget/widget_image.h" + +static widget_image_t *image_backgorund; + +static widget_button_t *button_play; +static widget_button_t *button_setting; +static widget_button_t *button_table; +static widget_button_t *button_credits; +static widget_button_t *button_end; + +void startScreenMainMenu() +{ +#ifndef NO_SOUND + playMusic("menu", MUSIC_GROUP_BASE); +#endif +} + +void drawScreenMainMenu() +{ + drawWidgetImage(image_backgorund); + + drawWidgetButton(button_play); + drawWidgetButton(button_setting); + drawWidgetButton(button_table); + drawWidgetButton(button_credits); + drawWidgetButton(button_end); +} + +void eventScreenMainMenu() +{ + eventWidgetButton(button_play); + eventWidgetButton(button_setting); + eventWidgetButton(button_table); + eventWidgetButton(button_credits); + eventWidgetButton(button_end); +} + +void stopScreenMainMenu() +{ +} + +static void eventWidget(void *p) +{ + widget_button_t *button; + + button = (widget_button_t *)(p); + + if( button == button_play ) + { + setScreen("gameType"); + } + + if( button == button_setting ) + { + setScreen("setting"); + } + + if( button == button_table ) + { + setScreen("table"); + } + + if( button == button_credits ) + { + setScreen("credits"); + } + + if( button == button_end ) + { + quitGame(); + } +} + +void initScreenMainMenu() +{ + SDL_Surface *image; + + image = addImageData("screen_main.png", IMAGE_NO_ALPHA, "screen_main", IMAGE_GROUP_BASE); + image_backgorund = newWidgetImage(0, 0, image); + + button_play = newWidgetButton(getMyText("PLAY"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 200, eventWidget); + button_setting = newWidgetButton(getMyText("SETTING"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 250, eventWidget); + button_table = newWidgetButton(getMyText("TABLE"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 300, eventWidget); + button_credits = newWidgetButton(getMyText("CREDITS"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 350, eventWidget); + button_end = newWidgetButton(getMyText("END"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 400, eventWidget); + +#ifndef NO_SOUND + addMusic("menu.ogg", "menu", MUSIC_GROUP_BASE); +#endif + + registerScreen( newScreen("mainMenu", startScreenMainMenu, eventScreenMainMenu, + drawScreenMainMenu, stopScreenMainMenu) ); +} + +void quitScreenMainMenu() +{ + destroyWidgetImage(image_backgorund); + + destroyWidgetButton(button_play); + destroyWidgetButton(button_setting); + destroyWidgetButton(button_table); + destroyWidgetButton(button_credits); + destroyWidgetButton(button_end); +} + diff --git a/src/screen/screen_mainMenu.h b/src/screen/screen_mainMenu.h new file mode 100644 index 0000000..c04e65f --- /dev/null +++ b/src/screen/screen_mainMenu.h @@ -0,0 +1,15 @@ + +#ifndef SCREEN_MIAN_MENU_H + +#define SCREEN_MIAN_MENU_H + +#include "base/main.h" + +extern void startScreenMainMenu(); +extern void drawScreenMainMenu(); +extern void eventScreenMainMenu(); +extern void stopScreenMainMenu(); +extern void initScreenMainMenu(); +extern void quitScreenMainMenu(); + +#endif diff --git a/src/screen/screen_setting.c b/src/screen/screen_setting.c new file mode 100644 index 0000000..fd1388d --- /dev/null +++ b/src/screen/screen_setting.c @@ -0,0 +1,532 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/textFile.h" +#include "base/director.h" +#include "base/homeDirector.h" + +#include "client/configFile.h" +#include "client/language.h" +#include "client/interface.h" +#include "client/screen.h" +#include "client/image.h" + +#ifndef NO_SOUND +#include "audio/music.h" +#include "audio/sound.h" +#endif + +#include "screen/screen_mainMenu.h" +#include "screen/screen_setting.h" + +#include "widget/widget_label.h" +#include "widget/widget_button.h" +#include "widget/widget_image.h" +#include "widget/widget_textfield.h" +#include "widget/widget_check.h" +#include "widget/widget_select.h" + +static widget_image_t *image_backgorund; + +static widget_button_t *button_back; + +static widget_label_t *label_count_round; +static widget_label_t *label_name_player1; +static widget_label_t *label_name_player2; +static widget_label_t *label_ai; + +#ifndef NO_SOUND +static widget_label_t *label_music; +static widget_label_t *label_sound; +#endif + +static widget_check_t *check[ITEM_COUNT]; +#ifndef NO_SOUND +static widget_check_t *check_music; +static widget_check_t *check_sound; +#endif + +static widget_image_t *image_gun_dual_revolver; +static widget_image_t *image_gun_scatter; +static widget_image_t *image_gun_tommy; +static widget_image_t *image_gun_lasser; +static widget_image_t *image_gun_mine; +static widget_image_t *image_gun_bombball; + +static widget_image_t *image_bonus_speed; +static widget_image_t *image_bonus_shot; +static widget_image_t *image_bonus_teleport; +static widget_image_t *image_bonus_ghost; +static widget_image_t *image_bonus_4x; +static widget_image_t *image_bonus_hidden; + +static widget_textfield_t *textfield_count_cound; +static widget_textfield_t *textfield_name_player1; +static widget_textfield_t *textfield_name_player2; + +static widget_select_t *select_ai; + +static textFile_t *configFile; + +void startScreenSetting() +{ +#ifndef NO_SOUND + playMusic("menu", MUSIC_GROUP_BASE); +#endif +} + +void drawScreenSetting() +{ + int i; + + drawWidgetImage(image_backgorund); + + drawWidgetLabel(label_count_round); + drawWidgetLabel(label_name_player1); + drawWidgetLabel(label_name_player2); + drawWidgetLabel(label_ai); + +#ifndef NO_SOUND + drawWidgetLabel(label_music); + drawWidgetLabel(label_sound); +#endif + + drawWidgetTextfield(textfield_count_cound); + drawWidgetTextfield(textfield_name_player1); + drawWidgetTextfield(textfield_name_player2); + + drawWidgetImage(image_gun_dual_revolver); + drawWidgetImage(image_gun_scatter); + drawWidgetImage(image_gun_tommy); + drawWidgetImage(image_gun_lasser); + drawWidgetImage(image_gun_mine); + drawWidgetImage(image_gun_bombball); + + drawWidgetImage(image_bonus_speed); + drawWidgetImage(image_bonus_shot); + drawWidgetImage(image_bonus_teleport); + drawWidgetImage(image_bonus_ghost); + drawWidgetImage(image_bonus_4x); + drawWidgetImage(image_bonus_hidden); + +#ifndef NO_SOUND + drawWidgetCheck(check_music); + drawWidgetCheck(check_sound); +#endif + + for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) + { + drawWidgetCheck(check[i]); + } + + for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) + { + drawWidgetCheck(check[i]); + } + + drawWidgetSelect(select_ai); + + drawWidgetButton(button_back); +} + +void eventScreenSetting() +{ + int i; +/* + int x, y; + + getMousePosition(&x, &y); + + if( isMouseClicked() ) + { + printf("x = %d y = %d\n", x, y); + } +*/ + eventWidgetTextfield(textfield_count_cound); + eventWidgetTextfield(textfield_name_player1); + eventWidgetTextfield(textfield_name_player2); + +#ifndef NO_SOUND + eventWidgetCheck(check_music); + eventWidgetCheck(check_sound); +#endif + + for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) + { + eventWidgetCheck(check[i]); + } + + for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) + { + eventWidgetCheck(check[i]); + } + + eventWidgetSelect(select_ai); + + eventWidgetButton(button_back); +} + +void stopScreenSetting() +{ +} + +static void eventWidget(void *p) +{ + widget_button_t *button; + widget_check_t *check; + + button = (widget_button_t *)(p); + check = (widget_check_t *)(p); + + if( button == button_back ) + { + setScreen("mainMenu"); + } + +#ifndef NO_SOUND + if( check == check_music ) + { + setMusicActive( check_music->status ); + } + + if( check == check_sound ) + { + setSoundActive( check_sound->status ); + } +#endif +} + +static void initSettingFile() +{ + char path[STR_PATH_SIZE]; + char val[STR_SIZE]; + + sprintf(path, "%s/tuxanci-ng.conf", getHomeDirector()); + + configFile = loadTextFile(path); + + if( configFile == NULL ) + { + fprintf(stderr, "Don't load %s\n", path); + fprintf(stderr, "I create %s\n", path); + + configFile = newTextFile(path); + } + + if( configFile == NULL ) + { + fprintf(stderr, "Don't create %s\n", path); + return; + } + + loadValueFromConfigFile(configFile, "COUNT_ROUND", val, STR_SIZE, "15"); + strcpy(textfield_count_cound->text, val); + loadValueFromConfigFile(configFile, "NAME_PLAYER_RIGHT", val, STR_SIZE, "name1"); + strcpy(textfield_name_player1->text, val); + loadValueFromConfigFile(configFile, "NAME_PLAYER_LEFT", val, STR_SIZE, "name2"); + strcpy(textfield_name_player2->text, val); + + loadValueFromConfigFile(configFile, "GUN_DUAL_SIMPLE", val, STR_SIZE, "YES"); + check[GUN_DUAL_SIMPLE]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "GUN_SCATTER", val, STR_SIZE, "YES"); + check[GUN_SCATTER]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "GUN_TOMMY", val, STR_SIZE, "YES"); + check[GUN_TOMMY]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "GUN_LASSER", val, STR_SIZE, "YES"); + check[GUN_LASSER]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "GUN_MINE", val, STR_SIZE, "YES"); + check[GUN_MINE]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "GUN_BOMBBALL", val, STR_SIZE, "YES"); + check[GUN_BOMBBALL]->status = isYesOrNO(val); + + loadValueFromConfigFile(configFile, "BONUS_SPEED", val, STR_SIZE, "YES"); + check[BONUS_SPEED]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "BONUS_SHOT", val, STR_SIZE, "YES"); + check[BONUS_SHOT]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "BONUS_TELEPORT", val, STR_SIZE, "YES"); + check[BONUS_TELEPORT]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "BONUS_GHOST", val, STR_SIZE, "YES"); + check[BONUS_GHOST]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "BONUS_4X", val, STR_SIZE, "YES"); + check[BONUS_4X]->status = isYesOrNO(val); + loadValueFromConfigFile(configFile, "BONUS_HIDDEN", val, STR_SIZE, "YES"); + check[BONUS_HIDDEN]->status = isYesOrNO(val); + +#ifndef NO_SOUND + loadValueFromConfigFile(configFile, "MUSIC", val, STR_SIZE, "YES"); + check_music->status = isYesOrNO(val); + + loadValueFromConfigFile(configFile, "SOUND", val, STR_SIZE, "YES"); + check_sound->status = isYesOrNO(val); +#endif + + saveTextFile(configFile); +} + +static void saveAndDestroyConfigFile() +{ + if( configFile == NULL ) + { + fprintf(stderr, "i can't save configure, " + "because config file not created !\n"); + + return; + } + + setValueInConfigFile(configFile, "COUNT_ROUND", textfield_count_cound->text ); + setValueInConfigFile(configFile, "NAME_PLAYER_RIGHT", textfield_name_player1->text ); + setValueInConfigFile(configFile, "NAME_PLAYER_LEFT", textfield_name_player2->text ); + + setValueInConfigFile(configFile, "GUN_DUAL_SIMPLE", getYesOrNo(check[GUN_DUAL_SIMPLE]->status) ); + setValueInConfigFile(configFile, "GUN_SCATTER", getYesOrNo(check[GUN_SCATTER]->status) ); + setValueInConfigFile(configFile, "GUN_TOMMY", getYesOrNo(check[GUN_TOMMY]->status) ); + setValueInConfigFile(configFile, "GUN_LASSER", getYesOrNo(check[GUN_LASSER]->status) ); + setValueInConfigFile(configFile, "GUN_MINE", getYesOrNo(check[GUN_MINE]->status) ); + setValueInConfigFile(configFile, "GUN_BOMBBALL", getYesOrNo(check[GUN_BOMBBALL]->status) ); + setValueInConfigFile(configFile, "BONUS_SPEED", getYesOrNo(check[BONUS_SPEED]->status) ); + setValueInConfigFile(configFile, "BONUS_SHOT", getYesOrNo(check[BONUS_SHOT]->status) ); + setValueInConfigFile(configFile, "BONUS_TELEPORT", getYesOrNo(check[BONUS_TELEPORT]->status) ); + setValueInConfigFile(configFile, "BONUS_GHOST", getYesOrNo(check[BONUS_GHOST]->status) ); + setValueInConfigFile(configFile, "BONUS_4X", getYesOrNo(check[BONUS_4X]->status) ); + setValueInConfigFile(configFile, "BONUS_HIDDEN", getYesOrNo(check[BONUS_HIDDEN]->status) ); + +#ifndef NO_SOUND + setValueInConfigFile(configFile, "MUSIC", getYesOrNo(check_music->status) ); + setValueInConfigFile(configFile, "SOUND", getYesOrNo(check_sound->status) ); +#endif + + saveTextFile(configFile); + destroyTextFile(configFile); +} + +static void findModulesAI() +{ + director_t *listModules; + int i; + + listModules = loadDirector(PATH_MODULES); + + for( i = 0 ; i < listModules->list->count ; i++) + { + char *line; + + line = (char *)listModules->list->list[i]; + + if( strstr(line,"AI") != NULL && + strstr(line,".so") != NULL ) + { + addToWidgetSelect(select_ai, line); + } + } + + destroyDirector(listModules); +} + +void initScreenSetting() +{ + SDL_Surface *image; + int i; + + image = getImage(IMAGE_GROUP_BASE, "screen_main"); + image_backgorund = newWidgetImage(0, 0, image); + + button_back = newWidgetButton(getMyText("BACK"), WINDOW_SIZE_X-200, WINDOW_SIZE_Y-100, eventWidget); + + label_count_round = newWidgetLabel(getMyText("COUNT_ROUND"), 100, WINDOW_SIZE_Y-200, WIDGET_LABEL_LEFT); + label_name_player1 = newWidgetLabel(getMyText("NAME_PLAYER1"), 100, WINDOW_SIZE_Y-160, WIDGET_LABEL_LEFT); + label_name_player2 = newWidgetLabel(getMyText("NAME_PLAYER2"), 100, WINDOW_SIZE_Y-120, WIDGET_LABEL_LEFT); + label_ai = newWidgetLabel("AI :", 430, 400, WIDGET_LABEL_LEFT); + + textfield_count_cound = newWidgetTextfield(getParamElse("--count", "15"), 110+label_count_round->w, WINDOW_SIZE_Y-200); + + textfield_name_player1 = newWidgetTextfield(getParamElse("--name1", "name1"), 110+label_name_player1->w, WINDOW_SIZE_Y-160); + textfield_name_player2 = newWidgetTextfield(getParamElse("--name2", "name2"), 110+label_name_player2->w, WINDOW_SIZE_Y-120); + +#ifndef NO_SOUND + label_music = newWidgetLabel(getMyText("MUSIC"), 100, WINDOW_SIZE_Y-85, WIDGET_LABEL_LEFT); + check_music = newWidgetCheck(label_music->x + label_music->w + 10, + WINDOW_SIZE_Y-80, isMusicActive() , eventWidget); + label_sound = newWidgetLabel(getMyText("SOUND"), check_music->x + WIDGET_CHECK_WIDTH + 10, + WINDOW_SIZE_Y-85, WIDGET_LABEL_LEFT); + check_sound = newWidgetCheck(label_sound->x + label_sound->w + 10, + WINDOW_SIZE_Y-80, isSoundActive() , eventWidget); +#endif + + for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) + { + int x = 0; + int y = 0; + + if( i >= GUN_DUAL_SIMPLE && i <= GUN_TOMMY ) + { + x = 100; + y = 200 + ( i - GUN_DUAL_SIMPLE ) * 50; + } + + if( i >= GUN_LASSER && i <= GUN_BOMBBALL ) + { + x = 250; + y = 200 + ( i - GUN_LASSER ) * 50; + } + + check[i] = newWidgetCheck(x, y, TRUE, NULL); + } + + for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) + { + int x = 0; + int y = 0; + + if( i >= BONUS_SPEED && i <= BONUS_TELEPORT ) + { + x = 430; + y = 200 + ( i - BONUS_SPEED ) * 50; + } + + if( i >= BONUS_GHOST && i <= BONUS_HIDDEN ) + { + x = 580; + y = 200 + ( i - BONUS_GHOST ) * 50; + } + + check[i] = newWidgetCheck(x, y, TRUE, NULL); + } + + image_gun_dual_revolver = newWidgetImage(110 + WIDGET_CHECK_WIDTH, 200, getImage(IMAGE_GROUP_BASE, "panel_dual")); + image_gun_scatter = newWidgetImage(110 + WIDGET_CHECK_WIDTH, 250, getImage(IMAGE_GROUP_BASE, "panel_scatter")); + image_gun_tommy = newWidgetImage(110 + WIDGET_CHECK_WIDTH, 300, getImage(IMAGE_GROUP_BASE, "panel_tommy")); + image_gun_lasser = newWidgetImage(260 + WIDGET_CHECK_WIDTH, 200, getImage(IMAGE_GROUP_BASE, "panel_lasser")); + image_gun_mine = newWidgetImage(260 + WIDGET_CHECK_WIDTH, 250, getImage(IMAGE_GROUP_BASE, "panel_mine")); + image_gun_bombball = newWidgetImage(260 + WIDGET_CHECK_WIDTH, 300, getImage(IMAGE_GROUP_BASE, "panel_bombball")); + + image_bonus_speed = newWidgetImage(430 + WIDGET_CHECK_WIDTH, 200, getImage(IMAGE_GROUP_BASE, "panel_speed"));; + image_bonus_shot = newWidgetImage(430 + WIDGET_CHECK_WIDTH, 250, getImage(IMAGE_GROUP_BASE, "panel_shot"));; + image_bonus_teleport = newWidgetImage(430 + WIDGET_CHECK_WIDTH, 300, getImage(IMAGE_GROUP_BASE, "panel_teleport"));; + image_bonus_ghost = newWidgetImage(580 + WIDGET_CHECK_WIDTH, 200, getImage(IMAGE_GROUP_BASE, "panel_ghost"));; + image_bonus_4x = newWidgetImage(580 + WIDGET_CHECK_WIDTH, 250, getImage(IMAGE_GROUP_BASE, "panel_4x"));; + image_bonus_hidden = newWidgetImage(580 + WIDGET_CHECK_WIDTH, 300, getImage(IMAGE_GROUP_BASE, "panel_hidden"));; + + registerScreen( newScreen("setting", startScreenSetting, eventScreenSetting, + drawScreenSetting, stopScreenSetting) ); + + initSettingFile(); + + select_ai = newWidgetSelect(label_ai->x, label_ai->y+40, eventWidget); + addToWidgetSelect(select_ai, "none"); + findModulesAI(); + +#ifndef NO_SOUND + eventWidget(check_music); + eventWidget(check_sound); +#endif +} + +void getSettingNameRight(char *s) +{ + strcpy(s, textfield_name_player1->text); +} + +void getSettingNameLeft(char *s) +{ + strcpy(s, textfield_name_player2->text); +} + +void getSettingCountRound(int *n) +{ + *n = atoi( textfield_count_cound->text ); +} + +char* getSettingAI() +{ + return (char *) select_ai->list->list[ select_ai->select ]; +} + +bool_t isSettingAnyItem() +{ + int i; + + for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) + { + if( check[i]->status == TRUE ) + { + return TRUE; + } + } + + for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) + { + if( check[i]->status == TRUE ) + { + return TRUE; + } + } + + return FALSE; +} + +bool_t isSettingItem(int item) +{ + return check[item]->status; +} + +void quitScreenSetting() +{ + int i; + + saveAndDestroyConfigFile(); + + destroyWidgetImage(image_backgorund); + + destroyWidgetLabel(label_count_round); + destroyWidgetLabel(label_name_player1); + destroyWidgetLabel(label_name_player2); + destroyWidgetLabel(label_ai); + +#ifndef NO_SOUND + destroyWidgetLabel(label_music); + destroyWidgetLabel(label_sound); +#endif + + destroyWidgetTextfield(textfield_count_cound); + destroyWidgetTextfield(textfield_name_player1); + destroyWidgetTextfield(textfield_name_player2); + + destroyWidgetImage(image_gun_dual_revolver); + destroyWidgetImage(image_gun_scatter); + destroyWidgetImage(image_gun_tommy); + destroyWidgetImage(image_gun_lasser); + destroyWidgetImage(image_gun_mine); + destroyWidgetImage(image_gun_bombball); + + destroyWidgetImage(image_bonus_speed); + destroyWidgetImage(image_bonus_shot); + destroyWidgetImage(image_bonus_teleport); + destroyWidgetImage(image_bonus_ghost); + destroyWidgetImage(image_bonus_4x); + destroyWidgetImage(image_bonus_hidden); + +#ifndef NO_SOUND + destroyWidgetCheck(check_music); + destroyWidgetCheck(check_sound); +#endif + + for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) + { + destroyWidgetCheck(check[i]); + } + + for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) + { + destroyWidgetCheck(check[i]); + } + + destroyWidgetSelect(select_ai); + + destroyWidgetButton(button_back); +} + diff --git a/src/screen/screen_setting.h b/src/screen/screen_setting.h new file mode 100644 index 0000000..5b0bae3 --- /dev/null +++ b/src/screen/screen_setting.h @@ -0,0 +1,20 @@ + +#ifndef SCREEN_SETTING + +#define SCREEN_SETTING + +#include "base/main.h" + +extern void initScreenSetting(); +extern void drawScreenSetting(); +extern void eventScreenSetting(); +extern void getSettingNameRight(char *s); +extern void getSettingNameLeft(char *s); +extern void getSettingCountRound(int *n); +extern char* getSettingAI(); +extern bool_t isSettingAnyItem(); +extern bool_t isSettingItem(int item); +extern void quitScreenSetting(); + +#endif + diff --git a/src/screen/screen_table.c b/src/screen/screen_table.c new file mode 100644 index 0000000..df1b7d5 --- /dev/null +++ b/src/screen/screen_table.c @@ -0,0 +1,240 @@ + +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/textFile.h" +#include "base/homeDirector.h" + +#include "client/language.h" +#include "client/interface.h" +#include "client/screen.h" +#include "client/image.h" + +#ifndef NO_SOUND +#include "audio/music.h" +#endif + +#include "screen/screen_table.h" + +#include "widget/widget_image.h" +#include "widget/widget_label.h" +#include "widget/widget_button.h" + +static widget_image_t *image_backgorund; +static widget_button_t *button_back; + +static list_t *listWidgetLabelNumer; +static list_t *listWidgetLabelName; +static list_t *listWidgetLabelScore; + +static textFile_t *textFile; + +void startScreenTable() +{ +#ifndef NO_SOUND + playMusic("menu", MUSIC_GROUP_BASE); +#endif +} + +void drawScreenTable() +{ + int i; + + drawWidgetImage(image_backgorund); + + drawWidgetButton(button_back); + + for( i = 0 ; i < listWidgetLabelNumer->count ; i++ ) + { + widget_label_t *this; + this = (widget_label_t *)listWidgetLabelNumer->list[i]; + drawWidgetLabel(this); + } + + for( i = 0 ; i < listWidgetLabelName->count ; i++ ) + { + widget_label_t *this; + this = (widget_label_t *)listWidgetLabelName->list[i]; + drawWidgetLabel(this); + } + + for( i = 0 ; i < listWidgetLabelScore->count ; i++ ) + { + widget_label_t *this; + this = (widget_label_t *)listWidgetLabelScore->list[i]; + drawWidgetLabel(this); + } +} + +void eventScreenTable() +{ + eventWidgetButton(button_back); +} + +void stopScreenTable() +{ +} + +static void eventWidget(void *p) +{ + widget_button_t *button; + + button = (widget_button_t *)(p); + + if( button == button_back ) + { + setScreen("mainMenu"); + } +} + +static void setWidgetLabel() +{ + int i; + + if( textFile == NULL ) + { + fprintf(stderr, "File %s not loaded !", + SCREEN_TABLE_FILE_HIGHSCORE_NAME); + + return; + } + + if( listWidgetLabelNumer != NULL || + listWidgetLabelName != NULL || + listWidgetLabelScore != NULL ) + { + destroyListItem(listWidgetLabelNumer, destroyWidgetLabel); + destroyListItem(listWidgetLabelName, destroyWidgetLabel); + destroyListItem(listWidgetLabelScore, destroyWidgetLabel); + } + + listWidgetLabelNumer = newList(); + listWidgetLabelName = newList(); + listWidgetLabelScore = newList(); + + for( i = 0 ; i < SCREEN_TABLE_MAX_PLAYERS ; i++ ) + { + widget_label_t *label; + char name[STR_NAME_SIZE]; + char score[STR_NUM_SIZE]; + char num[STR_NUM_SIZE]; + char *line; + + line = (char *) textFile->text->list[i]; + + sscanf(line, "%s %s", name, score); + sprintf(num, "%2d)", i+1); + + label = newWidgetLabel(num, WINDOW_SIZE_X/2 - 100, 200 + i * 20, WIDGET_LABEL_LEFT); + addList(listWidgetLabelNumer, label); + + label = newWidgetLabel(name, WINDOW_SIZE_X/2, 200 + i * 20, WIDGET_LABEL_CENTER); + addList(listWidgetLabelName, label); + + label = newWidgetLabel(score, WINDOW_SIZE_X/2 + 80, 200 + i * 20, WIDGET_LABEL_LEFT); + addList(listWidgetLabelScore, label); + } +} + +static void loadHighscoreFile() +{ + char path[STR_PATH_SIZE]; + int i; + + sprintf(path, "%s/" SCREEN_TABLE_FILE_HIGHSCORE_NAME , getHomeDirector()); + textFile = loadTextFile(path); + + if( textFile == NULL ) + { + fprintf(stderr, "I can't load %s !\n", path); + fprintf(stderr, "I create %s\n", path); + textFile = newTextFile(path); + } + else + { + return; + } + + if( textFile == NULL ) + { + fprintf(stderr, "I can't create %s !\n", path); + return; + } + + for( i = 0 ; i < SCREEN_TABLE_MAX_PLAYERS ; i++ ) + { + addList(textFile->text, strdup("no_name 0") ); + } + + saveTextFile(textFile); +} + +int addPlayerInHighScore(char *name, int score) +{ + int i; + + for( i = 0 ; i < SCREEN_TABLE_MAX_PLAYERS ; i++ ) + { + char *line; + char thisName[STR_NAME_SIZE]; + int thisCore; + + line = (char *) textFile->text->list[i]; + sscanf(line, "%s %d", thisName, &thisCore); + + if( score >= thisCore ) + { + char new[STR_SIZE]; + + sprintf(new, "%s %d", name, score); + insList(textFile->text, i, strdup(new) ); + delListItem(textFile->text, SCREEN_TABLE_MAX_PLAYERS, free); + setWidgetLabel(); + + return 0; + } + } + + return -1; +} + +void initScreenTable() +{ + SDL_Surface *image; + + + image = addImageData("screen_table.png", IMAGE_NO_ALPHA, "screen_table", IMAGE_GROUP_BASE); + image_backgorund = newWidgetImage(0, 0, image); + + button_back = newWidgetButton(getMyText("BACK"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, + WINDOW_SIZE_Y-100, eventWidget); + + loadHighscoreFile(); + listWidgetLabelNumer = NULL; + listWidgetLabelName = NULL; + listWidgetLabelScore = NULL; + setWidgetLabel(); + + registerScreen( newScreen("table", startScreenTable, eventScreenTable, + drawScreenTable, stopScreenTable) ); +} + +void quitScreenTable() +{ + destroyWidgetImage(image_backgorund); + + destroyWidgetButton(button_back); + destroyListItem(listWidgetLabelNumer, destroyWidgetLabel); + destroyListItem(listWidgetLabelName, destroyWidgetLabel); + destroyListItem(listWidgetLabelScore, destroyWidgetLabel); + + if( textFile != NULL ) + { + saveTextFile(textFile); + destroyTextFile(textFile); + } +} + diff --git a/src/screen/screen_table.h b/src/screen/screen_table.h new file mode 100644 index 0000000..d16cbbb --- /dev/null +++ b/src/screen/screen_table.h @@ -0,0 +1,17 @@ + +#ifndef SCREEN_TABLE_H + +#define SCREEN_TABLE_H + +#define SCREEN_TABLE_MAX_PLAYERS 10 +#define SCREEN_TABLE_FILE_HIGHSCORE_NAME "highscore" + +extern void startScreenTable(); +extern void drawScreenTable(); +extern void eventScreenTable(); +extern void stopScreenTable(); +extern int addPlayerInHighScore(char *name, int score); +extern void initScreenTable(); +extern void quitScreenTable(); + +#endif diff --git a/src/screen/screen_world.c b/src/screen/screen_world.c new file mode 100644 index 0000000..296c31f --- /dev/null +++ b/src/screen/screen_world.c @@ -0,0 +1,603 @@ + +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/item.h" +#include "base/shot.h" +#include "base/gun.h" +#include "base/myTimer.h" +#include "base/net_multiplayer.h" +#include "base/arena.h" +#include "base/arenaFile.h" +#include "base/proto.h" +#include "base/modules.h" +#include "base/idManager.h" + +#include "client/interface.h" +#include "client/keytable.h" +#include "client/layer.h" +#include "client/image.h" +//#include "client/heightScore.h" +#include "client/term.h" +#include "client/font.h" +#include "client/panel.h" + +#ifndef NO_SOUND +#include "audio/music.h" +#include "audio/sound.h" +#endif + +#include "client/screen.h" +#include "screen/screen_world.h" +#include "screen/screen_analyze.h" +#include "screen/screen_setting.h" +#include "screen/screen_gameType.h" +#include "screen/screen_choiceArena.h" +#include "screen/screen_table.h" + + +static arena_t *arena; +static int count; +static int max_count; +static my_time_t lastServerLag; +static bool_t isScreenWorldInit = FALSE; +static bool_t isEndWorld; + +static tux_t *tuxWithControlRightKeyboard; +static tux_t *tuxWithControlLeftKeyboard; + +bool_t isScreenWorldInicialized() +{ + return isScreenWorldInit; +} + +void setGameType() +{ + int ret = 0; + + ret = initNetMuliplayer( getSettingGameType(), getSettingIP(), getSettingPort(), getSettingProto() ); + + if( ret != 0 ) + { + fprintf(stderr, "Chyba inicalizacie sieti !\n"); + setWorldEnd(); + } +} + +void setWorldArena(int id) +{ + arena = getArena(id); + +#ifndef NO_SOUND + playMusic(arena->music, MUSIC_GROUP_USER); +#endif +} + +void setMaxCountRound(int n) +{ + max_count = n; +} + +void setLagServer(my_time_t lag) +{ + lastServerLag = lag; +} + +void setWorldEnd() +{ + isEndWorld = TRUE; +} + +static void timer_endArena() +{ + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + setWorldEnd(); + return; + } +} + +void countRoundInc() +{ + if( count == WORLD_COUNT_ROUND_UNLIMITED || + count >= max_count ) + { + return; + } + + count++; + //printf("count %d/%d\n", count, max_count); + + if( count >= max_count ) + { + printf("count %d ending\n", count); +#ifndef NO_SOUND + playSound("end", SOUND_GROUP_BASE); +#endif + addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_endArena, NULL, TIMER_END_ARENA); + } +} + +void prepareArena() +{ + tux_t *tux; + + //printf("getSettingAI = %s\n", getSettingAI()); + + switch( getNetTypeGame() ) + { + case NET_GAME_TYPE_NONE : + setWorldArena( getChoiceArenaId() ); + addNewItem(arena->spaceItem, ID_UNKNOWN); + getSettingCountRound(&max_count); + + tux = newTux(); + tux->control = TUX_CONTROL_KEYBOARD_RIGHT; + tuxWithControlRightKeyboard = tux; + getSettingNameRight(tux->name); + addObjectToSpace(arena->spaceTux, tux); + + tux = newTux(); + tux->control = TUX_CONTROL_KEYBOARD_LEFT; + tuxWithControlLeftKeyboard = tux; + getSettingNameLeft(tux->name); + addObjectToSpace(arena->spaceTux, tux); + + if( strcmp(getSettingAI(), "none") != 0 ) + { + loadModule( getSettingAI() ); + tux->control = TUX_CONTROL_AI; + } + break; + + case NET_GAME_TYPE_SERVER : + setWorldArena( getChoiceArenaId() ); + addNewItem(arena->spaceItem, ID_UNKNOWN); + getSettingCountRound(&max_count); + + tux = newTux(); + tux->control = TUX_CONTROL_KEYBOARD_RIGHT; + tuxWithControlRightKeyboard = tux; + getSettingNameRight(tux->name); + addObjectToSpace(arena->spaceTux, tux); + break; + + case NET_GAME_TYPE_CLIENT : + break; + } +} + +/* +static void drawServerLag() +{ + char msg[STR_SIZE]; + + if( lastServerLag == LAG_SERVER_UNKNOWN ) + { + sprintf(msg, "lag server: unknown"); + } + else + { + sprintf(msg, "lag server: %d", lastServerLag); + } + + drawFont(msg, WINDOW_SIZE_X-150, 20, COLOR_WHITE); + +} +*/ + +void drawWorld() +{ + if( arena != NULL ) + { + drawArena(arena); + drawPanel(arena->spaceTux->list); + } + + drawTerm(); +} + +static void netAction(tux_t *tux, int action) +{ + if( getSettingGameType() == NET_GAME_TYPE_SERVER ) + { + proto_send_event_server(PROTO_SEND_ALL_SEES_TUX, NULL, tux , action); + } + + if( getSettingGameType() == NET_GAME_TYPE_CLIENT ) + { + proto_send_event_client(action); + } +} + +static void control_keyboard_right(tux_t *tux) +{ + static int lastKey = 0; + int countKey; + Uint8 *mapa; + mapa = SDL_GetKeyState(NULL); + + assert( tux != NULL ); + assert( mapa != NULL ); + + countKey = 0; + + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_UP)] == SDL_PRESSED )countKey++; + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_RIGHT)] == SDL_PRESSED )countKey++; + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_LEFT)] == SDL_PRESSED )countKey++; + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_DOWN)] == SDL_PRESSED )countKey++; + + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_UP)] == SDL_PRESSED ) + { + if(countKey > 1 && lastKey == getKey(KEY_TUX_RIGHT_MOVE_UP) )goto tuUp; + + if( countKey == 1 )lastKey = getKey(KEY_TUX_RIGHT_MOVE_UP); + netAction(tux, TUX_UP); + actionTux(tux, TUX_UP); + return; + + tuUp:; + } + + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_RIGHT)] == SDL_PRESSED ) + { + if(countKey > 1 && lastKey == getKey(KEY_TUX_RIGHT_MOVE_RIGHT) )goto tuRight; + + if( countKey == 1 )lastKey = getKey(KEY_TUX_RIGHT_MOVE_RIGHT); + netAction(tux, TUX_RIGHT); + actionTux(tux, TUX_RIGHT); + return; + + tuRight:; + + } + + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_LEFT)] == SDL_PRESSED ) + { + if(countKey > 1 && lastKey == getKey(KEY_TUX_RIGHT_MOVE_LEFT) )goto tuLeft; + + if( countKey == 1 )lastKey = getKey(KEY_TUX_RIGHT_MOVE_LEFT); + netAction(tux, TUX_LEFT); + actionTux(tux, TUX_LEFT); + return; + + tuLeft:; + } + + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_DOWN)] == SDL_PRESSED ) + { + if(countKey > 1 && lastKey == getKey(KEY_TUX_RIGHT_MOVE_DOWN) )goto tuDown; + + if( countKey == 1 )lastKey = getKey(KEY_TUX_RIGHT_MOVE_DOWN); + netAction(tux, TUX_DOWN); + actionTux(tux, TUX_DOWN); + return; + + tuDown:; + } + + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_SHOOT)] == SDL_PRESSED && tux->isCanShot == TRUE ) + { + netAction(tux, TUX_SHOT); + actionTux(tux, TUX_SHOT); + return; + } + + if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_SWITCH_WEAPON)] == SDL_PRESSED ) + { + if( tux->isCanSwitchGun == TRUE ) + { + netAction(tux, TUX_SWITCH_GUN); + actionTux(tux, TUX_SWITCH_GUN); + return; + } + } +} + +static void control_keyboard_left(tux_t *tux) +{ + static int lastKey = 0; + int countKey; + Uint8 *mapa; + mapa = SDL_GetKeyState(NULL); + + assert( tux != NULL ); + assert( mapa != NULL ); + + countKey = 0; + + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_UP)] == SDL_PRESSED )countKey++; + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_RIGHT)] == SDL_PRESSED )countKey++; + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_LEFT)] == SDL_PRESSED )countKey++; + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_DOWN)] == SDL_PRESSED )countKey++; + + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_UP)] == SDL_PRESSED ) + { + if(countKey > 1 && lastKey == getKey(KEY_TUX_LEFT_MOVE_UP) )goto tuUp; + + if( countKey == 1 )lastKey = getKey(KEY_TUX_LEFT_MOVE_UP); + actionTux(tux, TUX_UP); + return; + + tuUp:; + } + + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_RIGHT)] == SDL_PRESSED ) + { + if(countKey > 1 && lastKey == getKey(KEY_TUX_LEFT_MOVE_RIGHT) )goto tuRight; + + if( countKey == 1 )lastKey = getKey(KEY_TUX_LEFT_MOVE_RIGHT); + actionTux(tux, TUX_RIGHT); + return; + + tuRight:; + + } + + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_LEFT)] == SDL_PRESSED ) + { + if(countKey > 1 && lastKey == getKey(KEY_TUX_LEFT_MOVE_LEFT) )goto tuLeft; + + if( countKey == 1 )lastKey = getKey(KEY_TUX_LEFT_MOVE_LEFT); + actionTux(tux, TUX_LEFT); + return; + + tuLeft:; + } + + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_DOWN)] == SDL_PRESSED ) + { + if(countKey > 1 && lastKey == getKey(KEY_TUX_LEFT_MOVE_DOWN) )goto tuDown; + + if( countKey == 1 )lastKey = getKey(KEY_TUX_LEFT_MOVE_DOWN); + actionTux(tux, TUX_DOWN); + return; + + tuDown:; + } + + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_SHOOT)] == SDL_PRESSED && tux->isCanShot == TRUE ) + { + actionTux(tux, TUX_SHOT); + return; + } + + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_SWITCH_WEAPON)] == SDL_PRESSED ) + { + if( tux->isCanSwitchGun == TRUE ) + { + actionTux(tux, TUX_SWITCH_GUN); + return; + } + } +/* + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_UP)] == SDL_PRESSED )actionTux(tux, TUX_UP); + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_RIGHT)] == SDL_PRESSED )actionTux(tux, TUX_RIGHT); + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_LEFT)] == SDL_PRESSED )actionTux(tux, TUX_LEFT); + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_DOWN)] == SDL_PRESSED )actionTux(tux, TUX_DOWN); + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_SHOOT)] == SDL_PRESSED )actionTux(tux, TUX_SHOT); + + if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_SWITCH_WEAPON)] == SDL_PRESSED ) + { + if( tux->isCanSwitchGun == TRUE ) + { + actionTux(tux, TUX_SWITCH_GUN); + } + } +*/ +} + +static void eventEsc() +{ + Uint8 *mapa; + mapa = SDL_GetKeyState(NULL); + + if( mapa[(SDLKey)SDLK_ESCAPE] == SDL_PRESSED ) + { + isEndWorld = TRUE; + setWorldEnd(); + return; + } +} + +static void eventEnd() +{ + if( isEndWorld == TRUE ) + { + setScreen("analyze"); + return; + } +} + +tux_t* getControlTux(int control_type) +{ + switch(control_type) + { + case TUX_CONTROL_KEYBOARD_RIGHT : + return tuxWithControlRightKeyboard; + break; + case TUX_CONTROL_KEYBOARD_LEFT : + return tuxWithControlLeftKeyboard; + break; + } + + return NULL; +} + +void setControlTux(tux_t *tux, int control_type) +{ + switch(control_type) + { + case TUX_CONTROL_KEYBOARD_RIGHT : + tuxWithControlRightKeyboard = tux; + break; + case TUX_CONTROL_KEYBOARD_LEFT : + tuxWithControlLeftKeyboard = tux; + break; + } +} + +void tuxControl(tux_t *p) +{ + assert( p != NULL ); + + if( p->status != TUX_STATUS_ALIVE ) + { + return; + } + + switch( p->control ) + { + case TUX_CONTROL_NONE : + assert( ! "Tux nema definovane ovladanie !" ); + break; + + case TUX_CONTROL_KEYBOARD_LEFT : + control_keyboard_left(p); + break; + + case TUX_CONTROL_KEYBOARD_RIGHT : + control_keyboard_right(p); + break; + } +} + +void eventWorld() +{ + if( arena == NULL ) + { + eventNetMultiplayer(); + eventEnd(); + eventEsc(); + return; + } + + eventNetMultiplayer(); + + eventArena(arena); + //eventModule(); + + eventTerm(); + eventEnd(); + eventEsc(); +} + +void startWorld() +{ + arena = NULL; + isEndWorld = FALSE; + + count = 0; + lastServerLag = LAG_SERVER_UNKNOWN; + + initListID(); + initModule(); + setGameType(); + initTerm(); + prepareArena(); +} + +static void setAnalyze() +{ + int i; + tux_t *tux; + + restartAnalyze(); + + for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) + { + tux = (tux_t *)(arena->spaceTux->list->list[i]); + addAnalyze(tux->name, tux->score); + } + + endAnalyze(); +} + +static void setTable() +{ + tux_t *tuxRight; + tux_t *tuxLeft; + + if( getSettingGameType() != NET_GAME_TYPE_NONE ) + { + return; + } + + tuxRight = getControlTux(TUX_CONTROL_KEYBOARD_RIGHT); + tuxLeft = getControlTux(TUX_CONTROL_KEYBOARD_LEFT); + + if( tuxRight->score > tuxLeft->score ) + { + addPlayerInHighScore(tuxRight->name, tuxRight->score); + } + + if( tuxLeft->score > tuxRight->score ) + { + addPlayerInHighScore(tuxLeft->name, tuxLeft->score); + } +} + +void stoptWorld() +{ + if( arena != NULL ) + { + setTable(); + setAnalyze(); + } + + quitNetMultiplayer(); + + if( arena != NULL ) + { + destroyArena(arena); + } + +#ifndef NO_SOUND + stopMusic(); +#endif + delAllImageInGroup(IMAGE_GROUP_USER); + +#ifndef NO_SOUND + delAllMusicInGroup(MUSIC_GROUP_USER); +#endif + quitModule(); + quitTerm(); + quitListID(); +} + +void initWorld() +{ + assert( isImageInicialized() == TRUE ); + assert( isTuxInicialized() == TRUE ); + assert( isShotInicialized() == TRUE ); + assert( isPanelInicialized() == TRUE ); + assert( isScreenInicialized() == TRUE ); + + registerScreen( newScreen("world", startWorld, eventWorld, drawWorld, stoptWorld) ); + +#ifndef NO_SOUND + addSound("dead.ogg", "dead", SOUND_GROUP_BASE); + addSound("explozion.ogg", "explozion", SOUND_GROUP_BASE); + addSound("gun_lasser.ogg", "gun_lasser", SOUND_GROUP_BASE); + addSound("gun_revolver.ogg", "gun_revolver", SOUND_GROUP_BASE); + addSound("gun_scatter.ogg", "gun_scatter", SOUND_GROUP_BASE); + addSound("gun_tommy.ogg", "gun_tommy", SOUND_GROUP_BASE); + addSound("put_mine.ogg", "put_mine", SOUND_GROUP_BASE); + addSound("teleport.ogg", "teleport", SOUND_GROUP_BASE); + addSound("item_bonus.ogg", "item_bonus", SOUND_GROUP_BASE); + addSound("item_gun.ogg", "item_gun", SOUND_GROUP_BASE); + addSound("switch_gun.ogg", "switch_gun", SOUND_GROUP_BASE); + addSound("end.ogg", "end", SOUND_GROUP_BASE); +#endif + + isScreenWorldInit = TRUE; +} + +void quitWorld() +{ + printf("quit world\n"); + isScreenWorldInit = FALSE; +} diff --git a/src/screen/screen_world.h b/src/screen/screen_world.h new file mode 100644 index 0000000..270d144 --- /dev/null +++ b/src/screen/screen_world.h @@ -0,0 +1,31 @@ + +#ifndef SCREEN_WORLD_H + +#define SCREEN_WORLD_H + +#include "base/main.h" +#include "base/myTimer.h" +#include "base/tux.h" +#include "base/arena.h" + +#define TIMER_END_ARENA 5000 +#define WORLD_COUNT_ROUND_UNLIMITED -1 +#define LAG_SERVER_UNKNOWN -1 + +extern bool_t isScreenWorldInicialized(); +extern void initWorld(); +extern void setWorldArena(int id); +extern void setLagServer(my_time_t lag); +extern void countRoundInc(); +extern void setWorldEnd(); +extern void setMaxCountRound(int n); +extern tux_t* getControlTux(int control_type); +extern void setControlTux(tux_t *tux, int control_type); +extern void tuxControl(tux_t *p); +extern void drawWorld(); +extern void eventWorld(); +extern void startArena(); +extern void stopArena(); +extern void quitWorld(); + +#endif diff --git a/src/screen_analyze.c b/src/screen_analyze.c deleted file mode 100644 index de84679..0000000 --- a/src/screen_analyze.c +++ /dev/null @@ -1,173 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "music.h" -#include "fake_audio.h" -#include "screen_mainMenu.h" -#include "list.h" -#include "screen_analyze.h" - -#include "widget_label.h" -#include "widget_button.h" -#include "widget_image.h" - -static widget_image_t *image_backgorund; - -static list_t *listWidgetLabelName; -static list_t *listWidgetLabelScore; -static list_t *listAnalyze; -static widget_label_t *widgetLabelMsg; - -static widget_button_t *button_ok; - -static analyze_t* newAnalyze(char *name, int score) -{ - analyze_t *new; - - new = malloc( sizeof(analyze_t) ); - new->name = strdup(name); - new->score = score; - - return new; -} - -static void destroyAnalyze(analyze_t *p) -{ - free(p->name); - free(p); -} - -void startScreenAnalyze() -{ - playMusic("menu", MUSIC_GROUP_BASE); -} - -void drawScreenAnalyze() -{ - widget_label_t *this; - int i; - - drawWidgetImage(image_backgorund); - - for( i = 0 ; i < listWidgetLabelName->count ; i++ ) - { - this = (widget_label_t *)(listWidgetLabelName->list[i]); - drawWidgetLabel(this); - } - - for( i = 0 ; i < listWidgetLabelScore->count ; i++ ) - { - this = (widget_label_t *)(listWidgetLabelScore->list[i]); - drawWidgetLabel(this); - } - - drawWidgetLabel(widgetLabelMsg); - drawWidgetButton(button_ok); -} - -void eventScreenAnalyze() -{ - eventWidgetButton(button_ok); -} - -void stopScreenAnalyze() -{ - destroyWidgetLabel(widgetLabelMsg); - widgetLabelMsg = newWidgetLabel("", WINDOW_SIZE_X / 2 , 250, WIDGET_LABEL_CENTER); -} - -static void eventWidget(void *p) -{ - widget_button_t *button; - - button = (widget_button_t *)(p); - - if( button == button_ok ) - { - setScreen("mainMenu"); - } -} - -void restartAnalyze() -{ - destroyListItem(listWidgetLabelName, destroyWidgetLabel); - destroyListItem(listWidgetLabelScore, destroyWidgetLabel); - destroyListItem(listAnalyze, destroyAnalyze); - - listWidgetLabelName = newList(); - listWidgetLabelScore = newList(); - listAnalyze = newList(); -} - -void addAnalyze(char *name, int score) -{ - addList(listAnalyze, newAnalyze(name, score) ); -} - -void setMsgToAnalyze(char *msg) -{ - destroyWidgetLabel(widgetLabelMsg); - widgetLabelMsg = newWidgetLabel(msg, WINDOW_SIZE_X / 2 , 250, WIDGET_LABEL_CENTER); -} - -void endAnalyze() -{ - int i; - - for( i = 0 ; i < listAnalyze->count ; i++ ) - { - analyze_t *this; - char *str; - - this = (analyze_t *)(listAnalyze->list[i]); - - addList(listWidgetLabelName, newWidgetLabel(this->name, - 100, 200 + 20*i, WIDGET_LABEL_LEFT) ); - - str = getString(this->score); - - addList(listWidgetLabelScore, newWidgetLabel(str, - WINDOW_SIZE_X - 100, 200 + 20*i, WIDGET_LABEL_RIGHT) ); - - free(str); - } -} - -void initScreenAnalyze() -{ - SDL_Surface *image; - - image = getImage(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = newWidgetImage(0, 0, image); - - listWidgetLabelName = newList(); - listWidgetLabelScore = newList(); - listAnalyze = newList(); - - button_ok = newWidgetButton("OK", WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, - WINDOW_SIZE_Y-100, eventWidget); - - registerScreen( newScreen("analyze", startScreenAnalyze, eventScreenAnalyze, - drawScreenAnalyze, stopScreenAnalyze) ); - - widgetLabelMsg = newWidgetLabel("", WINDOW_SIZE_X / 2 , 250, WIDGET_LABEL_CENTER); -} - -void quitScreenAnalyze() -{ - destroyWidgetImage(image_backgorund); - - destroyListItem(listWidgetLabelName, destroyWidgetLabel); - destroyListItem(listWidgetLabelScore, destroyWidgetLabel); - destroyListItem(listAnalyze, destroyAnalyze); - - destroyWidgetButton(button_ok); - destroyWidgetLabel(widgetLabelMsg); -} - diff --git a/src/screen_choiceArena.c b/src/screen_choiceArena.c deleted file mode 100644 index 272ae10..0000000 --- a/src/screen_choiceArena.c +++ /dev/null @@ -1,167 +0,0 @@ - -#include -#include - -#include "main.h" -#include "language.h" -#include "interface.h" -#include "list.h" -#include "screen.h" -#include "image.h" -#include "music.h" -#include "fake_audio.h" - -#include "arenaFile.h" -#include "screen_mainMenu.h" -#include "screen_choiceArena.h" - -#include "widget_label.h" -#include "widget_button.h" -#include "widget_image.h" -#include "widget_buttonimage.h" - -static widget_image_t *image_backgorund; - -static widget_button_t *button_play; -static widget_button_t *button_back; - -static list_t *listWidgetButtonimage; -static int choiceArenaId; - -void startScreenChoiceArena() -{ - playMusic("menu", MUSIC_GROUP_BASE); -} - -void drawScreenChoiceArena() -{ - int i; - - drawWidgetImage(image_backgorund); - - drawWidgetButton(button_play); - drawWidgetButton(button_back); - - for( i = 0 ; i < listWidgetButtonimage->count ; i++ ) - { - widget_buttonimage_t *this; - this = (widget_buttonimage_t *) listWidgetButtonimage->list[i]; - drawWidgetButtonimage(this); - } -} - -void eventScreenChoiceArena() -{ - int i; - - eventWidgetButton(button_play); - eventWidgetButton(button_back); - - for( i = 0 ; i < listWidgetButtonimage->count ; i++ ) - { - widget_buttonimage_t *this; - this = (widget_buttonimage_t *) listWidgetButtonimage->list[i]; - eventWidgetButtonimage(this); - } -} - -void stopScreenChoiceArena() -{ -} - -static void eventWidgetButtonImage(void *p) -{ - widget_buttonimage_t *buttonimage; - int i; - - buttonimage = (widget_buttonimage_t *)(p); - - for( i = 0 ; i < listWidgetButtonimage->count ; i++ ) - { - widget_buttonimage_t *this; - - this = (widget_buttonimage_t *)(listWidgetButtonimage->list[i]); - - if( buttonimage == this ) - { - this->active = 1; - choiceArenaId = i; - } - else - { - this->active = 0; - } - } -} - -int getChoiceArenaId() -{ - return choiceArenaId; -} - -static void eventWidget(void *p) -{ - widget_button_t *button; - - button = (widget_button_t *)(p); - - if( button == button_play ) - { - setScreen("world"); - } - - if( button == button_back ) - { - setScreen("gameType"); - } -} - -void initScreenChoiceArena() -{ - SDL_Surface *image; - int i; - - image = getImage(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = newWidgetImage(0, 0, image); - - button_back = newWidgetButton(getMyText("BACK"), 100, WINDOW_SIZE_Y-100, eventWidget); - button_play = newWidgetButton(getMyText("PLAY"), WINDOW_SIZE_X-200, WINDOW_SIZE_Y-100, eventWidget); - - listWidgetButtonimage = newList(); - choiceArenaId = 0; - - for( i = 0 ; i < getArenaCount() ; i++ ) - { - SDL_Surface *image; - widget_buttonimage_t *widget_buttonimage; - int x, y; - - image = addImageData(getArenaImage(i), IMAGE_NO_ALPHA, "none", IMAGE_GROUP_BASE); - - x = 100 + 200 * ( i - 3 * ( i/3 ) ); - y = 150 + 200 * ( i/3 ); - - widget_buttonimage = newWidgetButtonimage(image, - x, y, eventWidgetButtonImage); - - if( i == choiceArenaId ) - { - widget_buttonimage->active = 1; - } - - addList( listWidgetButtonimage, widget_buttonimage); - } - - registerScreen( newScreen("chiceArena", startScreenChoiceArena, eventScreenChoiceArena, - drawScreenChoiceArena, stopScreenChoiceArena) ); -} - -void quitScreenChoiceArena() -{ - destroyWidgetImage(image_backgorund); - - destroyWidgetButton(button_play); - destroyWidgetButton(button_back); - - destroyListItem(listWidgetButtonimage, destroyWidgetButtonimage); -} diff --git a/src/screen_credits.c b/src/screen_credits.c deleted file mode 100644 index 3416fb5..0000000 --- a/src/screen_credits.c +++ /dev/null @@ -1,128 +0,0 @@ - -#include -#include - -#include "main.h" -#include "language.h" -#include "interface.h" -#include "list.h" -#include "textFile.h" -#include "screen.h" -#include "image.h" -#include "music.h" -#include "fake_audio.h" -#include "screen_credits.h" - -#include "widget_image.h" -#include "widget_label.h" -#include "widget_button.h" - -static widget_image_t *image_backgorund; -static widget_button_t *button_back; -static list_t *listWidgetLabel; -static textFile_t *textFile; -static int offset; - -void startScreenCredits() -{ - playMusic("menu", MUSIC_GROUP_BASE); - offset = 0; -} - -void drawScreenCredits() -{ - int i; - - drawWidgetImage(image_backgorund); - - drawWidgetButton(button_back); - - for( i = 0 ; i < listWidgetLabel->count ; i++ ) - { - int z; - - widget_label_t *this; - this = (widget_label_t *)listWidgetLabel->list[i]; - - z = this->y; - this->y += offset; - - if( this->y > SCREEN_CREDITS_OFFSET_MIN && this->y < SCREEN_CREDITS_OFFSET_MAX ) - { - drawWidgetLabel(this); - } - - this->y = z; - } - -} - -void eventScreenCredits() -{ - eventWidgetButton(button_back); - - offset -= SCREEN_CREDITS_OFFSET_SPEED; - - if( offset < SCREEN_CREDITS_OFFSET_RESTART ) - { - offset = 0; - } - - //printf("offset = %d\n", offset); -} - -void stopScreenCredits() -{ -} - -static void eventWidget(void *p) -{ - widget_button_t *button; - - button = (widget_button_t *)(p); - - if( button == button_back ) - { - setScreen("mainMenu"); - } -} - -void initScreenCredits() -{ - SDL_Surface *image; - int i; - - image = getImage(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = newWidgetImage(0, 0, image); - - button_back = newWidgetButton(getMyText("BACK"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, WINDOW_SIZE_Y-80, eventWidget); - - listWidgetLabel = newList(); - accessExistFile(PATH_DATA SCREEN_CREDITS_FILE); - textFile = loadTextFile(PATH_DATA SCREEN_CREDITS_FILE); - - for( i = 0 ; i < textFile->text->count ; i++ ) - { - widget_label_t *label; - char *line; - - line = (char *)textFile->text->list[i]; - label = newWidgetLabel(line, WINDOW_SIZE_X/2, (WINDOW_SIZE_Y-100)+i*20, - WIDGET_LABEL_CENTER); - - addList(listWidgetLabel, label); - } - - registerScreen( newScreen("credits", startScreenCredits, eventScreenCredits, - drawScreenCredits, stopScreenCredits) ); -} - -void quitScreenCredits() -{ - destroyWidgetImage(image_backgorund); - - destroyWidgetButton(button_back); - destroyListItem(listWidgetLabel, destroyWidgetLabel); - destroyTextFile(textFile); -} - diff --git a/src/screen_gameType.c b/src/screen_gameType.c deleted file mode 100644 index 38cdbf8..0000000 --- a/src/screen_gameType.c +++ /dev/null @@ -1,252 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "language.h" -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "music.h" -#include "fake_audio.h" - -#include "net_multiplayer.h" -#include "screen_gameType.h" -#include "list.h" -#include "screen_setting.h" -#include "tux.h" -#include "udp.h" - -#include "widget_label.h" -#include "widget_button.h" -#include "widget_image.h" -#include "widget_textfield.h" -#include "widget_check.h" - -static widget_image_t *image_backgorund; - -static widget_button_t *button_back; -static widget_button_t *button_play; - -static widget_label_t *label_none; -static widget_label_t *label_server; -static widget_label_t *label_client; - -static widget_label_t *label_ip; -static widget_label_t *label_port; - -static widget_check_t *check_none; -static widget_check_t *check_server; -static widget_check_t *check_client; - -static widget_textfield_t *textfield_ip; -static widget_textfield_t *textfield_port; - -void startScreenGameType() -{ - playMusic("menu", MUSIC_GROUP_BASE); -} - -void drawScreenGameType() -{ - drawWidgetImage(image_backgorund); - - drawWidgetLabel(label_none); - drawWidgetLabel(label_server); - drawWidgetLabel(label_client); - - if( check_none->status == FALSE ) - { - drawWidgetLabel(label_port); - drawWidgetTextfield(textfield_port); - drawWidgetLabel(label_ip); - drawWidgetTextfield(textfield_ip); - } - - drawWidgetCheck(check_none); - drawWidgetCheck(check_server); - drawWidgetCheck(check_client); - - drawWidgetButton(button_back); - drawWidgetButton(button_play); -} - -void eventScreenGameType() -{ - eventWidgetCheck(check_none); - eventWidgetCheck(check_server); - eventWidgetCheck(check_client); - - eventWidgetTextfield(textfield_ip); - eventWidgetTextfield(textfield_port); - - eventWidgetButton(button_back); - eventWidgetButton(button_play); -} - -void stopScreenGameType() -{ -} - -static void eventWidget(void *p) -{ - widget_button_t *button; - widget_check_t *check; - - button = (widget_button_t *)(p); - check = (widget_check_t *)(p); - - if( check == check_none ) - { - check_none->status = TRUE; - check_server->status = FALSE; - check_client->status = FALSE; - return; - } - - if( check == check_server ) - { - check_none->status = FALSE; - check_server->status = TRUE; - check_client->status = FALSE; - return; - } - - if( check == check_client ) - { - check_none->status = FALSE; - check_server->status = FALSE; - check_client->status = TRUE; - return; - } - - if( button == button_back ) - { - setScreen("mainMenu"); - } - - if( button == button_play ) - { - if( getSettingGameType() == NET_GAME_TYPE_CLIENT ) - { - setScreen("world"); - } - else - { - setScreen("chiceArena"); - } - } -} - -void initScreenGameType() -{ - SDL_Surface *image; - - image = getImage(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = newWidgetImage(0, 0, image); - - button_back = newWidgetButton(getMyText("BACK"), 100, WINDOW_SIZE_Y-100, eventWidget); - button_play = newWidgetButton(getMyText("PLAY"), WINDOW_SIZE_X-200, WINDOW_SIZE_Y-100, eventWidget); - - check_none = newWidgetCheck(100, 150, FALSE, eventWidget); - check_server = newWidgetCheck(100, 200, FALSE, eventWidget); - check_client = newWidgetCheck(100, 250, FALSE, eventWidget); - - if( isParamFlag("--server") ) - { - check_server->status = TRUE; - } - else if( isParamFlag("--client") ) - { - check_client->status = TRUE; - } - else - { - check_none->status = TRUE; - } - - label_none = newWidgetLabel(getMyText("NONE"), 130, 145, WIDGET_LABEL_LEFT); - label_server = newWidgetLabel(getMyText("SERVER"), 130, 195, WIDGET_LABEL_LEFT); - label_client = newWidgetLabel(getMyText("CLIENT"), 130, 245, WIDGET_LABEL_LEFT); - - label_ip = newWidgetLabel(getMyText("IP_ADDR"), 300, 145, WIDGET_LABEL_LEFT); - label_port = newWidgetLabel(getMyText("NET_PORT"), 300, 245, WIDGET_LABEL_LEFT); - - textfield_ip = newWidgetTextfield(getParamElse("--ip", "127.0.0.1"), 300, 180); - textfield_port = newWidgetTextfield(getParamElse("--port", "2200"), 300, 280); - - registerScreen( newScreen("gameType", startScreenGameType, eventScreenGameType, - drawScreenGameType, stopScreenGameType) ); -} - -int getSettingGameType() -{ - if( check_none->status == TRUE ) - { - return NET_GAME_TYPE_NONE; - } - - if( check_server->status == TRUE ) - { - return NET_GAME_TYPE_SERVER; - } - - if( check_client->status == TRUE ) - { - return NET_GAME_TYPE_CLIENT; - } - - assert( ! "Chyba pri zistvani type hry !" ); - - return -1; -} - -char* getSettingIP() -{ - return textfield_ip->text; -} - -int getSettingPort() -{ - return atoi( textfield_port->text ); -} - -int getSettingProto() -{ - if( strstr(textfield_ip->text, ".") != NULL ) - { - return PROTO_UDPv4; - } - - if( strstr(textfield_ip->text, ":") != NULL ) - { - return PROTO_UDPv6; - } - - printf("IP protokol unknown !\n"); - return -1; -} - -void quitScreenGameType() -{ - destroyWidgetImage(image_backgorund); - - destroyWidgetLabel(label_none); - destroyWidgetLabel(label_server); - destroyWidgetLabel(label_client); - - destroyWidgetLabel(label_ip); - destroyWidgetLabel(label_port); - - destroyWidgetTextfield(textfield_ip); - destroyWidgetTextfield(textfield_port); - - destroyWidgetButton(button_back); - destroyWidgetButton(button_play); - - destroyWidgetCheck(check_none); - destroyWidgetCheck(check_server); - destroyWidgetCheck(check_client); -} - diff --git a/src/screen_mainMenu.c b/src/screen_mainMenu.c deleted file mode 100644 index ee295ba..0000000 --- a/src/screen_mainMenu.c +++ /dev/null @@ -1,121 +0,0 @@ - -#include -#include - -#include "main.h" -#include "game.h" -#include "interface.h" -#include "language.h" -#include "screen.h" -#include "image.h" -#include "music.h" -#include "fake_audio.h" - -#include "screen_mainMenu.h" -#include "screen_setting.h" - -#include "widget_label.h" -#include "widget_button.h" -#include "widget_textfield.h" -#include "widget_check.h" -#include "widget_image.h" - -static widget_image_t *image_backgorund; - -static widget_button_t *button_play; -static widget_button_t *button_setting; -static widget_button_t *button_table; -static widget_button_t *button_credits; -static widget_button_t *button_end; - -void startScreenMainMenu() -{ - playMusic("menu", MUSIC_GROUP_BASE); -} - -void drawScreenMainMenu() -{ - drawWidgetImage(image_backgorund); - - drawWidgetButton(button_play); - drawWidgetButton(button_setting); - drawWidgetButton(button_table); - drawWidgetButton(button_credits); - drawWidgetButton(button_end); -} - -void eventScreenMainMenu() -{ - eventWidgetButton(button_play); - eventWidgetButton(button_setting); - eventWidgetButton(button_table); - eventWidgetButton(button_credits); - eventWidgetButton(button_end); -} - -void stopScreenMainMenu() -{ -} - -static void eventWidget(void *p) -{ - widget_button_t *button; - - button = (widget_button_t *)(p); - - if( button == button_play ) - { - setScreen("gameType"); - } - - if( button == button_setting ) - { - setScreen("setting"); - } - - if( button == button_table ) - { - setScreen("table"); - } - - if( button == button_credits ) - { - setScreen("credits"); - } - - if( button == button_end ) - { - quitGame(); - } -} - -void initScreenMainMenu() -{ - SDL_Surface *image; - - image = addImageData("screen_main.png", IMAGE_NO_ALPHA, "screen_main", IMAGE_GROUP_BASE); - image_backgorund = newWidgetImage(0, 0, image); - - button_play = newWidgetButton(getMyText("PLAY"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 200, eventWidget); - button_setting = newWidgetButton(getMyText("SETTING"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 250, eventWidget); - button_table = newWidgetButton(getMyText("TABLE"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 300, eventWidget); - button_credits = newWidgetButton(getMyText("CREDITS"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 350, eventWidget); - button_end = newWidgetButton(getMyText("END"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, 400, eventWidget); - - addMusic("menu.ogg", "menu", MUSIC_GROUP_BASE); - - registerScreen( newScreen("mainMenu", startScreenMainMenu, eventScreenMainMenu, - drawScreenMainMenu, stopScreenMainMenu) ); -} - -void quitScreenMainMenu() -{ - destroyWidgetImage(image_backgorund); - - destroyWidgetButton(button_play); - destroyWidgetButton(button_setting); - destroyWidgetButton(button_table); - destroyWidgetButton(button_credits); - destroyWidgetButton(button_end); -} - diff --git a/src/screen_setting.c b/src/screen_setting.c deleted file mode 100644 index 5a42135..0000000 --- a/src/screen_setting.c +++ /dev/null @@ -1,527 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "language.h" -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "music.h" -#include "sound.h" -#include "fake_audio.h" - -#include "screen_mainMenu.h" -#include "list.h" -#include "homeDirector.h" -#include "screen_setting.h" -#include "tux.h" -#include "textFile.h" -#include "configFile.h" -#include "director.h" - -#include "widget_label.h" -#include "widget_button.h" -#include "widget_image.h" -#include "widget_textfield.h" -#include "widget_check.h" -#include "widget_select.h" - -static widget_image_t *image_backgorund; - -static widget_button_t *button_back; - -static widget_label_t *label_count_round; -static widget_label_t *label_name_player1; -static widget_label_t *label_name_player2; -static widget_label_t *label_ai; - -#ifndef NO_SOUND -static widget_label_t *label_music; -static widget_label_t *label_sound; -#endif - -static widget_check_t *check[ITEM_COUNT]; -#ifndef NO_SOUND -static widget_check_t *check_music; -static widget_check_t *check_sound; -#endif - -static widget_image_t *image_gun_dual_revolver; -static widget_image_t *image_gun_scatter; -static widget_image_t *image_gun_tommy; -static widget_image_t *image_gun_lasser; -static widget_image_t *image_gun_mine; -static widget_image_t *image_gun_bombball; - -static widget_image_t *image_bonus_speed; -static widget_image_t *image_bonus_shot; -static widget_image_t *image_bonus_teleport; -static widget_image_t *image_bonus_ghost; -static widget_image_t *image_bonus_4x; -static widget_image_t *image_bonus_hidden; - -static widget_textfield_t *textfield_count_cound; -static widget_textfield_t *textfield_name_player1; -static widget_textfield_t *textfield_name_player2; - -static widget_select_t *select_ai; - -static textFile_t *configFile; - -void startScreenSetting() -{ - playMusic("menu", MUSIC_GROUP_BASE); -} - -void drawScreenSetting() -{ - int i; - - drawWidgetImage(image_backgorund); - - drawWidgetLabel(label_count_round); - drawWidgetLabel(label_name_player1); - drawWidgetLabel(label_name_player2); - drawWidgetLabel(label_ai); - -#ifndef NO_SOUND - drawWidgetLabel(label_music); - drawWidgetLabel(label_sound); -#endif - - drawWidgetTextfield(textfield_count_cound); - drawWidgetTextfield(textfield_name_player1); - drawWidgetTextfield(textfield_name_player2); - - drawWidgetImage(image_gun_dual_revolver); - drawWidgetImage(image_gun_scatter); - drawWidgetImage(image_gun_tommy); - drawWidgetImage(image_gun_lasser); - drawWidgetImage(image_gun_mine); - drawWidgetImage(image_gun_bombball); - - drawWidgetImage(image_bonus_speed); - drawWidgetImage(image_bonus_shot); - drawWidgetImage(image_bonus_teleport); - drawWidgetImage(image_bonus_ghost); - drawWidgetImage(image_bonus_4x); - drawWidgetImage(image_bonus_hidden); - -#ifndef NO_SOUND - drawWidgetCheck(check_music); - drawWidgetCheck(check_sound); -#endif - - for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) - { - drawWidgetCheck(check[i]); - } - - for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) - { - drawWidgetCheck(check[i]); - } - - drawWidgetSelect(select_ai); - - drawWidgetButton(button_back); -} - -void eventScreenSetting() -{ - int i; -/* - int x, y; - - getMousePosition(&x, &y); - - if( isMouseClicked() ) - { - printf("x = %d y = %d\n", x, y); - } -*/ - eventWidgetTextfield(textfield_count_cound); - eventWidgetTextfield(textfield_name_player1); - eventWidgetTextfield(textfield_name_player2); - -#ifndef NO_SOUND - eventWidgetCheck(check_music); - eventWidgetCheck(check_sound); -#endif - - for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) - { - eventWidgetCheck(check[i]); - } - - for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) - { - eventWidgetCheck(check[i]); - } - - eventWidgetSelect(select_ai); - - eventWidgetButton(button_back); -} - -void stopScreenSetting() -{ -} - -static void eventWidget(void *p) -{ - widget_button_t *button; - widget_check_t *check; - - button = (widget_button_t *)(p); - check = (widget_check_t *)(p); - - if( button == button_back ) - { - setScreen("mainMenu"); - } - -#ifndef NO_SOUND - if( check == check_music ) - { - setMusicActive( check_music->status ); - } - - if( check == check_sound ) - { - setSoundActive( check_sound->status ); - } -#endif -} - -static void initSettingFile() -{ - char path[STR_PATH_SIZE]; - char val[STR_SIZE]; - - sprintf(path, "%s/tuxanci-ng.conf", getHomeDirector()); - - configFile = loadTextFile(path); - - if( configFile == NULL ) - { - fprintf(stderr, "Don't load %s\n", path); - fprintf(stderr, "I create %s\n", path); - - configFile = newTextFile(path); - } - - if( configFile == NULL ) - { - fprintf(stderr, "Don't create %s\n", path); - return; - } - - loadValueFromConfigFile(configFile, "COUNT_ROUND", val, STR_SIZE, "15"); - strcpy(textfield_count_cound->text, val); - loadValueFromConfigFile(configFile, "NAME_PLAYER_RIGHT", val, STR_SIZE, "name1"); - strcpy(textfield_name_player1->text, val); - loadValueFromConfigFile(configFile, "NAME_PLAYER_LEFT", val, STR_SIZE, "name2"); - strcpy(textfield_name_player2->text, val); - - loadValueFromConfigFile(configFile, "GUN_DUAL_SIMPLE", val, STR_SIZE, "YES"); - check[GUN_DUAL_SIMPLE]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "GUN_SCATTER", val, STR_SIZE, "YES"); - check[GUN_SCATTER]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "GUN_TOMMY", val, STR_SIZE, "YES"); - check[GUN_TOMMY]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "GUN_LASSER", val, STR_SIZE, "YES"); - check[GUN_LASSER]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "GUN_MINE", val, STR_SIZE, "YES"); - check[GUN_MINE]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "GUN_BOMBBALL", val, STR_SIZE, "YES"); - check[GUN_BOMBBALL]->status = isYesOrNO(val); - - loadValueFromConfigFile(configFile, "BONUS_SPEED", val, STR_SIZE, "YES"); - check[BONUS_SPEED]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "BONUS_SHOT", val, STR_SIZE, "YES"); - check[BONUS_SHOT]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "BONUS_TELEPORT", val, STR_SIZE, "YES"); - check[BONUS_TELEPORT]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "BONUS_GHOST", val, STR_SIZE, "YES"); - check[BONUS_GHOST]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "BONUS_4X", val, STR_SIZE, "YES"); - check[BONUS_4X]->status = isYesOrNO(val); - loadValueFromConfigFile(configFile, "BONUS_HIDDEN", val, STR_SIZE, "YES"); - check[BONUS_HIDDEN]->status = isYesOrNO(val); - -#ifndef NO_SOUND - loadValueFromConfigFile(configFile, "MUSIC", val, STR_SIZE, "YES"); - check_music->status = isYesOrNO(val); - - loadValueFromConfigFile(configFile, "SOUND", val, STR_SIZE, "YES"); - check_sound->status = isYesOrNO(val); -#endif - - saveTextFile(configFile); -} - -static void saveAndDestroyConfigFile() -{ - if( configFile == NULL ) - { - fprintf(stderr, "i can't save configure, " - "because config file not created !\n"); - - return; - } - - setValueInConfigFile(configFile, "COUNT_ROUND", textfield_count_cound->text ); - setValueInConfigFile(configFile, "NAME_PLAYER_RIGHT", textfield_name_player1->text ); - setValueInConfigFile(configFile, "NAME_PLAYER_LEFT", textfield_name_player2->text ); - - setValueInConfigFile(configFile, "GUN_DUAL_SIMPLE", getYesOrNo(check[GUN_DUAL_SIMPLE]->status) ); - setValueInConfigFile(configFile, "GUN_SCATTER", getYesOrNo(check[GUN_SCATTER]->status) ); - setValueInConfigFile(configFile, "GUN_TOMMY", getYesOrNo(check[GUN_TOMMY]->status) ); - setValueInConfigFile(configFile, "GUN_LASSER", getYesOrNo(check[GUN_LASSER]->status) ); - setValueInConfigFile(configFile, "GUN_MINE", getYesOrNo(check[GUN_MINE]->status) ); - setValueInConfigFile(configFile, "GUN_BOMBBALL", getYesOrNo(check[GUN_BOMBBALL]->status) ); - setValueInConfigFile(configFile, "BONUS_SPEED", getYesOrNo(check[BONUS_SPEED]->status) ); - setValueInConfigFile(configFile, "BONUS_SHOT", getYesOrNo(check[BONUS_SHOT]->status) ); - setValueInConfigFile(configFile, "BONUS_TELEPORT", getYesOrNo(check[BONUS_TELEPORT]->status) ); - setValueInConfigFile(configFile, "BONUS_GHOST", getYesOrNo(check[BONUS_GHOST]->status) ); - setValueInConfigFile(configFile, "BONUS_4X", getYesOrNo(check[BONUS_4X]->status) ); - setValueInConfigFile(configFile, "BONUS_HIDDEN", getYesOrNo(check[BONUS_HIDDEN]->status) ); - -#ifndef NO_SOUND - setValueInConfigFile(configFile, "MUSIC", getYesOrNo(check_music->status) ); - setValueInConfigFile(configFile, "SOUND", getYesOrNo(check_sound->status) ); -#endif - - saveTextFile(configFile); - destroyTextFile(configFile); -} - -static void findModulesAI() -{ - director_t *listModules; - int i; - - listModules = loadDirector(PATH_MODULES); - - for( i = 0 ; i < listModules->list->count ; i++) - { - char *line; - - line = (char *)listModules->list->list[i]; - - if( strstr(line,"AI") != NULL && - strstr(line,".so") != NULL ) - { - addToWidgetSelect(select_ai, line); - } - } - - destroyDirector(listModules); -} - -void initScreenSetting() -{ - SDL_Surface *image; - int i; - - image = getImage(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = newWidgetImage(0, 0, image); - - button_back = newWidgetButton(getMyText("BACK"), WINDOW_SIZE_X-200, WINDOW_SIZE_Y-100, eventWidget); - - label_count_round = newWidgetLabel(getMyText("COUNT_ROUND"), 100, WINDOW_SIZE_Y-200, WIDGET_LABEL_LEFT); - label_name_player1 = newWidgetLabel(getMyText("NAME_PLAYER1"), 100, WINDOW_SIZE_Y-160, WIDGET_LABEL_LEFT); - label_name_player2 = newWidgetLabel(getMyText("NAME_PLAYER2"), 100, WINDOW_SIZE_Y-120, WIDGET_LABEL_LEFT); - label_ai = newWidgetLabel("AI :", 430, 400, WIDGET_LABEL_LEFT); - - textfield_count_cound = newWidgetTextfield(getParamElse("--count", "15"), 110+label_count_round->w, WINDOW_SIZE_Y-200); - - textfield_name_player1 = newWidgetTextfield(getParamElse("--name1", "name1"), 110+label_name_player1->w, WINDOW_SIZE_Y-160); - textfield_name_player2 = newWidgetTextfield(getParamElse("--name2", "name2"), 110+label_name_player2->w, WINDOW_SIZE_Y-120); - -#ifndef NO_SOUND - label_music = newWidgetLabel(getMyText("MUSIC"), 100, WINDOW_SIZE_Y-85, WIDGET_LABEL_LEFT); - check_music = newWidgetCheck(label_music->x + label_music->w + 10, - WINDOW_SIZE_Y-80, isMusicActive() , eventWidget); - label_sound = newWidgetLabel(getMyText("SOUND"), check_music->x + WIDGET_CHECK_WIDTH + 10, - WINDOW_SIZE_Y-85, WIDGET_LABEL_LEFT); - check_sound = newWidgetCheck(label_sound->x + label_sound->w + 10, - WINDOW_SIZE_Y-80, isSoundActive() , eventWidget); -#endif - - for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) - { - int x = 0; - int y = 0; - - if( i >= GUN_DUAL_SIMPLE && i <= GUN_TOMMY ) - { - x = 100; - y = 200 + ( i - GUN_DUAL_SIMPLE ) * 50; - } - - if( i >= GUN_LASSER && i <= GUN_BOMBBALL ) - { - x = 250; - y = 200 + ( i - GUN_LASSER ) * 50; - } - - check[i] = newWidgetCheck(x, y, TRUE, NULL); - } - - for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) - { - int x = 0; - int y = 0; - - if( i >= BONUS_SPEED && i <= BONUS_TELEPORT ) - { - x = 430; - y = 200 + ( i - BONUS_SPEED ) * 50; - } - - if( i >= BONUS_GHOST && i <= BONUS_HIDDEN ) - { - x = 580; - y = 200 + ( i - BONUS_GHOST ) * 50; - } - - check[i] = newWidgetCheck(x, y, TRUE, NULL); - } - - image_gun_dual_revolver = newWidgetImage(110 + WIDGET_CHECK_WIDTH, 200, getImage(IMAGE_GROUP_BASE, "panel_dual")); - image_gun_scatter = newWidgetImage(110 + WIDGET_CHECK_WIDTH, 250, getImage(IMAGE_GROUP_BASE, "panel_scatter")); - image_gun_tommy = newWidgetImage(110 + WIDGET_CHECK_WIDTH, 300, getImage(IMAGE_GROUP_BASE, "panel_tommy")); - image_gun_lasser = newWidgetImage(260 + WIDGET_CHECK_WIDTH, 200, getImage(IMAGE_GROUP_BASE, "panel_lasser")); - image_gun_mine = newWidgetImage(260 + WIDGET_CHECK_WIDTH, 250, getImage(IMAGE_GROUP_BASE, "panel_mine")); - image_gun_bombball = newWidgetImage(260 + WIDGET_CHECK_WIDTH, 300, getImage(IMAGE_GROUP_BASE, "panel_bombball")); - - image_bonus_speed = newWidgetImage(430 + WIDGET_CHECK_WIDTH, 200, getImage(IMAGE_GROUP_BASE, "panel_speed"));; - image_bonus_shot = newWidgetImage(430 + WIDGET_CHECK_WIDTH, 250, getImage(IMAGE_GROUP_BASE, "panel_shot"));; - image_bonus_teleport = newWidgetImage(430 + WIDGET_CHECK_WIDTH, 300, getImage(IMAGE_GROUP_BASE, "panel_teleport"));; - image_bonus_ghost = newWidgetImage(580 + WIDGET_CHECK_WIDTH, 200, getImage(IMAGE_GROUP_BASE, "panel_ghost"));; - image_bonus_4x = newWidgetImage(580 + WIDGET_CHECK_WIDTH, 250, getImage(IMAGE_GROUP_BASE, "panel_4x"));; - image_bonus_hidden = newWidgetImage(580 + WIDGET_CHECK_WIDTH, 300, getImage(IMAGE_GROUP_BASE, "panel_hidden"));; - - registerScreen( newScreen("setting", startScreenSetting, eventScreenSetting, - drawScreenSetting, stopScreenSetting) ); - - initSettingFile(); - - select_ai = newWidgetSelect(label_ai->x, label_ai->y+40, eventWidget); - addToWidgetSelect(select_ai, "none"); - findModulesAI(); - -#ifndef NO_SOUND - eventWidget(check_music); - eventWidget(check_sound); -#endif -} - -void getSettingNameRight(char *s) -{ - strcpy(s, textfield_name_player1->text); -} - -void getSettingNameLeft(char *s) -{ - strcpy(s, textfield_name_player2->text); -} - -void getSettingCountRound(int *n) -{ - *n = atoi( textfield_count_cound->text ); -} - -char* getSettingAI() -{ - return (char *) select_ai->list->list[ select_ai->select ]; -} - -bool_t isSettingAnyItem() -{ - int i; - - for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) - { - if( check[i]->status == TRUE ) - { - return TRUE; - } - } - - for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) - { - if( check[i]->status == TRUE ) - { - return TRUE; - } - } - - return FALSE; -} - -bool_t isSettingItem(int item) -{ - return check[item]->status; -} - -void quitScreenSetting() -{ - int i; - - saveAndDestroyConfigFile(); - - destroyWidgetImage(image_backgorund); - - destroyWidgetLabel(label_count_round); - destroyWidgetLabel(label_name_player1); - destroyWidgetLabel(label_name_player2); - destroyWidgetLabel(label_ai); - -#ifndef NO_SOUND - destroyWidgetLabel(label_music); - destroyWidgetLabel(label_sound); -#endif - - destroyWidgetTextfield(textfield_count_cound); - destroyWidgetTextfield(textfield_name_player1); - destroyWidgetTextfield(textfield_name_player2); - - destroyWidgetImage(image_gun_dual_revolver); - destroyWidgetImage(image_gun_scatter); - destroyWidgetImage(image_gun_tommy); - destroyWidgetImage(image_gun_lasser); - destroyWidgetImage(image_gun_mine); - destroyWidgetImage(image_gun_bombball); - - destroyWidgetImage(image_bonus_speed); - destroyWidgetImage(image_bonus_shot); - destroyWidgetImage(image_bonus_teleport); - destroyWidgetImage(image_bonus_ghost); - destroyWidgetImage(image_bonus_4x); - destroyWidgetImage(image_bonus_hidden); - -#ifndef NO_SOUND - destroyWidgetCheck(check_music); - destroyWidgetCheck(check_sound); -#endif - - for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) - { - destroyWidgetCheck(check[i]); - } - - for( i = BONUS_SPEED ; i <= BONUS_HIDDEN ; i++ ) - { - destroyWidgetCheck(check[i]); - } - - destroyWidgetSelect(select_ai); - - destroyWidgetButton(button_back); -} - diff --git a/src/screen_table.c b/src/screen_table.c deleted file mode 100644 index 1571489..0000000 --- a/src/screen_table.c +++ /dev/null @@ -1,235 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "language.h" -#include "interface.h" -#include "list.h" -#include "textFile.h" -#include "homeDirector.h" -#include "screen.h" -#include "image.h" -#include "music.h" -#include "fake_audio.h" - -#include "screen_table.h" - -#include "widget_image.h" -#include "widget_label.h" -#include "widget_button.h" - -static widget_image_t *image_backgorund; -static widget_button_t *button_back; - -static list_t *listWidgetLabelNumer; -static list_t *listWidgetLabelName; -static list_t *listWidgetLabelScore; - -static textFile_t *textFile; - -void startScreenTable() -{ - playMusic("menu", MUSIC_GROUP_BASE); -} - -void drawScreenTable() -{ - int i; - - drawWidgetImage(image_backgorund); - - drawWidgetButton(button_back); - - for( i = 0 ; i < listWidgetLabelNumer->count ; i++ ) - { - widget_label_t *this; - this = (widget_label_t *)listWidgetLabelNumer->list[i]; - drawWidgetLabel(this); - } - - for( i = 0 ; i < listWidgetLabelName->count ; i++ ) - { - widget_label_t *this; - this = (widget_label_t *)listWidgetLabelName->list[i]; - drawWidgetLabel(this); - } - - for( i = 0 ; i < listWidgetLabelScore->count ; i++ ) - { - widget_label_t *this; - this = (widget_label_t *)listWidgetLabelScore->list[i]; - drawWidgetLabel(this); - } -} - -void eventScreenTable() -{ - eventWidgetButton(button_back); -} - -void stopScreenTable() -{ -} - -static void eventWidget(void *p) -{ - widget_button_t *button; - - button = (widget_button_t *)(p); - - if( button == button_back ) - { - setScreen("mainMenu"); - } -} - -static void setWidgetLabel() -{ - int i; - - if( textFile == NULL ) - { - fprintf(stderr, "File %s not loaded !", - SCREEN_TABLE_FILE_HIGHSCORE_NAME); - - return; - } - - if( listWidgetLabelNumer != NULL || - listWidgetLabelName != NULL || - listWidgetLabelScore != NULL ) - { - destroyListItem(listWidgetLabelNumer, destroyWidgetLabel); - destroyListItem(listWidgetLabelName, destroyWidgetLabel); - destroyListItem(listWidgetLabelScore, destroyWidgetLabel); - } - - listWidgetLabelNumer = newList(); - listWidgetLabelName = newList(); - listWidgetLabelScore = newList(); - - for( i = 0 ; i < SCREEN_TABLE_MAX_PLAYERS ; i++ ) - { - widget_label_t *label; - char name[STR_NAME_SIZE]; - char score[STR_NUM_SIZE]; - char num[STR_NUM_SIZE]; - char *line; - - line = (char *) textFile->text->list[i]; - - sscanf(line, "%s %s", name, score); - sprintf(num, "%2d)", i+1); - - label = newWidgetLabel(num, WINDOW_SIZE_X/2 - 100, 200 + i * 20, WIDGET_LABEL_LEFT); - addList(listWidgetLabelNumer, label); - - label = newWidgetLabel(name, WINDOW_SIZE_X/2, 200 + i * 20, WIDGET_LABEL_CENTER); - addList(listWidgetLabelName, label); - - label = newWidgetLabel(score, WINDOW_SIZE_X/2 + 80, 200 + i * 20, WIDGET_LABEL_LEFT); - addList(listWidgetLabelScore, label); - } -} - -static void loadHighscoreFile() -{ - char path[STR_PATH_SIZE]; - int i; - - sprintf(path, "%s/" SCREEN_TABLE_FILE_HIGHSCORE_NAME , getHomeDirector()); - textFile = loadTextFile(path); - - if( textFile == NULL ) - { - fprintf(stderr, "I can't load %s !\n", path); - fprintf(stderr, "I create %s\n", path); - textFile = newTextFile(path); - } - else - { - return; - } - - if( textFile == NULL ) - { - fprintf(stderr, "I can't create %s !\n", path); - return; - } - - for( i = 0 ; i < SCREEN_TABLE_MAX_PLAYERS ; i++ ) - { - addList(textFile->text, strdup("no_name 0") ); - } - - saveTextFile(textFile); -} - -int addPlayerInHighScore(char *name, int score) -{ - int i; - - for( i = 0 ; i < SCREEN_TABLE_MAX_PLAYERS ; i++ ) - { - char *line; - char thisName[STR_NAME_SIZE]; - int thisCore; - - line = (char *) textFile->text->list[i]; - sscanf(line, "%s %d", thisName, &thisCore); - - if( score >= thisCore ) - { - char new[STR_SIZE]; - - sprintf(new, "%s %d", name, score); - insList(textFile->text, i, strdup(new) ); - delListItem(textFile->text, SCREEN_TABLE_MAX_PLAYERS, free); - setWidgetLabel(); - - return 0; - } - } - - return -1; -} - -void initScreenTable() -{ - SDL_Surface *image; - - - image = addImageData("screen_table.png", IMAGE_NO_ALPHA, "screen_table", IMAGE_GROUP_BASE); - image_backgorund = newWidgetImage(0, 0, image); - - button_back = newWidgetButton(getMyText("BACK"), WINDOW_SIZE_X/2 -WIDGET_BUTTON_WIDTH/2, - WINDOW_SIZE_Y-100, eventWidget); - - loadHighscoreFile(); - listWidgetLabelNumer = NULL; - listWidgetLabelName = NULL; - listWidgetLabelScore = NULL; - setWidgetLabel(); - - registerScreen( newScreen("table", startScreenTable, eventScreenTable, - drawScreenTable, stopScreenTable) ); -} - -void quitScreenTable() -{ - destroyWidgetImage(image_backgorund); - - destroyWidgetButton(button_back); - destroyListItem(listWidgetLabelNumer, destroyWidgetLabel); - destroyListItem(listWidgetLabelName, destroyWidgetLabel); - destroyListItem(listWidgetLabelScore, destroyWidgetLabel); - - if( textFile != NULL ) - { - saveTextFile(textFile); - destroyTextFile(textFile); - } -} - diff --git a/src/screen_world.c b/src/screen_world.c deleted file mode 100644 index f889471..0000000 --- a/src/screen_world.c +++ /dev/null @@ -1,588 +0,0 @@ -#include -#include - -#include "main.h" -#include "interface.h" - -#include "screen.h" -#include "screen_world.h" -#include "screen_analyze.h" -#include "screen_setting.h" -#include "screen_gameType.h" -#include "screen_choiceArena.h" -#include "screen_table.h" - -#include "keytable.h" -#include "list.h" -#include "layer.h" -#include "image.h" -#include "music.h" -#include "sound.h" -#include "fake_audio.h" -#include "tux.h" -#include "item.h" -#include "shot.h" -#include "gun.h" -#include "panel.h" -#include "myTimer.h" -#include "net_multiplayer.h" -#include "arena.h" -#include "arenaFile.h" -#include "proto.h" -#include "term.h" -#include "font.h" -#include "music.h" -#include "modules.h" -#include "idManager.h" -#include "heightScore.h" - -static arena_t *arena; -static int count; -static int max_count; -static my_time_t lastServerLag; -static bool_t isScreenWorldInit = FALSE; -static bool_t isEndWorld; - -static tux_t *tuxWithControlRightKeyboard; -static tux_t *tuxWithControlLeftKeyboard; - -bool_t isScreenWorldInicialized() -{ - return isScreenWorldInit; -} - -void setGameType() -{ - int ret = 0; - - ret = initNetMuliplayer( getSettingGameType(), getSettingIP(), getSettingPort(), getSettingProto() ); - - if( ret != 0 ) - { - fprintf(stderr, "Chyba inicalizacie sieti !\n"); - setWorldEnd(); - } -} - -void setWorldArena(int id) -{ - arena = getArena(id); - playMusic(arena->music, MUSIC_GROUP_USER); -} - -void setMaxCountRound(int n) -{ - max_count = n; -} - -void setLagServer(my_time_t lag) -{ - lastServerLag = lag; -} - -void setWorldEnd() -{ - isEndWorld = TRUE; -} - -static void timer_endArena() -{ - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - setWorldEnd(); - return; - } -} - -void countRoundInc() -{ - if( count == WORLD_COUNT_ROUND_UNLIMITED || - count >= max_count ) - { - return; - } - - count++; - printf("count %d/%d\n", count, max_count); - - if( count >= max_count ) - { - printf("count %d ending\n", count); - playSound("end", SOUND_GROUP_BASE); - addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_endArena, NULL, TIMER_END_ARENA); - } -} - -void prepareArena() -{ - tux_t *tux; - - //printf("getSettingAI = %s\n", getSettingAI()); - - switch( getNetTypeGame() ) - { - case NET_GAME_TYPE_NONE : - setWorldArena( getChoiceArenaId() ); - addNewItem(arena->spaceItem, ID_UNKNOWN); - getSettingCountRound(&max_count); - - tux = newTux(); - tux->control = TUX_CONTROL_KEYBOARD_RIGHT; - tuxWithControlRightKeyboard = tux; - getSettingNameRight(tux->name); - addObjectToSpace(arena->spaceTux, tux); - - tux = newTux(); - tux->control = TUX_CONTROL_KEYBOARD_LEFT; - tuxWithControlLeftKeyboard = tux; - getSettingNameLeft(tux->name); - addObjectToSpace(arena->spaceTux, tux); - - if( strcmp(getSettingAI(), "none") != 0 ) - { - loadModule( getSettingAI() ); - tux->control = TUX_CONTROL_AI; - } - break; - - case NET_GAME_TYPE_SERVER : - setWorldArena( getChoiceArenaId() ); - addNewItem(arena->spaceItem, ID_UNKNOWN); - getSettingCountRound(&max_count); - - tux = newTux(); - tux->control = TUX_CONTROL_KEYBOARD_RIGHT; - tuxWithControlRightKeyboard = tux; - getSettingNameRight(tux->name); - addObjectToSpace(arena->spaceTux, tux); - break; - - case NET_GAME_TYPE_CLIENT : - break; - } -} - -/* -static void drawServerLag() -{ - char msg[STR_SIZE]; - - if( lastServerLag == LAG_SERVER_UNKNOWN ) - { - sprintf(msg, "lag server: unknown"); - } - else - { - sprintf(msg, "lag server: %d", lastServerLag); - } - - drawFont(msg, WINDOW_SIZE_X-150, 20, COLOR_WHITE); - -} -*/ - -void drawWorld() -{ - if( arena != NULL ) - { - drawArena(arena); - drawPanel(arena->spaceTux->list); - } - - drawTerm(); -} - -static void netAction(tux_t *tux, int action) -{ - if( getSettingGameType() == NET_GAME_TYPE_SERVER ) - { - proto_send_event_server(PROTO_SEND_ALL_SEES_TUX, NULL, tux , action); - } - - if( getSettingGameType() == NET_GAME_TYPE_CLIENT ) - { - proto_send_event_client(action); - } -} - -static void control_keyboard_right(tux_t *tux) -{ - static int lastKey = 0; - int countKey; - Uint8 *mapa; - mapa = SDL_GetKeyState(NULL); - - assert( tux != NULL ); - assert( mapa != NULL ); - - countKey = 0; - - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_UP)] == SDL_PRESSED )countKey++; - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_RIGHT)] == SDL_PRESSED )countKey++; - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_LEFT)] == SDL_PRESSED )countKey++; - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_DOWN)] == SDL_PRESSED )countKey++; - - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_UP)] == SDL_PRESSED ) - { - if(countKey > 1 && lastKey == getKey(KEY_TUX_RIGHT_MOVE_UP) )goto tuUp; - - if( countKey == 1 )lastKey = getKey(KEY_TUX_RIGHT_MOVE_UP); - netAction(tux, TUX_UP); - actionTux(tux, TUX_UP); - return; - - tuUp:; - } - - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_RIGHT)] == SDL_PRESSED ) - { - if(countKey > 1 && lastKey == getKey(KEY_TUX_RIGHT_MOVE_RIGHT) )goto tuRight; - - if( countKey == 1 )lastKey = getKey(KEY_TUX_RIGHT_MOVE_RIGHT); - netAction(tux, TUX_RIGHT); - actionTux(tux, TUX_RIGHT); - return; - - tuRight:; - - } - - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_LEFT)] == SDL_PRESSED ) - { - if(countKey > 1 && lastKey == getKey(KEY_TUX_RIGHT_MOVE_LEFT) )goto tuLeft; - - if( countKey == 1 )lastKey = getKey(KEY_TUX_RIGHT_MOVE_LEFT); - netAction(tux, TUX_LEFT); - actionTux(tux, TUX_LEFT); - return; - - tuLeft:; - } - - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_MOVE_DOWN)] == SDL_PRESSED ) - { - if(countKey > 1 && lastKey == getKey(KEY_TUX_RIGHT_MOVE_DOWN) )goto tuDown; - - if( countKey == 1 )lastKey = getKey(KEY_TUX_RIGHT_MOVE_DOWN); - netAction(tux, TUX_DOWN); - actionTux(tux, TUX_DOWN); - return; - - tuDown:; - } - - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_SHOOT)] == SDL_PRESSED && tux->isCanShot == TRUE ) - { - netAction(tux, TUX_SHOT); - actionTux(tux, TUX_SHOT); - return; - } - - if( mapa[(SDLKey)getKey(KEY_TUX_RIGHT_SWITCH_WEAPON)] == SDL_PRESSED ) - { - if( tux->isCanSwitchGun == TRUE ) - { - netAction(tux, TUX_SWITCH_GUN); - actionTux(tux, TUX_SWITCH_GUN); - return; - } - } -} - -static void control_keyboard_left(tux_t *tux) -{ - static int lastKey = 0; - int countKey; - Uint8 *mapa; - mapa = SDL_GetKeyState(NULL); - - assert( tux != NULL ); - assert( mapa != NULL ); - - countKey = 0; - - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_UP)] == SDL_PRESSED )countKey++; - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_RIGHT)] == SDL_PRESSED )countKey++; - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_LEFT)] == SDL_PRESSED )countKey++; - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_DOWN)] == SDL_PRESSED )countKey++; - - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_UP)] == SDL_PRESSED ) - { - if(countKey > 1 && lastKey == getKey(KEY_TUX_LEFT_MOVE_UP) )goto tuUp; - - if( countKey == 1 )lastKey = getKey(KEY_TUX_LEFT_MOVE_UP); - actionTux(tux, TUX_UP); - return; - - tuUp:; - } - - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_RIGHT)] == SDL_PRESSED ) - { - if(countKey > 1 && lastKey == getKey(KEY_TUX_LEFT_MOVE_RIGHT) )goto tuRight; - - if( countKey == 1 )lastKey = getKey(KEY_TUX_LEFT_MOVE_RIGHT); - actionTux(tux, TUX_RIGHT); - return; - - tuRight:; - - } - - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_LEFT)] == SDL_PRESSED ) - { - if(countKey > 1 && lastKey == getKey(KEY_TUX_LEFT_MOVE_LEFT) )goto tuLeft; - - if( countKey == 1 )lastKey = getKey(KEY_TUX_LEFT_MOVE_LEFT); - actionTux(tux, TUX_LEFT); - return; - - tuLeft:; - } - - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_DOWN)] == SDL_PRESSED ) - { - if(countKey > 1 && lastKey == getKey(KEY_TUX_LEFT_MOVE_DOWN) )goto tuDown; - - if( countKey == 1 )lastKey = getKey(KEY_TUX_LEFT_MOVE_DOWN); - actionTux(tux, TUX_DOWN); - return; - - tuDown:; - } - - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_SHOOT)] == SDL_PRESSED && tux->isCanShot == TRUE ) - { - actionTux(tux, TUX_SHOT); - return; - } - - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_SWITCH_WEAPON)] == SDL_PRESSED ) - { - if( tux->isCanSwitchGun == TRUE ) - { - actionTux(tux, TUX_SWITCH_GUN); - return; - } - } -/* - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_UP)] == SDL_PRESSED )actionTux(tux, TUX_UP); - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_RIGHT)] == SDL_PRESSED )actionTux(tux, TUX_RIGHT); - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_LEFT)] == SDL_PRESSED )actionTux(tux, TUX_LEFT); - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_MOVE_DOWN)] == SDL_PRESSED )actionTux(tux, TUX_DOWN); - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_SHOOT)] == SDL_PRESSED )actionTux(tux, TUX_SHOT); - - if( mapa[(SDLKey)getKey(KEY_TUX_LEFT_SWITCH_WEAPON)] == SDL_PRESSED ) - { - if( tux->isCanSwitchGun == TRUE ) - { - actionTux(tux, TUX_SWITCH_GUN); - } - } -*/ -} - -static void eventEsc() -{ - Uint8 *mapa; - mapa = SDL_GetKeyState(NULL); - - if( mapa[(SDLKey)SDLK_ESCAPE] == SDL_PRESSED ) - { - isEndWorld = TRUE; - setWorldEnd(); - return; - } -} - -static void eventEnd() -{ - if( isEndWorld == TRUE ) - { - setScreen("analyze"); - return; - } -} - -tux_t* getControlTux(int control_type) -{ - switch(control_type) - { - case TUX_CONTROL_KEYBOARD_RIGHT : - return tuxWithControlRightKeyboard; - break; - case TUX_CONTROL_KEYBOARD_LEFT : - return tuxWithControlLeftKeyboard; - break; - } - - return NULL; -} - -void setControlTux(tux_t *tux, int control_type) -{ - switch(control_type) - { - case TUX_CONTROL_KEYBOARD_RIGHT : - tuxWithControlRightKeyboard = tux; - break; - case TUX_CONTROL_KEYBOARD_LEFT : - tuxWithControlLeftKeyboard = tux; - break; - } -} - -void tuxControl(tux_t *p) -{ - assert( p != NULL ); - - if( p->status != TUX_STATUS_ALIVE ) - { - return; - } - - switch( p->control ) - { - case TUX_CONTROL_NONE : - assert( ! "Tux nema definovane ovladanie !" ); - break; - - case TUX_CONTROL_KEYBOARD_LEFT : - control_keyboard_left(p); - break; - - case TUX_CONTROL_KEYBOARD_RIGHT : - control_keyboard_right(p); - break; - } -} - -void eventWorld() -{ - if( arena == NULL ) - { - eventNetMultiplayer(); - eventEnd(); - eventEsc(); - return; - } - - eventNetMultiplayer(); - - eventArena(arena); - //eventModule(); - - eventTerm(); - eventEnd(); - eventEsc(); -} - -void startWorld() -{ - arena = NULL; - isEndWorld = FALSE; - - count = 0; - lastServerLag = LAG_SERVER_UNKNOWN; - - initListID(); - initModule(); - setGameType(); - initTerm(); - prepareArena(); -} - -static void setAnalyze() -{ - int i; - tux_t *tux; - - restartAnalyze(); - - for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) - { - tux = (tux_t *)(arena->spaceTux->list->list[i]); - addAnalyze(tux->name, tux->score); - } - - endAnalyze(); -} - -static void setTable() -{ - tux_t *tuxRight; - tux_t *tuxLeft; - - if( getSettingGameType() != NET_GAME_TYPE_NONE ) - { - return; - } - - tuxRight = getControlTux(TUX_CONTROL_KEYBOARD_RIGHT); - tuxLeft = getControlTux(TUX_CONTROL_KEYBOARD_LEFT); - - if( tuxRight->score > tuxLeft->score ) - { - addPlayerInHighScore(tuxRight->name, tuxRight->score); - } - - if( tuxLeft->score > tuxRight->score ) - { - addPlayerInHighScore(tuxLeft->name, tuxLeft->score); - } -} - -void stoptWorld() -{ - if( arena != NULL ) - { - setTable(); - setAnalyze(); - } - - quitNetMultiplayer(); - - if( arena != NULL ) - { - destroyArena(arena); - } - - stopMusic(); - delAllImageInGroup(IMAGE_GROUP_USER); - delAllMusicInGroup(MUSIC_GROUP_USER); - quitModule(); - quitTerm(); - quitListID(); -} - -void initWorld() -{ - assert( isImageInicialized() == TRUE ); - assert( isTuxInicialized() == TRUE ); - assert( isShotInicialized() == TRUE ); - assert( isPanelInicialized() == TRUE ); - assert( isScreenInicialized() == TRUE ); - - registerScreen( newScreen("world", startWorld, eventWorld, drawWorld, stoptWorld) ); - - addSound("dead.ogg", "dead", SOUND_GROUP_BASE); - addSound("explozion.ogg", "explozion", SOUND_GROUP_BASE); - addSound("gun_lasser.ogg", "gun_lasser", SOUND_GROUP_BASE); - addSound("gun_revolver.ogg", "gun_revolver", SOUND_GROUP_BASE); - addSound("gun_scatter.ogg", "gun_scatter", SOUND_GROUP_BASE); - addSound("gun_tommy.ogg", "gun_tommy", SOUND_GROUP_BASE); - addSound("put_mine.ogg", "put_mine", SOUND_GROUP_BASE); - addSound("teleport.ogg", "teleport", SOUND_GROUP_BASE); - addSound("item_bonus.ogg", "item_bonus", SOUND_GROUP_BASE); - addSound("item_gun.ogg", "item_gun", SOUND_GROUP_BASE); - addSound("switch_gun.ogg", "switch_gun", SOUND_GROUP_BASE); - addSound("end.ogg", "end", SOUND_GROUP_BASE); - - isScreenWorldInit = TRUE; -} - -void quitWorld() -{ - printf("quit world\n"); - isScreenWorldInit = FALSE; -} diff --git a/src/sdl_udp.c b/src/sdl_udp.c deleted file mode 100644 index 175ac6c..0000000 --- a/src/sdl_udp.c +++ /dev/null @@ -1,219 +0,0 @@ - - -#include -#include -#include -#include -#include - -#include "main.h" -#include "sdl_udp.h" - -#define BUFSIZE 1000 - - -sock_sdl_udp_t* newSdlSockUdp() -{ - sock_sdl_udp_t *new; - - new = malloc( sizeof(sock_sdl_udp_t) ); - memset(new, 0, sizeof(sock_sdl_udp_t)); - - if( !( new->packet = SDLNet_AllocPacket(512) ) ) - { - fprintf(stderr, "nemozem alokovat pamat pre packet !\n"); - destroySockSdlUdp(new); - } - - return new; -} - -void destroySockSdlUdp(sock_sdl_udp_t *p) -{ - assert( p != NULL ); - - SDLNet_FreePacket(p->packet); - free(p); -} - -sock_sdl_udp_t* bindSdlUdpSocket(int port) -{ - sock_sdl_udp_t *new; - - assert( port > 0 && port < 65535 ); - - new = newSdlSockUdp(); - - assert( new != NULL ); - - if( SDLNet_Init() < 0 ) - { - fprintf(stderr, "nemozem initcializovat SDLnet !\n"); - destroySockSdlUdp(new); - return NULL; - } - - if( !( new->sock = SDLNet_UDP_Open(port) ) ) - { - fprintf(stderr, "nemozem otovrit socket !\n"); - destroySockSdlUdp(new); - return NULL; - } - - new->type = SDL_UDP_SERVER; - - return new; -} - -sock_sdl_udp_t* connectSdlUdpSocket(char *address, int port) -{ - sock_sdl_udp_t *new; - - assert( address != NULL ); - assert( port > 0 && port < 65536 ); - - new = newSdlSockUdp(); - - assert( new != NULL ); - - /* Initialize SDL_net */ - if( SDLNet_Init() < 0 ) - { - fprintf(stderr, "nemozem initcializovat SDLnet !\n"); - destroySockSdlUdp(new); - return NULL; - } - - /* Open a socket on random port */ - if( !( new->sock = SDLNet_UDP_Open(0) ) ) - { - fprintf(stderr, "nemozem otovrit socket !\n"); - destroySockSdlUdp(new); - return NULL; - } - - /* Resolve server name */ - if( SDLNet_ResolveHost(&(new->srvadd), address, port) ) - { - fprintf(stderr, "nemozem sa pripojit na sokcet !\n"); - destroySockSdlUdp(new); - return NULL; - } - - new->type = SDL_UDP_CLIENT; - - return new; -} - -int readSdlUdpSocket(sock_sdl_udp_t *src, sock_sdl_udp_t *dst, void *address, int len) -{ - int x; - - if( SDLNet_UDP_Recv(src->sock, dst->packet) ) - { - x = (dst->packet->len > len ? len : dst->packet->len); - memcpy(address, dst->packet->data, x); - return x; - } - - return -1; -} - -int writeSdlUdpSocket(sock_sdl_udp_t *src, sock_sdl_udp_t *dst, void *address, int len) -{ - assert( src != NULL ); - assert( address != NULL ); - -#ifdef DEBUG_LOST_PACKET - if( rand() % DEBUG_LOST_PACKET == 0 ) - { - printf("debug lost packet %s\n", address); - return size; - } -#endif - - memcpy(dst->packet->data, address, len); - dst->packet->len = len; - - if( src->type == SDL_UDP_CLIENT ) - { - dst->packet->address.host = src->srvadd.host; - dst->packet->address.port = src->srvadd.port; - } - - SDLNet_UDP_Send(src->sock, -1, dst->packet); - - return len; -} - -int getSockSdlUdpPort(sock_sdl_udp_t *p) -{ - assert( p != NULL ); - - return p->packet->address.port; -} - - -void closeSdlUdpSocket(sock_sdl_udp_t *p) -{ - assert( p != NULL ); - - SDLNet_UDP_Close(p->sock); - - printf("close connect\n"); - - destroySockSdlUdp(p); -} - -#if 0 -int main(int argc, char *argv[]) -{ - sock_sdl_udp_t *sock; - char buf[BUFSIZE]; - int ret; - - if( strcmp(argv[1], "s") == 0 ) - { - sock_sdl_udp_t *cli; - - sock = bindSdlUdpSocket( atoi(argv[2]) ); - cli = newSdlSockUdp(); - - while(1) - { - memset(buf, 0, BUFSIZE); - - ret = readSdlUdpSocket(sock, cli, buf, BUFSIZE); - - if( ret > 0 ) - { - writeSdlUdpSocket(sock, cli, buf, ret); - } - } - } - - if( strcmp(argv[1], "c") == 0 ) - { - sock = connectSdlUdpSocket("127.0.0.1", atoi(argv[2])); - - strcpy(buf, "Hello world !\n"); - writeSdlUdpSocket(sock, sock, buf, strlen(buf)); - - while(1) - { - memset(buf, 0, BUFSIZE); - ret = readSdlUdpSocket(sock, sock, buf, BUFSIZE); - - if( ret > 0 ) - { - printf("buf = %s", buf); - } - } - - closeSdlUdpSocket(sock); - } - - return 0; -} - -#endif diff --git a/src/server.c b/src/server.c deleted file mode 100644 index 7d5c198..0000000 --- a/src/server.c +++ /dev/null @@ -1,986 +0,0 @@ - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "network.h" -#include "proto.h" -#include "server.h" -#include "assert.h" -#include "myTimer.h" -#include "arena.h" -#include "net_multiplayer.h" -#include "checkFront.h" -#include "protect.h" - -#ifndef PUBLIC_SERVER -#include "screen_world.h" -#endif - -#ifdef PUBLIC_SERVER -#include "publicServer.h" -#include "heightScore.h" -#endif - -#ifdef SUPPORT_NET_UNIX_UDP -#include "udp.h" -static sock_udp_t *sock_server_udp; -static sock_udp_t *sock_server_udp_second; -#endif - -#ifdef SUPPORT_NET_SDL_UDP -#include "sdl_udp.h" -static sock_sdl_udp_t *sock_server_sdl_udp; -#endif - -static list_t *listClient; -static list_t *listServerTimer; -static time_t timeUpdate; -static int maxClients; - -static void startUpdateServer() -{ - timeUpdate = time(NULL); -} - -time_t getUpdateServer() -{ - return time(NULL) - timeUpdate; -} - -static void delZombieCLient(void *p_nothink) -{ - client_t *thisClient; - my_time_t currentTime; - int i; - - currentTime = getMyTime(); - - for( i = 0 ; i < listClient->count; i++) - { - thisClient = (client_t *) listClient->list[i]; - - if( isDown(thisClient->protect) == TRUE ) - { - //proto_send_error_server(PROTO_SEND_ONE, thisClient, PROTO_ERROR_CODE_TIMEOUT); - proto_send_end_server(PROTO_SEND_ONE, thisClient); - eventMsgInCheckFront(thisClient); - thisClient->status = NET_STATUS_ZOMBIE; - } - - if( thisClient->status == NET_STATUS_ZOMBIE ) - { - if( thisClient->tux != NULL ) - { - proto_send_del_server(PROTO_SEND_ALL, NULL, thisClient->tux->id); - } - - delListItem(listClient, i, destroyClient); - } - } - -} - -static void eventPeriodicSyncClient(void *p_nothink) -{ - client_t *thisClientInfo; - client_t *thisClientSend; - tux_t *thisTux; - int i, j; - - for( i = 0 ; i < listClient->count; i++) - { - thisClientSend = (client_t *) listClient->list[i]; - - if( thisClientSend->tux == NULL ) - { - continue; - } - -#ifndef PUBLIC_SERVER - thisTux = getControlTux(TUX_CONTROL_KEYBOARD_RIGHT); - if( isTuxSeesTux(thisClientSend->tux, thisTux) ) - proto_send_newtux_server(PROTO_SEND_ONE, thisClientSend, thisTux); -#endif - - for( j = 0 ; j < listClient->count; j++) - { - thisClientInfo = (client_t *) listClient->list[j]; - thisTux = thisClientInfo->tux; - - if( thisTux != NULL && - thisClientSend != thisClientInfo && - isTuxSeesTux(thisClientSend->tux, thisTux) ) - { - proto_send_newtux_server(PROTO_SEND_ONE, - thisClientSend, thisTux); - } - } - } -} - -static void eventSendPingClients(void *p_nothink) -{ - proto_send_ping_server(PROTO_SEND_ALL, NULL); -} - -static void setServerTimer() -{ - if( listServerTimer != NULL ) - { - destroyTimer(listServerTimer); - } - - restartTimer(); - listServerTimer = newTimer(); - - addTaskToTimer(listServerTimer, TIMER_PERIODIC, delZombieCLient, NULL, SERVER_TIMEOUT); - addTaskToTimer(listServerTimer, TIMER_PERIODIC, eventPeriodicSyncClient, NULL, SERVER_TIME_SYNC); - addTaskToTimer(listServerTimer, TIMER_PERIODIC, eventSendPingClients, NULL, SERVER_TIME_PING); -} - -void static initServer() -{ - startUpdateServer(); - listServerTimer = NULL; - listClient = newList(); - setServerMaxClients(SERVER_MAX_CLIENTS); - setServerTimer(); -} - -#ifdef SUPPORT_NET_UNIX_UDP -int initUdpServer(char *ip, int port, int proto) -{ - sock_server_udp = bindUdpSocket(ip, port, proto); - sock_server_udp_second = NULL; - - if( sock_server_udp == NULL ) - { - return -1; - } - - initServer(); - - printf("server listen UDP port %s %d\n", ip, port); - - return 0; -} -#endif - -#ifdef PUBLIC_SERVER - -int initUdpPublicServer(char *ip4, int port4, char *ip6, int port6) -{ - sock_server_udp = NULL; - sock_server_udp_second = NULL; - - if( ip4 != NULL ) - { - sock_server_udp = bindUdpSocket(ip4, port4, PROTO_UDPv4); - - if( sock_server_udp == NULL ) - { - return -1; - } - - printf("server listen UDP port %s %d\n", ip4, port4); - } - - if( ip6 != NULL ) - { - sock_server_udp_second = bindUdpSocket(ip6, port6, PROTO_UDPv6); - - if( sock_server_udp_second == NULL ) - { - return -1; - } - - printf("server listen UDP port %s %d\n", ip6, port6); - } - - initServer(); - - return 0; -} - -#endif - -#ifdef SUPPORT_NET_SDL_UDP -int initSdlUdpServer(int port) -{ - sock_server_sdl_udp = bindSdlUdpSocket(port); - - if( sock_server_sdl_udp == NULL ) - { - return -1; - } - - initServer(); - - printf("server listen UDP port %d\n", port); - - return 0; -} -#endif - -static client_t* newAnyClient() -{ - client_t *new; - - new = malloc( sizeof(client_t) ); - memset(new, 0, sizeof(client_t)); - - new->status = NET_STATUS_OK; - new->listRecvMsg = newList(); - new->listSendMsg = newCheckFront(); - new->protect = newProtect(); - - return new; -} - -#ifdef SUPPORT_NET_UNIX_UDP -client_t* newUdpClient(sock_udp_t *sock_udp) -{ - client_t *new; - char str_ip[STR_IP_SIZE]; - - assert( sock_udp != NULL ); - - getSockUdpIp(sock_udp, str_ip, STR_IP_SIZE); - printf("new client from %s %d\n", str_ip, getSockUdpPort(sock_udp)); - - new = newAnyClient(); - new->socket_udp = sock_udp; - - return new; -} -#endif - -#ifdef SUPPORT_NET_SDL_UDP -client_t* newSdlUdpClient(sock_sdl_udp_t *sock_sdl_udp) -{ - client_t *new; - - assert( sock_sdl_udp != NULL ); - - printf("new client from %d\n", getSockSdlUdpPort(sock_sdl_udp)); - new = newAnyClient(); - new->socket_sdl_udp = sock_sdl_udp; - - return new; -} -#endif - -void destroyClient(client_t *p) -{ - char str_ip[STR_IP_SIZE]; - - assert( p != NULL ); - - eventMsgInCheckFront(p); - -#ifdef SUPPORT_NET_UNIX_UDP - getSockUdpIp(p->socket_udp, str_ip, STR_IP_SIZE); - printf("close connect %s %d\n", str_ip, getSockUdpPort(p->socket_udp) ); -#endif - -#ifdef SUPPORT_NET_UNIX_UDP - closeUdpSocket(p->socket_udp); -#endif - -#ifdef SUPPORT_NET_SDL_UDP - closeSdlUdpSocket(p->socket_sdl_udp); -#endif - - destroyListItem(p->listRecvMsg, free); - destroyCheckFront(p->listSendMsg); - - destroyProtect(p->protect); - - if( p->tux != NULL ) - { -#ifdef PUBLIC_SERVER - addPlayerInHighScore(p->tux->name, p->tux->score); -#endif - delObjectFromSpaceWithObject(getCurrentArena()->spaceTux, p->tux, destroyTux); - } - - free(p); -} - -list_t* getListServerClient() -{ - return listClient; -} - -int getServerMaxClients() -{ - return maxClients; -} - -void setServerMaxClients(int n) -{ - maxClients = n; -} - -void sendClient(client_t *p, char *msg) -{ - assert( p != NULL ); - assert( msg != NULL ); - - if( p->status != NET_STATUS_ZOMBIE ) - { - int ret; - -#ifndef PUBLIC_SERVER - if( isParamFlag("--send") ) - { - printf("send -> %s", msg); - } -#endif - -#ifdef SUPPORT_NET_UNIX_UDP - if( p->socket_udp->proto == PROTO_UDPv4 && - sock_server_udp != NULL && - sock_server_udp->proto == PROTO_UDPv4 ) - { - ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg)); - } - - if( p->socket_udp->proto == PROTO_UDPv6 && - sock_server_udp != NULL && - sock_server_udp->proto == PROTO_UDPv6 ) - { - ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg)); - } - - if( p->socket_udp->proto == PROTO_UDPv6 && - sock_server_udp_second != NULL && - sock_server_udp_second->proto == PROTO_UDPv6 ) - { - ret = writeUdpSocket(sock_server_udp_second, p->socket_udp, msg, strlen(msg)); - } -#endif - -#ifdef SUPPORT_NET_SDL_UDP - ret = writeSdlUdpSocket(sock_server_sdl_udp, p->socket_sdl_udp, msg, strlen(msg)); -#endif - } -} - -static void addMsgClient(client_t *p, char *msg, int type, int id) -{ - assert( p != NULL ); - assert( msg != NULL ); - - if( p->status != NET_STATUS_ZOMBIE ) - { - addMsgInCheckFront(p->listSendMsg, msg, type, id); - } -} - -static void addMsgAllClientBut(char *msg, client_t *p, int type, int id) -{ - client_t *thisClient; - int i; - - assert( msg != NULL ); - - for( i = 0 ; i < listClient->count; i++) - { - thisClient = (client_t *) listClient->list[i]; - - if( thisClient->tux != NULL && thisClient != p ) - { - addMsgClient(thisClient, msg, type, id); - } - } -} - -static void addMsgAllClient(char *msg, int type, int id) -{ - assert( msg != NULL ); - - addMsgAllClientBut(msg, NULL, type, id); -} - -static void addMsgAllClientSeesTux(char *msg, tux_t *tux, int type, int id) -{ - int i; - - assert( msg != NULL ); - - for( i = 0 ; i < listClient->count ; i++ ) - { - client_t *thisClient; - tux_t *thisTux; - - thisClient = (client_t *)listClient->list[i]; - thisTux = (tux_t *)thisClient->tux; - - if( thisTux == NULL ) - { - continue; - } - - if( tux != thisTux && - isTuxSeesTux(thisTux, tux) ) - { - addMsgClient(thisClient, msg, type, id); - } - } -} - -void protoSendClient(int type, client_t *client, char *msg, int type2, int id) -{ - assert( msg != NULL ); - - switch( type ) - { - case PROTO_SEND_ONE : - assert( client != NULL ); - addMsgClient(client, msg, type2, id); - break; - case PROTO_SEND_ALL : - assert( client == NULL ); - addMsgAllClient(msg, type2, id); - break; - case PROTO_SEND_BUT : - assert( client != NULL ); - addMsgAllClientBut(msg, client, type2, id); - break; - case PROTO_SEND_ALL_SEES_TUX : -#ifndef PUBLIC_SERVER - if( client != NULL ) - { - addMsgAllClientSeesTux(msg, client->tux, type2, id); - } - else - { - addMsgAllClientSeesTux(msg, getControlTux(TUX_CONTROL_KEYBOARD_RIGHT), type2, id); - } -#endif -#ifdef PUBLIC_SERVER - addMsgAllClientSeesTux(msg, client->tux, type2, id); -#endif - break; - default : - assert( ! "Premenna type ma zlu hodnotu !" ); - break; - } -} - -tux_t *getServerTux() -{ - return NULL; -} - -client_t *getClientFromTux(tux_t *tux) -{ - client_t *thisClient; - int i; - - for( i = 0 ; i < listClient->count; i++) - { - thisClient = (client_t *) listClient->list[i]; - - if( thisClient->tux == tux ) - { - return thisClient; - } - } - - return NULL; -} - -void sendInfoCreateClient(client_t *client) -{ - client_t *thisClient; - tux_t *thisTux; - item_t *thisItem; - int i; - - assert( client != NULL ); - - proto_send_init_server(PROTO_SEND_ONE, client, client); - -#ifndef PUBLIC_SERVER - proto_send_newtux_server(PROTO_SEND_ONE, client, getControlTux(TUX_CONTROL_KEYBOARD_RIGHT) ); -#endif - - for( i = 0 ; i < listClient->count; i++) - { - thisClient = (client_t *) listClient->list[i]; - thisTux = thisClient->tux; - - if( thisTux != NULL && thisTux != client->tux ) - { - proto_send_newtux_server(PROTO_SEND_ONE, thisClient, client->tux); - proto_send_newtux_server(PROTO_SEND_ONE, client, thisTux); - } - } - - for( i = 0 ; i < getCurrentArena()->spaceItem->list->count; i++) - { - thisItem = (item_t *) getCurrentArena()->spaceItem->list->list[i]; - proto_send_additem_server(PROTO_SEND_ONE, client, thisItem); - } -} - -#ifdef SUPPORT_NET_UNIX_UDP - -static void eventCreateNewUdpClient(sock_udp_t *socket_udp) -{ - client_t *client; - - assert( socket_udp != NULL ); - - client = newUdpClient( socket_udp ); - addList(listClient, client); -} - -#endif - -#ifdef SUPPORT_NET_SDL_UDP - -static void eventCreateNewSdlUdpClient(sock_sdl_udp_t *socket_sdl_udp) -{ - client_t *client; - - assert( socket_sdl_udp != NULL ); - - client = newSdlUdpClient( socket_sdl_udp ); - addList(listClient, client); -} - -#endif - -static void eventClientBuffer(client_t *client) -{ - char *line; - int i; - - assert( client != NULL ); - - /* obsluha udalosti od clientov */ - - //while( getBufferLine(client->listRecvMsg, line, STR_PROTO_SIZE) >= 0 ) - for( i = 0 ; i < client->listRecvMsg->count ; i++ ) - { - line = (char *)client->listRecvMsg->list[i]; - -#ifndef PUBLIC_SERVER - if( isParamFlag("--recv") ) - { - printf("recv -> %s", line); - } -#endif - - rereshLastPing(client->protect); - - if( strncmp(line, "hello", 5) == 0 ) - { - proto_recv_hello_server(client, line); - continue; - } - - if( strncmp(line, "status", 6) == 0 ) - { - proto_recv_status_server(client, line); - continue; - } - -#ifdef PUBLIC_SERVER - if( strncmp(line, "list", 4) == 0 ) - { - proto_recv_listscore_server(client, line); - continue; - } -#endif - - if( client->tux != NULL ) - { - if( strncmp(line, "event", 5) == 0 ) - { - proto_recv_event_server(client, line); - continue; - } - - if( strncmp(line, "check", 5) == 0 ) - { - proto_recv_check_server(client, line); - continue; - } - - if( strncmp(line, "ping", 4) == 0 ) - { - proto_recv_ping_server(client, line); - continue; - } - - if( strncmp(line, "end", 3) == 0 ) - { - proto_recv_end_server(client, line); - continue; - } - } - - if( client->tux != NULL ) - { - proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_COMMAND); - } - } - - destroyListItem(client->listRecvMsg, free); - client->listRecvMsg = newList(); -} - -void eventClientListBuffer() -{ - int i; - client_t *thisClient; - - for( i = 0 ; i < listClient->count; i++) - { - thisClient = (client_t *) listClient->list[i]; - eventClientBuffer(thisClient); - eventMsgInCheckFront(thisClient); - } -} - -#ifdef SUPPORT_NET_UNIX_UDP - -static client_t* findUdpClient(sock_udp_t *sock_udp) -{ - int i; - client_t *thisClient; - - for( i = 0 ; i < listClient->count; i++) - { - thisClient = (client_t *) listClient->list[i]; - - if( getSockUdpPort(thisClient->socket_udp) == getSockUdpPort(sock_udp) ) - { - return thisClient; - } - } - - return NULL; -} - -static void eventClientUdpSelect(sock_udp_t *sock_server) -{ - sock_udp_t *sock_client; - client_t *client; - char listRecvMsg[STR_SIZE]; - bool_t isCreateNewClient; - int ret; - - assert( sock_server != NULL ); - - sock_client = newSockUdp(sock_server->proto); - isCreateNewClient = FALSE; - - memset(listRecvMsg, 0, STR_SIZE); - - ret = readUdpSocket(sock_server, sock_client, listRecvMsg, STR_SIZE-1); - - client = findUdpClient(sock_client); - - if( client == NULL ) - { - if( getCurrentArena()->spaceTux->list->count+1 > maxClients ) - { - destroySockUdp(sock_client); - return; - } - - eventCreateNewUdpClient(sock_client); - client = findUdpClient(sock_client); - isCreateNewClient = TRUE; - } - - if( client == NULL ) - { - fprintf(stderr, "Client total not found !\n"); - return; - } - - if( isCreateNewClient == FALSE ) - { - destroySockUdp(sock_client); - } - - if( ret <= 0 ) - { - client->status = NET_STATUS_ZOMBIE; - return; - } - - //printf("add packet >>%s<<\n", listRecvMsg); - addList(client->listRecvMsg, strdup(listRecvMsg) ); -} - -int selectServerUdpSocket() -{ - struct timeval tv; - fd_set readfds; - fd_set errorfds; - int max_fd; - int ret; - int count; - -#ifndef PUBLIC_SERVER - tv.tv_sec = 0; - tv.tv_usec = 0; -#endif - -#ifdef PUBLIC_SERVER - tv.tv_sec = 0; - tv.tv_usec = 5000; -#endif - - FD_ZERO(&readfds); - FD_ZERO(&errorfds); - - max_fd = 0; - count = 0; - - if( sock_server_udp != NULL ) - { - FD_SET(sock_server_udp->sock, &errorfds); - FD_SET(sock_server_udp->sock, &readfds); - - if( sock_server_udp->sock > max_fd ) - { - max_fd = sock_server_udp->sock; - } - } - - if( sock_server_udp_second != NULL ) - { - FD_SET(sock_server_udp_second->sock, &readfds); - FD_SET(sock_server_udp_second->sock, &errorfds); - - if( sock_server_udp_second->sock > max_fd ) - { - max_fd = sock_server_udp_second->sock; - } - } - - //printf("select..\n"); - -#ifdef PUBLIC_SERVER - if( listClient->count == 0 ) - { - ret = select(max_fd+1, &readfds, (fd_set *)NULL, &errorfds, NULL); - setServerTimer(); - } - else - { - ret = select(max_fd+1, &readfds, (fd_set *)NULL,&errorfds, &tv); - } -#endif - -#ifndef PUBLIC_SERVER - ret = select(max_fd+1, &readfds, (fd_set *)NULL,&errorfds, &tv); -#endif - - if( ret < 0 ) - { - //printf("select ret = %d\n", ret); - return 0; - } - - if( sock_server_udp != NULL ) - { - if( FD_ISSET(sock_server_udp->sock, &readfds) ) - { - eventClientUdpSelect(sock_server_udp); - count = 1; - } - - if( FD_ISSET(sock_server_udp->sock, &errorfds) ) - { - printf("error\n"); - } - } - - if( sock_server_udp_second != NULL ) - { - if( FD_ISSET(sock_server_udp_second->sock, &readfds) ) - { - eventClientUdpSelect(sock_server_udp_second); - count = 1; - } - - if( FD_ISSET(sock_server_udp_second->sock, &errorfds) ) - { - printf("error\n"); - } - } - - return count; -} - -#endif - -#ifdef SUPPORT_NET_SDL_UDP - -static client_t* findSdlUdpClient(sock_sdl_udp_t *sock_sdl_udp) -{ - int i; - client_t *thisClient; - - for( i = 0 ; i < listClient->count; i++) - { - thisClient = (client_t *) listClient->list[i]; - - if( getSockSdlUdpPort(thisClient->socket_sdl_udp) == getSockSdlUdpPort(sock_sdl_udp) ) - { - return thisClient; - } - } - - return NULL; -} - -void selectServerSdlUdpSocket() -{ - sock_sdl_udp_t *sock_client; - client_t *client; - char listRecvMsg[STR_PROTO_SIZE]; - bool_t isCreateNewClient; - int ret; - - assert( sock_server_sdl_udp != NULL ); - - do{ - sock_client = newSdlSockUdp(); - isCreateNewClient = FALSE; - - memset(listRecvMsg, 0, STR_PROTO_SIZE); - - ret = readSdlUdpSocket(sock_server_sdl_udp, sock_client, listRecvMsg, STR_PROTO_SIZE-1); - - if( ret < 0 ) - { - destroySockSdlUdp(sock_client); - break; - } - - client = findSdlUdpClient(sock_client); - - if( client == NULL ) - { - eventCreateNewSdlUdpClient(sock_client); - client = findSdlUdpClient(sock_client); - isCreateNewClient = TRUE; - } - - if( client == NULL ) - { - fprintf(stderr, "Client total not found !\n"); - return; - } - - if( isCreateNewClient == FALSE ) - { - destroySockSdlUdp(sock_client); - } - - if( ret <= 0 ) - { - client->status = NET_STATUS_ZOMBIE; - return; - } - - addList(client->listRecvMsg, strdup(listRecvMsg) ); - }while( ret > 0 ); -} - -#endif - -void eventServer() -{ -#ifndef PUBLIC_SERVER - - int count; - -#ifdef SUPPORT_NET_SDL_UDP - selectServerSdlUdpSocket(); -#endif - -#ifdef SUPPORT_NET_UNIX_UDP - do{ - count = selectServerUdpSocket(); - }while( count > 0 ); -#endif - -#endif - -#ifdef PUBLIC_SERVER - selectServerUdpSocket(); -#endif - - eventClientListBuffer(); - eventTimer(listServerTimer); -} - -static void quitServer() -{ - proto_send_end_server(PROTO_SEND_ALL, NULL); - - assert( listClient != NULL ); - destroyListItem(listClient, destroyClient); - destroyTimer(listServerTimer); -} - -#ifdef SUPPORT_NET_UNIX_UDP - -void quitUdpServer() -{ - quitServer(); - - if( sock_server_udp != NULL ) - { - printf("close port %d\n", getSockUdpPort(sock_server_udp)); - closeUdpSocket(sock_server_udp); - } - - if( sock_server_udp_second != NULL ) - { - printf("close port %d\n", getSockUdpPort(sock_server_udp_second)); - closeUdpSocket(sock_server_udp_second); - } - - printf("quit UDP port\n"); -} - -#endif - -#ifdef SUPPORT_NET_SDL_UDP - -void quitSdlUdpServer() -{ - quitServer(); - - assert( sock_server_sdl_udp != NULL ); - closeSdlUdpSocket(sock_server_sdl_udp); - - printf("quit UDP port\n"); -} - -#endif diff --git a/src/server/Makefile b/src/server/Makefile new file mode 100644 index 0000000..41e140e --- /dev/null +++ b/src/server/Makefile @@ -0,0 +1,15 @@ + + +all: heightScore.o publicServer.o serverConfigFile.o + +heightScore.o: heightScore.c heightScore.h + $(CC) $(CFLAGS) -o heightScore.o -c heightScore.c + +publicServer.o: publicServer.c publicServer.h + $(CC) $(CFLAGS) -o publicServer.o -c publicServer.c + +serverConfigFile.o: serverConfigFile.c serverConfigFile.h + $(CC) $(CFLAGS) -o serverConfigFile.o -c serverConfigFile.c + +clean: + rm -rf *.o diff --git a/src/server/heightScore.c b/src/server/heightScore.c new file mode 100644 index 0000000..f36dcf7 --- /dev/null +++ b/src/server/heightScore.c @@ -0,0 +1,98 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/textFile.h" +#include "server/heightScore.h" + +static textFile_t *textFile; + +void initHeightScore(char *file) +{ + int i; + + textFile = loadTextFile(file); + + if( textFile == NULL ) + { + fprintf(stderr, "I can't load %s !\n", file); + fprintf(stderr, "I create %s\n", file); + textFile = newTextFile(file); + } + else + { + return; + } + + if( textFile == NULL ) + { + fprintf(stderr, "I can't create %s!\n", file); + return; + } + + for( i = 0 ; i < HEIGHTSCORE_MAX_PLAYERS ; i++ ) + { + addList(textFile->text, strdup("no_name 0") ); + } + + saveTextFile(textFile); +} + +int addPlayerInHighScore(char *name, int score) +{ + int i; + + if( score <= 0 ) + { + return -1; // Ha ha ha + } + + for( i = 0 ; i < HEIGHTSCORE_MAX_PLAYERS ; i++ ) + { + char *line; + char thisName[STR_NAME_SIZE]; + int thisCore; + + line = (char *) textFile->text->list[i]; + sscanf(line, "%s %d", thisName, &thisCore); + + if( score >= thisCore ) + { + char new[STR_SIZE]; + + sprintf(new, "%s %d", name, score); + insList(textFile->text, i, strdup(new) ); + delListItem(textFile->text, HEIGHTSCORE_MAX_PLAYERS, free); + saveTextFile(textFile); + + return 0; + } + } + + return -1; +} + +char* getTableItem(int index) +{ + if( textFile != NULL && + index >= 0 && + index < textFile->text->count ) + { + return (char *)textFile->text->list[index]; + } + + return NULL; +} + +void quitHeightScore() +{ + if( textFile != NULL ) + { + saveTextFile(textFile); + destroyTextFile(textFile); + } +} diff --git a/src/server/heightScore.h b/src/server/heightScore.h new file mode 100644 index 0000000..b1bf7e2 --- /dev/null +++ b/src/server/heightScore.h @@ -0,0 +1,13 @@ + +#ifndef HEIGHT_SCORE_H + +#define HEIGHT_SCORE_H + +#define HEIGHTSCORE_MAX_PLAYERS 100 + +extern void initHeightScore(char *file); +extern int addPlayerInHighScore(char *name, int score); +extern char* getTableItem(int index); +extern void quitHeightScore(); + +#endif diff --git a/src/server/logFile.c b/src/server/logFile.c new file mode 100644 index 0000000..12a4df8 --- /dev/null +++ b/src/server/logFile.c @@ -0,0 +1,46 @@ + +#include +#include +#include +#include +#include + +typedef struct logFile_struct +{ + char *name; + FILE *file; +} logFile_t; + +logFile_t* newLogFile(char *s) +{ + logFile_t *new; + + assert( s != NULL ); + + new = malloc( sizeof(logFile_t) ); + memset(new, 0, sizeof(logFile_t) ); + + new->name = strdup(s); + new->file = fopen(new->name, "a+"); + + assert( new->file != NULL ); + + return new; +} + +int writeLog(logFile_t *p, char *s) +{ + assert( p != NULL ); + assert( s != NULL ); + + return fprintf(p->file, s); +} + +void destroyLogFile(logFile_t *p) +{ + assert( p != NULL ); + + free(p->name); + fclose(p->file); + free(p); +} diff --git a/src/server/logFile.h b/src/server/logFile.h new file mode 100644 index 0000000..12f5606 --- /dev/null +++ b/src/server/logFile.h @@ -0,0 +1,18 @@ + +#ifndef LOGFILE_H + +#define LOGFILE_H + +#include "main.h" + +typedef struct logFile_struct +{ + char *name; + FILE *file; +} logFile_t; + +extern logFile_t* newLogFile(char *s); +extern int writeLog(logFile_t *p, char *s); +extern void destroyLogFile(logFile_t *p); + +#endif diff --git a/src/server/publicServer.c b/src/server/publicServer.c new file mode 100644 index 0000000..0bd88ac --- /dev/null +++ b/src/server/publicServer.c @@ -0,0 +1,192 @@ + +#include +#include +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/idManager.h" +#include "base/myTimer.h" +#include "base/arena.h" +#include "base/item.h" +#include "base/shot.h" +#include "base/arenaFile.h" +#include "base/proto.h" +#include "base/modules.h" +#include "base/net_multiplayer.h" + +#include "server/publicServer.h" +#include "server/serverConfigFile.h" +#include "server/heightScore.h" + +static int arenaId; +static arena_t *arena; +static bool_t isSignalEnd; + +void countRoundInc() +{ +} + +static char *getSetting(char *env, char *param, char *default_val) +{ + return getParamElse(param, getServerConfigFileValue(env, default_val) ); +} + +static int initPublicServerNetwork() +{ + char ip4[STR_IP_SIZE]; + char ip6[STR_IP_SIZE]; + char *p_ip4, *p_ip6; + int port4; + int port6; + int ret; + + ret = -1; + + strcpy(ip4, getSetting("IP4", "--ip4", "none" ) ); + strcpy(ip6, getSetting("IP6", "--ip6", "none" ) ); + + p_ip4 = ip4; + + if( strcmp(ip4, "none") == 0 ) + { + p_ip4 = NULL; + } + + p_ip6 = ip6; + + if( strcmp(ip6, "none") == 0 ) + { + p_ip6= NULL; + } + + port4 = atoi( getSetting("PORTv4", "--port", "2200") ); + port6 = atoi( getSetting("PORTv6", "--port", "2200") ); + + ret = initNetMulitplayerPublicServer(p_ip4, port4, p_ip6, port6); + + return ret; +} + +int initPublicServer() +{ + int ret; + + initListID(); + initModule(); + initArenaFile(); + initTux(); + initItem(); + initShot(); + initServerConfigFile(); + initHeightScore( getSetting("SCOREFILE", "--scorefile", "./heightscore") ); + + arenaId = getArenaIdFormNetName( getSetting("ARENA", "--arena", "FAGN") ); + arena = getArena(arenaId); + setCurrentArena(arena); + + addNewItem(arena->spaceItem, ID_UNKNOWN); + isSignalEnd = FALSE; + + ret = initPublicServerNetwork(); + + if( ret < 0 ) + { + printf("Nemozem inicalizovat sietovy socket !\n"); + return -1; + } + + setServerMaxClients( atoi( getSetting("MAX_CLIENTS", "--maxclients", "32") )); + + return 0; +} + +int getChoiceArenaId() +{ + return arenaId; +} + +void eventPublicServer() +{ + static my_time_t lastActive = 0; + my_time_t interval; + + eventNetMultiplayer(); + + if( isSignalEnd == TRUE ) + { + quitPublicServer(); + } + + if( lastActive == 0 ) + { + lastActive = getMyTime(); + } + + interval = getMyTime() - lastActive; + + if( interval < 50 ) + { + return; + } + +/* + printf("interval = %d\n", interval); +*/ + lastActive = getMyTime(); + + //fce_interval(); + eventArena(arena); +} + +void my_handler_quit(int n) +{ + printf("my_handler_quit\n"); + isSignalEnd = TRUE; +} + +void quitPublicServer() +{ + printf("quit public server\n"); + + quitNetMultiplayer(); + destroyArena(arena); + + quitTux(); + quitItem(); + quitShot(); + + quitArenaFile(); + quitServerConfigFile(); + quitModule(); + quitListID(); + quitHeightScore(); + destroyList(listHelp); + + exit(0); +} + +int startPublicServer() +{ + signal(SIGPIPE, SIG_IGN); + signal(SIGINT, my_handler_quit); + signal(SIGTERM, my_handler_quit); + signal(SIGQUIT, my_handler_quit); + + if( initPublicServer() < 0 ) + { + quitPublicServer(); + return -1; + } + + while(1) + { + eventPublicServer(); + } + + return 0; +} diff --git a/src/server/publicServer.h b/src/server/publicServer.h new file mode 100644 index 0000000..111ff9c --- /dev/null +++ b/src/server/publicServer.h @@ -0,0 +1,18 @@ + +#ifndef PUBLIC_SERVER_H + +#define PUBLIC_SERVER_H + +#include "base/arena.h" + +#define WORLD_COUNT_ROUND_UNLIMITED -1 + +extern void countRoundInc(); +extern int initPublicServer(); +extern int getChoiceArenaId(); +extern void eventPublicServer(); +extern void my_handler_quit(int n); +extern void quitPublicServer(); +extern int startPublicServer(); + +#endif diff --git a/src/server/serverConfigFile.c b/src/server/serverConfigFile.c new file mode 100644 index 0000000..7f03019 --- /dev/null +++ b/src/server/serverConfigFile.c @@ -0,0 +1,86 @@ + +#include +#include +#include +#include + +#include "base/main.h" +#include "base/list.h" +#include "base/textFile.h" +#include "server/serverConfigFile.h" + +static textFile_t *serverTextFile; + +static void prepareConfigFile(textFile_t *ts) +{ + int i; + int j; + + for( i = 0 ; i < serverTextFile->text->count ; i++ ) + { + char *line; + int len; + + line = (char *)(serverTextFile->text->list[i]); + len = strlen(line); + + for( j = 0 ; j < len ; j++ ) + { + if( line[j] == ' ' ) // [TAB] + { + line[j] = ' '; + } + } + } + +} + +void initServerConfigFile() +{ + serverTextFile = loadTextFile("./server.conf"); + + if( serverTextFile == NULL ) + { + fprintf(stderr, "I dont load config file, I used default values\n"); + return; + } + + prepareConfigFile(serverTextFile); + +} +char* getServerConfigFileValue(char *env, char *s) +{ + int i; + int len; + + if( serverTextFile == NULL ) + { + return s; + } + + len = strlen(env); + + for( i = 0 ; i < serverTextFile->text->count ; i++ ) + { + char *line; + + line = (char *)(serverTextFile->text->list[i]); + + if( strncmp(line, env, len) == 0 ) + { + return strrchr(line,' ')+1; + } + } + + return s; +} + +void quitServerConfigFile() +{ + if( serverTextFile != NULL ) + { + destroyTextFile(serverTextFile); + } +} + + diff --git a/src/server/serverConfigFile.h b/src/server/serverConfigFile.h new file mode 100644 index 0000000..d016807 --- /dev/null +++ b/src/server/serverConfigFile.h @@ -0,0 +1,11 @@ + + +#ifndef SERVER_CONFIG_FILE_H + +#define SERVER_CONFIG_FILE_H + +extern void initServerConfigFile(); +extern char* getServerConfigFileValue(char *env, char *s); +extern void quitServerConfigFile(); + +#endif diff --git a/src/serverConfigFile.c b/src/serverConfigFile.c deleted file mode 100644 index 8640333..0000000 --- a/src/serverConfigFile.c +++ /dev/null @@ -1,86 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "textFile.h" -#include "serverConfigFile.h" - -static textFile_t *serverTextFile; - -static void prepareConfigFile(textFile_t *ts) -{ - int i; - int j; - - for( i = 0 ; i < serverTextFile->text->count ; i++ ) - { - char *line; - int len; - - line = (char *)(serverTextFile->text->list[i]); - len = strlen(line); - - for( j = 0 ; j < len ; j++ ) - { - if( line[j] == ' ' ) // [TAB] - { - line[j] = ' '; - } - } - } - -} - -void initServerConfigFile() -{ - serverTextFile = loadTextFile("./server.conf"); - - if( serverTextFile == NULL ) - { - fprintf(stderr, "I dont load config file, I used default values\n"); - return; - } - - prepareConfigFile(serverTextFile); - -} -char* getServerConfigFileValue(char *env, char *s) -{ - int i; - int len; - - if( serverTextFile == NULL ) - { - return s; - } - - len = strlen(env); - - for( i = 0 ; i < serverTextFile->text->count ; i++ ) - { - char *line; - - line = (char *)(serverTextFile->text->list[i]); - - if( strncmp(line, env, len) == 0 ) - { - return strrchr(line,' ')+1; - } - } - - return s; -} - -void quitServerConfigFile() -{ - if( serverTextFile != NULL ) - { - destroyTextFile(serverTextFile); - } -} - - diff --git a/src/shot.c b/src/shot.c deleted file mode 100644 index 50e6e9a..0000000 --- a/src/shot.c +++ /dev/null @@ -1,369 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "shot.h" -#include "gun.h" -#include "net_multiplayer.h" -#include "proto.h" -#include "idManager.h" - -#ifndef PUBLIC_SERVER -#include "image.h" -#include "screen_world.h" -#include "layer.h" -#endif - -#ifdef PUBLIC_SERVER -#include "publicServer.h" -#endif - -#ifndef PUBLIC_SERVER - -static SDL_Surface *g_shot_simple; -static SDL_Surface *g_shot_lasserX; -static SDL_Surface *g_shot_lasserY; -static SDL_Surface *g_shot_bombball; - -#endif - -static bool_t isShotInit = FALSE; - -bool_t isShotInicialized() -{ - return isShotInit; -} - -void initShot() -{ -#ifndef PUBLIC_SERVER - assert( isImageInicialized() == TRUE ); -#endif - -#ifndef PUBLIC_SERVER - - g_shot_simple = addImageData("shot.png", IMAGE_ALPHA, "shot", IMAGE_GROUP_BASE); - g_shot_lasserX = addImageData("lasserX.png", IMAGE_NO_ALPHA, "lasserX", IMAGE_GROUP_BASE); - g_shot_lasserY = addImageData("lasserY.png", IMAGE_NO_ALPHA, "lasserY", IMAGE_GROUP_BASE); - g_shot_bombball = addImageData("bombball.png", IMAGE_ALPHA, "bombball", IMAGE_GROUP_BASE); - -#endif - - isShotInit = TRUE; -} - -shot_t* newShot(int x,int y, int px, int py, int gun, int author_id) -{ - shot_t *new; - tux_t *author; - - new = malloc( sizeof(shot_t) ); - memset(new, 0, sizeof(shot_t) ); - - new->id = getNewID(); - new->x = x; - new->y = y; - new->px = px; - new->py = py; - new->gun = gun; - new->author_id = author_id; - - new->position = POSITION_UNKNOWN; - - author = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, author_id); - - if( author != NULL ) - { - new->position = author->position; - } - - new->isCanKillAuthor = FALSE; - - switch( gun ) - { - case GUN_SIMPLE : - new->w = GUN_SHOT_WIDTH; - new->h = GUN_SHOT_HEIGHT; -#ifndef PUBLIC_SERVER - new->img = g_shot_simple; -#endif - break; - - case GUN_LASSER : - switch( author-> position ) - { - case TUX_RIGHT : - case TUX_LEFT : - new->w = GUN_LASSER_HORIZONTAL; - new->h = GUN_SHOT_VERTICAL; -#ifndef PUBLIC_SERVER - new->img = g_shot_lasserX; -#endif - break; - case TUX_UP : - case TUX_DOWN : - new->w = GUN_SHOT_VERTICAL; - new->h = GUN_LASSER_HORIZONTAL; -#ifndef PUBLIC_SERVER - new->img = g_shot_lasserY; -#endif - break; - } - break; - - case GUN_BOMBBALL : - new->w = GUN_BOMBBALL_WIDTH; - new->h = GUN_BOMBBALL_HEIGHT; - -#ifndef PUBLIC_SERVER - new->img = g_shot_bombball; -#endif - break; - - default : - assert( ! "Premenna weapon ma zlu hodnotu !" ); - break; - } - - return new; -} - -void replaceShotID(shot_t *shot, int id) -{ - replaceID(shot->id, id); - shot->id = id; -} - - -void getStatusShot(void *p, int *id, int *x,int *y, int *w, int *h) -{ - shot_t *shot; - - shot = p; - *id = shot->id; - *x = shot->x; - *y = shot->y; - *w = shot->w; - *h = shot->h; -} - -void setStatusShot(void *p, int x, int y, int w, int h) -{ - shot_t *shot; - - shot = p; - shot->x = x; - shot->y = y; - shot->w = w; - shot->h = h; -} - -#ifndef PUBLIC_SERVER - -void drawShot(shot_t *p) -{ - assert( p != NULL ); - addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, TUX_LAYER); -} - -void drawListShot(list_t *listShot) -{ - shot_t *thisShot; - int i; - - assert( listShot != NULL ); - - for( i = 0 ; i < listShot->count ; i++ ) - { - thisShot = (shot_t *)listShot->list[i]; - assert( thisShot != NULL ); - drawShot(thisShot); - } -} - -#endif - -static int getRandomCourse(int x, int y) -{ - int ret; - - do{ - switch( random() % 3 ) - { - case 0 : - ret = y; - break; - case 1 : - ret = -y; - break; - case 2 : - ret = 0; - break; - } - }while( ret == x ); - - return ret; -} - -void boundBombBall(shot_t *shot) -{ - if( shot->gun != GUN_BOMBBALL ) - { - return; - } - - shot->px = getRandomCourse(shot->px, +10); - shot->py = getRandomCourse(shot->py, +10); - shot->isCanKillAuthor = 1; - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_shot_server(PROTO_SEND_ALL, NULL, shot); - } -} - -void transformOnlyLasser(shot_t *shot) -{ - switch( shot->position ) - { - case TUX_RIGHT : - case TUX_LEFT : - shot->w = GUN_LASSER_HORIZONTAL; - shot->h = GUN_SHOT_VERTICAL; -#ifndef PUBLIC_SERVER - shot->img = g_shot_lasserX; -#endif - break; - case TUX_UP : - case TUX_DOWN : - shot->w = GUN_SHOT_VERTICAL; - shot->h = GUN_LASSER_HORIZONTAL; -#ifndef PUBLIC_SERVER - shot->img = g_shot_lasserY; -#endif - break; - } -} - -void eventMoveListShot(arena_t *arena) -{ - shot_t *thisShot; - int new_x, new_y; - int i; - - for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) - { - thisShot = (shot_t *) arena->spaceShot->list->list[i]; - assert( thisShot != NULL ); - - new_x = thisShot->x + thisShot->px; - new_y = thisShot->y + thisShot->py; - - moveObjectInSpace(getCurrentArena()->spaceShot, thisShot, new_x, new_y); - - if( thisShot->x+thisShot->w < 0 || thisShot->x > arena->w || - thisShot->y+thisShot->h < 0 || thisShot->y > arena->h ) - { - delObjectFromSpaceWithObject(arena->spaceShot, - thisShot, destroyShot); - i--; - continue; - } - } -} - -static int myAbs(int n) -{ - return ( n > 0 ? n : -n ); -} - -static int getSppedShot(shot_t *shot) -{ - return ( myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py) ); -} - - -void checkShotIsInTuxScreen(arena_t *arena) -{ - int screen_x, screen_y; - shot_t *thisShot; - tux_t *thisTux; - int speed; - int i, j; - - if( getNetTypeGame() != NET_GAME_TYPE_SERVER ) - { - return; - } - - for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) - { - thisTux = (tux_t *)arena->spaceTux->list->list[i]; - speed = 25; - - if( thisTux->control != TUX_CONTROL_NET ) - { - continue; - } - - getCenterScreen(&screen_x, &screen_y, thisTux->x, thisTux->y); - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceShot, screen_x-speed, screen_y, speed, WINDOW_SIZE_Y, listHelp); - - for( j = 0 ; j count ; j++ ) - { - thisShot = (shot_t *)listHelp->list[j]; - client_t *thisClient; - thisClient = getClientFromTux(thisTux); - proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); - } - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceShot, screen_x+WINDOW_SIZE_X, screen_y, speed, WINDOW_SIZE_Y, listHelp); - - for( j = 0 ; j count ; j++ ) - { - thisShot = (shot_t *)listHelp->list[j]; - client_t *thisClient; - thisClient = getClientFromTux(thisTux); - proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); - } - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceShot, screen_x, screen_y-speed, WINDOW_SIZE_X, speed, listHelp); - - for( j = 0 ; j count ; j++ ) - { - thisShot = (shot_t *)listHelp->list[j]; - client_t *thisClient; - thisClient = getClientFromTux(thisTux); - proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); - } - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceShot, screen_x, screen_y+WINDOW_SIZE_Y, WINDOW_SIZE_X, speed, listHelp); - - for( j = 0 ; j count ; j++ ) - { - thisShot = (shot_t *)listHelp->list[j]; - client_t *thisClient; - thisClient = getClientFromTux(thisTux); - proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); - } - } -} - -void destroyShot(shot_t *p) -{ - assert( p != NULL ); - delID(p->id); - free(p); -} - -void quitShot() -{ - isShotInit = FALSE; -} diff --git a/src/sound.c b/src/sound.c deleted file mode 100644 index a35d996..0000000 --- a/src/sound.c +++ /dev/null @@ -1,137 +0,0 @@ - -#include -#include -#include - -#include "main.h" -#include "interface.h" -#include "list.h" -#include "path.h" -#include "string_length.h" -#include "audio.h" -#include "sound.h" -#include "storage.h" - -static list_t *listStorage; - -static bool_t isSoundInit = FALSE; -static bool_t var_isSoundActive = TRUE; - -bool_t isSoundInicialized() -{ - return isSoundInit; -} - -void initSound() -{ - if( isAudioInicialized() == FALSE ) - { - isSoundInit = FALSE; - return; - } - - listStorage = newStorage(); - isSoundInit = TRUE; - var_isSoundActive = TRUE; - - if( isParamFlag("--nosound") ) - { - setSoundActive(FALSE); - } - - if( isParamFlag("--sound") ) - { - setSoundActive(TRUE); - } - - printf("init sound..\n"); -} - -static Mix_Chunk* loadMixSound(char *file) -{ - Mix_Chunk *new; - char str[STR_PATH_SIZE]; - - - printf("load sound %s..\n", file); - sprintf(str, PATH_SOUND "%s", file); - - accessExistFile(str); - - new = Mix_LoadWAV(str); - - if( new == NULL ) - { - fprintf(stderr, "Nelze nahrat zvuk %s : %s\n", str, Mix_GetError()); - return NULL; - } - - return new; -} - - -static void playMixSound(Mix_Chunk *p) -{ - if( Mix_PlayChannel(-1, p, 0) == -1 ) - { - fprintf(stderr, "Nelze prehrat zvuk : %s\n", Mix_GetError()); - return; - } -} - -static void destroySound(void *p) -{ - Mix_FreeChunk((Mix_Chunk *)p); -} - -void addSound(char *file, char *name, char *group) -{ - Mix_Chunk *new; - - if( isSoundInit == FALSE ) - { - return; - } - - assert( file != NULL ); - assert( name != NULL ); - assert( group != NULL ); - - new = loadMixSound(file); - - addItemToStorage(listStorage, group, name, new ); -} - -void playSound(char *name, char *group) -{ - if( isSoundInit == FALSE || - var_isSoundActive == FALSE ) - { - return; - } - - playMixSound( getItemFromStorage(listStorage, group, name) ); -} - -void setSoundActive(bool_t n) -{ - var_isSoundActive = n; -} - -bool_t isSoundActive() -{ - return var_isSoundActive; -} - -void quitSound() -{ - if( isSoundInit == FALSE ) - { - return; - } - - destroyStorage(listStorage, destroySound); - isSoundInit = FALSE; - printf("quit sound..\n"); -} - diff --git a/src/space.c b/src/space.c deleted file mode 100644 index 0d3060a..0000000 --- a/src/space.c +++ /dev/null @@ -1,600 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "space.h" - -#define DEBUG_SPACE -#define ERROR_SUPPORT - -#ifdef DEBUG_SPACE - - -typedef struct recv_struct -{ - int id; - int x, y; - int w, h; -} recv_t; - -recv_t* newRecv(int id, int x , int y, int w, int h) -{ - recv_t *new; - - new = malloc( sizeof(recv_t) ); - memset(new, 0, sizeof(recv_t)); - - new->id = id; - new->x = x; - new->y = y; - new->w = w; - new->h = h; - - return new; -} - -void getStatus(void *p, int *id, int *x, int *y, int *w, int *h) -{ - recv_t *recv; - - recv = p; - - *id = recv->id; - *x = recv->x; - *y = recv->y; - *w = recv->w; - *h = recv->h; -} - -void setStatus(void *p, int x, int y, int w, int h) -{ - recv_t *recv; - - recv = p; - - recv->x = x; - recv->y = y; - recv->w = w; - recv->h = h; - recv->x = x; -} - -void destroyRecv(recv_t *p) -{ - free(p); -} - -#endif - -static int my_conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2) -{ - return (x1w = w / segW + 1; - new->h = h / segH + 1; - new->segW = segW; - new->segH = segH; - new->getStatus = getStatus; - new->setStatus = setStatus; - new->list = newList(); - - new->area = malloc( new->w * sizeof(list_t **) ); - - for( i = 0 ; i < new->w ; i++ ) - { - new->area[i] = malloc( new->h * sizeof(list_t *) ); - } - - for( i = 0 ; i < new->h ; i++ ) - { - for( j = 0 ; j < new->w ; j++ ) - { - new->area[j][i] = newList(); - } - } - - return new; -} - -static void getSegment(space_t *p, int x, int y, int w, int h, - int *segX, int *segY, int *segW, int *segH) -{ - *segX = x / p->segW; - *segY = y / p->segH; - *segW = ( (x+w) / p->segW + 1 ) - *segX; - *segH = ( (y+h) / p->segH + 1 ) - *segY; -} - -#ifdef ERROR_SUPPORT - -void printListID(space_t *space) -{ - int i; - - for( i = 0 ; i < space->list->count ; i++ ) - { - int id, x, y, w, h; - - space->getStatus(space->list->list[i], &id, &x, &y, &w, &h); - printf("%d\n", id); - } - - putchar('\n'); -} - -static void checkList(space_t *space) -{ - int id, x, y, w, h; - int thisId; - int i; - - if( space->list->count == 0 ) - { - printf("nothing\n"); - return; - } - - space->getStatus(space->list->list[0], &thisId, &x, &y, &w, &h); - - for( i = 1 ; i < space->list->count ; i++ ) - { - space->getStatus(space->list->list[i], &id, &x, &y, &w, &h); - - if( id <= thisId ) - { - printListID(space); - assert( ! "error" ); - } - - thisId = id; - } -} - -static void addToListOnTheBaseIndex(space_t *space, void *p) -{ - int id, point_id, inc_point_id; - int x, y, w, h; - int len; - int min, max; - int point; - - - len = space->list->count; - - space->getStatus(p, &id, &x, &y, &w, &h); -/* - printf("addToListOnTheBaseIndex (%d)\n", id); - printListID(space); - assert( id >= 0 ); -*/ - if( len == 0 ) - { - //printf("OK first\n"); - addList(space->list, p); - return; - } - - if( len == 1 ) - { - space->getStatus(space->list->list[0], &point_id, &x, &y, &w, &h); - - if( id > point_id ) - { - addList(space->list, p); - } - - if( id < point_id ) - { - insList(space->list, 0, p); - } - - //printf("OK\n"); - - return; - } - - min = 0; - max = len-1; -/* - if( max < 0 ) - { - max = 0; - } -*/ - for(;;) - { - point = min + ( max - min ) / 2; -/* - space->getStatus(space->list->list[min], &id_min, &x, &y, &w, &h); - space->getStatus(space->list->list[max], &id_max, &x, &y, &w, &h); -*/ - //printf("min = %d max = %d point = %d len = %d id = %d\n", min, max, point, len, id); - - if( max < 0 ) - { - //printf("OK first\n"); - insList(space->list, 0, p); - //checkList(space); - return; - } - - if( point+1 >= len /*|| min >= len*/ ) - { - addList(space->list, p); - //printf("OK height\n"); - //checkList(space); - return; - } - - space->getStatus(space->list->list[point], &point_id, &x, &y, &w, &h); - space->getStatus(space->list->list[point+1], &inc_point_id, &x, &y, &w, &h); -/* - if( min == max ) - { - //printf("OK\n"); - insList(space->list, min, p); - checkList(space); - return; - } -*/ - if( point_id < id && id < inc_point_id ) - { - //printf("OK\n"); - insList(space->list, point+1, p); - //checkList(space); - return; - } - - if( id > inc_point_id ) - { - min = point + 1; - continue; - } - - if( id < point_id ) - { - max = point - 1; - continue; - } - } -} - -#endif - -void addObjectToSpace(space_t *p, void *item) -{ - int segX, segY, segW, segH; - int id, x, y, w, h; - int i, j; - - p->getStatus(item, &id, &x, &y, &w, &h); - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for( i = segY ; i < segY + segH ; i++ ) - { - for( j = segX ; j < segX + segW ; j++ ) - { - addList(p->area[j][i], item); - } - } - -#ifdef ERROR_SUPPORT - addToListOnTheBaseIndex(p, item); -#endif - -#ifndef ERROR_SUPPORT - addList(p->list, item); -#endif -} - -void getObjectFromSpace(space_t *p, int x, int y, int w, int h, list_t *list) -{ - int segX, segY, segW, segH; - int id, this_x, this_y, this_w, this_h; - int i, j, k; - - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for( i = segY ; i < segY + segH ; i++ ) - { - for( j = segX ; j < segX + segW ; j++ ) - { - void *this; - - for( k = 0 ; k < p->area[j][i]->count ; k++ ) - { - this = p->area[j][i]->list[k]; - p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); - - if( my_conflictSpace(x, y, w, h, this_x, this_y, this_w, this_h) && - searchListItem(list, this) == -1 ) - { - addList(list, this); - } - } - } - } -} - -int searchObjectInSpace(space_t *space, int id) -{ - int point_id; - int x, y, w, h; - int len; - int min, max; - int point; - -/* - printf("getObjectFromSpaceWithID %d\n", id); - printListID(space); -*/ - len = space->list->count; - - min = 0; - max = len-1; - - for(;;) - { - point = min + ( max - min ) / 2; - - //printf("min = %d max = %d point = %d len = %d id = %d\n", min, max, point, len, id); - - if( max < 0 || point >= len || max < min ) - { - return -1; - } - - space->getStatus(space->list->list[point], &point_id, &x, &y, &w, &h); - - if( min == max ) - { - space->getStatus(space->list->list[min], &point_id, &x, &y, &w, &h); - } - - if( point_id == id ) - { - return point; - } - - if( id > point_id ) - { - min = point + 1; - continue; - } - - if( id < point_id ) - { - max = point - 1; - continue; - } - } -} - -void* getObjectFromSpaceWithID(space_t *space, int id) -{ - int index; - - index = searchObjectInSpace(space, id); - - return ( index != -1 ? space->list->list[index] : NULL ); -} - -int isConflictWithObjectFromSpace(space_t *p, int x, int y, int w, int h) -{ - int segX, segY, segW, segH; - int id, this_x, this_y, this_w, this_h; - int i, j, k; - - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for( i = segY ; i < segY + segH ; i++ ) - { - for( j = segX ; j < segX + segW ; j++ ) - { - void *this; - - for( k = 0 ; k < p->area[j][i]->count ; k++ ) - { - this = p->area[j][i]->list[k]; - p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); - - if( my_conflictSpace(x, y, w, h, this_x, this_y, this_w, this_h) ) - { - return 1; - } - } - } - } - - return 0; -} - -int isConflictWithObjectFromSpaceBut(space_t *p, int x, int y, int w, int h, void *but) -{ - int segX, segY, segW, segH; - int id, this_x, this_y, this_w, this_h; - int i, j, k; - - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for( i = segY ; i < segY + segH ; i++ ) - { - for( j = segX ; j < segX + segW ; j++ ) - { - void *this; - - for( k = 0 ; k < p->area[j][i]->count ; k++ ) - { - this = p->area[j][i]->list[k]; - - if( this == but ) - { - continue; - } - - p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); - - if( my_conflictSpace(x, y, w, h, this_x, this_y, this_w, this_h) ) - { - return 1; - } - } - } - } - - return 0; -} - -void delObjectFromSpace(space_t *p, void *item) -{ - int segX, segY, segW, segH; - int id, x, y, w, h; - int index; - int i, j; - - p->getStatus(item, &id, &x, &y, &w, &h); - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for( i = segY ; i < segY + segH ; i++ ) - { - for( j = segX ; j < segX + segW ; j++ ) - { - index = searchListItem(p->area[j][i], item); - assert( index != -1 ); - delList(p->area[j][i], index); - } - } - - index = searchObjectInSpace(p, id); - assert( index != -1 ); - delList(p->list, index); -} - -void delObjectFromSpaceWithObject(space_t *p, void *item, void *f) -{ - void (*fce)(void *param); - - fce = f; - - delObjectFromSpace(p, item); - fce(item); -} - -void moveObjectInSpace(space_t *p, void *item, int move_x, int move_y) -{ - int old_segX, old_segY, old_segW, old_segH; - int new_segX, new_segY, new_segW, new_segH; - int id, x, y, w, h; - - p->getStatus(item, &id, &x, &y, &w, &h); - getSegment(p, x, y, w, h, &old_segX, &old_segY, &old_segW, &old_segH); - getSegment(p, move_x, move_y, w, h, &new_segX, &new_segY, &new_segW, &new_segH); -/* - printf("%d %d %d %d -> %d %d %d %d \n", - old_segX, old_segY, old_segW, old_segH, - new_segX, new_segY, new_segW, new_segH); -*/ - if( old_segX != new_segX || old_segY != new_segY || - old_segW != new_segW || old_segH != new_segH ) - { - delObjectFromSpace(p, item); - p->setStatus(item, move_x, move_y, w, h); - addObjectToSpace(p, item); - return; - } - - p->setStatus(item, move_x, move_y, w, h); -} - -void printSpace(space_t *p) -{ - int i, j; - - printf("print space : \n"); - - for( i = 0 ; i < p->h ; i++ ) - { - for( j = 0 ; j < p->w ; j++ ) - { - printf("%3d ", p->area[j][i]->count); - } - - putchar('\n'); - } -} - -void destroySpace(space_t *p) -{ - int j, i; - - destroyList(p->list); - - for( i = 0 ; i < p->h ; i++ ) - { - for( j = 0 ; j < p->w ; j++ ) - { - destroyList(p->area[j][i]); - } - } - - for( i = 0 ; i < p->w ; i++ ) - { - free(p->area[i]); - } - - free(p->area); - free(p); -} - -void destroySpaceWithObject(space_t *p, void *f) -{ - void (*fce)(void *param); - int i; - - fce = f; - - for( i = 0 ; i < p->list->count ; i++ ) - fce(p->list->list[i]); - - destroySpace(p); -} - -#ifdef DEBUG_SPACE - -void test_space() -{/* - space_t *space; - recv_t *recv; - - space = newSpace(5000, 2500, 320, 240, getStatus, setStatus); - - addObjectToSpace(space, newRecv(5, 0, 0, 1, 1) ); - addObjectToSpace(space, newRecv(7, 0, 0, 1, 1) ); - addObjectToSpace(space, newRecv(8, 0, 0, 1, 1) ); - addObjectToSpace(space, newRecv(9, 0, 0, 1, 1) ); - addObjectToSpace(space, newRecv(6, 0, 0, 1, 1) ); -*/ -/* - recv = getObjectFromSpaceWithID(space, 6); - if( recv != NULL )printf("id = %d\n", recv->id); -*/ -// printListID(space); -} - -#endif - diff --git a/src/storage.c b/src/storage.c deleted file mode 100644 index e7afa45..0000000 --- a/src/storage.c +++ /dev/null @@ -1,138 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" - -typedef struct struct_storage_item -{ - char *name; - char *group; - void *data; -} storage_item_t; - -list_t* newStorage() -{ - return newList(); -} - -static storage_item_t* newStorageItem(char *group, char *name, void *data) -{ - storage_item_t *new; - - assert( name != NULL ); - assert( group != NULL ); - assert( data != NULL ); - - new = malloc( sizeof(storage_item_t) ); - new->name = strdup(name); - new->group = strdup(group); - new->data = data; - - return new; -} - -static void destroyStorageItem(storage_item_t *p, void *f) -{ - void(*fce)(void *); - - assert( p != NULL ); - assert( f != NULL ); - - fce = f; - - fce(p->data); - free(p->name); - free(p->group); - free(p); -} - -void addItemToStorage(list_t *list, char *group, char *name, void *data) -{ - addList(list, newStorageItem(group, name, data) ); -} - -void* getItemFromStorage(list_t *list, char *group, char *name) -{ - storage_item_t *this; - int i; - - assert( group != NULL ); - assert( name != NULL ); - - for(i = 0 ; i < list->count; i++) - { - this = (storage_item_t *) list->list[i]; - - if( strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0 ) - { - return this->data; - } - } - - printf("%s %s not found in storage !!!\n", group, name); - return NULL; -} - -void delItemFromStorage(list_t *list, char *group, char *name, void *f) -{ - storage_item_t *this; - int i; - - assert( group != NULL ); - assert( name != NULL ); - - for(i = 0 ; i < list->count; i++) - { - this = (storage_item_t *) list->list[i]; - - if( strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0 ) - { - destroyStorageItem(this, f); - delList(list, i); - return; - } - } - - return; -} - -void delAllItemFromStorage(list_t *list, char *group, void *f) -{ - storage_item_t *this; - int i; - - assert( group != NULL ); - - for(i = 0 ; i < list->count; i++) - { - this = (storage_item_t *) list->list[i]; - - if( strcmp(group, this->group) == 0 ) - { - destroyStorageItem(this, f); - delList(list, i); - i--; - } - } -} - -void destroyStorage(list_t *p, void *f) -{ - storage_item_t *this; - int i; - - assert( p != NULL ); - assert( f != NULL ); - - for( i = 0 ; i < p->count ; i++ ) - { - this = (storage_item_t *)p->list[i]; - destroyStorageItem(this, f); - } - - destroyList(p); -} diff --git a/src/tcp.c b/src/tcp.c deleted file mode 100755 index a80eca8..0000000 --- a/src/tcp.c +++ /dev/null @@ -1,235 +0,0 @@ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "main.h" -#include "tcp.h" - -#define BUFSIZE 1000 - -sock_tcp_t* newSockTcp() -{ - sock_tcp_t *new; - - new = malloc( sizeof(sock_tcp_t) ); - memset(new, 0, sizeof(sock_tcp_t)); - - return new; -} - -void destroySockTcp(sock_tcp_t *p) -{ - assert( p != NULL ); - free(p); -} - -sock_tcp_t* bindTcpSocket(int port) -{ - sock_tcp_t *new; - int len; - - assert( port > 0 && port < 65535 ); - - new = newSockTcp(); - - assert( new != NULL ); - - new->sock = socket(AF_INET, SOCK_STREAM, 0); - - if( new->sock < 0 ) - { - fprintf(stderr, "Nemozem vytvorit sietovy socket\n"); - destroySockTcp(new); - return NULL; - } - - new->sockAddr.sin_family = AF_INET; - new->sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); - new->sockAddr.sin_port = htons(port); - - len = sizeof(new->sockAddr); - - if ( bind(new->sock, (struct sockaddr *)&new->sockAddr, len) < 0 ) - { - fprintf(stderr, "Nemozem obsadit sietovy port %d\n", port); - destroySockTcp(new); - return NULL; - } - - listen(new->sock, 5); - - return new; -} - -sock_tcp_t* getTcpNewClient(sock_tcp_t *p) -{ - sock_tcp_t *new; - int client_len; - - assert( p != NULL ); - assert( p->sock >= 0 ); - - new = newSockTcp(); - - client_len = sizeof(new->sockAddr); - - new->sock = accept(p->sock, (struct sockaddr *)&new->sockAddr, - (socklen_t *)&client_len); - - return new; -} - -void getSockTcpIp(sock_tcp_t *p, char *str_ip) -{ - assert( p != NULL ); - assert( str_ip != NULL ); - - strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr)); -} - -int getSockTcpPort(sock_tcp_t *p) -{ - assert( p != NULL ); - return htons(p->sockAddr.sin_port); -} - -sock_tcp_t* connectTcpSocket(char *ip, int port) -{ - sock_tcp_t *new; - int len; - - assert( ip != NULL ); - assert( port > 0 && port < 65535 ); - - new = newSockTcp(); - - new->sock = socket(AF_INET, SOCK_STREAM, 0); - - if( new->sock < 0 ) - { - fprintf(stderr, "Nemozem vytvorit sietovy socket\n"); - destroySockTcp(new); - return NULL; - } - - new->sockAddr.sin_family = AF_INET; - new->sockAddr.sin_addr.s_addr = inet_addr(ip); - new->sockAddr.sin_port = htons(port); - - len = sizeof(new->sockAddr); - - if ( connect(new->sock, (struct sockaddr *)&new->sockAddr, len) < 0 ) - { - fprintf(stderr, "Nemozem sa pripojit na %s %d\n", ip, port); - destroySockTcp(new); - return NULL; - } - - return new; -} - -int readTcpSocket(sock_tcp_t *p, void *address, int len) -{ - assert( p != NULL ); - assert( address != NULL ); - - return read(p->sock, address, len); -} - -int writeTcpSocket(sock_tcp_t *p, void *address, int len) -{ - assert( p != NULL ); - assert( address != NULL ); - - return write(p->sock, address, len); -} - -void closeTcpSocket(sock_tcp_t *p) -{ - assert( p != NULL ); - - close(p->sock); - destroySockTcp(p); -} - -static int myWait(sock_tcp_t *p) -{ - fd_set readfds; - fd_set errorfds; - struct timeval tv; - int max_fd; - - tv.tv_sec = 1; - tv.tv_usec = 0; - - FD_ZERO(&readfds); - FD_ZERO(&errorfds); - max_fd = p->sock; - - FD_SET(p->sock, &readfds); - FD_SET(p->sock, &errorfds); - - select(max_fd+1, &readfds, (fd_set *)0, &errorfds, &tv); - - if( FD_ISSET(p->sock, &readfds) ) - { - FD_CLR(p->sock, &readfds); - return 1; - } - - return 0; -} - -int test_tcp_main(int argc, char **argv) -{ - sock_tcp_t *sock; - char buf[BUFSIZE]; - int ret; - - if( strcmp(argv[1], "s") == 0 ) - { - sock_tcp_t *cli; - - sock = bindTcpSocket( atoi(argv[2]) ); - - while(1) - { - while( myWait(sock) == 0 ) - ; - - cli = getTcpNewClient(sock); - - memset(buf, 0, BUFSIZE); - ret = readTcpSocket(cli, buf, BUFSIZE); - writeTcpSocket(cli, buf, ret); - - closeTcpSocket(cli); - } - } - - if( strcmp(argv[1], "c") == 0 ) - { - sock = connectTcpSocket("127.0.0.1", atoi(argv[2])); - - strcpy(buf, "Hello world !\n"); - writeTcpSocket(sock, buf, strlen(buf)); - - memset(buf, 0, BUFSIZE); - readTcpSocket(sock, buf, BUFSIZE); - - printf("buf = %s", buf); - - closeTcpSocket(sock); - } - - return 0; -} diff --git a/src/term.c b/src/term.c deleted file mode 100644 index 62dcd8f..0000000 --- a/src/term.c +++ /dev/null @@ -1,105 +0,0 @@ - -#include -#include - -#include "main.h" -#include "list.h" -#include "interface.h" -#include "font.h" -#include "term.h" -#include "myTimer.h" - -typedef struct struct_term_t -{ - char *text; - my_time_t delTime; -} term_t; - -static list_t *listText; - -static term_t* newTermItem(char *s) -{ - term_t *new; - char *line; - int len; - - assert( s != NULL ); - - line = strdup(s); - len = strlen(line); - line[len-1] = '\0'; - - new = malloc( sizeof(term_t) ); - memset(new, 0, sizeof(term_t)); - - new->text = line; - new->delTime = getMyTime() + TERM_DEL_TEXT_TIMEOUT; - - return new; -} - -static void destroyTermItem(term_t *p) -{ - assert( p != NULL ); - - free(p->text); - free(p); -} - -void initTerm() -{ - listText = newList(); -} - -void drawTerm() -{ - int i; - - for( i = 0 ; i < listText->count ; i++ ) - { - char *line; - - line = (char *)( (term_t *)listText->list[i] )->text; - drawFont(line, 10, 10 + i*20, COLOR_WHITE); - } -} - -void eventTerm() -{ - my_time_t currentTime; - int i; - - currentTime = getMyTime(); - - - for( i = 0 ; i < listText->count ; i++ ) - { - my_time_t thisTime; - - thisTime = ( (term_t *)listText->list[i] )->delTime; - - if( currentTime > thisTime ) - { - delListItem(listText, i, destroyTermItem); - i--; - } - } - -} - -void appendTextInTerm(char *s) -{ - - addList(listText, newTermItem(s) ); - - if( listText->count > TERM_MAX_LINES ) - { - delListItem(listText, 0, destroyTermItem); - } -} - -void quitTerm() -{ - assert( listText != NULL ); - destroyListItem(listText, destroyTermItem); -} diff --git a/src/textFile.c b/src/textFile.c deleted file mode 100755 index b3c0156..0000000 --- a/src/textFile.c +++ /dev/null @@ -1,164 +0,0 @@ - -#include -#include -#include -#include -#include -#include - -#include "main.h" -#include "textFile.h" -#include "string_length.h" - -textFile_t* newTextFile(char *s) -{ - textFile_t *new; - char path[STR_PATH_SIZE]; - - assert( s != NULL ); - - if( s[0] == '/' ) - { - strcpy(path, s); - } - else - { - getcwd(path, STR_PATH_SIZE); - strcat(path, "/"); - strcat(path, s); - } - - new = malloc(sizeof(textFile_t)); - new->file = strdup(path); - new->text = newList(); - - return new; -} - -static void createLine(list_t *list, char *p, int len) -{ - char *line; - char *begin_line; - char *end_line; - int length_line = 0; - - assert( list != NULL ); - assert( p != NULL ); - assert( len >= 0 ); - - begin_line = p; - - do{ - end_line = memchr( begin_line, '\n', len); - if( end_line == NULL )break; - - length_line = (int ) (end_line - ( begin_line ) ); - - line = malloc( (length_line+1) * sizeof(char) ); - memset(line, 0, (length_line+1) * sizeof(char) ); - strncpy(line, begin_line, length_line); - - addList(list, line); - len -= (length_line+1); - - begin_line = end_line+1; - - }while( begin_line != NULL ); -} - -/* - * Nacita subor *s a vrati ho f - * formatovany v type textFile_t* - */ -textFile_t* loadTextFile(char *s) -{ - FILE *file; - textFile_t *ret; - char *p; - int file_length = 0; - struct stat buf; - - assert( s != NULL ); - - if( lstat(s, &buf) < 0 ) - { - fprintf(stderr, "CHYBA: nemozem ziskat status suboru %s!\n", s); - return NULL; - } - - file_length = buf.st_size; - if( (file = fopen(s, "rt")) == NULL ) - { - fprintf(stderr, "CHYBA: nemozem otvorit subor %s pre citanie\n", s); - return NULL; - } - - p = malloc(file_length * sizeof(char)); - - if( fread(p, file_length * sizeof(char), 1, file) != 1 ) - { - fprintf(stderr, "CHYBA: nemozem nacitat udaje zo suboru %s\n",s); - fclose(file); - return NULL; - } - - fclose(file); - - ret = newTextFile(s); - createLine(ret->text, p, file_length); - - free(p); - - return ret; -} - -/* - * Zobrazi text ulozeny v *p - * do stdout - */ -void printTextFile(textFile_t *p) -{ - int i; - - assert( p != NULL ); - - printf("file : %s\n\n", p->file); - - for(i = 0; i < p->text->count; i++) - { - printf("%3d >> %s\n", i, (char *)p->text->list[i]); - } -} - -void saveTextFile(textFile_t *p) -{ - int i; - FILE *file; - - assert( p != NULL ); - - file = fopen(p->file, "wt"); - - for(i = 0; i < p->text->count; i++) - { - fprintf(file, "%s\n", (char *)p->text->list[i]); - } - - fclose(file); -} - -/* - * Uvolni text ulozeny v type textFile_t* - * z pamate. - */ -void destroyTextFile(textFile_t *p) -{ - assert( p != NULL ); - - free(p->file); - destroyListItem(p->text, free); - free(p); -} - - - diff --git a/src/tmp b/src/tmp deleted file mode 100644 index b685a93..0000000 --- a/src/tmp +++ /dev/null @@ -1,10 +0,0 @@ -static void proto_recv_init(char *msg) -{ - char cmd[STR_SIZE]; - tux_t *tux; - - tux = newTux(); - sscanf(msg, "%s %d\n", cmd, &tux->id); - my_tux_id = tux->id; - addList(listTux, tux); -} \ No newline at end of file diff --git a/src/tux.c b/src/tux.c deleted file mode 100644 index ea083de..0000000 --- a/src/tux.c +++ /dev/null @@ -1,792 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "tux.h" -#include "arena.h" -#include "shot.h" -#include "gun.h" -#include "item.h" -#include "arena.h" -#include "myTimer.h" -#include "net_multiplayer.h" -#include "proto.h" -#include "modules.h" -#include "idManager.h" - -#ifndef PUBLIC_SERVER -#include "image.h" -#include "sound.h" -#include "fake_audio.h" -#include "layer.h" -#include "screen_world.h" -#include "term.h" -#endif - -#ifdef PUBLIC_SERVER -#include "publicServer.h" -#endif - -#ifndef PUBLIC_SERVER - -static SDL_Surface *g_tux_up; -static SDL_Surface *g_tux_right; -static SDL_Surface *g_tux_left; -static SDL_Surface *g_tux_down; -static SDL_Surface *g_cross; - -#endif - -static bool_t isTuxInit = FALSE; - -bool_t isTuxInicialized() -{ - return isTuxInit; -} - -void initTux() -{ -#ifndef PUBLIC_SERVER - assert( isImageInicialized() == TRUE ); -#endif - -#ifndef PUBLIC_SERVER - g_tux_up = addImageData("tux8.png", IMAGE_ALPHA, "tux8", IMAGE_GROUP_BASE); - g_tux_right = addImageData("tux6.png", IMAGE_ALPHA, "tux6", IMAGE_GROUP_BASE); - g_tux_left = addImageData("tux4.png", IMAGE_ALPHA, "tux4", IMAGE_GROUP_BASE); - g_tux_down = addImageData("tux2.png", IMAGE_ALPHA, "tux2", IMAGE_GROUP_BASE); - g_cross = addImageData("cross.png", IMAGE_ALPHA, "cross", IMAGE_GROUP_BASE); -#endif - - isTuxInit = TRUE; -} - -tux_t* newTux() -{ - int x, y; - tux_t *new; - - new = malloc( sizeof(tux_t) ); - assert( new != NULL ); - - memset(new, 0, sizeof(tux_t) ); - - new->id = getNewID(); - new->status = TUX_STATUS_ALIVE; - new->control = TUX_CONTROL_NONE; - - findFreeSpace(getCurrentArena(), &x, &y, TUX_WIDTH, TUX_HEIGHT); - setTuxProportion(new, x, y); - - new->position = TUX_DOWN; - new->gun = GUN_SIMPLE; - new->shot[ new->gun ] = GUN_MAX_SHOT; - - sprintf(new->name, "no_name_id_%d", new->id); - new->score = 0; - new->frame = 0; - - new->bonus = BONUS_NONE; - new->bonus_time = 0; - - new->isCanShot = TRUE; - new->isCanSwitchGun = TRUE; - - new->client = NULL; - - return new; -} - -bool_t isTuxAnyGun(tux_t *tux) -{ - int i; - - for( i = 0 ; i < GUN_COUNT ; i++ ) - { - if( tux->shot[i] > 0 ) - { - return TRUE; - } - } - - return FALSE; -} - -void getCourse(int n, int *x, int *y) -{ - assert( x != NULL ); - assert( y != NULL ); - - switch( n ) - { - case TUX_UP : - *x = 0; - *y = -1; - break; - - case TUX_LEFT : - *x = -1; - *y = 0; - break; - - case TUX_RIGHT : - *x = +1; - *y = 0; - break; - - case TUX_DOWN : - *x = 0; - *y = +1; - break; - - default : - assert( ! "premenna n ma zlu hodnotu !" ); - break; - } -} - -#ifndef PUBLIC_SERVER - -void drawTux(tux_t *tux) -{ - SDL_Surface *g_image = NULL; - - assert( tux != NULL ); - - if( tux->bonus == BONUS_HIDDEN && - getNetTypeGame() != NET_GAME_TYPE_NONE && - tux->control == TUX_CONTROL_NET ) - { - return; - } - - switch( tux->position ) - { - case TUX_UP : - g_image = g_tux_up; - break; - - case TUX_LEFT : - g_image = g_tux_left; - break; - - case TUX_RIGHT : - g_image = g_tux_right; - break; - - case TUX_DOWN : - g_image = g_tux_down; - break; - - default : - assert( ! "premenna p->control ma zlu hodnotu !" ); - break; - } - - if( tux->status == TUX_STATUS_DEAD ) - { - g_image = g_cross; - - addLayer(g_cross, - tux->x - g_cross->w / 2, - ( tux->y + g_cross->h / 2 ) - g_cross->h, - 0, 0, - g_cross->w, g_cross->h, TUX_LAYER); - - return; - } - - addLayer(g_image, - tux->x - TUX_IMG_WIDTH / 2, - ( tux->y + TUX_HEIGHT / 2 ) - TUX_IMG_HEIGHT, - (tux->frame/TUX_KEY) * TUX_IMG_WIDTH, 0, - TUX_IMG_WIDTH, TUX_IMG_HEIGHT, TUX_LAYER); -} - -void drawListTux(list_t *listTux) -{ - tux_t *thisTux; - int i; - - assert( listTux != NULL ); - - for( i = 0 ; i < listTux->count ; i++ ) - { - thisTux = (tux_t *)listTux->list[i]; - assert( thisTux != NULL ); - drawTux(thisTux); - } -} - -#endif - -void replaceTuxID(tux_t *tux, int id) -{ - replaceID(tux->id, id); - tux->id = id; -} - -int isTuxSeesTux(tux_t *tux, tux_t *thisTux) -{ - int tux_x, tux_y, tux_w, tux_h; - int thisTux_x, thisTux_y, thisTux_w, thisTux_h; - int screen_x, screen_y; - - getTuxProportion(tux, &tux_x, &tux_y, &tux_w, &tux_h); - getTuxProportion(thisTux, &thisTux_x, &thisTux_y, &thisTux_w, &thisTux_h); - - getCenterScreen(&screen_x, &screen_y, tux->x, tux->y); - - return conflictSpace(screen_x-WINDOW_SIZE_X/4, screen_y-WINDOW_SIZE_Y/4, - WINDOW_SIZE_X*1.25, WINDOW_SIZE_Y*1.25, thisTux_x, thisTux_y, thisTux_w, thisTux_h); -} - -static void timer_spawnTux(void *p) -{ - tux_t *tux; - arena_t *arena; - int x, y; - int id; - - id = * ((int *)p); - free(p); - - arena = getCurrentArena(); - tux = getObjectFromSpaceWithID(arena->spaceTux, id); - - if( tux == NULL )return; - - tux->status = TUX_STATUS_ALIVE; - findFreeSpace(getCurrentArena(), &x, &y, TUX_WIDTH, TUX_HEIGHT); - moveObjectInSpace(getCurrentArena()->spaceTux, tux, x, y); - tux->gun = GUN_SIMPLE; - - addNewItem(arena->spaceItem, ID_UNKNOWN); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } -} - -static void timer_tuxCanShot(void *p) -{ - tux_t *tux; - int id; - - id = * ((int *)p); - free(p); - - tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); - - if( tux == NULL )return; - - tux->isCanShot = TRUE; -} - -static void timer_tuxCanSwitchGun(void *p) -{ - tux_t *tux; - int id; - - id = * ((int *)p); - free(p); - - tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); - - if( tux == NULL )return; - - tux->isCanSwitchGun = TRUE; -} - -void eventTuxIsDead(tux_t *tux) -{ -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; -#endif - - if( tux->status == TUX_STATUS_DEAD ) - { - return; - } - -#ifndef PUBLIC_SERVER - sprintf(msg, "tux with id %d is dead\n", tux->id); - playSound("dead", SOUND_GROUP_BASE); - appendTextInTerm(msg); -#endif - - tux->status = TUX_STATUS_DEAD; - memset(tux->shot, 0, sizeof(int) * GUN_COUNT); - tux->gun = GUN_SIMPLE; - tux->shot[ tux->gun ] = GUN_MAX_SHOT; - - tux->bonus = BONUS_NONE; - tux->bonus_time = 0; - tux->pickup_time = 0; - - tux->isCanShot = TRUE; - tux->isCanSwitchGun = TRUE; - - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_spawnTux, newInt(tux->id), TUX_TIME_SPAWN ); - } - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_kill_server(PROTO_SEND_ALL, NULL, tux); - } -} - -static void eventTuxIsDeadWIthShot(tux_t *tux, shot_t *shot) -{ - if( shot->author_id != tux->id ) - { - tux_t *author; - - author = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, shot->author_id); - - if( author != NULL ) - { - author->score++; - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, author); - } - } - - } - - countRoundInc(); - eventTuxIsDead(tux); -} - -void tuxTeleport(tux_t *tux) -{ - int x, y; -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; -#endif - -#ifndef PUBLIC_SERVER - sprintf(msg, "tux with id %d teleporting\n", tux->id); - playSound("teleport", SOUND_GROUP_BASE); - appendTextInTerm(msg); -#endif - - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - findFreeSpace(getCurrentArena(), &x, &y, TUX_WIDTH, TUX_HEIGHT); - moveObjectInSpace(getCurrentArena()->spaceTux, tux, x, y); - //setTuxProportion(tux, x, y); - } - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } -} - -static void bombBallExplosion(shot_t *shot) -{ - item_t *item; - int x, y; - - x = ( shot->x + shot->w/2 ) - ITEM_BIG_EXPLOSION_WIDTH/2; - y = ( shot->y + shot->h/2 ) - ITEM_BIG_EXPLOSION_HEIGHT/2; - - item = newItem(x, y, ITEM_BIG_EXPLOSION, shot->author_id ); - - addObjectToSpace(getCurrentArena()->spaceItem, item ); - - delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_del_server(PROTO_SEND_ALL, NULL, shot->id); - proto_send_additem_server(PROTO_SEND_ALL, NULL, item); - } -} - -void eventConflictTuxWithShot(arena_t *arena) -{ - shot_t *thisShot; - tux_t *thisTux; - int i, j; - - for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) - { - thisShot = (shot_t *)arena->spaceShot->list->list[i]; - assert( thisShot != NULL ); - - listDoEmpty(listHelp); - getObjectFromSpace(arena->spaceTux, thisShot->x, thisShot->y, - thisShot->w, thisShot->h, listHelp); - - for( j = 0 ; j < listHelp->count ; j++ ) - { - thisTux = listHelp->list[j]; - - if( thisTux->status == TUX_STATUS_ALIVE ) - { - if( thisShot->author_id == thisTux->id && - thisShot->isCanKillAuthor == FALSE ) - { - continue; - } - - if( thisTux->bonus == BONUS_TELEPORT ) - { - tuxTeleport(thisTux); - continue; - } - - if( thisShot->gun == GUN_BOMBBALL ) - { - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - bombBallExplosion(thisShot); - i--; - } - - continue; - } - - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - eventTuxIsDeadWIthShot(thisTux, thisShot); - } - } - - delObjectFromSpaceWithObject(arena->spaceShot, thisShot, destroyShot); - i--; - - continue; - } - } -} - -void moveTux(tux_t *tux, int n) -{ - int px, py; - int zal_x, zal_y; - int new_x, new_y; - int w, h; - arena_t *arena; - - assert( tux != NULL ); - - arena = getCurrentArena(); - //interval(); - - if( tux->position != n ) - { - tux->position = n; - return; - } - - if( n == TUX_LEFT && tux->x-TUX_STEP <= 0 ) - { - return; - } - - if( n == TUX_RIGHT && tux->x+TUX_STEP >= arena->w ) - { - return; - } - - if( n == TUX_UP && tux->y-TUX_STEP <= 0 ) - { - return; - } - - if( n == TUX_DOWN && tux->y+TUX_STEP >= arena->h ) - { - return; - } - - getTuxProportion(tux, &zal_x, &zal_y, &w, &h); - - getCourse(tux->position, &px, &py); - - new_x = zal_x; - new_y = zal_y; - - new_x += px * TUX_STEP; - new_y += py * TUX_STEP; - - if( tux->bonus == BONUS_SPEED ) - { - new_x += px * TUX_STEP; - new_y += py * TUX_STEP; - } - - if( tux->bonus != BONUS_GHOST && ( - isConflictWithObjectFromSpaceBut(arena->spaceTux, new_x, new_y, w, h, tux) || - isConflictModule(new_x, new_y, w, h) ) ) - { - setTuxProportion(tux, zal_x, zal_y); - return; - } - - setTuxProportion(tux, zal_x, zal_y); - moveObjectInSpace(arena->spaceTux, tux, new_x, new_y); - eventGiveTuxListItem(tux, getCurrentArena()->spaceItem); - tux->frame++; - if( tux->frame == TUX_KEY * TUX_MAX_ANIMATION_FRAME ) tux->frame = 0; -} - -void switchTuxGun(tux_t *tux) -{ - int i; -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; -#endif - - if( tux->control != TUX_CONTROL_NET && - tux->isCanSwitchGun == FALSE ) - { - return; - } - - for( i = tux->gun+1 ; i < GUN_COUNT ; i++ ) - { - if( tux->shot[i] > 0 ) - { - tux->gun = i; - tux->isCanSwitchGun = FALSE; - addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_tuxCanSwitchGun, - newInt(tux->id), TUX_TIME_CAN_SWITCH_GUN ); -#ifndef PUBLIC_SERVER - playSound("switch_gun", SOUND_GROUP_BASE); - sprintf(msg, "tux with id %d switch gun\n", tux->id); - appendTextInTerm(msg); -#endif - return; - } - } - - for( i = 0 ; i < GUN_COUNT ; i++ ) - { - if( tux->shot[i] > 0 ) - { - tux->gun = i; - tux->isCanSwitchGun = FALSE; - addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_tuxCanSwitchGun, - newInt(tux->id), TUX_TIME_CAN_SWITCH_GUN ); - -#ifndef PUBLIC_SERVER - playSound("switch_gun", SOUND_GROUP_BASE); - sprintf(msg, "tux with id %d switch gun\n", tux->id); - appendTextInTerm(msg); -#endif - - return; - } - } -} - -void shotTux(tux_t *tux) -{ - assert( tux != NULL ); - - if( tux->isCanShot == FALSE || - tux->shot[tux->gun] == 0 ) - { - return; - } - -#ifndef PUBLIC_SERVER - playSound("switch_gun", SOUND_GROUP_BASE); -#endif - - shotInGun(tux); - - tux->isCanShot = FALSE; - - addTaskToTimer(getCurrentArena()->listTimer, TIMER_ONE, timer_tuxCanShot, - newInt(tux->id), TUX_TIME_CAN_SHOT ); - - if( tux->shot[tux->gun] == 0 ) - { - switchTuxGun(tux); - } -} - -void actionTux(tux_t *tux, int action) -{ - if( tux->status == TUX_STATUS_DEAD ) - { - return; - } - - switch( action ) - { - case TUX_UP : - case TUX_LEFT : - case TUX_RIGHT : - case TUX_DOWN : - moveTux(tux, action); - break; - - case TUX_SHOT : - shotTux(tux); - break; - - case TUX_SWITCH_GUN : - switchTuxGun(tux); - break; - } -} - -static void pickUpGun(tux_t *tux) -{ - if( isTuxAnyGun(tux) == FALSE ) - { - tux->gun = GUN_SIMPLE; - - if( tux->pickup_time < TUX_MAX_PICKUP ) - { - tux->pickup_time++; - } - - if( tux->pickup_time == TUX_MAX_PICKUP && getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; - playSound("switch_gun", SOUND_GROUP_BASE); - sprintf(msg, "tux with id %d pickup\n", tux->id); - appendTextInTerm(msg); -#endif - - tux->gun = GUN_SIMPLE; - tux->shot[ tux->gun ] = GUN_MAX_SHOT; - tux->pickup_time = 0; - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - } - } -} - -static void eventBonus(tux_t *tux) -{ - if( tux->bonus != BONUS_NONE ) - { - if( tux->bonus_time > 0 ) - { - tux->bonus_time--; - } - - if( tux->bonus_time == 0 && getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { -#ifndef PUBLIC_SERVER - char msg[STR_SIZE]; -#endif - - if( tux->bonus == BONUS_GHOST ) - { - int x, y, w, h; - getTuxProportion(tux, &x, &y, &w, &h); - - tux->bonus = BONUS_NONE; - - if( /*isConflictTuxWithListTux(tux, getCurrentArena()->listTux) || */ - isConflictModule(x, y, w, h) ) - { - tux->bonus = BONUS_GHOST; - return; - } - } - - tux->bonus = BONUS_NONE; - -#ifndef PUBLIC_SERVER - sprintf(msg, "tux with id %d bonus disabled\n", tux->id); - appendTextInTerm(msg); -#endif - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - } - } -} - -void eventListTux(list_t *listTux) -{ - tux_t *thisTux; - arena_t *arena; - int i; - - assert( listTux != NULL ); - arena = getCurrentArena(); - - for( i = 0 ; i < listTux->count ; i++ ) - { - thisTux = (tux_t *)listTux->list[i]; - assert( thisTux != NULL ); - -#ifndef PUBLIC_SERVER - tuxControl(thisTux); -#endif - pickUpGun(thisTux); - eventBonus(thisTux); - eventGiveTuxListItem(thisTux, arena->spaceItem); - } -} - -void getTuxProportion(tux_t *tux, int *x,int *y, int *w, int *h) -{ - assert( tux != NULL ); - - if( x != NULL ) *x = tux->x - TUX_WIDTH / 2; - if( y != NULL ) *y = tux->y - TUX_HEIGHT / 2; - if( w != NULL ) *w = TUX_WIDTH; - if( h != NULL ) *h = TUX_HEIGHT; -} - -void setTuxProportion(tux_t *tux, int x, int y) -{ - assert( tux != NULL ); - - tux->x = x + TUX_WIDTH / 2; - tux->y = y + TUX_WIDTH / 2; -} - -void getStatusTux(void *p, int *id, int *x,int *y, int *w, int *h) -{ - tux_t *tux; - - tux = p; - *id = tux->id; - getTuxProportion(tux, x, y, w, h); -} - -void setStatusTux(void *p, int x, int y, int w, int h) -{ - tux_t *tux; - - tux = p; - setTuxProportion(tux, x, y); -} - -void destroyTux(tux_t *tux) -{ - assert( tux != NULL ); - delID(tux->id); - free(tux); -} - -void quitTux() -{ - isTuxInit = FALSE; -} diff --git a/src/udp.c b/src/udp.c deleted file mode 100644 index 545b639..0000000 --- a/src/udp.c +++ /dev/null @@ -1,356 +0,0 @@ - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define BUFSIZE 1000 -#include "udp.h" -#include "main.h" - -sock_udp_t* newSockUdp(int proto) -{ - sock_udp_t *new; - - new = malloc( sizeof(sock_udp_t) ); - memset(new, 0, sizeof(sock_udp_t)); - new->proto = proto; - - return new; -} - -void destroySockUdp(sock_udp_t *p) -{ - assert( p != NULL ); - free(p); -} - -sock_udp_t* bindUdpSocket(char *address, int port, int proto) -{ - sock_udp_t *new; - int res; - - assert( port > 0 && port < 65535 ); - - new = newSockUdp(proto); - - assert( new != NULL ); - - if( new->proto == PROTO_UDPv4 ) - { - new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - } - -#ifdef SUPPORT_IPv6 - if( new->proto == PROTO_UDPv6 ) - { - new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); - } -#endif - - if( new->sock < 0 ) - { - fprintf(stderr, "nemozem vytvorit socket !\n"); - destroySockUdp(new); - return NULL; - } - - memset(&(new->sockAddr), 0, sizeof(new->sockAddr)); - - if( new->proto == PROTO_UDPv4 ) - { - new->sockAddr.sin_family = AF_INET; - new->sockAddr.sin_port = htons(port); - new->sockAddr.sin_addr.s_addr = inet_addr(address); - - res = bind(new->sock, (struct sockaddr *)&(new->sockAddr), sizeof(new->sockAddr)); - } - -#ifdef SUPPORT_IPv6 - if( new->proto == PROTO_UDPv6 ) - { - new->sockAddr6.sin6_family = AF_INET6; - new->sockAddr6.sin6_port = htons(port); - inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr)); - - res = bind(new->sock, (struct sockaddr *)&(new->sockAddr6), sizeof(new->sockAddr6)); - } -#endif - - if( res < 0 ) - { - fprintf(stderr, "nemozem nastavit sokcet %s %d !\n", address, port); - destroySockUdp(new); - return NULL; - } - - return new; -} - -sock_udp_t* connectUdpSocket(char *address, int port, int proto) -{ - sock_udp_t *new; - - assert( address != NULL ); - assert( port > 0 && port < 65536 ); - - new = newSockUdp(proto); - - assert( new != NULL ); - - if( new->proto == PROTO_UDPv4 ) - { - new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - } - -#ifdef SUPPORT_IPv6 - if( new->proto == PROTO_UDPv6 ) - { - new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); - } -#endif - - if( new->sock < 0 ) - { - fprintf(stderr, "nemozem vytvorit socket !\n"); - destroySockUdp(new); - return NULL; - } - - memset(&(new->sockAddr), 0, sizeof(new->sockAddr)); - - if( new->proto == PROTO_UDPv4 ) - { - new->sockAddr.sin_family = AF_INET; - new->sockAddr.sin_port = htons(port); - new->sockAddr.sin_addr.s_addr = inet_addr(address); - } - -#ifdef SUPPORT_IPv6 - if( new->proto == PROTO_UDPv6 ) - { - new->sockAddr6.sin6_family = AF_INET6; - new->sockAddr6.sin6_port = htons(port); - inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr)); - } -#endif - return new; -} - -int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) -{ - int addrlen; - int size; - - assert( src != NULL ); - assert( dst != NULL ); - assert( address != NULL ); - - if( dst->proto == PROTO_UDPv4 ) - { - addrlen = sizeof(src->sockAddr); - - size = recvfrom(src->sock, address, len, 0, - (struct sockaddr *)&dst->sockAddr, (socklen_t *)&addrlen); - } - -#ifdef SUPPORT_IPv6 - if( dst->proto == PROTO_UDPv6 ) - { - addrlen = sizeof(src->sockAddr6); - - size = recvfrom(src->sock, address, len, 0, - (struct sockaddr *)&dst->sockAddr6, (socklen_t *)&addrlen); - } -#endif - - if( size < 0 ) - { - char str_ip[STR_IP_SIZE]; - - getSockUdpIp(dst, str_ip, STR_IP_SIZE); - - fprintf(stderr, "nemozem zapisat zo socketu %d %s %d!\n", size, str_ip, getSockUdpPort(dst)); - return -1; - } - - return size; -} - -int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) -{ - int addrlen; - int size; - - assert( src != NULL ); - assert( dst != NULL ); - assert( address != NULL ); - - if( dst->proto == PROTO_UDPv4 ) - { - addrlen = sizeof(src->sockAddr); - - size = sendto(src->sock, address, len, 0, - (struct sockaddr *)&dst->sockAddr, (socklen_t)addrlen); - } - -#ifdef SUPPORT_IPv6 - if( dst->proto == PROTO_UDPv6 ) - { - addrlen = sizeof(src->sockAddr6); - - size = sendto(src->sock, address, len, 0, - (struct sockaddr *)&dst->sockAddr6, (socklen_t)addrlen); - } -#endif - - if( size < 0 ) - { - char str_ip[STR_IP_SIZE]; - - getSockUdpIp(dst, str_ip, STR_IP_SIZE); - - fprintf(stderr, "nemozem zapisat na socket %d %s %d!\n", size, str_ip, getSockUdpPort(dst)); - return -1; - } - - return size; -} - -void getSockUdpIp(sock_udp_t *p, char *str_ip, int len) -{ - assert( p != NULL ); - assert( str_ip != NULL ); - - //strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr)); - - if( p->proto == PROTO_UDPv4 ) - { - inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len); - } - -#ifdef SUPPORT_IPv6 - if( p->proto == PROTO_UDPv6 ) - { - inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len); - } -#endif -} - -int getSockUdpPort(sock_udp_t *p) -{ - assert( p != NULL ); - - if( p->proto == PROTO_UDPv4 ) - { - return htons(p->sockAddr.sin_port); - } - -#ifdef SUPPORT_IPv6 - if( p->proto == PROTO_UDPv6 ) - { - return htons(p->sockAddr6.sin6_port); - } -#endif - - assert( ! "bad proto !" ); - - return -1; -} - - -void closeUdpSocket(sock_udp_t *p) -{ - assert( p != NULL ); - - close(p->sock); - destroySockUdp(p); -} - -#if 0 - -static int myWait(sock_udp_t *p) -{ - fd_set readfds; - fd_set errorfds; - struct timeval tv; - int max_fd; - - tv.tv_sec = 1; - tv.tv_usec = 0; - - FD_ZERO(&readfds); - FD_ZERO(&errorfds); - max_fd = p->sock; - - FD_SET(p->sock, &readfds); - FD_SET(p->sock, &errorfds); - - select(max_fd+1, &readfds, (fd_set *)0, &errorfds, &tv); - - if( FD_ISSET(p->sock, &readfds) ) - { - FD_CLR(p->sock, &readfds); - return 1; - } - - return 0; -} - -int main(int argc, char *argv[]) -{ - sock_udp_t *sock; - char buf[BUFSIZE]; - int ret; - - if( strcmp(argv[1], "s") == 0 ) - { - sock_udp_t *cli; - - //sock = bindUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6 ); - sock = bindUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4 ); - - //cli = newSockUdp(PROTO_UDPv6); - cli = newSockUdp(PROTO_UDPv4); - - while(1) - { - while( myWait(sock) == 0 ) - ; - - memset(buf, 0, BUFSIZE); - ret = readUdpSocket(sock, cli, buf, BUFSIZE); - writeUdpSocket(sock, cli, buf, ret); - } - } - - if( strcmp(argv[1], "c") == 0 ) - { - //sock = connectUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6); - sock = connectUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4); - - strcpy(buf, "Hello world !\n"); - writeUdpSocket(sock, sock, buf, strlen(buf)); - - memset(buf, 0, BUFSIZE); - readUdpSocket(sock, sock, buf, BUFSIZE); - - printf("buf = %s", buf); - //while(1)sleep(1); - closeUdpSocket(sock); - } - - return 0; -} -#endif - diff --git a/src/widget/Makefile b/src/widget/Makefile new file mode 100644 index 0000000..cdb504b --- /dev/null +++ b/src/widget/Makefile @@ -0,0 +1,28 @@ + +all: widget_button.o widget_buttonimage.o widget_check.o widget_label.o widget_select.o widget_textfield.o widget_image.o + +widget_button.o: widget_button.c widget_button.h + $(CC) $(CFLAGS) -o widget_button.o -c widget_button.c + +widget_buttonimage.o: widget_buttonimage.c widget_buttonimage.h + $(CC) $(CFLAGS) -o widget_buttonimage.o -c widget_buttonimage.c + +widget_check.o: widget_check.c widget_check.h + $(CC) $(CFLAGS) -o widget_check.o -c widget_check.c + +widget_image.o: widget_image.c widget_image.h + $(CC) $(CFLAGS) -o widget_image.o -c widget_image.c + +widget_label.o: widget_label.c widget_label.h + $(CC) $(CFLAGS) -o widget_label.o -c widget_label.c + +widget_select.o: widget_select.c widget_select.h + $(CC) $(CFLAGS) -o widget_select.o -c widget_select.c + +widget_textfield.o: widget_textfield.c widget_textfield.h + $(CC) $(CFLAGS) -o widget_textfield.o -c widget_textfield.c + +clean: + rm -rf *.o + + diff --git a/src/widget/widget_button.c b/src/widget/widget_button.c new file mode 100644 index 0000000..7f0957b --- /dev/null +++ b/src/widget/widget_button.c @@ -0,0 +1,84 @@ + +#include + +#include "base/main.h" + +#include "client/interface.h" +#include "client/image.h" +#include "client/font.h" + +#include "widget_button.h" + +widget_button_t* newWidgetButton(char *text, int x, int y, void (*fce_event)(void *)) +{ + widget_button_t *new; + + new = malloc( sizeof(widget_button_t) ); + new->text = strdup(text); + new->x = x; + new->y = y; + new->fce_event = fce_event; + getTextSize(text, &new->w, &new->h); + + return new; +} + +void drawWidgetButton(widget_button_t *p) +{ + static SDL_Surface *g_button0 = NULL; + static SDL_Surface *g_button1 = NULL; + int x, y; + + getMousePosition(&x, &y); + + if( g_button0 == NULL ) + { + g_button0 = addImageData("button0.png", IMAGE_ALPHA, "button0", IMAGE_GROUP_BASE); + } + + if( g_button1 == NULL ) + { + g_button1 = addImageData("button1.png", IMAGE_ALPHA, "button1", IMAGE_GROUP_BASE); + } + + if( x >= p->x && x <= p->x+WIDGET_BUTTON_WIDTH && + y >= p->y && y <= p->y+WIDGET_BUTTON_HEIGHT ) + { + drawImage(g_button1, p->x, p->y, 0, 0, g_button0->w, g_button0->h); + } + else + { + drawImage(g_button0, p->x, p->y, 0, 0, g_button0->w, g_button0->h); + } + + //drawFont(p->text, p->x+WIDGET_BUTTON_WIDTH/2-p->w/2, p->y+p->h/2, COLOR_WHITE); + drawFont(p->text, p->x + WIDGET_BUTTON_WIDTH/2 - p->w/2, p->y + WIDGET_BUTTON_HEIGHT/2 - p->h/2, COLOR_WHITE); +} + +void eventWidgetButton(widget_button_t *p) +{ + static int time = 0; + int x, y; + + if( time > 0) + { + time--; + return; + } + + getMousePosition(&x, &y); + + if( x >= p->x && x <= p->x+WIDGET_BUTTON_WIDTH && + y >= p->y && y <= p->y+WIDGET_BUTTON_HEIGHT && + isMouseClicked() ) + { + time = WIDGET_BUTTON_TIME; + p->fce_event(p); + } +} + +void destroyWidgetButton(widget_button_t *p) +{ + free(p->text); + free(p); +} diff --git a/src/widget/widget_button.h b/src/widget/widget_button.h new file mode 100644 index 0000000..1662407 --- /dev/null +++ b/src/widget/widget_button.h @@ -0,0 +1,27 @@ + +#ifndef WIDGET_BUTTON_H + +#define WIDGET_BUTTON_H + +#include "base/main.h" + +#define WIDGET_BUTTON_TIME 10 + +#define WIDGET_BUTTON_WIDTH 125 +#define WIDGET_BUTTON_HEIGHT 36 + +typedef struct widget_button +{ + int x, y; + int w, h; + char *text; + void (*fce_event)(void *); +} widget_button_t; + +extern widget_button_t* newWidgetButton(char *text, int x, int y, void (*fce_event)(void *)); +extern void drawWidgetButton(widget_button_t *p); +extern void eventWidgetButton(widget_button_t *p); +extern void destroyWidgetButton(widget_button_t *p); + +#endif + diff --git a/src/widget/widget_buttonimage.c b/src/widget/widget_buttonimage.c new file mode 100644 index 0000000..7c59f3b --- /dev/null +++ b/src/widget/widget_buttonimage.c @@ -0,0 +1,55 @@ + +#include +#include "base/main.h" +#include "client/interface.h" +#include "client/font.h" +#include "client/image.h" +#include "widget_buttonimage.h" + +widget_buttonimage_t* newWidgetButtonimage(SDL_Surface *image, int x, int y, void (*fce_event)(void *)) +{ + widget_buttonimage_t *new; + + new = malloc( sizeof(widget_buttonimage_t) ); + new->image = image; + new->x = x; + new->y = y; + new->w = image->w/2; + new->h = image->h; + new->active = 0; + new->fce_event = fce_event; + + return new; +} + +void drawWidgetButtonimage(widget_buttonimage_t *p) +{ + drawImage(p->image, p->x, p->y, p->active*p->w, 0, p->w, p->h); +} + +void eventWidgetButtonimage(widget_buttonimage_t *p) +{ + static int time = 0; + int x, y; + + if( time > 0) + { + time--; + return; + } + + getMousePosition(&x, &y); + + if( x >= p->x && x <= p->x+p->w && + y >= p->y && y <= p->y+p->h && + isMouseClicked() ) + { + time = WIDGET_BUTTONIMAGE_TIME; + p->fce_event(p); + } +} + +void destroyWidgetButtonimage(widget_buttonimage_t *p) +{ + free(p); +} diff --git a/src/widget/widget_buttonimage.h b/src/widget/widget_buttonimage.h new file mode 100644 index 0000000..d30e5bf --- /dev/null +++ b/src/widget/widget_buttonimage.h @@ -0,0 +1,27 @@ + +#ifndef WIDGET_BUTTONIMAGE_H + +#include "base/main.h" + +#define WIDGET_BUTTONIMAGE_H + +#define WIDGET_BUTTONIMAGE_TIME 10 + +typedef struct widget_buttonimageimage +{ + int x, y; + int w, h; + int active; + SDL_Surface *image; + void (*fce_event)(void *); +} widget_buttonimage_t; + +extern widget_buttonimage_t* newWidgetButtonimage(SDL_Surface *image, int x, int y, + void (*fce_event)(void *)); + +extern void drawWidgetButtonimage(widget_buttonimage_t *p); +extern void eventWidgetButtonimage(widget_buttonimage_t *p); +extern void destroyWidgetButtonimage(widget_buttonimage_t *p); + +#endif + diff --git a/src/widget/widget_check.c b/src/widget/widget_check.c new file mode 100644 index 0000000..feb2414 --- /dev/null +++ b/src/widget/widget_check.c @@ -0,0 +1,84 @@ + +#include +#include "base/main.h" +#include "client/interface.h" +#include "client/image.h" +#include "widget_check.h" + +widget_check_t* newWidgetCheck(int x, int y, bool_t status, void (*fce_event)(void *)) +{ + widget_check_t *new; + + new = malloc( sizeof(widget_check_t) ); + new->x = x; + new->y = y; + new->time = 0; + new->status = status; + new->fce_event = fce_event; + + return new; +} + +void drawWidgetCheck(widget_check_t *p) +{ + static SDL_Surface *g_check = NULL; + int x, y; + int offset; + + getMousePosition(&x, &y); + + if( g_check == NULL ) + { + g_check = addImageData("check.png", IMAGE_ALPHA, "check", IMAGE_GROUP_BASE); + } + + if( p->status == TRUE ) + { + offset = 0; + } + else + { + offset = WIDGET_CHECK_WIDTH; + } + + drawImage(g_check, p->x, p->y, offset, 0, WIDGET_CHECK_WIDTH, WIDGET_CHECK_HEIGHT); +} + +void eventWidgetCheck(widget_check_t *p) +{ + int x, y; + + if( p->time > 0 ) + { + p->time--; + return; + } + + getMousePosition(&x, &y); + + if( x >= p->x && x <= p->x+WIDGET_CHECK_WIDTH && + y >= p->y && y <= p->y+WIDGET_CHECK_HEIGHT && + isMouseClicked() ) + { + if( p->status == TRUE ) + { + p->status = FALSE; + p->time = WIDGET_CHECK_TIME_SWITCH_STATUS; + } + else + { + p->status = TRUE; + p->time = WIDGET_CHECK_TIME_SWITCH_STATUS; + } + + if( p->fce_event != NULL ) + { + p->fce_event(p); + } + } +} + +void destroyWidgetCheck(widget_check_t *p) +{ + free(p); +} diff --git a/src/widget/widget_check.h b/src/widget/widget_check.h new file mode 100644 index 0000000..68aecd9 --- /dev/null +++ b/src/widget/widget_check.h @@ -0,0 +1,27 @@ + +#ifndef WIDGET_CHECK_H + +#define WIDGET_CHECK_H + +#include "base/main.h" + +#define WIDGET_CHECK_TIME_SWITCH_STATUS 5 + +#define WIDGET_CHECK_WIDTH 25 +#define WIDGET_CHECK_HEIGHT 25 + +typedef struct widget_check +{ + int x, y; + bool_t status; + int time; + void (*fce_event)(void *); +} widget_check_t; + +extern widget_check_t* newWidgetCheck(int x, int y, bool_t status, void (*fce_event)(void *)); +extern void drawWidgetCheck(widget_check_t *p); +extern void eventWidgetCheck(widget_check_t *p); +extern void destroyWidgetCheck(widget_check_t *p); + +#endif + diff --git a/src/widget/widget_image.c b/src/widget/widget_image.c new file mode 100644 index 0000000..5e1b743 --- /dev/null +++ b/src/widget/widget_image.c @@ -0,0 +1,35 @@ + +#include + +#include +#include "base/main.h" +#include "client/image.h" +#include "client/interface.h" +#include "client/font.h" +#include "widget_image.h" + +widget_image_t* newWidgetImage(int x, int y, SDL_Surface *image) +{ + widget_image_t *new; + + new = malloc( sizeof(widget_image_t) ); + new->x = x; + new->y = y; + new->image = image; + + return new; +} + +void drawWidgetImage(widget_image_t *p) +{ + drawImage(p->image, p->x, p->y, 0, 0, p->image->w, p->image->h); +} + +void eventWidgetImage(widget_image_t *p) +{ +} + +void destroyWidgetImage(widget_image_t *p) +{ + free(p); +} diff --git a/src/widget/widget_image.h b/src/widget/widget_image.h new file mode 100644 index 0000000..85c98d7 --- /dev/null +++ b/src/widget/widget_image.h @@ -0,0 +1,21 @@ + +#ifndef WIDGET_IMAGE_H + +#define WIDGET_IMAGE_H + +#include "base/main.h" +#include "client/interface.h" + +typedef struct widget_image +{ + int x, y; + SDL_Surface *image; +} widget_image_t; + +extern widget_image_t* newWidgetImage(int x, int y, SDL_Surface *image); +extern void drawWidgetImage(widget_image_t *p); +extern void eventWidgetImage(widget_image_t *p); +extern void destroyWidgetImage(widget_image_t *p); + +#endif + diff --git a/src/widget/widget_label.c b/src/widget/widget_label.c new file mode 100644 index 0000000..a472784 --- /dev/null +++ b/src/widget/widget_label.c @@ -0,0 +1,47 @@ + +#include + +#include "base/main.h" +#include "client/interface.h" +#include "client/font.h" +#include "widget_label.h" + +widget_label_t* newWidgetLabel(char *text, int x, int y, int bind) +{ + widget_label_t *new; + + new = malloc( sizeof(widget_label_t) ); + new->text = strdup(text); + new->x = x; + new->y = y; + new->bind = bind; + getTextSize(text, &new->w, &new->h); + + return new; +} + +void drawWidgetLabel(widget_label_t *p) +{ + switch( p->bind ) + { + case WIDGET_LABEL_RIGHT : + drawFont(p->text, p->x+p->w, p->y + p->h/2, COLOR_WHITE); + break; + case WIDGET_LABEL_LEFT : + drawFont(p->text, p->x, p->y + p->h/2, COLOR_WHITE); + break; + case WIDGET_LABEL_CENTER : + drawFont(p->text, p->x-p->w/2, p->y + p->h/2, COLOR_WHITE); + break; + } +} + +void eventWidgetLabel(widget_label_t *p) +{ +} + +void destroyWidgetLabel(widget_label_t *p) +{ + free(p->text); + free(p); +} diff --git a/src/widget/widget_label.h b/src/widget/widget_label.h new file mode 100644 index 0000000..a3bb060 --- /dev/null +++ b/src/widget/widget_label.h @@ -0,0 +1,26 @@ + +#ifndef WIDGET_LABEL_H + +#define WIDGET_LABEL_H + +#include "base/main.h" + +typedef struct widget_label +{ + int x, y; + int w, h; + char *text; + int bind; +} widget_label_t; + +#define WIDGET_LABEL_RIGHT 1 +#define WIDGET_LABEL_LEFT 2 +#define WIDGET_LABEL_CENTER 3 + +extern widget_label_t* newWidgetLabel(char *text, int x, int y, int bind); +extern void drawWidgetLabel(widget_label_t *p); +extern void eventWidgetLabel(widget_label_t *p); +extern void destroyWidgetLabel(widget_label_t *p); + +#endif + diff --git a/src/widget/widget_select.c b/src/widget/widget_select.c new file mode 100644 index 0000000..f51239c --- /dev/null +++ b/src/widget/widget_select.c @@ -0,0 +1,97 @@ + +#include +#include "base/main.h" +#include "client/interface.h" +#include "client/font.h" +#include "widget_select.h" + +/* +typedef struct widget_select +{ + int x, y; + int w, h; + list_t *list; + void (*fce_event)(void *); +} widget_select_t; +*/ + +widget_select_t* newWidgetSelect(int x, int y, void (*fce_event)(void *)) +{ + widget_select_t *new; + + new = malloc( sizeof(widget_select_t) ); + new->x = x; + new->y = y; + new->select = 0; + new->fce_event = fce_event; + new->list = newList(); + + return new; +} + +void addToWidgetSelect(widget_select_t *p, char *s) +{ + addList(p->list, strdup(s) ); +} + +void drawWidgetSelect(widget_select_t *p) +{ + int x, y, w, h; + int i; + + getMousePosition(&x, &y); + + for( i = 0 ; i < p->list->count ; i++ ) + { + char *line; + + line = (char *)p->list->list[i]; + + getTextSize(line, &w, &h); + + if( x > p->x && y > p->y + i*20 && x < p->x+w && y < p->y + i*20 + h ) + { + drawFont(line, p->x, p->y + i*20, COLOR_YELLOW); + } + else + { + if( p->select == i ) + { + drawFont(line, p->x, p->y + i*20, COLOR_RED); + } + else + { + drawFont(line, p->x, p->y + i*20, COLOR_WHITE); + } + } + } +} + +void eventWidgetSelect(widget_select_t *p) +{ + int x, y, w, h; + int i; + + getMousePosition(&x, &y); + + for( i = 0 ; i < p->list->count ; i++ ) + { + char *line; + + line = (char *)p->list->list[i]; + + getTextSize(line, &w, &h); + + if( x > p->x && y > p->y + i*20 && x < p->x+w && y < p->y + i*20 + h && isMouseClicked() ) + { + p->select = i; + p->fce_event(p); + } + } +} + +void destroyWidgetSelect(widget_select_t *p) +{ + destroyListItem(p->list, free); + free(p); +} diff --git a/src/widget/widget_select.h b/src/widget/widget_select.h new file mode 100644 index 0000000..2765f11 --- /dev/null +++ b/src/widget/widget_select.h @@ -0,0 +1,26 @@ + +#ifndef WIDGET_SELECT_H + +#define WIDGET_SELECT_H + +#include "base/main.h" +#include "base/list.h" + + +typedef struct widget_select +{ + int x, y; + int w, h; + list_t *list; + int select; + void (*fce_event)(void *); +} widget_select_t; + +extern widget_select_t* newWidgetSelect(int x, int y, void (*fce_event)(void *)); +extern void addToWidgetSelect(widget_select_t *p, char *s); +extern void drawWidgetSelect(widget_select_t *p); +extern void eventWidgetSelect(widget_select_t *p); +extern void destroyWidgetSelect(widget_select_t *p); + +#endif + diff --git a/src/widget/widget_textfield.c b/src/widget/widget_textfield.c new file mode 100644 index 0000000..f9186c9 --- /dev/null +++ b/src/widget/widget_textfield.c @@ -0,0 +1,194 @@ + +#include +#include "base/main.h" +#include "client/interface.h" +#include "client/font.h" +#include "client/image.h" +#include "widget_textfield.h" + +widget_textfield_t* newWidgetTextfield(char *text, int x, int y) +{ + widget_textfield_t *new; + + new = malloc( sizeof(widget_textfield_t) ); + strcpy(new->text, text); + new->x = x; + new->y = y; + new->time = 0; + new->timeBlick = 0; + new->active = FALSE; + getTextSize(text, &new->w, &new->h); + + return new; +} + +static void drawBackground(widget_textfield_t *p) +{ + static SDL_Surface *g_textfield0 = NULL; + static SDL_Surface *g_textfield1 = NULL; + int x, y; + + getMousePosition(&x, &y); + + if( g_textfield0 == NULL ) + { + g_textfield0 = addImageData("button0.png", IMAGE_ALPHA, "textfiled0", IMAGE_GROUP_BASE); + } + + if( g_textfield1 == NULL ) + { + g_textfield1 = addImageData("button1.png", IMAGE_ALPHA, "textfiled1", IMAGE_GROUP_BASE); + } + + if( p->active ) + { + drawImage(g_textfield1, p->x, p->y, 0, 0, g_textfield1->w, g_textfield1->h); + } + else + { + drawImage(g_textfield0, p->x, p->y, 0, 0, g_textfield0->w, g_textfield0->h); + } +} + +static void drawText(widget_textfield_t *p) +{ + char str[STR_SIZE]; + + strcpy(str, p->text); + + if( p->timeBlick > WIDGET_TEXTFIELD_TIME_BLICK_CURSOR/2 && + p->active == TRUE ) + { + //strcat(str, ">"); + strcat(str, "\f"); + } + + p->timeBlick++; + + if( p->timeBlick == WIDGET_TEXTFIELD_TIME_BLICK_CURSOR ) + { + p->timeBlick = 0; + } + + //drawFont(str, p->x+WIDGET_TEXTFIELD_TEXT_OFFSET_X, p->y+p->h/2, COLOR_WHITE); + drawFont(str, p->x+WIDGET_TEXTFIELD_TEXT_OFFSET_X, p->y+WIDGET_TEXTFIELD_HEIGHT/2-p->h/2, COLOR_WHITE); + //printf("p->y: %d\nheight: %d\n p->h: %d\n", p->y, WIDGET_TEXTFIELD_HEIGHT, p->h); +} + +void drawWidgetTextfield(widget_textfield_t *p) +{ + drawBackground(p); + drawText(p); +} + +static void readKey(widget_textfield_t *p) +{ + Uint8 *mapa; + int len; + int i; + + if( p->time > 0 ) + { + p->time--; + return; + } + + if( p->active == FALSE ) + { + return; + } + + mapa = SDL_GetKeyState(NULL); + len = strlen(p->text); + + // mazanie posledneho klavesu + if( mapa[SDLK_BACKSPACE] == SDL_PRESSED ) + { + if( len > 0 ) + { + p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; + p->text[len-1]='\0'; + getTextSize(p->text, &p->w, &p->h); + } + + return; + } + + if( p->w > WIDGET_TEXTFIELD_WIDTH-40 ) + { + return; + } + + // klavesy abecedy + for(i=SDLK_SPACE /*SDLK_a*/;i<=SDLK_z;i++) + { + //if(widthtime = WIDGET_TEXTFIELD_TIME_READ_KEY; + + strcat( p->text, SDL_GetKeyName(i) ); + getTextSize(p->text, &p->w, &p->h); + + if( mapa[SDLK_LSHIFT] == SDL_PRESSED || + mapa[SDLK_RSHIFT] == SDL_PRESSED) + { + p->text[ strlen( p->text ) - 1 ] -= 32; + getTextSize(p->text, &p->w, &p->h); + return; + } + } + } + + // aby ve jmene bulanka fungovaly take cislice alfanumericke klavesnice + for( i = SDLK_0 ; i <= SDLK_9 ; i++ ) + { + if( mapa[i] == SDL_PRESSED && len < STR_SIZE ) + { + p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; + strcat( p->text, SDL_GetKeyName(i) ); + getTextSize(p->text, &p->w, &p->h); + return; + } + } + + // aby ve jmene bulanka fungovaly take cislice napsane na numericke klavesnici + for( i=SDLK_KP0 ; i<=SDLK_KP9 ; i++ ) + { + if( mapa[i] == SDL_PRESSED && len < STR_SIZE ) + { + p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; + strncat( p->text, SDL_GetKeyName(i)+1, 1 ); // napr. "[4]" + getTextSize(p->text, &p->w, &p->h); + return; + } + } +} + +void eventWidgetTextfield(widget_textfield_t *p) +{ + int x, y; + + getMousePosition(&x, &y); + + if( isMouseClicked() ) + { + if( x >= p->x && x <= p->x+WIDGET_TEXTFIELD_WIDTH && + y >= p->y && y <= p->y+WIDGET_TEXTFIELD_HEIGHT ) + { + p->active = TRUE; + } + else + { + p->active = FALSE; + } + } + + readKey(p); +} + +void destroyWidgetTextfield(widget_textfield_t *p) +{ + free(p); +} + diff --git a/src/widget/widget_textfield.h b/src/widget/widget_textfield.h new file mode 100644 index 0000000..368f329 --- /dev/null +++ b/src/widget/widget_textfield.h @@ -0,0 +1,33 @@ + +#ifndef WIDGET_TEXTFIELD_H + +#define WIDGET_TEXTFIELD_H + +#include "base/main.h" + +#define WIDGET_TEXTFIELD_WIDTH 188 +#define WIDGET_TEXTFIELD_HEIGHT 36 + +#define WIDGET_TEXTFIELD_TEXT_OFFSET_X 10 + +#define WIDGET_TEXTFIELD_TIME_READ_KEY 2 +#define WIDGET_TEXTFIELD_TIME_BLICK_CURSOR 20 + +typedef struct widget_textfield +{ + int x, y; + int w, h; + int canWrite; + int timeBlick; + int time; + char text[STR_SIZE]; + bool_t active; +} widget_textfield_t; + +extern widget_textfield_t* newWidgetTextfield(char *text, int x, int y); +extern void drawWidgetTextfield(widget_textfield_t *p); +extern void eventWidgetTextfield(widget_textfield_t *p); +extern void destroyWidgetTextfield(widget_textfield_t *p); + +#endif + diff --git a/src/widget_button.c b/src/widget_button.c deleted file mode 100644 index 2638619..0000000 --- a/src/widget_button.c +++ /dev/null @@ -1,82 +0,0 @@ - -#include - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "widget_button.h" -#include "image.h" - -widget_button_t* newWidgetButton(char *text, int x, int y, void (*fce_event)(void *)) -{ - widget_button_t *new; - - new = malloc( sizeof(widget_button_t) ); - new->text = strdup(text); - new->x = x; - new->y = y; - new->fce_event = fce_event; - getTextSize(text, &new->w, &new->h); - - return new; -} - -void drawWidgetButton(widget_button_t *p) -{ - static SDL_Surface *g_button0 = NULL; - static SDL_Surface *g_button1 = NULL; - int x, y; - - getMousePosition(&x, &y); - - if( g_button0 == NULL ) - { - g_button0 = addImageData("button0.png", IMAGE_ALPHA, "button0", IMAGE_GROUP_BASE); - } - - if( g_button1 == NULL ) - { - g_button1 = addImageData("button1.png", IMAGE_ALPHA, "button1", IMAGE_GROUP_BASE); - } - - if( x >= p->x && x <= p->x+WIDGET_BUTTON_WIDTH && - y >= p->y && y <= p->y+WIDGET_BUTTON_HEIGHT ) - { - drawImage(g_button1, p->x, p->y, 0, 0, g_button0->w, g_button0->h); - } - else - { - drawImage(g_button0, p->x, p->y, 0, 0, g_button0->w, g_button0->h); - } - - //drawFont(p->text, p->x+WIDGET_BUTTON_WIDTH/2-p->w/2, p->y+p->h/2, COLOR_WHITE); - drawFont(p->text, p->x + WIDGET_BUTTON_WIDTH/2 - p->w/2, p->y + WIDGET_BUTTON_HEIGHT/2 - p->h/2, COLOR_WHITE); -} - -void eventWidgetButton(widget_button_t *p) -{ - static int time = 0; - int x, y; - - if( time > 0) - { - time--; - return; - } - - getMousePosition(&x, &y); - - if( x >= p->x && x <= p->x+WIDGET_BUTTON_WIDTH && - y >= p->y && y <= p->y+WIDGET_BUTTON_HEIGHT && - isMouseClicked() ) - { - time = WIDGET_BUTTON_TIME; - p->fce_event(p); - } -} - -void destroyWidgetButton(widget_button_t *p) -{ - free(p->text); - free(p); -} diff --git a/src/widget_buttonimage.c b/src/widget_buttonimage.c deleted file mode 100644 index db7e537..0000000 --- a/src/widget_buttonimage.c +++ /dev/null @@ -1,56 +0,0 @@ - -#include - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "widget_buttonimage.h" -#include "image.h" - -widget_buttonimage_t* newWidgetButtonimage(SDL_Surface *image, int x, int y, void (*fce_event)(void *)) -{ - widget_buttonimage_t *new; - - new = malloc( sizeof(widget_buttonimage_t) ); - new->image = image; - new->x = x; - new->y = y; - new->w = image->w/2; - new->h = image->h; - new->active = 0; - new->fce_event = fce_event; - - return new; -} - -void drawWidgetButtonimage(widget_buttonimage_t *p) -{ - drawImage(p->image, p->x, p->y, p->active*p->w, 0, p->w, p->h); -} - -void eventWidgetButtonimage(widget_buttonimage_t *p) -{ - static int time = 0; - int x, y; - - if( time > 0) - { - time--; - return; - } - - getMousePosition(&x, &y); - - if( x >= p->x && x <= p->x+p->w && - y >= p->y && y <= p->y+p->h && - isMouseClicked() ) - { - time = WIDGET_BUTTONIMAGE_TIME; - p->fce_event(p); - } -} - -void destroyWidgetButtonimage(widget_buttonimage_t *p) -{ - free(p); -} diff --git a/src/widget_check.c b/src/widget_check.c deleted file mode 100644 index 0222652..0000000 --- a/src/widget_check.c +++ /dev/null @@ -1,85 +0,0 @@ - -#include - -#include "main.h" -#include "interface.h" -#include "widget_check.h" -#include "image.h" - -widget_check_t* newWidgetCheck(int x, int y, bool_t status, void (*fce_event)(void *)) -{ - widget_check_t *new; - - new = malloc( sizeof(widget_check_t) ); - new->x = x; - new->y = y; - new->time = 0; - new->status = status; - new->fce_event = fce_event; - - return new; -} - -void drawWidgetCheck(widget_check_t *p) -{ - static SDL_Surface *g_check = NULL; - int x, y; - int offset; - - getMousePosition(&x, &y); - - if( g_check == NULL ) - { - g_check = addImageData("check.png", IMAGE_ALPHA, "check", IMAGE_GROUP_BASE); - } - - if( p->status == TRUE ) - { - offset = 0; - } - else - { - offset = WIDGET_CHECK_WIDTH; - } - - drawImage(g_check, p->x, p->y, offset, 0, WIDGET_CHECK_WIDTH, WIDGET_CHECK_HEIGHT); -} - -void eventWidgetCheck(widget_check_t *p) -{ - int x, y; - - if( p->time > 0 ) - { - p->time--; - return; - } - - getMousePosition(&x, &y); - - if( x >= p->x && x <= p->x+WIDGET_CHECK_WIDTH && - y >= p->y && y <= p->y+WIDGET_CHECK_HEIGHT && - isMouseClicked() ) - { - if( p->status == TRUE ) - { - p->status = FALSE; - p->time = WIDGET_CHECK_TIME_SWITCH_STATUS; - } - else - { - p->status = TRUE; - p->time = WIDGET_CHECK_TIME_SWITCH_STATUS; - } - - if( p->fce_event != NULL ) - { - p->fce_event(p); - } - } -} - -void destroyWidgetCheck(widget_check_t *p) -{ - free(p); -} diff --git a/src/widget_image.c b/src/widget_image.c deleted file mode 100644 index b3b895e..0000000 --- a/src/widget_image.c +++ /dev/null @@ -1,33 +0,0 @@ - -#include - -#include "main.h" -#include "interface.h" -#include "image.h" -#include "widget_image.h" - -widget_image_t* newWidgetImage(int x, int y, SDL_Surface *image) -{ - widget_image_t *new; - - new = malloc( sizeof(widget_image_t) ); - new->x = x; - new->y = y; - new->image = image; - - return new; -} - -void drawWidgetImage(widget_image_t *p) -{ - drawImage(p->image, p->x, p->y, 0, 0, p->image->w, p->image->h); -} - -void eventWidgetImage(widget_image_t *p) -{ -} - -void destroyWidgetImage(widget_image_t *p) -{ - free(p); -} diff --git a/src/widget_label.c b/src/widget_label.c deleted file mode 100644 index 15dea4d..0000000 --- a/src/widget_label.c +++ /dev/null @@ -1,47 +0,0 @@ - -#include - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "widget_label.h" - -widget_label_t* newWidgetLabel(char *text, int x, int y, int bind) -{ - widget_label_t *new; - - new = malloc( sizeof(widget_label_t) ); - new->text = strdup(text); - new->x = x; - new->y = y; - new->bind = bind; - getTextSize(text, &new->w, &new->h); - - return new; -} - -void drawWidgetLabel(widget_label_t *p) -{ - switch( p->bind ) - { - case WIDGET_LABEL_RIGHT : - drawFont(p->text, p->x+p->w, p->y + p->h/2, COLOR_WHITE); - break; - case WIDGET_LABEL_LEFT : - drawFont(p->text, p->x, p->y + p->h/2, COLOR_WHITE); - break; - case WIDGET_LABEL_CENTER : - drawFont(p->text, p->x-p->w/2, p->y + p->h/2, COLOR_WHITE); - break; - } -} - -void eventWidgetLabel(widget_label_t *p) -{ -} - -void destroyWidgetLabel(widget_label_t *p) -{ - free(p->text); - free(p); -} diff --git a/src/widget_select.c b/src/widget_select.c deleted file mode 100644 index dbd6fa9..0000000 --- a/src/widget_select.c +++ /dev/null @@ -1,99 +0,0 @@ - -#include - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "widget_select.h" -#include "image.h" - -/* -typedef struct widget_select -{ - int x, y; - int w, h; - list_t *list; - void (*fce_event)(void *); -} widget_select_t; -*/ - -widget_select_t* newWidgetSelect(int x, int y, void (*fce_event)(void *)) -{ - widget_select_t *new; - - new = malloc( sizeof(widget_select_t) ); - new->x = x; - new->y = y; - new->select = 0; - new->fce_event = fce_event; - new->list = newList(); - - return new; -} - -void addToWidgetSelect(widget_select_t *p, char *s) -{ - addList(p->list, strdup(s) ); -} - -void drawWidgetSelect(widget_select_t *p) -{ - int x, y, w, h; - int i; - - getMousePosition(&x, &y); - - for( i = 0 ; i < p->list->count ; i++ ) - { - char *line; - - line = (char *)p->list->list[i]; - - getTextSize(line, &w, &h); - - if( x > p->x && y > p->y + i*20 && x < p->x+w && y < p->y + i*20 + h ) - { - drawFont(line, p->x, p->y + i*20, COLOR_YELLOW); - } - else - { - if( p->select == i ) - { - drawFont(line, p->x, p->y + i*20, COLOR_RED); - } - else - { - drawFont(line, p->x, p->y + i*20, COLOR_WHITE); - } - } - } -} - -void eventWidgetSelect(widget_select_t *p) -{ - int x, y, w, h; - int i; - - getMousePosition(&x, &y); - - for( i = 0 ; i < p->list->count ; i++ ) - { - char *line; - - line = (char *)p->list->list[i]; - - getTextSize(line, &w, &h); - - if( x > p->x && y > p->y + i*20 && x < p->x+w && y < p->y + i*20 + h && isMouseClicked() ) - { - p->select = i; - p->fce_event(p); - } - } -} - -void destroyWidgetSelect(widget_select_t *p) -{ - destroyListItem(p->list, free); - free(p); -} diff --git a/src/widget_textfield.c b/src/widget_textfield.c deleted file mode 100644 index d61718f..0000000 --- a/src/widget_textfield.c +++ /dev/null @@ -1,194 +0,0 @@ -#include - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "widget_textfield.h" -#include "image.h" - -widget_textfield_t* newWidgetTextfield(char *text, int x, int y) -{ - widget_textfield_t *new; - - new = malloc( sizeof(widget_textfield_t) ); - strcpy(new->text, text); - new->x = x; - new->y = y; - new->time = 0; - new->timeBlick = 0; - new->active = FALSE; - getTextSize(text, &new->w, &new->h); - - return new; -} - -static void drawBackground(widget_textfield_t *p) -{ - static SDL_Surface *g_textfield0 = NULL; - static SDL_Surface *g_textfield1 = NULL; - int x, y; - - getMousePosition(&x, &y); - - if( g_textfield0 == NULL ) - { - g_textfield0 = addImageData("button0.png", IMAGE_ALPHA, "textfiled0", IMAGE_GROUP_BASE); - } - - if( g_textfield1 == NULL ) - { - g_textfield1 = addImageData("button1.png", IMAGE_ALPHA, "textfiled1", IMAGE_GROUP_BASE); - } - - if( p->active ) - { - drawImage(g_textfield1, p->x, p->y, 0, 0, g_textfield1->w, g_textfield1->h); - } - else - { - drawImage(g_textfield0, p->x, p->y, 0, 0, g_textfield0->w, g_textfield0->h); - } -} - -static void drawText(widget_textfield_t *p) -{ - char str[STR_SIZE]; - - strcpy(str, p->text); - - if( p->timeBlick > WIDGET_TEXTFIELD_TIME_BLICK_CURSOR/2 && - p->active == TRUE ) - { - //strcat(str, ">"); - strcat(str, "\f"); - } - - p->timeBlick++; - - if( p->timeBlick == WIDGET_TEXTFIELD_TIME_BLICK_CURSOR ) - { - p->timeBlick = 0; - } - - //drawFont(str, p->x+WIDGET_TEXTFIELD_TEXT_OFFSET_X, p->y+p->h/2, COLOR_WHITE); - drawFont(str, p->x+WIDGET_TEXTFIELD_TEXT_OFFSET_X, p->y+WIDGET_TEXTFIELD_HEIGHT/2-p->h/2, COLOR_WHITE); - //printf("p->y: %d\nheight: %d\n p->h: %d\n", p->y, WIDGET_TEXTFIELD_HEIGHT, p->h); -} - -void drawWidgetTextfield(widget_textfield_t *p) -{ - drawBackground(p); - drawText(p); -} - -static void readKey(widget_textfield_t *p) -{ - Uint8 *mapa; - int len; - int i; - - if( p->time > 0 ) - { - p->time--; - return; - } - - if( p->active == FALSE ) - { - return; - } - - mapa = SDL_GetKeyState(NULL); - len = strlen(p->text); - - // mazanie posledneho klavesu - if( mapa[SDLK_BACKSPACE] == SDL_PRESSED ) - { - if( len > 0 ) - { - p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; - p->text[len-1]='\0'; - getTextSize(p->text, &p->w, &p->h); - } - - return; - } - - if( p->w > WIDGET_TEXTFIELD_WIDTH-40 ) - { - return; - } - - // klavesy abecedy - for(i=SDLK_SPACE /*SDLK_a*/;i<=SDLK_z;i++) - { - //if(widthtime = WIDGET_TEXTFIELD_TIME_READ_KEY; - - strcat( p->text, SDL_GetKeyName(i) ); - getTextSize(p->text, &p->w, &p->h); - - if( mapa[SDLK_LSHIFT] == SDL_PRESSED || - mapa[SDLK_RSHIFT] == SDL_PRESSED) - { - p->text[ strlen( p->text ) - 1 ] -= 32; - getTextSize(p->text, &p->w, &p->h); - return; - } - } - } - - // aby ve jmene bulanka fungovaly take cislice alfanumericke klavesnice - for( i = SDLK_0 ; i <= SDLK_9 ; i++ ) - { - if( mapa[i] == SDL_PRESSED && len < STR_SIZE ) - { - p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; - strcat( p->text, SDL_GetKeyName(i) ); - getTextSize(p->text, &p->w, &p->h); - return; - } - } - - // aby ve jmene bulanka fungovaly take cislice napsane na numericke klavesnici - for( i=SDLK_KP0 ; i<=SDLK_KP9 ; i++ ) - { - if( mapa[i] == SDL_PRESSED && len < STR_SIZE ) - { - p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; - strncat( p->text, SDL_GetKeyName(i)+1, 1 ); // napr. "[4]" - getTextSize(p->text, &p->w, &p->h); - return; - } - } -} - -void eventWidgetTextfield(widget_textfield_t *p) -{ - int x, y; - - getMousePosition(&x, &y); - - if( isMouseClicked() ) - { - if( x >= p->x && x <= p->x+WIDGET_TEXTFIELD_WIDTH && - y >= p->y && y <= p->y+WIDGET_TEXTFIELD_HEIGHT ) - { - p->active = TRUE; - } - else - { - p->active = FALSE; - } - } - - readKey(p); -} - -void destroyWidgetTextfield(widget_textfield_t *p) -{ - free(p); -} - diff --git a/src/zal_pipe.c b/src/zal_pipe.c deleted file mode 100644 index 65ba6b2..0000000 --- a/src/zal_pipe.c +++ /dev/null @@ -1,256 +0,0 @@ - -#include -#include - -#include "pipe.h" -#include "layer.h" -#include "screen_world.h" -#include "shot.h" - -pipe_t* newPipe(int x, int y, int w, int h, int layer, int id, int id_out, int position, SDL_Surface *img) -{ - pipe_t *new; - - assert( img != NULL ); - - new = malloc( sizeof(pipe_t) ); - assert( new != NULL ); - - new->x = x; - new->y = y; - new->w = w; - new->h = h; - new->layer = layer; - new->id = id; - new->id_out = id_out; - new->position = position; - new->img = img; - - return new; -} - -void drawPipe(pipe_t *p) -{ - assert( p != NULL ); - - addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, p->layer); -} - -void drawListPipe(list_t *listPipe) -{ - pipe_t *thisPipe; - int i; - - assert( listPipe != NULL ); - - for( i = 0 ; i < listPipe->count ; i++ ) - { - thisPipe = (pipe_t *)listPipe->list[i]; - assert( thisPipe != NULL ); - drawPipe(thisPipe); - } -} - -pipe_t* isConflictWithListPipe(list_t *listPipe, int x, int y, int w, int h) -{ - pipe_t *thisPipe; - int i; - - assert( listPipe != NULL ); - - for( i = 0 ; i < listPipe->count ; i++ ) - { - thisPipe = (pipe_t *)listPipe->list[i]; - assert( thisPipe != NULL ); - - if( conflictSpace(x, y, w, h, - thisPipe->x, thisPipe->y, thisPipe->w, thisPipe->h) ) - { - return thisPipe; - } - } - - return NULL; -} - -static int negPosition(int n) -{ - switch( n ) - { - case TUX_UP : - return TUX_DOWN; - - case TUX_LEFT : - return TUX_RIGHT; - - case TUX_RIGHT : - return TUX_LEFT; - - case TUX_DOWN : - return TUX_UP; - - default : - assert( ! "premenna n ma zlu hodnotu !" ); - break; - } -} - -static pipe_t* getPipeId(list_t *listPipe, int id) -{ - int i; - - for( i = 0 ; i < listPipe->count ; i++ ) - { - pipe_t *thisPipe; - - thisPipe = (pipe_t *) listPipe->list[i]; - - if( thisPipe->id == id ) - { - return thisPipe; - } - } - - return NULL; -} - -static int myAbs(int n) -{ - return ( n > 0 ? n : -n ); -} - -static int getSppedShot(shot_t *shot) -{ - return ( myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py) ); -} - -static void transformShot(shot_t *shot, int position) -{ - int speed; - - speed = getSppedShot(shot); - - switch( position ) - { - case TUX_UP : - shot->px = 0; - shot->py = -speed; - break; - - case TUX_LEFT : - shot->px = -speed; - shot->py = 0; - break; - - case TUX_RIGHT : - shot->px = speed; - shot->py = 0; - break; - - case TUX_DOWN : - shot->px = 0; - shot->py = +speed; - break; - - } - - shot->position = position; - shot->isCanKillAuthor = TRUE; -} - -static void moveShot(shot_t *shot, pipe_t *pipe, list_t *listPipe) -{ - pipe_t *distPipe; - int offset; - - distPipe = getPipeId(listPipe, pipe->id_out); - - if( distPipe == NULL ) - { - fprintf(stderr, "Pipe %d ID not found\n", pipe->id); - return; - } - - printf("XXX\n"); - - switch( shot->position ) - { - case TUX_UP : - case TUX_DOWN : - offset = shot->x - pipe->x; - break; - - case TUX_RIGHT : - case TUX_LEFT : - offset = shot->y - pipe->y; - break; - } - - transformShot(shot, distPipe->position); - - switch( shot->position ) - { - case TUX_UP : - shot->x = distPipe->x + offset; - shot->y = distPipe->y; - break; - - case TUX_LEFT : - shot->x = distPipe->x; - shot->y = distPipe->y + offset; - break; - - case TUX_RIGHT : - shot->x = distPipe->x + distPipe->w; - shot->y = distPipe->y + offset; - break; - - case TUX_DOWN : - shot->x = distPipe->x + offset; - shot->y = distPipe->y + distPipe->h; - break; - } -} - -void eventConflictShotWithPipe(list_t *listPipe, list_t *listShot) -{ - shot_t *thisShot; - pipe_t *thisPipe; - int i; - - assert( listPipe != NULL ); - assert( listShot != NULL ); - - for( i = 0 ; i < listShot->count ; i++ ) - { - thisShot = (shot_t *)listShot->list[i]; - assert( thisShot != NULL ); - - if( ( thisPipe = isConflictWithListPipe(listPipe, thisShot->x, thisShot->y, - thisShot->w, thisShot->h) ) != NULL ) - { - if( thisShot->author->bonus == BONUS_GHOST && - thisShot->author->bonus_time > 0 ) - { - continue; - } - - if( negPosition( thisShot->position ) == thisPipe->position ) - { - moveShot(thisShot, thisPipe, listPipe); - } - else - { - delListItem(listShot, i, destroyShot); - i--; - } - } - } -} - -void destroyPipe(pipe_t *p) -{ - assert( p != NULL ); - free(p); -} - diff --git a/src/zal_proto.c b/src/zal_proto.c deleted file mode 100644 index a091e19..0000000 --- a/src/zal_proto.c +++ /dev/null @@ -1,692 +0,0 @@ - -#include -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "item.h" -#include "shot.h" -#include "arenaFile.h" -#include "myTimer.h" -#include "server.h" -#include "proto.h" -#include "net_multiplayer.h" - -#ifndef PUBLIC_SERVER -#include "network.h" -#include "screen_world.h" -#include "screen_setting.h" -#include "screen_choiceArena.h" -#include "client.h" -#include "term.h" -#endif - -#ifdef PUBLIC_SERVER -#include "publicServer.h" -#endif - -static void proto_send(int type, client_t *client, char *msg) -{ - assert( msg != NULL ); - - switch( type ) - { - case PROTO_SEND_ONE : - assert( client != NULL ); - sendClient(client, msg); -#ifdef DEBUG_SERVER_SEND - printf("send one client msg->%s\n", msg); -#endif - break; - case PROTO_SEND_ALL : - assert( client == NULL ); - sendAllClient(msg); -#ifdef DEBUG_SERVER_SEND - printf("send all clients msg->%s\n", msg); -#endif - break; - case PROTO_SEND_BUT : - assert( client != NULL ); - sendAllClientBut(msg, client); -#ifdef DEBUG_SERVER_SEND - printf("send but client msg->%s\n", msg); -#endif - break; - default : - assert( ! "Premenna type ma zlu hodnotu !" ); - break; - } -} - -void proto_send_error_server(int type, client_t *client, int errorcode) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "error %d\n", errorcode); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_error_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int errorcode; - - assert( msg != NULL ); - - sscanf(msg, "%s %d", - cmd, &errorcode); - - printf("proto error code %d\n", errorcode); -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_hello_client(char *name) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "hello %s %s\n", TUXANCI_NG_VERSION, name); - sendServer(msg); -} - -#endif - -void proto_recv_hello_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - char version[STR_NAME_SIZE]; - char name[STR_NAME_SIZE]; - - assert( client != NULL ); - - strcpy(version, ""); - strcpy(name, ""); - - sscanf(msg, "%s %s %s", cmd, version, name); - - if( strncmp(version, TUXANCI_NG_VERSION, strlen(TUXANCI_NG_VERSION)) != 0 ) - { - proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_VERSION); - client->status = NET_STATUS_ZOMBIE; - return; - } - - client->tux = newTux(); - client->tux->control = TUX_CONTROL_NET; - addList(getCurrentArena()->listTux, client->tux); - - if( strlen(name) > STR_NAME_SIZE-1 ) - { - name[STR_NAME_SIZE-1] = '\0'; - } - - strcpy(client->tux->name, name); - sendInfoCreateClient(client); -} - -void proto_send_status_server(int type, client_t *client) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "version: %s\n" - "clients: %d\n" - "maxclients: %d\n" - "uptime: %d\n", - TUXANCI_NG_VERSION, getCurrentArena()->listTux->count, - getServerMaxClients(), getMyTime() ); - - proto_send(type, client, msg); -} - -void proto_recv_status_server(client_t *client, char *msg) -{ - proto_send_status_server(PROTO_SEND_ONE, client); -} - -void proto_send_init_server(int type, client_t *client, client_t *client2) -{ - char msg[STR_PROTO_SIZE]; - int n = WORLD_COUNT_ROUND_UNLIMITED; - - assert( client2 != NULL ); - -#ifndef PUBLIC_SERVER - getSettingCountRound(&n); -#endif - - sprintf(msg, "init %d %d %d %d %s\n", - client2->tux->id, client2->tux->x, client2->tux->y, - n, getArenaNetName( getChoiceArenaId() )); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_init_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - char arena_name[STR_PROTO_SIZE]; - tux_t *tux; - int id; - int x, y, n; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d %d %d %s", - cmd, &id, &x, &y, &n, arena_name); - - setWorldArena( getArenaIdFormNetName(arena_name) ); - setMaxCountRound(n); - - tux = newTux(); - tux->id = id; - tux->x = x; - tux->y = y; - tux->control = TUX_CONTROL_KEYBOARD_RIGHT; - - getSettingNameRight(tux->name); - - addList(getCurrentArena()->listTux, tux); -} - -#endif - -void proto_send_event_server(int type, client_t *client, tux_t *tux, int action) -{ - char msg[STR_PROTO_SIZE]; - - assert( tux != NULL ); - - sprintf(msg, "event %d %d\n", tux->id, action); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_event_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, action; - tux_t *tux; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d", cmd, &id, &action); - - tux = getTuxID(getCurrentArena()->listTux, id); - - if( tux != NULL ) - { - actionTux(tux, action); - } -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_event_client(int action) -{ - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "event %d\n", action); - - sendServer(msg); -} - -#endif - -void proto_recv_event_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int action; - - assert( client != NULL ); - assert( msg != NULL ); - - sscanf(msg, "%s %d", cmd, &action); - - proto_send_event_server(PROTO_SEND_BUT, client, client->tux, action); - - actionTux(client->tux, action); - -} - -void proto_send_newtux_server(int type, client_t *client, tux_t *tux) -{ - char msg[STR_PROTO_SIZE]; - int x, y; - - assert( tux != NULL ); - - if( tux->bonus == BONUS_HIDDEN ) - { - x = TUX_LOCATE_UNKNOWN; - y = TUX_LOCATE_UNKNOWN; - } - else - { - x = tux->x; - y = tux->y; - } - - sprintf(msg, "newtux %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d\n", - tux->id ,x, y, tux->status, tux->position ,tux->frame, tux->score, tux->name, - tux->gun, tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE], - tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY], tux->shot[GUN_LASSER], - tux->shot[GUN_MINE], tux->shot[GUN_BOMBBALL], - tux->bonus_time, tux->pickup_time); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_newtux_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - char name[STR_NAME_SIZE]; - int id, x, y, status, position, frame, score; - int myGun, myBonus; - int gun1, gun2, gun3, gun4, gun5, gun6, gun7; - int time1, time2; - tux_t *tux; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d", - cmd, &id, &x, &y, &status, &position, &frame, &score, name, - &myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, &gun7, &time1, &time2); - - tux = getTuxID(getCurrentArena()->listTux, id); - - if( tux == NULL ) - { - char term_msg[STR_SIZE]; - sprintf(term_msg, "connect new client( id = %d)\n", id); - appendTextInTerm(term_msg); - - tux = newTux(); - tux->control = TUX_CONTROL_NET; - addList(getCurrentArena()->listTux, tux); - } - - tux->id = id; - - tux->x = x; - tux->y = y; - tux->status = status; - tux->position = position; - tux->frame = frame; - tux->score = score; - strcpy(tux->name, name); - tux->gun = myGun; - tux->bonus = myBonus; - tux->shot[GUN_SIMPLE] = gun1; - tux->shot[GUN_DUAL_SIMPLE] = gun2; - tux->shot[GUN_SCATTER] = gun3; - tux->shot[GUN_TOMMY] = gun4; - tux->shot[GUN_LASSER] = gun5; - tux->shot[GUN_MINE] = gun6; - tux->shot[GUN_BOMBBALL] = gun7; - tux->bonus_time = time1; - tux->pickup_time = time2; -} - -#endif - -void proto_send_kill_server(int type, client_t *client, tux_t *tux) -{ - char msg[STR_PROTO_SIZE]; - - assert( tux != NULL ); - - sprintf(msg, "kill %d\n", tux->id); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_kill_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id; - tux_t *tux; - - assert( msg != NULL ); - - sscanf(msg, "%s %d", cmd, &id); - - tux = getTuxID(getCurrentArena()->listTux, id); - - if( tux != NULL ) - { - eventTuxIsDead(tux); - } -} - -#endif - -void proto_send_score_server(int type, client_t *client, tux_t *tux) -{ - char msg[STR_PROTO_SIZE]; - - assert( tux != NULL ); - - sprintf(msg, "score %d %d\n", tux->id, tux->score); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_score_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, score; - tux_t *tux; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d", cmd, &id, &score); - - tux = getTuxID(getCurrentArena()->listTux, id); - - if( tux != NULL ) - { - char term_msg[STR_SIZE]; - sprintf(term_msg, "tux with id %d set score to %d\n", tux->id, score); - appendTextInTerm(term_msg); - - tux->score = score; - } - - countRoundInc(); -} - -#endif - -void proto_send_deltux_server(int type, client_t *client, client_t *client2) -{ - char msg[STR_PROTO_SIZE]; - - assert( client2 != NULL ); - - sprintf(msg, "deltux %d\n", client2->tux->id); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_deltux_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id; - tux_t *tux; - - assert( msg != NULL ); - - sscanf(msg, "%s %d", cmd, &id); - - tux = getTuxID(getCurrentArena()->listTux, id); - - if( tux != NULL ) - { - char term_msg[STR_SIZE]; - int index; - - sprintf(term_msg, "tux with id %d is disconnect\n", tux->id); - appendTextInTerm(term_msg); - - index = searchListItem(getCurrentArena()->listTux, tux); - delListItem(getCurrentArena()->listTux, index, destroyTux); - } -} - -#endif - -void proto_send_additem_server(int type, client_t *client, item_t *p) -{ - char msg[STR_PROTO_SIZE]; - int id = -1; - - assert( p != NULL ); - - if( p->author != NULL ) - { - id = p->author->id; - } - - sprintf(msg, "additem %d %d %d %d %d %d %d\n", - p->id, p->type, p->x, p->y, p->count, p->frame, id); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_additem_client(char *msg) -{ - char term_msg[STR_SIZE]; - char cmd[STR_PROTO_SIZE]; - int id, type, x, y, count, frame, author_id; - item_t *item; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d %d %d %d %d %d", cmd, &id, &type, &x, &y, &count, &frame, &author_id); - - if( getItemID(getCurrentArena()->listItem, id) != NULL ) - { - return; - } - - item = newItem(x, y, type, getTuxID(getCurrentArena()->listTux, author_id)); -/* - if( isConflictWithListItem(getCurrentArena()->listItem, item->x, item->y, item->w, item->h) ) - { - destroyItem(item); - return; - } -*/ - item->id = id; - item->count = count; - item->frame = frame; - - addList(getCurrentArena()->listItem, item); - - sprintf(term_msg, "in arena is new item\n"); - appendTextInTerm(term_msg); - -} - -#endif - -void proto_send_item_server(int type, client_t *client, tux_t *tux, item_t *item) -{ - char msg[STR_PROTO_SIZE]; - int tux_id = -1; - - assert( item != NULL ); - - if( tux != NULL ) - { - tux_id = tux->id; - } - - sprintf(msg, "item %d %d\n", - tux_id, item->id); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_item_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int tux_id, item_id; - arena_t *arena; - - item_t *item; - tux_t *tux; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d", cmd, &tux_id, &item_id); - - arena = getCurrentArena(); - - item = getItemID(arena->listItem, item_id); - tux = getTuxID(arena->listTux, tux_id); - - if( item == NULL) - { - return; - } - - if( tux != NULL ) - { - eventGiveTuxItem(tux, arena->listItem, item); - } - else - { - mineExplosion(arena->listItem, item); - } -} - -#endif - -void proto_send_shot_server(int type, client_t *client, shot_t *p) -{ - char msg[STR_PROTO_SIZE]; - int id = -1; - - assert( p != NULL ); - - if( p->author != NULL ) - { - id = p->author->id; - } - - sprintf(msg, "shot %d %d %d %d %d %d %d %d\n", - p->x, p->y, p->px, p->py, p->position, p->gun, id, p->isCanKillAuthor); - - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_shot_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int x, y, px, py, position, gun, id, isCanKillAuthor; - shot_t *shot; - - assert( msg != NULL ); - - sscanf(msg, "%s %d %d %d %d %d %d %d %d", - cmd, &x, &y, &px, &py, &position, &gun, &id, &isCanKillAuthor); - - shot = newShot(x, y, px, py, gun, getTuxID(getCurrentArena()->listTux, id)); - - shot->isCanKillAuthor = isCanKillAuthor; - shot->position = position; - - if( shot->gun == GUN_LASSER ) - { - transformOnlyLasser(shot); - } - - addList(getCurrentArena()->listShot, shot); -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_ping_client() -{ - char msg[STR_PROTO_SIZE]; - sprintf(msg, "ping %d\n", getMyTime()); - sendServer(msg); -} - -#endif - -void proto_recv_ping_server(client_t *client, char *msg) -{ - proto_send_ping_server(PROTO_SEND_ONE, client, msg); -} - -void proto_send_ping_server(int type, client_t *client, char *msg) -{ - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_ping_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - my_time_t currentTime; - my_time_t time; - - sscanf(msg, "%s %d", cmd, &time); - - currentTime = getMyTime(); - printf("lag = %d\n", (currentTime-time)); - setLagServer((currentTime-time)); -} - -#endif - -void proto_send_end_server(int type, client_t *client) -{ - char msg[STR_PROTO_SIZE]; - strcpy(msg, "end\n"); - proto_send(type, client, msg); -} - -#ifndef PUBLIC_SERVER - -void proto_recv_end_client(char *msg) -{ - assert( msg != NULL ); - - setWorldEnd(); -} - -#endif - -#ifndef PUBLIC_SERVER - -void proto_send_end_client() -{ - char msg[STR_PROTO_SIZE]; - strcpy(msg, "end\n"); - sendServer(msg); -} - -#endif - -void proto_recv_end_server(client_t *client, char *msg) -{ - assert( msg != NULL ); - assert( client != NULL ); - - client->status = NET_STATUS_ZOMBIE; -} - -- cgit v1.2.3