boardmem.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /* $Id$
  2. * MegaZeux
  3. *
  4. * Copyright (C) 1996 Greg Janson
  5. * Copyright (C) 1998 Matthew D. Williams - dbwilli@scsn.net
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. //Code to find space to store boards, and push and pull them into and out
  22. //of storage. Also deallocates board storage and helps simplify file
  23. //access.
  24. #include "window.h"
  25. #include "meter.h"
  26. #include "roballoc.h"
  27. #include "boardmem.h"
  28. #include "meminter.h"
  29. #include <dir.h>
  30. #include "ems.h"
  31. #include "data.h"
  32. #include "const.h"
  33. #include <stdio.h>
  34. #include "string.h"
  35. #include "error.h"
  36. #include <dos.h>
  37. #include "mod.h"
  38. #define SAVE_INDIVIDUAL
  39. //Normalize an unsigned char far * ptr-
  40. #define normalize(x) {\
  41. x=(unsigned char far *)MK_FP(FP_SEG(x)+(FP_OFF(x)>>4),FP_OFF(x)&15); }
  42. //Find space for board of given size and allocates it to given id. Don't use
  43. //if space is already allocated. If allocated, returns 0. Out of mem returns
  44. //1. This is really rare- Checks for ems, then conventional, then disk.
  45. //Doesn't affect board_sizes[]. If conv_mem_ok is 0, then only ems and disk
  46. //are checked.
  47. char allocate_board_space(long size,unsigned char id,char conv_mem_ok) {
  48. int t1,ems_pages;
  49. FILE *fp;
  50. //Check for ems first...
  51. //Get number of 16k pages needed
  52. ems_pages=(int)(size>>14);
  53. if(size&16383) ems_pages++;
  54. t1=alloc_EMS(ems_pages);
  55. if(t1) {
  56. //Got it!
  57. board_where[id]=W_EMS;
  58. board_offsets[id].EMS.handle=t1;
  59. board_offsets[id].EMS.page=0;
  60. board_filenames[id*FILENAME_SIZE]=0;
  61. return 0;
  62. }
  63. //Nope.. try conventional
  64. // SPIDER'S NOTE: WILL NOT USE CONVENTIONAL, DISABLED
  65. if(conv_mem_ok) {
  66. board_offsets[id].mem=(unsigned char *)farmalloc(size);
  67. if(board_offsets[id].mem) {
  68. //Got it!
  69. board_where[id]=W_MEMORY;
  70. board_filenames[id*FILENAME_SIZE]=0;
  71. return 0;
  72. }
  73. //Retry after clearing sfx cache
  74. free_sam_cache(1);
  75. board_offsets[id].mem=(unsigned char *)farmalloc(size);
  76. if(board_offsets[id].mem) {
  77. //Got it!
  78. board_where[id]=W_MEMORY;
  79. board_filenames[id*FILENAME_SIZE]=0;
  80. return 0;
  81. }
  82. }
  83. //Nope.. try disk
  84. //Make temporary filename
  85. str_cpy(&board_filenames[id*FILENAME_SIZE],"~MZTMPXXXXXX");
  86. mktemp(&board_filenames[id*FILENAME_SIZE]);
  87. //Open..
  88. fp=fopen(&board_filenames[id*FILENAME_SIZE],"wb");
  89. if(fp==NULL) {
  90. //Error
  91. board_filenames[id*FILENAME_SIZE]=0;
  92. return 1;
  93. }
  94. //Try to solidify room
  95. fseek(fp,size-1,SEEK_SET);
  96. fputc(0,fp);
  97. fseek(fp,size,SEEK_SET);
  98. if(ftell(fp)<size) {
  99. //Not enough room on hard drive
  100. fclose(fp);
  101. unlink(&board_filenames[id*FILENAME_SIZE]);
  102. board_filenames[id*FILENAME_SIZE]=0;
  103. return 2;
  104. }
  105. //Room made!
  106. fclose(fp);
  107. board_where[id]=W_TEMPFILE;
  108. board_offsets[id].offset=0;
  109. return 0;
  110. }
  111. //Deallocates the space allocated to id given. Sets where to W_NOWHERE and
  112. //clears all others except for board_sizes[].
  113. void deallocate_board_space(unsigned char id) {
  114. //According to type...
  115. switch(board_where[id]) {
  116. case W_EMS:
  117. //EMS- deallocate.
  118. free_EMS(board_offsets[id].EMS.handle);
  119. break;
  120. case W_MEMORY:
  121. //Memory- deallocate.
  122. farfree(board_offsets[id].mem);
  123. break;
  124. case W_TEMPFILE:
  125. //Temp file- delete.
  126. unlink(&board_filenames[id*FILENAME_SIZE]);
  127. break;
  128. }
  129. //Reset variables for id
  130. board_where[id]=W_NOWHERE;
  131. board_offsets[id].offset=0;
  132. board_filenames[id*FILENAME_SIZE]=0;
  133. }
  134. //Returns size array would take up RLE2 encoded, using current x size and
  135. //y size.
  136. unsigned int RLE2_size(unsigned char far *plane) {
  137. unsigned int size=4;//4 for x/y size
  138. int t1,t2;
  139. int runchar=-1,runsize=-1,chr;
  140. for(t1=0;t1<board_ysiz;t1++) {
  141. for(t2=0;t2<board_xsiz;t2++) {
  142. //Get char
  143. chr=plane[t1*max_bxsiz+t2];
  144. //Continue run?
  145. if(chr==runchar) {
  146. //Yep.
  147. runsize++;
  148. //Run size at max of 127?
  149. if(runsize==127) {
  150. //Store run and reset it.
  151. size+=2;
  152. runchar=runsize=-1;
  153. }
  154. }
  155. else {
  156. //Nope- Store current run...
  157. if((runchar<128)&&(runsize==1)) size++;
  158. else if(runchar>-1) size+=2;
  159. //...and set up for a new run.
  160. runchar=chr;
  161. runsize=1;
  162. }
  163. //Loop.
  164. }
  165. }
  166. //Store last run, if any
  167. if((runchar<128)&&(runsize==1)) size++;
  168. else if(runchar>-1) size+=2;
  169. //Done- return size.
  170. return size;
  171. }
  172. //Stores plane in where as RLE2 compressed, using current x/y sizes.
  173. //Returns size.
  174. unsigned int RLE2_store(unsigned char far *where,unsigned char far *plane) {
  175. unsigned int size=4;//4 for x/y size
  176. int t1,t2;
  177. int runchar=-1,runsize=-1,chr;
  178. where[0]=board_xsiz&255; where[1]=board_xsiz>>8;
  179. where[2]=board_ysiz&255; where[3]=board_ysiz>>8;
  180. for(t1=0;t1<board_ysiz;t1++) {
  181. for(t2=0;t2<board_xsiz;t2++) {
  182. //Get char
  183. chr=plane[t1*max_bxsiz+t2];
  184. //Continue run?
  185. if(chr==runchar) {
  186. //Yep.
  187. runsize++;
  188. //Run size at max of 127?
  189. if(runsize==127) {
  190. //Store run and reset it.
  191. where[size++]=runsize|128;
  192. where[size++]=runchar;
  193. runchar=runsize=-1;
  194. }
  195. }
  196. else {
  197. //Nope- Store current run...
  198. if((runchar<128)&&(runsize==1)) where[size++]=runchar;
  199. else if(runchar>-1) {
  200. where[size++]=runsize|128;
  201. where[size++]=runchar;
  202. }
  203. //...and set up for a new run.
  204. runchar=chr;
  205. runsize=1;
  206. }
  207. //Loop.
  208. }
  209. }
  210. //Store last run, if any
  211. if((runchar<128)&&(runsize==1)) where[size++]=runchar;
  212. else if(runchar>-1) {
  213. where[size++]=runsize|128;
  214. where[size++]=runchar;
  215. }
  216. //Done.
  217. return size;
  218. }
  219. //Stores plane in file as RLE2 compressed, using current x/y sizes.
  220. void RLE2_save(FILE *fp,unsigned char far *plane) {
  221. int t1,t2;
  222. int runchar=-1,runsize=-1,chr;
  223. fwrite(&board_xsiz,2,1,fp);
  224. fwrite(&board_ysiz,2,1,fp);
  225. for(t1=0;t1<board_ysiz;t1++) {
  226. for(t2=0;t2<board_xsiz;t2++) {
  227. //Get char
  228. chr=plane[t1*max_bxsiz+t2];
  229. //Continue run?
  230. if(chr==runchar) {
  231. //Yep.
  232. runsize++;
  233. //Run size at max of 127?
  234. if(runsize==127) {
  235. //Store run and reset it.
  236. fputc(runsize|128,fp);
  237. fputc(runchar,fp);
  238. runchar=runsize=-1;
  239. }
  240. }
  241. else {
  242. //Nope- Store current run...
  243. if((runchar<128)&&(runsize==1)) fputc(runchar,fp);
  244. else if(runchar>-1) {
  245. fputc(runsize|128,fp);
  246. fputc(runchar,fp);
  247. }
  248. //...and set up for a new run.
  249. runchar=chr;
  250. runsize=1;
  251. }
  252. //Loop.
  253. }
  254. }
  255. //Store last run, if any
  256. if((runchar<128)&&(runsize==1)) fputc(runchar,fp);
  257. else if(runchar>-1) {
  258. fputc(runsize|128,fp);
  259. fputc(runchar,fp);
  260. }
  261. //Done.
  262. }
  263. //Reads RLE2-compressed plane from where and puts into plane.
  264. //Also sets board_xsiz and board_ysiz unless specified otherwise.
  265. //Returns byte count of RLE2 read.
  266. unsigned int RLE2_read(unsigned char far *where,unsigned char far *plane,
  267. char change_xysizes) {
  268. unsigned int pos=4;//2 for x/y size
  269. int t1,t2,t3,xsiz=where[0]+(where[1]<<8),ysiz=where[2]+(where[3]<<8);
  270. int runsize,chr;
  271. if(change_xysizes) {
  272. board_xsiz=xsiz;
  273. board_ysiz=ysiz;
  274. }
  275. t1=t2=0;//y and x position
  276. do {
  277. chr=where[pos++];
  278. if(!(chr&128)) {//Regular character
  279. plane[t1*max_bxsiz+t2]=chr;
  280. if((++t2)>=xsiz) {
  281. t2=0;
  282. if((++t1)>=ysiz) goto done;
  283. }
  284. }
  285. else {
  286. //A run
  287. runsize=chr&127;
  288. chr=where[pos++];
  289. for(t3=0;t3<runsize;t3++) {
  290. plane[t1*max_bxsiz+t2]=chr;
  291. if((++t2)>=xsiz) {
  292. t2=0;
  293. if((++t1)>=ysiz) goto done;
  294. }
  295. }
  296. }
  297. } while(1);
  298. done:
  299. //Done!
  300. return pos;
  301. }
  302. //Reads RLE2-compressed plane from file and puts into plane.
  303. //Also sets board_xsiz and board_ysiz unless specified otherwise.
  304. void RLE2_load(FILE *fp,unsigned char far *plane,char change_xysizes) {
  305. int t1,t2,t3,xsiz,ysiz;
  306. int runsize,chr;
  307. fread(&xsiz,2,1,fp);
  308. fread(&ysiz,2,1,fp);
  309. if(change_xysizes) {
  310. board_xsiz=xsiz;
  311. board_ysiz=ysiz;
  312. }
  313. t1=t2=0;//y and x position
  314. do {
  315. chr=fgetc(fp);
  316. if(!(chr&128)) {//Regular character
  317. plane[t1*max_bxsiz+t2]=chr;
  318. if((++t2)>=xsiz) {
  319. t2=0;
  320. if((++t1)>=ysiz) goto done;
  321. }
  322. }
  323. else {
  324. //A run
  325. runsize=chr&127;
  326. chr=fgetc(fp);
  327. for(t3=0;t3<runsize;t3++) {
  328. plane[t1*max_bxsiz+t2]=chr;
  329. if((++t2)>=xsiz) {
  330. t2=0;
  331. if((++t1)>=ysiz) goto done;
  332. }
  333. }
  334. }
  335. } while(1);
  336. done:
  337. //Done!
  338. }
  339. //Gets size current board would take up, were it stored.
  340. long size_of_current_board(void) {
  341. long size=224;//Size of info other than board and robots/etc
  342. int t1,count=0;
  343. //Count robots
  344. for(t1=1;t1<NUM_ROBOTS;t1++)
  345. if(robots[t1].used) count=t1;
  346. //Add room for robots and all their programs plus the count itself
  347. size+=sizeof(Robot)*count;
  348. if(count)
  349. for(t1=0;t1<count;t1++)
  350. size+=robots[t1+1].program_length;
  351. //Count scrolls
  352. count=0;
  353. for(t1=1;t1<NUM_SCROLLS;t1++)
  354. if(scrolls[t1].used) count=t1;
  355. //Add room for scrolls and all their texts plus the count itself
  356. size+=sizeof(Scroll)*count;
  357. if(count)
  358. for(t1=0;t1<count;t1++)
  359. size+=scrolls[t1+1].mesg_size;
  360. //Count sensors
  361. count=0;
  362. for(t1=1;t1<NUM_SENSORS;t1++)
  363. if(sensors[t1].used) count=t1;
  364. //Add room for sensors plus the count itself
  365. size+=sizeof(Sensor)*count;
  366. //If overlay on, add room for 2 bytes (one for overlay mode, one
  367. //for indicator bit) and RLE2 of both planes
  368. if(overlay_mode) {
  369. size+=2;
  370. size+=RLE2_size(overlay);
  371. size+=RLE2_size(overlay_color);
  372. }
  373. //Finally, add room for all six planes, RLE2 encoded, and return
  374. size+=RLE2_size(level_id);
  375. size+=RLE2_size(level_color);
  376. size+=RLE2_size(level_param);
  377. size+=RLE2_size(level_under_id);
  378. size+=RLE2_size(level_under_color);
  379. return(size+RLE2_size(level_under_param));
  380. }
  381. //Stores current board in slot given. Space must already be allocated.
  382. //board_sizes and other board variables MUST be accurate. Returns non-0
  383. //if out of room in memory and on disk for working, or for misc. errors.
  384. //Does not affect and is not affected by a current object or global robot.
  385. char store_current(unsigned char id) {
  386. unsigned char far *ptr;//Where to store for mem/ems
  387. unsigned char far *old_ptr;//Saves old ems allocated ptr
  388. FILE *fp;//Where to store for tempfile (or ems if pressed for space)
  389. int t1,count,tcpy;
  390. long copied;
  391. //Space of proper size already assumed to be allocated. This function
  392. //has two major parts- The code for memory/ems, and the code for temp
  393. //files.
  394. //Switch according to storage type.
  395. switch(board_where[id]) {
  396. case W_MEMORY:
  397. case W_EMS:
  398. //If EMS, allocate a temporary area to allow storage in conventional
  399. //memory for now, then copy to EMS later. If not possible, we use a
  400. //temp file and then copy THAT to EMS later. If no room for a temp
  401. //file... return error.
  402. if(board_where[id]==W_EMS) {
  403. //Allocate a temporary area
  404. ptr=(unsigned char far *)farmalloc(board_sizes[id]);
  405. if(ptr==NULL) {
  406. //No room in mem- try a temp file
  407. //Make temporary filename
  408. str_cpy(&board_filenames[id*FILENAME_SIZE],"~MZTMPXXXXXX");
  409. mktemp(&board_filenames[id*FILENAME_SIZE]);
  410. //Open..
  411. fp=fopen(&board_filenames[id*FILENAME_SIZE],"wb+");
  412. if(fp==NULL) {
  413. //Error
  414. board_filenames[id*FILENAME_SIZE]=0;
  415. return 1;
  416. }
  417. //Try to make room
  418. fseek(fp,board_sizes[id],SEEK_SET);
  419. if(ftell(fp)!=(board_sizes[id])) {
  420. //Not enough room on hard drive
  421. fclose(fp);
  422. unlink(&board_filenames[id*FILENAME_SIZE]);
  423. board_filenames[id*FILENAME_SIZE]=0;
  424. return 2;
  425. }
  426. //Room made! Solidify.
  427. fseek(fp,-1,SEEK_CUR);
  428. fputc(0,fp);
  429. fclose(fp);
  430. //Go to tempfile storage
  431. goto store_tempfile;
  432. }
  433. else old_ptr=ptr;
  434. }
  435. else ptr=(unsigned char far *)board_offsets[id].mem;
  436. *(ptr++)=max_bsiz_mode;
  437. //Store RLE2 stuff
  438. if(overlay_mode) {
  439. *(ptr++)=0;
  440. *(ptr++)=overlay_mode;
  441. ptr+=RLE2_store((unsigned char far*)ptr,overlay);
  442. normalize(ptr);
  443. ptr+=RLE2_store((unsigned char far*)ptr,overlay_color);
  444. normalize(ptr);
  445. }
  446. ptr+=RLE2_store((unsigned char far *)ptr,level_id);
  447. normalize(ptr);
  448. ptr+=RLE2_store((unsigned char far *)ptr,level_color);
  449. normalize(ptr);
  450. ptr+=RLE2_store((unsigned char far *)ptr,level_param);
  451. normalize(ptr);
  452. ptr+=RLE2_store((unsigned char far *)ptr,level_under_id);
  453. normalize(ptr);
  454. ptr+=RLE2_store((unsigned char far *)ptr,level_under_color);
  455. normalize(ptr);
  456. ptr+=RLE2_store((unsigned char far *)ptr,level_under_param);
  457. normalize(ptr);
  458. //Store variables- Due to the way they are stored in DATA.ASM,
  459. //they can all be stored as a series of 210 bytes starting at
  460. //mod_playing. Appropriate code to save each individually is
  461. //also included in case this ever fails. To use the individual
  462. //code, define the symbol SAVE_INDIVIDUAL.
  463. #ifndef SAVE_INDIVIDUAL
  464. mem_cpy((char far *)ptr,mod_playing,207+FILENAME_SIZE);
  465. ptr+=207+FILENAME_SIZE;
  466. #else
  467. mem_cpy((char far *)ptr,mod_playing,FILENAME_SIZE);
  468. ptr+=FILENAME_SIZE;
  469. *(ptr++)=viewport_x;
  470. *(ptr++)=viewport_y;
  471. *(ptr++)=viewport_xsiz;
  472. *(ptr++)=viewport_ysiz;
  473. *(ptr++)=can_shoot;
  474. *(ptr++)=can_bomb;
  475. *(ptr++)=fire_burn_brown;
  476. *(ptr++)=fire_burn_space;
  477. *(ptr++)=fire_burn_fakes;
  478. *(ptr++)=fire_burn_trees;
  479. *(ptr++)=explosions_leave;
  480. *(ptr++)=save_mode;
  481. *(ptr++)=forest_becomes;
  482. *(ptr++)=collect_bombs;
  483. *(ptr++)=fire_burns;
  484. mem_cpy((char far *)ptr,(char far *)board_dir,4);
  485. ptr+=4;
  486. *(ptr++)=restart_if_zapped;
  487. mem_cpy((char far *)ptr,(char far *)&time_limit,2);
  488. ptr+=2;
  489. *(ptr++)=last_key;
  490. mem_cpy((char far *)ptr,(char far *)&num_input,2);
  491. ptr+=2;
  492. *(ptr++)=input_size;
  493. mem_cpy((char far *)ptr,input_string,81);
  494. ptr+=81;
  495. *(ptr++)=player_last_dir;
  496. mem_cpy((char far *)ptr,bottom_mesg,81);
  497. ptr+=81;
  498. *(ptr++)=b_mesg_timer;
  499. *(ptr++)=lazwall_start;
  500. *(ptr++)=b_mesg_row;
  501. *(ptr++)=b_mesg_col;
  502. mem_cpy((char far *)ptr,(char far *)&scroll_x,2);
  503. ptr+=2;
  504. mem_cpy((char far *)ptr,(char far *)&scroll_y,2);
  505. ptr+=2;
  506. mem_cpy((char far *)ptr,(char far *)&locked_x,2);
  507. ptr+=2;
  508. mem_cpy((char far *)ptr,(char far *)&locked_y,2);
  509. ptr+=2;
  510. *(ptr++)=player_ns_locked;
  511. *(ptr++)=player_ew_locked;
  512. *(ptr++)=player_attack_locked;
  513. *(ptr++)=volume;
  514. *(ptr++)=volume_inc;
  515. *(ptr++)=volume_target;
  516. #endif
  517. //Robot count
  518. count=0;
  519. for(t1=1;t1<NUM_ROBOTS;t1++)
  520. if(robots[t1].used) count=t1;
  521. *(ptr++)=count;
  522. //Robots themselves
  523. prepare_robot_mem();
  524. if(count) {
  525. for(t1=1;t1<=count;t1++) {
  526. //Copy robot t1
  527. mem_cpy((char far *)ptr,(char far *)&robots[t1],sizeof(Robot));
  528. ptr+=sizeof(Robot);
  529. normalize(ptr);
  530. mem_cpy((char far *)ptr,
  531. (char far *)&robot_mem[robots[t1].program_location],
  532. robots[t1].program_length);
  533. ptr+=robots[t1].program_length;
  534. }
  535. }
  536. //Scroll count
  537. count=0;
  538. for(t1=1;t1<NUM_SCROLLS;t1++)
  539. if(scrolls[t1].used) count=t1;
  540. *(ptr++)=count;
  541. //Scrolls themselves
  542. if(count) {
  543. for(t1=1;t1<=count;t1++) {
  544. //Copy scroll t1
  545. mem_cpy((char far *)ptr,(char far *)&scrolls[t1],sizeof(Scroll));
  546. ptr+=sizeof(Scroll);
  547. normalize(ptr);
  548. mem_cpy((char far *)ptr,
  549. (char far *)&robot_mem[scrolls[t1].mesg_location],
  550. scrolls[t1].mesg_size);
  551. ptr+=scrolls[t1].mesg_size;
  552. }
  553. }
  554. //Sensor count
  555. count=0;
  556. for(t1=1;t1<NUM_SENSORS;t1++)
  557. if(sensors[t1].used) count=t1;
  558. *(ptr++)=count;
  559. //Sensors themselves
  560. if(count) {
  561. for(t1=1;t1<=count;t1++) {
  562. //Copy sensor t1
  563. mem_cpy((char far *)ptr,(char far *)&sensors[t1],sizeof(Sensor));
  564. ptr+=sizeof(Sensor);
  565. }
  566. }
  567. //All copied! Return if memory...
  568. if(board_where[id]==W_MEMORY) return 0;
  569. //...otherwise copy to EMS.
  570. //Copy a page at a time...
  571. t1=board_offsets[id].EMS.page;//Current page
  572. copied=0;//Copied nothing so far.
  573. ptr=old_ptr;//Use original pointer
  574. do {
  575. //Map page
  576. map_page_EMS(board_offsets[id].EMS.handle,0,t1);
  577. //Copy a page, or part of a page if that's all that's left.
  578. tcpy=16384;
  579. if((copied+tcpy)>board_sizes[id]) //Too much...
  580. tcpy=(int)(board_sizes[id]-(unsigned long)copied);
  581. //Now copy it...
  582. mem_cpy(page_frame_EMS,(char far *)ptr,tcpy);
  583. //...increment status...
  584. copied+=tcpy;
  585. ptr+=tcpy;
  586. normalize(ptr);
  587. t1++;//Page
  588. //...loop.
  589. } while(copied<board_sizes[id]);
  590. //All copied! Free memory and exit
  591. farfree(old_ptr);
  592. return 0;
  593. case W_TEMPFILE:
  594. store_tempfile:
  595. //This is storage in a tempfile, sometimes a subfuction of storage
  596. //in EMS.
  597. //Open file...
  598. fp=fopen(&board_filenames[id*FILENAME_SIZE],"rb+");
  599. if(fp==NULL) {
  600. if(board_where[id]==W_EMS) board_filenames[id*FILENAME_SIZE]=0;
  601. return 1;//Error...
  602. }
  603. //...and begin output!
  604. fputc(max_bsiz_mode,fp);
  605. //Store RLE2 stuff
  606. if(overlay_mode) {
  607. fputc(0,fp);
  608. fputc(overlay_mode,fp);
  609. RLE2_save(fp,overlay);
  610. RLE2_save(fp,overlay_color);
  611. }
  612. RLE2_save(fp,level_id);
  613. RLE2_save(fp,level_color);
  614. RLE2_save(fp,level_param);
  615. RLE2_save(fp,level_under_id);
  616. RLE2_save(fp,level_under_color);
  617. RLE2_save(fp,level_under_param);
  618. //Store variables- Due to the way they are stored in DATA.ASM,
  619. //they can all be stored as a series of 210 bytes starting at
  620. //mod_playing. Appropriate code to save each individually is
  621. //also included in case this ever fails. To use the individual
  622. //code, define the symbol SAVE_INDIVIDUAL.
  623. #ifndef SAVE_INDIVIDUAL
  624. fwrite(mod_playing,1,207+FILENAME_SIZE,fp);
  625. #else
  626. fwrite(mod_playing,1,FILENAME_SIZE,fp);
  627. fputc(viewport_x,fp);
  628. fputc(viewport_y,fp);
  629. fputc(viewport_xsiz,fp);
  630. fputc(viewport_ysiz,fp);
  631. fputc(can_shoot,fp);
  632. fputc(can_bomb,fp);
  633. fputc(fire_burn_brown,fp);
  634. fputc(fire_burn_space,fp);
  635. fputc(fire_burn_fakes,fp);
  636. fputc(fire_burn_trees,fp);
  637. fputc(explosions_leave,fp);
  638. fputc(save_mode,fp);
  639. fputc(forest_becomes,fp);
  640. fputc(collect_bombs,fp);
  641. fputc(fire_burns,fp);
  642. fwrite(board_dir,1,4,fp);
  643. fputc(restart_if_zapped,fp);
  644. fwrite(&time_limit,2,1,fp);
  645. fputc(last_key,fp);
  646. fwrite(&num_input,2,1,fp);
  647. fputc(input_size,fp);
  648. fwrite(input_string,1,81,fp);
  649. fputc(player_last_dir,fp);
  650. fwrite(bottom_mesg,1,81,fp);
  651. fputc(b_mesg_timer,fp);
  652. fputc(lazwall_start,fp);
  653. fputc(b_mesg_row,fp);
  654. fputc(b_mesg_col,fp);
  655. fwrite(&scroll_x,2,1,fp);
  656. fwrite(&scroll_y,2,1,fp);
  657. fwrite(&locked_x,2,1,fp);
  658. fwrite(&locked_y,2,1,fp);
  659. fputc(player_ns_locked,fp);
  660. fputc(player_ew_locked,fp);
  661. fputc(player_attack_locked,fp);
  662. fputc(volume,fp);
  663. fputc(volume_inc,fp);
  664. fputc(volume_target,fp);
  665. #endif
  666. //Robot count
  667. count=0;
  668. for(t1=1;t1<NUM_ROBOTS;t1++)
  669. if(robots[t1].used) count=t1;
  670. fputc(count,fp);
  671. //Robots themselves
  672. prepare_robot_mem();
  673. if(count) {
  674. for(t1=1;t1<=count;t1++) {
  675. //Copy robot t1
  676. fwrite(&robots[t1],sizeof(Robot),1,fp);
  677. fwrite(&robot_mem[robots[t1].program_location],1,
  678. robots[t1].program_length,fp);
  679. }
  680. }
  681. //Scroll count
  682. count=0;
  683. for(t1=1;t1<NUM_SCROLLS;t1++)
  684. if(scrolls[t1].used) count=t1;
  685. fputc(count,fp);
  686. //Scrolls themselves
  687. if(count) {
  688. for(t1=1;t1<=count;t1++) {
  689. //Copy scroll t1
  690. fwrite(&scrolls[t1],sizeof(Scroll),1,fp);
  691. fwrite(&robot_mem[scrolls[t1].mesg_location],1,
  692. scrolls[t1].mesg_size,fp);
  693. }
  694. }
  695. //Sensor count
  696. count=0;
  697. for(t1=1;t1<NUM_SENSORS;t1++)
  698. if(sensors[t1].used) count=t1;
  699. fputc(count,fp);
  700. //Sensors themselves
  701. if(count) {
  702. for(t1=1;t1<=count;t1++) //Copy sensor t1
  703. fwrite(&sensors[t1],sizeof(Sensor),1,fp);
  704. }
  705. //All saved! Return if tempfile...
  706. if(board_where[id]==W_TEMPFILE) {
  707. fclose(fp);
  708. return 0;
  709. }
  710. //...otherwise copy to EMS.
  711. //Copy a page at a time...
  712. t1=board_offsets[id].EMS.page;//Current page
  713. copied=0;//Copied nothing so far.
  714. fseek(fp,0,SEEK_SET);//Return to start of file
  715. do {
  716. //Map page
  717. map_page_EMS(board_offsets[id].EMS.handle,0,t1);
  718. //Copy a page, or part of a page if that's all that's left.
  719. tcpy=16384;
  720. if((copied+tcpy)>board_sizes[id]) //Too much...
  721. tcpy=(int)(board_sizes[id]-(unsigned long)copied);
  722. //Now copy it...
  723. fread(page_frame_EMS,tcpy,1,fp);
  724. //...increment status...
  725. copied+=tcpy;
  726. t1++;//Page
  727. //...loop.
  728. } while(copied<board_sizes[id]);
  729. //All copied! Clear file and exit
  730. fclose(fp);
  731. unlink(&board_filenames[id*FILENAME_SIZE]);
  732. board_filenames[id*FILENAME_SIZE]=0;
  733. return 0;
  734. }
  735. return 3;
  736. }
  737. //Loads current board from slot given. Space must already be filled.
  738. //board_sizes and other board variables MUST be accurate. Returns non-0
  739. //if out of room in memory and on disk for working, or for misc. errors.
  740. //Returns 4 if out of robot memory. (This is usually due to a large current
  741. //robot/scroll or a large global robot) Does not affect current object or
  742. //global robot.
  743. char grab_current(unsigned char id) {
  744. unsigned char far *ptr;//Where to grab from for mem/ems
  745. unsigned char far *old_ptr;//Saves old ems allocated ptr
  746. FILE *fp;//Where to grab from for tempfile (or ems if pressed for space)
  747. int t1,count,tcpy;
  748. unsigned int oldsize,newsize,oldloc;//For robot/scroll allocation
  749. long copied;
  750. //Space of proper size already assumed to be allocated. This function
  751. //has two major parts- The code for memory/ems, and the code for temp
  752. //files.
  753. //Switch according to storage type.
  754. switch(board_where[id]) {
  755. case W_EMS:
  756. //If EMS, allocate a temporary area to allow storage in conventional
  757. //memory for now, then copy to robots/etc. later. If not possible,
  758. //we use a temp file and then copy THAT to mem. later. If no room
  759. //for a temp file... return error.
  760. if(board_where[id]==W_EMS) {
  761. //Allocate a temporary area
  762. ptr=(unsigned char far *)farmalloc(board_sizes[id]);
  763. if(ptr==NULL) {
  764. //No room in mem- try a temp file
  765. //Make temporary filename
  766. str_cpy(&board_filenames[id*FILENAME_SIZE],"~MZTMPXXXXXX");
  767. mktemp(&board_filenames[id*FILENAME_SIZE]);
  768. //Open..
  769. fp=fopen(&board_filenames[id*FILENAME_SIZE],"wb+");
  770. if(fp==NULL) {
  771. //Error
  772. board_filenames[id*FILENAME_SIZE]=0;
  773. return 1;
  774. }
  775. //Try to make room
  776. fseek(fp,board_sizes[id],SEEK_SET);
  777. if(ftell(fp)!=(board_sizes[id])) {
  778. //Not enough room on hard drive
  779. fclose(fp);
  780. unlink(&board_filenames[id*FILENAME_SIZE]);
  781. board_filenames[id*FILENAME_SIZE]=0;
  782. return 2;
  783. }
  784. //Room made! Solidify.
  785. fseek(fp,-1,SEEK_CUR);
  786. fputc(0,fp);
  787. //Load from EMS
  788. //Copy a page at a time...
  789. t1=board_offsets[id].EMS.page;//Current page
  790. copied=0;//Copied nothing so far.
  791. fseek(fp,0,SEEK_SET);//Return to start of file
  792. do {
  793. //Map page
  794. map_page_EMS(board_offsets[id].EMS.handle,0,t1);
  795. //Copy a page, or part of a page if that's all that's left.
  796. tcpy=16384;
  797. if((copied+tcpy)>board_sizes[id]) //Too much...
  798. tcpy=(int)(board_sizes[id]-(unsigned long)copied);
  799. //Now copy it...
  800. fwrite(page_frame_EMS,tcpy,1,fp);
  801. //...increment status...
  802. copied+=tcpy;
  803. t1++;//Page
  804. //...loop.
  805. } while(copied<board_sizes[id]);
  806. //All copied! Close file and jump to tempfile code
  807. fclose(fp);
  808. goto grab_tempfile;
  809. }
  810. //Allocated. Copy from EMS to memory first...
  811. old_ptr=ptr;
  812. //Copy a page at a time...
  813. t1=board_offsets[id].EMS.page;//Current page
  814. copied=0;//Copied nothing so far.
  815. do {
  816. //Map page
  817. map_page_EMS(board_offsets[id].EMS.handle,0,t1);
  818. //Copy a page, or part of a page if that's all that's left.
  819. tcpy=16384;
  820. if((copied+tcpy)>board_sizes[id]) //Too much...
  821. tcpy=(int)(board_sizes[id]-(unsigned long)copied);
  822. //Now copy it...
  823. mem_cpy((char far *)ptr,page_frame_EMS,tcpy);
  824. //...increment status...
  825. copied+=tcpy;
  826. ptr+=tcpy;
  827. normalize(ptr);
  828. t1++;//Page
  829. //...loop.
  830. } while(copied<board_sizes[id]);
  831. //All copied! Now do regular memory code.
  832. ptr=old_ptr;//Restore pointer
  833. goto grab_memory;
  834. }
  835. case W_MEMORY:
  836. ptr=(unsigned char far *)board_offsets[id].mem;
  837. grab_memory:
  838. //Clear overlay
  839. overlay_mode=0;
  840. for(t1=0;t1<10000;t1++) {
  841. level_id[t1]=level_under_id[t1]=level_param[t1]=level_under_param[t1]=0;
  842. level_color[t1]=level_under_color[t1]=overlay_color[t1]=7;
  843. overlay[t1]=32;
  844. }
  845. max_bsiz_mode=*(ptr++);
  846. convert_max_bsiz_mode();
  847. //Load RLE2 stuff
  848. if(*ptr==0) {
  849. //Overlay
  850. ptr++;
  851. overlay_mode=*(ptr++);
  852. ptr+=RLE2_read((unsigned char far *)ptr,overlay);
  853. normalize(ptr);
  854. ptr+=RLE2_read((unsigned char far *)ptr,overlay_color);
  855. normalize(ptr);
  856. }
  857. ptr+=RLE2_read((unsigned char far *)ptr,level_id);
  858. normalize(ptr);
  859. ptr+=RLE2_read((unsigned char far *)ptr,level_color);
  860. normalize(ptr);
  861. ptr+=RLE2_read((unsigned char far *)ptr,level_param);
  862. normalize(ptr);
  863. ptr+=RLE2_read((unsigned char far *)ptr,level_under_id);
  864. normalize(ptr);
  865. ptr+=RLE2_read((unsigned char far *)ptr,level_under_color);
  866. normalize(ptr);
  867. ptr+=RLE2_read((unsigned char far *)ptr,level_under_param);
  868. normalize(ptr);
  869. //Read variables- Due to the way they are stored in DATA.ASM,
  870. //they can all be read as a series of 210 bytes starting at
  871. //mod_playing. Appropriate code to read each individually is
  872. //also included in case this ever fails. To use the individual
  873. //code, define the symbol SAVE_INDIVIDUAL.
  874. #ifndef SAVE_INDIVIDUAL
  875. mem_cpy(mod_playing,(char far *)ptr,207+FILENAME_SIZE);
  876. ptr+=207+FILENAME_SIZE;
  877. #else
  878. mem_cpy(mod_playing,(char far *)ptr,FILENAME_SIZE);
  879. ptr+=FILENAME_SIZE;
  880. viewport_x=*(ptr++);
  881. viewport_y=*(ptr++);
  882. viewport_xsiz=*(ptr++);
  883. viewport_ysiz=*(ptr++);
  884. can_shoot=*(ptr++);
  885. can_bomb=*(ptr++);
  886. fire_burn_brown=*(ptr++);
  887. fire_burn_space=*(ptr++);
  888. fire_burn_fakes=*(ptr++);
  889. fire_burn_trees=*(ptr++);
  890. explosions_leave=*(ptr++);
  891. save_mode=*(ptr++);
  892. forest_becomes=*(ptr++);
  893. collect_bombs=*(ptr++);
  894. fire_burns=*(ptr++);
  895. mem_cpy((char far *)board_dir,(char far *)ptr,4);
  896. ptr+=4;
  897. restart_if_zapped=*(ptr++);
  898. mem_cpy((char far *)&time_limit,(char far *)ptr,2);
  899. ptr+=2;
  900. last_key=*(ptr++);
  901. mem_cpy((char far *)&num_input,(char far *)ptr,2);
  902. ptr+=2;
  903. input_size=*(ptr++);
  904. mem_cpy(input_string,(char far *)ptr,81);
  905. ptr+=81;
  906. player_last_dir=*(ptr++);
  907. mem_cpy(bottom_mesg,(char far *)ptr,81);
  908. ptr+=81;
  909. b_mesg_timer=*(ptr++);
  910. lazwall_start=*(ptr++);
  911. b_mesg_row=*(ptr++);
  912. b_mesg_col=*(ptr++);
  913. mem_cpy((char far *)&scroll_x,(char far *)ptr,2);
  914. ptr+=2;
  915. mem_cpy((char far *)&scroll_y,(char far *)ptr,2);
  916. ptr+=2;
  917. mem_cpy((char far *)&locked_x,(char far *)ptr,2);
  918. ptr+=2;
  919. mem_cpy((char far *)&locked_y,(char far *)ptr,2);
  920. ptr+=2;
  921. player_ns_locked=*(ptr++);
  922. player_ew_locked=*(ptr++);
  923. player_attack_locked=*(ptr++);
  924. volume=*(ptr++);
  925. volume_inc=*(ptr++);
  926. volume_target=*(ptr++);
  927. #endif
  928. //Before doing robots/scrolls, make sure all but #0 and global
  929. //are allocated to the minimum. Also clear all sensors.
  930. prepare_robot_mem();
  931. for(t1=1;t1<NUM_ROBOTS;t1++)
  932. clear_robot(t1);
  933. for(t1=1;t1<NUM_SCROLLS;t1++)
  934. clear_scroll(t1);
  935. for(t1=1;t1<NUM_SENSORS;t1++)
  936. clear_sensor(t1);
  937. //Robots- get count, then load 'em up. All must be allocated
  938. //first. A mis-allocation frees any ems memory and returns error
  939. //code #4.
  940. count=*(ptr++);
  941. if(count) {
  942. for(t1=1;t1<=count;t1++) {
  943. //Read robot t1
  944. oldsize=robots[t1].program_length;
  945. oldloc=robots[t1].program_location;
  946. mem_cpy((char far *)&robots[t1],(char far *)ptr,sizeof(Robot));
  947. ptr+=sizeof(Robot);
  948. normalize(ptr);
  949. newsize=robots[t1].program_length;
  950. robots[t1].program_length=oldsize;
  951. robots[t1].program_location=oldloc;
  952. if(reallocate_robot_mem(T_ROBOT,t1,newsize)) {
  953. //Error in allocation.
  954. //EMS- delete temp area
  955. if(board_where[id]==W_EMS) farfree(ptr);
  956. return 4;
  957. }
  958. //Load in robot
  959. mem_cpy((char far *)&robot_mem[robots[t1].program_location],
  960. (char far *)ptr,robots[t1].program_length);
  961. ptr+=robots[t1].program_length;
  962. }
  963. }
  964. //Scroll count
  965. count=*(ptr++);
  966. if(count) {
  967. for(t1=1;t1<=count;t1++) {
  968. //Read scroll t1
  969. oldsize=scrolls[t1].mesg_size;
  970. oldloc=scrolls[t1].mesg_location;
  971. mem_cpy((char far *)&scrolls[t1],(char far *)ptr,sizeof(Scroll));
  972. ptr+=sizeof(Scroll);
  973. normalize(ptr);
  974. newsize=scrolls[t1].mesg_size;
  975. scrolls[t1].mesg_size=oldsize;
  976. scrolls[t1].mesg_location=oldloc;
  977. if(reallocate_robot_mem(T_SCROLL,t1,newsize)) {
  978. //Error in allocation.
  979. //EMS- delete temp area
  980. if(board_where[id]==W_EMS) farfree(ptr);
  981. return 4;
  982. }
  983. mem_cpy((char far *)&robot_mem[scrolls[t1].mesg_location],
  984. (char far *)ptr,scrolls[t1].mesg_size);
  985. ptr+=scrolls[t1].mesg_size;
  986. }
  987. }
  988. //Sensor count
  989. count=*(ptr++);
  990. if(count) {
  991. for(t1=1;t1<=count;t1++) {
  992. //Read sensor t1
  993. mem_cpy((char far *)&sensors[t1],(char far *)ptr,sizeof(Sensor));
  994. ptr+=sizeof(Sensor);
  995. }
  996. }
  997. //All grabbed! Return if memory...
  998. if(board_where[id]==W_MEMORY) return 0;
  999. //...otherwise free EMS temp memory.
  1000. farfree(old_ptr);
  1001. return 0;
  1002. case W_TEMPFILE:
  1003. grab_tempfile:
  1004. //This is loading from a tempfile, sometimes a subfuction of loading
  1005. //from EMS.
  1006. //Open file...
  1007. fp=fopen(&board_filenames[id*FILENAME_SIZE],"rb");
  1008. if(fp==NULL) {
  1009. if(board_where[id]==W_EMS) board_filenames[id*FILENAME_SIZE]=0;
  1010. return 1;//Error...
  1011. }
  1012. //...and begin input!
  1013. //Clear overlay
  1014. overlay_mode=0;
  1015. for(t1=0;t1<10000;t1++) {
  1016. level_id[t1]=level_under_id[t1]=level_param[t1]=level_under_param[t1]=0;
  1017. level_color[t1]=level_under_color[t1]=overlay_color[t1]=7;
  1018. overlay[t1]=32;
  1019. }
  1020. max_bsiz_mode=fgetc(fp);
  1021. convert_max_bsiz_mode();
  1022. //Overlay
  1023. if(fgetc(fp)==0) {
  1024. overlay_mode=fgetc(fp);
  1025. RLE2_load(fp,overlay);
  1026. RLE2_load(fp,overlay_color);
  1027. }
  1028. else fseek(fp,-1,SEEK_CUR);
  1029. //load RLE2 stuff
  1030. RLE2_load(fp,level_id);
  1031. RLE2_load(fp,level_color);
  1032. RLE2_load(fp,level_param);
  1033. RLE2_load(fp,level_under_id);
  1034. RLE2_load(fp,level_under_color);
  1035. RLE2_load(fp,level_under_param);
  1036. //Load variables- Due to the way they are stored in DATA.ASM,
  1037. //they can all be loaded as a series of 210 bytes starting at
  1038. //mod_playing. Appropriate code to load each individually is
  1039. //also included in case this ever fails. To use the individual
  1040. //code, define the symbol SAVE_INDIVIDUAL.
  1041. #ifndef SAVE_INDIVIDUAL
  1042. fread(mod_playing,1,207+FILENAME_SIZE,fp);
  1043. #else
  1044. fread(mod_playing,1,FILENAME_SIZE,fp);
  1045. viewport_x=fgetc(fp);
  1046. viewport_y=fgetc(fp);
  1047. viewport_xsiz=fgetc(fp);
  1048. viewport_ysiz=fgetc(fp);
  1049. can_shoot=fgetc(fp);
  1050. can_bomb=fgetc(fp);
  1051. fire_burn_brown=fgetc(fp);
  1052. fire_burn_space=fgetc(fp);
  1053. fire_burn_fakes=fgetc(fp);
  1054. fire_burn_trees=fgetc(fp);
  1055. explosions_leave=fgetc(fp);
  1056. save_mode=fgetc(fp);
  1057. forest_becomes=fgetc(fp);
  1058. collect_bombs=fgetc(fp);
  1059. fire_burns=fgetc(fp);
  1060. fread(board_dir,1,4,fp);
  1061. restart_if_zapped=fgetc(fp);
  1062. fread(&time_limit,2,1,fp);
  1063. last_key=fgetc(fp);
  1064. fread(&num_input,2,1,fp);
  1065. input_size=fgetc(fp);
  1066. fread(input_string,1,81,fp);
  1067. player_last_dir=fgetc(fp);
  1068. fread(bottom_mesg,1,81,fp);
  1069. b_mesg_timer=fgetc(fp);
  1070. lazwall_start=fgetc(fp);
  1071. b_mesg_row=fgetc(fp);
  1072. b_mesg_col=fgetc(fp);
  1073. fread(&scroll_x,2,1,fp);
  1074. fread(&scroll_y,2,1,fp);
  1075. fread(&locked_x,2,1,fp);
  1076. fread(&locked_y,2,1,fp);
  1077. player_ns_locked=fgetc(fp);
  1078. player_ew_locked=fgetc(fp);
  1079. player_attack_locked=fgetc(fp);
  1080. volume=fgetc(fp);
  1081. volume_inc=fgetc(fp);
  1082. volume_target=fgetc(fp);
  1083. #endif
  1084. //Before doing robots/scrolls, make sure all but #0 and global
  1085. //are allocated to the minimum. Also clear all sensors.
  1086. prepare_robot_mem();
  1087. for(t1=1;t1<NUM_ROBOTS;t1++)
  1088. clear_robot(t1);
  1089. for(t1=1;t1<NUM_SCROLLS;t1++)
  1090. clear_scroll(t1);
  1091. for(t1=1;t1<NUM_SENSORS;t1++)
  1092. clear_sensor(t1);
  1093. //Robots- get count, then load 'em up. All must be allocated
  1094. //first. A mis-allocation frees any ems memory and returns error
  1095. //code #4.
  1096. count=fgetc(fp);
  1097. if(count) {
  1098. for(t1=1;t1<=count;t1++) {
  1099. //Read robot t1
  1100. oldsize=robots[t1].program_length;
  1101. oldloc=robots[t1].program_location;
  1102. fread(&robots[t1],sizeof(Robot),1,fp);
  1103. newsize=robots[t1].program_length;
  1104. robots[t1].program_length=oldsize;
  1105. robots[t1].program_location=oldloc;
  1106. if(reallocate_robot_mem(T_ROBOT,t1,newsize)) {
  1107. //Error in allocation.
  1108. fclose(fp);
  1109. //EMS- delete temp file
  1110. if(board_where[id]==W_EMS) {
  1111. unlink(&board_filenames[id*FILENAME_SIZE]);
  1112. board_filenames[id*FILENAME_SIZE]=0;
  1113. }
  1114. return 4;
  1115. }
  1116. //Load in robot
  1117. fread(&robot_mem[robots[t1].program_location],1,
  1118. robots[t1].program_length,fp);
  1119. }
  1120. }
  1121. //Scroll count
  1122. count=fgetc(fp);
  1123. if(count) {
  1124. for(t1=1;t1<=count;t1++) {
  1125. //Read scroll t1
  1126. oldsize=scrolls[t1].mesg_size;
  1127. oldloc=scrolls[t1].mesg_location;
  1128. fread(&scrolls[t1],sizeof(Scroll),1,fp);
  1129. newsize=scrolls[t1].mesg_size;
  1130. scrolls[t1].mesg_size=oldsize;
  1131. scrolls[t1].mesg_location=oldloc;
  1132. if(reallocate_robot_mem(T_SCROLL,t1,newsize)) {
  1133. //Error in allocation.
  1134. fclose(fp);
  1135. //EMS- delete temp file
  1136. if(board_where[id]==W_EMS) {
  1137. unlink(&board_filenames[id*FILENAME_SIZE]);
  1138. board_filenames[id*FILENAME_SIZE]=0;
  1139. }
  1140. return 4;
  1141. }
  1142. fread(&robot_mem[scrolls[t1].mesg_location],1,
  1143. scrolls[t1].mesg_size,fp);
  1144. }
  1145. }
  1146. //Sensor count
  1147. count=fgetc(fp);
  1148. if(count) {
  1149. for(t1=1;t1<=count;t1++) //Read sensor t1
  1150. fread(&sensors[t1],sizeof(Sensor),1,fp);
  1151. }
  1152. //All loaded! Return if tempfile...
  1153. fclose(fp);
  1154. if(board_where[id]==W_TEMPFILE) return 0;
  1155. //...otherwise erase EMS file.
  1156. unlink(&board_filenames[id*FILENAME_SIZE]);
  1157. board_filenames[id*FILENAME_SIZE]=0;
  1158. return 0;
  1159. }
  1160. return 3;
  1161. }
  1162. //Loads OR saves board of given id to/from an already open file. Space
  1163. //must already be allocated and board_sizes, etc must be set properly.
  1164. //All read characters are xor'd with xor_with, although not if it is 0,
  1165. //to save time for the same result. Returns non-0 for misc. errors.
  1166. //Set loading to non-0 to load FROM file, 0 to save TO file.
  1167. char disk_board(unsigned char id,FILE *fp,char loading,
  1168. unsigned char xor_with) {
  1169. unsigned int siz,cpg1,buffsize;
  1170. long copied=0,tmp;
  1171. char far *ptr;
  1172. FILE *destfp;
  1173. //Switch according to storage type.
  1174. switch(board_where[id]) {
  1175. case W_MEMORY:
  1176. //Simply do a fread then a mem_xor. Must be read in chunks in case
  1177. //size is larger than an unsigned int.
  1178. ptr=(char far *)board_offsets[id].mem;
  1179. do {
  1180. siz=(unsigned int)(board_sizes[id]-copied);
  1181. if((board_sizes[id]-copied)>32768U) siz=32768U;
  1182. if(!loading)//If saving, must xor first, but later xor again
  1183. //to restore memory copy to normal.
  1184. if(xor_with) mem_xor(ptr,siz,xor_with);
  1185. if(loading) fread(ptr,siz,1,fp);//LOAD
  1186. else fwrite(ptr,siz,1,fp);//SAVE
  1187. if(xor_with) mem_xor(ptr,siz,xor_with);
  1188. copied+=siz;
  1189. ptr+=siz;
  1190. } while(copied<board_sizes[id]);
  1191. //Done!
  1192. return 0;
  1193. case W_EMS:
  1194. //Simply do a fread then a mem_xor. Read in chunks of one page.
  1195. cpg1=board_offsets[id].EMS.page;
  1196. do {
  1197. map_page_EMS(board_offsets[id].EMS.handle,0,cpg1);
  1198. siz=(unsigned int)(board_sizes[id]-copied);
  1199. if((board_sizes[id]-copied)>16384) siz=16384;
  1200. if(!loading)//If saving, must xor first, but later xor again
  1201. //to restore memory copy to normal.
  1202. if(xor_with) mem_xor(page_frame_EMS,siz,xor_with);
  1203. if(loading) fread(page_frame_EMS,siz,1,fp);//LOAD
  1204. else fwrite(page_frame_EMS,siz,1,fp);//SAVE
  1205. if(xor_with) mem_xor(page_frame_EMS,siz,xor_with);
  1206. copied+=siz;
  1207. cpg1++;
  1208. } while(copied<board_sizes[id]);
  1209. //Done!
  1210. return 0;
  1211. case W_TEMPFILE:
  1212. //First, verify file can be opened...
  1213. destfp=fopen(&board_filenames[id*FILENAME_SIZE],"rb+");
  1214. if(destfp==NULL) return 1;
  1215. //This one is easier to program but slower. Allocates a buffer
  1216. //in conventional memory as large as possible (up to 32k) then
  1217. //buffers from fp to dest file, xor'ing if required. If buffer
  1218. //cannot be allocated, does it directly. (IE one byte at a time)
  1219. copied=farcoreleft();
  1220. if(copied>32768U) buffsize=32768U;
  1221. else buffsize=(unsigned int)copied;
  1222. if(buffsize>=128) {
  1223. //Allocate buffer
  1224. ptr=(char far *)farmalloc(buffsize);
  1225. if(ptr==NULL) //Huh? Ok we'll do singles..
  1226. goto transfer_bytewise;
  1227. //Do transferring
  1228. copied=0;
  1229. do {
  1230. //Pick size
  1231. siz=((unsigned int)(board_sizes[id]-copied));
  1232. if(board_sizes[id]>buffsize) siz=buffsize;
  1233. //Read
  1234. if(loading) fread(ptr,siz,1,fp);
  1235. else fread(ptr,siz,1,destfp);
  1236. //XOR
  1237. if(xor_with) mem_xor(ptr,siz,xor_with);
  1238. //Write
  1239. if(loading) fwrite(ptr,siz,1,destfp);
  1240. else fwrite(ptr,siz,1,fp);
  1241. //Update variables
  1242. copied+=siz;
  1243. } while(copied<board_sizes[id]);
  1244. //Done! Close file and deallocate buffer.
  1245. farfree(ptr);
  1246. fclose(destfp);
  1247. return 0;
  1248. }
  1249. transfer_bytewise:
  1250. //Couldn't allocate memory buffer. Do transfer byte by byte...
  1251. tmp=board_sizes[id];
  1252. if(xor_with) {
  1253. for(;copied<tmp;copied++) {//Read, XOR, and write
  1254. if(loading) fputc(fgetc(fp)^xor_with,destfp);
  1255. else fputc(fgetc(destfp)^xor_with,fp);
  1256. }
  1257. }
  1258. else {
  1259. for(;copied<tmp;copied++) {//Read, XOR, and write
  1260. if(loading) fputc(fgetc(fp),destfp);
  1261. else fputc(fgetc(destfp),fp);
  1262. }
  1263. }
  1264. //Done! Close file.
  1265. fclose(destfp);
  1266. return 0;
  1267. }
  1268. //Misc. error
  1269. return 2;
  1270. }
  1271. //Attempts to clear up conventional memory by moving all boards in
  1272. //conventional memory to either EMS or disk. If there is no more room on
  1273. //disk/EMS, then boards are no longer moved. Runs a meter.
  1274. void free_up_board_memory(void) {
  1275. int t1,t2,nmb=0,nb_done=0;
  1276. unsigned char far *tmp_ptr;
  1277. unsigned char far *ptr;
  1278. long copied;
  1279. unsigned int tcpy;
  1280. char temp=curr_rmem_status;
  1281. FILE *fp;
  1282. //Count boards
  1283. for(t1=0;t1<NUM_BOARDS;t1++) {
  1284. if(board_where[t1]==W_MEMORY) nmb++;
  1285. }
  1286. if(nmb==0) return;//None to swap!
  1287. save_screen(current_pg_seg);
  1288. meter("Swapping boards to disk/EMS...",current_pg_seg,nb_done,nmb);
  1289. //Go through the boards, picking out those in conventional memory...
  1290. for(t1=0;t1<NUM_BOARDS;t1++) {
  1291. if(board_where[t1]==W_MEMORY) {
  1292. //Ok, board #t1 is in conventional memory. Now we need to try to
  1293. //allocate a new area for it in anything BUT conventional memory.
  1294. //First, since the allocate function destroys the allocation
  1295. //variables, we need to save board_offset. (board_where is obvious
  1296. //and board_sizes/board_filenames are not affected)
  1297. tmp_ptr=board_offsets[t1].mem;
  1298. if(allocate_board_space(board_sizes[t1],t1,0)) {
  1299. //No room. Restore variables and return.
  1300. board_offsets[t1].mem=tmp_ptr;
  1301. board_where[t1]=W_MEMORY;
  1302. restore_screen(current_pg_seg);
  1303. return;
  1304. }
  1305. //Aha, new area allocated. Switch for EMS or disk.
  1306. switch(board_where[t1]) {
  1307. case W_EMS:
  1308. //Copy to EMS from tmp_ptr for board_sizes[t1] bytes.
  1309. //Copy a page at a time...
  1310. t2=board_offsets[t1].EMS.page;//Current page
  1311. copied=0;//Copied nothing so far.
  1312. ptr=tmp_ptr;//Use a copy of the ptr so we save it
  1313. do {
  1314. //Map page
  1315. map_page_EMS(board_offsets[t1].EMS.handle,0,t2);
  1316. //Copy a page, or part of a page if that's all that's left.
  1317. tcpy=16384;
  1318. if((copied+tcpy)>board_sizes[t1]) //Too much...
  1319. tcpy=(int)(board_sizes[t1]-(unsigned long)copied);
  1320. //Now copy it...
  1321. mem_cpy(page_frame_EMS,(char far *)ptr,tcpy);
  1322. //...increment status...
  1323. copied+=tcpy;
  1324. ptr+=tcpy;
  1325. t2++;//Page
  1326. //...loop.
  1327. } while(copied<board_sizes[t1]);
  1328. //All copied! Free old memory for board and we're done.
  1329. farfree(tmp_ptr);
  1330. break;
  1331. case W_TEMPFILE:
  1332. //We're saving memory to a disk file. REAL simple.
  1333. fp=fopen(&board_filenames[t1*FILENAME_SIZE],"rb+");
  1334. if(fp==NULL) {
  1335. //Not gonna work.
  1336. board_offsets[t1].mem=tmp_ptr;
  1337. board_where[t1]=W_MEMORY;
  1338. board_filenames[t1*FILENAME_SIZE]=0;
  1339. break;
  1340. }
  1341. //Do in chunks of 32768 bytes.
  1342. ptr=tmp_ptr;
  1343. copied=0;
  1344. do {
  1345. tcpy=(unsigned int)board_sizes[t1];
  1346. if(board_sizes[t1]>32768U) tcpy=32768U;
  1347. fwrite(ptr,tcpy,1,fp);//SAVE
  1348. copied+=tcpy;
  1349. ptr+=tcpy;
  1350. } while(copied<board_sizes[t1]);
  1351. //Done. Close file and free memory.
  1352. fclose(fp);
  1353. farfree(tmp_ptr);
  1354. break;
  1355. default:
  1356. //Error.
  1357. restore_screen(current_pg_seg);
  1358. error("Error accessing boards",2,20,current_pg_seg,0x0701);
  1359. }
  1360. //All done with THIS board.
  1361. meter_interior(current_pg_seg,++nb_done,nmb);
  1362. }
  1363. //Loop to next board.
  1364. }
  1365. restore_screen(current_pg_seg);
  1366. //All done totally.
  1367. prepare_robot_mem(temp);
  1368. }
  1369. void convert_max_bsiz_mode(void) {
  1370. switch(max_bsiz_mode) {
  1371. case 0:
  1372. max_bxsiz=60;
  1373. max_bysiz=166;
  1374. break;
  1375. case 1:
  1376. max_bxsiz=80;
  1377. max_bysiz=125;
  1378. break;
  1379. default:
  1380. max_bxsiz=100;
  1381. max_bysiz=100;
  1382. break;
  1383. case 3:
  1384. max_bxsiz=200;
  1385. max_bysiz=50;
  1386. break;
  1387. case 4:
  1388. max_bxsiz=400;
  1389. max_bysiz=25;
  1390. break;
  1391. }
  1392. }