ezboard.cpp 12 KB

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