1 /*
2 Copyright (C) 2004 Parallel Realities
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #include "headers.h"
22 
Virus()23 Virus::Virus()
24 {
25 	active = true;
26 	hasFile = false;
27 	insideDir = false;
28 	thinktime = 0;
29 	type = 0;
30 	x = y = -1;
31 	dx = dy = 0;
32 	targetDir = NULL;
33 	health = 1;
34 	speed = Math::rrand(1, 3);
35 	file = NULL;
36 	sprite = NULL;
37 	pointsImage = NULL;
38 }
39 
~Virus()40 Virus::~Virus()
41 {
42 	if (pointsImage != NULL)
43 		SDL_FreeSurface(pointsImage);
44 
45 	pointsImage = NULL;
46 }
47 
setDestinationDir(Directory * targetDir)48 void Virus::setDestinationDir(Directory *targetDir)
49 {
50 	if (targetDir == NULL)
51 		return;
52 
53 	this->targetDir = targetDir;
54 
55 	Math::calculateSlope(x, y, targetDir->x, targetDir->y, &dx, &dy);
56 
57 	dx /= speed;
58 	dy /= speed;
59 }
60 
moveErratic()61 void Virus::moveErratic()
62 {
63 	int targetX = targetDir->x;
64 	int targetY = targetDir->y;
65 	int r = rand() % 10;
66 
67 	if (r <= 1)
68 	{
69 		targetX += Math::rrand(-50, 100);
70 		targetY += Math::rrand(-50, 100);
71 	}
72 	else if (r <= 4)
73 	{
74 		targetX = Math::rrand(50, 750);
75 		targetY = Math::rrand(50, 550);
76 	}
77 
78 	if ((x < 0) || (x > 800) || (y < 0) || (y > 600))
79 	{
80 		targetX = 400;
81 		targetY = 300;
82 	}
83 
84 	Math::calculateSlope(x, y, targetX, targetY, &dx, &dy);
85 
86 	dx /= speed;
87 	dy /= speed;
88 
89 	thinktime = Math::rrand(500, 2000);
90 }
91 
setBase(Base * base)92 void Virus::setBase(Base *base)
93 {
94 	this->base = base;
95 	this->x = base->x;
96 	this->y = base->y;
97 }
98 
goHome()99 void Virus::goHome()
100 {
101 	Math::calculateSlope(x, y, base->x, base->y, &dx, &dy);
102 
103 	dx /= speed;
104 	dy /= speed;
105 }
106 
destroy()107 void Virus::destroy()
108 {
109 	if (pointsImage != NULL)
110 		SDL_FreeSurface(pointsImage);
111 
112 	pointsImage = NULL;
113 }
114