1 /*
2     C-Dogs SDL
3     A port of the legendary (and fun) action/arcade cdogs.
4     Copyright (C) 1995 Ronny Wester
5     Copyright (C) 2003 Jeremy Chin
6     Copyright (C) 2003-2007 Lucas Martin-King
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22     This file incorporates work covered by the following copyright and
23     permission notice:
24 
25     Copyright (c) 2013-2016, Cong Xu
26     All rights reserved.
27 
28     Redistribution and use in source and binary forms, with or without
29     modification, are permitted provided that the following conditions are met:
30 
31     Redistributions of source code must retain the above copyright notice, this
32     list of conditions and the following disclaimer.
33     Redistributions in binary form must reproduce the above copyright notice,
34     this list of conditions and the following disclaimer in the documentation
35     and/or other materials provided with the distribution.
36 
37     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47     POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "gamedata.h"
50 
51 #include <assert.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <ctype.h>
56 
57 #include <tinydir/tinydir.h>
58 
59 #include "actors.h"
60 #include "config.h"
61 #include "defs.h"
62 #include "keyboard.h"
63 #include "log.h"
64 #include "music.h"
65 #include "objs.h"
66 #include "pickup.h"
67 #include "player_template.h"
68 #include "quick_play.h"
69 #include "sys_config.h"
70 #include "utils.h"
71 
72 Campaign gCampaign;
73 
74 struct MissionOptions gMission;
75 
76 struct SongDef *gGameSongs = NULL;
77 struct SongDef *gMenuSongs = NULL;
78 
79 
CampaignLoad(Campaign * co,const CampaignEntry * entry)80 bool CampaignLoad(Campaign *co, const CampaignEntry *entry)
81 {
82 	CASSERT(!co->IsLoaded, "loading campaign without unloading last one");
83 	// Note: use the mode already set by the menus
84 	const GameMode mode = co->Entry.Mode;
85 	CampaignEntryCopy(&co->Entry, entry);
86 	co->Entry.Mode = mode;
87 	CampaignSettingInit(&co->Setting);
88 	if (entry->Mode == GAME_MODE_QUICK_PLAY)
89 	{
90 		SetupQuickPlayCampaign(&co->Setting, false);
91 		co->IsLoaded = true;
92 	}
93 	else
94 	{
95 		// Normalise the path
96 		char buf[CDOGS_PATH_MAX];
97 		GetDataFilePath(buf, entry->Path);
98 		if (MapNewLoad(buf, &co->Setting))
99 		{
100 			LOG(LM_MAIN, LL_ERROR, "failed to load campaign %s!", buf);
101 			CASSERT(false, "Failed to load campaign");
102 		}
103 		else
104 		{
105 			co->IsLoaded = true;
106 		}
107 	}
108 
109 	if (co->IsLoaded)
110 	{
111 		LOG(LM_MAIN, LL_INFO, "loaded campaign/dogfight");
112 	}
113 	return co->IsLoaded;
114 }
CampaignUnload(Campaign * co)115 void CampaignUnload(Campaign *co)
116 {
117 	co->MissionIndex = 0;
118 	co->IsLoaded = false;
119 	co->IsClient = false;	// TODO: select is client from menu
120 	co->OptionsSet = false;
121 	co->IsComplete = false;
122 	gCampaign.IsQuit = false;
123 	CampaignEntryTerminate(&co->Entry);
124 }
125 
MissionOptionsInit(struct MissionOptions * mo)126 void MissionOptionsInit(struct MissionOptions *mo)
127 {
128 	memset(mo, 0, sizeof *mo);
129 	CArrayInit(&mo->Weapons, sizeof(WeaponClass *));
130 }
MissionOptionsTerminate(struct MissionOptions * mo)131 void MissionOptionsTerminate(struct MissionOptions *mo)
132 {
133 	ActorsTerminate();
134 	ObjsTerminate();
135 	MobObjsTerminate();
136 	PickupsTerminate();
137 	ParticlesTerminate(&gParticles);
138 	WatchesTerminate();
139 	CA_FOREACH(PlayerData, p, gPlayerDatas)
140 		p->ActorUID = -1;
141 	CA_FOREACH_END()
142 	gMission.HasStarted = false;
143 	gMission.HasBegun = false;
144 	CArrayTerminate(&mo->Weapons);
145 
146 	memset(mo, 0, sizeof *mo);
147 }
148 
149 
GameIsMouseUsed(void)150 bool GameIsMouseUsed(void)
151 {
152 	CA_FOREACH(const PlayerData, p, gPlayerDatas)
153 		if (p->IsLocal && p->inputDevice == INPUT_DEVICE_MOUSE)
154 		{
155 			const TActor *a = ActorGetByUID(p->ActorUID);
156 			if (a == NULL) continue;
157 			if (a->dead) continue;
158 			return true;
159 		}
160 	CA_FOREACH_END()
161 	return false;
162 }
163