ezboard.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. /* EZ-Board management code */
  22. #include "timer.h"
  23. #include "scrdisp.h"
  24. #include "string.h"
  25. #include "beep.h"
  26. #include "mod.h"
  27. #include "roballoc.h"
  28. #include "egacode.h"
  29. #include "error.h"
  30. #include "boardmem.h"
  31. #include "data.h"
  32. #include "const.h"
  33. #include "struct.h"
  34. #include "ezboard.h"
  35. #include "palette.h"
  36. #include "game.h"
  37. #include "counter.h"
  38. #include "sprite.h"
  39. #include "mstring.h"
  40. void set_built_in_messages(int param);
  41. int built_in_messages;
  42. // The way boards work-
  43. // There is the current board, in memory, and all other boards, in memory.
  44. // They are stored in conventional, EMS, or temp files. The current board
  45. // has a code of W_CURRENT and NO allocated memory.
  46. // On startup, before any other board-related code, call board_setup. Since
  47. // nothing is allocated, it simply clears all the arrays for use and sets
  48. // up the current board.
  49. void board_setup(void) {
  50. int t1;
  51. //Clear all board-related arrays
  52. for(t1=0;t1<NUM_BOARDS;t1++) {
  53. board_where[t1]=W_NOWHERE;//Doesn't exist
  54. board_offsets[t1].offset=0;//No offset
  55. board_filenames[t1*FILENAME_SIZE]=0;//No filename
  56. board_list[t1*BOARD_NAME_SIZE]=0;//No name
  57. board_sizes[t1]=0;//No size
  58. }
  59. //Select first as active
  60. board_where[curr_board=0]=W_CURRENT;//Both methods of determining current
  61. //Still has no offest, filename, size, or name. Offset, filename, and
  62. //size only relate to storage in memory, and name is undefined yet.
  63. //Done.
  64. }
  65. // Now boards are in the required normalized setup format. :)
  66. // Access the current board in any way normally allowed. To store the current
  67. // board into memory, use this function. It will leave the board in current
  68. // memory as well. It stores it in id curr_board. Note that changing to a new
  69. // current board is important, since the board_where[] array no longer has
  70. // W_CURRENT for the "current" board. Error message produced if run when
  71. // a board is already stored there. Error message uses current_pg_seg.
  72. void store_current(void) {
  73. //Get size of current board
  74. long size=size_of_current_board();
  75. //Are we already stored? (err- exit or help only)
  76. if((board_where[curr_board]!=W_CURRENT)&&
  77. (board_where[curr_board]!=W_NOWHERE))
  78. error("Attempt to overwrite board in storage",2,20,current_pg_seg,
  79. 0x0101);
  80. //Ok. Allocate space for new board- check for error
  81. if(allocate_board_space(size,curr_board))
  82. error("Out of memory and/or disk space",2,4,
  83. current_pg_seg,0x0201);
  84. board_sizes[curr_board]=size;
  85. //Space allocated.
  86. //Now we must store!
  87. if(store_current(curr_board))
  88. error("Error storing current board",2,20,current_pg_seg,0x0301);
  89. //Stored!
  90. }
  91. // Now we might want to clear the current board... (not neccesary if the
  92. // NEXT function is going to be used...) This also sets id to be the current
  93. // board. If id has space allocated to it, the board is deleted. This is used
  94. // usually to add a board. Rundown- Clear current board and select id as
  95. // current, using the newly cleared board. If adopt_settings is on, the
  96. // board_info settings from the prev. current board are kept. (viewport
  97. // and board size and most other local settings)
  98. void clear_current_and_select(unsigned char id,char adopt_settings) {
  99. //If board to become current exists, delete it (works fine if it
  100. //is already current);
  101. if(board_where[id]!=W_NOWHERE)
  102. deallocate_board_space(id);
  103. //Select as current and clear size
  104. board_where[curr_board=id]=W_CURRENT;
  105. board_sizes[id]=0;
  106. //Clear board and settings
  107. clear_current(adopt_settings);
  108. //Done!
  109. }
  110. // We also need to be able to load ANOTHER board into current. This will,
  111. // of course, only work if the current board is already stored here. Since
  112. // we can't tell for sure if the current board has been stored, this is not
  113. // checked for. The current board is simply overwritten by the board
  114. // specified by id. Errors include loading a nonexistant board and running
  115. // out of robot memory. (due to current and global objects) This fully
  116. // selects the board as current, including setting board_where[], curr_board,
  117. // and deallocating any space allocated to the board. This function also
  118. // searches for the player and sets player_x and player_y appropriately.
  119. void select_current(unsigned char id) {
  120. int t1;
  121. char temp[FILENAME_SIZE];
  122. //Save current mod...
  123. str_cpy(temp,real_mod_playing);
  124. //Does board exist?
  125. if(board_where[id]==W_NOWHERE)
  126. error("Attempt to load nonexistent board",2,20,current_pg_seg,0x0401);
  127. //If board is current, we're done
  128. if(board_where[id]==W_CURRENT) return;
  129. //Otherwise, load it in...
  130. if((t1=grab_current(id))!=0) {
  131. //Out of robot memory or other error?
  132. if(t1==4) //Out of robot memory
  133. error("Out of robot memory",1,21,current_pg_seg,0x0501);
  134. //They chose FAIL. We'll continue with robots possibly missing...
  135. else //Out of work space
  136. error("Out of memory and/or disk space",2,4,
  137. current_pg_seg,0x0202);
  138. }
  139. //Loaded. Deallocate...
  140. deallocate_board_space(id);
  141. //...and select as current.
  142. board_where[curr_board=id]=W_CURRENT;
  143. board_sizes[id]=0;
  144. //Mods-
  145. if( str_cmp(mod_playing,"*") && str_cmp(mod_playing,temp) ) {
  146. //Different mod.
  147. if(mod_playing[0]==0) end_mod();
  148. else {
  149. str_cpy(temp,mod_playing);
  150. load_mod(temp);
  151. volume_mod(volume);
  152. }
  153. }
  154. find_player();
  155. //Done!
  156. }
  157. // Often we simply need to swap the current board with another. That is
  158. // taken care of simply using the next function.
  159. void swap_with(unsigned char id) {
  160. if(id==curr_board) return;//Why bother?
  161. store_current();
  162. select_current(id);
  163. }
  164. // This takes care of most important board functions. We also want to be
  165. // able to delete a board... This function deallocates all space assigned
  166. // to the noted board and marks it as empty. You cannot delete the title
  167. // screen, and deleting the current board first swaps to the title screen.
  168. void delete_board(unsigned char id) {
  169. //Simply deallocate board and clear name in board_list.
  170. if(id==0) return;//Can't delete title screen !
  171. if(id==curr_board) swap_with(0);
  172. if(board_where[id]==W_NOWHERE) return;//No board to clear
  173. //Deallocate...
  174. deallocate_board_space(id);
  175. //...set board_sizes...
  176. board_sizes[id]=0;
  177. //...and clear board_list.
  178. board_list[id*BOARD_NAME_SIZE]=0;
  179. //Done!
  180. }
  181. // It would also be nice to have a clear board function that just cleared
  182. // the current board without marking up the board list.. IE clear the floor
  183. // plan and settings, etc. The board_info settings are not cleared if
  184. // adopt_settings is on.
  185. void clear_current(char adopt_settings) {
  186. int t1;
  187. //Stop any mod IF not adopting settings
  188. if(!adopt_settings) {
  189. end_mod();
  190. mod_playing[0]=0;
  191. real_mod_playing[0]=0;
  192. }
  193. //Clear page
  194. for(t1=0;t1<10000;t1++) {
  195. level_id[t1]=level_under_id[t1]=level_param[t1]=level_under_param[t1]=0;
  196. level_color[t1]=level_under_color[t1]=overlay_color[t1]=7;
  197. overlay[t1]=32;
  198. }
  199. //Place player
  200. level_id[0]=127; level_color[0]=27; player_x=player_y=0;
  201. //Clear robots (NOT temp or global)
  202. for(t1=1;t1<NUM_ROBOTS;t1++) clear_robot(t1);
  203. //Clear scrolls (NOT temp)
  204. for(t1=1;t1<NUM_SCROLLS;t1++) clear_scroll(t1);
  205. //Clear sensors (NOT temp)
  206. for(t1=1;t1<NUM_SENSORS;t1++) clear_sensor(t1);
  207. //Clear settings
  208. if(!adopt_settings) {
  209. viewport_x=3; viewport_y=2;
  210. viewport_xsiz=74; viewport_ysiz=21;
  211. board_xsiz=100;
  212. board_ysiz=50;
  213. max_bsiz_mode=2;
  214. max_bxsiz=max_bysiz=100;
  215. can_shoot=can_bomb=fire_burn_space=fire_burn_fakes=fire_burn_trees=
  216. collect_bombs=1;
  217. fire_burn_brown=0;
  218. explosions_leave=EXPL_LEAVE_ASH;
  219. save_mode=CAN_SAVE;
  220. forest_becomes=FOREST_TO_FLOOR;
  221. fire_burns=FIRE_BURNS_LIMITED;
  222. }
  223. restart_if_zapped=time_limit=last_key=num_input=
  224. input_size=input_string[0]=bottom_mesg[0]=
  225. b_mesg_timer=scroll_x=scroll_y=0;
  226. player_last_dir=16;
  227. b_mesg_row=24;
  228. b_mesg_col=255;
  229. locked_x=locked_y=65535;
  230. overlay_mode=OVERLAY_OFF;
  231. //Clear board exits
  232. for(t1=0;t1<4;t1++) board_dir[t1]=NO_BOARD;
  233. //DON'T clear board title
  234. //Done!
  235. }
  236. // And of course, a clean sweep purging function is always helpful... IE one
  237. // that clears EVERYTHING. This one will do the trick.
  238. void clear_world(char clear_curr_file) {
  239. int t1;
  240. //Select board 0 as current
  241. deallocate_board_space(0);
  242. board_where[curr_board=0]=W_CURRENT;
  243. //Give it no name and no size
  244. board_list[0]=0;
  245. board_sizes[0]=0;
  246. //Deallocate all other boards
  247. for(t1=1;t1<NUM_BOARDS;t1++) delete_board(t1);
  248. //Clear current board
  249. clear_current(0);
  250. //Clear global robot
  251. clear_robot(GLOBAL_ROBOT);
  252. robots[GLOBAL_ROBOT].used=1;
  253. set_built_in_messages(1);
  254. //Clear all other global parameters
  255. mem_cpy((char far *)id_chars,(char far *)def_id_chars,324);
  256. mem_cpy((char far *)bullet_color,(char far *)def_id_chars+324,3);
  257. mem_cpy((char far *)id_dmg,(char far *)(def_id_chars+327),128);
  258. missile_color = 8;
  259. first_board=clear_on_exit=endgame_x=endgame_y=death_x=
  260. death_y=only_from_swap=protection_method=password[0]=
  261. enemy_hurt_enemy=0;
  262. if(clear_curr_file) curr_file[0]=0;
  263. endgame_board=NO_ENDGAME_BOARD;
  264. death_board=NO_DEATH_BOARD;
  265. lives_limit=99;
  266. health_limit=200;
  267. starting_lives=7;
  268. starting_health=100;
  269. for(t1=0;t1<NUM_STATUS_CNTRS*COUNTER_NAME_SIZE;t1++)
  270. status_shown_counters[t1]=0;
  271. edge_color=8;
  272. //Load in default mzx character set and palette
  273. ec_load_mzx();
  274. default_palette();
  275. //Done!
  276. }
  277. // This next function clears all game-related counters and other parameters.
  278. // Run this only in game-related situations- the affected parameters are only
  279. // used and saved in game situations, not editing situations. The parameters
  280. // are also not saved in any way in a MZX or MZB file, only SAV files.
  281. void clear_game_params(void) {
  282. int t1;
  283. //Clear all global game parameters
  284. for(t1=0;t1<NUM_KEYS;t1++) keys[t1]=NO_KEY;
  285. score=quicksave_file[0]=volume_inc=volume_target=player_ns_locked=
  286. player_ew_locked=player_attack_locked=blind_dur=firewalker_dur=
  287. freeze_time_dur=slow_time_dur=wind_dur=under_player_id=
  288. under_player_param=0;
  289. volume=255;
  290. scroll_color=15;
  291. saved_pl_color=29;
  292. under_player_color=7;
  293. mesg_edges=1;
  294. for(t1=0;t1<8;t1++)
  295. pl_saved_x[t1]=pl_saved_y[t1]=pl_saved_board[t1]=0;
  296. //Clear counters
  297. for(t1=0;t1<NUM_COUNTERS;t1++)
  298. counters[t1].counter_name[0]=counters[t1].counter_value=0;
  299. // Clear sprites. - Exo
  300. for(t1 = 0; t1 < 64; t1++)
  301. {
  302. sprites[t1].flags = 0;
  303. }
  304. // Clear strings. - Exo
  305. for(t1 = 0; t1 < 1024; t1++)
  306. {
  307. strings[0][t1] = '\0';
  308. }
  309. total_sprites = 0;
  310. sprite_y_order = 0;
  311. sprite_num = 0;
  312. //Set all nine built-in counters
  313. str_cpy(counters[0].counter_name,"GEMS");
  314. str_cpy(counters[1].counter_name,"AMMO");
  315. str_cpy(counters[2].counter_name,"LOBOMBS");
  316. str_cpy(counters[3].counter_name,"HIBOMBS");
  317. str_cpy(counters[4].counter_name,"COINS");
  318. str_cpy(counters[5].counter_name,"LIVES");
  319. str_cpy(counters[6].counter_name,"HEALTH");
  320. str_cpy(counters[7].counter_name,"TIME");
  321. str_cpy(counters[8].counter_name,"INVINCO");
  322. counters[5].counter_value=starting_lives;
  323. counters[6].counter_value=starting_health;
  324. counters[7].counter_value=time_limit;
  325. //Scroll colors
  326. scroll_base_color=143;
  327. scroll_corner_color=135;
  328. scroll_pointer_color=128;
  329. scroll_title_color=143;
  330. scroll_arrow_color=142;
  331. //Done!
  332. player_restart_x=player_x;
  333. player_restart_y=player_y;
  334. for(t1=0;t1<16;t1++)
  335. set_color_intensity(t1,100);
  336. }
  337. // We don't need a cleanup function- clear_world handles this nicely. It only
  338. // leaves robot memory allocated, which is a seperate entity that is
  339. // deallocated anyways. A nice function would be one to clear current
  340. // objects, those numbered 0, for use within the editing fields.
  341. void clear_zero_objects(void) {
  342. //Clear the three zero objects
  343. clear_robot(0);
  344. clear_scroll(0);
  345. clear_sensor(0);
  346. }
  347. void set_built_in_messages(int param)
  348. {
  349. built_in_messages = param;
  350. }
  351. int get_built_in_messages(void)
  352. {
  353. return (built_in_messages);
  354. }