JNR
rpgSheet_Fighter.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laCreatureSheet.cpp
32 //
33 // This class stores the RPG properites for fighting creatures (player and mobs)
34 //
35 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
36 //
37 #include "stdafx.h"
38 #include "RPG.h"
39 #include "Core-Level-JR.h"
40 
41 void rpgSheet_Fighter_Props::load(class laPropertyList* pElement)
42 {
43 
44  rpgSheet_Acrobate_Props::load(pElement);
45 
46  nHP_Max = nHP = pElement->getInt( "HP" );
47  nAP = pElement->getInt( "AP" );
48 
49  // Load Attacks
50  //
51  int i=0;
52 
53  try{
54  while(1)
55  {
56  char s[16];
57  rpgAttack a;
58 
59  sprintf(s, "attack-%d",i++);
60  a.load( pElement->getChild(s) );
61 
62  vAttacks.push_back(a);
63  }
64  }
65  catch(laError_PropertyNotDefined&) { /*MSG("%d attacks", i);*/ };
66 
67  // Load DPs
68  //
69  for(unsigned i=0; i<M_ELEMENTS; i++)
70  {
71  char s[16];
72  sprintf(s, "DP-%d",i);
73  arDP[i] = pElement->getDouble( s );
74  }
75 }
76 
77 void rpgSheet_Fighter_Props::load(class laFileParser *fp)
78 {
79  rpgSheet_Acrobate_Props::load(fp);
80 
81  fp->readInt(&nHP_Max);
82  nHP = nHP_Max;
83 
84  fp->readUnsigned(&nAP);
85 
86  // Load attacks
87  //
88  std::vector<rpgAttack>::iterator iter;
89 
90  for(iter=vAttacks.begin(); iter!=vAttacks.end(); iter++)
91  {
92  fp->readLabel();
93  iter->load(fp);
94  }
95 
96  // Load DPs
97  //
98  for(unsigned i=0; i<M_ELEMENTS; i++) fp->readDouble( arDP + i );
99 }
100 
101 // Perform attack
102 //
103 unsigned rpgSheet_Fighter::attack( unsigned nAttack, rpgSheet_Fighter* pTarget )
104 {
105  return pTarget->hit( this->modAttack(nAttack) );
106 }
107 
108 // Suffer an attack
109 //
110 unsigned rpgSheet_Fighter::hit( rpgAttack attack )
111 {
112  int nAttackPower = attack.nAP;
113  double dElementDefense = M_MIN(1, this->modDP(attack.nElement) );
114 
115  // The defensive throw represents the % of damage that is resisted by the creature;
116  // Given an element defense in the range [0;1], the defensive thorw varies randomly
117  // in the range [50%;100%] of that value.
118  //
119  double dDefensiveThrow = dElementDefense/2.0 + ((rand()%100)/100.0) * (dElementDefense/2.0);
120  int nDamage = M_MAX(0, nAttackPower * (1 - dDefensiveThrow) );
121 
122  properties()->nHP = M_MAX(0, properties()->nHP - nDamage);
123  return nDamage;
124 }
125 
126 // Heal self
127 //
128 unsigned rpgSheet_Fighter::heal( unsigned hp_heal )
129 {
130  if( !isAlive() ) return 0;
131 
132  unsigned nHP_Old = properties()->nHP;
133  properties()->nHP = M_MIN( (properties()->nHP + hp_heal), properties()->nHP_Max);
134 
135  return properties()->nHP - nHP_Old;
136 }
Attack RPG Properties.
Definition: rpgAttack.h:55
File Parser.
Definition: laFileParser.h:41