/*******************************************************************************
 Preliminary No Fear: Dangerous Sports (Williams, 1995) Pinball Simulator

 by Marton Larrosa (marton@mail.com)
 Dec. 24, 2000

 Read PZ.c or FH.c if you like more help.

 ******************************************************************************/

/*------------------------------------------------------------------------------
  Keys for the No Fear: Dangerous Sports Simulator:
  -------------------------------------------------
    +I  L/R Inlane
    +O  L/R Outlane
    +-  L/R Slingshot
     Q  SDTM (Drain Ball)
   WER  Jet Bumpers

   More to be added...

------------------------------------------------------------------------------*/

#include "driver.h"
#include "core.h"
#include "wpc.h"
#include "sim.h"
#include "wmssnd.h"

/*------------------
/  Local functions
/-------------------*/
static int  nf_handleBallState(sim_tBallStatus *ball, int *inports);
static void nf_drawStatic(BMTYPE **line);
static void init_nf(void);

/*-----------------------
  local static variables
 ------------------------*/
/* Uncomment if you wish to use locals. type variables */
//static struct {
//  int
//} locals;

/*--------------------------
/ Game specific input ports
/---------------------------*/
WPC_INPUT_PORTS_START(nf,4)

  PORT_START /* 0 */
    COREPORT_BIT(0x0001,"Left Qualifier",	KEYCODE_LCONTROL)
    COREPORT_BIT(0x0002,"Right Qualifier",	KEYCODE_RCONTROL)
    COREPORT_BIT(0x0004,"",		        KEYCODE_R)
    COREPORT_BIT(0x0008,"L/R Outlane",		KEYCODE_O)
    COREPORT_BIT(0x0010,"L/R Slingshot",		KEYCODE_MINUS)
    COREPORT_BIT(0x0020,"L/R Inlane",		KEYCODE_I)
    COREPORT_BIT(0x0040,"",			KEYCODE_W)
    COREPORT_BIT(0x0080,"",			KEYCODE_E)
    COREPORT_BIT(0x0100,"",			KEYCODE_R)
    COREPORT_BIT(0x0200,"",			KEYCODE_T)
    COREPORT_BIT(0x0400,"",			KEYCODE_Y)
    COREPORT_BIT(0x0800,"",			KEYCODE_U)
    COREPORT_BIT(0x1000,"",			KEYCODE_I)
    COREPORT_BIT(0x2000,"",			KEYCODE_O)
    COREPORT_BIT(0x4000,"",			KEYCODE_A)
    COREPORT_BIT(0x8000,"Drain",			KEYCODE_Q)

  PORT_START /* 1 */
    COREPORT_BIT(0x0001,"",			KEYCODE_S)
    COREPORT_BIT(0x0002,"",			KEYCODE_D)
    COREPORT_BIT(0x0004,"",			KEYCODE_F)
    COREPORT_BIT(0x0008,"",			KEYCODE_G)
    COREPORT_BIT(0x0010,"",			KEYCODE_H)
    COREPORT_BIT(0x0020,"",			KEYCODE_J)
    COREPORT_BIT(0x0040,"",			KEYCODE_K)
    COREPORT_BIT(0x0080,"",			KEYCODE_L)
    COREPORT_BIT(0x0100,"",			KEYCODE_Z)
    COREPORT_BIT(0x0200,"",			KEYCODE_X)
    COREPORT_BIT(0x0400,"",			KEYCODE_C)
    COREPORT_BIT(0x0800,"",			KEYCODE_V)
    COREPORT_BIT(0x1000,"",			KEYCODE_B)
    COREPORT_BIT(0x2000,"",			KEYCODE_N)
    COREPORT_BIT(0x4000,"",			KEYCODE_M)
    COREPORT_BIT(0x8000,"",			KEYCODE_COMMA)

WPC_INPUT_PORTS_END

/*-------------------
/ Switch definitions
/--------------------*/
/* Standard Switches */
#define swStart      	13
#define swTilt       	14
#define swSlamTilt	21
#define swCoinDoor	22
#define swTicket     	23

/* Other switches */
#define swLaunch	11
#define swShooter	15
#define swRightOutlane	17
#define swRightInlane	18
#define swLeftOutlane	25
#define swLeftInlane	26
#define swLeftSling	27
#define swRightSling	28
#define swTroughJam	31
#define swTrough1	32
#define swTrough2	33
#define swTrough3	34
#define swTrough4	35

/*---------------------
/ Solenoid definitions
/----------------------*/
#define sLaunch		2
#define sKickBack	4
#define sKnocker	7
#define sRightSling	10
#define sLeftSling	11
#define sTrough		14

/*---------------------
/  Ball state handling
/----------------------*/
enum {stTrough4=SIM_FIRSTSTATE, stTrough3, stTrough2, stTrough1, stTrough, stDrain,
      stShooter, stBallLane, stRightOutlane, stLeftOutlane, stRightInlane, stLeftInlane, stLeftSling, stRightSling
	  };

static sim_tState nf_stateDef[] = {
  {"Not Installed",	0,0,		 0,		stDrain,	0,	0,	0,	SIM_STNOTEXCL},
  {"Moving"},
  {"Playfield",		0,0,		 0,		0,		0,	0,	0,	SIM_STNOTEXCL},

  /*Line 1*/
  {"Trough 4",		1,swTrough4,	0,		stTrough3,	1},
  {"Trough 3",		1,swTrough3,	0,		stTrough2,	1},
  {"Trough 2",		1,swTrough2,	0,		stTrough1,	1},
  {"Trough 1",		1,swTrough1,	sTrough,	stTrough,	1},
  {"Trough Jam",	1,swTroughJam,  0,		stShooter,	1},
  {"Drain",		1,0,		0,		stTrough4,	0,	0,	0,	SIM_STNOTEXCL},

  /*Line 2*/
  {"Shooter",		1,swShooter,	 sLaunch,	stBallLane,	0,	0,	0,	SIM_STNOTEXCL|SIM_STSHOOT},
  {"Ball Lane",		1,0,		 0,		stFree,		7,	0,	0,	SIM_STNOTEXCL},
  {"Right Outlane",	1,swRightOutlane,0,		stDrain,	15},
  {"Left Outlane",      1,swLeftOutlane, 0,             stDrain,        15, sKickBack, stFree, 5},
  {"Right Inlane",	1,swRightInlane, 0,		stFree,		5},
  {"Left Inlane",	1,swLeftInlane,	 0,		stFree,		5},
  {"Left Slingshot",	1,swLeftSling,	 0,		stFree,		1},
  {"Rt Slingshot",	1,swRightSling,	 0,		stFree,		1},

  /*Line 3*/

  /*Line 4*/

  /*Line 5*/

  /*Line 6*/

  {0}
};

static int nf_handleBallState(sim_tBallStatus *ball, int *inports) {
  switch (ball->state)
	{
	}
    return 0;
  }

/*---------------------------
/  Keyboard conversion table
/----------------------------*/

static sim_tInportData nf_inportData[] = {

/* Port 0 */
//  {0, 0x0005, st},
//  {0, 0x0006, st},
  {0, 0x0009, stLeftOutlane},
  {0, 0x000a, stRightOutlane},
  {0, 0x0011, stLeftSling},
  {0, 0x0012, stRightSling},
  {0, 0x0021, stLeftInlane},
  {0, 0x0022, stRightInlane},
//  {0, 0x0040, st},
//  {0, 0x0080, st},
//  {0, 0x0100, st},
//  {0, 0x0200, st},
//  {0, 0x0400, st},
//  {0, 0x0800, st},
//  {0, 0x1000, st},
//  {0, 0x2000, st},
//  {0, 0x4000, st},
  {0, 0x8000, stDrain},

/* Port 1 */
//  {1, 0x0001, st},
//  {1, 0x0002, st},
//  {1, 0x0004, st},
//  {1, 0x0008, st},
//  {1, 0x0010, st},
//  {1, 0x0020, st},
//  {1, 0x0040, st},
//  {1, 0x0080, st},
//  {1, 0x0100, st},
//  {1, 0x0200, st},
//  {1, 0x0400, st},
//  {1, 0x0800, st},
//  {1, 0x1000, st},
//  {1, 0x2000, st},
//  {1, 0x4000, st},
//  {1, 0x8000, st},
  {0}
};

/*--------------------
  Drawing information
  --------------------*/
  static void nf_drawStatic(BMTYPE **line) {

/* Help */

  core_textOutf(30, 60,BLACK,"Help on this Simulator:");
  core_textOutf(30, 70,BLACK,"L/R Ctrl+- = L/R Slingshot");
  core_textOutf(30, 80,BLACK,"L/R Ctrl+I/O = L/R Inlane/Outlane");
  core_textOutf(30, 90,BLACK,"Q = Drain Ball");
  core_textOutf(30,100,BLACK,"");
  core_textOutf(30,110,BLACK,"");
  core_textOutf(30,120,BLACK,"");
  core_textOutf(30,130,BLACK,"      *** PRELIMINARY ***");
  core_textOutf(30,140,BLACK,"");
  core_textOutf(30,150,BLACK,"");
  core_textOutf(30,160,BLACK,"");
  }

/*-----------------
/  ROM definitions
/------------------*/
WPC_ROMSTART(nf,23x,"nofe2_3x.rom",0x80000,CRC(d853650b) SHA1(06d58f86c68ccdc242d6b96a22c6226758dc3e44))
DCS_SOUNDROM6x("nfu2s",CRC(136caeb9) SHA1(61a56b29a7655e8aab4987d300173e1acf27c77c),
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

WPC_ROMSTART(nf,23,"nofe2_3.rom",0x80000,CRC(47746cbc) SHA1(61606da50894c2f01c64dd5c3aef72cb17a0bc31))
DCS_SOUNDROM6x("nfu2s",CRC(136caeb9) SHA1(61a56b29a7655e8aab4987d300173e1acf27c77c),
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

WPC_ROMSTART(nf,23f,"nofe2_3f.rom",0x80000,CRC(996e5e75) SHA1(ca3d2c13388e674b26cebf7f9ae65c8a722b68b1))
DCS_SOUNDROM6x("nfu2s",CRC(136caeb9) SHA1(61a56b29a7655e8aab4987d300173e1acf27c77c),
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

WPC_ROMSTART(nf,22,"nofe2_2a.rom",0x80000,CRC(8694b32e) SHA1(fbdc45910ef5e34c90557491831854de3b4889a8))
DCS_SOUNDROM6x("nfu2s",CRC(136caeb9) SHA1(61a56b29a7655e8aab4987d300173e1acf27c77c),
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

WPC_ROMSTART(nf,20,"nofe2_0a.rom",0x80000,CRC(dceee809) SHA1(44aaeeb268d67eb48087bd8958f864d4c4ee5138))
DCS_SOUNDROM6x("nfu2s",CRC(136caeb9) SHA1(61a56b29a7655e8aab4987d300173e1acf27c77c),
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

WPC_ROMSTART(nf,11x,"nofe1_1x.rom",0x80000,CRC(1541978b) SHA1(ed8e32453001c3e7e95e786c62967aff5f75d81e))
DCS_SOUNDROM6x("nfu2s",CRC(136caeb9) SHA1(61a56b29a7655e8aab4987d300173e1acf27c77c),
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

WPC_ROMSTART(nf,10,"nofe1_0.rom",0x80000,CRC(f8f6521c) SHA1(5c26f4878f257b157c2a1c46995ec8100fa20723))
DCS_SOUNDROM6x("nfu2s",CRC(136caeb9) SHA1(61a56b29a7655e8aab4987d300173e1acf27c77c),
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

WPC_ROMSTART(nf,101,"nofe1_01.rom",0x80000,CRC(4eeb8add) SHA1(c482ae6f7229696bc0cbfd50638dadc8404a98c8))
DCS_SOUNDROM6x("nfu2s",CRC(136caeb9) SHA1(61a56b29a7655e8aab4987d300173e1acf27c77c),
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

WPC_ROMSTART(nf,08x,"nofe0_8x.rom",0x80000,CRC(64871e6a) SHA1(0e116104b06446b0d435f715c33535080cdd2378))
DCS_SOUNDROM6x("SND-U2.SL1",CRC(84f48e27) SHA1(cdf0ff55c1493ea5ac7cef618c985f41442c6f60), // this was found separate from nofe0_8x.rom, so we just add it to the oldest revision we have
               "nfu3s",CRC(983e5578) SHA1(374b1397abbdde5fd9257fd45fd8613c94fbd02d),
               "nfu4s",CRC(9469cd40) SHA1(8a1dd1088f24018f48b114c0b27f0331263d4eea),
               "nfu5s",CRC(e14d4315) SHA1(63d5ae800cc8a750ea2e3a87c646ab175b60abc7),
               "nfu6s",CRC(40a58903) SHA1(78f7e99f39efc83f3cf17801a30e6dc6e4864125),
               "nfu7s",CRC(61002bdd) SHA1(e623399ff95f59a4ab7efdd7c69b1a1370479398))
WPC_ROMEND

/*--------------
/  Game drivers
/---------------*/
CORE_GAMEDEF(nf,23x,"No Fear: Dangerous Sports (2.3X)",1995,"Williams",wpc_mSecurityS,0)
CORE_CLONEDEF(nf,23,23x,"No Fear: Dangerous Sports (2.3)",1995,"Williams",wpc_mSecurityS,0)
CORE_CLONEDEF(nf,23f,23x,"No Fear: Dangerous Sports (2.3F French)",1995,"Williams",wpc_mSecurityS,0)
CORE_CLONEDEF(nf,22,23x,"No Fear: Dangerous Sports (2.2)",1995,"Williams",wpc_mSecurityS,0)
CORE_CLONEDEF(nf,20,23x,"No Fear: Dangerous Sports (2.0)",1995,"Williams",wpc_mSecurityS,0)
CORE_CLONEDEF(nf,11x,23x,"No Fear: Dangerous Sports (1.1X)",1995,"Williams",wpc_mSecurityS,0)
CORE_CLONEDEF(nf,10,23x,"No Fear: Dangerous Sports (1.0)",1995,"Williams",wpc_mSecurityS,0)
CORE_CLONEDEF(nf,101,23x,"No Fear: Dangerous Sports (1.01 LED Ghost Fix)",1995,"Williams",wpc_mSecurityS,0)
CORE_CLONEDEF(nf,08x,23x,"No Fear: Dangerous Sports (0.8X, SL-1)",1995,"Williams",wpc_mSecurityS,0)

/*-----------------------
/ Simulation Definitions
/-----------------------*/
static sim_tSimData nfSimData = {
  2,    				/* 2 game specific input ports */
  nf_stateDef,				/* Definition of all states */
  nf_inportData,			/* Keyboard Entries */
  { stTrough1, stTrough2, stTrough3, stTrough4, stDrain, stDrain, stDrain },	/*Position where balls start.. Max 7 Balls Allowed*/
  NULL, 				/* no init */
  nf_handleBallState,			/*Function to handle ball state changes*/
  nf_drawStatic,			/*Function to handle mechanical state changes*/
  FALSE, 				/* Simulate manual shooter? */
  NULL  				/* Custom key conditions? */
};

/*----------------------
/ Game Data Information
/----------------------*/
static core_tGameData nfGameData = {
  GEN_WPCSECURITY, wpc_dispDMD,
  {
    FLIP_SW(FLIP_L | FLIP_U) | FLIP_SOL(FLIP_L | FLIP_UR)
  },
  &nfSimData,
  {
    "525 123456 12345 123",
    /*Coin    1     2     3     4     5     6     7     8     9    10   Cab.  Cust */
    { 0x00, 0x00, 0x00, 0xff, 0xe3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* inverted switches */
    /*Start    Tilt    SlamTilt    CoinDoor    Shooter */
    { swStart, swTilt, swSlamTilt, swCoinDoor, swLaunch},
  }
};

/*---------------
/  Game handling
/----------------*/
static void init_nf(void) {
  core_gameData = &nfGameData;
}

