summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/mon-project.cc
blob: 0052055d2a84b78ebd8974a876f868d9ca392e6d (plain) (blame)
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
/*
 *  File:       mon-project.cc
 *  Summary:    Slow projectiles, done as monsters.
 *  Written by: Adam Borowski
 */

#include "AppHdr.h"

#include "mon-project.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>

#include "externs.h"

#include "coord.h"
#include "env.h"
#include "mgen_data.h"
#include "mon-place.h"
#include "mon-stuff.h"
#include "stuff.h"
#include "terrain.h"
#include "viewchar.h"

bool mons_is_projectile(monster_type mt)
{
    return (mt == MONS_ORB_OF_DESTRUCTION);
}

bool cast_iood(actor *caster, int pow, bolt *beam)
{
    int mtarg = mgrd(beam->target);
    
    int mind = mons_place(mgen_data(MONS_ORB_OF_DESTRUCTION,
                (caster->atype() == ACT_PLAYER) ? BEH_FRIENDLY :
                    ((monsters*)caster)->wont_attack() ? BEH_FRIENDLY : BEH_HOSTILE,
                caster,
                0,
                SPELL_IOOD,
                coord_def(-1, -1),
                (mtarg != NON_MONSTER) ? mtarg :
                    (you.pos() == beam->target) ? MHITYOU : MHITNOT,
                0,
                GOD_NO_GOD));
    if (mind == -1)
    {
        canned_msg(MSG_NOTHING_HAPPENS);
        return (false);
    }

    monsters &mon = menv[mind];
    const coord_def pos = caster->pos();
    mon.props["iood_x"] = (float)pos.x;
    mon.props["iood_y"] = (float)pos.y;
    mon.props["iood_vx"] = (float)(beam->target.x - pos.x);
    mon.props["iood_vy"] = (float)(beam->target.y - pos.y);
    mon.props["iood_kc"].get_byte() = (caster->atype() == ACT_PLAYER) ? KC_YOU :
            ((monsters*)caster)->wont_attack() ? KC_FRIENDLY : KC_OTHER;
    mon.flags &= ~MF_JUST_SUMMONED;

    // Move away from the caster's square.
    iood_act(mon);
    mon.lose_energy(EUT_MOVE);
    return (true);
}

void _normalize(float &x, float &y)
{
    const float d = sqrt(x*x + y*y);
    if (d <= 0.000001)
        return;
    x/=d;
    y/=d;
}

void _iood_dissipate(monsters &mon)
{
    simple_monster_message(&mon, " dissipates.");
    mpr("iood: dissipating");
    monster_die(&mon, KILL_DISMISSED, NON_MONSTER);
}

bool _iood_hit(monsters &mon, const coord_def &pos)
{
    bolt beam;
    beam.name = "orb of destruction";
    beam.flavour = BEAM_NUKE;
    beam.attitude = mon.attitude;
    beam.thrower = (mon.props["iood_kc"].get_byte() == KC_YOU)
                        ? KILL_YOU_MISSILE : KILL_MON_MISSILE;
    beam.colour = WHITE;
    beam.type = dchar_glyph(DCHAR_FIRED_BURST);
    beam.range = 1;
    beam.source = pos;
    beam.target = pos;
    beam.hit = AUTOMATIC_HIT;
    beam.damage = dice_def(3, 20);
    beam.fire();

    return (true);
}

// returns true if the orb is gone
bool iood_act(monsters &mon)
{
    ASSERT(mons_is_projectile(mon.type));

    float x = mon.props["iood_x"];
    float y = mon.props["iood_y"];
    float vx = mon.props["iood_vx"];
    float vy = mon.props["iood_vy"];

    mprf("iood_act: pos (%d,%d) rpos (%f,%f) v (%f,%f)", mon.pos().x, mon.pos().y,
        x, y, vx, vy);

    if (!vx && !vy) // not initialized
    {
        _iood_dissipate(mon);
        return (true);
    }

    coord_def target(-1, -1);
    if (mon.foe == MHITYOU)
        target = you.pos();
    else if (invalid_monster_index(mon.foe))
        ;
    else if (invalid_monster_type(menv[mon.foe].type))
    {
        // Our target is gone.  Since picking a new one would require
        // intelligence, the orb continues on a ballistic course.
        mon.foe = MHITNOT;
    }
    else
        target = menv[mon.foe].pos();

    _normalize(vx, vy);

    x += vx;
    y += vy;

    mon.props["iood_x"] = x;
    mon.props["iood_y"] = y;

    coord_def pos(round(x), round(y));
    if (!in_bounds(pos))
    {
        _iood_dissipate(mon);
        return (true);
    }

    if (pos == mon.pos())
        return (false);

    if (cell_is_solid(pos) || actor_at(pos))
    {
        if (_iood_hit(mon, pos))
        {
            monster_die(&mon, KILL_DISMISSED, NON_MONSTER);
            return (true);
        }
    }

    if (!mon.move_to_pos(pos))
    {
        _iood_dissipate(mon);
        return (true);
    }

    return (false);
}