Debian Codegraph @

similar/editor/kgame.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Game Loading editor functions
23 *
24 */
25
26
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "inferno.h"
31 #include "editor.h"
32 #include "ui.h"
33 #include "gamesave.h"
34 #include "object.h"
35 #include "gameseq.h"
36 #include "gameseg.h"
37 #include "kdefs.h"
38 #include "d_levelstate.h"
39
40 namespace {
41
42 static std::array<char, PATH_MAX> game_filename{"*." DXX_LEVEL_FILE_EXTENSION};
43
44 static void checkforgamext(std::array<char, PATH_MAX> &f)
45 {
46 int i;
47
48 for (i=1; f[i]; i++ )
49 {
50 if (f[i]=='.') return;
51
52 if ((f[i]==' '||f[i]==0) )
53 {
54 f[i]='.';
55 f[i+1]='L';
56 f[i+2]= 'V';
57 f[i+3]= 'L';
58 f[i+4]=0;
59 return;
60 }
61 }
62
63 if (i < 123)
64 {
65 f[i]='.';
66 f[i+1]='L';
67 f[i+2]= 'V';
68 f[i+3]= 'L';
69 f[i+4]=0;
70 return;
71 }
72 }
73
74 //these variables store the "permanant" player position, which overrides
75 //whatever the player's position happens to be when the game is saved
76 segnum_t Perm_player_segnum=segment_none; //-1 means position not set
77 vms_vector Perm_player_position;
78 vms_matrix Perm_player_orient;
79
80 }
81
82 //set the player's "permanant" position from the current position
83 int SetPlayerPosition()
84 {
85 Perm_player_position = ConsoleObject->pos;
86 Perm_player_orient = ConsoleObject->orient;
87 Perm_player_segnum = ConsoleObject->segnum;
88
89 editor_status("Player initial position set");
90 return 0;
91 }
92
93 // Save game
94 // returns 1 if successful
95 // returns 0 if unsuccessful
96 int SaveGameData()
97 {
98 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
99 auto &Objects = LevelUniqueObjectState.Objects;
100 auto &Vertices = LevelSharedVertexState.get_vertices();
101 auto &vmobjptr = Objects.vmptr;
102 auto &vmobjptridx = Objects.vmptridx;
103 if (gamestate == editor_gamestate::unsaved) {
104 if (ui_messagebox(-2, -2, 2, "Game State has not been saved...\nContinue?\n", "NO", "Yes") == 1)
105 return 0;
106 }
107
108 if (ui_get_filename( game_filename, "*." DXX_LEVEL_FILE_EXTENSION, "Save Level" )) {
109 int saved_flag;
110 vms_vector save_pos = ConsoleObject->pos;
111 vms_matrix save_orient = ConsoleObject->orient;
112 auto save_segnum = ConsoleObject->segnum;
113
114 checkforgamext(game_filename);
115
116 if (Perm_player_segnum > Highest_segment_index)
117 Perm_player_segnum = segment_none;
118
119 auto &vcvertptr = Vertices.vcptr;
120 if (Perm_player_segnum!=segment_none) {
121 if (get_seg_masks(vcvertptr, Perm_player_position, vcsegptr(Perm_player_segnum), 0).centermask == sidemask_t{})
122 {
123 ConsoleObject->pos = Perm_player_position;
124 ConsoleObject->orient = Perm_player_orient;
125 obj_relink(vmobjptr, vmsegptr, vmobjptridx(ConsoleObject), vmsegptridx(Perm_player_segnum));
126 }
127 else
128 Perm_player_segnum=segment_none; //position was bogus
129 }
130 saved_flag = save_level(
131 #if DXX_BUILD_DESCENT == 2
132 LevelSharedSegmentState.DestructibleLights,
133 #endif
134 game_filename.data());
135 if (Perm_player_segnum!=segment_none) {
136
137 if (save_segnum > Highest_segment_index)
138 save_segnum = {};
139
140 ConsoleObject->pos = save_pos;
141 const auto &&save_segp = vmsegptridx(save_segnum);
142 auto found_save_segnum = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, save_pos, save_segp);
143 if (found_save_segnum == segment_none) {
144 found_save_segnum = save_segp;
145 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
146 auto &Vertices = LevelSharedVertexState.get_vertices();
147 auto &vcvertptr = Vertices.vcptr;
148 save_pos = compute_segment_center(vcvertptr, save_segp);
149 }
150
151 obj_relink(vmobjptr, vmsegptr, vmobjptridx(ConsoleObject), found_save_segnum);
152 ConsoleObject->orient = save_orient;
153 }
154 if (saved_flag)
155 return 0;
156 mine_changed = 0;
157 gamestate = editor_gamestate::none;
158 }
159 return 1;
160 }
161
162 // returns 1 if successful
163 // returns 0 if unsuccessful
164 int LoadGameData()
165 {
166 if (SafetyCheck()) {
167 if (ui_get_filename( game_filename, "*." DXX_LEVEL_FILE_EXTENSION, "Load Level" ))
168 {
169 checkforgamext(game_filename);
170 if (load_level(
171 #if DXX_BUILD_DESCENT == 2
172 LevelSharedSegmentState.DestructibleLights,
173 #endif
174 game_filename.data()))
175 return 0;
176 Current_level_num = 1; // assume level 1
177 gamestate = editor_gamestate::none;
178 Update_flags = UF_WORLD_CHANGED;
179 Perm_player_position = ConsoleObject->pos;
180 Perm_player_orient = ConsoleObject->orient;
181 Perm_player_segnum = ConsoleObject->segnum;
182 }
183 }
184 return 1;
185 }
186
187 //called whenever a new mine is created, so new mine doesn't get name
188 //of last saved mine as default
189 void ResetFilename()
190 {
191 strcpy(game_filename.data(), "*.LVL");
192 }
193