Skip to content

Commit

Permalink
Fixed multiple mistakes
Browse files Browse the repository at this point in the history
- Default configuration is debug, releases are stripped.
- Map is included into release.
- Unconditional repaint of stdscr from scratch every 1 second. Allows to outlive glitches if any.
- Fixed server console command 'dump here' and disabled 'kick 16'.
- Fixed implicit fallthrough warnings in state machine source.
- User terminal size does not matter anymore, just won't draw more than 80x24.
- Removed redundant MOVE, FIRE, ROTATE message sendings from client, when its state doesn't allows it.
  • Loading branch information
Str1ker17 committed Aug 9, 2018
1 parent c677a45 commit e7f3207
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 67 deletions.
24 changes: 13 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@ matrix:
- name: GCC
env: CC=gcc CC_ARCH=x64_gcc
before_script:
script: CONFIG=debug make projects tests && make
script:
- make projects tests
- CONFIG=release make
- name: Clang
env: CC=clang CC_ARCH=x64_clang
before_script:
script: CONFIG=debug make projects tests && make
script:
- make projects tests
- CONFIG=release make
- name: Linaro ARM
env:
- CROSS_COMPILE="$(pwd)/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc"
- CC=$CROSS_COMPILE
- CROSS_COMPILE="$(pwd)/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-"
- CC_ARCH=armv7l
before_script:
- wget -nv http://releases.linaro.org/components/toolchain/binaries/4.9-2016.02/arm-linux-gnueabihf/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf.tar.xz
- tar -xf gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf.tar.xz --checkpoint=.100
script:
- CONFIG=debug make -f Makefile.ARM projects tests
- make -f Makefile.ARM
- make -f Makefile.ARM projects tests
- CONFIG=release make -f Makefile.ARM
- name: OpenWRT MIPS
env:
- CROSS_COMPILE="$(pwd)/lede-sdk-17.01.4-ar71xx-generic_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.16/bin/mips-openwrt-linux-musl-gcc"
- CROSS_COMPILE="$(pwd)/lede-sdk-17.01.4-ar71xx-generic_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.16/bin/mips-openwrt-linux-musl-"
- STAGING_DIR=/tmp
- CC=$CROSS_COMPILE
- CC_ARCH=mips-24kc
before_script:
- wget -nv https://downloads.openwrt.org/releases/17.01.4/targets/ar71xx/generic/lede-sdk-17.01.4-ar71xx-generic_gcc-5.4.0_musl-1.1.16.Linux-x86_64.tar.xz
- tar -xf lede-sdk-17.01.4-ar71xx-generic_gcc-5.4.0_musl-1.1.16.Linux-x86_64.tar.xz --checkpoint=.100
script:
- CONFIG=debug make -f Makefile.MIPS projects tests
- make -f Makefile.MIPS
before_deploy: mkdir release && cd bin && ls -1 . | zip -9o@ ../release/bcs-$CC_ARCH.zip && cd .. && ls -1 release/*
- make -f Makefile.MIPS projects tests
- CONFIG=release make -f Makefile.MIPS
before_deploy: mkdir release && cp res/propeller.bcsmap bin && cd bin && ls -1 . | zip -9o@ ../release/bcs-$CC_ARCH.zip && cd .. && ls -1 release/*
deploy:
provider: releases
user: Str1ker17
Expand Down
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ distclean:
# TODO: apply these changes to cross-platform Makefiles
config:
ifeq ($(strip $(CONFIG)),)
CONFIG := release
CONFIG := debug
endif

ifeq ($(CONFIG),release)
CFLAGS += -O3 -g -D NDEBUG=1 -D RELEASE=1
ifeq ($(strip $(CONFIG)),release)
CFLAGS += -O3 -D NDEBUG=1 -D RELEASE=1
LDFLAGS += -s
endif

# dirty fix for visual studio
ifeq ($(CONFIG),Debug)
ifeq ($(strip $(CONFIG)),Debug)
CONFIG := debug
endif

ifeq ($(CONFIG),debug)
ifeq ($(strip $(CONFIG)),debug)
CFLAGS += -O0 -ggdb -ffunction-sections -D DEBUG=1
CFLAGS += -D VALGRIND_SUCKS
endif
Expand Down
11 changes: 6 additions & 5 deletions Makefile.ARM
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ include $(ROOTDIR)/src-build/default-config.mk
# If you want to move to another architecture, you might have to recompile ncurses from sources!
# cross-compiler
ifeq ($(strip $(CROSS_COMPILE)),)
CROSS_COMPILE := /home/striker/bin/cross-compile/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
CROSS_COMPILE := /home/striker/bin/cross-compile/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
endif

override CC := $(CROSS_COMPILE)
override CC := $(CROSS_COMPILE)gcc
# whole solution flags
CFLAGS := -ansi -Wextra -pedantic -Wno-unused-parameter
CFLAGS += -mabi=aapcs-linux -mfloat-abi=hard -march=armv7 -I$(ROOTDIR)/src-build/arch/arm-hf/include -D TIMEVAL16
Expand Down Expand Up @@ -44,14 +44,15 @@ distclean:
# To build one project, for ex. `cs` in debug configuration, type `CONFIG=debug make cs`
config:
ifeq ($(strip $(CONFIG)),)
CONFIG := release
CONFIG := debug
endif

ifeq ($(CONFIG),release)
ifeq ($(strip $(CONFIG)),release)
CFLAGS += -O3 -D NDEBUG=1 -D RELEASE=1
LDFLAGS += -s
endif

ifeq ($(CONFIG),debug)
ifeq ($(strip $(CONFIG)),debug)
CFLAGS += -O0 -ggdb -ffunction-sections -D DEBUG=1
CFLAGS += -D VALGRIND_SUCKS
endif
Expand Down
11 changes: 6 additions & 5 deletions Makefile.MIPS
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ include $(ROOTDIR)/src-build/default-config.mk
# If you want to move to another architecture, you might have to recompile ncurses from sources!
# cross-compiler
ifeq ($(strip $(CROSS_COMPILE)),)
CROSS_COMPILE := /home/striker/bin/cross-compile/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.16/bin/mips-openwrt-linux-gcc
CROSS_COMPILE := /home/striker/bin/cross-compile/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.16/bin/mips-openwrt-linux-
endif

override CC := $(CROSS_COMPILE)
override CC := $(CROSS_COMPILE)gcc
# whole solution flags
CFLAGS := -ansi -Wextra -pedantic -Wno-unused-parameter
CFLAGS += -march=24kc -mtune=24kc -msoft-float -I$(ROOTDIR)/src-build/arch/mips/include -D TIMEVAL16
Expand Down Expand Up @@ -47,14 +47,15 @@ distclean:
# To build one project, for ex. `cs` in debug configuration, type `CONFIG=debug make cs`
config:
ifeq ($(strip $(CONFIG)),)
CONFIG := release
CONFIG := debug
endif

ifeq ($(CONFIG),release)
ifeq ($(strip $(CONFIG)),release)
CFLAGS += -O3 -D NDEBUG=1 -D RELEASE=1
LDFLAGS += -s
endif

ifeq ($(CONFIG),debug)
ifeq ($(strip $(CONFIG)),debug)
CFLAGS += -O0 -ggdb -ffunction-sections -D DEBUG=1
CFLAGS += -D VALGRIND_SUCKS
endif
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $(SUBDIRS): subdirs

# warning: bash-specific script...
last-config:
@(echo [i] Compiler is $(CC), linker is $(LD), configuration is $(CONFIG) && \
@((echo -n "[i] Compiler is $(CC), linker is $(LD), configuration is " && tput setaf 1 && echo $(CONFIG) && tput sgr0) && \
echo [i] CFLAGS are: $(CFLAGS) && \
echo [i] LDFLAGS are: $(LDFLAGS)) > "$(ROOTDIR)/make.config.now"
@if [[ $$SILENCE_IS_GOLDEN -ne 1 ]] ; then cat "$(ROOTDIR)/make.config.now" ; fi
Expand All @@ -47,7 +47,7 @@ clean:
-rm -rf "$(LIBDIR)"
-rm -rf "$(OBJDIR)"
-rm -f "$(ROOTDIR)/make.config.prev"

targets := $(addprefix clean-, $(SUBDIRS))

$(targets):
Expand Down
62 changes: 47 additions & 15 deletions src/cs/cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
#define STATS_WIDTH (BCSPLAYER_NICKLEN + 5 + 5 + 2)
#define STATS_HEIGHT (BCSSERVER_MAXCLIENTS + 2)

#define MAPVIEW_WIDTH 80
#define MAPVIEW_HEIGHT 24

// WARNING: this is a workaround for dumb C libraries like musl
typedef union sigval sigval_t;

Expand Down Expand Up @@ -234,6 +237,8 @@ void *receiver_func(void *argv) {
break;

case BCSREPLT_SHUTDOWN:
// don't quit until frame is not drawn
pthread_mutex_lock(&pfs->mutex_frame);
curs_set(true);
endwin();
printf(ANSI_COLOR_RED "Server shutted down" ANSI_CLRST "\n");
Expand Down Expand Up @@ -320,11 +325,28 @@ void do_action(BCSPLAYER_FULL_STATE *pfs, BCSACTION action, BCSDIRECTION dir) {
, .un.long_p = 0
};


pthread_mutex_lock(&pfs->mutex_self);
BCSCLST state = pfs->self.state;
pthread_mutex_unlock(&pfs->mutex_self);

switch(action) {
case BCSACTION_DISCONNECT: break;
case BCSACTION_MOVE: msg.un.ints.int_lo = htobe32(dir); break;
case BCSACTION_FIRE: break;
case BCSACTION_MOVE: {
// не спамим на сервер если ничё не можем
if(state != BCSCLST_CONNECTED && state != BCSCLST_PLAYING)
return;
msg.un.ints.int_lo = htobe32(dir);
} break;
case BCSACTION_FIRE: {
// не спамим на сервер если ничё не можем
if(state != BCSCLST_CONNECTED && state != BCSCLST_PLAYING)
return;
} break;
case BCSACTION_ROTATE: {
// не спамим на сервер если ничё не можем
if(state != BCSCLST_PLAYING)
return;
switch(dir) {
case BCSDIR_LEFT:
case BCSDIR_RIGHT:
Expand Down Expand Up @@ -361,21 +383,24 @@ void draw_window(BCSPLAYER_FULL_STATE *pfs) {

// блокируем этот мьютекс только в случае чтения изменяющихся данных или записи изменений
pthread_mutex_lock(&pfs->mutex_self);
//size_t frames =
++pfs->frames;
size_t frames = ++pfs->frames;
BCSCLIENT_PUBLIC self = pfs->self;
BCSMAP map = pfs->map;
WINDOW *mappad = pfs->mappad;
WINDOW *mapobj = pfs->mapobj;
//WINDOW *below = pfs->below;
WINDOW *stats = pfs->stats;

// стата
bool show_stats = pfs->show_stats;
pthread_mutex_unlock(&pfs->mutex_self);
uint16_t others_count = pfs->others.count;
//pthread_mutex_unlock(&pfs->mutex_self);

//nassert(clearok(stdscr, true));
nassert(touchwin(stdscr));
nassert(touchwin(mapobj));
// repaint from scratch every second
if((frames % 30) == 0)
nassert(clearok(stdscr, true));
//nassert(touchwin(stdscr));
//nassert(touchwin(mapobj));
nassert(werase(stdscr));
nassert(werase(mapobj));

Expand All @@ -387,7 +412,7 @@ void draw_window(BCSPLAYER_FULL_STATE *pfs) {
}

// lock for all enemies, I don't think this is slow
pthread_mutex_lock(&pfs->mutex_self);
//pthread_mutex_lock(&pfs->mutex_self);
for(uint16_t i = 0; i < pfs->others.count; i++) {
if(i != pfs->others.index_self && pfs->others.array[i].state == BCSCLST_PLAYING) {
BCSCLIENT_PUBLIC enemy = pfs->others.array[i];
Expand Down Expand Up @@ -419,7 +444,8 @@ void draw_window(BCSPLAYER_FULL_STATE *pfs) {
if(stats->_clear) {
stats->_clear = false;
nassert(werase(stats));
nassert(box(stats, '#', '#'));
nassert(wresize(stats, others_count + 2, getmaxx(stats) + 1));
nassert(wborder(stats, '|', '|', '=', '=', '#', '#', '#', '#'));
for(uint16_t i = 0; i < pfs->others.count; ++i) {
nassert(mvwaddattrfstr(stats, i + 1, 1, BCSPLAYER_NICKLEN
, pfs->others.stats[i].nickname, A_NORMAL));
Expand All @@ -435,15 +461,18 @@ void draw_window(BCSPLAYER_FULL_STATE *pfs) {
// следующие четыре - куда на экран проецируем
//nassert(touchwin(mappad));
//nassert(touchwin(mapobj));
// фиксим размеры: пришло время
int size_y = min(MAPVIEW_HEIGHT, min(h + 1, map.height));
int size_x = min(MAPVIEW_WIDTH, min(w + 1, map.width));
//nassert(pnoutrefresh(mappad, 0, 0, 0, 0, 0 + h, 0 + w));
nassert(copywin(mappad, stdscr, 0, 0, 0, 0, 0 + h - 1, 0 + w - 1, true));
nassert(copywin(mapobj, stdscr, 0, 0, 0, 0, 0 + h - 1, 0 + w - 1, true));
nassert(copywin(mappad, stdscr, 0, 0, 0, 0, size_y - 1, size_x - 1, true));
nassert(copywin(mapobj, stdscr, 0, 0, 0, 0, size_y - 1, size_x - 1, true));
//nassert(pnoutrefresh(mapobj, 0, 0, 0, 0, 0 + h, 0 + w));
//nassert(wnoutrefresh(mapobj));

if(show_stats) {
//pnoutrefresh(stats, 0, 0, 0, 0, STATS_HEIGHT, STATS_WIDTH);
nassert(copywin(stats, stdscr, 0, 0, 0, 0, STATS_HEIGHT - 1, STATS_WIDTH, false));
nassert(copywin(stats, stdscr, 0, 0, 0, 0, others_count + 1, STATS_WIDTH - 1, false));
}

// эта панелька наложится поверх карты
Expand Down Expand Up @@ -534,7 +563,8 @@ int main(int argc, char **argv) {
if (getenv("LOGNAME") != NULL)
strncpy(pfs.self_ext.nickname, getenv("LOGNAME"), BCSPLAYER_NICKLEN);
else
strncpy(pfs.self_ext.nickname, cuserid(NULL), BCSPLAYER_NICKLEN);
//strncpy(pfs.self_ext.nickname, cuserid(NULL), BCSPLAYER_NICKLEN);
ALOGW("Could not determine your username\n");
}

if(argc > 1) {
Expand Down Expand Up @@ -939,7 +969,7 @@ int main(int argc, char **argv) {
pthread_t receiver_thread;
lassert(pthread_create(&receiver_thread, NULL, receiver_func, &pfs) == 0);

bool has_pressed = true;
bool has_pressed = false;
while(true) {
/////////////////////////
// ОТРИСОВКА //
Expand Down Expand Up @@ -1022,6 +1052,8 @@ int main(int argc, char **argv) {
}

loop_leave:
// don't quit until frame is not drawn
pthread_mutex_lock(&pfs.mutex_frame);
curs_set(true);
endwin();

Expand Down
31 changes: 18 additions & 13 deletions src/csds/csds.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ void dump_state(BCSSERVER_FULL_STATE *state, FILE *f) {
}
fprintf(f, "\n");
}
fclose(f);
pthread_mutex_unlock(&state->mutex_self);
}

Expand Down Expand Up @@ -493,19 +492,24 @@ int main(int argc, char **argv) {
linkedlist_init(&state.bullets);

// map loading
char mapfile_name[PATH_MAX] = "res/propeller.bcsmap";
while(true) {
if(bcsmap_load(mapfile_name, &(state.map)))
break;
ALOGW("Could not load map from file '%s'\n", mapfile_name);
printf("Enter filename of map in .bcsmap format: ");
fflush(stdout); // for sure
if(fgets(mapfile_name, PATH_MAX, stdin) == NULL) {
ALOGE("Fatal error: fgets() failed\n");
exit(EXIT_FAILURE);
char mapfile_name[PATH_MAX] = "propeller.bcsmap";
if( !bcsmap_load("res/propeller.bcsmap", &(state.map))
&& !bcsmap_load("propeller.bcsmap", &(state.map))
) {
while(true) {
ALOGW("Could not load map from file '%s'\n", mapfile_name);
printf("Enter filename of map in .bcsmap format: ");
fflush(stdout); // for sure
if(fgets(mapfile_name, PATH_MAX, stdin) == NULL) {
ALOGE("Fatal error: fgets() failed\n");
exit(EXIT_FAILURE);
}
mapfile_name[strlen(mapfile_name) - 1] = '\0';
if(bcsmap_load(mapfile_name, &(state.map)))
break;
}
mapfile_name[strlen(mapfile_name) - 1] = '\0';
}
ALOGI("Map '%s' loaded.\n", mapfile_name);

// allocate memory for overlay
state.map_overlay.width = state.map.width;
Expand Down Expand Up @@ -629,7 +633,7 @@ int main(int argc, char **argv) {
printf("[$] Dump is in front of you.\n");
}
else if(sscanf(buf, "kick %hu", &id) == 1) {
if (id <= BCSSERVER_MAXCLIENTS) {
if (id < BCSSERVER_MAXCLIENTS) {
pthread_mutex_lock(&state.mutex_self);
if (state.client[id].public_info.state == BCSCLST_FREESLOT) {
printf("[$] The slot %u is already free.\n", id);
Expand All @@ -638,6 +642,7 @@ int main(int argc, char **argv) {
BCSMSGREPLY repl = { .type = htobe32(BCSREPLT_SHUTDOWN) };
sendto2(state.sock_u, &repl, sizeof(BCSMSGREPLY), MSG_DONTWAIT
,(sockaddr*)&(state.client[id].private_info.endpoint), sizeof(sockaddr_in));
--(state.player_count);
memset(&state.client[id], 0, sizeof(BCSCLIENT));
printf("[$] Kicked %u for you.\n", id);
}
Expand Down
Loading

0 comments on commit e7f3207

Please sign in to comment.