Debian Codegraph @

editor/ObjectClipboard.cpp

raw
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 ObjectClipboard.cpp
21 This file contains the cut,copy,paste, and delete routines used when you right click in the view
22 */
23
24 #include "stdafx.h"
25
26 #include "TextureGrWnd.h"
27
28 #include "editor.h"
29 #include "render.h"
30 #include "manage.h"
31 #include "gr.h"
32 #include "gameloop.h"
33 #include "descent.h"
34 #include "terrain.h"
35 #include "HTexture.h"
36 #include "terraindialog.h"
37 #include "room.h"
38 #include <string.h>
39
40 #include <assert.h>
41
42 #include "mono.h"
43 #include "3d.h"
44 #include "gr.h"
45 #include "gamepath.h"
46 #include "ObjectClipboard.h"
47 #include "HObject.h"
48
49 object ClipBoardObject;
50 bool ObjectInBuffer;
51
52 // Returns the number if OBJ_PLAYER in the Objects list
53 int GetNumberPlayerObjs() {
54 int count;
55 int i;
56 count = 0;
57
58 for (i = 0; i <= Highest_object_index; i++) {
59 if (Objects[i].type == OBJ_PLAYER)
60 count++;
61 }
62 mprintf(0, "GetNumberPlayerObjs returns %d\n", count);
63 return count;
64 }
65
66 // Makes a copy of the current object to the clipboard object, then deletes this object
67 // Returns true on success
68 bool ObjectCut() {
69 mprintf(0, "ObjectCut()\n");
70 if (Cur_object_index != -1) {
71 int objnum = Cur_object_index;
72 int num_player_objects = GetNumberPlayerObjs();
73 if ((num_player_objects == 1) && Objects[objnum].type == OBJ_PLAYER) {
74 mprintf(0, "Oops, Can't delete the only player...sorry\n");
75 return false;
76 }
77 mprintf(0, "Attempting to cut object #%d into buffer...", objnum);
78 memcpy(&ClipBoardObject, &Objects[objnum], sizeof(object));
79 mprintf(0, "Done!\n");
80 ObjectInBuffer = true;
81 HObjectDelete();
82 return true;
83 }
84 return false;
85 }
86
87 // Makes a copy of the current object to the clipboard object
88 // Returns true on success
89 bool ObjectCopy() {
90 mprintf(0, "ObjectCopy()\n");
91 if (Cur_object_index != -1) {
92 int objnum = Cur_object_index;
93 mprintf(0, "Attempting to copy object #%d into buffer...", objnum);
94 memcpy(&ClipBoardObject, &Objects[objnum], sizeof(object));
95 mprintf(0, "Done!\n");
96 ObjectInBuffer = true;
97 return true;
98 }
99
100 return false;
101 }
102
103 // Just deletes the current object
104 // Returns true on success
105 bool ObjectDelete() {
106 mprintf(0, "ObjectDelete()\n");
107 if (Cur_object_index != -1) {
108 int objnum = Cur_object_index;
109 int num_player_objects = GetNumberPlayerObjs();
110 if ((num_player_objects == 1) && Objects[objnum].type == OBJ_PLAYER) {
111 mprintf(0, "Oops, Can't delete the only player...sorry\n");
112 return false;
113 }
114 HObjectDelete();
115 return true;
116 }
117 return false;
118 }
119
120 // Pastes the object in the Clipboard to the middle of the current segment
121 // Returns true on success
122 bool ObjectPaste() {
123 mprintf(0, "ObjectPaste()\n");
124
125 Int3(); // I'm not so sure about this routine -- by copying everything over, it may copy
126 // some data it shouldn't, such as script info.
127
128 if (!ObjectInBuffer)
129 return false;
130
131 int16_t next, prev;
132 int roomnum;
133 vector pos;
134 matrix orient;
135 vector last_pos;
136 float creation_time;
137 int handle;
138
139 if (!HObjectPlace(ClipBoardObject.type, ClipBoardObject.id)) {
140 return false;
141 }
142 mprintf(0, "Saving data...\n");
143 handle = Objects[Cur_object_index].handle;
144 next = Objects[Cur_object_index].next;
145 prev = Objects[Cur_object_index].prev;
146 roomnum = Objects[Cur_object_index].roomnum;
147 pos = Objects[Cur_object_index].pos;
148 orient = Objects[Cur_object_index].orient;
149 last_pos = Objects[Cur_object_index].last_pos;
150 creation_time = Objects[Cur_object_index].creation_time;
151
152 mprintf(0, "Overwriting with buffer data\n");
153 memcpy(&Objects[Cur_object_index], &ClipBoardObject, sizeof(object));
154
155 mprintf(0, "Restoring data...\n");
156 Objects[Cur_object_index].handle = handle;
157 Objects[Cur_object_index].next = next;
158 Objects[Cur_object_index].prev = prev;
159 Objects[Cur_object_index].roomnum = roomnum;
160 Objects[Cur_object_index].pos = pos;
161 ObjSetOrient(&Objects[Cur_object_index], &orient);
162 Objects[Cur_object_index].last_pos = last_pos;
163 Objects[Cur_object_index].creation_time = creation_time;
164
165 return true;
166 }