-
Notifications
You must be signed in to change notification settings - Fork 26
/
enemygen.cpp
141 lines (119 loc) · 2.52 KB
/
enemygen.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "enemygen.h"
#include "sprite.h"
#include "terrain.h"
#include "world.h"
EnemyGen* enemygen_head = 0;
EnemyGen* enemygen_tail = 0;
#ifdef EDITOR
extern Sprite* enemygen_sprite;
EnemyGen* HitEnemyGen(double* p, double* v)
{
double proj = 0;
EnemyGen* best = 0;
int anim = 0;
int frame = 0;
float yaw = 0;
Sprite* sprite = enemygen_sprite;
EnemyGen* eg = enemygen_head;
while (eg)
{
double r[3];
bool hit = HitSprite(sprite, anim, frame, eg->pos, yaw, p, v, r, false);
if (hit)
{
double pr = v[0]*(r[0]-p[0]) + v[1]*(r[1]-p[1]) + v[2]*(r[2]-p[2]);
if (pr < proj || !best)
{
proj = pr;
best = eg;
}
}
eg = eg->next;
}
if (best)
printf("EG-HIT\n");
return best;
}
void DeleteEnemyGen(EnemyGen* eg)
{
if (eg)
{
if (eg->prev)
eg->prev->next = eg->next;
else
enemygen_head = eg->next;
if (eg->next)
eg->next->prev = eg->prev;
else
enemygen_tail = eg->prev;
}
}
#endif
void FreeEnemyGens()
{
EnemyGen* eg = enemygen_head;
while (eg)
{
EnemyGen* n = eg->next;
free(eg);
eg = n;
}
enemygen_head = 0;
enemygen_tail = 0;
}
void LoadEnemyGens(FILE* f)
{
FreeEnemyGens();
int num = 0;
if (fread(&num, 4, 1, f) != 1)
return;
for (int i = 0; i < num; i++)
{
EnemyGen* eg = (EnemyGen*)malloc(sizeof(EnemyGen));
int r;
r = (int)fread(eg->pos, sizeof(float), 3, f);
r = (int)fread(&eg->alive_max, sizeof(int), 1, f);
r = (int)fread(&eg->revive_min, sizeof(int), 1, f);
r = (int)fread(&eg->revive_max, sizeof(int), 1, f);
r = (int)fread(&eg->armor, sizeof(int), 1, f);
r = (int)fread(&eg->helmet, sizeof(int), 1, f);
r = (int)fread(&eg->shield, sizeof(int), 1, f);
r = (int)fread(&eg->sword, sizeof(int), 1, f);
r = (int)fread(&eg->crossbow, sizeof(int), 1, f);
eg->prev = 0;
eg->next = enemygen_head;
if (enemygen_head)
enemygen_head->prev = eg;
else
enemygen_tail = 0;
enemygen_head = eg;
}
}
void SaveEnemyGens(FILE* f)
{
int num = 0;
EnemyGen* eg = enemygen_head;
while (eg)
{
num++;
eg = eg->next;
}
fwrite(&num, 4, 1, f);
eg = enemygen_head;
while (eg)
{
fwrite(eg->pos, sizeof(float), 3, f);
fwrite(&eg->alive_max, sizeof(int), 1, f);
fwrite(&eg->revive_min, sizeof(int), 1, f);
fwrite(&eg->revive_max, sizeof(int), 1, f);
fwrite(&eg->armor, sizeof(int), 1, f);
fwrite(&eg->helmet, sizeof(int), 1, f);
fwrite(&eg->shield, sizeof(int), 1, f);
fwrite(&eg->sword, sizeof(int), 1, f);
fwrite(&eg->crossbow, sizeof(int), 1, f);
eg = eg->next;
}
}