moved function to different source file
[rocksndiamonds.git] / src / game_em / convert.c
1 /* 2007-04-04 08:33:37
2  *
3  * convert intermediate cave format to internal cave structure for
4  * use in logic(). initializes entire internal structure.
5  */
6
7 #include "main_em.h"
8
9
10 void prepare_em_level(void)
11 {
12   int i, x, y;
13   int players_left;
14   boolean team_mode;
15
16   /* reset all runtime variables to their initial values */
17
18   game_init_cave_buffers();
19
20   lev.left = CAVE_BUFFER_XOFFSET;
21   lev.top  = CAVE_BUFFER_YOFFSET;
22   lev.right = lev.left + lev.width;
23   lev.bottom = lev.top + lev.height;
24
25   /* add linked cave buffer columns for wrap-around movement */
26   for (x = 0; x < lev.left; x++)
27   {
28     lev.cavecol[x] = lev.cavecol[lev.width + x];
29     lev.nextcol[x] = lev.nextcol[lev.width + x];
30     lev.drawcol[x] = lev.drawcol[lev.width + x];
31     lev.boomcol[x] = lev.boomcol[lev.width + x];
32
33     lev.cavecol[lev.right + x] = lev.cavecol[lev.left + x];
34     lev.nextcol[lev.right + x] = lev.nextcol[lev.left + x];
35     lev.drawcol[lev.right + x] = lev.drawcol[lev.left + x];
36     lev.boomcol[lev.right + x] = lev.boomcol[lev.left + x];
37   }
38
39   for (x = 0; x < lev.width; x++)
40     for (y = 0; y < lev.height; y++)
41       lev.cave[lev.left + x][lev.top + y] = native_em_level.cave[x][y];
42
43   for (x = lev.left; x < lev.right; x++)
44     for (y = lev.top; y < lev.bottom; y++)
45       lev.next[x][y] = lev.draw[x][y] = lev.cave[x][y];
46
47   lev.time_initial = lev.time_seconds;
48   lev.time = lev.time_initial;
49
50   lev.required = lev.required_initial;
51   lev.score = 0;
52
53   lev.android_move_cnt  = lev.android_move_time;
54   lev.android_clone_cnt = lev.android_clone_time;
55
56   lev.ball_pos = 0;
57   lev.ball_state = lev.ball_state_initial;
58   lev.ball_cnt = lev.ball_time;
59
60   lev.eater_pos = 0;
61   lev.shine_cnt = 0;
62
63   lev.lenses_cnt = lev.lenses_cnt_initial;
64   lev.magnify_cnt = lev.magnify_cnt_initial;
65
66   lev.wheel_cnt = lev.wheel_cnt_initial;
67   lev.wheel_x   = lev.wheel_x_initial;
68   lev.wheel_y   = lev.wheel_y_initial;
69
70   lev.wind_direction = lev.wind_direction_initial;
71   lev.wind_cnt       = lev.wind_cnt_initial;
72
73   lev.wonderwall_state = lev.wonderwall_state_initial;
74   lev.wonderwall_time  = lev.wonderwall_time_initial;
75
76   lev.killed_out_of_time = FALSE;
77
78   /* determine number of players in this level */
79   lev.home_initial = 0;
80
81   for (i = 0; i < MAX_PLAYERS; i++)
82   {
83     ply[i].exists = 0;
84     ply[i].alive_initial = FALSE;
85
86     if (ply[i].x_initial != -1 && ply[i].y_initial != -1)
87     {
88       ply[i].exists = 1;
89
90       lev.home_initial++;
91     }
92   }
93
94   team_mode = getTeamMode_EM();
95
96   if (!team_mode)
97     lev.home_initial = 1;
98
99   lev.home = lev.home_initial;
100   players_left = lev.home_initial;
101
102   for (i = 0; i < MAX_PLAYERS; i++)
103   {
104     if (ply[i].exists)
105     {
106       if (players_left)
107       {
108         ply[i].alive_initial = TRUE;
109         players_left--;
110       }
111       else
112       {
113         int x = ply[i].x_initial;
114         int y = ply[i].y_initial;
115
116         native_em_level.cave[x][y] = Xblank;
117
118         lev.cave[lev.left + x][lev.top + y] = Xblank;
119         lev.next[lev.left + x][lev.top + y] = Xblank;
120         lev.draw[lev.left + x][lev.top + y] = Xblank;
121       }
122     }
123   }
124
125   for (i = 0; i < MAX_PLAYERS; i++)
126   {
127     ply[i].num = i;
128     ply[i].alive = ply[i].alive_initial;
129     ply[i].dynamite = 0;
130     ply[i].dynamite_cnt = 0;
131     ply[i].keys = 0;
132     ply[i].anim = 0;
133     ply[i].oldx = ply[i].x = ply[i].x_initial + lev.left;
134     ply[i].oldy = ply[i].y = ply[i].y_initial + lev.top;
135     ply[i].last_move_dir = MV_NONE;
136     ply[i].joy_n = ply[i].joy_e = ply[i].joy_s = ply[i].joy_w = 0;
137     ply[i].joy_snap  = ply[i].joy_drop = 0;
138     ply[i].joy_stick = ply[i].joy_spin = 0;
139   }
140
141   // the following engine variables are initialized to version-specific values
142   // in function InitGameEngine() (src/game.c):
143   //
144   // - game_em.use_single_button (default: TRUE)
145   // - game_em.use_snap_key_bug (default: FALSE)
146
147   game_em.level_solved = FALSE;
148   game_em.game_over = FALSE;
149
150   game_em.any_player_moving = FALSE;
151   game_em.any_player_snapping = FALSE;
152
153   game_em.last_moving_player = 0;       /* default: first player */
154
155   for (i = 0; i < MAX_PLAYERS; i++)
156     game_em.last_player_direction[i] = MV_NONE;
157
158   lev.exit_x = lev.exit_y = -1; /* kludge for playing player exit sound */
159 }