| 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 |
* Functions to change entire mines. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include "dxxerror.h" |
| 30 |
#include "strutil.h" |
| 31 |
#include "inferno.h" |
| 32 |
#include "editor.h" |
| 33 |
#include "editor/esegment.h" |
| 34 |
#include "editor/medmisc.h" |
| 35 |
#include "ui.h" |
| 36 |
#include "texpage.h" // For texpage_goto_first |
| 37 |
#include "segment.h" |
| 38 |
#include "kdefs.h" |
| 39 |
#include "info.h" |
| 40 |
#include "game.h" |
| 41 |
#include "physfsx.h" |
| 42 |
#include "gameseq.h" |
| 43 |
#include "object.h" |
| 44 |
|
| 45 |
namespace dcx { |
| 46 |
|
| 47 |
mine_filename_type mine_filename{{"*.MIN"}}; |
| 48 |
|
| 49 |
namespace { |
| 50 |
|
| 51 |
static mine_filename_type sit_filename{{"*.SIT"}}; |
| 52 |
|
| 53 |
#define MAX_NAME_LENGTH PATH_MAX |
| 54 |
|
| 55 |
// See if filename f contains an extent. If not, add extent ext. |
| 56 |
static void checkforext(mine_filename_type &f, const char (&ext)[4]) |
| 57 |
{ |
| 58 |
int i; |
| 59 |
|
| 60 |
for (i=1; i<MAX_NAME_LENGTH; i++ ) { |
| 61 |
if (f[i]=='.') |
| 62 |
return; |
| 63 |
|
| 64 |
if ((f[i] == ' ') || (f[i]==0) ) { |
| 65 |
f[i] = '.'; |
| 66 |
f[i+1] = ext[0]; |
| 67 |
f[i+2] = ext[1]; |
| 68 |
f[i+3] = ext[2]; |
| 69 |
f[i+4] = 0; |
| 70 |
return; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if (i < 123) { |
| 75 |
f[i] = '.'; |
| 76 |
f[i+1] = ext[0]; |
| 77 |
f[i+2] = ext[1]; |
| 78 |
f[i+3] = ext[2]; |
| 79 |
f[i+4] = 0; |
| 80 |
return; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// See if filename f contains an extent. If not, add extent ext. |
| 85 |
static void set_extension(mine_filename_type &f, const char (&ext)[4]) |
| 86 |
{ |
| 87 |
int i; |
| 88 |
|
| 89 |
for (i=1; i<MAX_NAME_LENGTH-4; i++ ) { |
| 90 |
if ((f[i]=='.') || (f[i] == ' ') || (f[i]==0) ) { |
| 91 |
f[i] = '.'; |
| 92 |
f[i+1] = ext[0]; |
| 93 |
f[i+2] = ext[1]; |
| 94 |
f[i+3] = ext[2]; |
| 95 |
f[i+4] = 0; |
| 96 |
return; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
int SaveMine() |
| 106 |
{ |
| 107 |
// Save mine |
| 108 |
// med_save_mine("TEMP.MIN"); |
| 109 |
if (ui_get_filename( mine_filename, "*.MIN", "SAVE MINE" )) |
| 110 |
{ |
| 111 |
checkforext(mine_filename, "MIN"); |
| 112 |
if (med_save_mine(mine_filename)) |
| 113 |
return 0; |
| 114 |
mine_changed = 0; |
| 115 |
} |
| 116 |
|
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
int CreateNewMine() |
| 121 |
{ |
| 122 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 123 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 124 |
if (SafetyCheck()) { |
| 125 |
texpage_goto_first(); |
| 126 |
create_new_mine(); |
| 127 |
LargeView.ev_matrix = vmd_identity_matrix; //FrontView.ev_matrix; |
| 128 |
auto &vcvertptr = Vertices.vcptr; |
| 129 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 130 |
Update_flags = UF_WORLD_CHANGED; |
| 131 |
SetPlayerFromCurseg(); |
| 132 |
SetPlayerPosition(); //say default is permanant position |
| 133 |
mine_changed = 0; |
| 134 |
Found_segs.clear(); |
| 135 |
Selected_segs.clear(); |
| 136 |
med_compress_mine(); |
| 137 |
gamestate = editor_gamestate::none; |
| 138 |
init_info = 1; |
| 139 |
ResetFilename(); |
| 140 |
Game_mode = game_mode_flags::normal; |
| 141 |
Current_level_num = 1; // make level 1 |
| 142 |
} |
| 143 |
return 1; |
| 144 |
} |
| 145 |
|
| 146 |
int MineMenu() |
| 147 |
{ |
| 148 |
int x; |
| 149 |
static const char *const MenuItems[] = { "New mine", |
| 150 |
"Load mine", |
| 151 |
"Save mine", |
| 152 |
"Print mine", |
| 153 |
"Redraw mine" }; |
| 154 |
|
| 155 |
x = MenuX( -1, -1, 5, MenuItems ); |
| 156 |
|
| 157 |
switch( x ) |
| 158 |
{ |
| 159 |
case 1: // New |
| 160 |
CreateNewMine(); |
| 161 |
break; |
| 162 |
case 2: // Load |
| 163 |
//@@LoadMine(); |
| 164 |
break; |
| 165 |
case 3: // Save |
| 166 |
SaveMine(); |
| 167 |
break; |
| 168 |
case 4: // Print |
| 169 |
break; |
| 170 |
case 5: // Redraw |
| 171 |
Update_flags = UF_ALL; |
| 172 |
break; |
| 173 |
} |
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
// ----------------------------------------------------------------------------- |
| 178 |
// returns 1 if error, else 0 |
| 179 |
static int med_load_situation(mine_filename_type &filename) |
| 180 |
{ |
| 181 |
if (filename[0] == 97) |
| 182 |
Int3(); |
| 183 |
Int3(); |
| 184 |
|
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
// ----------------------------------------------------------------------------- |
| 189 |
static int med_save_situation(const mine_filename_type &filename) |
| 190 |
{ |
| 191 |
auto &&[SaveFile, physfserr] = PHYSFSX_openWriteBuffered(filename.data()); |
| 192 |
if (!SaveFile) { |
| 193 |
char ErrorMessage[512]; |
| 194 |
|
| 195 |
snprintf(ErrorMessage, sizeof(ErrorMessage), "ERROR: Unable to open %.440s: %s", filename.data(), PHYSFS_getErrorByCode(physfserr)); |
| 196 |
ui_messagebox( -2, -2, 1, ErrorMessage, "Ok" ); |
| 197 |
return 1; |
| 198 |
} |
| 199 |
|
| 200 |
// Write mine name. |
| 201 |
const auto path = d_splitpath(filename.data()); |
| 202 |
PHYSFSX_printf(SaveFile, "%.*s.min\n", DXX_ptrdiff_cast_int(path.base_end - path.base_start), path.base_start); |
| 203 |
|
| 204 |
// Write player position. |
| 205 |
PHYSFSX_printf(SaveFile, "%x %x %x\n",static_cast<unsigned>(ConsoleObject->pos.x),static_cast<unsigned>(ConsoleObject->pos.y),static_cast<unsigned>(ConsoleObject->pos.z)); |
| 206 |
|
| 207 |
// Write player orientation. |
| 208 |
PHYSFSX_printf(SaveFile, "%8x %8x %8x\n",static_cast<unsigned>(ConsoleObject->orient.rvec.x),static_cast<unsigned>(ConsoleObject->orient.rvec.y),static_cast<unsigned>(ConsoleObject->orient.rvec.z)); |
| 209 |
PHYSFSX_printf(SaveFile, "%8x %8x %8x\n",static_cast<unsigned>(ConsoleObject->orient.uvec.x),static_cast<unsigned>(ConsoleObject->orient.uvec.y),static_cast<unsigned>(ConsoleObject->orient.uvec.z)); |
| 210 |
PHYSFSX_printf(SaveFile, "%8x %8x %8x\n",static_cast<unsigned>(ConsoleObject->orient.fvec.x),static_cast<unsigned>(ConsoleObject->orient.fvec.y),static_cast<unsigned>(ConsoleObject->orient.fvec.z)); |
| 211 |
PHYSFSX_printf(SaveFile, "%i\n", ConsoleObject->segnum); |
| 212 |
return 1; |
| 213 |
} |
| 214 |
|
| 215 |
// ----------------------------------------------------------------------------- |
| 216 |
int SaveSituation(void) |
| 217 |
{ |
| 218 |
if (ui_get_filename( sit_filename, "*.SIT", "Save Situation" )) { |
| 219 |
set_extension(sit_filename, "MIN"); |
| 220 |
if (med_save_mine(sit_filename)) { |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
set_extension(sit_filename, "SIT"); |
| 225 |
if (med_save_situation(sit_filename)) |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
return 1; |
| 230 |
} |
| 231 |
|
| 232 |
// ----------------------------------------------------------------------------- |
| 233 |
// Load a situation file which consists of x,y,z, orientation matrix, mine name. |
| 234 |
int LoadSituation(void) |
| 235 |
{ |
| 236 |
if (SafetyCheck()) { |
| 237 |
if (ui_get_filename( sit_filename, "*.sit", "Load Situation" )) { |
| 238 |
checkforext(sit_filename, "SIT"); |
| 239 |
if (med_load_situation(sit_filename)) |
| 240 |
return 0; |
| 241 |
// set_view_target_from_segment(Cursegp); |
| 242 |
Update_flags = UF_WORLD_CHANGED; |
| 243 |
// SetPlayerFromCurseg(); |
| 244 |
med_compress_mine(); |
| 245 |
init_info = 1; |
| 246 |
mine_changed = 0; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return 1; |
| 251 |
} |
| 252 |
|