-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonster.h
executable file
·149 lines (132 loc) · 3.27 KB
/
monster.h
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#include "types.h"
//#include "all.h"
//#include "items.h"
//#include "room.h"
/* monster mash */
void GenerateMonsters() {
int i,p,t,hiti;
bool valid;
MDist=-1;
MonsterI=0;
for(i=0; i<=RoomI; i++) {
valid=false;
p=rand() % 100;
if(p>60) {
t=0;
while(valid==false && t<5) {
Monsters[MonsterI].dx=(rand() % (Rooms[i].x2-Rooms[i].x1-2)) + Rooms[i].x1 + 1;
Monsters[MonsterI].dy=(rand() % (Rooms[i].y2-Rooms[i].y1-2)) + Rooms[i].y1 + 1;
Monsters[MonsterI].x=Monsters[MonsterI].dx;
Monsters[MonsterI].y=Monsters[MonsterI].dy;
Monsters[MonsterI].Room = i;
hiti=HitItem(Monsters[MonsterI].x,Monsters[MonsterI].y);
if(!(Monsters[MonsterI].x==CPlayer.x && Monsters[MonsterI].y==CPlayer.y) && (hiti==-1)) {
valid=true;
}
t++;
}
MonsterI++;
}
}
MonsterI--; //we end up with +1, so need to sub
}
void MoveRandom(int i) {
int tries,d,tx,ty;
bool valid=false;
d = rand() % 3;
tries = 0;
while(tries<5 && valid==false) {
tries++;
if(tries!=1) d=(d+1)%4; //try next direction
tx=Monsters[i].x;
ty=Monsters[i].y;
if(d==0) {
tx--;
}
else if(d==1) {
tx++;
}
else if(d==2) {
ty--;
}
else if(d==3) {
ty++;
};
valid = (HitWall(tx,ty)==false) && (HitItem(tx,ty)==-1);
}
if(valid==true) {
Monsters[i].x=tx;
Monsters[i].y=ty;
}
}
void MoveToPlayer(int i, int d) {
int cx,cy,tx,ty;
cx = Monsters[i].x - CPlayer.x;
cy = Monsters[i].y - CPlayer.y;
if(cx!=0 && cy!=0) {
tx=Monsters[i].x;
ty=Monsters[i].y;
if(abs(cx)>abs(cy)) {
if(cx>0) {
tx=Monsters[i].x-d;
}
else {
tx=Monsters[i].x+d;
}
}
else {
if(cy>0) {
ty=Monsters[i].y-d;
}
else {
ty=Monsters[i].y+d;
}
}
if((HitWall(tx,ty)==false) && (HitItem(tx,ty)==-1)) {
Monsters[i].x=tx;
Monsters[i].y=ty;
}
else {
MoveRandom(i);
}
}
}
void MoveMonsters() {
int i;
int d=-1; //d = 1 to player, -1 away from player
if(L==0) d=1;
for(i=0; i<=MonsterI; i++) {
if(Monsters[i].Room==CPlayer.Room) {
MoveToPlayer(i,d);
}
else {
MoveRandom(i);
}
}
}
/* HitMonster returns -1 if not hit or int of distance if within 4 chars */
int HitMonster(int x1, int y1) {
int cx,cy;
int i=0;
bool found=false;
while(i<=MonsterI && found==false) {
if(Monsters[i].Room==HitRoom(x1,y1)) {
cx=abs(x1-Monsters[i].x);
cy=abs(y1-Monsters[i].y);
if(cx<4 && cy<4) {
found=true;
}
else {
i++;
}
}
else {
i++;
}
}
if(found==true) return Max(cx,cy);
return -1;
}