Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

src/pet.c

Go to the documentation of this file.
00001 /* {{{
00002  * CalcRogue, a roguelike game for PCs, calculators and PDAs
00003  * Copyright (C) 2003 Jim Babcock
00004  * 
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  * }}} */
00019 // pet.c
00022 
00023 #include "crogue.h"
00024 
00025 void monst_swing_monst(int attacker, int target, int trigger);
00026 void monst_attack_monst(int attacknum, int attacker, int target);
00027 
00028 //{{{
00029 void pet_move(int pet)
00030 {
00031     int xpos, ypos;
00032     int xi, yi;
00033     int target;
00034     xpos = w->m[pet].x;
00035     ypos = w->m[pet].y;
00036     
00037     // If a monster is adjacent, attack it
00038     for(yi = ypos-1; yi <= ypos+1; yi++)
00039     for(xi = xpos-1; xi <= xpos+1; xi++)
00040     {
00041         if(xi == xpos && yi == ypos) continue;
00042         
00043         target = monstbytile(xi, yi);
00044         if(target == -1) continue;
00045         if(monst_is_peaceful(target)) continue;
00046         
00047         monst_swing_monst(pet, target, MTRIGGER_NORMAL);
00048         return;
00049     }
00050     
00051     // If far from the player, follow him
00052     if( distancesquare(w->plr.x, w->plr.y, xpos, ypos) > 4 )
00053     {
00054         monstmovetowardsplayer(pet);
00055         return;
00056     }
00057     
00058     // Otherwise, move randomly
00059     monstmoverandomly(pet, 0);
00060 }
00061 //}}}
00062 
00063 //{{{
00064 void monst_swing_monst(int attacker, int target, int trigger)
00065 {
00066     int i;
00067     int ret=0;
00068     
00069     for(i=0; i<MDESC(attacker)->numattacks; i++)
00070     {
00071         if(MATTACK(attacker, i).trigger == trigger)
00072         {
00073             ret = 1;
00074             if(monst_hit_monst(i, attacker, target))
00075                 monst_attack_monst(i, attacker, target);
00076             else
00077             {
00078                 if(player_can_see(attacker) && player_can_see(target))
00079                     message(gettext("The %s misses the %s!"), monstname(attacker), monstname(target));
00080                 else if(player_can_see(attacker))
00081                     message(gettext("The %s misses something!"), monstname(attacker));
00082                 else if(player_can_see(target))
00083                     message(gettext("The %s evades something!"), monstname(target));
00084                 else
00085                     message(gettext("You hear the sound of combat."));
00086             }
00087         }
00088         
00089         if(isNull(w->m[attacker].type) || isNull(w->m[target].type))
00090             return;
00091     }
00092 }
00093 //}}}
00094 //{{{
00095 void monst_attack_monst(int attacknum, int attacker, int target)
00096 {
00097     int damage;
00098     monstattack *attacks;
00099     
00100     if(attacker == target) // sanity check
00101         return;
00102     
00103     attacks = MDESC(attacker)->attacks;
00104     damage = rrandom( attacks[attacknum].damage );
00105     
00106     monst_anger(target);
00107     call_attackfunc( attacks[attacknum].type, attacker, damage, target );
00108 }
00109 //}}}
00110 
00111 //{{{
00112 // If able to attack a pet belonging to the player, do so
00113 ushort monst_battle_pet(ushort monst)
00114 {
00115     int xpos, ypos;
00116     int xi, yi;
00117     int target;
00118     xpos = w->m[monst].x;
00119     ypos = w->m[monst].y;
00120     
00121     for(yi = ypos-1; yi <= ypos+1; yi++)
00122     for(xi = xpos-1; xi <= xpos+1; xi++)
00123     {
00124         target = monstbytile(xi, yi);
00125         if(target >= 0)
00126         {
00127             if(monst_is_pet(target))
00128             {
00129                 monst_swing_monst(monst, target, MTRIGGER_NORMAL);
00130                 return 1;
00131             }
00132         }
00133     }
00134     return 0;
00135 }
00136 //}}}
00137 

Generated on Thu May 20 13:12:10 2004 for CalcRogue by doxygen 1.3.6