00001 #include "objects.h"
00002
00003 void Monster::get_border() {
00004 int i=game.RAND(0,3);
00005 if (i%2==0) target_x=game.RAND(0, game.scrwidth-bill.width);
00006 else target_y=game.RAND(0, game.scrheight-bill.height);
00007 switch (i) {
00008 case 0: target_y=-bill.height-16; break;
00009 case 1: target_x=game.scrwidth+1; break;
00010 case 2: target_y=game.scrwidth+1; break;
00011 case 3: target_x=-bill.width-2; break;
00012 }
00013 }
00014
00015
00016 void Monster::enter(){
00017 state = IN;
00018 get_border();
00019 x = target_x; y=target_y;
00020 index = 0;
00021 cels = bill.lcels;
00022 cargo = OS.WINGDOWS;
00023 x_offset = -2;
00024 y_offset = -15;
00025 target_c = game.RAND(0, net.units-1);
00026 target_x = net.computers[target_c].x+net.width-XOFFSET;
00027 target_y = net.computers[target_c].y+YOFFSET;
00028 bill.on_screen++; bill.off_screen--;
00029 }
00030
00031
00032 int Monster::move (int mode) {
00033 int xdist = target_x - x;
00034 int ydist = target_y - y;
00035 int step = step_size(game.level);
00036 int dx, dy;
00037 int signx = xdist >= 0 ? 1 : -1;
00038 int signy = ydist >= 0 ? 1 : -1;
00039 xdist = abs(xdist);
00040 ydist = abs(ydist);
00041 if (!xdist && !ydist) return 0;
00042 else if (xdist<step && ydist<step) {
00043 x = target_x;
00044 y = target_y;
00045 }
00046 else {
00047 dx = (xdist*step*signx)/(xdist+ydist);
00048 dy = (ydist*step*signy)/(xdist+ydist);
00049 switch(mode) {
00050 case SLOW: break;
00051 case FAST: dx = 5*dx/4; dy = 5*dy/4; break;
00052 }
00053 x+=dx;
00054 y+=dy;
00055 if (dx<0)
00056 cels = bill.lcels;
00057 else if (dx>0)
00058 cels = bill.rcels;
00059 }
00060 return 1;
00061 }
00062
00063 void Monster::draw() {
00064 switch (state) {
00065 case IN:
00066 case OUT:
00067 case DYING: draw_std(); break;
00068 case AT: draw_at(); break;
00069 case STRAY: draw_stray(); break;
00070 default: break;
00071 }
00072 }
00073
00074
00075 void Monster::update_in() {
00076 int moved = move(SLOW);
00077 if (!moved && (net.computers[target_c].os != OS.WINGDOWS)
00078 && !(net.computers[target_c].busy))
00079 {
00080 net.computers[target_c].busy=1;
00081 cels = bill.acels;
00082 index=0;
00083 state = AT;
00084 return;
00085 }
00086 else if (!moved) {
00087 int i;
00088 do {
00089 i=game.RAND(0, net.units-1);
00090 } while (i == target_c);
00091 target_c = i;
00092 target_x = net.computers[target_c].x + net.width-XOFFSET;
00093 target_y = net.computers[target_c].y + YOFFSET;
00094 }
00095 index++;
00096 index%=bill.WCELS;
00097 y_offset+=(8*(index%2)-4);
00098 }
00099
00100
00101 void Monster::update_at() {
00102 int sys;
00103 if (index==0 && net.computers[target_c].os == OS.OFF) {
00104 index=6;
00105 sys = net.computers[target_c].find_stray();
00106 if (sys<0) cargo = -1;
00107 else {
00108 cargo = bill.list[sys].cargo;
00109 bill.list[sys].state = OFF;
00110 }
00111 }
00112 else index++;
00113 if (index == 13) {
00114 y_offset = -15;
00115 x_offset = -2;
00116 get_border();
00117 index = 0;
00118 cels = bill.lcels;
00119 state = OUT;
00120 net.computers[target_c].busy=0;
00121 return;
00122 }
00123 y_offset = bill.height - OS.height;
00124 switch (index) {
00125 case 1 :
00126 case 2 : x -= 8; x_offset +=8; break;
00127 case 3 : x -= 10; x_offset +=10; break;
00128 case 4 : x += 3; x_offset -=3; break;
00129 case 5 : x += 2; x_offset -=2; break;
00130 case 6 :
00131 if (net.computers[target_c].os != OS.OFF) {
00132 net.base--; net.off++;
00133 cargo = net.computers[target_c].os;
00134 }
00135 else {
00136 x-=21; x_offset+=21;
00137 }
00138 net.computers[target_c].os = OS.OFF;
00139 y_offset = -15;
00140 x += 20;
00141 x_offset -=20;
00142 break;
00143 case 7 : sy = y_offset; sx = -2; break;
00144 case 8 : sy = -15; sx = -2; break;
00145 case 9 : sy = -7; sx = -7; x -= 8; x_offset +=8; break;
00146 case 10 : sy = 0; sx = -7; x -= 15; x_offset +=15; break;
00147 case 11 : sy = 0; sx = -7;
00148 net.computers[target_c].os = OS.WINGDOWS;
00149 net.off--; net.win++;
00150 break;
00151 case 12 : x += 11; x_offset -=11;
00152 }
00153 }
00154
00155
00156 void Monster::update_out() {
00157 if (game.INTERSECT(x, y, bill.width, bill.height, 0, 0, game.scrwidth,
00158 game.scrheight))
00159 {
00160 move(FAST);
00161 index++;
00162 index%=bill.WCELS;
00163 y_offset+=(8*(index%2)-4);
00164 }
00165 else {
00166 state = OFF;
00167 bill.on_screen--; bill.off_screen++;
00168 }
00169 }
00170
00171
00172
00173 void Monster::update_dying() {
00174 if (index < bill.DCELS-1){
00175 y_offset += (index*GRAVITY);
00176 index++;
00177 }
00178 else {
00179 y+=y_offset;
00180 if (cargo<0 || cargo == OS.WINGDOWS) state = OFF;
00181 else state = STRAY;
00182 bill.on_screen--;
00183 }
00184 }
00185
00186 void Monster::update() {
00187 switch (state) {
00188 case IN: update_in(); break;
00189 case AT: update_at(); break;
00190 case OUT: update_out(); break;
00191 case DYING: update_dying(); break;
00192 default: break;
00193 }
00194 }
00195
00196 int Monster::clicked(int locx, int locy) {
00197 return (locx>x && locx<x+bill.width && locy>y && locy<y+bill.height);
00198 }
00199
00200 int Monster::clickedstray(int locx, int locy) {
00201 return (locx>x && locx<x+OS.width && locy>y && locy<y+OS.height);
00202 }
00203
00204 int Monster::step_size(unsigned int lev) {
00205 return game.MIN(14+lev, 18);
00206 }
00207
00208 void Monster::draw_std() {
00209 if (cargo>=0)
00210 ui.draw(OS.os[cargo], x + x_offset, y + y_offset);
00211 ui.draw(cels[index], x, y);
00212 }
00213
00214 void Monster::draw_at() {
00215 if (index>6 && index<12)
00216 ui.draw(OS.os[0], x + sx, y + sy);
00217 if (cargo>=0)
00218 ui.draw(OS.os[cargo], x + x_offset, y + y_offset);
00219 ui.draw(cels[index], net.computers[target_c].x,
00220 net.computers[target_c].y);
00221 }
00222
00223 void Monster::draw_stray() {
00224 if (game.grabbed==-1 || x != bill.list[game.grabbed].x)
00225 ui.draw(OS.os[cargo], x, y);
00226 }