/
pokemon.a
executable file
·188 lines (153 loc) · 5.34 KB
/
pokemon.a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import a.util.Comparator;
import a.natives.unit;
import a.natives.Player;
import a.natives.trigger;
import a.natives.point;
include "types.a"
include "species.a"
include "player.a"
public class Pokemon [1024] {
// The type of pokemon.
public PokemonSpecies species;
// The level of this pokemon.
public int level;
// The current amount of hp.
public int hp;
// Who owns this pokemon.
public Trainer owner;
// The SC2 unit for this pokemon.
private unit _unit;
public Pokemon(PokemonSpecies species, int level, Trainer owner, point p) {
this.species = species;
this.level = level;
this.owner = owner;
this._unit = unit.create(species.name, 0, (int)owner.player, p, 0);
this._unit.setCustomValue(0, (int)this);
updateLevel();
unitref ref = _unit.getUnitRef();
trigger.create(unitDamaged.name).addEventUnitDamaged(ref, c_unitDamageTypeAny, c_unitDamageEither, null);
trigger.create(unitFainted.name).addEventUnitDied(ref);
trigger.create(rareCandy.name).addEventUnitAbility(ref, AbilityCommand("Rare Candy", 0), c_unitAbilStageComplete, false);
}
public ~Pokemon() {
owner.pokemon.remove(this);
}
public void updateLevel() {
hp = getHp();
_unit.setPropertyInt(c_unitPropLifeMax, hp);
_unit.setInfoText("Level " + level, "", "");
}
public int getStat(int base) {
int iv = 31;
int ev = 0;
return (iv + 2 * base + ev / 4) * level / 100 + 5;
}
public int getHp() {
return 5 * (getStat(species.hp) + 5);
}
public int getAtk() {
return getStat(species.atk);
}
public int getDef() {
return getStat(species.def);
}
public int getSAtk() {
return getStat(species.satk);
}
public int getSDef() {
return getStat(species.sdef);
}
// Calculates the damage this pokemon would do to another pokemon.
// http://bulbapedia.bulbagarden.net/wiki/Damage_calculation#Damage_formula
public int damage(Pokemon other) {
Move move = this.species.move;
fixed attack;
fixed defense;
if (move.kind == Kind.PHYSICAL) {
attack = this.getAtk();
defense = other.getDef();
} else if (move.kind == Kind.SPECIAL) {
attack = this.getSAtk();
defense = other.getSDef();
}
fixed damage = ((2.0 * level + 10.0) / 250 * attack / defense * move.power + 2);
fixed effectiveness = getTypeAdvantage(other);
damage *= effectiveness;
fixed stab = 1.0;
if (move.type == species.type1 || move.type == species.type2) {
stab = 1.5;
}
damage *= stab;
fixed random = 1.0;
//random = RandomFixed(0.85, 1.0);
damage *= random;
return (int) damage;
}
public fixed getTypeAdvantage(Pokemon other) {
Type attacking = this.species.move.type;
return attacking.getAdvantage(other.species.type1) * attacking.getAdvantage(other.species.type2);
}
public unit getUnit() {
return _unit;
}
public void setUnit(unit u) {
_unit = u;
}
public static Pokemon fromUnit(unit u) {
return (Pokemon)(int)u.getCustomValue(0);
}
public Pokemon clone(Trainer trainer, point spawn) {
return new Pokemon(species, level, trainer, spawn);
}
public void remove() {
_unit.remove();
delete this;
}
public string toString() {
return species.name + " level " + (string)level + " owned by " + owner.toString();
}
public static Comparator<Pokemon> comparator = new PokemonComparator();
public static Hasher<Pokemon> hasher = new PokemonHasher();
}
public class PokemonComparator extends Comparator<Pokemon> {
public override int compare(Pokemon p1, Pokemon p2) {
return (int) p1 - (int) p2;
}
}
private class PokemonHasher extends Hasher<Pokemon> {
public override int hashCode(Pokemon p) {
return (int) p;
}
public override bool equal(Pokemon p1, Pokemon p2) {
return p1 == p2;
}
}
private bool unitDamaged(bool conditions, bool actions) {
unit src = EventUnitDamageSourceUnit();
unit trg = EventUnit();
Pokemon p_src = Pokemon.fromUnit(src);
Pokemon p_trg = Pokemon.fromUnit(trg);
//System.debug((string)(int)p_src.species);
//System.debug((string)(int)p_trg.species);
//System.debug(p_src.species.move.toString());
int damage = p_src.damage(p_trg);
//System.debug((string) damage);
p_trg.hp -= damage;
if (p_trg.hp < 0) {
p_trg.hp = 0;
}
trg.setPropertyInt(c_unitPropLife, p_trg.hp);
return true;
}
private bool unitFainted(bool conditions, bool actions) {
Pokemon fainted = Pokemon.fromUnit(EventUnit());
delete fainted;
return true;
}
private trigger rareCandyTrigger = trigger.create(rareCandy.name);
private bool rareCandy(bool conditions, bool actions) {
Pokemon p = Pokemon.fromUnit(EventUnit());
p.level += 1;
p.updateLevel();
return true;
}