1 /*
2 Copyright (C) 2009-2021 Parallel Realities
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 */
19 
20 #include "../headers.h"
21 
22 #include "../graphics/animation.h"
23 #include "../system/error.h"
24 #include "../system/properties.h"
25 
26 extern Entity *self;
27 
28 static Target target[MAX_TARGETS];
29 static Entity targetEntity;
30 
freeTargets()31 void freeTargets()
32 {
33 	/* Clear the list */
34 
35 	memset(target, 0, sizeof(Target) * MAX_TARGETS);
36 }
37 
addTarget(int x,int y,char * name)38 Target *addTarget(int x, int y, char *name)
39 {
40 	int i;
41 
42 	targetEntity.inUse = TRUE;
43 
44 	loadProperties("lift/lift_target", &targetEntity);
45 
46 	setEntityAnimation(&targetEntity, "STAND");
47 
48 	targetEntity.draw = &drawLoopingAnimationToMap;
49 
50 	for (i=0;i<MAX_TARGETS;i++)
51 	{
52 		if (strcmpignorecase(name, target[i].name) == 0)
53 		{
54 			showErrorAndExit("Duplicate target name %s", name);
55 		}
56 	}
57 
58 	/* Loop through all the targets and find a free slot */
59 
60 	for (i=0;i<MAX_TARGETS;i++)
61 	{
62 		if (target[i].active == FALSE)
63 		{
64 			memset(&target[i], 0, sizeof(Target));
65 
66 			target[i].active = TRUE;
67 
68 			target[i].x = x;
69 			target[i].y = y;
70 
71 			STRNCPY(target[i].name, name, sizeof(target[i].name));
72 
73 			return &target[i];
74 		}
75 	}
76 
77 	showErrorAndExit("No free slots to add a target");
78 
79 	return NULL;
80 }
81 
drawTargets()82 void drawTargets()
83 {
84 	int i;
85 
86 	self = &targetEntity;
87 
88 	self->alpha = 255;
89 
90 	for (i=0;i<MAX_TARGETS;i++)
91 	{
92 		if (target[i].active == TRUE)
93 		{
94 			self->x = target[i].x;
95 			self->y = target[i].y;
96 
97 			self->draw();
98 		}
99 	}
100 }
101 
getTargetByName(char * name)102 Target *getTargetByName(char *name)
103 {
104 	int i;
105 
106 	for (i=0;i<MAX_TARGETS;i++)
107 	{
108 		if (target[i].active == TRUE && strcmpignorecase(target[i].name, name) == 0)
109 		{
110 			return &target[i];
111 		}
112 	}
113 
114 	#if DEV == 1
115 		printf("Could not find target %s\n", name);
116 	#endif
117 
118 	return NULL;
119 }
120 
getTargets()121 Target *getTargets()
122 {
123 	return target;
124 }
125 
writeTargetsToFile(FILE * fp)126 void writeTargetsToFile(FILE *fp)
127 {
128 	int i;
129 
130 	for (i=0;i<MAX_TARGETS;i++)
131 	{
132 		if (target[i].active == TRUE)
133 		{
134 			fprintf(fp, "{\n");
135 			fprintf(fp, "TYPE TARGET\n");
136 			fprintf(fp, "NAME %s\n", target[i].name);
137 			fprintf(fp, "START_X %d\n", target[i].x);
138 			fprintf(fp, "START_Y %d\n", target[i].y);
139 			fprintf(fp, "}\n\n");
140 		}
141 	}
142 }
143