Initial push
[shermanaquarium] / sherman-aquarium / shermans / bottom.c
1
2 #include <stdlib.h>
3 #include "aquarium.h"
4 #include "draw.h"
5 #include "bottom.h"
6 #include "fish.h"
7
8 static Bottom_settings bottom_settings;
9 static AquariumData *ad;
10
11
12
13 Bottom_settings *bottom_get_settings_ptr(void)
14 {
15     return &bottom_settings;
16 }
17
18 void bottom_init(void)
19 {
20     SA_Image sea_floor;
21     SA_Image bottom_stuff;
22
23     Bottom bottom_items[NUMOFBOTTOMITEMS] = {
24     {"bottom/plant1.png",        0},
25     {"bottom/plant2.png",        0},
26     {"bottom/plant3.png",        0},
27     {"bottom/plant4.png",        0},
28     {"bottom/plant5.png",        0},
29     {"bottom/plant6.png",        0},
30     {"bottom/plant7.png",        0},
31     {"bottom/plant9.png",        0},
32     {"bottom/plant10.png",       0},
33     {"bottom/smallstone1.png",   0},
34     {"bottom/smallstone3.png",   0},
35     {"bottom/stone1.png",        0},
36     {"bottom/stone2.png",        0},
37     {"bottom/stone3.png",        0},
38     {"bottom/weirdplant.png",    0},
39     {"bottom/weirdplant2.png",   0},
40     {"bottom/octo1.png",         0},
41     {"bottom/bigplant.png",      0}
42     };
43
44     char *bottom_image_files[] = {
45     "bottom/bottom1.png",
46     "bottom/bottom2.png",
47     "bottom/bottom3.png",
48     NULL
49     };
50
51
52     int start_sea_floor_x, start_sea_floor_y;
53
54     int curr_sea_floor_height;
55     int i,j,k,l,jj, step_max, current_loc,x=0;
56     int old_height=0,old_x=0, old_current_loc=0, bx[10], bw[10], nfa;
57     Fish *fishes;
58     Fish_settings *fish_settings;
59
60     ad = aquarium_get_settings_ptr();
61
62     fishes = fish_get_fishes_ptr();
63
64     fish_settings = fish_get_settings_ptr();
65
66
67     if(!bottom_settings.have_sea_floor) 
68         return;
69
70     i = g_rand_int_range(ad->rnd, 0, NUMBOTTOMIMAGES);
71
72     load_image_n_scale(bottom_image_files[i], &sea_floor, 1, bottom_settings.scale);
73
74     start_sea_floor_x = g_rand_int_range(ad->rnd, 0, sea_floor.width);
75
76     curr_sea_floor_height = sea_floor.height - g_rand_int_range(ad->rnd, 0, sea_floor.height*4/5);
77
78     if(curr_sea_floor_height == 0)
79         curr_sea_floor_height = 1;
80
81     
82     start_sea_floor_y = ad->ymax - curr_sea_floor_height;
83
84
85     for(i = (start_sea_floor_x - sea_floor.width); i < ad->xmax; i += sea_floor.width)
86         draw_image_bg(i, start_sea_floor_y, 0, 0, &sea_floor);
87     
88
89   
90     /* Number of plants and stones default max 15 */
91
92     if(bottom_settings.random_plants)
93         j = g_rand_int_range(ad->rnd, 1, bottom_settings.max_plants+1);
94     else
95         j = bottom_settings.max_plants;
96   
97     step_max = curr_sea_floor_height / j;
98
99     /* The smallest stone/plant is 32 high */
100     current_loc = ad->ymax - curr_sea_floor_height + 10;
101     //printf("Curr loc: %d %d %d\n", current_loc, ad->ymax, curr_sea_floor_height);
102
103     /* See if we have a bottom dweller and or a hacktorn*/
104     nfa=0;
105     
106     for(i = 0;i < fish_settings->num_fish; i++){
107         if(fishes[i].type == BDWELLER || fishes[i].type == HAWTHORNE){
108
109             for(jj = 0; jj < 10; jj++){
110                 if(ad->xmax-fishes[i].width == 0)                  
111                     x = 0;
112                 else
113                     x = g_rand_int_range(ad->rnd, 0, ad->xmax - fishes[i].width);
114
115                 for(l = 0; l < nfa; l++)
116                     if(x > bx[l] && x < (bx[l]+bw[l])) 
117                         break;
118                 if(l == nfa) break;
119             }  
120             /* Do just 10 athempts to place a bottom fish, if not sucessfull,
121                then try next fish */
122             if(jj == 10){
123                 /* Make sure the fish is outside the picture. */
124                 fishes[i].tx = 2 * ad->xmax;
125                 fishes[i].y = 0;
126                 continue;
127             }     
128  
129             bx[nfa] = x;
130             bw[nfa] = fishes[i].width;
131             fishes[i].tx = bx[nfa];
132             /* (int)(20*(float)scale/100)*/
133             fishes[i].y = current_loc + curr_sea_floor_height / 3 + g_rand_int_range(ad->rnd, 0, curr_sea_floor_height) + ad->viewpoint_start_y - 30;
134             if(fishes[i].type == BDWELLER)
135                 fishes[i].fast_frame_change = 0.02;
136             else
137                 fishes[i].fast_frame_change = 0.07;
138
139             fishes[i].speed_mul = 0.0001;
140             nfa++;
141         }
142     }
143       
144
145     /* Freeing sea floor. Not needed any longer */
146     g_object_unref(sea_floor.pixbuf);
147
148       
149     for(i = 0; i < j; i++){
150         k=g_rand_int_range(ad->rnd, 0, NUMOFBOTTOMITEMS);
151
152
153         load_image_n_scale(bottom_items[k].image, &bottom_stuff,
154                            1, (bottom_settings.scale / 2) + (g_rand_int_range(ad->rnd, 0, bottom_settings.scale / 2) * bottom_settings.scale / 100));
155     
156         /* from -15 to ad.xmax+15 */
157         jj = 0;
158         if(nfa != 0){
159             for(jj = 0; jj < 10; jj++){
160                 x = g_rand_int_range(ad->rnd, 0, ad->xmax + 30) - 15;
161                 for(l = 0;l < nfa; l++)
162                     if(x > (bx[l] - bottom_stuff.width) && x < (bx[l] + bw[l])) break;
163                 if(l == nfa) break;
164             }
165             
166         }
167         else
168             x = g_rand_int_range(ad->rnd, 0, ad->xmax + 30) - 15;
169
170         /* If we fail to put plants too many times.. */
171         if(jj == 10) continue;
172
173         /* Are they too close? */
174         if(abs(x - old_x) < bottom_stuff.width)
175             if((old_current_loc + old_height) > (bottom_stuff.height + current_loc)){
176                 current_loc = old_current_loc + old_height - bottom_stuff.height + 5;
177             }
178         
179     
180         draw_image_bg(x, current_loc-bottom_stuff.height + 4,
181                       0, g_rand_int_range(ad->rnd, 0, 2),
182                       &bottom_stuff);
183     
184         old_current_loc = current_loc;
185         current_loc += g_rand_int_range(ad->rnd, 0, step_max);
186
187         old_height = bottom_stuff.height;
188         old_x = x;
189     
190         g_object_unref(bottom_stuff.pixbuf);
191
192         bottom_stuff.image = NULL;
193
194     }
195
196 }
197
198 void bottom_exit(void)
199 {
200
201 }