1 /* Tornado - Two player weather action game
2  *
3  * Copyright (C) 2000-2002 Oliver Feiler (kiza@gmx.net)
4  *
5  * erwin.c - The program's AI
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (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
15  * GNU 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 
23 #include <stdlib.h>
24 #include "main.h"
25 #include "draw.h"
26 
27 
28 /* Lets the AI choose a weather based on the current house destruction.
29  * The idea is to take lightning and tornado which do much "vertical" damage
30  * (split the house) when the house is still in good condition. Rain, snow
31  * and hail don't do as much damage but are great to "remove" small pieces
32  * of the house that are difficult to hit otherwise. */
33 
erwin_choice(int erwin_destroy_count,int self_destroyed,int cloud_x,int current_player)34 char erwin_choice(int erwin_destroy_count, int self_destroyed,
35 		  int cloud_x, int current_player) {
36   int select;			/* just an integer that holds a random number */
37 
38   select = main_rand(9);
39   if (select == 0)
40     return 's';
41 
42   if (erwin_destroy_count < 45) {
43     if (current_player == 2) {
44       if (cloud_x <= 15)
45 	return 'l';
46       else
47 	return 't';
48     } else if (current_player == 1) {
49       if (cloud_x >= 40)
50 	return 'l';
51       else
52 	return 't';
53     }
54   } else {
55     select = main_rand(2);
56     switch (select) {
57     case 0:
58       return 'h';
59       break;
60     case 1:
61       return 'r';
62       break;
63     }
64   }
65   return 't';
66 }
67 
68 
69 
70 /* Return aim based on the selected weather, the current windspeed,
71  * the cloud position and the current player. */
72 
erwin_aim(char input,int windspeed,int cloud_x,int current_player)73 int erwin_aim(char input, int windspeed, int cloud_x, int current_player) {
74   int aim;							   /* aim the function returns to main */
75 
76   if (input == 'l')		/* If Lightning is selected, we don't
77 		                   need aim and can return immediately.*/
78     return 0;
79 
80   if (input == 's') {
81     if (current_player == 1) {
82       if (windspeed <= 0)
83 	aim = ((abs(windspeed * 20) + (-3 - cloud_x)) / 20);
84       else
85 	aim = ((-abs(windspeed * 20) + (-3 - cloud_x)) / 20);
86     } else {
87       if (windspeed <= 0)
88 	aim = ((abs(windspeed * 20) + (69 - cloud_x)) / 20.0);
89       else
90 	aim = ((-abs(windspeed * 20) + (60 - cloud_x)) / 20.0);
91     }
92   } else if (current_player == 1) {	/* ...we aim at the right house */
93     if (windspeed <= 0)
94       aim = ((abs(windspeed * 20) + (69 - cloud_x)) / 20.0);
95     else
96       aim = ((-abs(windspeed * 20) + (60 - cloud_x)) / 20.0);
97   } else {			/*  and now on the left */
98     if (windspeed <= 0)
99       aim = ((abs(windspeed * 20) + (-3 - cloud_x)) / 20);
100     else
101       aim = ((-abs(windspeed * 20) + (-3 - cloud_x)) / 20);
102   }
103 
104   return aim;
105 }
106 
107 /* The new improved AI */
erwin_choice_new(int cloud_x,int current_player)108 int erwin_choice_new(int cloud_x, int current_player) {
109 
110   int select;			/* random int */
111 
112   count_snow(59, 14, 17, 10);
113 
114   /* Choose snow randomly from time to time. */
115   select = main_rand(4);
116   if (select == 0)
117     return 's';
118 
119   /* If the cloud is right above the enemy house and it has >75%
120      then we strike with lightning to do most damage.
121      If the cloud is close to the enemy house and it has >75%
122      then we strike with tornado.
123      If the cloud is far away from the enemy house
124      then we strike with hail.
125      If the place where we would like to hit is covered with snow
126      then we use rain to get rid of it. */
127   if (current_player == 1) {
128     if (((count_destroyed(59, 14, 17, 10)-32) < 25) && (cloud_x >= 40))
129       return 'l';
130     else if (((count_destroyed(59, 14, 17, 10)-32) < 25) && (cloud_x >= 30))
131       return 't';
132     else if (count_snow(59, 14, 17, 10) > 5)
133       return 'r';
134     else
135       return 'h';
136   }
137 
138   if (current_player == 2) {
139     if (((count_destroyed(5, 14, 17, 10)-32) < 25) && (cloud_x <= 15))
140       return 'l';
141     else if (((count_destroyed(5, 14, 17, 10)-32) < 25) && (cloud_x <= 25))
142       return 't';
143     else if (count_snow(5, 14, 17, 10) > 5)
144       return 'r';
145     else
146       return 'h';
147   }
148 
149   return 't';
150 }
151 
erwin_aim_new(char input,int windspeed,int cloud_x,int current_player)152 int erwin_aim_new(char input, int windspeed, int cloud_x, int current_player) {
153 
154   int aim;			/* returned aim */
155 
156   if (input == 'l')		/* If Lightning is selected, we don't
157 		                   need aim and can return immediately.*/
158     return 0;
159 
160   if (input == 's') {
161     if (current_player == 1) {
162       if (windspeed <= 0)
163 	aim = ((abs(windspeed * 20) + (-3 - cloud_x)) / 20);
164       else
165 	aim = ((-abs(windspeed * 20) + (-3 - cloud_x)) / 20);
166     } else {
167       if (windspeed <= 0)
168 	aim = ((abs(windspeed * 20) + (69 - cloud_x)) / 20.0);
169       else
170 	aim = ((-abs(windspeed * 20) + (60 - cloud_x)) / 20.0);
171     }
172   } else if (current_player == 1) {	/* ...we aim at the right house */
173     if (windspeed <= 0)
174       aim = ((abs(windspeed * 20) + (69 - cloud_x)) / 20.0);
175     else
176       aim = ((-abs(windspeed * 20) + (60 - cloud_x)) / 20.0);
177   } else {			/*  and now on the left */
178     if (windspeed <= 0)
179       aim = ((abs(windspeed * 20) + (-3 - cloud_x)) / 20);
180     else
181       aim = ((-abs(windspeed * 20) + (-3 - cloud_x)) / 20);
182   }
183 
184   return aim;
185 }
186