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 "../collisions.h"
23 #include "../entity.h"
24 #include "../graphics/animation.h"
25 #include "../hud.h"
26 #include "../inventory.h"
27 #include "../map.h"
28 #include "../system/error.h"
29 #include "../system/properties.h"
30 #include "../world/save_point.h"
31 #include "key_items.h"
32 
33 extern Entity *self, player;
34 extern Game game;
35 
36 static void createSave(int);
37 static void savePointWait(void);
38 
addPortableSave(int x,int y,char * name)39 Entity *addPortableSave(int x, int y, char *name)
40 {
41 	Entity *e = getFreeEntity();
42 
43 	if (e == NULL)
44 	{
45 		showErrorAndExit("No free slots to add a Portable Save");
46 	}
47 
48 	loadProperties(name, e);
49 
50 	e->x = x;
51 	e->y = y;
52 
53 	e->type = KEY_ITEM;
54 
55 	e->face = RIGHT;
56 
57 	e->action = &doNothing;
58 
59 	e->touch = &keyItemTouch;
60 
61 	e->fallout = &keyItemFallout;
62 
63 	e->activate = &createSave;
64 
65 	e->draw = &drawLoopingAnimationToMap;
66 
67 	setEntityAnimation(e, "LITTLE");
68 
69 	return e;
70 }
71 
createSave(int val)72 static void createSave(int val)
73 {
74 	Entity *e;
75 
76 	if (game.status == IN_GAME && player.element != WATER)
77 	{
78 		player.inUse = FALSE;
79 
80 		if (game.gameType != NORMAL || strcmpignorecase(getMapFilename(), self->requires) != 0)
81 		{
82 			setInfoBoxMessage(120, 255, 255, 255, _("You cannot use this here"));
83 		}
84 
85 		else if (player.standingOn != NULL || isSpaceEmpty(&player) != NULL)
86 		{
87 			setInfoBoxMessage(120, 255, 255, 255, _("You can only use this item in an empty space"));
88 		}
89 
90 		else
91 		{
92 			e = addEntity(*self, player.x, player.y);
93 
94 			e->touch = &entityTouch;
95 
96 			e->thinkTime = 180;
97 
98 			e->x += (player.w - e->w) / 2;
99 
100 			e->mental = 0;
101 
102 			e->action = &savePointWait;
103 
104 			removeInventoryItemByObjectiveName(self->objectiveName);
105 		}
106 
107 		player.inUse = TRUE;
108 	}
109 }
110 
savePointWait()111 static void savePointWait()
112 {
113 	Entity *e;
114 
115 	self->thinkTime--;
116 
117 	if (self->thinkTime % 15 == 0)
118 	{
119 		setEntityAnimation(self, self->mental == 0 ? "BIG" : "LITTLE");
120 
121 		self->mental = self->mental == 0 ? 1 : 0;
122 	}
123 
124 	if (self->thinkTime <= 0)
125 	{
126 		e = addSavePoint(self->x, self->y);
127 
128 		e->y = self->y + self->h;
129 
130 		e->y -= e->h;
131 
132 		self->inUse = FALSE;
133 	}
134 
135 	checkToMap(self);
136 }
137