2002-08-26 03:32:39 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Philipp Pertermann
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-26 03:32:39 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2003-06-29 16:33:04 +00:00
|
|
|
#include "plugin.h"
|
2008-11-20 11:27:31 +00:00
|
|
|
#include "lib/configfile.h"
|
|
|
|
#include "lib/helper.h"
|
2009-02-26 17:10:27 +00:00
|
|
|
#include "lib/playback_control.h"
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2010-08-28 21:46:32 +00:00
|
|
|
#ifdef DEBUG_WORMLET
|
|
|
|
static long max_cycle;
|
|
|
|
#endif
|
2006-01-15 18:20:18 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* size of the field the worm lives in */
|
|
|
|
#define FIELD_RECT_X 1
|
|
|
|
#define FIELD_RECT_Y 1
|
2002-09-27 07:13:02 +00:00
|
|
|
#define FIELD_RECT_WIDTH (LCD_WIDTH - 45)
|
2002-09-09 23:30:16 +00:00
|
|
|
#define FIELD_RECT_HEIGHT (LCD_HEIGHT - 2)
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* when the game starts */
|
2006-03-13 03:13:05 +00:00
|
|
|
#define INITIAL_WORM_LENGTH 10
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* num of pixel the worm grows per eaten food */
|
2006-03-13 03:13:05 +00:00
|
|
|
#define WORM_PER_FOOD 7
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* num of worms creeping in the FIELD */
|
2006-03-13 03:13:05 +00:00
|
|
|
#define MAX_WORMS 3
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/* minimal distance between a worm and an argh
|
2002-09-09 23:30:16 +00:00
|
|
|
when a new argh is made */
|
|
|
|
#define MIN_ARGH_DIST 5
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
#if (CONFIG_KEYPAD == RECORDER_PAD)
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_PLAYER2_DIR1 BUTTON_F2
|
|
|
|
#define BTN_PLAYER2_DIR2 BUTTON_F3
|
|
|
|
#define BTN_STARTPAUSE BUTTON_PLAY
|
|
|
|
#define BTN_QUIT BUTTON_OFF
|
|
|
|
#define BTN_STOPRESET BUTTON_ON
|
|
|
|
#define BTN_TOGGLE_KEYS BUTTON_F1
|
|
|
|
|
2006-11-27 02:16:32 +00:00
|
|
|
#if BUTTON_REMOTE != 0
|
|
|
|
#define BTN_RC_UP BUTTON_RC_VOL_UP
|
|
|
|
#define BTN_RC_DOWN BUTTON_RC_VOL_DOWN
|
2006-03-13 03:13:05 +00:00
|
|
|
#define REMOTE
|
|
|
|
#define MULTIPLAYER
|
2006-11-27 02:16:32 +00:00
|
|
|
#endif
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2007-01-14 13:48:09 +00:00
|
|
|
#elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_PLAYER2_DIR1 BUTTON_F2
|
|
|
|
#define BTN_PLAYER2_DIR2 BUTTON_F3
|
|
|
|
#define BTN_STARTPAUSE BUTTON_SELECT
|
|
|
|
#define BTN_QUIT BUTTON_OFF
|
|
|
|
#define BTN_STOPRESET BUTTON_ON
|
|
|
|
#define BTN_TOGGLE_KEYS BUTTON_F1
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
#elif (CONFIG_KEYPAD == ONDIO_PAD)
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
2006-03-13 03:31:02 +00:00
|
|
|
#define BTN_STARTPAUSE (BUTTON_MENU|BUTTON_REL)
|
|
|
|
#define BTN_QUIT (BUTTON_OFF|BUTTON_REL)
|
|
|
|
#define BTN_STOPRESET (BUTTON_OFF|BUTTON_MENU)
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2007-07-27 09:57:27 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IPOD_1G2G_PAD)
|
2006-03-13 03:13:05 +00:00
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_MENU
|
|
|
|
#define BTN_DIR_DOWN BUTTON_PLAY
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
|
|
|
|
#define BTN_QUIT (BUTTON_SELECT|BUTTON_MENU)
|
|
|
|
#define BTN_STOPRESET (BUTTON_SELECT|BUTTON_PLAY)
|
|
|
|
|
2006-03-13 03:31:02 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
|
2006-03-13 03:13:05 +00:00
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE (BUTTON_SELECT|BUTTON_REL)
|
|
|
|
#define BTN_QUIT BUTTON_OFF
|
|
|
|
#define BTN_STOPRESET BUTTON_ON
|
|
|
|
|
2006-06-30 17:03:01 +00:00
|
|
|
#define BTN_RC_QUIT BUTTON_RC_STOP
|
|
|
|
|
2007-03-16 23:02:39 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
|
2006-05-23 19:58:37 +00:00
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_PLAY
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_REC
|
|
|
|
|
2006-05-22 16:28:19 +00:00
|
|
|
#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_SELECT
|
2007-05-19 23:38:09 +00:00
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_A
|
2006-05-22 16:28:19 +00:00
|
|
|
|
2007-09-20 10:49:48 +00:00
|
|
|
#elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == SANSA_C200_PAD)
|
2006-10-26 13:38:09 +00:00
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_SELECT
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_REC
|
|
|
|
|
2009-04-10 17:28:26 +00:00
|
|
|
#elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
|
2008-11-28 00:37:28 +00:00
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_SELECT
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_HOME
|
|
|
|
|
2009-04-10 17:28:26 +00:00
|
|
|
#elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_SELECT
|
|
|
|
#define BTN_QUIT (BUTTON_HOME|BUTTON_REPEAT)
|
|
|
|
#define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
|
|
|
|
|
2008-12-12 19:50:49 +00:00
|
|
|
#elif (CONFIG_KEYPAD == SANSA_M200_PAD)
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE (BUTTON_SELECT | BUTTON_REL)
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET (BUTTON_SELECT | BUTTON_UP)
|
|
|
|
|
2006-08-03 20:17:25 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_SCROLL_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_SCROLL_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_PLAY
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_REW
|
|
|
|
|
Initial commit of the Samsung YP-R0 port.
This port is a hybrid native/RaaA port. It runs on a embedded linux system,
but is the only application. It therefore can implement lots of stuff that
native targets also implement, while leveraging the underlying linux kernel.
The port is quite advanced. User interface, audio playback, plugins work
mostly fine. Missing is e.g. power mangement and USB (see SamsungYPR0 wiki page).
Included in utils/ypr0tools are scripts and programs required to generate
a patched firmware. The patched firmware has the rootfs modified to load
Rockbox. It includes a early/safe USB mode.
This port needs a new toolchain, one that includes glibc headers and libraries.
rockboxdev.sh can generate it, but e.g. codesourcey and distro packages may
also work.
Most of the initial effort is done by Lorenzo Miori and others (on ABI),
including reverse engineering and patching of the original firmware,
initial drivers, and more. Big thanks to you.
Flyspray: FS#12348
Author: Lorenzo Miori, myself
Merry christmas to ypr0 owners! :)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@31415 a1c6a512-1295-4272-9138-f99709370657
2011-12-24 11:56:46 +00:00
|
|
|
#elif (CONFIG_KEYPAD == GIGABEAT_S_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == SAMSUNG_YPR0_PAD)
|
2008-02-17 12:23:02 +00:00
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_SELECT
|
|
|
|
#define BTN_QUIT BUTTON_BACK
|
|
|
|
#define BTN_STOPRESET BUTTON_MENU
|
|
|
|
|
2008-03-01 22:55:09 +00:00
|
|
|
#elif (CONFIG_KEYPAD == MROBE100_PAD)
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_SELECT
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_DISPLAY
|
|
|
|
|
2008-03-22 10:24:28 +00:00
|
|
|
#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_RC_VOL_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_RC_VOL_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_RC_REW
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RC_FF
|
|
|
|
#define BTN_STARTPAUSE BUTTON_RC_PLAY
|
|
|
|
#define BTN_QUIT BUTTON_RC_REC
|
|
|
|
#define BTN_STOPRESET BUTTON_RC_MODE
|
|
|
|
|
2009-12-15 20:51:41 +00:00
|
|
|
#elif (CONFIG_KEYPAD == COWON_D2_PAD)
|
2008-03-22 22:03:34 +00:00
|
|
|
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
|
2008-12-04 21:28:56 +00:00
|
|
|
#elif CONFIG_KEYPAD == CREATIVEZVM_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
2008-12-08 09:51:42 +00:00
|
|
|
#define BTN_STARTPAUSE BUTTON_PLAY
|
2008-12-04 21:28:56 +00:00
|
|
|
#define BTN_QUIT BUTTON_BACK
|
|
|
|
#define BTN_STOPRESET BUTTON_MENU
|
|
|
|
|
2009-01-24 22:41:55 +00:00
|
|
|
#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_MENU
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_VIEW
|
|
|
|
|
2010-11-02 03:29:16 +00:00
|
|
|
#elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_PREV
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_NEXT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_MENU
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_RIGHT
|
|
|
|
|
2009-12-02 01:56:39 +00:00
|
|
|
#elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_PREV
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_MENU
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_RIGHT
|
|
|
|
|
2009-08-31 21:11:32 +00:00
|
|
|
#elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == ONDAVX777_PAD) || \
|
|
|
|
CONFIG_KEYPAD == MROBE500_PAD
|
2009-04-07 23:41:44 +00:00
|
|
|
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
|
2009-08-04 03:08:32 +00:00
|
|
|
#elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_PLAY
|
|
|
|
#define BTN_QUIT BUTTON_FFWD
|
|
|
|
#define BTN_STOPRESET BUTTON_REW
|
|
|
|
|
2010-02-13 15:46:34 +00:00
|
|
|
#elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_PREV
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_NEXT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_PLAY
|
|
|
|
#define BTN_QUIT BUTTON_REC
|
|
|
|
#define BTN_STOPRESET BUTTON_CANCEL
|
|
|
|
|
2010-04-26 21:40:00 +00:00
|
|
|
#elif CONFIG_KEYPAD == MPIO_HD200_PAD
|
|
|
|
|
2012-02-02 13:42:42 +00:00
|
|
|
#define BTN_DIR_UP BUTTON_REW
|
|
|
|
#define BTN_DIR_DOWN BUTTON_FF
|
|
|
|
#define BTN_DIR_LEFT BUTTON_VOL_DOWN
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_VOL_UP
|
2010-11-02 10:44:34 +00:00
|
|
|
#define BTN_STARTPAUSE BUTTON_FUNC
|
2010-04-26 21:40:00 +00:00
|
|
|
#define BTN_QUIT (BUTTON_REC|BUTTON_PLAY)
|
2010-11-02 10:44:34 +00:00
|
|
|
#define BTN_STOPRESET (BUTTON_FUNC|BUTTON_REPEAT)
|
2010-04-26 21:40:00 +00:00
|
|
|
|
2010-11-30 10:52:14 +00:00
|
|
|
#elif CONFIG_KEYPAD == MPIO_HD300_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
2012-02-02 13:42:42 +00:00
|
|
|
#define BTN_DIR_LEFT BUTTON_REW
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_FF
|
2010-11-30 10:52:14 +00:00
|
|
|
#define BTN_STARTPAUSE BUTTON_PLAY
|
2012-02-02 13:42:42 +00:00
|
|
|
#define BTN_QUIT (BUTTON_MENU | BUTTON_REPEAT)
|
|
|
|
#define BTN_STOPRESET (BUTTON_PLAY | BUTTON_REPEAT)
|
2010-11-30 10:52:14 +00:00
|
|
|
|
2011-10-02 16:51:32 +00:00
|
|
|
#elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_PLAYPAUSE
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_BACK
|
|
|
|
|
2011-11-16 14:08:01 +00:00
|
|
|
#elif CONFIG_KEYPAD == SANSA_CONNECT_PAD
|
|
|
|
|
|
|
|
#define BTN_DIR_UP BUTTON_UP
|
|
|
|
#define BTN_DIR_DOWN BUTTON_DOWN
|
|
|
|
#define BTN_DIR_LEFT BUTTON_LEFT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_RIGHT
|
|
|
|
#define BTN_STARTPAUSE BUTTON_SELECT
|
|
|
|
#define BTN_QUIT BUTTON_POWER
|
|
|
|
#define BTN_STOPRESET BUTTON_VOL_DOWN
|
|
|
|
|
2008-03-01 22:55:09 +00:00
|
|
|
#else
|
|
|
|
#error No keymap defined!
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-23 09:46:38 +00:00
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
2008-04-27 15:30:19 +00:00
|
|
|
#ifndef BTN_DIR_UP
|
|
|
|
#define BTN_DIR_UP BUTTON_TOPMIDDLE
|
|
|
|
#endif
|
|
|
|
#ifndef BTN_DIR_DOWN
|
|
|
|
#define BTN_DIR_DOWN BUTTON_BOTTOMMIDDLE
|
|
|
|
#endif
|
|
|
|
#ifndef BTN_DIR_LEFT
|
|
|
|
#define BTN_DIR_LEFT BUTTON_MIDLEFT
|
|
|
|
#endif
|
|
|
|
#ifndef BTN_DIR_RIGHT
|
|
|
|
#define BTN_DIR_RIGHT BUTTON_MIDRIGHT
|
|
|
|
#endif
|
|
|
|
#ifndef BTN_STARTPAUSE
|
|
|
|
#define BTN_STARTPAUSE BUTTON_CENTER
|
|
|
|
#endif
|
|
|
|
#ifndef BTN_QUIT
|
|
|
|
#define BTN_QUIT BUTTON_TOPLEFT
|
|
|
|
#endif
|
|
|
|
#ifndef BTN_STOPRESET
|
|
|
|
#define BTN_STOPRESET BUTTON_TOPRIGHT
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-11-13 16:16:18 +00:00
|
|
|
#if (LCD_WIDTH == 96) && (LCD_HEIGHT == 96)
|
|
|
|
#define FOOD_SIZE 3
|
|
|
|
#define ARGH_SIZE 4
|
|
|
|
#define SPEED 14
|
|
|
|
#define MAX_WORM_SEGMENTS 128
|
|
|
|
#elif (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
|
2006-03-13 03:13:05 +00:00
|
|
|
#define FOOD_SIZE 3
|
2008-11-28 00:37:28 +00:00
|
|
|
#define ARGH_SIZE 4
|
|
|
|
#define SPEED 14
|
|
|
|
#define MAX_WORM_SEGMENTS 128
|
|
|
|
#elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 64)
|
|
|
|
#define FOOD_SIZE 3
|
2006-03-13 03:13:05 +00:00
|
|
|
#define ARGH_SIZE 4
|
|
|
|
#define SPEED 14
|
2006-05-22 06:56:39 +00:00
|
|
|
#define MAX_WORM_SEGMENTS 128
|
2007-09-20 10:49:48 +00:00
|
|
|
#elif (LCD_WIDTH == 132) && (LCD_HEIGHT == 80)
|
|
|
|
#define FOOD_SIZE 3
|
|
|
|
#define ARGH_SIZE 4
|
|
|
|
#define SPEED 14
|
|
|
|
#define MAX_WORM_SEGMENTS 128
|
2008-03-22 10:24:28 +00:00
|
|
|
#elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 96)
|
|
|
|
#define FOOD_SIZE 3
|
|
|
|
#define ARGH_SIZE 4
|
|
|
|
#define SPEED 12
|
|
|
|
#define MAX_WORM_SEGMENTS 128
|
2006-03-30 17:29:21 +00:00
|
|
|
#elif (LCD_WIDTH == 138) && (LCD_HEIGHT == 110)
|
|
|
|
#define FOOD_SIZE 4
|
|
|
|
#define ARGH_SIZE 5
|
|
|
|
#define SPEED 10
|
2006-05-22 06:56:39 +00:00
|
|
|
#define MAX_WORM_SEGMENTS 128
|
2007-01-18 00:25:57 +00:00
|
|
|
#elif (LCD_WIDTH == 128) && (LCD_HEIGHT == 128)
|
|
|
|
#define FOOD_SIZE 4
|
|
|
|
#define ARGH_SIZE 5
|
|
|
|
#define SPEED 9
|
|
|
|
#define MAX_WORM_SEGMENTS 128
|
2011-01-09 21:42:25 +00:00
|
|
|
#elif ((LCD_WIDTH == 160) && (LCD_HEIGHT == 128)) || \
|
|
|
|
((LCD_WIDTH == 128) && (LCD_HEIGHT == 160))
|
2006-03-13 03:13:05 +00:00
|
|
|
#define FOOD_SIZE 4
|
|
|
|
#define ARGH_SIZE 5
|
|
|
|
#define SPEED 8
|
2006-05-22 06:56:39 +00:00
|
|
|
#define MAX_WORM_SEGMENTS 256
|
2006-03-13 03:13:05 +00:00
|
|
|
#elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
|
|
|
|
#define FOOD_SIZE 4
|
|
|
|
#define ARGH_SIZE 5
|
|
|
|
#define SPEED 6
|
2006-05-22 06:56:39 +00:00
|
|
|
#define MAX_WORM_SEGMENTS 256
|
2006-03-13 03:13:05 +00:00
|
|
|
#elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
|
|
|
|
#define FOOD_SIZE 5
|
|
|
|
#define ARGH_SIZE 6
|
|
|
|
#define SPEED 4
|
2006-05-22 06:56:39 +00:00
|
|
|
#define MAX_WORM_SEGMENTS 512
|
2007-01-15 20:40:48 +00:00
|
|
|
#elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
|
|
|
|
#define FOOD_SIZE 5
|
|
|
|
#define ARGH_SIZE 6
|
|
|
|
#define SPEED 4
|
|
|
|
#define MAX_WORM_SEGMENTS 512
|
2009-07-21 03:52:59 +00:00
|
|
|
#elif ((LCD_WIDTH == 320) && (LCD_HEIGHT == 240)) || \
|
|
|
|
((LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400)))
|
2006-03-13 03:13:05 +00:00
|
|
|
#define FOOD_SIZE 7
|
|
|
|
#define ARGH_SIZE 8
|
|
|
|
#define SPEED 4
|
2006-05-22 06:56:39 +00:00
|
|
|
#define MAX_WORM_SEGMENTS 512
|
2009-07-21 03:52:59 +00:00
|
|
|
#elif ((LCD_WIDTH == 640) && (LCD_HEIGHT == 480)) || \
|
|
|
|
((LCD_WIDTH == 480) && (LCD_HEIGHT == 640))
|
|
|
|
#define FOOD_SIZE 14
|
|
|
|
#define ARGH_SIZE 16
|
2006-05-22 16:28:19 +00:00
|
|
|
#define SPEED 4
|
|
|
|
#define MAX_WORM_SEGMENTS 512
|
2006-05-22 06:56:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
#define COLOR_WORM LCD_RGBPACK(80, 40, 0)
|
|
|
|
#define COLOR_ARGH LCD_RGBPACK(175, 0, 0)
|
|
|
|
#define COLOR_FOOD LCD_RGBPACK(0, 150, 0)
|
|
|
|
#define COLOR_FG LCD_RGBPACK(0, 0, 0)
|
|
|
|
#define COLOR_BG LCD_RGBPACK(181, 199, 231)
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-17 21:05:35 +00:00
|
|
|
#define CHECK_SQUARE_COLLISION(x1,y1,s1,x2,y2,s2) (x1+s1>x2)&&(x2+s2>x1)&&(y1+s1>y2)&&(y2+s2>y1)
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/**
|
|
|
|
* All the properties that a worm has.
|
|
|
|
*/
|
2002-12-18 14:57:45 +00:00
|
|
|
static struct worm {
|
2002-09-09 23:30:16 +00:00
|
|
|
/* The worm is stored in a ring of xy coordinates */
|
2006-03-13 03:13:05 +00:00
|
|
|
int x[MAX_WORM_SEGMENTS];
|
|
|
|
int y[MAX_WORM_SEGMENTS];
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
int head; /* index of the head within the buffer */
|
|
|
|
int tail; /* index of the tail within the buffer */
|
|
|
|
int growing; /* number of cyles the worm still keeps growing */
|
|
|
|
bool alive; /* the worms living state */
|
|
|
|
|
|
|
|
/* direction vector in which the worm moves */
|
|
|
|
int dirx; /* only values -1 0 1 allowed */
|
|
|
|
int diry; /* only values -1 0 1 allowed */
|
|
|
|
|
|
|
|
/* this method is used to fetch the direction the user
|
|
|
|
has selected. It can be one of the values
|
2006-03-13 03:13:05 +00:00
|
|
|
human_player1, human_player2, remote_player, virtual_player.
|
|
|
|
All these values are fuctions, that can change the direction
|
2002-09-09 23:30:16 +00:00
|
|
|
of the worm */
|
|
|
|
void (*fetch_worm_direction)(struct worm *w);
|
|
|
|
} worms[MAX_WORMS];
|
|
|
|
|
|
|
|
/* stores the highscore - besides it was scored by a virtual player */
|
|
|
|
static int highscore;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
#define MAX_FOOD 5 /* maximal number of food items */
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
/* The arrays store the food coordinates */
|
2006-05-22 06:56:39 +00:00
|
|
|
static int foodx[MAX_FOOD];
|
|
|
|
static int foody[MAX_FOOD];
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
#define MAX_ARGH 100 /* maximal number of argh items */
|
2002-08-26 09:21:59 +00:00
|
|
|
#define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* The arrays store the argh coordinates */
|
2006-05-22 06:56:39 +00:00
|
|
|
static int arghx[MAX_ARGH];
|
|
|
|
static int arghy[MAX_ARGH];
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/* the number of arghs that are currently in use */
|
2002-09-09 23:30:16 +00:00
|
|
|
static int argh_count;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
/* the number of arghs per food, settable by user */
|
|
|
|
static int arghs_per_food = ARGHS_PER_FOOD;
|
|
|
|
/* the size of the argh, settable by user */
|
|
|
|
static int argh_size = ARGH_SIZE;
|
|
|
|
/* the size of the food, settable by user */
|
|
|
|
static int food_size = FOOD_SIZE;
|
|
|
|
/* the speed of the worm, settable by user */
|
|
|
|
static int speed = SPEED;
|
|
|
|
/* the amount a worm grows by eating a food, settable by user */
|
|
|
|
static int worm_food = WORM_PER_FOOD;
|
|
|
|
|
|
|
|
/* End additional variables */
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* the number of active worms (dead or alive) */
|
|
|
|
static int worm_count = MAX_WORMS;
|
|
|
|
|
|
|
|
/* in multiplayer mode: en- / disables the remote worm control
|
|
|
|
in singleplayer mode: toggles 4 / 2 button worm control */
|
|
|
|
static bool use_remote = false;
|
|
|
|
|
|
|
|
/* return values of check_collision */
|
2002-08-26 03:32:39 +00:00
|
|
|
#define COLLISION_NONE 0
|
|
|
|
#define COLLISION_WORM 1
|
|
|
|
#define COLLISION_FOOD 2
|
|
|
|
#define COLLISION_ARGH 3
|
|
|
|
#define COLLISION_FIELD 4
|
|
|
|
|
2010-08-28 21:46:32 +00:00
|
|
|
static const char *const state_desc[] = {
|
|
|
|
[COLLISION_NONE] = NULL,
|
|
|
|
[COLLISION_WORM] = "Wormed",
|
|
|
|
[COLLISION_FOOD] = "Growing",
|
|
|
|
[COLLISION_ARGH] = "Argh",
|
|
|
|
[COLLISION_FIELD] = "Crashed",
|
|
|
|
};
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* constants for use as directions.
|
|
|
|
Note that the values are ordered clockwise.
|
|
|
|
Thus increasing / decreasing the values
|
|
|
|
is equivalent to right / left turns. */
|
|
|
|
#define WEST 0
|
|
|
|
#define NORTH 1
|
|
|
|
#define EAST 2
|
|
|
|
#define SOUTH 3
|
|
|
|
|
|
|
|
/* direction of human player 1 */
|
2006-03-13 03:13:05 +00:00
|
|
|
static int player1_dir = EAST;
|
2002-09-09 23:30:16 +00:00
|
|
|
/* direction of human player 2 */
|
2006-03-13 03:13:05 +00:00
|
|
|
static int player2_dir = EAST;
|
2002-09-09 23:30:16 +00:00
|
|
|
/* direction of human player 3 */
|
|
|
|
static int player3_dir = EAST;
|
|
|
|
|
|
|
|
/* the number of (human) players that currently
|
|
|
|
control a worm */
|
|
|
|
static int players = 1;
|
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
#define SETTINGS_VERSION 1
|
|
|
|
#define SETTINGS_MIN_VERSION 1
|
|
|
|
#define SETTINGS_FILENAME "wormlet.cfg"
|
|
|
|
|
|
|
|
static struct configdata config[] =
|
|
|
|
{
|
2009-08-03 16:30:08 +00:00
|
|
|
{TYPE_INT, 0, 1024, { .int_p = &highscore }, "highscore", NULL},
|
|
|
|
{TYPE_INT, 0, 15, { .int_p = &arghs_per_food }, "arghs per food", NULL},
|
|
|
|
{TYPE_INT, 0, 15, { .int_p = &argh_size }, "argh size", NULL},
|
|
|
|
{TYPE_INT, 0, 15, { .int_p = &food_size }, "food size", NULL},
|
|
|
|
{TYPE_INT, 0, 3, { .int_p = &players }, "players", NULL},
|
|
|
|
{TYPE_INT, 0, 3, { .int_p = &worm_count }, "worms", NULL},
|
|
|
|
{TYPE_INT, 0, 20, { .int_p = &speed }, "speed", NULL},
|
|
|
|
{TYPE_INT, 0, 15, { .int_p = &worm_food }, "Worm Growth Per Food", NULL}
|
2006-05-22 06:56:39 +00:00
|
|
|
};
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/**
|
|
|
|
* Returns the direction id in which the worm
|
|
|
|
* currently is creeping.
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param struct worm *w The worm that is to be investigated.
|
2002-09-09 23:30:16 +00:00
|
|
|
* w Must not be null.
|
|
|
|
* @return int A value 0 <= value < 4
|
|
|
|
* Note the predefined constants NORTH, SOUTH, EAST, WEST
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static int get_worm_dir(struct worm *w)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int retVal ;
|
|
|
|
if (w->dirx == 0) {
|
|
|
|
if (w->diry == 1) {
|
|
|
|
retVal = SOUTH;
|
|
|
|
} else {
|
|
|
|
retVal = NORTH;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (w->dirx == 1) {
|
|
|
|
retVal = EAST;
|
|
|
|
} else {
|
|
|
|
retVal = WEST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Set the direction of the specified worm with a direction id.
|
|
|
|
* Increasing the value by 1 means to turn the worm direction
|
|
|
|
* to right by 90 degree.
|
|
|
|
* @param struct worm *w The worm that is to be altered. w Must not be null.
|
|
|
|
* @param int dir The new direction in which the worm is to creep.
|
2006-03-13 03:13:05 +00:00
|
|
|
* dir must be 0 <= dir < 4. Use predefined constants
|
2002-09-09 23:30:16 +00:00
|
|
|
* NORTH, SOUTH, EAST, WEST
|
2002-08-26 03:32:39 +00:00
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static void set_worm_dir(struct worm *w, int dir)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
switch (dir) {
|
|
|
|
case WEST:
|
|
|
|
w->dirx = -1;
|
|
|
|
w->diry = 0;
|
|
|
|
break;
|
2009-08-03 16:30:08 +00:00
|
|
|
case NORTH:
|
2002-09-09 23:30:16 +00:00
|
|
|
w->dirx = 0;
|
|
|
|
w->diry = - 1;
|
|
|
|
break;
|
|
|
|
case EAST:
|
|
|
|
w->dirx = 1;
|
|
|
|
w->diry = 0;
|
|
|
|
break;
|
|
|
|
case SOUTH:
|
|
|
|
w->dirx = 0;
|
|
|
|
w->diry = 1;
|
|
|
|
break;
|
2009-08-03 16:30:08 +00:00
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current length of the worm array. This
|
|
|
|
* is also a value for the number of bends that are in the worm.
|
|
|
|
* @return int a positive value with 0 <= value < MAX_WORM_SEGMENTS
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static int get_worm_array_length(struct worm *w)
|
|
|
|
{
|
2002-08-26 09:21:59 +00:00
|
|
|
/* initial simple calculation will be overwritten if wrong. */
|
2002-09-09 23:30:16 +00:00
|
|
|
int retVal = w->head - w->tail;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* if the worm 'crosses' the boundaries of the ringbuffer */
|
2002-08-26 03:32:39 +00:00
|
|
|
if (retVal < 0) {
|
2002-09-09 23:30:16 +00:00
|
|
|
retVal = w->head + MAX_WORM_SEGMENTS - w->tail;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/**
|
|
|
|
* Returns the score the specified worm. The score is the length
|
|
|
|
* of the worm.
|
|
|
|
* @param struct worm *w The worm that is to be investigated.
|
|
|
|
* w must not be null.
|
|
|
|
* @return int The length of the worm (>= 0).
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static int get_score(struct worm *w)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int retval = 0;
|
|
|
|
int length = get_worm_array_length(w);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
|
|
|
/* The iteration iterates the length of the worm.
|
|
|
|
Here's the conversion to the true indices within the worm arrays. */
|
|
|
|
int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
|
|
|
|
int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
|
|
|
|
int startx = w->x[linestart];
|
|
|
|
int starty = w->y[linestart];
|
|
|
|
int endx = w->x[lineend];
|
|
|
|
int endy = w->y[lineend];
|
|
|
|
|
|
|
|
int minimum, maximum;
|
|
|
|
|
|
|
|
if (startx == endx) {
|
|
|
|
minimum = MIN(starty, endy);
|
|
|
|
maximum = MAX(starty, endy);
|
|
|
|
} else {
|
|
|
|
minimum = MIN(startx, endx);
|
|
|
|
maximum = MAX(startx, endx);
|
|
|
|
}
|
2009-08-03 16:30:08 +00:00
|
|
|
retval += abs(maximum - minimum);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines wether the line specified by startx, starty, endx, endy intersects
|
|
|
|
* the rectangle specified by x, y, width, height. Note that the line must be exactly
|
2006-03-13 03:13:05 +00:00
|
|
|
* horizontal or vertical (startx == endx or starty == endy).
|
2002-09-09 23:30:16 +00:00
|
|
|
* @param int startx The x coordinate of the start point of the line.
|
|
|
|
* @param int starty The y coordinate of the start point of the line.
|
|
|
|
* @param int endx The x coordinate of the end point of the line.
|
|
|
|
* @param int endy The y coordinate of the end point of the line.
|
|
|
|
* @param int x The x coordinate of the top left corner of the rectangle.
|
|
|
|
* @param int y The y coordinate of the top left corner of the rectangle.
|
|
|
|
* @param int width The width of the rectangle.
|
|
|
|
* @param int height The height of the rectangle.
|
|
|
|
* @return bool Returns true if the specified line intersects with the recangle.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool line_in_rect(int startx, int starty, int endx, int endy,
|
|
|
|
int x, int y, int width, int height)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
bool retval = false;
|
|
|
|
int simple, simplemin, simplemax;
|
|
|
|
int compa, compb, compmin, compmax;
|
|
|
|
int temp;
|
|
|
|
if (startx == endx) {
|
|
|
|
simple = startx;
|
|
|
|
simplemin = x;
|
|
|
|
simplemax = x + width;
|
|
|
|
|
|
|
|
compa = starty;
|
|
|
|
compb = endy;
|
|
|
|
compmin = y;
|
|
|
|
compmax = y + height;
|
|
|
|
} else {
|
|
|
|
simple = starty;
|
|
|
|
simplemin = y;
|
|
|
|
simplemax = y + height;
|
|
|
|
|
|
|
|
compa = startx;
|
|
|
|
compb = endx;
|
|
|
|
compmin = x;
|
|
|
|
compmax = x + width;
|
|
|
|
};
|
|
|
|
|
|
|
|
temp = compa;
|
|
|
|
compa = MIN(compa, compb);
|
|
|
|
compb = MAX(temp, compb);
|
|
|
|
|
2002-09-17 12:39:47 +00:00
|
|
|
if (simplemin <= simple && simple <= simplemax) {
|
|
|
|
if ((compmin <= compa && compa <= compmax) ||
|
|
|
|
(compmin <= compb && compb <= compmax) ||
|
|
|
|
(compa <= compmin && compb >= compmax)) {
|
2002-09-09 23:30:16 +00:00
|
|
|
retval = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Tests wether the specified worm intersects with the rect.
|
|
|
|
* @param struct worm *w The worm to be investigated
|
|
|
|
* @param int x The x coordinate of the top left corner of the rect
|
|
|
|
* @param int y The y coordinate of the top left corner of the rect
|
|
|
|
* @param int widht The width of the rect
|
|
|
|
* @param int height The height of the rect
|
|
|
|
* @return bool Returns true if the worm intersects with the rect
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool worm_in_rect(struct worm *w, int x, int y, int width, int height)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
bool retval = false;
|
|
|
|
|
|
|
|
|
|
|
|
/* get_worm_array_length is expensive -> buffer the value */
|
|
|
|
int wormLength = get_worm_array_length(w);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* test each entry that is part of the worm */
|
|
|
|
for (i = 0; i < wormLength && retval == false; i++) {
|
|
|
|
|
|
|
|
/* The iteration iterates the length of the worm.
|
|
|
|
Here's the conversion to the true indices within the worm arrays. */
|
|
|
|
int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
|
|
|
|
int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
|
|
|
|
int startx = w->x[linestart];
|
|
|
|
int starty = w->y[linestart];
|
|
|
|
int endx = w->x[lineend];
|
|
|
|
int endy = w->y[lineend];
|
|
|
|
|
|
|
|
retval = line_in_rect(startx, starty, endx, endy, x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2002-08-26 03:32:39 +00:00
|
|
|
/**
|
|
|
|
* Checks wether a specific food in the food arrays is at the
|
|
|
|
* specified coordinates.
|
|
|
|
* @param int foodIndex The index of the food in the food arrays
|
|
|
|
* @param int x the x coordinate.
|
|
|
|
* @param int y the y coordinate.
|
|
|
|
* @return Returns true if the coordinate hits the food specified by
|
|
|
|
* foodIndex.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool specific_food_collision(int foodIndex, int x, int y)
|
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
bool retVal = false;
|
|
|
|
if (x >= foodx[foodIndex] &&
|
2006-05-22 06:56:39 +00:00
|
|
|
x < foodx[foodIndex] + food_size &&
|
2002-08-26 03:32:39 +00:00
|
|
|
y >= foody[foodIndex] &&
|
2006-05-22 06:56:39 +00:00
|
|
|
y < foody[foodIndex] + food_size) {
|
2002-08-26 03:32:39 +00:00
|
|
|
|
|
|
|
retVal = true;
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the index of the food that is at the
|
|
|
|
* given coordinates. If no food is at the coordinates
|
|
|
|
* -1 is returned.
|
|
|
|
* @return int -1 <= value < MAX_FOOD
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static int food_collision(int x, int y)
|
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
int i = 0;
|
|
|
|
int retVal = -1;
|
2002-08-26 09:21:59 +00:00
|
|
|
for (i = 0; i < MAX_FOOD; i++) {
|
2002-09-09 23:30:16 +00:00
|
|
|
if (specific_food_collision(i, x, y)) {
|
2002-08-26 03:32:39 +00:00
|
|
|
retVal = i;
|
2002-08-26 09:21:59 +00:00
|
|
|
break;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks wether a specific argh in the argh arrays is at the
|
|
|
|
* specified coordinates.
|
|
|
|
* @param int arghIndex The index of the argh in the argh arrays
|
|
|
|
* @param int x the x coordinate.
|
|
|
|
* @param int y the y coordinate.
|
|
|
|
* @return Returns true if the coordinate hits the argh specified by
|
|
|
|
* arghIndex.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool specific_argh_collision(int arghIndex, int x, int y)
|
|
|
|
{
|
2002-08-26 09:21:59 +00:00
|
|
|
if ( x >= arghx[arghIndex] &&
|
|
|
|
y >= arghy[arghIndex] &&
|
2006-05-22 06:56:39 +00:00
|
|
|
x < arghx[arghIndex] + argh_size &&
|
|
|
|
y < arghy[arghIndex] + argh_size )
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
|
|
|
return true;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
2002-08-26 09:21:59 +00:00
|
|
|
|
|
|
|
return false;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the index of the argh that is at the
|
|
|
|
* given coordinates. If no argh is at the coordinates
|
|
|
|
* -1 is returned.
|
|
|
|
* @param int x The x coordinate.
|
|
|
|
* @param int y The y coordinate.
|
2002-09-09 23:30:16 +00:00
|
|
|
* @return int -1 <= value < argh_count <= MAX_ARGH
|
2002-08-26 03:32:39 +00:00
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static int argh_collision(int x, int y)
|
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
int i = 0;
|
|
|
|
int retVal = -1;
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* search for the argh that has the specified coords */
|
2002-09-09 23:30:16 +00:00
|
|
|
for (i = 0; i < argh_count; i++) {
|
|
|
|
if (specific_argh_collision(i, x, y)) {
|
2002-08-26 03:32:39 +00:00
|
|
|
retVal = i;
|
2002-08-26 09:21:59 +00:00
|
|
|
break;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks wether the worm collides with the food at the specfied food-arrays.
|
|
|
|
* @param int foodIndex The index of the food in the arrays. Ensure the value is
|
|
|
|
* 0 <= foodIndex <= MAX_FOOD
|
|
|
|
* @return Returns true if the worm collides with the specified food.
|
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static bool worm_food_collision(struct worm *w, int foodIndex)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
bool retVal = false;
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
retVal = worm_in_rect(w, foodx[foodIndex], foody[foodIndex],
|
2006-05-22 06:56:39 +00:00
|
|
|
food_size - 1, food_size - 1);
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the worm hits the argh within the next moves (unless
|
|
|
|
* the worm changes it's direction).
|
|
|
|
* @param struct worm *w - The worm to investigate
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param int argh_idx - The index of the argh
|
2002-09-09 23:30:16 +00:00
|
|
|
* @param int moves - The number of moves that are considered.
|
|
|
|
* @return Returns false if the specified argh is not hit within the next
|
|
|
|
* moves.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool worm_argh_collision_in_moves(struct worm *w, int argh_idx, int moves)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
bool retVal = false;
|
|
|
|
int x1, y1, x2, y2;
|
|
|
|
x1 = w->x[w->head];
|
|
|
|
y1 = w->y[w->head];
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
x2 = w->x[w->head] + moves * w->dirx;
|
|
|
|
y2 = w->y[w->head] + moves * w->diry;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
retVal = line_in_rect(x1, y1, x2, y2, arghx[argh_idx], arghy[argh_idx],
|
2006-05-22 06:56:39 +00:00
|
|
|
argh_size, argh_size);
|
2002-08-26 03:32:39 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks wether the worm collides with the argh at the specfied argh-arrays.
|
2002-08-26 09:21:59 +00:00
|
|
|
* @param int arghIndex The index of the argh in the arrays.
|
2002-09-09 23:30:16 +00:00
|
|
|
* Ensure the value is 0 <= arghIndex < argh_count <= MAX_ARGH
|
2002-08-26 03:32:39 +00:00
|
|
|
* @return Returns true if the worm collides with the specified argh.
|
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static bool worm_argh_collision(struct worm *w, int arghIndex)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
bool retVal = false;
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
retVal = worm_in_rect(w, arghx[arghIndex], arghy[arghIndex],
|
2006-05-22 06:56:39 +00:00
|
|
|
argh_size - 1, argh_size - 1);
|
2002-08-26 03:32:39 +00:00
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find new coordinates for the food stored in foodx[index], foody[index]
|
|
|
|
* that don't collide with any other food or argh
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param int index
|
2002-08-26 03:32:39 +00:00
|
|
|
* Ensure that 0 <= index < MAX_FOOD.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static void make_food(int index)
|
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
bool collisionDetected = false;
|
2002-09-09 23:30:16 +00:00
|
|
|
int i;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
|
|
|
do {
|
2002-08-26 09:21:59 +00:00
|
|
|
/* make coordinates for a new food so that
|
|
|
|
the entire food lies within the FIELD */
|
2006-05-22 06:56:39 +00:00
|
|
|
x = rb->rand() % (FIELD_RECT_WIDTH - food_size);
|
|
|
|
y = rb->rand() % (FIELD_RECT_HEIGHT - food_size);
|
2009-09-17 21:05:35 +00:00
|
|
|
collisionDetected = false;
|
2002-08-26 09:21:59 +00:00
|
|
|
/* Ensure that the new food doesn't collide with any
|
|
|
|
existing foods or arghs.
|
2009-09-17 21:05:35 +00:00
|
|
|
If the new food hit any existing
|
2002-08-26 09:21:59 +00:00
|
|
|
argh or food a collision is detected.
|
|
|
|
*/
|
2009-09-17 21:05:35 +00:00
|
|
|
|
|
|
|
for (i=0; i<MAX_FOOD && !collisionDetected; i++) {
|
|
|
|
collisionDetected = CHECK_SQUARE_COLLISION(x,y,food_size,foodx[i],foody[i],food_size);
|
|
|
|
}
|
|
|
|
for (i=0; i<argh_count && !collisionDetected; i++) {
|
|
|
|
collisionDetected = CHECK_SQUARE_COLLISION(x,y,food_size,arghx[i],arghy[i],argh_size);
|
|
|
|
}
|
2002-08-26 09:21:59 +00:00
|
|
|
|
|
|
|
/* use coordinates for further testing */
|
2002-08-26 03:32:39 +00:00
|
|
|
foodx[index] = x;
|
|
|
|
foody[index] = y;
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* now test wether we accidently hit the worm with food ;) */
|
2002-09-09 23:30:16 +00:00
|
|
|
i = 0;
|
|
|
|
for (i = 0; i < worm_count && !collisionDetected; i++) {
|
2009-09-17 21:05:35 +00:00
|
|
|
|
|
|
|
collisionDetected = worm_food_collision(&worms[i], index);
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
while (collisionDetected);
|
2006-05-22 06:56:39 +00:00
|
|
|
return;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears a food from the lcd buffer.
|
|
|
|
* @param int index The index of the food arrays under which
|
2006-03-13 03:13:05 +00:00
|
|
|
* the coordinates of the desired food can be found. Ensure
|
2002-08-26 03:32:39 +00:00
|
|
|
* that the value is 0 <= index <= MAX_FOOD.
|
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static void clear_food(int index)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
|
|
|
/* remove the old food from the screen */
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
|
|
|
|
rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
|
|
|
|
foody[index] + FIELD_RECT_Y,
|
2006-05-22 06:56:39 +00:00
|
|
|
food_size, food_size);
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a food in the lcd buffer.
|
|
|
|
* @param int index The index of the food arrays under which
|
2006-03-13 03:13:05 +00:00
|
|
|
* the coordinates of the desired food can be found. Ensure
|
2002-08-26 03:32:39 +00:00
|
|
|
* that the value is 0 <= index <= MAX_FOOD.
|
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static void draw_food(int index)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
|
|
|
/* draw the food object */
|
2006-03-13 07:05:15 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2006-05-22 06:56:39 +00:00
|
|
|
rb->lcd_set_foreground(COLOR_FOOD);
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
|
|
|
rb->lcd_fillrect(foodx[index] + FIELD_RECT_X,
|
2005-06-24 22:33:21 +00:00
|
|
|
foody[index] + FIELD_RECT_Y,
|
2006-05-22 06:56:39 +00:00
|
|
|
food_size, food_size);
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
|
|
|
|
rb->lcd_fillrect(foodx[index] + FIELD_RECT_X + 1,
|
|
|
|
foody[index] + FIELD_RECT_Y + 1,
|
2006-05-22 06:56:39 +00:00
|
|
|
food_size - 2, food_size - 2);
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
2006-03-13 07:05:15 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2006-05-22 06:56:39 +00:00
|
|
|
rb->lcd_set_foreground(COLOR_FG);
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find new coordinates for the argh stored in arghx[index], arghy[index]
|
|
|
|
* that don't collide with any other food or argh.
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param int index
|
2002-09-09 23:30:16 +00:00
|
|
|
* Ensure that 0 <= index < argh_count < MAX_ARGH.
|
2002-08-26 03:32:39 +00:00
|
|
|
*/
|
2006-05-22 06:56:39 +00:00
|
|
|
static void make_argh(int index)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2006-03-13 03:13:05 +00:00
|
|
|
int x = -1;
|
|
|
|
int y = -1;
|
2002-08-26 03:32:39 +00:00
|
|
|
bool collisionDetected = false;
|
2002-09-09 23:30:16 +00:00
|
|
|
int i;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
|
|
|
do {
|
2002-08-26 09:21:59 +00:00
|
|
|
/* make coordinates for a new argh so that
|
|
|
|
the entire food lies within the FIELD */
|
2006-05-22 06:56:39 +00:00
|
|
|
x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
|
|
|
|
y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
|
2009-09-17 21:05:35 +00:00
|
|
|
collisionDetected = false;
|
2002-08-26 09:21:59 +00:00
|
|
|
/* Ensure that the new argh doesn't intersect with any
|
|
|
|
existing foods or arghs.
|
2009-09-17 21:05:35 +00:00
|
|
|
If the new argh hit any existing
|
2002-08-26 09:21:59 +00:00
|
|
|
argh or food an intersection is detected.
|
|
|
|
*/
|
2009-09-17 21:05:35 +00:00
|
|
|
|
|
|
|
for (i=0; i<MAX_FOOD && !collisionDetected; i++) {
|
|
|
|
collisionDetected = CHECK_SQUARE_COLLISION(x,y,argh_size,foodx[i],foody[i],food_size);
|
|
|
|
}
|
|
|
|
for (i=0; i<argh_count && !collisionDetected; i++) {
|
|
|
|
collisionDetected = CHECK_SQUARE_COLLISION(x,y,argh_size,arghx[i],arghy[i],argh_size);
|
|
|
|
}
|
2002-08-26 09:21:59 +00:00
|
|
|
|
|
|
|
/* use the candidate coordinates to make a real argh */
|
2002-08-26 03:32:39 +00:00
|
|
|
arghx[index] = x;
|
|
|
|
arghy[index] = y;
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* now test wether we accidently hit the worm with argh ;) */
|
2002-09-09 23:30:16 +00:00
|
|
|
for (i = 0; i < worm_count && !collisionDetected; i++) {
|
|
|
|
collisionDetected |= worm_argh_collision(&worms[i], index);
|
2006-03-13 03:13:05 +00:00
|
|
|
collisionDetected |= worm_argh_collision_in_moves(&worms[i], index,
|
2002-09-09 23:30:16 +00:00
|
|
|
MIN_ARGH_DIST);
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
while (collisionDetected);
|
2006-05-22 06:56:39 +00:00
|
|
|
return;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws an argh in the lcd buffer.
|
|
|
|
* @param int index The index of the argh arrays under which
|
2006-03-13 03:13:05 +00:00
|
|
|
* the coordinates of the desired argh can be found. Ensure
|
2002-09-09 23:30:16 +00:00
|
|
|
* that the value is 0 <= index < argh_count <= MAX_ARGH.
|
2002-08-26 03:32:39 +00:00
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static void draw_argh(int index)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
|
|
|
/* draw the new argh */
|
2006-03-13 07:05:15 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2006-05-22 06:56:39 +00:00
|
|
|
rb->lcd_set_foreground(COLOR_ARGH);
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
|
|
|
rb->lcd_fillrect(arghx[index] + FIELD_RECT_X,
|
2009-08-03 16:30:08 +00:00
|
|
|
arghy[index] + FIELD_RECT_Y,
|
|
|
|
argh_size, argh_size);
|
2006-03-13 07:05:15 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2006-05-22 06:56:39 +00:00
|
|
|
rb->lcd_set_foreground(COLOR_FG);
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
static void virtual_player(struct worm *w);
|
|
|
|
/**
|
|
|
|
* Initialzes the specified worm with INITIAL_WORM_LENGTH
|
|
|
|
* and the tail at the specified position. The worm will
|
|
|
|
* be initialized alive and creeping EAST.
|
|
|
|
* @param struct worm *w The worm that is to be initialized
|
|
|
|
* @param int x The x coordinate at which the tail of the worm starts.
|
|
|
|
* x must be 0 <= x < FIELD_RECT_WIDTH.
|
|
|
|
* @param int y The y coordinate at which the tail of the worm starts
|
|
|
|
* y must be 0 <= y < FIELD_RECT_WIDTH.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static void init_worm(struct worm *w, int x, int y)
|
|
|
|
{
|
|
|
|
/* initialize the worm size */
|
|
|
|
w->head = 1;
|
|
|
|
w->tail = 0;
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2009-08-03 16:30:08 +00:00
|
|
|
w->x[w->head] = x + 1;
|
|
|
|
w->y[w->head] = y;
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2009-08-03 16:30:08 +00:00
|
|
|
w->x[w->tail] = x;
|
|
|
|
w->y[w->tail] = y;
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2009-08-03 16:30:08 +00:00
|
|
|
/* set the initial direction the worm creeps to */
|
|
|
|
w->dirx = 1;
|
|
|
|
w->diry = 0;
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2009-08-03 16:30:08 +00:00
|
|
|
w->growing = INITIAL_WORM_LENGTH - 1;
|
|
|
|
w->alive = true;
|
|
|
|
w->fetch_worm_direction = virtual_player;
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Writes the direction that was stored for
|
|
|
|
* human player 1 into the specified worm. This function
|
2006-03-13 03:13:05 +00:00
|
|
|
* may be used to be stored in worm.fetch_worm_direction.
|
|
|
|
* The value of
|
2002-09-09 23:30:16 +00:00
|
|
|
* the direction is read from player1_dir.
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param struct worm *w - The worm of which the direction
|
2002-09-09 23:30:16 +00:00
|
|
|
* is altered.
|
|
|
|
*/
|
|
|
|
static void human_player1(struct worm *w) {
|
|
|
|
set_worm_dir(w, player1_dir);
|
|
|
|
}
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Writes the direction that was stored for
|
|
|
|
* human player 2 into the specified worm. This function
|
2006-03-13 03:13:05 +00:00
|
|
|
* may be used to be stored in worm.fetch_worm_direction.
|
|
|
|
* The value of
|
2002-09-09 23:30:16 +00:00
|
|
|
* the direction is read from player2_dir.
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param struct worm *w - The worm of which the direction
|
2002-09-09 23:30:16 +00:00
|
|
|
* is altered.
|
|
|
|
*/
|
|
|
|
static void human_player2(struct worm *w) {
|
|
|
|
set_worm_dir(w, player2_dir);
|
|
|
|
}
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Writes the direction that was stored for
|
2006-03-13 03:13:05 +00:00
|
|
|
* human player using a remote control
|
2002-09-09 23:30:16 +00:00
|
|
|
* into the specified worm. This function
|
2006-03-13 03:13:05 +00:00
|
|
|
* may be used to be stored in worm.fetch_worm_direction.
|
|
|
|
* The value of
|
2002-09-09 23:30:16 +00:00
|
|
|
* the direction is read from player3_dir.
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param struct worm *w - The worm of which the direction
|
2002-09-09 23:30:16 +00:00
|
|
|
* is altered.
|
|
|
|
*/
|
|
|
|
static void remote_player(struct worm *w) {
|
|
|
|
set_worm_dir(w, player3_dir);
|
|
|
|
}
|
|
|
|
|
2002-08-26 03:32:39 +00:00
|
|
|
/**
|
|
|
|
* Initializes the worm-, food- and argh-arrays, draws a frame,
|
|
|
|
* makes some food and argh and display all that stuff.
|
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static void init_wormlet(void)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
|
|
|
int i;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
for (i = 0; i< worm_count; i++) {
|
2009-08-03 16:30:08 +00:00
|
|
|
/* Initialize all the worm coordinates to center. */
|
|
|
|
int x = (int)(FIELD_RECT_WIDTH / 2);
|
|
|
|
int y = (int)((FIELD_RECT_HEIGHT - 20)/ 2) + i * 10;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
init_worm(&worms[i], x, y);
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
player1_dir = EAST;
|
|
|
|
player2_dir = EAST;
|
|
|
|
player3_dir = EAST;
|
|
|
|
|
|
|
|
if (players > 0) {
|
|
|
|
worms[0].fetch_worm_direction = human_player1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (players > 1) {
|
|
|
|
if (use_remote) {
|
|
|
|
worms[1].fetch_worm_direction = remote_player;
|
|
|
|
} else {
|
|
|
|
worms[1].fetch_worm_direction = human_player2;
|
|
|
|
}
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
if (players > 2) {
|
|
|
|
worms[2].fetch_worm_direction = human_player2;
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/* Needed when the game is restarted using BTN_STOPRESET */
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* make and display some food and argh */
|
2002-09-09 23:30:16 +00:00
|
|
|
argh_count = MAX_FOOD;
|
2002-08-26 03:32:39 +00:00
|
|
|
for (i = 0; i < MAX_FOOD; i++) {
|
2002-09-09 23:30:16 +00:00
|
|
|
make_food(i);
|
|
|
|
draw_food(i);
|
|
|
|
make_argh(i);
|
|
|
|
draw_argh(i);
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* draw the game field */
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
|
|
|
|
rb->lcd_fillrect(0, 0, FIELD_RECT_WIDTH + 2, FIELD_RECT_HEIGHT + 2);
|
|
|
|
rb->lcd_fillrect(1, 1, FIELD_RECT_WIDTH, FIELD_RECT_HEIGHT);
|
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* make everything visible */
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Move the worm one step further if it is alive.
|
2006-03-13 03:13:05 +00:00
|
|
|
* The direction in which the worm moves is taken from dirx and diry.
|
2002-09-09 23:30:16 +00:00
|
|
|
* move_worm decreases growing if > 0. While the worm is growing the tail
|
|
|
|
* is left untouched.
|
|
|
|
* @param struct worm *w The worm to move. w must not be NULL.
|
2002-08-26 03:32:39 +00:00
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static void move_worm(struct worm *w)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
if (w->alive) {
|
|
|
|
/* determine the head point and its precessor */
|
|
|
|
int headx = w->x[w->head];
|
|
|
|
int heady = w->y[w->head];
|
|
|
|
int prehead = (w->head + MAX_WORM_SEGMENTS - 1) % MAX_WORM_SEGMENTS;
|
|
|
|
int preheadx = w->x[prehead];
|
|
|
|
int preheady = w->y[prehead];
|
|
|
|
|
|
|
|
/* determine the old direction */
|
|
|
|
int olddirx;
|
|
|
|
int olddiry;
|
|
|
|
if (headx == preheadx) {
|
|
|
|
olddirx = 0;
|
|
|
|
olddiry = (heady > preheady) ? 1 : -1;
|
|
|
|
} else {
|
|
|
|
olddiry = 0;
|
|
|
|
olddirx = (headx > preheadx) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* olddir == dir?
|
2006-03-13 03:13:05 +00:00
|
|
|
a change of direction means a new segment
|
2002-09-09 23:30:16 +00:00
|
|
|
has been opened */
|
|
|
|
if (olddirx != w->dirx ||
|
|
|
|
olddiry != w->diry) {
|
|
|
|
w->head = (w->head + 1) % MAX_WORM_SEGMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* new head position */
|
|
|
|
w->x[w->head] = headx + w->dirx;
|
|
|
|
w->y[w->head] = heady + w->diry;
|
|
|
|
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* while the worm is growing no tail procession is necessary */
|
|
|
|
if (w->growing > 0) {
|
2002-08-26 09:21:59 +00:00
|
|
|
/* update the worms grow state */
|
2002-09-09 23:30:16 +00:00
|
|
|
w->growing--;
|
|
|
|
}
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* if the worm isn't growing the tail has to be dragged */
|
|
|
|
else {
|
2006-03-13 03:13:05 +00:00
|
|
|
/* index of the end of the tail segment */
|
2002-09-09 23:30:16 +00:00
|
|
|
int tail_segment_end = (w->tail + 1) % MAX_WORM_SEGMENTS;
|
|
|
|
|
|
|
|
/* drag the end of the tail */
|
2006-03-13 03:13:05 +00:00
|
|
|
/* only one coordinate has to be altered. Here it is
|
|
|
|
determined which one */
|
2002-09-09 23:30:16 +00:00
|
|
|
int dir = 0; /* specifies wether the coord has to be in- or decreased */
|
|
|
|
if (w->x[w->tail] == w->x[tail_segment_end]) {
|
|
|
|
dir = (w->y[w->tail] - w->y[tail_segment_end] < 0) ? 1 : -1;
|
|
|
|
w->y[w->tail] += dir;
|
|
|
|
} else {
|
|
|
|
dir = (w->x[w->tail] - w->x[tail_segment_end] < 0) ? 1 : -1;
|
|
|
|
w->x[w->tail] += dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* when the tail has been dragged so far that it meets
|
|
|
|
the next segment start the tail segment is obsolete and
|
|
|
|
must be freed */
|
|
|
|
if (w->x[w->tail] == w->x[tail_segment_end] &&
|
|
|
|
w->y[w->tail] == w->y[tail_segment_end]){
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* drop the last tail point */
|
|
|
|
w->tail = tail_segment_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-03-13 03:13:05 +00:00
|
|
|
* Draws the head and clears the tail of the worm in
|
2002-08-26 03:32:39 +00:00
|
|
|
* the display buffer. lcd_update() is NOT called thus
|
|
|
|
* the caller has to take care that the buffer is displayed.
|
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static void draw_worm(struct worm *w)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
|
|
|
/* draw the new head */
|
2002-09-09 23:30:16 +00:00
|
|
|
int x = w->x[w->head];
|
|
|
|
int y = w->y[w->head];
|
2009-08-03 16:30:08 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_foreground(COLOR_WORM);
|
|
|
|
#endif
|
2002-09-09 23:30:16 +00:00
|
|
|
if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* clear the space behind the worm */
|
2002-09-09 23:30:16 +00:00
|
|
|
x = w->x[w->tail] ;
|
|
|
|
y = w->y[w->tail] ;
|
|
|
|
if (x >= 0 && x < FIELD_RECT_WIDTH && y >= 0 && y < FIELD_RECT_HEIGHT) {
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_drawpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
2006-03-13 07:05:15 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2006-05-22 06:56:39 +00:00
|
|
|
rb->lcd_set_foreground(COLOR_FG);
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Checks wether the coordinate is part of the worm. Returns
|
|
|
|
* true if any part of the worm was hit - including the head.
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param x int The x coordinate
|
2002-08-26 03:32:39 +00:00
|
|
|
* @param y int The y coordinate
|
|
|
|
* @return int The index of the worm arrays that contain x, y.
|
|
|
|
* Returns -1 if the coordinates are not part of the worm.
|
2006-03-13 03:13:05 +00:00
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static int specific_worm_collision(struct worm *w, int x, int y)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
int retVal = -1;
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* get_worm_array_length is expensive -> buffer the value */
|
|
|
|
int wormLength = get_worm_array_length(w);
|
2002-08-26 03:32:39 +00:00
|
|
|
int i;
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* test each entry that is part of the worm */
|
2002-08-26 03:32:39 +00:00
|
|
|
for (i = 0; i < wormLength && retVal == -1; i++) {
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* The iteration iterates the length of the worm.
|
|
|
|
Here's the conversion to the true indices within the worm arrays. */
|
2002-09-09 23:30:16 +00:00
|
|
|
int linestart = (w->tail + i ) % MAX_WORM_SEGMENTS;
|
|
|
|
int lineend = (linestart + 1) % MAX_WORM_SEGMENTS;
|
|
|
|
bool samex = (w->x[linestart] == x) && (w->x[lineend] == x);
|
|
|
|
bool samey = (w->y[linestart] == y) && (w->y[lineend] == y);
|
|
|
|
if (samex || samey){
|
|
|
|
int test, min, max, tmp;
|
|
|
|
|
|
|
|
if (samey) {
|
|
|
|
min = w->x[linestart];
|
|
|
|
max = w->x[lineend];
|
2006-03-13 03:13:05 +00:00
|
|
|
test = x;
|
2002-09-09 23:30:16 +00:00
|
|
|
} else {
|
|
|
|
min = w->y[linestart];
|
|
|
|
max = w->y[lineend];
|
|
|
|
test = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = min;
|
|
|
|
min = MIN(min, max);
|
|
|
|
max = MAX(tmp, max);
|
|
|
|
|
|
|
|
if (min <= test && test <= max) {
|
|
|
|
retVal = lineend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-03-13 03:13:05 +00:00
|
|
|
* Increases the length of the specified worm by marking
|
2002-09-09 23:30:16 +00:00
|
|
|
* that it may grow by len pixels. Note that the worm has
|
|
|
|
* to move to make the growing happen.
|
|
|
|
* @param worm *w The worm that is to be altered.
|
|
|
|
* @param int len A positive value specifying the amount of
|
|
|
|
* pixels the worm may grow.
|
|
|
|
*/
|
|
|
|
static void add_growing(struct worm *w, int len) {
|
|
|
|
w->growing += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determins the worm that is at the coordinates x, y. The parameter
|
|
|
|
* w is a switch parameter that changes the functionality of worm_collision.
|
|
|
|
* If w is specified and x,y hits the head of w NULL is returned.
|
2006-03-13 03:13:05 +00:00
|
|
|
* This is a useful way to determine wether the head of w hits
|
|
|
|
* any worm but including itself but excluding its own head.
|
2002-09-09 23:30:16 +00:00
|
|
|
* (It hits always its own head ;))
|
|
|
|
* If w is set to NULL worm_collision returns any worm including all heads
|
|
|
|
* that is at position of x,y.
|
|
|
|
* @param struct worm *w The worm of which the head should be excluded in
|
|
|
|
* the test. w may be set to NULL.
|
|
|
|
* @param int x The x coordinate that is checked
|
|
|
|
* @param int y The y coordinate that is checkec
|
|
|
|
* @return struct worm* The worm that has been hit by x,y. If no worm
|
|
|
|
* was at the position NULL is returned.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static struct worm* worm_collision(struct worm *w, int x, int y)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
struct worm *retVal = NULL;
|
|
|
|
int i;
|
|
|
|
for (i = 0; (i < worm_count) && (retVal == NULL); i++) {
|
|
|
|
int collision_at = specific_worm_collision(&worms[i], x, y);
|
|
|
|
if (collision_at != -1) {
|
|
|
|
if (!(w == &worms[i] && collision_at == w->head)){
|
|
|
|
retVal = &worms[i];
|
|
|
|
}
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the head of the worm just has
|
2006-03-13 03:13:05 +00:00
|
|
|
* crossed the field boundaries.
|
2002-08-26 03:32:39 +00:00
|
|
|
* @return bool true if the worm just has wrapped.
|
2006-03-13 03:13:05 +00:00
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static bool field_collision(struct worm *w)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
bool retVal = false;
|
2002-09-09 23:30:16 +00:00
|
|
|
if ((w->x[w->head] >= FIELD_RECT_WIDTH) ||
|
|
|
|
(w->y[w->head] >= FIELD_RECT_HEIGHT) ||
|
|
|
|
(w->x[w->head] < 0) ||
|
|
|
|
(w->y[w->head] < 0))
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
retVal = true;
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2002-08-26 03:32:39 +00:00
|
|
|
/**
|
2006-03-13 03:13:05 +00:00
|
|
|
* Returns true if the specified coordinates are within the
|
2002-09-09 23:30:16 +00:00
|
|
|
* field specified by the FIELD_RECT_XXX constants.
|
|
|
|
* @param int x The x coordinate of the point that is investigated
|
|
|
|
* @param int y The y coordinate of the point that is investigated
|
2006-03-13 03:13:05 +00:00
|
|
|
* @return bool Returns false if x,y specifies a point outside the
|
2002-09-09 23:30:16 +00:00
|
|
|
* field of worms.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool is_in_field_rect(int x, int y)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
bool retVal = false;
|
|
|
|
retVal = (x >= 0 && x < FIELD_RECT_WIDTH &&
|
|
|
|
y >= 0 && y < FIELD_RECT_HEIGHT);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks and returns wether the head of the w
|
2002-08-26 03:32:39 +00:00
|
|
|
* is colliding with something currently.
|
2002-08-26 09:21:59 +00:00
|
|
|
* @return int One of the values:
|
|
|
|
* COLLISION_NONE
|
2002-09-09 23:30:16 +00:00
|
|
|
* COLLISION_w
|
2002-08-26 09:21:59 +00:00
|
|
|
* COLLISION_FOOD
|
|
|
|
* COLLISION_ARGH
|
|
|
|
* COLLISION_FIELD
|
2002-08-26 03:32:39 +00:00
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static int check_collision(struct worm *w)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
int retVal = COLLISION_NONE;
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL)
|
2002-08-26 03:32:39 +00:00
|
|
|
retVal = COLLISION_WORM;
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
if (food_collision(w->x[w->head], w->y[w->head]) >= 0)
|
2002-08-26 03:32:39 +00:00
|
|
|
retVal = COLLISION_FOOD;
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
if (argh_collision(w->x[w->head], w->y[w->head]) >= 0)
|
2002-08-26 03:32:39 +00:00
|
|
|
retVal = COLLISION_ARGH;
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
if (field_collision(w))
|
2002-08-26 03:32:39 +00:00
|
|
|
retVal = COLLISION_FIELD;
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Returns the index of the food that is closest to the point
|
2006-03-13 03:13:05 +00:00
|
|
|
* specified by x, y. This index may be used in the foodx and
|
2002-09-09 23:30:16 +00:00
|
|
|
* foody arrays.
|
|
|
|
* @param int x The x coordinate of the point
|
|
|
|
* @param int y The y coordinate of the point
|
|
|
|
* @return int A value usable as index in foodx and foody.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static int get_nearest_food(int x, int y)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int nearestfood = 0;
|
|
|
|
int olddistance = FIELD_RECT_WIDTH + FIELD_RECT_HEIGHT;
|
|
|
|
int deltax = 0;
|
|
|
|
int deltay = 0;
|
|
|
|
int foodindex;
|
|
|
|
for (foodindex = 0; foodindex < MAX_FOOD; foodindex++) {
|
|
|
|
int distance;
|
|
|
|
deltax = foodx[foodindex] - x;
|
|
|
|
deltay = foody[foodindex] - y;
|
|
|
|
deltax = deltax > 0 ? deltax : deltax * (-1);
|
|
|
|
deltay = deltay > 0 ? deltay : deltay * (-1);
|
|
|
|
distance = deltax + deltay;
|
|
|
|
|
|
|
|
if (distance < olddistance) {
|
|
|
|
olddistance = distance;
|
|
|
|
nearestfood = foodindex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nearestfood;
|
|
|
|
}
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* Returns wether the specified position is next to the worm
|
2006-03-13 03:13:05 +00:00
|
|
|
* and in the direction the worm looks. Use this method to
|
|
|
|
* test wether this position would be hit with the next move of
|
2002-09-09 23:30:16 +00:00
|
|
|
* the worm unless the worm changes its direction.
|
|
|
|
* @param struct worm *w - The worm to be investigated
|
|
|
|
* @param int x - The x coordinate of the position to test.
|
|
|
|
* @param int y - The y coordinate of the position to test.
|
|
|
|
* @return Returns true if the worm will hit the position unless
|
|
|
|
* it change its direction before the next move.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool is_in_front_of_worm(struct worm *w, int x, int y)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
bool infront = false;
|
|
|
|
int deltax = x - w->x[w->head];
|
|
|
|
int deltay = y - w->y[w->head];
|
|
|
|
|
|
|
|
if (w->dirx == 0) {
|
|
|
|
infront = (w->diry * deltay) > 0;
|
|
|
|
} else {
|
|
|
|
infront = (w->dirx * deltax) > 0;
|
|
|
|
}
|
|
|
|
return infront;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the worm will collide with the next move unless
|
|
|
|
* it changes its direction.
|
|
|
|
* @param struct worm *w - The worm to be investigated.
|
|
|
|
* @return Returns true if the worm will collide with the next move
|
|
|
|
* unless it changes its direction.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool will_worm_collide(struct worm *w)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int x = w->x[w->head] + w->dirx;
|
|
|
|
int y = w->y[w->head] + w->diry;
|
|
|
|
bool retVal = !is_in_field_rect(x, y);
|
|
|
|
if (!retVal) {
|
|
|
|
retVal = (argh_collision(x, y) != -1);
|
|
|
|
}
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
if (!retVal) {
|
|
|
|
retVal = (worm_collision(w, x, y) != NULL);
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
/**
|
2002-09-09 23:30:16 +00:00
|
|
|
* This function
|
|
|
|
* may be used to be stored in worm.fetch_worm_direction for
|
2006-03-13 03:13:05 +00:00
|
|
|
* worms that are not controlled by humans but by artificial stupidity.
|
2002-09-09 23:30:16 +00:00
|
|
|
* A direction is searched that doesn't lead to collision but to the nearest
|
|
|
|
* food - but not very intelligent. The direction is written to the specified
|
|
|
|
* worm.
|
2006-03-13 03:13:05 +00:00
|
|
|
* @param struct worm *w - The worm of which the direction
|
2002-09-09 23:30:16 +00:00
|
|
|
* is altered.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static void virtual_player(struct worm *w)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
bool isright;
|
|
|
|
int plana, planb, planc;
|
|
|
|
/* find the next lunch */
|
|
|
|
int nearestfood = get_nearest_food(w->x[w->head], w->y[w->head]);
|
|
|
|
|
|
|
|
/* determine in which direction it is */
|
|
|
|
|
|
|
|
/* in front of me? */
|
|
|
|
bool infront = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
|
|
|
|
|
|
|
|
/* left right of me? */
|
|
|
|
int olddir = get_worm_dir(w);
|
|
|
|
set_worm_dir(w, (olddir + 1) % 4);
|
|
|
|
isright = is_in_front_of_worm(w, foodx[nearestfood], foody[nearestfood]);
|
|
|
|
set_worm_dir(w, olddir);
|
|
|
|
|
|
|
|
/* detect situation, set strategy */
|
|
|
|
if (infront) {
|
|
|
|
if (isright) {
|
|
|
|
plana = olddir;
|
|
|
|
planb = (olddir + 1) % 4;
|
|
|
|
planc = (olddir + 3) % 4;
|
|
|
|
} else {
|
|
|
|
plana = olddir;
|
|
|
|
planb = (olddir + 3) % 4;
|
|
|
|
planc = (olddir + 1) % 4;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isright) {
|
|
|
|
plana = (olddir + 1) % 4;
|
|
|
|
planb = olddir;
|
|
|
|
planc = (olddir + 3) % 4;
|
|
|
|
} else {
|
|
|
|
plana = (olddir + 3) % 4;
|
|
|
|
planb = olddir;
|
|
|
|
planc = (olddir + 1) % 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test for collision */
|
|
|
|
set_worm_dir(w, plana);
|
|
|
|
if (will_worm_collide(w)){
|
|
|
|
|
|
|
|
/* plan b */
|
|
|
|
set_worm_dir(w, planb);
|
|
|
|
|
|
|
|
/* test for collision */
|
|
|
|
if (will_worm_collide(w)) {
|
|
|
|
|
|
|
|
/* plan c */
|
|
|
|
set_worm_dir(w, planc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* prints out the score board with all the status information
|
2002-08-26 03:32:39 +00:00
|
|
|
* about the game.
|
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static void score_board(void)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int i;
|
|
|
|
int y = 0;
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, 0,
|
|
|
|
LCD_WIDTH - FIELD_RECT_WIDTH - 2, LCD_HEIGHT);
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
2002-09-09 23:30:16 +00:00
|
|
|
for (i = 0; i < worm_count; i++) {
|
|
|
|
int score = get_score(&worms[i]);
|
2010-08-28 21:46:32 +00:00
|
|
|
int collision = check_collision(&worms[i]);
|
|
|
|
const char *state_str;
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* high score */
|
|
|
|
if (worms[i].fetch_worm_direction != virtual_player){
|
|
|
|
if (highscore < score) {
|
|
|
|
highscore = score;
|
|
|
|
}
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* worm state */
|
2010-08-28 21:46:32 +00:00
|
|
|
if (collision == COLLISION_NONE) {
|
|
|
|
if (worms[i].growing > 0)
|
|
|
|
state_str = "Growing";
|
|
|
|
else {
|
|
|
|
state_str = worms[i].alive ? "Hungry" : "Wormed";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state_str = state_desc[collision];
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
2010-08-28 21:46:32 +00:00
|
|
|
|
|
|
|
/* length */
|
|
|
|
rb->lcd_putsxyf(FIELD_RECT_WIDTH + 3, y , "Len:%d", score);
|
|
|
|
rb->lcd_putsxyf(FIELD_RECT_WIDTH + 3, y+8, state_str);
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
if (!worms[i].alive){
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
|
|
|
|
rb->lcd_fillrect(FIELD_RECT_WIDTH + 2, y,
|
|
|
|
LCD_WIDTH - FIELD_RECT_WIDTH - 2, 17);
|
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
y += 19;
|
|
|
|
}
|
2010-08-28 21:46:32 +00:00
|
|
|
#ifdef DEBUG_WORMLET
|
|
|
|
rb->lcd_putsxyf(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, "ticks %d", max_cycle);
|
2002-09-09 23:30:16 +00:00
|
|
|
#else
|
2010-08-28 21:46:32 +00:00
|
|
|
rb->lcd_putsxyf(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, "Hs: %d", highscore);
|
2002-09-09 23:30:16 +00:00
|
|
|
#endif
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for collisions of the worm and its environment and
|
|
|
|
* takes appropriate actions like growing the worm or killing it.
|
|
|
|
* @return bool Returns true if the worm is dead. Returns
|
|
|
|
* false if the worm is healthy, up and creeping.
|
|
|
|
*/
|
2002-09-09 23:30:16 +00:00
|
|
|
static bool process_collisions(struct worm *w)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
int index = -1;
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
w->alive &= !field_collision(w);
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
if (w->alive) {
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* check if food was eaten */
|
2002-09-09 23:30:16 +00:00
|
|
|
index = food_collision(w->x[w->head], w->y[w->head]);
|
2002-08-26 03:32:39 +00:00
|
|
|
if (index != -1){
|
2002-08-26 09:21:59 +00:00
|
|
|
int i;
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
clear_food(index);
|
|
|
|
make_food(index);
|
|
|
|
draw_food(index);
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
for (i = 0; i < arghs_per_food; i++) {
|
2002-09-09 23:30:16 +00:00
|
|
|
argh_count++;
|
|
|
|
if (argh_count > MAX_ARGH)
|
|
|
|
argh_count = MAX_ARGH;
|
|
|
|
make_argh(argh_count - 1);
|
|
|
|
draw_argh(argh_count - 1);
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
add_growing(w, worm_food);
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
draw_worm(w);
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* check if argh was eaten */
|
2002-08-26 03:32:39 +00:00
|
|
|
else {
|
2002-09-09 23:30:16 +00:00
|
|
|
index = argh_collision(w->x[w->head], w->y[w->head]);
|
|
|
|
if (index != -1) {
|
|
|
|
w->alive = false;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
else {
|
|
|
|
if (worm_collision(w, w->x[w->head], w->y[w->head]) != NULL) {
|
|
|
|
w->alive = false;
|
2009-08-03 16:30:08 +00:00
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !w->alive;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main loop of the game.
|
|
|
|
* @return bool Returns true if the game ended
|
|
|
|
* with a dead worm. Returns false if the user
|
|
|
|
* aborted the game manually.
|
2006-03-13 03:13:05 +00:00
|
|
|
*/
|
2006-05-22 06:56:39 +00:00
|
|
|
static int run(void)
|
2002-08-26 09:21:59 +00:00
|
|
|
{
|
2002-08-26 03:32:39 +00:00
|
|
|
int button = 0;
|
|
|
|
int wormDead = false;
|
2006-05-22 06:56:39 +00:00
|
|
|
bool paused = false;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
/* ticks are counted to compensate speed variations */
|
|
|
|
long cycle_start = 0, cycle_end = 0;
|
|
|
|
#ifdef DEBUG_WORMLET
|
|
|
|
int ticks_to_max_cycle_reset = 20;
|
2010-08-28 21:46:32 +00:00
|
|
|
max_cycle = 0;
|
2002-09-09 23:30:16 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-26 09:21:59 +00:00
|
|
|
/* initialize the board and so on */
|
2002-09-09 23:30:16 +00:00
|
|
|
init_wormlet();
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
cycle_start = *rb->current_tick;
|
2002-08-26 09:21:59 +00:00
|
|
|
/* change the direction of the worm */
|
2006-05-22 06:56:39 +00:00
|
|
|
while (!wormDead)
|
2002-08-26 03:32:39 +00:00
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int i;
|
2006-05-22 06:56:39 +00:00
|
|
|
long cycle_duration=0;
|
|
|
|
|
2006-07-30 03:10:09 +00:00
|
|
|
#ifdef HAS_BUTTON_HOLD
|
|
|
|
if (rb->button_hold())
|
|
|
|
paused = true;
|
|
|
|
#endif
|
|
|
|
|
2002-08-26 03:32:39 +00:00
|
|
|
switch (button) {
|
2006-05-22 06:56:39 +00:00
|
|
|
case BTN_STARTPAUSE:
|
|
|
|
paused = !paused;
|
2002-08-26 03:32:39 +00:00
|
|
|
break;
|
2006-05-22 06:56:39 +00:00
|
|
|
case BTN_STOPRESET:
|
|
|
|
if (paused)
|
|
|
|
return 1; /* restart game */
|
|
|
|
else
|
|
|
|
paused = true;
|
2002-08-26 03:32:39 +00:00
|
|
|
break;
|
2006-06-30 16:43:47 +00:00
|
|
|
#ifdef BTN_RC_QUIT
|
|
|
|
case BTN_RC_QUIT:
|
|
|
|
#endif
|
2006-05-22 06:56:39 +00:00
|
|
|
case BTN_QUIT:
|
|
|
|
return 2; /* back to menu */
|
2002-08-26 03:32:39 +00:00
|
|
|
break;
|
2006-05-22 06:56:39 +00:00
|
|
|
}
|
|
|
|
if (!paused)
|
|
|
|
{
|
|
|
|
switch (button) {
|
|
|
|
case BTN_DIR_UP:
|
|
|
|
if (players == 1 && !use_remote) {
|
|
|
|
player1_dir = NORTH;
|
|
|
|
}
|
|
|
|
break;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
case BTN_DIR_DOWN:
|
|
|
|
if (players == 1 && !use_remote) {
|
|
|
|
player1_dir = SOUTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTN_DIR_LEFT:
|
|
|
|
if (players != 1 || use_remote) {
|
|
|
|
player1_dir = (player1_dir + 3) % 4;
|
|
|
|
} else {
|
|
|
|
player1_dir = WEST;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTN_DIR_RIGHT:
|
|
|
|
if (players != 1 || use_remote) {
|
|
|
|
player1_dir = (player1_dir + 1) % 4;
|
|
|
|
} else {
|
|
|
|
player1_dir = EAST;
|
|
|
|
}
|
|
|
|
break;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
#ifdef MULTIPLAYER
|
2006-05-22 06:56:39 +00:00
|
|
|
case BTN_PLAYER2_DIR1:
|
|
|
|
player2_dir = (player2_dir + 3) % 4;
|
|
|
|
break;
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
case BTN_PLAYER2_DIR2:
|
|
|
|
player2_dir = (player2_dir + 1) % 4;
|
|
|
|
break;
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2006-03-13 03:13:05 +00:00
|
|
|
#ifdef REMOTE
|
2006-05-22 06:56:39 +00:00
|
|
|
case BTN_RC_UP:
|
|
|
|
player3_dir = (player3_dir + 1) % 4;
|
|
|
|
break;
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
case BTN_RC_DOWN:
|
|
|
|
player3_dir = (player3_dir + 3) % 4;
|
|
|
|
break;
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
2006-05-22 06:56:39 +00:00
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
for (i = 0; i < worm_count; i++) {
|
|
|
|
worms[i].fetch_worm_direction(&worms[i]);
|
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
wormDead = true;
|
2006-05-22 06:56:39 +00:00
|
|
|
for (i = 0; i < worm_count; i++){
|
|
|
|
struct worm *w = &worms[i];
|
|
|
|
move_worm(w);
|
|
|
|
wormDead &= process_collisions(w);
|
|
|
|
draw_worm(w);
|
|
|
|
}
|
|
|
|
score_board();
|
|
|
|
rb->lcd_update();
|
|
|
|
if (button == BTN_STOPRESET) {
|
|
|
|
wormDead = true;
|
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
/* here the wormlet game cycle ends
|
|
|
|
thus the current tick is stored
|
|
|
|
as end time */
|
|
|
|
cycle_end = *rb->current_tick;
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
/* The duration of the game cycle */
|
|
|
|
cycle_duration = cycle_end - cycle_start;
|
|
|
|
cycle_duration = MAX(0, cycle_duration);
|
|
|
|
cycle_duration = MIN(speed -1, cycle_duration);
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_WORMLET
|
2006-05-22 06:56:39 +00:00
|
|
|
ticks_to_max_cycle_reset--;
|
|
|
|
if (ticks_to_max_cycle_reset <= 0) {
|
|
|
|
max_cycle = 0;
|
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
if (max_cycle < cycle_duration) {
|
|
|
|
max_cycle = cycle_duration;
|
|
|
|
ticks_to_max_cycle_reset = 20;
|
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
#endif
|
2006-05-22 06:56:39 +00:00
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
/* adjust the number of ticks to wait for a button.
|
|
|
|
This ensures that a complete game cycle including
|
|
|
|
user input runs in constant time */
|
2006-05-22 06:56:39 +00:00
|
|
|
button = rb->button_get_w_tmo(speed - cycle_duration);
|
2003-06-29 16:33:04 +00:00
|
|
|
cycle_start = *rb->current_tick;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
2006-05-22 06:56:39 +00:00
|
|
|
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Game Over!");
|
2006-05-22 06:56:39 +00:00
|
|
|
|
|
|
|
return 2; /* back to menu */
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
#ifdef DEBUG_WORMLET
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just a test routine that checks that worm_food_collision works
|
|
|
|
* in some typical situations.
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static void test_worm_food_collision(void)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int collision_count = 0;
|
|
|
|
int i;
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
2002-09-09 23:30:16 +00:00
|
|
|
init_worm(&worms[0], 10, 10);
|
|
|
|
add_growing(&worms[0], 10);
|
|
|
|
set_worm_dir(&worms[0], EAST);
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
move_worm(&worms[0]);
|
|
|
|
draw_worm(&worms[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_worm_dir(&worms[0], SOUTH);
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
move_worm(&worms[0]);
|
|
|
|
draw_worm(&worms[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foodx[0] = 15;
|
|
|
|
foody[0] = 12;
|
|
|
|
for (foody[0] = 20; foody[0] > 0; foody[0] --) {
|
|
|
|
bool collision;
|
|
|
|
draw_worm(&worms[0]);
|
|
|
|
draw_food(0);
|
|
|
|
collision = worm_food_collision(&worms[0], 0);
|
|
|
|
if (collision) {
|
|
|
|
collision_count++;
|
|
|
|
}
|
2010-08-28 21:46:32 +00:00
|
|
|
rb->lcd_putsxyf(0, LCD_HEIGHT -8, "collisions: %d", collision_count);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
2006-05-22 06:56:39 +00:00
|
|
|
if (collision_count != food_size) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foody[0] = 15;
|
|
|
|
for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) {
|
|
|
|
bool collision;
|
|
|
|
draw_worm(&worms[0]);
|
|
|
|
draw_food(0);
|
|
|
|
collision = worm_food_collision(&worms[0], 0);
|
|
|
|
if (collision) {
|
|
|
|
collision_count ++;
|
|
|
|
}
|
2010-08-28 21:46:32 +00:00
|
|
|
rb->lcd_putsxyf(0, LCD_HEIGHT -8, "collisions: %d", collision_count);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
2006-05-22 06:56:39 +00:00
|
|
|
if (collision_count != food_size * 2) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-08-03 16:30:08 +00:00
|
|
|
static bool expensive_worm_in_rect(struct worm *w, int rx, int ry, int rw, int rh)
|
|
|
|
{
|
2002-09-17 12:39:47 +00:00
|
|
|
int x, y;
|
|
|
|
bool retVal = false;
|
|
|
|
for (x = rx; x < rx + rw; x++){
|
|
|
|
for (y = ry; y < ry + rh; y++) {
|
|
|
|
if (specific_worm_collision(w, x, y) != -1) {
|
|
|
|
retVal = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2009-08-03 16:30:08 +00:00
|
|
|
static void test_worm_argh_collision(void)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int i;
|
|
|
|
int dir;
|
|
|
|
int collision_count = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
2002-09-09 23:30:16 +00:00
|
|
|
init_worm(&worms[0], 10, 10);
|
|
|
|
add_growing(&worms[0], 40);
|
|
|
|
for (dir = 0; dir < 4; dir++) {
|
|
|
|
set_worm_dir(&worms[0], (EAST + dir) % 4);
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
move_worm(&worms[0]);
|
|
|
|
draw_worm(&worms[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
arghx[0] = 12;
|
2006-05-22 06:56:39 +00:00
|
|
|
for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){
|
2002-09-09 23:30:16 +00:00
|
|
|
bool collision;
|
|
|
|
draw_argh(0);
|
|
|
|
collision = worm_argh_collision(&worms[0], 0);
|
|
|
|
if (collision) {
|
|
|
|
collision_count ++;
|
|
|
|
}
|
2010-08-28 21:46:32 +00:00
|
|
|
rb->lcd_putsxyf(0, LCD_HEIGHT -8, "collisions: %d", collision_count);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
2006-05-22 06:56:39 +00:00
|
|
|
if (collision_count != argh_size * 2) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arghy[0] = 12;
|
2006-05-22 06:56:39 +00:00
|
|
|
for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){
|
2002-09-09 23:30:16 +00:00
|
|
|
bool collision;
|
|
|
|
draw_argh(0);
|
|
|
|
collision = worm_argh_collision(&worms[0], 0);
|
|
|
|
if (collision) {
|
|
|
|
collision_count ++;
|
|
|
|
}
|
2010-08-28 21:46:32 +00:00
|
|
|
rb->lcd_putsxyf(0, LCD_HEIGHT -8, "collisions: %d", collision_count);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
2006-05-22 06:56:39 +00:00
|
|
|
if (collision_count != argh_size * 4) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-03 16:30:08 +00:00
|
|
|
static int testline_in_rect(void)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int testfailed = -1;
|
|
|
|
|
|
|
|
int rx = 10;
|
|
|
|
int ry = 15;
|
|
|
|
int rw = 20;
|
|
|
|
int rh = 25;
|
|
|
|
|
|
|
|
/* Test 1 */
|
|
|
|
int x1 = 12;
|
|
|
|
int y1 = 8;
|
|
|
|
int x2 = 12;
|
|
|
|
int y2 = 42;
|
|
|
|
|
|
|
|
if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
|
|
|
!line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 1");
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 2 */
|
|
|
|
y2 = 20;
|
|
|
|
if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
|
|
|
!line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 2");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 3 */
|
|
|
|
y1 = 30;
|
|
|
|
if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
|
|
|
!line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 3");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 4 */
|
|
|
|
y2 = 45;
|
|
|
|
if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
|
|
|
!line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 4");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 5 */
|
|
|
|
y1 = 50;
|
|
|
|
if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
|
|
|
|
line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 5");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 6 */
|
|
|
|
y1 = 5;
|
|
|
|
y2 = 7;
|
|
|
|
if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
|
|
|
|
line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 6");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 7 */
|
|
|
|
x1 = 8;
|
|
|
|
y1 = 20;
|
|
|
|
x2 = 35;
|
|
|
|
y2 = 20;
|
|
|
|
if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
|
|
|
!line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 7");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 8 */
|
|
|
|
x2 = 12;
|
|
|
|
if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
|
|
|
!line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 8");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 9 */
|
|
|
|
x1 = 25;
|
|
|
|
if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
|
|
|
!line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 9");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 10 */
|
|
|
|
x2 = 37;
|
|
|
|
if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
|
|
|
!line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 10");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 11 */
|
|
|
|
x1 = 42;
|
|
|
|
if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
|
|
|
|
line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 11");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 12 */
|
|
|
|
x1 = 5;
|
|
|
|
x2 = 7;
|
|
|
|
if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) ||
|
|
|
|
line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 12");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test 13 */
|
|
|
|
rx = 9;
|
|
|
|
ry = 15;
|
2006-05-22 06:56:39 +00:00
|
|
|
rw = food_size;
|
|
|
|
rh = food_size;
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
x1 = 10;
|
|
|
|
y1 = 10;
|
|
|
|
x2 = 10;
|
|
|
|
y2 = 20;
|
|
|
|
if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
2002-09-24 18:04:15 +00:00
|
|
|
line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 13");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
testfailed = 13;
|
|
|
|
}
|
|
|
|
|
2002-09-17 12:39:47 +00:00
|
|
|
/* test 14 */
|
|
|
|
rx = 9;
|
|
|
|
ry = 15;
|
|
|
|
rw = 4;
|
|
|
|
rh = 4;
|
|
|
|
|
|
|
|
x1 = 10;
|
|
|
|
y1 = 10;
|
|
|
|
x2 = 10;
|
|
|
|
y2 = 19;
|
|
|
|
if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) &&
|
2002-09-24 18:04:15 +00:00
|
|
|
line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_drawline(x1, y1, x2, y2);
|
|
|
|
rb->lcd_invertrect(rx, ry, rw, rh);
|
|
|
|
rb->lcd_putsxy(0, 0, "failed 14");
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(true);
|
2002-09-17 12:39:47 +00:00
|
|
|
testfailed = 14;
|
|
|
|
}
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
return testfailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just a test routine to test wether specific_worm_collision might work properly
|
|
|
|
*/
|
2009-08-03 16:30:08 +00:00
|
|
|
static int test_specific_worm_collision(void)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int collisions = 0;
|
|
|
|
int dir;
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
2002-09-09 23:30:16 +00:00
|
|
|
init_worm(&worms[0], 10, 20);
|
|
|
|
add_growing(&worms[0], 20 - INITIAL_WORM_LENGTH);
|
|
|
|
|
|
|
|
for (dir = EAST; dir < EAST + 4; dir++) {
|
|
|
|
int i;
|
|
|
|
set_worm_dir(&worms[0], dir % 4);
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
if (!(dir % 4 == NORTH && i == 9)) {
|
|
|
|
move_worm(&worms[0]);
|
|
|
|
draw_worm(&worms[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (y = 15; y < 30; y ++){
|
|
|
|
for (x = 5; x < 20; x++) {
|
|
|
|
if (specific_worm_collision(&worms[0], x, y) != -1) {
|
|
|
|
collisions ++;
|
|
|
|
}
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_invertpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y);
|
2010-08-28 21:46:32 +00:00
|
|
|
rb->lcd_putsxyf(0, LCD_HEIGHT - 8, "collisions %d", collisions);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (collisions != 21) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->button_get(true);
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
return collisions;
|
|
|
|
}
|
|
|
|
|
2009-08-03 16:30:08 +00:00
|
|
|
static void test_make_argh(void)
|
|
|
|
{
|
2002-09-09 23:30:16 +00:00
|
|
|
int dir;
|
|
|
|
int seed = 0;
|
|
|
|
int hit = 0;
|
|
|
|
int failures = 0;
|
2002-09-17 12:39:47 +00:00
|
|
|
int last_failures = 0;
|
|
|
|
int i, worm_idx;
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
2002-09-17 12:39:47 +00:00
|
|
|
worm_count = 3;
|
|
|
|
|
|
|
|
for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
|
|
|
|
init_worm(&worms[worm_idx], 10 + worm_idx * 20, 20);
|
|
|
|
add_growing(&worms[worm_idx], 40 - INITIAL_WORM_LENGTH);
|
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
for (dir = EAST; dir < EAST + 4; dir++) {
|
2002-09-17 12:39:47 +00:00
|
|
|
for (worm_idx = 0; worm_idx < worm_count; worm_idx++) {
|
|
|
|
set_worm_dir(&worms[worm_idx], dir % 4);
|
2002-09-09 23:30:16 +00:00
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
if (!(dir % 4 == NORTH && i == 9)) {
|
2002-09-17 12:39:47 +00:00
|
|
|
move_worm(&worms[worm_idx]);
|
|
|
|
draw_worm(&worms[worm_idx]);
|
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
for (seed = 0; hit < 20; seed += 2) {
|
2002-09-17 12:39:47 +00:00
|
|
|
int x, y;
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->srand(seed);
|
2006-05-22 06:56:39 +00:00
|
|
|
x = rb->rand() % (FIELD_RECT_WIDTH - argh_size);
|
|
|
|
y = rb->rand() % (FIELD_RECT_HEIGHT - argh_size);
|
2002-09-17 12:39:47 +00:00
|
|
|
|
|
|
|
for (worm_idx = 0; worm_idx < worm_count; worm_idx++){
|
2006-05-22 06:56:39 +00:00
|
|
|
if (expensive_worm_in_rect(&worms[worm_idx], x, y, argh_size, argh_size)) {
|
2002-09-17 12:39:47 +00:00
|
|
|
int tries = 0;
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->srand(seed);
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2002-09-17 12:39:47 +00:00
|
|
|
tries = make_argh(0);
|
|
|
|
if ((x == arghx[0] && y == arghy[0]) || tries < 2) {
|
2009-08-03 16:30:08 +00:00
|
|
|
failures ++;
|
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2010-08-28 21:46:32 +00:00
|
|
|
rb->lcd_putsxyf(0, LCD_HEIGHT - 8, "(%d;%d) fail%d try%d",
|
2009-08-03 16:30:08 +00:00
|
|
|
x, y, failures, tries);
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y,
|
|
|
|
argh_size, argh_size);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2002-09-17 12:39:47 +00:00
|
|
|
draw_argh(0);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->lcd_invertrect(x + FIELD_RECT_X, y + FIELD_RECT_Y,
|
|
|
|
argh_size, argh_size);
|
|
|
|
rb->lcd_clearrect(arghx[0] + FIELD_RECT_X, arghy[0] + FIELD_RECT_Y,
|
|
|
|
argh_size, argh_size);
|
2002-09-09 23:30:16 +00:00
|
|
|
|
2002-09-17 12:39:47 +00:00
|
|
|
if (failures > last_failures) {
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->button_get(true);
|
|
|
|
}
|
2002-09-17 12:39:47 +00:00
|
|
|
last_failures = failures;
|
2009-08-03 16:30:08 +00:00
|
|
|
hit ++;
|
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_worm_argh_collision_in_moves(void) {
|
|
|
|
int hit_count = 0;
|
|
|
|
int i;
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
2002-09-09 23:30:16 +00:00
|
|
|
init_worm(&worms[0], 10, 20);
|
|
|
|
|
|
|
|
arghx[0] = 20;
|
|
|
|
arghy[0] = 18;
|
|
|
|
draw_argh(0);
|
|
|
|
|
|
|
|
set_worm_dir(&worms[0], EAST);
|
2002-09-17 12:39:47 +00:00
|
|
|
for (i = 0; i < 20; i++) {
|
2002-09-09 23:30:16 +00:00
|
|
|
move_worm(&worms[0]);
|
|
|
|
draw_worm(&worms[0]);
|
|
|
|
if (worm_argh_collision_in_moves(&worms[0], 0, 5)){
|
|
|
|
hit_count ++;
|
|
|
|
}
|
2010-08-28 21:46:32 +00:00
|
|
|
rb->lcd_putsxyf(0, LCD_HEIGHT - 8, "in 5 moves hits: %d", hit_count);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2006-03-13 03:13:05 +00:00
|
|
|
}
|
2006-05-22 06:56:39 +00:00
|
|
|
if (hit_count != argh_size + 5) {
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->button_get(true);
|
2006-03-13 03:13:05 +00:00
|
|
|
}
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
#endif /* DEBUG_WORMLET */
|
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
/*
|
2009-08-03 16:30:08 +00:00
|
|
|
* Reverts default settings
|
|
|
|
*/
|
|
|
|
static void default_settings(void)
|
2006-05-22 06:56:39 +00:00
|
|
|
{
|
|
|
|
arghs_per_food = ARGHS_PER_FOOD;
|
|
|
|
argh_size = ARGH_SIZE;
|
|
|
|
food_size = FOOD_SIZE;
|
|
|
|
speed = SPEED;
|
|
|
|
worm_food = WORM_PER_FOOD;
|
|
|
|
players = 1;
|
|
|
|
worm_count = MAX_WORMS;
|
|
|
|
use_remote = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-08-03 16:30:08 +00:00
|
|
|
* Launches the wormlet game
|
|
|
|
*/
|
|
|
|
static bool launch_wormlet(void)
|
2006-05-22 06:56:39 +00:00
|
|
|
{
|
|
|
|
int game_result = 1;
|
|
|
|
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
|
2007-08-15 12:42:09 +00:00
|
|
|
/* Turn off backlight timeout */
|
2011-01-24 12:29:16 +00:00
|
|
|
backlight_ignore_timeout();
|
2006-05-22 06:56:39 +00:00
|
|
|
|
|
|
|
/* start the game */
|
|
|
|
while (game_result == 1)
|
|
|
|
game_result = run();
|
|
|
|
|
|
|
|
switch (game_result)
|
|
|
|
{
|
|
|
|
case 2:
|
2007-08-15 12:42:09 +00:00
|
|
|
/* Turn on backlight timeout (revert to settings) */
|
2011-01-24 12:29:16 +00:00
|
|
|
backlight_use_settings();
|
2006-05-22 06:56:39 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-08-26 03:32:39 +00:00
|
|
|
/**
|
2003-06-29 16:33:04 +00:00
|
|
|
* Main entry point
|
2002-08-26 03:32:39 +00:00
|
|
|
*/
|
2009-01-16 10:34:40 +00:00
|
|
|
enum plugin_status plugin_start(const void* parameter)
|
2002-08-26 03:32:39 +00:00
|
|
|
{
|
2006-05-22 06:56:39 +00:00
|
|
|
int result;
|
|
|
|
int menu_quit = 0;
|
|
|
|
int new_setting;
|
2006-03-13 03:13:05 +00:00
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
(void)(parameter);
|
2006-05-22 06:56:39 +00:00
|
|
|
|
|
|
|
default_settings();
|
|
|
|
if (configfile_load(SETTINGS_FILENAME, config,
|
|
|
|
sizeof(config)/sizeof(*config),
|
|
|
|
SETTINGS_MIN_VERSION ) < 0)
|
|
|
|
{
|
|
|
|
/* If the loading failed, save a new config file (as the disk is
|
|
|
|
already spinning) */
|
|
|
|
configfile_save(SETTINGS_FILENAME, config,
|
|
|
|
sizeof(config)/sizeof(*config),
|
|
|
|
SETTINGS_VERSION);
|
|
|
|
}
|
2002-10-29 10:45:29 +00:00
|
|
|
|
2006-03-13 07:05:15 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2006-05-22 06:56:39 +00:00
|
|
|
rb->lcd_set_foreground(COLOR_FG);
|
|
|
|
rb->lcd_set_background(COLOR_BG);
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
|
|
|
|
2006-11-15 06:46:35 +00:00
|
|
|
#if LCD_DEPTH > 1
|
2006-11-15 06:14:27 +00:00
|
|
|
rb->lcd_set_backdrop(NULL);
|
2006-11-15 06:46:35 +00:00
|
|
|
#endif
|
2006-11-15 06:14:27 +00:00
|
|
|
|
2002-09-09 23:30:16 +00:00
|
|
|
#ifdef DEBUG_WORMLET
|
2002-09-17 12:39:47 +00:00
|
|
|
testline_in_rect();
|
2002-09-09 23:30:16 +00:00
|
|
|
test_worm_argh_collision_in_moves();
|
|
|
|
test_make_argh();
|
|
|
|
test_worm_food_collision();
|
|
|
|
test_worm_argh_collision();
|
|
|
|
test_specific_worm_collision();
|
2006-03-13 03:13:05 +00:00
|
|
|
#endif
|
2002-09-09 23:30:16 +00:00
|
|
|
|
|
|
|
/* Setup screen */
|
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
static const struct opt_items noyes[2] = {
|
2006-08-11 12:48:36 +00:00
|
|
|
{ "No", -1 },
|
|
|
|
{ "Yes", -1 },
|
2006-05-22 06:56:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct opt_items remoteonly_option[1] = {
|
2006-08-11 12:48:36 +00:00
|
|
|
{ "Remote Control", -1 }
|
2006-05-22 06:56:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct opt_items key24_option[2] = {
|
2006-08-11 12:48:36 +00:00
|
|
|
{ "4 Key Control", -1 },
|
|
|
|
{ "2 Key Control", -1 }
|
2006-05-22 06:56:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef REMOTE
|
|
|
|
static const struct opt_items remote_option[2] = {
|
2006-08-11 12:48:36 +00:00
|
|
|
{ "Remote Control", -1 },
|
|
|
|
{ "No Rem. Control", -1 }
|
2006-05-22 06:56:39 +00:00
|
|
|
};
|
|
|
|
#else
|
|
|
|
static const struct opt_items key2_option[1] = {
|
2006-08-11 12:48:36 +00:00
|
|
|
{ "2 Key Control", -1 }
|
2006-05-22 06:56:39 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct opt_items nokey_option[1] = {
|
2006-08-11 12:48:36 +00:00
|
|
|
{ "Out of Control", -1 }
|
2006-05-22 06:56:39 +00:00
|
|
|
};
|
|
|
|
|
2007-04-03 20:10:40 +00:00
|
|
|
MENUITEM_STRINGLIST(menu, "Wormlet Menu", NULL, "Play Wormlet!",
|
|
|
|
"Number of Worms", "Number of Players", "Control Style",
|
|
|
|
"Worm Growth Per Food","Worm Speed","Arghs Per Food",
|
|
|
|
"Argh Size","Food Size","Revert to Default Settings",
|
2009-02-26 17:10:27 +00:00
|
|
|
"Playback Control", "Quit");
|
2006-05-22 06:56:39 +00:00
|
|
|
|
|
|
|
rb->button_clear_queue();
|
|
|
|
|
|
|
|
while (!menu_quit) {
|
2008-03-26 03:35:24 +00:00
|
|
|
switch(rb->do_menu(&menu, &result, NULL, false))
|
2006-05-22 06:56:39 +00:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
|
|
|
launch_wormlet();
|
|
|
|
break;
|
|
|
|
case 1:
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_int("Number of Worms", "", UNIT_INT, &worm_count, NULL,
|
|
|
|
1, 1, 3, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
if (worm_count < players) {
|
|
|
|
worm_count = players;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2006-03-13 03:13:05 +00:00
|
|
|
#ifdef MULTIPLAYER
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_int("Number of Players", "", UNIT_INT, &players, NULL,
|
|
|
|
1, 0, 4, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
#else
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_int("Number of Players", "", UNIT_INT, &players, NULL,
|
|
|
|
1, 0, 2, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
#endif
|
|
|
|
if (players > worm_count) {
|
|
|
|
worm_count = players;
|
|
|
|
}
|
2006-03-13 03:13:05 +00:00
|
|
|
if (players > 2) {
|
|
|
|
use_remote = true;
|
|
|
|
}
|
|
|
|
break;
|
2006-05-22 06:56:39 +00:00
|
|
|
case 3:
|
|
|
|
switch(players) {
|
|
|
|
case 0:
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_option("Control Style",&use_remote,INT,
|
|
|
|
nokey_option, 1, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_option("Control Style",&use_remote,INT,
|
|
|
|
key24_option, 2, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
#ifdef REMOTE
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_option("Control Style",&use_remote,INT,
|
|
|
|
remote_option, 2, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
#else
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_option("Control Style",&use_remote,INT,
|
|
|
|
key2_option, 1, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 3:
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_option("Control Style",&use_remote,INT,
|
|
|
|
remoteonly_option, 1, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
break;
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-05-22 06:56:39 +00:00
|
|
|
case 4:
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_int("Worm Growth Per Food", "", UNIT_INT, &worm_food,
|
|
|
|
NULL, 1, 0, 15, NULL);
|
2002-09-09 23:30:16 +00:00
|
|
|
break;
|
2006-05-22 06:56:39 +00:00
|
|
|
case 5:
|
2009-08-03 16:30:08 +00:00
|
|
|
new_setting = 20 - speed;
|
|
|
|
rb->set_int("Worm Speed", "", UNIT_INT, &new_setting,
|
|
|
|
NULL, 1, 0, 20, NULL);
|
|
|
|
speed = 20 - new_setting;
|
2002-09-09 23:30:16 +00:00
|
|
|
break;
|
2006-05-22 06:56:39 +00:00
|
|
|
case 6:
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_int("Arghs Per Food", "", UNIT_INT, &arghs_per_food,
|
|
|
|
NULL, 1, 0, 8, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
break;
|
|
|
|
case 7:
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_int("Argh Size", "", UNIT_INT, &argh_size,
|
|
|
|
NULL, 1, 2, 10, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_int("Food Size", "", UNIT_INT, &food_size,
|
|
|
|
NULL, 1, 2, 10, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
new_setting = 0;
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->set_option("Reset Settings?", &new_setting, INT, noyes , 2, NULL);
|
2006-05-22 06:56:39 +00:00
|
|
|
if (new_setting == 1)
|
|
|
|
default_settings();
|
2002-09-09 23:30:16 +00:00
|
|
|
break;
|
2009-02-26 17:10:27 +00:00
|
|
|
case 10:
|
|
|
|
playback_control(NULL);
|
|
|
|
break;
|
2004-10-23 00:05:42 +00:00
|
|
|
default:
|
2006-05-22 06:56:39 +00:00
|
|
|
menu_quit=1;
|
2004-10-23 00:05:42 +00:00
|
|
|
break;
|
2002-09-09 23:30:16 +00:00
|
|
|
}
|
2006-05-22 06:56:39 +00:00
|
|
|
}
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2006-05-22 06:56:39 +00:00
|
|
|
configfile_save(SETTINGS_FILENAME, config,
|
|
|
|
sizeof(config)/sizeof(*config),
|
|
|
|
SETTINGS_VERSION);
|
2002-08-26 03:32:39 +00:00
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
return PLUGIN_OK;
|
2002-08-26 03:32:39 +00:00
|
|
|
}
|