NETWORK.C 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include "c:\netnow\hmistd.h"
  8. #include "c:\netnow\netnow.h"
  9. #include "c:\netnow\vdata.h"
  10. #include "c:\netnow\vchat.h"
  11. #include "c:\sos\include\sosm.h"
  12. #include "network.h"
  13. #include "defines.h"
  14. #include "mouse.h"
  15. //#include <iostream.h>
  16. //#include <dos.h>
  17. unsigned char machine_no;
  18. // Player vars
  19. volatile int Team_A,Team_B;
  20. volatile int Teams[MAX_NET_USERS],Players[MAX_NET_USERS];
  21. int TeamChoice,PlayerChoice;
  22. // General vars
  23. char old_mf;
  24. int netsetup_on; // In network setup
  25. int network_on; // Network on flag / No of network players
  26. int onetwork_on;
  27. short wConsoleNode; // Network ID for machine
  28. short master; // Network ID for master
  29. volatile int active_nodes;
  30. volatile int net_quit;
  31. int num_of_users=0;
  32. // Packet types
  33. packet_header in_header;
  34. packet_header p_header;
  35. packet_data p_data;
  36. packet_seed test_seed;
  37. packet_single test_single;
  38. packet_multiple test_multiple;
  39. //volatile packet_multiple copy_multiple[512];
  40. volatile int writeptr;
  41. volatile int readptr;
  42. volatile packet_single user_inp; // Temp input data
  43. // Internal network vars
  44. _NETNOW_NODE_ADDR address[MAX_NET_USERS]; // Station addresses that we know about.
  45. _NETNOW_NODE_ADDR laddress; // Local machines address
  46. volatile int net_players[16];
  47. volatile int start_multiple; // No of players master has sent to
  48. volatile int received_seed; //
  49. volatile int frame_number;
  50. volatile int slave_pause=FALSE;
  51. volatile int NetDone;
  52. // Timer vars
  53. volatile int count;
  54. volatile int tick;
  55. volatile int ftick;
  56. int game_speed;
  57. // Richards
  58. volatile int hold_loop;
  59. // User input arrays
  60. struct user_config{
  61. short type;
  62. short control;
  63. int inp_x;
  64. int inp_y;
  65. int inp_ox;
  66. int inp_oy;
  67. short inp_fb;
  68. char chng;
  69. signed char plr;
  70. };
  71. volatile struct user_config users[MAX_NET_USERS];
  72. struct user_vectors_i{
  73. short x_f1;
  74. short y_f2;
  75. };
  76. volatile struct user_vectors_i users_dir_i[512][MAX_NET_USERS];
  77. int icth,isth; // viewing angle in integers
  78. // Vars taken from Andy's code (mostly input stuff)
  79. volatile char paused;
  80. volatile char pause_request;
  81. volatile char keys[256];
  82. volatile char key_togs[256];
  83. signed char key1[6];
  84. signed char key2[6];
  85. unsigned short joy1_cnt_x,joy1_cnt_y;
  86. unsigned short joy2_cnt_x,joy2_cnt_y;
  87. int joy1_max_x,joy1_max_y,joy1_min_x,joy1_min_y;
  88. int joy2_max_x,joy2_max_y,joy2_min_x,joy2_min_y;
  89. volatile int joy1_x=0;
  90. volatile int joy1_y=0;
  91. volatile int joy1_fire=0;
  92. volatile int joy2_x=0;
  93. volatile int joy2_y=0;
  94. volatile int joy2_fire=0;
  95. volatile char joy1_move=0;
  96. volatile char joy2_move=0;
  97. char joystick1_on=FALSE;
  98. char joystick2_on=FALSE;
  99. char digital_joy1=FALSE;
  100. char digital_joy2=FALSE;
  101. struct joy_struct {
  102. int FireA1,FireA2,JoyAX,JoyAY;
  103. int FireB1,FireB2,JoyBX,JoyBY;
  104. };
  105. /*
  106. struct rjoy_struct {
  107. unsigned short x1;
  108. unsigned short y1;
  109. unsigned short x2;
  110. unsigned short y2;
  111. unsigned short sw;
  112. };
  113. volatile struct rjoy_struct rjoy_pos;
  114. */
  115. volatile struct joy_struct joy_pos;
  116. unsigned short dead_zone=25; // percentage
  117. volatile int ux1,ux2,wptr,rptr;
  118. /********************/
  119. /* INPUT ROUTINES */
  120. /********************/
  121. /////////////////
  122. // JOYSTICKS //
  123. //Function to gather joystick data and return in a joystick structure
  124. int x1ok=1; // These values indicate whether the axes are connected or not...
  125. int y1ok=2; // for calibration they should be initialised.
  126. int x2ok=4;
  127. int y2ok=8;
  128. int bitaccept=15; // Above values OR'ed
  129. #define JOY_TIMEOUT 65535
  130. extern void __far JoyRead3(void *);
  131. extern void __far JoyRead2(void *);
  132. extern void __far JoyRead1(void *);
  133. /******************************************************************************
  134. ******************************************************************************/
  135. /*
  136. void ConvJoys()
  137. {
  138. joy_pos.FireA1=!((rjoy_pos.sw>>4)&1);
  139. joy_pos.FireA2=!((rjoy_pos.sw>>5)&1);
  140. joy_pos.FireB1=!((rjoy_pos.sw>>6)&1);
  141. joy_pos.FireB2=!((rjoy_pos.sw>>7)&1);
  142. joy_pos.JoyAX=rjoy_pos.x1;
  143. joy_pos.JoyAY=rjoy_pos.y1;
  144. joy_pos.JoyBX=rjoy_pos.x2;
  145. joy_pos.JoyBY=rjoy_pos.y2;
  146. }
  147. */
  148. /******************************************************************************
  149. ******************************************************************************/
  150. /*
  151. void ReadJoys()
  152. {
  153. if (joystick1_on && joystick2_on)
  154. // Two joysticks plugged in...
  155. {
  156. JoyRead3(&rjoy_pos);
  157. ConvJoys();
  158. }
  159. else
  160. {
  161. if (joystick1_on)
  162. // Port 1 joystick only...
  163. {
  164. JoyRead1(&rjoy_pos);
  165. ConvJoys();
  166. }
  167. else
  168. // Port 2 joystick only...
  169. {
  170. if (joystick2_on)
  171. {
  172. JoyRead2(&rjoy_pos);
  173. ConvJoys();
  174. }
  175. }
  176. }
  177. }
  178. */
  179. void ReadJoys(struct joy_struct *JoyData)
  180. {
  181. //read joysticks for calibration - always returns!
  182. int XJA=-1,YJA=-1,XJB=-1,YJB=-1,Fire,FireA,FireB,j;
  183. int timecount=0;
  184. int x1read,y1read,x2read,y2read;
  185. Fire=((int)inp(0x201) ^ 255)>>4;
  186. FireA=Fire & 3;
  187. FireB=(Fire>>2) & 3;
  188. JoyData->FireA1=FireA & 1;
  189. JoyData->FireA2=FireA>>1;
  190. JoyData->FireB1=FireB & 1;
  191. JoyData->FireB2=FireB>>1;
  192. XJA=0;
  193. YJA=0;
  194. XJB=0;
  195. YJB=0;
  196. _disable();
  197. outp(0x201,255); //Request a reading from the joystick port
  198. x1read=x1ok;
  199. y1read=y1ok;
  200. x2read=x2ok;
  201. y2read=y2ok;
  202. do
  203. {
  204. j=(int)inp(0x201); //read the current port status
  205. if (x1ok)
  206. if (x1read=(j & 1)) XJA++; //increase X counter
  207. if (y1ok)
  208. if (y1read=(j & 2)) YJA++; //increase Y counter
  209. if (x2ok)
  210. if (x2read=(j & 4)) XJB++; //increase X counter
  211. if (y2ok)
  212. if (y2read=(j & 8)) YJB++; //increase Y counter
  213. timecount++;
  214. }
  215. while((j & bitaccept) && timecount<JOY_TIMEOUT); //this will cause time out during calibration if not connected!
  216. _enable();
  217. if (!x1read)
  218. JoyData->JoyAX=XJA;
  219. else
  220. {
  221. JoyData->JoyAX=0;
  222. x1ok=0; //timed out - stop reading!
  223. bitaccept=x1ok | y1ok | x2ok | y2ok;
  224. }
  225. if (!y1read)
  226. JoyData->JoyAY=YJA;
  227. else
  228. {
  229. JoyData->JoyAY=0;
  230. y1ok=0;
  231. bitaccept=x1ok | y1ok | x2ok | y2ok;
  232. }
  233. if (!x2read)
  234. JoyData->JoyBX=XJB;
  235. else
  236. {
  237. JoyData->JoyBX=0;
  238. x2ok=0;
  239. bitaccept=x1ok | y1ok | x2ok | y2ok;
  240. }
  241. if (!y2read)
  242. JoyData->JoyBY=YJB;
  243. else
  244. {
  245. JoyData->JoyBY=0;
  246. y2ok=0;
  247. bitaccept=x1ok | y1ok | x2ok | y2ok;
  248. }
  249. }
  250. void read_joy1(unsigned short *x, unsigned short *y, unsigned char *sw)
  251. {
  252. *x=joy_pos.JoyAX;
  253. *y=joy_pos.JoyAY;
  254. *sw=joy_pos.FireA1|(joy_pos.FireA2<<1);
  255. }
  256. void read_joy2(unsigned short *x, unsigned short *y, unsigned char *sw)
  257. {
  258. *x=joy_pos.JoyBX;
  259. *y=joy_pos.JoyBY;
  260. *sw=joy_pos.FireB1|(joy_pos.FireB2<<1);
  261. }
  262. void read_joystick(char j, unsigned short *x, unsigned short *y, unsigned char *sw)
  263. {
  264. ReadJoys(&joy_pos);
  265. if (!j)
  266. read_joy1(x,y,sw);
  267. else
  268. read_joy2(x,y,sw);
  269. }
  270. void get_joy_max_min(char j)
  271. {
  272. int i;
  273. unsigned short x,y;
  274. unsigned char butt;
  275. unsigned short max_x=0;
  276. unsigned short min_x=65535;
  277. unsigned short max_y=0;
  278. unsigned short min_y=65535;
  279. do
  280. {
  281. read_joystick(j,&x,&y,&butt);
  282. if (x>max_x)
  283. max_x=x;
  284. if (x<min_x)
  285. min_x=x;
  286. if (y>max_y)
  287. max_y=y;
  288. if (y<min_y)
  289. min_y=y;
  290. // STRANGE???? Need this software loop to get normal range values from joystick!
  291. // Shrug....
  292. for (i=0; i<200000; i++)
  293. {
  294. i+=1;
  295. i-=1;
  296. }
  297. }while(!(butt&3));
  298. if (!j)
  299. {
  300. joy1_max_x=max_x;
  301. joy1_max_y=max_y;
  302. joy1_min_x=min_x;
  303. joy1_min_y=min_y;
  304. }
  305. else
  306. {
  307. joy2_max_x=max_x;
  308. joy2_max_y=max_y;
  309. joy2_min_x=min_x;
  310. joy2_min_y=min_y;
  311. }
  312. }
  313. void calibrate_joy(char j,unsigned short *x,unsigned short *y)
  314. {
  315. unsigned short left,right,up,down;
  316. unsigned char butt;
  317. unsigned short max_x,min_x,max_y,min_y;
  318. float dead_zone=0.6; // 33% dead_zone!
  319. char str[40];
  320. printf("\nPlease circle joystick %d now...\n",j+1);
  321. get_joy_max_min(j);
  322. if (!j)
  323. {
  324. left=joy1_min_x;
  325. right=joy1_max_x;
  326. up=joy1_min_y;
  327. down=joy1_max_y;
  328. }
  329. else
  330. {
  331. left=joy2_min_x;
  332. right=joy2_max_x;
  333. up=joy2_min_y;
  334. down=joy2_max_y;
  335. }
  336. printf("Left %d\n",left);
  337. printf("Right %d\n",right);
  338. printf("Up %d\n",up);
  339. printf("Down %d\n",down);
  340. printf("\nNow centre joystick %d and press fire...\n",j+1);
  341. if (!j)
  342. {
  343. do
  344. {
  345. read_joystick(0,x,y,&butt);
  346. }while(butt&3);
  347. do
  348. {
  349. read_joystick(0,x,y,&butt);
  350. }while(!(butt&3));
  351. }
  352. else
  353. {
  354. do
  355. {
  356. read_joystick(1,x,y,&butt);
  357. }while(butt&3);
  358. do
  359. {
  360. read_joystick(1,x,y,&butt);
  361. }while(!(butt&3));
  362. }
  363. }
  364. int sqrt_i(int a)
  365. {
  366. int x,d,b;
  367. int bt=0x80000000;
  368. if (a <= 0) return (0);
  369. for (b=32; b>0; b--)
  370. {
  371. if (a&bt)
  372. break;
  373. else
  374. bt>>=1;
  375. }
  376. x=1<<(b>>1);
  377. while (d = ((((x * x - a) / x) + 1) >> 1))
  378. x-=d;
  379. return (x);
  380. }
  381. void normalise_joy_i(volatile int *x,volatile int *y)
  382. {
  383. int d;
  384. d=(((*x)*(*x))+((*y)*(*y)));
  385. d<<=12;
  386. if (d>0)
  387. {
  388. d=sqrt_i(d);
  389. *x<<=21;
  390. *x/=d;
  391. *y<<=21;
  392. *y/=d;
  393. }
  394. else
  395. {
  396. *x=*y=0;
  397. }
  398. }
  399. int calc_dist_i(int x,int y)
  400. {
  401. return (sqrt_i(x*x+y*y));
  402. }
  403. void get_joy_dir1(volatile int *x,volatile int *y,volatile int *f)
  404. {
  405. unsigned short rx,ry;
  406. unsigned char sw;
  407. int d;
  408. *x=*y=0;
  409. read_joy1(&rx,&ry,&sw);
  410. *x=rx-joy1_cnt_x;
  411. *y=ry-joy1_cnt_y;
  412. ux1=*x;
  413. ux2=*y;
  414. d=calc_dist_i((joy1_max_x-joy1_min_x),(joy1_max_y-joy1_min_y))*dead_zone/200;
  415. if (calc_dist_i(*x,*y)<d)
  416. {
  417. joy1_move=FALSE;
  418. *x=*y=0;
  419. }
  420. else
  421. {
  422. joy1_move=TRUE;
  423. normalise_joy_i(x,y);
  424. }
  425. if (digital_joy1)
  426. {
  427. if (!joy1_move)
  428. {
  429. *x=*y=0;
  430. }
  431. else
  432. {
  433. int d=0;
  434. if (*y>=0)
  435. {
  436. if (*x>=0)
  437. {
  438. if (*x>*y)
  439. {
  440. if (*x>((*y)*2))
  441. {
  442. *x=TUNIT;
  443. *y=0;
  444. }
  445. else
  446. {
  447. *x=TDIAG;
  448. *y=TDIAG;
  449. }
  450. }
  451. else
  452. {
  453. if (*y>((*x)*2))
  454. {
  455. *x=0;
  456. *y=TUNIT;
  457. }
  458. else
  459. {
  460. *x=TDIAG;
  461. *y=TDIAG;
  462. }
  463. }
  464. }
  465. else
  466. {
  467. if (-*x>*y)
  468. {
  469. if (-*x>((*y)*2))
  470. {
  471. *x=-TUNIT;
  472. *y=0;
  473. }
  474. else
  475. {
  476. *x=-TDIAG;
  477. *y=TDIAG;
  478. }
  479. }
  480. else
  481. {
  482. if (*y>(-(*x)*2))
  483. {
  484. *x=0;
  485. *y=TUNIT;
  486. }
  487. else
  488. {
  489. *x=-TDIAG;
  490. *y=TDIAG;
  491. }
  492. }
  493. }
  494. }
  495. else
  496. {
  497. if (*x>=0)
  498. {
  499. if (*x>-*y)
  500. {
  501. if (x>(-(*y)*2))
  502. {
  503. *x=TUNIT;
  504. *y=0;
  505. }
  506. else
  507. {
  508. *x=TDIAG;
  509. *y=-TDIAG;
  510. }
  511. }
  512. else
  513. {
  514. if (-*y>((*x)*2))
  515. {
  516. *x=0;
  517. *y=-TUNIT;
  518. }
  519. else
  520. {
  521. *x=TDIAG;
  522. *y=-TDIAG;
  523. }
  524. }
  525. }
  526. else
  527. {
  528. if (-*x>-*y)
  529. {
  530. if (-*x>(-*y*2))
  531. {
  532. *x=TUNIT;
  533. *y=0;
  534. }
  535. else
  536. {
  537. *x=-TDIAG;
  538. *y=-TDIAG;
  539. }
  540. }
  541. else
  542. {
  543. if (-*y>(-(*x)*2))
  544. {
  545. *x=0;
  546. *y=-TUNIT;
  547. }
  548. else
  549. {
  550. *x=-TDIAG;
  551. *y=-TDIAG;
  552. }
  553. }
  554. }
  555. }
  556. }
  557. }
  558. *f=sw;
  559. }
  560. void get_joy_dir2(volatile int *x,volatile int *y,volatile int *f)
  561. {
  562. unsigned short rx,ry;
  563. unsigned char sw;
  564. int d;
  565. *x=*y=0;
  566. read_joy2(&rx,&ry,&sw);
  567. *x=rx-joy2_cnt_x;
  568. *y=ry-joy2_cnt_y;
  569. d=calc_dist_i((joy2_max_x-joy2_min_x),(joy2_max_y-joy2_min_y))*dead_zone/200;
  570. if (calc_dist_i(*x,*y)<d)
  571. {
  572. joy2_move=FALSE;
  573. *x=*y=0;
  574. }
  575. else
  576. {
  577. joy2_move=TRUE;
  578. normalise_joy_i(x,y);
  579. }
  580. if (digital_joy2)
  581. {
  582. if (!joy2_move)
  583. {
  584. *x=*y=0;
  585. }
  586. else
  587. {
  588. int d=0;
  589. if (*y>=0)
  590. {
  591. if (*x>=0)
  592. {
  593. if (*x>*y)
  594. {
  595. if (*x>((*y)*2))
  596. {
  597. *x=TUNIT;
  598. *y=0;
  599. }
  600. else
  601. {
  602. *x=TDIAG;
  603. *y=TDIAG;
  604. }
  605. }
  606. else
  607. {
  608. if (*y>((*x)*2))
  609. {
  610. *x=0;
  611. *y=TUNIT;
  612. }
  613. else
  614. {
  615. *x=TDIAG;
  616. *y=TDIAG;
  617. }
  618. }
  619. }
  620. else
  621. {
  622. if (-*x>*y)
  623. {
  624. if (-*x>((*y)*2))
  625. {
  626. *x=-TUNIT;
  627. *y=0;
  628. }
  629. else
  630. {
  631. *x=-TDIAG;
  632. *y=TDIAG;
  633. }
  634. }
  635. else
  636. {
  637. if (*y>(-(*x)*2))
  638. {
  639. *x=0;
  640. *y=TUNIT;
  641. }
  642. else
  643. {
  644. *x=-TDIAG;
  645. *y=TDIAG;
  646. }
  647. }
  648. }
  649. }
  650. else
  651. {
  652. if (*x>=0)
  653. {
  654. if (*x>-*y)
  655. {
  656. if (x>(-(*y)*2))
  657. {
  658. *x=TUNIT;
  659. *y=0;
  660. }
  661. else
  662. {
  663. *x=TDIAG;
  664. *y=-TDIAG;
  665. }
  666. }
  667. else
  668. {
  669. if (-*y>((*x)*2))
  670. {
  671. *x=0;
  672. *y=-TUNIT;
  673. }
  674. else
  675. {
  676. *x=TDIAG;
  677. *y=-TDIAG;
  678. }
  679. }
  680. }
  681. else
  682. {
  683. if (-*x>-*y)
  684. {
  685. if (-*x>(-*y*2))
  686. {
  687. *x=TUNIT;
  688. *y=0;
  689. }
  690. else
  691. {
  692. *x=-TDIAG;
  693. *y=-TDIAG;
  694. }
  695. }
  696. else
  697. {
  698. if (-*y>(-(*x)*2))
  699. {
  700. *x=0;
  701. *y=-TUNIT;
  702. }
  703. else
  704. {
  705. *x=-TDIAG;
  706. *y=-TDIAG;
  707. }
  708. }
  709. }
  710. }
  711. }
  712. }
  713. *f=sw;
  714. }
  715. /////////////
  716. // MOUSE //
  717. void get_mouse_dir(volatile struct user_config *user)
  718. {
  719. int x,y;
  720. ReportMouse();
  721. x=Mouse.x-160;
  722. y=Mouse.y-100;
  723. x<<=15;
  724. x/=160;
  725. y<<=15;
  726. y/=100;
  727. user->inp_fb=Mouse.b;
  728. user->inp_x=x;
  729. user->inp_y=y;
  730. MouseXYset(160,100); // reset mouse origin.
  731. }
  732. ////////////////
  733. // KEYBOARD //
  734. short get_key_code(signed char *ks)
  735. {
  736. int i;
  737. short k=0;
  738. signed short ki;
  739. for (i=0; i<6; i++)
  740. {
  741. ki=*ks++;
  742. if (ki<0)
  743. {
  744. ki=SPECIALK-ki;
  745. if (keys[ki])
  746. k+=(1<<i);
  747. }
  748. else
  749. if (keys[ki])
  750. k+=(1<<i);
  751. }
  752. return(k);
  753. }
  754. void get_key_dir(volatile struct user_config *user,signed char *kb)
  755. {
  756. short k;
  757. k=get_key_code(kb);
  758. user->inp_fb=FALSE;
  759. if (k>=32) // Fire 2
  760. {
  761. user->inp_fb=2;
  762. k-=32;
  763. }
  764. if (k>=16) // Fire 1
  765. {
  766. user->inp_fb+=1;
  767. k-=16;
  768. }
  769. switch(k)
  770. {
  771. case(0):
  772. user->inp_x=0;
  773. user->inp_y=0;
  774. break;
  775. case(1):
  776. user->inp_x=0;
  777. user->inp_y=-32768;
  778. break;
  779. case(2):
  780. user->inp_x=0;
  781. user->inp_y=32767;
  782. break;
  783. case(4):
  784. user->inp_x=-32768;
  785. user->inp_y=0;
  786. break;
  787. case(5):
  788. user->inp_x=-TDIAG;
  789. user->inp_y=-TDIAG;
  790. break;
  791. case(6):
  792. user->inp_x=-TDIAG;
  793. user->inp_y=TDIAG;
  794. break;
  795. case(7):
  796. user->inp_x=-32768;
  797. user->inp_y=0;
  798. break;
  799. case(8):
  800. user->inp_x=32767;
  801. user->inp_y=0;
  802. break;
  803. case(9):
  804. user->inp_x=TDIAG;
  805. user->inp_y=-TDIAG;
  806. break;
  807. case(10):
  808. user->inp_x=TDIAG;
  809. user->inp_y=TDIAG;
  810. break;
  811. case(11):
  812. user->inp_x=32767;
  813. user->inp_y=0;
  814. break;
  815. case(13):
  816. user->inp_x=0;
  817. user->inp_y=-32768;
  818. break;
  819. case(14):
  820. user->inp_x=0;
  821. user->inp_y=32767;
  822. break;
  823. default:
  824. user->inp_x=0;
  825. user->inp_y=0;
  826. break;
  827. }
  828. }
  829. ///////////////
  830. // GENERAL //
  831. void get_user_inputs()
  832. {
  833. int i;
  834. ReadJoys(&joy_pos);
  835. for (i=0; i<num_of_users; i++)
  836. {
  837. switch(users[i].control)
  838. {
  839. case(JOY1_CTRL):
  840. get_joy_dir1(&joy1_x,&joy1_y,&joy1_fire);
  841. users[i].inp_x=joy1_x;
  842. users[i].inp_y=joy1_y;
  843. users[i].inp_fb=joy1_fire;
  844. break;
  845. case(JOY2_CTRL):
  846. get_joy_dir2(&joy2_x,&joy2_y,&joy2_fire);
  847. users[i].inp_x=joy2_x;
  848. users[i].inp_y=joy2_y;
  849. users[i].inp_fb=joy2_fire;
  850. break;
  851. case(KEY1_CTRL):
  852. get_key_dir(&users[i],key1);
  853. break;
  854. case(KEY2_CTRL):
  855. get_key_dir(&users[i],key2);
  856. break;
  857. case(MOUSE_CTRL):
  858. get_mouse_dir(&users[i]);
  859. break;
  860. }
  861. }
  862. }
  863. void process_user_inputs()
  864. {
  865. int x,y,i,a,b;
  866. for (i=0; i<num_of_users; i++)
  867. {
  868. if (users[i].type && users[i].control!=NET_CTRL) // Not computer or net guy
  869. {
  870. x=users[i].inp_x;
  871. y=users[i].inp_y;
  872. a=x*icth;
  873. b=y*isth;
  874. a=(a-b)>>15;
  875. if (a<-32767)
  876. a=-32767;
  877. if (a>32767)
  878. a=32767;
  879. users_dir_i[writeptr][i].x_f1=a;
  880. a=y*icth;
  881. b=x*isth;
  882. a=(a+b)>>15;
  883. if (a<-32767)
  884. a=-32767;
  885. if (a>32767)
  886. a=32767;
  887. users_dir_i[writeptr][i].y_f2=a;
  888. users_dir_i[writeptr][i].x_f1&=0xfffe;
  889. users_dir_i[writeptr][i].x_f1|=(users[i].inp_fb&1);
  890. users_dir_i[writeptr][i].y_f2&=0xfffe;
  891. users_dir_i[writeptr][i].y_f2|=((users[i].inp_fb&2)>>1);
  892. users[i].inp_ox=x;
  893. users[i].inp_oy=y;
  894. }
  895. }
  896. }
  897. /**************************/
  898. /* INTERRUPT TIMER CODE */
  899. /**************************/
  900. unsigned int NetworkHandle; // Handle for this timer routine
  901. void __far nethandler(void)
  902. {
  903. int i;
  904. count++;
  905. ftick+=game_speed;
  906. if (ftick>=TIMER_SPEED)
  907. {
  908. do
  909. ftick-=TIMER_SPEED;
  910. while (ftick>TIMER_SPEED);
  911. tick++;
  912. hold_loop=1; //ACTIVE
  913. if (!old_mf)
  914. {
  915. if (netsetup_on) // If in network setup...
  916. {
  917. if (network_on)
  918. NetInitialisation(); // Scan for new nodes and player data
  919. }
  920. else
  921. {
  922. if (network_on) // If a network game...
  923. {
  924. if (wConsoleNode==master) // If master...
  925. {
  926. if ( start_multiple==network_on-1 ) // If all data sent last frame
  927. {
  928. for (i=0;i<MAX_NET_USERS;i++)
  929. {
  930. users_dir_i[writeptr][i].x_f1=users_dir_i[(writeptr-1)&511][i].x_f1; // Copy all last input data for defaults
  931. users_dir_i[writeptr][i].y_f2=users_dir_i[(writeptr-1)&511][i].y_f2; // ...
  932. }
  933. get_user_inputs(); // Read own input data..
  934. process_user_inputs();
  935. if (!(tick&7)) putch('.');
  936. receive_all_singles(); // Receive input data from all other machines
  937. }
  938. if (active_nodes==network_on) // Only start transmitting when ready
  939. {
  940. if (start_multiple!=network_on-1 || !paused) // Stop sending if paused, and block fully transmitted
  941. {
  942. send_multiple();
  943. if ( start_multiple==network_on-1 ) // If block fully transmitted, move to next
  944. {
  945. writeptr++;
  946. writeptr&=511;
  947. }
  948. }
  949. }
  950. }
  951. else // If slave...
  952. {
  953. get_user_inputs(); // Read own input data
  954. process_user_inputs();
  955. for (i=0; i<network_on; i++)
  956. if (users[i].type && users[i].control!=NET_CTRL && // If local human user,...
  957. (users_dir_i[writeptr][i].x_f1!=users_dir_i[(writeptr-1)&511][i].x_f1 || // and input has changed...
  958. users_dir_i[writeptr][i].y_f2!=users_dir_i[(writeptr-1)&511][i].y_f2) )
  959. send_single(i); // then send to master
  960. receive_multiple(); // Receive all input data from master
  961. }
  962. }
  963. else // If not a network game...
  964. {
  965. if (!paused) // and not paused...
  966. {
  967. get_user_inputs();
  968. process_user_inputs();
  969. writeptr++;
  970. writeptr&=511; // Move to next block
  971. }
  972. }
  973. }
  974. }
  975. }
  976. }
  977. // Wait for next interrupt
  978. void TickPause()
  979. {
  980. int tcount;
  981. tcount=count;
  982. while (count<=tcount+TIMER_FRAC);
  983. }
  984. /********************/
  985. /* RECEIVING DATA */
  986. /********************/
  987. // Routine for master to receive all input data types.
  988. void receive_all_singles()
  989. {
  990. packet_header *header;
  991. packet_single *data;
  992. packet_info info;
  993. PSTR pPacket;
  994. if (network_on)
  995. {
  996. header=&in_header;
  997. while (hmiNETNOWGetHeader((PSTR)header,sizeof(packet_header),&pPacket))
  998. {
  999. switch(header->type)
  1000. {
  1001. case NET_ONLINE:
  1002. hmiNETNOWGetBlock(pPacket,(PSTR) &info,sizeof(packet_info));
  1003. // Master receiving data
  1004. break;
  1005. case NET_SINGLE:
  1006. data=&users_dir_i[writeptr][header->player];
  1007. hmiNETNOWGetBlock(pPacket,(PSTR)data,sizeof(packet_single));
  1008. break;
  1009. case NET_READY:
  1010. puts("NET_READY received.");
  1011. active_nodes++;
  1012. break;
  1013. case NET_QUIT:
  1014. hmiNETNOWPostListen();
  1015. send_quit(); // All quit if anyone quits!
  1016. break;
  1017. case NET_PAUSE:
  1018. slave_pause=TRUE;
  1019. break;
  1020. }
  1021. hmiNETNOWPostListen();
  1022. }
  1023. }
  1024. }
  1025. // Routine for slaves to receive all input data types.
  1026. void receive_multiple()
  1027. {
  1028. packet_header *header;
  1029. packet_multiple *data;
  1030. packet_single *sing;
  1031. packet_seed *seed;
  1032. packet_info info;
  1033. packet_pause pausep;
  1034. PSTR pPacket;
  1035. if (network_on)
  1036. {
  1037. header=&in_header;
  1038. sing=&test_single;
  1039. seed=&test_seed;
  1040. while (hmiNETNOWGetHeader((PSTR)header,sizeof(packet_header),&pPacket))
  1041. {
  1042. switch(header->type)
  1043. {
  1044. case NET_ONLINE:
  1045. hmiNETNOWGetBlock(pPacket,(PSTR) &info,sizeof(packet_info));
  1046. // Slave receiving data
  1047. break;
  1048. case NET_MULTIPLE:
  1049. data=(packet_multiple *)users_dir_i[writeptr];
  1050. hmiNETNOWGetBlock(pPacket,(PSTR)data,sizeof(packet_multiple));
  1051. writeptr++;
  1052. writeptr&=511;
  1053. if (!net_quit) active_nodes=network_on;
  1054. break;
  1055. case NET_SEED:
  1056. // Slave receiving seed
  1057. hmiNETNOWGetBlock(pPacket,(PSTR)seed,sizeof(packet_seed));
  1058. received_seed=seed->num?seed->num:1;
  1059. break;
  1060. case NET_QUIT:
  1061. net_quit=TRUE;
  1062. break;
  1063. case NET_PAUSE:
  1064. hmiNETNOWGetBlock(pPacket,(PSTR) &pausep,sizeof(packet_pause));
  1065. paused=pausep.paused;
  1066. break;
  1067. }
  1068. hmiNETNOWPostListen();
  1069. }
  1070. }
  1071. }
  1072. /******************/
  1073. /* SENDING DATA */
  1074. /******************/
  1075. //Master transmits all input data, sends NET_MULTIPLE header and multiple packet
  1076. void send_multiple()
  1077. {
  1078. packet_header *header;
  1079. packet_multiple *data;
  1080. int i,sent;
  1081. if (network_on)
  1082. {
  1083. header=&p_header;
  1084. data=&test_multiple;
  1085. header->player=wConsoleNode;
  1086. header->type=NET_MULTIPLE;
  1087. header->frame=frame_number;
  1088. frame_number++;
  1089. // data=(packet_single *)users_dir_i[writeptr];
  1090. for (i=0;i<network_on;i++)
  1091. {
  1092. data->inp[i].x=users_dir_i[writeptr][i].x_f1;
  1093. data->inp[i].y=users_dir_i[writeptr][i].y_f2;
  1094. }
  1095. i=abs(start_multiple);
  1096. sent=TRUE;
  1097. while (i>=0 && sent)
  1098. {
  1099. if (i!=master)
  1100. sent=hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) data,sizeof(packet_multiple),i);
  1101. else
  1102. sent=TRUE;
  1103. if (sent) i--;
  1104. }
  1105. if (sent) start_multiple=network_on-1;
  1106. else start_multiple=-i;
  1107. }
  1108. }
  1109. // Slave sends own input data, send NET_SINGLE header and input packet to master
  1110. void send_single(int plyr)
  1111. {
  1112. packet_header *header;
  1113. packet_single *data;
  1114. int wait;
  1115. if (network_on)
  1116. {
  1117. header=&p_header;
  1118. data=&test_single;
  1119. header->player=wConsoleNode;
  1120. header->type=NET_SINGLE;
  1121. data=(packet_single *)&users_dir_i[writeptr][plyr];
  1122. // do
  1123. wait=hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) data,sizeof(packet_single),master);
  1124. // while( !wait );
  1125. }
  1126. }
  1127. // Slave is ready to play, send NET_READY header and null data packet to master
  1128. void send_ready()
  1129. {
  1130. packet_header *header;
  1131. packet_data *data;
  1132. if (network_on)
  1133. {
  1134. header=&p_header;
  1135. data=&p_data;
  1136. header->player=wConsoleNode;
  1137. header->type=NET_READY;
  1138. if (wConsoleNode==master)
  1139. active_nodes++; // If master just increase active nodes
  1140. else
  1141. {puts("NET_READY sent.");
  1142. while( !hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) data,0,master) );
  1143. }
  1144. }
  1145. }
  1146. // Master sends a random seed, send NET_SEED header and seed packet to slaves
  1147. void send_seed(int num)
  1148. {
  1149. packet_header *header;
  1150. packet_seed *data;
  1151. int i;
  1152. if (network_on)
  1153. {
  1154. header=&p_header;
  1155. data=&test_seed;
  1156. header->player=wConsoleNode;
  1157. header->type=NET_SEED;
  1158. data->num=num;
  1159. for (i=0;i<network_on;i++)
  1160. if (i!=master)
  1161. while( !hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) data,sizeof(packet_seed),i) );
  1162. // puts("Net_seed sent");
  1163. }
  1164. }
  1165. // Send a message, send NET_MESSAGE header and message packet containing string (watch maximum length) to everyone
  1166. void send_mes(char *m)
  1167. {
  1168. packet_header *header;
  1169. packet_data *data;
  1170. WORD head_size,data_size;
  1171. int i;
  1172. if (network_on)
  1173. {
  1174. header=&p_header;
  1175. data=&p_data;
  1176. strcpy(data->mes,m);
  1177. header->player=wConsoleNode;
  1178. header->type=NET_MESSAGE;
  1179. for (i=0;i<network_on;i++)
  1180. {
  1181. if (i!=wConsoleNode)
  1182. while( !hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) data,sizeof(packet_data),i) );
  1183. }
  1184. }
  1185. }
  1186. // Player is to quit, send NET_QUIT header and null data packet, if master quits, all nodes quit
  1187. void send_quit()
  1188. {
  1189. packet_header *header;
  1190. packet_data *data;
  1191. WORD head_size,data_size;
  1192. int i;
  1193. if (network_on)
  1194. {
  1195. header=&p_header;
  1196. data=&p_data;
  1197. header->player=wConsoleNode;
  1198. header->type=NET_QUIT;
  1199. if (wConsoleNode==master)
  1200. {
  1201. for (i=0;i<network_on;i++)
  1202. if (i!=wConsoleNode)
  1203. while( !hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) data,0,i) );
  1204. net_quit=TRUE;
  1205. }
  1206. else while( !hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) data,0,master) );
  1207. }
  1208. }
  1209. // Send pause request, send NET_PAUSE header and pause packet to master
  1210. void send_pause()
  1211. {
  1212. packet_header header;
  1213. packet_pause data;
  1214. if (network_on)
  1215. {
  1216. header.type=NET_PAUSE;
  1217. data.paused=!paused; //request to invert! (not used by master - simply inverts its copy!)
  1218. while (!hmiNETNOWSendData((PSTR) &header,sizeof(packet_header),(PSTR) &data,sizeof(packet_pause),master));
  1219. }
  1220. }
  1221. // Transmit pause to slaves, send NET_PAUSE header and pause packet to all slaves
  1222. void transmitpausetoslaves(void)
  1223. {
  1224. //transmit new pause status to slaves
  1225. packet_header header;
  1226. packet_pause data;
  1227. int i;
  1228. if (network_on)
  1229. {
  1230. header.type=NET_PAUSE;
  1231. data.paused=paused; //request to invert!
  1232. for (i=0;i<network_on;i++)
  1233. if (i!=master)
  1234. while (!hmiNETNOWSendData((PSTR) &header,sizeof(packet_header),(PSTR) &data,sizeof(packet_pause),i));
  1235. }
  1236. }
  1237. /****************************/
  1238. /* INITIALISE/SYNCHRONISE */
  1239. /****************************/
  1240. // Send initialisation data to or from newly logged on node (broadcast message necessary, others must discard)
  1241. void TransmitInit(void)
  1242. {
  1243. packet_header header;
  1244. packet_info info;
  1245. if (network_on)
  1246. {
  1247. // Send address & data
  1248. info.address=laddress;
  1249. info.Team_A=Team_A;
  1250. info.Team_B=Team_B;
  1251. info.Team=TeamChoice;
  1252. info.Player=PlayerChoice;
  1253. header.type=NET_ONLINE;
  1254. while (!hmiNETNOWSendData((PSTR) &header,sizeof(packet_header),(PSTR) &info,sizeof(packet_info),_NETNOW_BROADCAST));
  1255. }
  1256. }
  1257. // Finds if supplied address is in address list
  1258. int FindAddress(_NETNOW_NODE_ADDR daddress)
  1259. {
  1260. int duplic=-1;
  1261. int i,j;
  1262. char diff;
  1263. char *a1,*a2;
  1264. int sizeaddress;
  1265. sizeaddress=sizeof(_NETNOW_NODE_ADDR);
  1266. for (i=0;i<network_on;i++)
  1267. {
  1268. diff=FALSE;
  1269. for (j=0;j<sizeaddress;j++)
  1270. {
  1271. a1=(char *) &address[i];
  1272. a2=(char *) &daddress;
  1273. if (*(a1+j)!=*(a2+j)) diff=TRUE;
  1274. }
  1275. if (!diff) { duplic=i; break; }
  1276. }
  1277. return (duplic);
  1278. }
  1279. void NetInitialisation()
  1280. {
  1281. packet_header header;
  1282. packet_info info;
  1283. packet_seed *seed;
  1284. PSTR pPacket;
  1285. _NETNOW_NODE_ADDR taddress;
  1286. int error,Node;
  1287. int i;
  1288. seed=&test_seed;
  1289. // wNETNodes=network_on;
  1290. while (hmiNETNOWGetHeader((PSTR) &header,sizeof(packet_header),&pPacket))
  1291. {
  1292. switch(header.type)
  1293. {
  1294. // Received online broadcast...
  1295. case NET_ONLINE:
  1296. hmiNETNOWGetBlock(pPacket,(PSTR) &info,sizeof(packet_info));
  1297. // Have we already added this node (will happen a lot because this is a broadcast message)
  1298. if (FindAddress(info.address)<0 && !NetDone)
  1299. {
  1300. // This is a new node - add it in.
  1301. error=hmiNETNOWAddNode(&info.address);
  1302. switch(error)
  1303. {
  1304. case _NETNOW_ADD_COMPLETE:
  1305. hmiNETNOWSortNodes();
  1306. address[network_on]=info.address;
  1307. if (info.Team_A>=0) Team_A=info.Team_A,Team_B=info.Team_B;
  1308. Teams[network_on]=info.Team;
  1309. Players[network_on]=info.Player;
  1310. network_on++;
  1311. // Send own data back through a NET_ONLINE message, those already on will ignore!!!
  1312. TransmitInit();
  1313. break;
  1314. case _NETNOW_ADD_DUPLICATE:
  1315. exit(1);
  1316. break;
  1317. case _NETNOW_ADD_LIST_FULL:
  1318. exit(1);
  1319. break;
  1320. default:
  1321. break;
  1322. }
  1323. }
  1324. break;
  1325. // Received go message, find node number; no more people can log on now.
  1326. case NET_GO:
  1327. if (!net_players[header.player])
  1328. {
  1329. NetDone++;
  1330. net_players[header.player]=TRUE;
  1331. // if (NetDone<=1)
  1332. // {
  1333. // SendNetDone();
  1334. // FoundNodes();
  1335. // SendPlayerInfo();
  1336. // }
  1337. }
  1338. break;
  1339. // Received initialisation info (team or player choices)
  1340. case NET_INFO:
  1341. hmiNETNOWGetBlock(pPacket,(PSTR) &info,sizeof(packet_info));
  1342. // Set up player data
  1343. if (info.Team_A>=0)
  1344. {
  1345. Team_A=info.Team_A;
  1346. Team_B=info.Team_B;
  1347. }
  1348. else
  1349. {
  1350. Node=FindAddress(info.address);
  1351. if (header.player>=0)
  1352. {
  1353. // Informant has sent his node value, swap own data structures to match...
  1354. taddress=address[Node];
  1355. address[Node]=address[header.player];
  1356. address[header.player]=taddress;
  1357. Teams[Node]=Teams[header.player];
  1358. Players[Node]=Players[header.player];
  1359. Node=header.player;
  1360. }
  1361. Teams[Node]=info.Team;
  1362. Players[Node]=info.Player;
  1363. }
  1364. break;
  1365. // Received seed from master
  1366. case NET_SEED:
  1367. // puts("Net_seed found");
  1368. hmiNETNOWGetBlock(pPacket,(PSTR)seed,sizeof(packet_seed));
  1369. received_seed=seed->num?seed->num:1;
  1370. break;
  1371. // Received quit command from master
  1372. case NET_QUIT:
  1373. puts("Net_quit found");
  1374. net_quit=TRUE;
  1375. break;
  1376. }
  1377. hmiNETNOWPostListen();
  1378. }
  1379. }
  1380. // Finished waiting for new nodes, find no of nodes and own node number
  1381. void FoundNodes()
  1382. {
  1383. if (master!=0)
  1384. {
  1385. network_on=hmiNETNOWGetActiveNodes(); // Get active nodes
  1386. wConsoleNode=hmiNETNOWGetConsoleNode(); // Get console node
  1387. master=0;
  1388. // num_of_users=network_on;
  1389. start_multiple=network_on-1;
  1390. }
  1391. }
  1392. // Player presses start
  1393. void SendNetDone()
  1394. {
  1395. packet_header header;
  1396. packet_data data;
  1397. int i,node,tval;
  1398. NetDone++;
  1399. FoundNodes();
  1400. node=FindAddress(laddress);
  1401. address[node]=address[wConsoleNode];
  1402. address[wConsoleNode]=laddress;
  1403. tval=Teams[node];
  1404. Teams[node]=Teams[wConsoleNode];
  1405. Teams[wConsoleNode]=tval;
  1406. tval=Players[node];
  1407. Players[node]=Players[wConsoleNode];
  1408. Players[wConsoleNode]=tval;
  1409. net_players[wConsoleNode]=TRUE;
  1410. header.player=wConsoleNode;
  1411. header.type=NET_GO;
  1412. for (i=0;i<network_on;i++)
  1413. if (i!=wConsoleNode)
  1414. while (!hmiNETNOWSendData((PSTR) &header,sizeof(packet_header),(PSTR) &data,0,i));
  1415. SendPlayerInfo();
  1416. }
  1417. void SendTeamInfo()
  1418. {
  1419. packet_header *header;
  1420. packet_info info;
  1421. int i;
  1422. if (network_on)
  1423. {
  1424. header=&p_header;
  1425. // Set up team data
  1426. info.Team_A=Team_A;
  1427. info.Team_B=Team_B;
  1428. header->type=NET_INFO;
  1429. for (i=0;i<network_on;i++)
  1430. if (i!=wConsoleNode)
  1431. while (!hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) &info,sizeof(packet_info),i));
  1432. }
  1433. }
  1434. void SendPlayerInfo()
  1435. {
  1436. packet_header *header;
  1437. packet_info info;
  1438. int i;
  1439. if (network_on)
  1440. {
  1441. header=&p_header;
  1442. // Set up player data
  1443. info.Team_A=info.Team_B=-1;
  1444. info.Team=TeamChoice;
  1445. info.Player=PlayerChoice;
  1446. info.address=address[(wConsoleNode<0) ? 0 : wConsoleNode];
  1447. header->player=wConsoleNode;
  1448. header->type=NET_INFO;
  1449. for (i=0;i<network_on;i++)
  1450. if (i!=wConsoleNode)
  1451. while( !hmiNETNOWSendData((PSTR) header,sizeof(packet_header),(PSTR) &info,sizeof(packet_info),i) );
  1452. }
  1453. }
  1454. void pausehandler()
  1455. {
  1456. if (pause_request)
  1457. {
  1458. if (!network_on)
  1459. {
  1460. paused=!paused;
  1461. }
  1462. else
  1463. {
  1464. if (wConsoleNode==master)
  1465. {
  1466. //master initiated pause.
  1467. paused=!paused;
  1468. transmitpausetoslaves(); //all machines will pause
  1469. }
  1470. else
  1471. {
  1472. send_pause(); //send pause to master
  1473. }
  1474. }
  1475. pause_request=FALSE;
  1476. }
  1477. if (network_on && slave_pause && wConsoleNode==master)
  1478. {
  1479. //master received a request to pause from slave
  1480. paused=!paused;
  1481. transmitpausetoslaves();
  1482. slave_pause=FALSE;
  1483. }
  1484. }
  1485. /*****************/
  1486. /* NETWORK SETUP */
  1487. /*****************/
  1488. int Initialise_Network()
  1489. {
  1490. int i;
  1491. // active_nodes=1; // if no network 1 player.
  1492. net_quit=FALSE;
  1493. master=-1;
  1494. wConsoleNode=-1;
  1495. if (network_on)
  1496. {
  1497. received_seed=FALSE;
  1498. frame_number=0;
  1499. active_nodes=0; //reset if network on.
  1500. // puts ("INTIALIZING NETWORK.");
  1501. if ( hmiNETNOWInitSystem( 16 ) != _NETNOW_NO_ERROR )
  1502. {
  1503. // puts( "ERROR: NetNOW! could not locate IPX or NetBIOS!" );
  1504. network_on=FALSE;
  1505. netsetup_on=FALSE;
  1506. old_mf=TRUE;
  1507. return(-1); //...
  1508. }
  1509. else
  1510. {
  1511. // puts( "NetNOW! system initialized!" );
  1512. // Check for information display
  1513. if ( wNETInfoFlag )
  1514. switch( hmiNETNOWGetNetworkType() )
  1515. {
  1516. case _NETNOW_IPX :
  1517. // display type
  1518. // puts( "Network is using a IPX based protocol." );
  1519. break;
  1520. case _NETNOW_NETBIOS:
  1521. // display type
  1522. // puts( "Network is using a NetBIOS based protocol." );
  1523. break;
  1524. }
  1525. while (hmiNETNOWPostListen());
  1526. for (i=0;i<MAX_NET_USERS;i++) net_players[i]=FALSE;
  1527. hmiNETNOWGetNetworkAddr(&laddress); //read this nodes address.
  1528. address[0]=laddress;
  1529. TransmitInit(); //send NET_ONLINE (with address) as a broadcast to other machines
  1530. NetDone=0;
  1531. network_on=onetwork_on=1;
  1532. }
  1533. }
  1534. return (0);
  1535. }
  1536. void close_network()
  1537. {
  1538. packet_header *header;
  1539. PSTR pPacket;
  1540. short head_size;
  1541. if (network_on)
  1542. {
  1543. // puts("Resetting network.");
  1544. header=&in_header;
  1545. hmiNETNOWPostListen();
  1546. while (hmiNETNOWGetHeader((PSTR)header,sizeof(packet_header),&pPacket)) hmiNETNOWPostListen();
  1547. hmiNETNOWUnInitSystem();
  1548. // puts("Network removed.");
  1549. }
  1550. netsetup_on=FALSE;
  1551. network_on=FALSE;
  1552. old_mf=TRUE;
  1553. master=-1;
  1554. }
  1555. int Poll_Network()
  1556. {
  1557. static scount=0;
  1558. int i,s;
  1559. // if (!(count&127)) putch('.');
  1560. // if (keys[0x13]) {close_network();exit(1);}
  1561. if (NetDone<network_on)
  1562. {
  1563. return (-1);
  1564. if (onetwork_on!=network_on)
  1565. {
  1566. for (i=onetwork_on;i<network_on;i++)
  1567. printf("\nFound node %d\n",i);
  1568. onetwork_on=network_on;
  1569. }
  1570. return (-1);
  1571. }
  1572. else
  1573. {
  1574. if (wConsoleNode==master)
  1575. {
  1576. if (!(s=clock()&0xffff)) s=1;
  1577. send_seed(s);
  1578. }
  1579. else
  1580. while(!(s=received_seed));
  1581. return (s);
  1582. }
  1583. }
  1584. int Start_Network_Game()
  1585. {
  1586. int i;
  1587. Team_A=-1;
  1588. Team_B=-1;
  1589. for (i=0; i<MAX_NET_USERS; i++)
  1590. {
  1591. Teams[i]=-1;
  1592. Players[i]=-1;
  1593. }
  1594. TeamChoice=-1;
  1595. PlayerChoice=-1;
  1596. network_on=netsetup_on=TRUE;
  1597. old_mf=FALSE;
  1598. return (Initialise_Network());
  1599. }
  1600. void Network_Play_Ready()
  1601. {
  1602. if (network_on)
  1603. {
  1604. send_ready();
  1605. while (active_nodes<network_on); // escape key?
  1606. }
  1607. }
  1608. void Quit_Network_Play()
  1609. {
  1610. if (network_on)
  1611. {
  1612. send_quit();
  1613. while (!net_quit); // Timeout??
  1614. active_nodes=0;
  1615. }
  1616. }
  1617. void End_Network_Game()
  1618. {
  1619. if (network_on)
  1620. close_network();
  1621. }
  1622. int NetTest()
  1623. {
  1624. int i,Starts,Seed,stick;
  1625. char TeamsSelected,ChoiceAltered,StartPressed;
  1626. //...whatever front end stuff comes first...
  1627. // There are variables Team_A & Team_B for teams playing, TeamChoice for this players choice from these
  1628. // two, (value 0 or 1), PlayerChoice is the player in the team chosen, 1 to 11 (1 indicates auto-player).
  1629. // The variable network_on is the number of players currently logged on to the game, and the arrays
  1630. // Teams[] and Players[] give the current selections of all the players from 0 to network_on-1, (Note, which is
  1631. // your entry in this array is NOT stored!), you may also want to use the local variable Starts (see below)
  1632. if (Start_Network_Game()<0) // Initialises Team_A, Team_B, TeamChoice & PlayerChoice and starts network
  1633. {
  1634. old_mf=1;
  1635. return(-1);
  1636. }
  1637. TickPause();
  1638. stick=tick;
  1639. // Choose teams to play
  1640. while (Team_A<0) // Wait until they are chosen (add escape key?)
  1641. {
  1642. TeamsSelected=1; // Your condition
  1643. // If local user sets Team_A & Team_B first send them using SendTeamInfo()
  1644. if (TeamsSelected)
  1645. {
  1646. Team_A=14;Team_B=0;
  1647. SendTeamInfo();
  1648. }
  1649. TickPause();
  1650. Poll_Network();
  1651. }
  1652. //printf("\nTeam %d plays team %d\n",Team_A,Team_B);
  1653. // Teams are chosen, allow player to choose team to play for and player to control...
  1654. // 2 is added to TeamChoice when player presses start
  1655. // puts("Choose team, 'A' or 'B', then press return");
  1656. do
  1657. {
  1658. // If start has been pressed, no further selection allowed
  1659. if (TeamChoice<2)
  1660. {
  1661. // Your condition
  1662. ChoiceAltered=machine_no;
  1663. // if (keys[0x1e]) ChoiceAltered=1;
  1664. // if (keys[0x30]) ChoiceAltered=2;
  1665. // If local user alters TeamChoice or PlayerChoice then call SendPlayerInfo();
  1666. // Must check Players and Teams arrays to make sure no-one else has chosen the same
  1667. if (ChoiceAltered)
  1668. {
  1669. TeamChoice=ChoiceAltered-1;
  1670. // if (TeamChoice==0) puts("Team A chosen");
  1671. // else puts("Team B chosen");
  1672. PlayerChoice=1;
  1673. SendPlayerInfo();
  1674. }
  1675. // Your condition
  1676. StartPressed=1;//(keys[0x1c]);
  1677. // If the start button is pressed and team is chosen add 2 to TeamChoice...
  1678. if (TeamChoice>=0 && PlayerChoice>=0 && StartPressed)
  1679. {
  1680. // puts("OK, now waiting for others to start");
  1681. TeamChoice+=2;
  1682. SendPlayerInfo();
  1683. }
  1684. }
  1685. TickPause();
  1686. Poll_Network();
  1687. // Condition indicating selection complete (player chosen + start button), perhaps Starts should be displayed
  1688. Starts=0;
  1689. for (i=0;i<network_on;i++)
  1690. if (Teams[i]>=2) Starts++;
  1691. } while ((Starts<network_on || Starts<=1) && tick<stick+10*20); // Must be more than one player!
  1692. if (tick<stick+10*20)
  1693. {
  1694. // Final syncronisation (sorts nodes and sets random seed)
  1695. SendNetDone();
  1696. while ((Seed=Poll_Network())<0); // (Add escape key?)
  1697. netsetup_on=FALSE;
  1698. // Remove start flags from team data
  1699. for (i=0;i<network_on;i++)
  1700. Teams[i]-=2;
  1701. // Now set Andy's data structures from the network variables
  1702. // printf("\nTeam %d plays team %d\n",Team_A,Team_B);
  1703. // printf("This node is %d\n",wConsoleNode);
  1704. // printf("This node chose team %d, player %d\n",TeamChoice,PlayerChoice);
  1705. // for (i=0;i<network_on;i++)
  1706. // printf("Node %d plays team %d, player %d\n",i,Teams[i],Players[i]);
  1707. // printf("Seed is %d\n",Seed);
  1708. }
  1709. else
  1710. {
  1711. End_Network_Game(); // Cleans up network stuff
  1712. old_mf=1;
  1713. return (-1);
  1714. }
  1715. // ...call the game...
  1716. // ...whetever front end stuff comes after...
  1717. return (0);
  1718. }
  1719. void crap()
  1720. {
  1721. printf(" net %d %d \n",*(int *)&users[0].inp_x,&users[0].inp_x);
  1722. }