From 8a1e000cf813117487cd9122a963bb289deb4b19 Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Fri, 28 Feb 2014 13:51:37 -0430 Subject: [PATCH] First commit. --- .gitignore | 19 ++++ LICENSE | 23 +++++ Makefile | 23 +++++ README.md | 4 + include/constants.h | 22 ++++ include/fov.h | 245 ++++++++++++++++++++++++++++++++++++++++++++ include/island.h | 63 ++++++++++++ lib/libfov.a | Bin 0 -> 16870 bytes lib/libisland.a | Bin 0 -> 4796 bytes libfov.COPYING | 22 ++++ src/main.c | 193 ++++++++++++++++++++++++++++++++++ 11 files changed, 614 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 include/constants.h create mode 100644 include/fov.h create mode 100644 include/island.h create mode 100644 lib/libfov.a create mode 100644 lib/libisland.a create mode 100644 libfov.COPYING create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b3504ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Object files +*.o +*.ko + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib +obj/ + +# Executables +*.exe +*.out +*.app +bin/ + +# Emacs backups +*~ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..738f296 --- /dev/null +++ b/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) 2014, Miguel Angel Astor Romero +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c2c281c --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +CC = gcc +SOURCES = src/main.c +OBJECTS = obj/main.o +TARGET = bin/cyjam +CFLAGS = -Wall -I./include +LDFLAGS = -L./lib +LDLIBS = -lm -lisland -lfov -lncurses + +all: CFLAGS += -O3 +all: $(TARGET) + +debug: CFLAGS += -g +debug: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CC) -o $(TARGET) $(OBJECTS) $(CLFAGS) $(LDFLAGS) $(LDLIBS) + +obj/main.o: src/main.c + $(CC) -c -o $@ $< $(CFLAGS) + +clean: + $(RM) $(TARGET) $(OBJECTS) + diff --git a/README.md b/README.md new file mode 100644 index 0000000..582c256 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +TITLE PENDING +=================== + +A game for the Cyberpunk Jam 2014. diff --git a/include/constants.h b/include/constants.h new file mode 100644 index 0000000..ae042e0 --- /dev/null +++ b/include/constants.h @@ -0,0 +1,22 @@ +#pragma once +#ifndef STATE_CONSTS_H +#define STATE_CONSTS_H + +static const int BAR_COLOR = 1; +static const int BSC_COLOR = 2; +static const int HLT_COLOR = 3; +static const int OFF_COLOR = 4; +static const int DIM_COLOR = 5; +static const int LIT_COLOR = 6; +static const int GUI_COLOR = 7; +static const int EMP_COLOR = 8; + +static const int INTRO_STATE = 0; +static const int MENU_STATE = 1; +static const int LOAD_STATE = 2; +static const int SCORE_STATE = 3; +static const int GAME_STATE = 4; + +static const int QUIT_STATE = -1; + +#endif diff --git a/include/fov.h b/include/fov.h new file mode 100644 index 0000000..b27f59c --- /dev/null +++ b/include/fov.h @@ -0,0 +1,245 @@ +/* + * Copyright (C) 2006-2007, Greg McIntyre. All rights reserved. See the file + * named COPYING in the distribution for more details. + */ + +/** + * \mainpage Field of View Library + * + * \section about About + * + * This is a C library which implements a course-grained lighting + * algorithm suitable for tile-based games such as roguelikes. + * + * \section copyright Copyright + * + * \verbinclude COPYING + * + * \section thanks Thanks + * + * Thanks to Björn Bergström + * for the algorithm. + * + */ + +/** + * \file fov.h + * Field-of-view algorithm for dynamically casting light/shadow on a + * low resolution 2D raster. + */ +#ifndef LIBFOV_HEADER +#define LIBFOV_HEADER + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** Eight-way directions. */ +typedef enum { + FOV_EAST = 0, + FOV_NORTHEAST, + FOV_NORTH, + FOV_NORTHWEST, + FOV_WEST, + FOV_SOUTHWEST, + FOV_SOUTH, + FOV_SOUTHEAST +} fov_direction_type; + +/** Values for the shape setting. */ +typedef enum { + FOV_SHAPE_CIRCLE_PRECALCULATE, + FOV_SHAPE_SQUARE, + FOV_SHAPE_CIRCLE, + FOV_SHAPE_OCTAGON +} fov_shape_type; + +/** Values for the corner peek setting. */ +typedef enum { + FOV_CORNER_NOPEEK, + FOV_CORNER_PEEK +} fov_corner_peek_type; + +/** Values for the opaque apply setting. */ +typedef enum { + FOV_OPAQUE_APPLY, + FOV_OPAQUE_NOAPPLY +} fov_opaque_apply_type; + +/** @cond INTERNAL */ +typedef /*@null@*/ unsigned *height_array_t; +/** @endcond */ + +typedef struct { + /** Opacity test callback. */ + /*@null@*/ bool (*opaque)(void *map, int x, int y); + + /** Lighting callback to set lighting on a map tile. */ + /*@null@*/ void (*apply)(void *map, int x, int y, int dx, int dy, void *src); + + /** Shape setting. */ + fov_shape_type shape; + + /** Whether to peek around corners. */ + fov_corner_peek_type corner_peek; + + /** Whether to call apply on opaque tiles. */ + fov_opaque_apply_type opaque_apply; + + /** \cond INTERNAL */ + + /** Pre-calculated data. \internal */ + /*@null@*/ height_array_t *heights; + + /** Size of pre-calculated data. \internal */ + unsigned numheights; + + /** \endcond */ +} fov_settings_type; + +/** The opposite direction to that given. */ +#define fov_direction_opposite(direction) ((fov_direction_type)(((direction)+4)&0x7)) + +/** + * Set all the default options. You must call this option when you + * create a new settings data structure. + * + * These settings are the defaults used: + * + * - shape: FOV_SHAPE_CIRCLE_PRECALCULATE + * - corner_peek: FOV_CORNER_NOPEEK + * - opaque_apply: FOV_OPAQUE_APPLY + * + * Callbacks still need to be set up after calling this function. + * + * \param settings Pointer to data structure containing settings. + */ +void fov_settings_init(fov_settings_type *settings); + +/** + * Set the shape of the field of view. + * + * \param settings Pointer to data structure containing settings. + * \param value One of the following values, where R is the radius: + * + * - FOV_SHAPE_CIRCLE_PRECALCULATE \b (default): Limit the FOV to a + * circle with radius R by precalculating, which consumes more memory + * at the rate of 4*(R+2) bytes per R used in calls to fov_circle. + * Each radius is only calculated once so that it can be used again. + * Use fov_free() to free this precalculated data's memory. + * + * - FOV_SHAPE_CIRCLE: Limit the FOV to a circle with radius R by + * calculating on-the-fly. + * + * - FOV_SHAPE_OCTOGON: Limit the FOV to an octogon with maximum radius R. + * + * - FOV_SHAPE_SQUARE: Limit the FOV to an R*R square. + */ +void fov_settings_set_shape(fov_settings_type *settings, fov_shape_type value); + +/** + * NOT YET IMPLEMENTED. + * + * Set whether sources will peek around corners. + * + * \param settings Pointer to data structure containing settings. + * \param value One of the following values: + * + * - FOV_CORNER_PEEK \b (default): Renders: +\verbatim + ........ + ........ + ........ + ..@# + ...# +\endverbatim + * - FOV_CORNER_NOPEEK: Renders: +\verbatim + ...... + ..... + .... + ..@# + ...# +\endverbatim + */ +void fov_settings_set_corner_peek(fov_settings_type *settings, fov_corner_peek_type value); + +/** + * Whether to call the apply callback on opaque tiles. + * + * \param settings Pointer to data structure containing settings. + * \param value One of the following values: + * + * - FOV_OPAQUE_APPLY \b (default): Call apply callback on opaque tiles. + * - FOV_OPAQUE_NOAPPLY: Do not call the apply callback on opaque tiles. + */ +void fov_settings_set_opaque_apply(fov_settings_type *settings, fov_opaque_apply_type value); + +/** + * Set the function used to test whether a map tile is opaque. + * + * \param settings Pointer to data structure containing settings. + * \param f The function called to test whether a map tile is opaque. + */ +void fov_settings_set_opacity_test_function(fov_settings_type *settings, bool (*f)(void *map, int x, int y)); + +/** + * Set the function used to apply lighting to a map tile. + * + * \param settings Pointer to data structure containing settings. + * \param f The function called to apply lighting to a map tile. + */ +void fov_settings_set_apply_lighting_function(fov_settings_type *settings, void (*f)(void *map, int x, int y, int dx, int dy, void *src)); + +/** + * Free any memory that may have been cached in the settings + * structure. + * + * \param settings Pointer to data structure containing settings. + */ +void fov_settings_free(fov_settings_type *settings); + +/** + * Calculate a full circle field of view from a source at (x,y). + * + * \param settings Pointer to data structure containing settings. + * \param map Pointer to map data structure to be passed to callbacks. + * \param source Pointer to data structure holding source of light. + * \param source_x x-axis coordinate from which to start. + * \param source_y y-axis coordinate from which to start. + * \param radius Euclidean distance from (x,y) after which to stop. + */ +void fov_circle(fov_settings_type *settings, void *map, void *source, + int source_x, int source_y, unsigned radius +); + +/** + * Calculate a field of view from source at (x,y), pointing + * in the given direction and with the given angle. The larger + * the angle, the wider, "less focused" the beam. Each side of the + * line pointing in the direction from the source will be half the + * angle given such that the angle specified will be represented on + * the raster. + * + * \param settings Pointer to data structure containing settings. + * \param map Pointer to map data structure to be passed to callbacks. + * \param source Pointer to data structure holding source of light. + * \param source_x x-axis coordinate from which to start. + * \param source_y y-axis coordinate from which to start. + * \param radius Euclidean distance from (x,y) after which to stop. + * \param direction One of eight directions the beam of light can point. + * \param angle The angle at the base of the beam of light, in degrees. + */ +void fov_beam(fov_settings_type *settings, void *map, void *source, + int source_x, int source_y, unsigned radius, + fov_direction_type direction, float angle +); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/include/island.h b/include/island.h new file mode 100644 index 0000000..f4b9bd4 --- /dev/null +++ b/include/island.h @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2014, Miguel Angel Astor Romero. All rights reserved. + * See the file LICENSE for more details. + */ + +#ifndef ISLAND_H +#define ISLAND_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum TERRAIN_TYPE { DEEP_WATER, + SHALLOW_WATER, + SAND, + GRASS, + FOREST, + HILL, + MOUNTAIN + } terrain_t; + + /** + * Generate a diamond-square fractal map. + */ + extern void ds ( float ***, const unsigned int ); + + /** + * Generate a mask using particle deposition. + */ + extern void island ( int ***, unsigned int ); + + /** + * Normalize a float matrix between 0 and 1. + */ + extern void norm ( float ***, unsigned int ); + + /** + * Normalize an int matrix between 0 and 255. + */ + extern void normInt ( int ***, unsigned int ); + + /** + * Perform a 3x3 average blur. + */ + extern void smooth ( int ***, unsigned int ); + + /** + * Multiply the diamond square map with the island mask. + * Both matrices must have been normalized before. + */ + extern void mult ( float ***, int ***, unsigned int ); + + /** + * Given a sample from a heightmap, return the terrain + * type that correspond it. + */ + terrain_t terrainType( int ); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/libfov.a b/lib/libfov.a new file mode 100644 index 0000000000000000000000000000000000000000..c66ba879dc64ab05f4d0024056ce6bc24e61d49f GIT binary patch literal 16870 zcmeHO4RloHnZA>mXv8shq*-JYWN-%zGK>?6G+>m3gn%@qC>Z=N%2H5erXT@`BC86rQTrE`h0$||POcJ_I{@4Yj3 zei9O(o;};%lbPp!|3B~hyzhIzo0(Z$(^lJX!=$U7$ydSrh4U8_6kJ<0-|6&Oqa-so47a!^$5Yp7|hOV6%tX=|=) zt8T5UyDPo0rM2enO?A~Zt*wpsW-P7U=)bqxU+42z*KcaB^>1uxPOm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O^&w_ougaq6B^+pzK@9pmH#3xm-r&sR`-nO>o#(ur6{afIIXy>N;H}YB0BMZ9A5}L*zG6lvDG9@8`}UaAzNPV?IAF2d-p{>W$RW{)mqt$g_LJlpV(FatBT9=BS=;la{!L zu~rc2?V9Y6K7`(!JFx@G20^@!CU$f5Ik*Go(-n3Wcg$S7@aviUoo|6~XV;dD%qr%D zF_X=|2Ifezx%2sWMmxhlANppTy#7A= z_k)Js?vMR1q`R%FtGAtXcXqdL*~T{Zb#`9(_XtD!nG3c28GDx<-&BHlC^gy8PkzIfRvZ4zhOgOB#Xe|#y$zqR;lBWJ_daw0 literal 0 HcmV?d00001 diff --git a/libfov.COPYING b/libfov.COPYING new file mode 100644 index 0000000..5a2247e --- /dev/null +++ b/libfov.COPYING @@ -0,0 +1,22 @@ +Copyright (c) 2006 Greg McIntyre +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +http://code.google.com/p/libfov/ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ea0bcf7 --- /dev/null +++ b/src/main.c @@ -0,0 +1,193 @@ +/** + * Copyright (c) 2014, Miguel Angel Astor Romero. All rights reserved. + * See the file LICENSE for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "constants.h" + +void leave(void); +void manage_signal(int signal); +int start_ncurses(void); +void set_colors(void); +void clear_screen(void); +void on_resize(int); + +static int w = 0, h = 0; + +int main() { + bool finished = false; + char *home_dir = getenv("HOME"); + char *data_dir; + char *log_file; + time_t raw_date; + struct tm *current_date; + + atexit(leave); + signal(SIGINT, manage_signal); + signal(SIGSEGV, manage_signal); + signal(SIGTERM, manage_signal); + signal(SIGWINCH, on_resize); + + /* If there is a HOME environment variable, enable scoring. */ + if(home_dir != NULL){ + /* If we got the user's home directory, build the data directory path. */ + data_dir = (char*)malloc(strlen(home_dir) + 7); + strcpy(data_dir, home_dir); + strcat(data_dir, "/.tomb"); + + /* Redirect stderr output to a file. */ + log_file = (char*)malloc(strlen(data_dir) + 8); + strcpy(log_file, data_dir); + strcat(log_file, "/stderr"); + freopen(log_file, "a", stderr); + + /* Log the current date and time. */ + time(&raw_date); + current_date = localtime(&raw_date); + fprintf(stderr, "%s", asctime(current_date)); + + /* Try to create the data directory with permissions 775. */ + if(mkdir(data_dir, S_IRWXU | S_IWGRP | S_IRGRP| S_IROTH | S_IXOTH) == 0){ + /* The data directory was sucessfully created. */ + //init_scores(data_dir); + }else{ + if(errno != EEXIST){ + /* The directory does not exists and could not be created. */ + perror("\tmain.c"); + fprintf(stderr, "\tdata_dir is: %s\n", data_dir); + //init_scores(NULL); + }else{ + /* The directory already exits. */ + //init_scores(data_dir); + } + } + }else{ + /* If there is no HOME environment variable, quit. */ + fprintf(stderr, "\tmain.c: Couldn't find the user's home directory\n"); + return EXIT_FAILURE; + } + + /* Start ncurses. */ + if(start_ncurses() != 0){ + fprintf(stderr, "\tmain.c: Ncurses could not be initialized.\n"); + return EXIT_FAILURE; + } + set_colors(); + + do{ + clear_screen(); + + refresh(); + }while(!finished); + + return EXIT_SUCCESS; +} + + +void leave(void){ + int i; + + /* Finish ncurses. */ + endwin(); + /* Close the scores database and todays log. */ + // close_scores(); + for(i = 0; i < 80; i++) + fprintf(stderr, "-"); + fprintf(stderr, "\n"); +} + +void manage_signal(int signal){ + + switch(signal){ + case SIGINT: + fprintf(stderr, "\tSIGINT caught.\n"); + break; + case SIGSEGV: + fprintf(stderr, "\tSegmentation fault.\n"); + case SIGTERM: + exit(EXIT_FAILURE); + break; + } +} + +void on_resize(int signal){ + struct winsize ws; + + /* Request the new size of the terminal. */ + ioctl(1, TIOCGWINSZ, &ws); + /* Resize ncurse's stdscr. */ + resizeterm(ws.ws_row, ws.ws_col); + /* Get the new size of the window. */ + getmaxyx(stdscr, h, w); + fprintf(stderr, "\tSIGWINCH caught. (W: %d, H: %d)\n", w, h); +} + +int start_ncurses(void){ + WINDOW *win_ptr; + int ret_code; + /* Prepare the terminal. */ + win_ptr = initscr(); + if(win_ptr == NULL) + return -1; + /* Enable special characters. */ + ret_code = keypad(stdscr, TRUE); + if(ret_code == ERR) + return -1; + /* Disable line buffering. */ + ret_code = cbreak(); + if(ret_code == ERR) + return -1; + /* Disable echo. */ + ret_code = noecho(); + if(ret_code == ERR) + return -1; + /* Hide the cursor. */ + ret_code = curs_set(FALSE); + if(ret_code == ERR) + return -1; + /* Initialize the screen size variables. */ + getmaxyx(stdscr, h, w); + + return 0; +} + +void set_colors(void){ + int ret_code; + ret_code = start_color(); + if(ret_code == OK){ + if(has_colors() == TRUE){ + init_pair(1, COLOR_WHITE, COLOR_RED); /* The color for the top and bottom bars. */ + init_pair(2, COLOR_WHITE, COLOR_BLACK); /* Basic text color. */ + init_pair(3, COLOR_YELLOW, COLOR_BLACK); /* Highlighted text color. */ + init_pair(4, COLOR_BLUE, COLOR_BLACK); /* Lights off color. */ + init_pair(5, COLOR_RED, COLOR_BLACK); /* Dim light color. */ + init_pair(6, COLOR_YELLOW, COLOR_BLACK); /* Lights on color. */ + init_pair(7, COLOR_YELLOW, COLOR_YELLOW); /* Main GUI bar color. */ + init_pair(8, COLOR_WHITE, COLOR_WHITE); /* Empty GUI bar color. */ + } + } +} + +void clear_screen(void){ + int i, j; + move(0,0); + attron(COLOR_PAIR(BSC_COLOR)); + for(i = 0; i < w; i++){ + for(j = 0; j < h; j++){ + move(j, i); + printw(" "); + } + } +}