mzm.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* $Id: idput.h,v 1.2 1999/01/17 20:35:41 mental Exp $
  2. * MegaZeux
  3. *
  4. * Copyright (C) 2002 Gilead Kutnick - exophase@adelphia.net
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <stdlib.h>
  21. #include "mzm.h"
  22. #include "idput.h"
  23. #include "data.h"
  24. #include "world.h"
  25. void save_mzm(World *mzx_world, char *name, int x, int y, int w,
  26. int h, int mode, int mode2)
  27. {
  28. Board *src_board = mzx_world->current_board;
  29. int i, i2;
  30. int bwidth, bheight;
  31. char *src_char;
  32. char *src_color;
  33. int edge_skip;
  34. if((mode == 1) && !src_board->overlay_mode) return;
  35. if(mode == 2)
  36. {
  37. bwidth = mzx_world->vlayer_width;
  38. bheight = mzx_world->vlayer_height;
  39. }
  40. else
  41. {
  42. bwidth = src_board->board_width;;
  43. bheight = src_board->board_height;
  44. }
  45. edge_skip = bwidth - w;
  46. // No attempt is made to clip it.. if it runs off the board, it just
  47. // doesn't do it.
  48. if(!((x < 0) | (y < 0) | ((x + w) > bwidth) | ((y + h) > bheight)))
  49. {
  50. FILE *fp = fopen((const char *)name, "wb");
  51. fprintf(fp, "MZMX");
  52. fputc(w, fp);
  53. fputc(h, fp);
  54. fseek(fp, 16, SEEK_SET);
  55. // mode 0 = from board
  56. // mode 1 = from overlay
  57. // mode 2 = from vlayer
  58. if(mode == 0)
  59. {
  60. char *src_id =
  61. src_board->level_id + (y * bwidth) + x;
  62. int src_id2 = (y * bwidth) + x;
  63. char *src_param =
  64. src_board->level_param + (y * bwidth) + x;
  65. char *src_color =
  66. src_board->level_color + (y * bwidth) + x;
  67. char *src_uid =
  68. src_board->level_under_id + (y * bwidth) + x;
  69. char *src_uparam =
  70. src_board->level_under_param + (y * bwidth) + x;
  71. char *src_ucolor =
  72. src_board->level_under_color + (y * bwidth) + x;
  73. int edge_skip = bwidth - w;
  74. char id;
  75. for(i = 0; i < h; i++)
  76. {
  77. for(i2 = 0; i2 < w; i2++)
  78. {
  79. // Don't put a new player, robot, pushable robot, scroll,
  80. // sign, or sensor
  81. id = *src_id;
  82. if(id < 122)
  83. {
  84. fputc(*src_id, fp);
  85. // if mode2 switch, save the char there as param
  86. if(mode2)
  87. {
  88. fputc(get_id_char(src_board, src_id2), fp);
  89. }
  90. else
  91. {
  92. fputc(*src_param, fp);
  93. }
  94. }
  95. else
  96. {
  97. // Instead, put a customblock that looks like the thing
  98. fputc(5, fp);
  99. fputc(get_id_char(src_board, src_id2), fp);
  100. }
  101. fputc(*src_color, fp);
  102. fputc(*src_uid, fp);
  103. fputc(*src_uparam, fp);
  104. fputc(*src_ucolor, fp);
  105. src_id++;
  106. src_param++;
  107. src_color++;
  108. src_uid++;
  109. src_uparam++;
  110. src_ucolor++;
  111. src_id2++;
  112. }
  113. src_id += edge_skip;
  114. src_id2 += edge_skip;
  115. src_param += edge_skip;
  116. src_color += edge_skip;
  117. src_uid += edge_skip;
  118. src_uparam += edge_skip;
  119. src_ucolor += edge_skip;
  120. }
  121. fclose(fp);
  122. return;
  123. }
  124. if(mode <= 2)
  125. {
  126. if(mode == 1)
  127. {
  128. src_char = src_board->overlay + (y * bwidth) + x;
  129. src_color = src_board->overlay_color + (y * bwidth) + x;
  130. }
  131. else
  132. if(mode == 2)
  133. {
  134. src_char = mzx_world->vlayer_chars +
  135. (y * mzx_world->vlayer_width) + x;
  136. src_color = mzx_world->vlayer_colors +
  137. (y * mzx_world->vlayer_width) + x;
  138. }
  139. for(i = 0; i < h; i++)
  140. {
  141. for(i2 = 0; i2 < w; i2++)
  142. {
  143. fputc(5, fp);
  144. fputc(*src_char, fp);
  145. fputc(*src_color, fp);
  146. fputc(0, fp);
  147. fputc(0, fp);
  148. fputc(0, fp);
  149. src_char++;
  150. src_color++;
  151. }
  152. src_char += edge_skip;
  153. src_color += edge_skip;
  154. }
  155. }
  156. fclose(fp);
  157. }
  158. }
  159. void load_mzm(World *mzx_world, char *name, int x, int y, int mode)
  160. {
  161. Board *src_board = mzx_world->current_board;
  162. int i, i2;
  163. int w, h;
  164. if((mode == 1) && !src_board->overlay_mode) return;
  165. FILE *fp = fopen(name, "rb");
  166. if(fp && (fgetc(fp) == 'M') && (fgetc(fp) == 'Z') &&
  167. (fgetc(fp) == 'M') && (fgetc(fp) == 'X'))
  168. {
  169. int bwidth, bheight;
  170. // No attempt is made to clip it.. if it runs off the board, it just
  171. // doesn't do it.
  172. w = fgetc(fp);
  173. h = fgetc(fp);
  174. fseek(fp, 16, SEEK_SET);
  175. if(mode == 2)
  176. {
  177. bwidth = mzx_world->vlayer_width;
  178. bheight = mzx_world->vlayer_height;
  179. }
  180. else
  181. {
  182. bwidth = src_board->board_width;
  183. bheight = src_board->board_height;
  184. }
  185. // No attempt is made to clip it.. if it runs off the board, it just
  186. // doesn't do it.
  187. if(!((x < 0) | (y < 0) | ((x + w) > bwidth) | ((y + h) > bheight)))
  188. {
  189. // mode 0 = to board
  190. // mode 1 = to overlay
  191. // mode 2 = to vlayer
  192. if(mode == 0)
  193. {
  194. char *dest_id =
  195. src_board->level_id + (y * bwidth) + x;
  196. char *dest_param =
  197. src_board->level_param + (y * bwidth) + x;
  198. char *dest_color =
  199. src_board->level_color + (y * bwidth) + x;
  200. char *dest_uid =
  201. src_board->level_under_id + (y * bwidth) + x;
  202. char *dest_uparam =
  203. src_board->level_under_param + (y * bwidth) + x;
  204. char *dest_ucolor =
  205. src_board->level_under_color + (y * bwidth) + x;
  206. int edge_skip = bwidth - w;
  207. for(i = 0; i < h; i++)
  208. {
  209. for(i2 = 0; i2 < w; i2++)
  210. {
  211. // Don't overwrite the player!
  212. if(*dest_id != 127)
  213. {
  214. *dest_id = fgetc(fp);
  215. }
  216. else
  217. {
  218. fgetc(fp);
  219. }
  220. *dest_param = fgetc(fp);
  221. *dest_color = fgetc(fp);
  222. *dest_uid = fgetc(fp);
  223. *dest_uparam = fgetc(fp);
  224. *dest_ucolor = fgetc(fp);
  225. dest_id++;
  226. dest_param++;
  227. dest_color++;
  228. dest_uid++;
  229. dest_uparam++;
  230. dest_ucolor++;
  231. }
  232. dest_id += edge_skip;
  233. dest_param += edge_skip;
  234. dest_color += edge_skip;
  235. dest_uid += edge_skip;
  236. dest_uparam += edge_skip;
  237. dest_ucolor += edge_skip;
  238. }
  239. fclose(fp);
  240. return;
  241. }
  242. if(mode <= 2)
  243. {
  244. char *dest_char;
  245. char *dest_color;
  246. int edge_skip = bwidth - w;
  247. if(mode == 1)
  248. {
  249. dest_char = src_board->overlay + (y * bwidth) + x;
  250. dest_color = src_board->overlay_color + (y * bwidth) + x;
  251. }
  252. else
  253. if(mode == 2)
  254. {
  255. dest_char = mzx_world->vlayer_chars +
  256. (y * bwidth) + x;
  257. dest_color = mzx_world->vlayer_colors +
  258. (y * bwidth) + x;
  259. }
  260. for(i = 0; i < h; i++)
  261. {
  262. for(i2 = 0; i2 < w; i2++)
  263. {
  264. fgetc(fp);
  265. *dest_char = fgetc(fp);
  266. *dest_color = fgetc(fp);
  267. fgetc(fp);
  268. fgetc(fp);
  269. fgetc(fp);
  270. dest_char++;
  271. dest_color++;
  272. }
  273. dest_char += edge_skip;
  274. dest_color += edge_skip;
  275. }
  276. }
  277. }
  278. fclose(fp);
  279. }
  280. }