Debian Codegraph @

Descent3/matcen.cpp

raw symbols SCIP
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 * $Logfile: /DescentIII/main/matcen.cpp $
21 * $Revision: 71 $
22 * $Date: 4/19/00 5:35p $
23 * $Author: Matt $
24 *
25 * Matcen code
26 *
27 * $Log: /DescentIII/main/matcen.cpp $
28 *
29 * 71 4/19/00 5:35p Matt
30 * From Duane for 1.4
31 * Added include
32 *
33 * 70 3/20/00 12:14p Matt
34 * Merge of Duane's post-1.3 changes.
35 * Check for bad array index. This change duplicates Jason's fix of the
36 * same bug, but Duane's code looks a little more thorough.
37 *
38 * 69 1/24/00 11:49p Jason
39 * Fixed array indexing error with matcens
40 *
41 * 68 10/21/99 4:50p Matt
42 * Mac merge
43 *
44 * 67 8/15/99 12:22p Gwar
45 * more matcen support in NEWEDITOR (almost done)
46 *
47 * 66 8/12/99 7:55p Gwar
48 * fixed a compiler warning
49 *
50 * 65 8/11/99 9:15p Gwar
51 * matcen support for NEWEDITOR
52 *
53 * 64 7/28/99 4:05p Kevin
54 * Mac
55 *
56 * 63 5/19/99 3:24p Jason
57 * fixed wrong ordering of InitObjectScripts and MultiSendObject
58 *
59 * 62 5/10/99 10:22p Ardussi
60 * changes to compile on Mac
61 *
62 * 61 5/09/99 1:15a Jason
63 * got rid of weird post production effect
64 *
65 * 60 5/05/99 4:51p Jason
66 * don't tile matcens so much
67 *
68 * 59 5/04/99 8:51p Chris
69 * Fixed homing missile problem (a PTMC matcen created robot would have a
70 * parent of a non-AI object). Homers use the ultimate parent. Fun bug.
71 * :)
72 *
73 * 58 5/03/99 1:15p Jason
74 * fixed matcen procedural effect
75 *
76 * 57 5/02/99 10:26p Matt
77 * Fixed silly little bug
78 *
79 * 56 5/02/99 7:30p Jason
80 * added "none" type procedural creation effect
81 *
82 * 55 5/02/99 6:55p Jeff
83 * fixed bug where a sound based off an object would not play because the
84 * object wasn't created yet (Jason)
85 *
86 * 54 4/29/99 4:50p Chris
87 * Fixed matcen and Osiris created objects so thier ambient sounds play
88 *
89 * 53 4/25/99 3:16p Chris
90 * Fixed a coop bug with matcen functioning on the server
91 *
92 * 52 4/23/99 11:28a Jason
93 * fixed matcen viseffect problem
94 *
95 * 51 4/21/99 11:05a Kevin
96 * new ps_rand and ps_srand to replace rand & srand
97 *
98 * 50 4/19/99 3:45a Jeff
99 * fixed min/max problem
100 *
101 * 49 4/18/99 10:55p Chris
102 * matcens now have defualts sounds
103 *
104 * 48 4/14/99 2:51a Jeff
105 * fixed some case mismatched #includes
106 *
107 * 47 4/12/99 6:15p Samir
108 * Sound priorities pass 1
109 *
110 * 46 4/02/99 3:58p Jason
111 * sped up vis effect stuff
112 *
113 * 45 3/22/99 11:24a Chris
114 * Matcen generates an osiris call even if no object is produced
115 *
116 * 44 3/05/99 9:57a Chris
117 * Fixed bugs with walkers
118 *
119 * 43 3/04/99 3:24p Chris
120 * FIxed matcens and walkers
121 *
122 * 42 2/24/99 10:58a Dan
123 * CHRIS - Fixed the external room matcen problem
124 *
125 * 41 2/19/99 4:49p Dan
126 * Matcens *should* be able to be placed on external rooms
127 *
128 * 40 2/19/99 4:41p Dan
129 * External rooms can now be matcens... (Objects are created in the
130 * connection room)
131 *
132 * 39 2/13/99 4:24p Chris
133 * Fixed an off by one bug in num_prod_type
134 *
135 * 38 2/10/99 1:47p Matt
136 * Changed object handle symbolic constants
137 *
138 * 37 2/09/99 1:23p Matt
139 * Added object-generated notification event
140 *
141 */
142
143 #ifdef NEWEDITOR
144 #include "globals.h"
145 #endif
146
147 #include "matcen.h"
148 #include "game.h"
149 #include "sounds.h"
150 #include "ssl_lib.h"
151 #include "string.h"
152 #include "soundload.h"
153 #include "room.h"
154 #include "object.h"
155 #include "objinfo.h"
156 #include "findintersection.h"
157 #ifndef NEWEDITOR
158 #include "multi.h"
159 #endif
160 #include <stdlib.h>
161 #include "fireball.h"
162 #include "weapon.h"
163 #include "polymodel.h"
164 #include "hlsoundlib.h"
165 #include "viseffect.h"
166 #include "viseffect_external.h"
167 #include "damage.h"
168 #include "physics.h"
169 #include "mem.h"
170 #include "ObjScript.h"
171 #ifndef NEWEDITOR
172 #include "player.h"
173 #include "osiris_dll.h"
174 #endif
175 #include "psrand.h"
176 #include "demofile.h"
177
178
179 #include <algorithm>
180
181 // Beginning of the real file
182 #define LEVEL_DATA_MATCEN_VERSION 1
183 #define GAME_DATA_MATCEN_VERSION 1
184
185 #if (defined(EDITOR) || defined(NEWEDITOR))
186
187 char *MatcenEffectStrings[NUM_MATCEN_EFFECTS] = {"Line lightning", "Line sinewave", "Procedural lightning", "None"};
188
189 #endif
190
191 int Num_matcens = 0;
192 matcen *Matcen[MAX_MATCENS];
193 bool Matcen_created = false;
194
195 matcen::matcen() {
196 int i;
197
198 m_prod_mode = MMODE_NOTPROD;
199 m_cur_saturation_count = 0;
200
201 m_num_prod_types = 0;
202 m_control_type = MPC_SCRIPT;
203 m_type = MT_UNASSIGNED;
204 m_creation_effect = MEFFECT_LINE_LIGHTNING;
205
206 // Get lightning handle in case we need it
207 m_creation_texture = FindTextureName("Matcen Lightning");
208 if (m_creation_texture == -1)
209 m_creation_texture = 0;
210
211 m_num_spawn_pnts = 0;
212
213 m_roomnum = MATCEN_ERROR;
214
215 for (i = 0; i < MAX_SPAWN_PNTS; i++) {
216 m_spawn_pnt[i] = MATCEN_ERROR;
217 }
218
219 m_max_prod = 0;
220
221 for (i = 0; i < MAX_PROD_TYPES; i++) {
222 m_prod_type[i] = MATCEN_ERROR;
223 m_prod_time[i] = 1.0f;
224 m_prod_priority[i] = 100;
225 m_max_prod_type[i] = 3;
226 }
227
228 m_sounds[MATCEN_ACTIVE_SOUND] = FindSoundName("AmbMatCenRun");
229 m_sounds[MATCEN_DISABLE_SOUND] = -1;
230 m_sounds[MATCEN_PROD_SOUND] = FindSoundName("AmbMatCenProduce");
231
232 m_speed_multi = 1.0f;
233
234 m_max_alive_children = -1;
235 m_num_alive = 0;
236 m_alive_list = NULL;
237
238 m_preprod_time = 1.5f;
239 m_postprod_time = 1.0f;
240
241 m_status = 0;
242
243 m_create_pnt = Zero_vector;
244 m_create_room = MATCEN_ERROR;
245
246 m_cached_prod_index = -1;
247 m_cached_prod_time = 0.0f;
248
249 Reset();
250 }
251
252 matcen::~matcen() {
253 if (m_alive_list) {
254 mem_free(m_alive_list);
255 }
256 }
257
258 bool matcen::SetMaxProd(int max_p) {
259 if (max_p < -1) {
260 return false;
261 }
262
263 m_max_prod = max_p;
264
265 ComputeNextProdInfo();
266
267 return true;
268 }
269
270 int matcen::GetMaxProd() { return m_max_prod; }
271
272 #ifndef NEWEDITOR
273
274 bool matcen::StartObjProd() {
275 if (m_cached_prod_index < 0 || m_max_prod_type[m_cached_prod_index] >= m_num_prod_type[m_cached_prod_index])
276 ComputeNextProdInfo();
277
278 if (m_status & (MSTAT_DONE_PROD | MSTAT_ACTIVE_PAUSE | MSTAT_DISABLED))
279 return false;
280
281 if (m_prod_mode != MMODE_NOTPROD)
282 return false;
283
284 if (!(m_status & (MSTAT_ACTIVE)))
285 return false;
286
287 if (m_max_prod <= m_num_prod && m_max_prod != -1) {
288 mprintf(0, "MATCEN: Done\n");
289 m_status |= MSTAT_DONE_PROD;
290 return false;
291 }
292
293 if (m_cached_prod_index < 0)
294 return false;
295
296 // Guarentees against designer error
297 pos_state pos;
298 matrix idmat = Identity_matrix;
299
300 int croom = m_create_room;
301 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
302 croom = GetTerrainRoomFromPos(&m_create_pnt);
303 }
304
305 pos.position = &m_create_pnt;
306 pos.orient = &idmat;
307 pos.roomnum = croom;
308
309 Sound_system.StopSoundLooping(Sound_system.Play3dSound(m_sounds[MATCEN_PROD_SOUND], SND_PRIORITY_HIGH, &pos));
310
311 m_status &= ~MSTAT_NEVER_PROD;
312
313 m_prod_mode_time = 0.0f;
314 m_prod_mode = MMODE_PREPROD;
315 m_cur_saturation_count = 0;
316
317 return true;
318 }
319
320 bool matcen::DoObjProd() {
321 int objnum;
322 int p_handle = OBJECT_HANDLE_NONE;
323 bool script_initted = false;
324
325 ASSERT(m_prod_mode == MMODE_PREPROD);
326 ASSERT(m_type == MT_ROOM || m_type == MT_OBJECT);
327
328 if (m_type == MT_OBJECT) {
329 object *parent = ObjGet(m_objref);
330 ASSERT(parent);
331
332 if (parent->control_type == CT_AI || parent->type == OBJ_PLAYER) {
333 p_handle = parent->handle;
334 }
335 }
336
337 ComputeCreatePnt();
338 matrix orient;
339 vector fvec = Player_object->pos - m_create_pnt;
340 vm_NormalizeVector(&fvec);
341
342 vm_VectorToMatrix(&orient, &fvec, NULL, NULL);
343
344 if (m_prod_type[m_cached_prod_index] >= 0) {
345 int croom = m_create_room;
346 Matcen_created = true;
347
348 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
349 croom = GetTerrainRoomFromPos(&m_create_pnt);
350 }
351
352 objnum = ObjCreate(Object_info[m_prod_type[m_cached_prod_index]].type, m_prod_type[m_cached_prod_index], croom,
353 &m_create_pnt, &orient, p_handle);
354 Matcen_created = false;
355
356 if (objnum >= 0 && (Object_info[m_prod_type[m_cached_prod_index]].flags & OIF_CONTROL_AI) &&
357 (Object_info[m_prod_type[m_cached_prod_index]].ai_info->movement_type != MC_FLYING)) {
358 vector gp;
359 object *obj = &Objects[objnum];
360
361 if (PhysCalcGround(&gp, NULL, obj, 0)) {
362 fvi_info hit_info;
363 fvi_query fq;
364 int fate;
365 vector start = obj->pos;
366 vector end = obj->pos - 2000.0f * obj->orient.uvec;
367
368 fq.p0 = &start;
369 fq.p1 = &end;
370 fq.startroom = obj->roomnum;
371
372 fq.rad = 0.0f;
373 fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_POWERUPS | FQ_IGNORE_WEAPONS | FQ_IGNORE_MOVING_OBJECTS;
374 fq.thisobjnum = OBJNUM(obj);
375 fq.ignore_obj_list = NULL;
376
377 fate = fvi_FindIntersection(&fq, &hit_info);
378
379 if (fate != HIT_NONE) {
380 float ps;
381 float pr;
382 float diff;
383
384 ps = (gp - obj->pos) * obj->orient.uvec;
385 pr = (hit_info.hit_pnt - obj->pos) * obj->orient.uvec;
386
387 if (ps != pr) {
388 diff = ps - pr;
389 obj->pos -= diff * obj->orient.uvec;
390 ObjSetPos(obj, &obj->pos, obj->roomnum, NULL, false);
391 obj->flags |= OF_MOVED_THIS_FRAME;
392
393 m_create_pnt = obj->pos;
394 }
395 }
396 }
397 }
398
399 if (objnum >= 0) {
400 // Make it fade in for a second
401 Objects[objnum].effect_info->type_flags |= EF_FADING_IN;
402 Objects[objnum].effect_info->fade_time = 1.0;
403 Objects[objnum].effect_info->fade_max_time = 1.0;
404
405 object *obj = &Objects[objnum];
406
407 if (Game_mode & GM_MULTI) {
408 // ASSERT (Netgame.local_role==LR_SERVER);
409 if (Netgame.local_role == LR_SERVER) {
410 MultiSendObject(&Objects[objnum], 0);
411 }
412 }
413
414 InitObjectScripts(obj);
415 script_initted = true;
416
417 if (IS_GENERIC(obj->type)) {
418 int ambient_sound = Object_info[obj->id].sounds[GSI_AMBIENT];
419 if (ambient_sound != SOUND_NONE_INDEX) {
420 Sound_system.Play3dSound(ambient_sound, SND_PRIORITY_LOWEST, obj);
421 if (Game_mode & GM_MULTI)
422 MultiPlay3dSound(ambient_sound, objnum, SND_PRIORITY_LOW);
423 if (Demo_flags == DF_RECORDING)
424 DemoWrite3DSound(ambient_sound, objnum, SND_PRIORITY_LOW);
425 }
426 }
427 }
428 } else {
429 objnum = -1;
430 }
431
432 // Send notification event
433 tOSIRISEventInfo ei;
434 if (objnum >= 0)
435 ei.evt_matcen_create.it_handle = Objects[objnum].handle;
436 else
437 ei.evt_matcen_create.it_handle = OBJECT_HANDLE_NONE;
438 ei.evt_matcen_create.id = FindMatcenIndex(m_name);
439 Osiris_CallLevelEvent(EVT_MATCEN_CREATE, &ei);
440
441 if (objnum >= 0) {
442 if (!script_initted)
443 InitObjectScripts(&Objects[objnum]);
444 AddToAliveList(Objects[objnum].handle);
445
446 m_last_prod_objref = Objects[objnum].handle;
447 } else {
448 m_last_prod_objref = OBJECT_HANDLE_NONE;
449 }
450
451 m_status |= MSTAT_CREATE_OBJ_FRAME;
452
453 m_last_prod_type_index = m_cached_prod_index;
454 m_num_prod_type[m_cached_prod_index]++;
455 m_num_prod++;
456
457 m_prod_mode_time = 0.0f;
458 m_prod_mode = MMODE_POSTPROD;
459 m_cur_saturation_count = 0;
460
461 return true;
462 }
463
464 bool matcen::DoAliveListFrame() {
465 int i, j;
466
467 for (i = 0; i < m_num_alive; i++) {
468 if (!ObjGet(m_alive_list[i])) {
469 for (j = i; j < m_num_alive - 1; j++) {
470 m_alive_list[j] = m_alive_list[j + 1];
471 }
472
473 m_num_alive--;
474 i--; // Example - 6 is now 5 (after 5 was deleted); so we need to check 5 again...
475 }
476 }
477
478 return true;
479 }
480
481 bool matcen::AddToAliveList(int objref) {
482 if (m_max_alive_children > 0 && m_max_alive_children > m_num_alive) {
483 m_alive_list[m_num_alive++] = objref;
484 }
485
486 return false;
487 }
488
489 bool matcen::FinishObjProd() {
490 ASSERT(m_prod_mode == MMODE_POSTPROD);
491
492 m_prod_mode_time = 0.0f;
493 m_prod_mode = MMODE_NOTPROD;
494 m_cur_saturation_count = 0;
495
496 ComputeNextProdInfo();
497
498 return false;
499 }
500
501 #endif // NEWEDITOR
502
503 bool matcen::SetAttach(int attach) {
504 m_roomnum = attach;
505 ComputeCreatePnt();
506
507 return true;
508 }
509
510 int matcen::GetAttach() { return m_roomnum; }
511
512 char matcen::GetAttachType() { return m_type; }
513
514 bool matcen::SetAttachType(char type) {
515 if (type == MT_OBJECT || type == MT_ROOM || type == MT_UNASSIGNED) {
516 m_type = type;
517 return true;
518 }
519
520 return false;
521 }
522
523 char matcen::GetControlType() { return m_control_type; }
524
525 bool matcen::SetControlType(char type) {
526 if (type >= 0 && type < MAX_MATCEN_CONTROL_TYPES) {
527 m_control_type = type;
528 return true;
529 }
530
531 return false;
532 }
533
534 char matcen::GetNumSpawnPnts() { return m_num_spawn_pnts; }
535
536 bool matcen::SetNumSpawnPnts(char num_s) {
537 if (num_s >= 0 && num_s <= MAX_SPAWN_PNTS) {
538 m_num_spawn_pnts = num_s;
539 ComputeCreatePnt();
540 return true;
541 }
542
543 return false;
544 }
545
546 bool matcen::ComputeCreatePnt() {
547 if (m_status & MSTAT_MANUAL_UPDATE_CREATE_PNT) {
548 return true;
549 } else if (m_type != MT_UNASSIGNED) {
550 float spawn_dist;
551
552 if (m_cached_prod_index >= 0 && m_cached_prod_index < m_num_prod_types && m_prod_type[m_cached_prod_index] >= 0 &&
553 m_prod_type[m_cached_prod_index] <= MAX_OBJECT_TYPES &&
554 Object_info[m_prod_type[m_cached_prod_index]].type != OBJ_NONE) {
555 spawn_dist = Object_info[m_prod_type[m_cached_prod_index]].size + 1.0f;
556 } else {
557 spawn_dist = 8.0f;
558 }
559
560 if (m_type == MT_ROOM) {
561 bool f_computed = false;
562
563 if (!(m_roomnum >= 0 && m_roomnum <= Highest_room_index && Rooms[m_roomnum].used))
564 goto error;
565
566 if (m_num_spawn_pnts) {
567 int i;
568 int num_valid_faces = 0;
569 room *rp = &Rooms[m_roomnum];
570
571 m_create_pnt = Zero_vector;
572
573 for (i = 0; i < m_num_spawn_pnts; i++) {
574 if (m_spawn_pnt[i] >= 0 && m_spawn_pnt[i] < rp->num_faces) {
575 face *fp = &rp->faces[m_spawn_pnt[i]];
576 int y;
577
578 m_spawn_vec[num_valid_faces].x = m_spawn_vec[num_valid_faces].y = m_spawn_vec[num_valid_faces].z = 0;
579
580 for (y = 0; y < fp->num_verts; y++)
581 m_spawn_vec[num_valid_faces] += rp->verts[fp->face_verts[y]];
582
583 m_spawn_vec[num_valid_faces] /= fp->num_verts;
584 m_create_pnt += m_spawn_vec[num_valid_faces];
585
586 m_spawn_normal[num_valid_faces] = fp->normal;
587
588 num_valid_faces++;
589 }
590 }
591
592 if (num_valid_faces) {
593 m_create_pnt /= num_valid_faces;
594
595 for (i = 0; i < num_valid_faces; i++) {
596 // Compute the vector to the m_create_pnt
597 vector to_dist = m_create_pnt - m_spawn_vec[i];
598 float dot = m_spawn_normal[i] * to_dist;
599
600 if (dot < spawn_dist) {
601 float add_dist = spawn_dist - dot;
602 m_create_pnt += (add_dist * m_spawn_normal[i]);
603 }
604 }
605
606 f_computed = true;
607 }
608 }
609
610 if (!f_computed) {
611 m_create_pnt = Rooms[m_roomnum].path_pnt;
612 }
613
614 m_create_room = m_roomnum;
615 } else {
616 ASSERT(m_type == MT_OBJECT);
617 object *master_obj = ObjGet(m_objref);
618 bool f_computed = false;
619
620 if (!master_obj)
621 goto error;
622
623 if (m_num_spawn_pnts) {
624 int i;
625 int num_valid_gps = 0;
626
627 m_create_pnt = Zero_vector;
628
629 for (i = 0; i < m_num_spawn_pnts; i++) {
630 if (WeaponCalcGun(&m_spawn_vec[num_valid_gps], &m_spawn_normal[num_valid_gps], master_obj, m_spawn_pnt[i])) {
631 m_create_pnt += m_spawn_vec[num_valid_gps];
632 num_valid_gps++;
633 }
634 }
635
636 if (num_valid_gps) {
637 m_create_pnt /= num_valid_gps;
638
639 for (i = 0; i < num_valid_gps; i++) {
640 // Compute the vector to the m_create_pnt
641 vector to_dist = m_create_pnt - m_spawn_vec[i];
642 float dot = m_spawn_normal[i] * to_dist;
643
644 if (dot < spawn_dist) {
645 float add_dist = spawn_dist - dot;
646 m_create_pnt += (add_dist * m_spawn_normal[i]);
647 }
648 }
649
650 f_computed = true;
651 }
652 }
653
654 if (!f_computed) {
655 m_create_pnt = master_obj->pos;
656 }
657
658 // Determine the correct creation room
659 fvi_query fq;
660 fvi_info hit_info;
661
662 fq.p0 = &master_obj->pos;
663 fq.startroom = master_obj->roomnum;
664 fq.p1 = &m_create_pnt;
665 fq.rad = 0.0f;
666 fq.thisobjnum = -1;
667 fq.ignore_obj_list = NULL;
668 fq.flags = FQ_IGNORE_WALLS;
669
670 fvi_FindIntersection(&fq, &hit_info);
671 m_create_room = hit_info.hit_room;
672 }
673 }
674
675 error:
676 return false;
677 }
678
679 bool matcen::GetCreatePnt(vector *pnt) {
680 ComputeCreatePnt();
681 *pnt = m_create_pnt;
682 return true;
683 }
684
685 bool matcen::SetCreatePnt(vector *pnt) {
686 if (m_status & MSTAT_MANUAL_UPDATE_CREATE_PNT) {
687 m_create_pnt = *pnt;
688 return true;
689 }
690
691 return false;
692 }
693
694 int matcen::GetCreateRoom() {
695 ComputeCreatePnt();
696
697 return m_create_room;
698 }
699
700 bool matcen::SetCreateRoom(int room) {
701 if ((m_status & MSTAT_MANUAL_UPDATE_CREATE_PNT)) // chrishack -- add room validation
702 {
703 m_create_room = room;
704 return true;
705 }
706
707 return false;
708 }
709
710 int matcen::GetSpawnPnt(char s_index) {
711 if (s_index >= 0 && s_index < MAX_SPAWN_PNTS)
712 return m_spawn_pnt[s_index];
713
714 return MATCEN_ERROR;
715 }
716
717 bool matcen::SetSpawnPnt(char s_index, int s_value) {
718 if (s_index >= 0 && s_index < MAX_SPAWN_PNTS) {
719 m_spawn_pnt[s_index] = s_value;
720 ComputeCreatePnt();
721 return true;
722 }
723
724 return false;
725 }
726
727 bool matcen::SetName(const char *name) {
728 strcpy(m_name, name);
729
730 return true;
731 }
732
733 void matcen::GetName(char *name) { strcpy(name, m_name); }
734
735 void matcen::SaveData(CFILE *fp) {
736 int i, j;
737 int len;
738
739 cf_WriteInt(fp, MATCEN_LOADSAVE_VERSION);
740
741 cf_WriteShort(fp, MAX_SPAWN_PNTS);
742 cf_WriteShort(fp, MAX_PROD_TYPES);
743 cf_WriteShort(fp, MAX_MATCEN_SOUNDS);
744
745 len = strlen(m_name) + 1; // Accounts for NULL charactor
746 cf_WriteShort(fp, len);
747 for (i = 0; i < len; i++) {
748 cf_WriteByte(fp, m_name[i]);
749 }
750
751 cf_WriteByte(fp, m_num_prod_types);
752 cf_WriteByte(fp, m_control_type);
753 cf_WriteByte(fp, m_type);
754 cf_WriteByte(fp, m_creation_effect);
755 cf_WriteShort(fp, m_creation_texture);
756
757 cf_WriteInt(fp, m_num_spawn_pnts);
758
759 cf_WriteInt(fp, m_roomnum);
760
761 cf_WriteFloat(fp, m_create_pnt.x);
762 cf_WriteFloat(fp, m_create_pnt.y);
763 cf_WriteFloat(fp, m_create_pnt.z);
764
765 cf_WriteInt(fp, m_create_room);
766
767 for (i = 0; i < MAX_SPAWN_PNTS; i++) {
768 cf_WriteInt(fp, m_spawn_pnt[i]);
769
770 cf_WriteFloat(fp, m_spawn_vec[i].x);
771 cf_WriteFloat(fp, m_spawn_vec[i].y);
772 cf_WriteFloat(fp, m_spawn_vec[i].x);
773
774 cf_WriteFloat(fp, m_spawn_normal[i].x);
775 cf_WriteFloat(fp, m_spawn_normal[i].y);
776 cf_WriteFloat(fp, m_spawn_normal[i].x);
777 }
778
779 cf_WriteInt(fp, m_max_prod);
780
781 for (i = 0; i < MAX_PROD_TYPES; i++) {
782 int type = m_prod_type[i];
783
784 if (type >= 0) {
785 len = strlen(Object_info[type].name) + 1; // Accounts for NULL charactor
786 cf_WriteShort(fp, len);
787 for (j = 0; j < len; j++) {
788 cf_WriteByte(fp, Object_info[type].name[j]);
789 }
790 } else {
791 cf_WriteShort(fp, 1);
792 cf_WriteByte(fp, '\0');
793 }
794
795 cf_WriteFloat(fp, m_prod_time[i]);
796 cf_WriteInt(fp, m_prod_priority[i]);
797 cf_WriteInt(fp, m_max_prod_type[i]);
798 }
799
800 cf_WriteShort(fp, m_max_alive_children);
801 if (m_max_alive_children > 0) {
802 cf_WriteShort(fp, m_num_alive);
803 for (i = 0; i < m_num_alive; i++) {
804 cf_WriteInt(fp, m_alive_list[i]);
805 }
806 }
807
808 cf_WriteFloat(fp, m_preprod_time);
809 cf_WriteFloat(fp, m_postprod_time);
810
811 // Convert these to names
812 for (i = 0; i < MAX_MATCEN_SOUNDS; i++) {
813 len = strlen(Sounds[m_sounds[i]].name) + 1; // Accounts for NULL charactor
814 cf_WriteShort(fp, len);
815 for (j = 0; j < len; j++) {
816 cf_WriteByte(fp, Sounds[m_sounds[i]].name[j]);
817 }
818 }
819
820 cf_WriteFloat(fp, m_speed_multi);
821
822 cf_WriteByte(fp, m_prod_mode);
823 cf_WriteFloat(fp, m_prod_mode_time);
824
825 cf_WriteInt(fp, m_status);
826
827 cf_WriteInt(fp, m_num_prod);
828 cf_WriteInt(fp, m_last_prod_type_index);
829 cf_WriteFloat(fp, m_last_prod_finish_time);
830
831 cf_WriteInt(fp, m_cached_prod_index);
832 cf_WriteFloat(fp, m_cached_prod_time);
833
834 cf_WriteFloat(fp, m_next_active_check_time);
835 cf_WriteByte(fp, m_last_active_check_result);
836
837 cf_WriteInt(fp, m_last_prod_objref);
838
839 for (i = 0; i < MAX_PROD_TYPES; i++) {
840 cf_WriteInt(fp, m_num_prod_type[i]);
841 }
842
843 cf_WriteInt(fp, m_sound_active_handle);
844 }
845
846 extern int16_t texture_xlate[];
847 void matcen::LoadData(CFILE *fp) {
848 int i, j;
849 int len;
850
851 int16_t max_spawn_pnts;
852 int16_t max_prod_types;
853 int16_t max_matcen_sounds;
854
855 int version = cf_ReadInt(fp);
856
857 max_spawn_pnts = cf_ReadShort(fp);
858 ASSERT(max_spawn_pnts <= MAX_SPAWN_PNTS);
859 max_prod_types = cf_ReadShort(fp);
860 ASSERT(max_prod_types <= MAX_PROD_TYPES);
861 max_matcen_sounds = cf_ReadShort(fp);
862 ASSERT(max_matcen_sounds <= MAX_MATCEN_SOUNDS);
863
864 len = cf_ReadShort(fp);
865 ASSERT(len <= MAX_MATCEN_NAME_LEN);
866 for (i = 0; i < len; i++) {
867 m_name[i] = cf_ReadByte(fp);
868 }
869
870 m_num_prod_types = cf_ReadByte(fp);
871 m_control_type = cf_ReadByte(fp);
872 m_type = cf_ReadByte(fp);
873 m_creation_effect = cf_ReadByte(fp);
874
875 if (version >= 2)
876 m_creation_texture = texture_xlate[cf_ReadShort(fp)];
877
878 m_num_spawn_pnts = cf_ReadInt(fp);
879
880 m_roomnum = cf_ReadInt(fp);
881
882 m_create_pnt.x = cf_ReadFloat(fp);
883 m_create_pnt.y = cf_ReadFloat(fp);
884 m_create_pnt.z = cf_ReadFloat(fp);
885
886 m_create_room = cf_ReadInt(fp);
887
888 for (i = 0; i < max_spawn_pnts; i++) {
889 m_spawn_pnt[i] = cf_ReadInt(fp);
890
891 m_spawn_vec[i].x = cf_ReadFloat(fp);
892 m_spawn_vec[i].y = cf_ReadFloat(fp);
893 m_spawn_vec[i].z = cf_ReadFloat(fp);
894
895 m_spawn_normal[i].x = cf_ReadFloat(fp);
896 m_spawn_normal[i].y = cf_ReadFloat(fp);
897 m_spawn_normal[i].z = cf_ReadFloat(fp);
898 }
899
900 m_max_prod = cf_ReadInt(fp);
901
902 char temp_name[256];
903 for (i = 0; i < max_prod_types; i++) {
904 len = cf_ReadShort(fp);
905 for (j = 0; j < len; j++) {
906 {
907 temp_name[j] = cf_ReadByte(fp);
908 }
909 }
910
911 m_prod_type[i] = FindObjectIDName(temp_name);
912
913 m_prod_time[i] = cf_ReadFloat(fp);
914 m_prod_priority[i] = cf_ReadInt(fp);
915 m_max_prod_type[i] = cf_ReadInt(fp);
916 }
917
918 m_max_alive_children = cf_ReadShort(fp);
919 if (m_max_alive_children > 0) {
920 m_num_alive = cf_ReadShort(fp);
921 m_alive_list = (int *)mem_malloc(sizeof(int) * m_max_alive_children);
922
923 for (i = 0; i < m_num_alive; i++) {
924 m_alive_list[i] = cf_ReadInt(fp);
925 }
926 } else {
927 m_num_alive = 0;
928 m_alive_list = NULL;
929 }
930
931 m_preprod_time = cf_ReadFloat(fp);
932 m_postprod_time = cf_ReadFloat(fp);
933
934 // Convert these to names
935 for (i = 0; i < max_matcen_sounds; i++) {
936 char sound_name[256];
937
938 len = cf_ReadShort(fp);
939 for (j = 0; j < len; j++) {
940 sound_name[j] = cf_ReadByte(fp);
941 }
942
943 m_sounds[i] = FindSoundName(sound_name);
944 }
945
946 m_speed_multi = cf_ReadFloat(fp);
947
948 m_prod_mode = cf_ReadByte(fp);
949 m_prod_mode_time = cf_ReadFloat(fp);
950
951 m_status = cf_ReadInt(fp);
952
953 m_num_prod = cf_ReadInt(fp);
954 m_last_prod_type_index = cf_ReadInt(fp);
955 m_last_prod_finish_time = cf_ReadFloat(fp);
956
957 m_cached_prod_index = cf_ReadInt(fp);
958 m_cached_prod_time = cf_ReadFloat(fp);
959
960 m_next_active_check_time = cf_ReadFloat(fp);
961 m_last_active_check_result = (cf_ReadByte(fp) != 0);
962
963 m_last_prod_objref = cf_ReadInt(fp);
964
965 for (i = 0; i < max_prod_types; i++) {
966 m_num_prod_type[i] = cf_ReadInt(fp);
967 }
968
969 if (version >= 3)
970 m_sound_active_handle = cf_ReadInt(fp);
971 }
972
973 char matcen::GetNumProdTypes() { return m_num_prod_types; }
974
975 bool matcen::SetNumProdTypes(char num_prod_types) {
976 if (num_prod_types >= 0 && num_prod_types <= MAX_PROD_TYPES) {
977 if ((m_num_prod_types < num_prod_types || num_prod_types == -1) && (m_status & MSTAT_DONE_PROD)) {
978 ComputeNextProdInfo();
979 }
980
981 m_num_prod_types = num_prod_types;
982 return true;
983 }
984
985 return false;
986 }
987
988 bool matcen::GetProdInfo(char index, int *type_id, int *priority, float *time, int *max_prod) {
989 if (index >= 0 && index < MAX_PROD_TYPES) {
990 if (type_id)
991 *type_id = m_prod_type[index];
992
993 if (priority)
994 *priority = m_prod_priority[index];
995
996 if (time)
997 *time = m_prod_time[index];
998
999 if (max_prod)
1000 *max_prod = m_max_prod_type[index];
1001
1002 return true;
1003 }
1004
1005 return false;
1006 }
1007
1008 bool matcen::SetProdInfo(char index, int *type_id, int *priority, float *time, int *max_prod) {
1009 if (index >= 0 && index < MAX_PROD_TYPES) {
1010 if (type_id && (*type_id >= -1))
1011 m_prod_type[index] = *type_id;
1012
1013 if (priority && (*priority >= 0))
1014 m_prod_priority[index] = *priority;
1015
1016 if (time && (*time >= 0.0f))
1017 m_prod_time[index] = *time;
1018
1019 if (max_prod && (*max_prod >= -1)) {
1020 if (*max_prod == -1 && m_max_prod_type[index] < *max_prod && (m_status & MSTAT_DONE_PROD)) {
1021 ComputeNextProdInfo();
1022 }
1023
1024 m_max_prod_type[index] = *max_prod;
1025 }
1026
1027 return true;
1028 }
1029
1030 return false;
1031 }
1032
1033 float matcen::GetProdMultiplier() { return m_speed_multi; }
1034
1035 bool matcen::SetProdMultiplier(float multi) {
1036 if (multi >= 0.0f) {
1037 m_speed_multi = multi;
1038 return true;
1039 }
1040
1041 return false;
1042 }
1043
1044 int matcen::GetStatus() { return m_status; }
1045
1046 bool matcen::ComputeNextProdInfo() {
1047 int i;
1048
1049 if (m_status & (MSTAT_DISABLED))
1050 return false;
1051
1052 m_status &= ~MSTAT_DONE_PROD;
1053
1054 if (m_max_prod <= m_num_prod && m_max_prod != -1) {
1055 mprintf(0, "MATCEN: Done\n");
1056 m_status |= MSTAT_DONE_PROD;
1057 return false;
1058 }
1059
1060 ASSERT(m_prod_mode == MMODE_NOTPROD);
1061
1062 int cur_index;
1063 bool f_do = true;
1064
1065 if (m_status & MSTAT_RANDOM_PROD_ORDER) {
1066 int total_priorities = 0;
1067
1068 for (i = 0; i < m_num_prod_types; i++) {
1069 if ((m_max_prod_type[i] == -1 || m_max_prod_type[i] > m_num_prod_type[i]) && m_prod_type[i] >= -1) {
1070 total_priorities += m_prod_priority[i];
1071 }
1072 }
1073
1074 if (total_priorities <= 0) {
1075 f_do = false;
1076 } else {
1077 int r_val = ps_rand() % total_priorities;
1078 bool f_looking = true;
1079
1080 i = 0;
1081
1082 do {
1083 ASSERT(i < m_num_prod_types);
1084
1085 if ((m_max_prod_type[i] == -1 || m_max_prod_type[i] > m_num_prod_type[i]) && m_prod_type[i] >= -1) {
1086 r_val -= m_prod_priority[i];
1087
1088 if (r_val <= 0) {
1089 cur_index = i;
1090 f_looking = false;
1091 }
1092 }
1093
1094 i++;
1095 } while (f_looking);
1096 }
1097 } else {
1098 cur_index = -1;
1099 int best_priority = -1;
1100
1101 for (i = 0; i < m_num_prod_types; i++) {
1102 if ((m_max_prod_type[i] == -1 || m_max_prod_type[i] > m_num_prod_type[i]) && m_prod_type[i] >= -1 &&
1103 m_prod_priority[i] > best_priority) {
1104 cur_index = i;
1105 best_priority = m_prod_priority[i];
1106 }
1107 }
1108
1109 if (cur_index == -1) {
1110 f_do = false;
1111 }
1112 }
1113
1114 if (f_do) {
1115 m_cached_prod_index = cur_index;
1116 m_cached_prod_time = Gametime + m_prod_time[cur_index];
1117
1118 return true;
1119 }
1120
1121 m_cached_prod_index = -1;
1122 m_status |= MSTAT_DONE_PROD;
1123 return false;
1124 }
1125
1126 bool matcen::SetStatus(int status, bool f_enable) // Not all flags are settable
1127 {
1128 status &= ~(MSTAT_DONE_PROD | MSTAT_NEVER_PROD);
1129 m_last_active_check_result = (m_status & MSTAT_ACTIVE) != 0;
1130
1131 if (f_enable) {
1132 bool f_recompute = false;
1133
1134 if ((m_prod_mode == MMODE_NOTPROD) && (status & MSTAT_ACTIVE) && !(m_status & MSTAT_ACTIVE)) {
1135 f_recompute = true;
1136 }
1137
1138 m_status |= status;
1139
1140 if (f_recompute) {
1141 ComputeNextProdInfo();
1142 }
1143 } else {
1144 m_status &= ~status;
1145 }
1146
1147 #ifdef NEWEDITOR
1148
1149 ASSERT(!(m_status & MSTAT_ACTIVE));
1150
1151 #else
1152
1153 bool cur_active = (m_status & MSTAT_ACTIVE) != 0;
1154
1155 if (cur_active && !m_last_active_check_result) {
1156 if (m_type == MT_OBJECT) {
1157 object *obj = ObjGet(m_objref);
1158
1159 if (obj)
1160 m_sound_active_handle = Sound_system.Play3dSound(m_sounds[MATCEN_ACTIVE_SOUND], SND_PRIORITY_NORMAL, obj);
1161 } else {
1162 pos_state pos;
1163 matrix idmat = Identity_matrix;
1164
1165 int croom = m_create_room;
1166 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
1167 croom = GetTerrainRoomFromPos(&m_create_pnt);
1168 }
1169
1170 pos.position = &m_create_pnt;
1171 pos.orient = &idmat;
1172 pos.roomnum = croom;
1173
1174 m_sound_active_handle = Sound_system.Play3dSound(m_sounds[MATCEN_ACTIVE_SOUND], SND_PRIORITY_NORMAL, &pos);
1175 }
1176 } else if (!cur_active && m_last_active_check_result) {
1177 Sound_system.StopSoundLooping(m_sound_active_handle);
1178 }
1179
1180 #endif // NEWEDITOR
1181
1182 return true;
1183 }
1184
1185 #ifndef NEWEDITOR
1186
1187 void matcen::CheckActivateStatus() {
1188 m_next_active_check_time = Gametime + MATCEN_ACTIVE_CHECK_RATE +
1189 (MATCEN_ACTIVE_CHECK_VARIENCE * (((float)ps_rand()) / ((float)D3_RAND_MAX) - .5f));
1190 m_last_active_check_result = (m_status & MSTAT_ACTIVE) != 0;
1191
1192 // Determine if active
1193 if (m_status & (MSTAT_PROD_TILL_DONE)) {
1194 m_status |= MSTAT_ACTIVE;
1195 } else {
1196 switch (m_control_type) {
1197 case MPC_SCRIPT:
1198 break;
1199
1200 case MPC_WHILE_PLAYER_VISIBLE:
1201 case MPC_AFTER_PLAYER_VISIBLE: {
1202 m_status &= ~MSTAT_ACTIVE;
1203
1204 // Determine the correct creation room
1205 fvi_query fq;
1206 fvi_info hit_info;
1207 int fate;
1208
1209 int croom = m_create_room;
1210 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
1211 croom = GetTerrainRoomFromPos(&m_create_pnt);
1212 }
1213
1214 fq.p0 = &m_create_pnt;
1215 fq.startroom = croom;
1216 fq.p1 = &Player_object->pos;
1217 fq.rad = 0.0f;
1218 fq.thisobjnum = -1;
1219 fq.ignore_obj_list = NULL;
1220 fq.flags = FQ_NO_RELINK | FQ_CHECK_OBJS | FQ_ONLY_DOOR_OBJ;
1221
1222 fate = fvi_FindIntersection(&fq, &hit_info);
1223
1224 if (fate == HIT_NONE)
1225 m_status |= MSTAT_ACTIVE;
1226 } break;
1227
1228 case MPC_WHILE_PLAYER_NEAR:
1229 case MPC_AFTER_PLAYER_NEAR: {
1230 m_status &= ~MSTAT_ACTIVE;
1231
1232 if (m_create_room == -1) {
1233 } else if (ROOMNUM_OUTSIDE(m_create_room) || (m_create_room <= Highest_room_index && Rooms[m_create_room].used &&
1234 (Rooms[m_create_room].flags & RF_EXTERNAL))) {
1235 if (vm_VectorDistance(&Player_object->pos, &m_create_pnt) <= MATCEN_OUTSIDE_NEAR_DIST) {
1236 m_status |= MSTAT_ACTIVE;
1237 }
1238 } else if (m_create_room >= 0 && m_create_room <= Highest_room_index && Rooms[m_create_room].used) {
1239 room *rp = &Rooms[m_create_room];
1240
1241 if (m_create_room == Player_object->roomnum) {
1242 m_status |= MSTAT_ACTIVE;
1243 } else {
1244 int x;
1245
1246 for (x = 0; x < rp->num_portals; x++) {
1247 int c_room = rp->portals[x].croom;
1248 if (c_room < 0 && (vm_VectorDistance(&Player_object->pos, &m_create_pnt) <= MATCEN_OUTSIDE_NEAR_DIST)) {
1249 m_status |= MSTAT_ACTIVE;
1250 } else {
1251 if (c_room == Player_object->roomnum) {
1252 m_status |= MSTAT_ACTIVE;
1253 }
1254 }
1255
1256 // Already active; so stop checking
1257 if (m_status & MSTAT_ACTIVE) {
1258 break;
1259 }
1260 }
1261 }
1262 }
1263 } break;
1264
1265 default:
1266 Int3(); // Get Chris immediately
1267 break;
1268 }
1269 }
1270
1271 bool cur_active = (m_status & MSTAT_ACTIVE) != 0;
1272
1273 if (cur_active && !m_last_active_check_result) {
1274 if (m_type == MT_OBJECT) {
1275 object *obj = ObjGet(m_objref);
1276
1277 if (obj)
1278 m_sound_active_handle = Sound_system.Play3dSound(m_sounds[MATCEN_ACTIVE_SOUND], SND_PRIORITY_NORMAL, obj);
1279 } else {
1280 pos_state pos;
1281 matrix idmat = Identity_matrix;
1282
1283 int croom = m_create_room;
1284 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
1285 croom = GetTerrainRoomFromPos(&m_create_pnt);
1286 }
1287
1288 pos.position = &m_create_pnt;
1289 pos.orient = &idmat;
1290 pos.roomnum = croom;
1291
1292 m_sound_active_handle = Sound_system.Play3dSound(m_sounds[MATCEN_ACTIVE_SOUND], SND_PRIORITY_NORMAL, &pos);
1293 }
1294 } else if (!cur_active && m_last_active_check_result) {
1295 Sound_system.StopSoundLooping(m_sound_active_handle);
1296 }
1297
1298 if (cur_active && (m_control_type == MPC_AFTER_PLAYER_NEAR || m_control_type == MPC_AFTER_PLAYER_VISIBLE))
1299 m_status |= MSTAT_PROD_TILL_DONE;
1300 }
1301
1302 #define MATCEN_DAMAGE_PER_SECOND 20.0f
1303 #define MATCEN_DAMAGE_DIST 10.0f
1304 #define MATCEN_FORCE 60000.0f
1305
1306 void matcen::DoThinkFrame() {
1307 if ((Game_mode & GM_MULTI) && Netgame.local_role != LR_SERVER)
1308 return;
1309
1310 if (m_type == MT_UNASSIGNED)
1311 return;
1312
1313 // If disabled and not finishing up a production...
1314 if ((m_prod_mode == MMODE_NOTPROD) && (m_status & MSTAT_DISABLED))
1315 return;
1316
1317 if (m_type == MT_OBJECT) {
1318 object *parent = ObjGet(m_objref);
1319
1320 if (!parent) {
1321 m_prod_mode_time = 0.0f;
1322 m_prod_mode = MMODE_NOTPROD;
1323 m_cur_saturation_count = 0;
1324
1325 m_type = MT_UNASSIGNED;
1326 return;
1327 }
1328
1329 if ((m_prod_mode != MMODE_NOTPROD &&
1330 (parent->movement_type == MT_PHYSICS || parent->movement_type == MT_WALKING)) ||
1331 (m_status & MSTAT_COMPUTE_CREATE_PNT_EVERY_FRAME)) {
1332 ComputeCreatePnt();
1333 }
1334 }
1335
1336 // Increment time
1337 m_prod_mode_time += Frametime;
1338
1339 // Do alive children frame
1340 if (m_max_alive_children > 0) {
1341 DoAliveListFrame();
1342 }
1343
1344 // Determine if it is time to start a production
1345 switch (m_prod_mode) {
1346 case MMODE_NOTPROD: {
1347 if (!(m_status & (MSTAT_DONE_PROD | MSTAT_ACTIVE_PAUSE)) && (m_num_alive != m_max_alive_children)) {
1348
1349 if (Gametime >= m_next_active_check_time) {
1350 CheckActivateStatus();
1351 }
1352
1353 if ((m_status & MSTAT_ACTIVE) && (Gametime >= m_cached_prod_time) && (m_cached_prod_index >= 0)) {
1354 StartObjProd();
1355 }
1356 } else if ((m_status & MSTAT_DONE_PROD) && (m_status & MSTAT_ACTIVE)) {
1357 Sound_system.StopSoundLooping(m_sound_active_handle);
1358 m_status &= ~MSTAT_ACTIVE;
1359 }
1360 } break;
1361
1362 case MMODE_PREPROD: {
1363 if (m_prod_mode_time >= m_preprod_time)
1364 DoObjProd();
1365 } break;
1366
1367 case MMODE_POSTPROD: {
1368 m_status &= ~MSTAT_CREATE_OBJ_FRAME;
1369
1370 if (m_prod_mode_time >= m_postprod_time)
1371 FinishObjProd();
1372 } break;
1373
1374 default:
1375 Int3(); // get chris immediately
1376 break;
1377 }
1378
1379 if ((!(m_status & MSTAT_NOT_HURT_PLAYER)) && (m_prod_mode == MMODE_PREPROD || m_prod_mode == MMODE_POSTPROD)) {
1380 float damage = MATCEN_DAMAGE_PER_SECOND * Frametime;
1381 if (Game_mode & GM_MULTI) {
1382 // Multiplayer targetting (Major difference is that robot will ignore players while infighting in single player)
1383 for (int i = 0; i < MAX_PLAYERS; ++i) {
1384 if (!((NetPlayers[i].flags & NPF_CONNECTED) && (NetPlayers[i].sequence >= NETSEQ_PLAYING)))
1385 continue;
1386
1387 object *p = &Objects[Players[i].objnum];
1388 if (p->type == OBJ_GHOST)
1389 continue;
1390
1391 int croom = m_create_room;
1392 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
1393 croom = GetTerrainRoomFromPos(&m_create_pnt);
1394 }
1395
1396 if (croom != p->roomnum)
1397 continue;
1398
1399 vector dir = p->pos - m_create_pnt;
1400 float dist = vm_NormalizeVector(&dir) - p->size;
1401 if (dist >= MATCEN_DAMAGE_DIST)
1402 continue;
1403
1404 if (Netgame.local_role == LR_SERVER) {
1405 ApplyDamageToPlayer(p, p, PD_ENERGY_WEAPON, damage);
1406 }
1407
1408 if (p == Player_object) {
1409 dir *= MATCEN_FORCE;
1410
1411 vector movement_pos, movement_vec, pos;
1412 PhysicsDoSimLinear(*p, pos, dir, p->mtype.phys_info.velocity, movement_vec, movement_pos, Frametime, 1);
1413 }
1414 }
1415 } else {
1416 object *p = Player_object;
1417 if (p->type != OBJ_GHOST) {
1418 int croom = m_create_room;
1419 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
1420 croom = GetTerrainRoomFromPos(&m_create_pnt);
1421 }
1422
1423 if (croom == p->roomnum) {
1424 vector dir = p->pos - m_create_pnt;
1425
1426 float dist = vm_NormalizeVector(&dir) - p->size;
1427 if (dist < MATCEN_DAMAGE_DIST) {
1428 ApplyDamageToPlayer(p, p, PD_ENERGY_WEAPON, damage);
1429
1430 dir *= MATCEN_FORCE;
1431
1432 vector movement_pos, movement_vec, pos;
1433 PhysicsDoSimLinear(*p, pos, dir, p->mtype.phys_info.velocity, movement_vec, movement_pos, Frametime, 1);
1434 }
1435 }
1436 }
1437 }
1438 }
1439 }
1440
1441 void matcen::DoRenderFrame() {
1442 // MATCEN NOTE: The objref of the created object is valid from the creation time to the
1443 // creation of the next object
1444
1445 // Useful member variables:
1446 // m_prod_mode -- See below
1447 // m_last_prod_objref
1448 // m_prod_mode_time -- Zero the first frame of the current production mode
1449 // m_preprod_time
1450 // m_postprod_time
1451
1452 // Useful status members:
1453 // MSTAT_CREATE_OBJ_FRAME
1454
1455 // DAJ FIXES YET ANOTHER BUG WHERE THE ARRAY INDEX IS NOT CHECKED BEFORE USE (m_prod_type[m_cached_prod_index]
1456 // == -1)
1457 if (!(m_cached_prod_index >= 0 && m_cached_prod_index < m_num_prod_types && m_prod_type[m_cached_prod_index] >= 0 &&
1458 m_prod_type[m_cached_prod_index] <= MAX_OBJECT_TYPES &&
1459 Object_info[m_prod_type[m_cached_prod_index]].type != OBJ_NONE))
1460 return;
1461
1462 // MATCEN NOTE: If an effect does something graphical, add it here
1463 switch (m_prod_mode) {
1464 case MMODE_NOTPROD: {
1465 switch (m_creation_effect) {
1466 case MEFFECT_LINE_LIGHTNING:
1467 default:
1468 return;
1469 }
1470 } break;
1471
1472 case MMODE_PREPROD: {
1473 switch (m_creation_effect) {
1474 case MEFFECT_LINE_LIGHTNING: {
1475 int i;
1476
1477 int oi_index = m_prod_type[m_cached_prod_index];
1478
1479 if (oi_index < 0 || oi_index >= MAX_POLY_MODELS)
1480 break;
1481
1482 PageInPolymodel(Object_info[oi_index].render_handle, Object_info[oi_index].type, &Object_info[oi_index].size);
1483 float size;
1484
1485 if (oi_index >= 0)
1486 size = Object_info[oi_index].size * 1.5;
1487 else
1488 size = 0.0f;
1489
1490 if (m_cur_saturation_count < 1) {
1491 for (i = 0; i < m_num_spawn_pnts; i++) {
1492 int croom = m_create_room;
1493 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
1494 croom = GetTerrainRoomFromPos(&m_create_pnt);
1495 }
1496
1497 m_spawn_vis_effects[m_cur_saturation_count][i] = -1;
1498
1499 int visnum = VisEffectCreate(VIS_FIREBALL, LIGHTNING_BOLT_INDEX, croom, &m_create_pnt);
1500 if (visnum >= 0) {
1501 vis_effect *vis = &VisEffects[visnum];
1502 vis->lifeleft = .8f;
1503 vis->lifetime = .8f;
1504 vis->end_pos = m_spawn_vec[i];
1505 vis->flags = VF_USES_LIFELEFT | VF_EXPAND;
1506 vis->velocity.x = .8f;
1507 vis->velocity.y = 1;
1508 m_spawn_vis_effects[m_cur_saturation_count][i] = visnum;
1509 }
1510 }
1511 m_cur_saturation_count++;
1512 }
1513
1514 // Move the viseffects if needed
1515 for (i = 0; i < m_cur_saturation_count; i++) {
1516 for (int t = 0; t < m_num_spawn_pnts; t++) {
1517 int visnum = m_spawn_vis_effects[i][t];
1518 if (visnum == -1)
1519 continue;
1520
1521 ASSERT(visnum >= 0 && visnum < MAX_VIS_EFFECTS);
1522 vis_effect *vis = &VisEffects[visnum];
1523
1524 vis->pos = m_create_pnt;
1525 vis->end_pos = m_spawn_vec[t];
1526 }
1527 }
1528
1529 vector center = m_create_pnt;
1530 size = (m_prod_mode_time / m_preprod_time) * size;
1531
1532 if (size > 0.0)
1533 DrawColoredDisk(&center, 0.3f, 0.5f, 1.0f, .7f, 0, size, 1);
1534
1535 } break;
1536
1537 case MEFFECT_LINE_SINE_WAVE: {
1538 int i;
1539
1540 int oi_index = m_prod_type[m_cached_prod_index];
1541
1542 if (oi_index < 0 || oi_index >= MAX_POLY_MODELS)
1543 break;
1544
1545 PageInPolymodel(Object_info[oi_index].render_handle, Object_info[oi_index].type, &Object_info[oi_index].size);
1546 float size;
1547
1548 if (oi_index >= 0)
1549 size = Object_info[oi_index].size * 1.5;
1550 else
1551 size = 0.0f;
1552
1553 if (m_cur_saturation_count < 1) {
1554 for (i = 0; i < m_num_spawn_pnts; i++) {
1555 int croom = m_create_room;
1556 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
1557 croom = GetTerrainRoomFromPos(&m_create_pnt);
1558 }
1559
1560 m_spawn_vis_effects[m_cur_saturation_count][i] = -1;
1561
1562 int visnum = VisEffectCreate(VIS_FIREBALL, SINE_WAVE_INDEX, croom, &m_create_pnt);
1563 if (visnum >= 0) {
1564 vis_effect *vis = &VisEffects[visnum];
1565 vis->lifeleft = 1;
1566 vis->lifetime = 1;
1567 vis->end_pos = m_spawn_vec[i];
1568 vis->flags = VF_USES_LIFELEFT | VF_EXPAND;
1569 vis->velocity.x = 1;
1570 vis->velocity.y = 1;
1571 m_spawn_vis_effects[m_cur_saturation_count][i] = visnum;
1572 }
1573 }
1574
1575 m_cur_saturation_count++;
1576 }
1577
1578 // Move the viseffects if needed
1579 for (i = 0; i < m_cur_saturation_count; i++) {
1580 for (int t = 0; t < m_num_spawn_pnts; t++) {
1581 int visnum = m_spawn_vis_effects[i][t];
1582
1583 if (visnum == -1)
1584 continue;
1585
1586 ASSERT(visnum >= 0 && visnum < MAX_VIS_EFFECTS);
1587
1588 vis_effect *vis = &VisEffects[visnum];
1589
1590 vis->pos = m_create_pnt;
1591 vis->end_pos = m_spawn_vec[t];
1592 }
1593 }
1594
1595 vector center = m_create_pnt;
1596 size = (m_prod_mode_time / m_preprod_time) * size;
1597
1598 if (size > 0.0)
1599 DrawColoredDisk(&center, 0.6f, 0.4f, 1.0f, .7f, 0, size, 1);
1600 } break;
1601
1602 case MEFFECT_PROCEDURAL_LIGHTNING: {
1603 int i;
1604
1605 int oi_index = m_prod_type[m_cached_prod_index];
1606 PageInPolymodel(Object_info[oi_index].render_handle, Object_info[oi_index].type, &Object_info[oi_index].size);
1607 float size;
1608
1609 if (oi_index >= 0)
1610 size = Object_info[oi_index].size * 1.5;
1611 else
1612 size = 0.0f;
1613
1614 if (m_cur_saturation_count < MAX_MATCEN_EFFECT_SATURATION) {
1615 for (i = 0; i < m_num_spawn_pnts; i++) {
1616 int croom = m_create_room;
1617 if (!ROOMNUM_OUTSIDE(m_create_room) && (Rooms[m_create_room].flags & RF_EXTERNAL)) {
1618 croom = GetTerrainRoomFromPos(&m_create_pnt);
1619 }
1620
1621 m_spawn_vis_effects[m_cur_saturation_count][i] = -1;
1622
1623 int visnum = VisEffectCreate(VIS_FIREBALL, THICK_LIGHTNING_INDEX, croom, &m_create_pnt);
1624 if (visnum >= 0) {
1625 vis_effect *vis = &VisEffects[visnum];
1626
1627 vis->end_pos = m_spawn_vec[i];
1628 vis->lifeleft = 1;
1629 vis->lifetime = 1;
1630 vis->flags = VF_USES_LIFELEFT | VF_EXPAND | VF_LINK_TO_VIEWER;
1631 vis->billboard_info.width = std::max<float>(1, size / 2);
1632 vis->billboard_info.texture = 1;
1633 vis->velocity.x = 1;
1634 vis->velocity.y = 1;
1635 vis->velocity.z = .5;
1636 vis->lighting_color = GR_RGB16(255, 255, 255);
1637
1638 vis->custom_handle = m_creation_texture;
1639 vis->size = vm_VectorDistanceQuick(&vis->pos, &vis->end_pos);
1640 m_spawn_vis_effects[m_cur_saturation_count][i] = visnum;
1641 }
1642 }
1643 m_cur_saturation_count++;
1644 }
1645
1646 // Move the viseffects if needed
1647 for (i = 0; i < m_cur_saturation_count; i++) {
1648 for (int t = 0; t < m_num_spawn_pnts; t++) {
1649 int visnum = m_spawn_vis_effects[i][t];
1650
1651 if (visnum == -1)
1652 continue;
1653
1654 ASSERT(visnum >= 0 && visnum < MAX_VIS_EFFECTS);
1655
1656 vis_effect *vis = &VisEffects[visnum];
1657 vis->pos = m_create_pnt;
1658 vis->end_pos = m_spawn_vec[t];
1659 }
1660 }
1661
1662 vector center = m_create_pnt;
1663 size = (m_prod_mode_time / m_preprod_time) * size;
1664
1665 if (size > 0.0)
1666 DrawColoredDisk(&center, 0.3f, 0.5f, 1.0f, .7f, 0, size, 1);
1667
1668 } break;
1669
1670 default:
1671 return;
1672 }
1673 } break;
1674
1675 case MMODE_POSTPROD: {
1676
1677 /*switch(m_creation_effect)
1678 {
1679
1680
1681 }*/
1682
1683 return;
1684
1685 } break;
1686
1687 default:
1688 Int3(); // Get chris immediately
1689 }
1690 }
1691
1692 #endif // NEWEDITOR
1693
1694 char matcen::GetCreationEffect() { return m_creation_effect; }
1695
1696 bool matcen::SetCreationEffect(char effect_index) {
1697 if (effect_index < 0 || effect_index >= NUM_MATCEN_EFFECTS)
1698 return false;
1699
1700 m_creation_effect = effect_index;
1701
1702 // MATCEN NOTE: Set default preprod and postprod stuff here
1703
1704 // MATCEN NOTE: In addition, status flags can be set by effect type. See Chris if
1705 // this is done as there are default functionality and script interaction issues
1706 switch (m_creation_effect) {
1707 case MEFFECT_LINE_LIGHTNING:
1708 m_preprod_time = 1.5f;
1709 m_postprod_time = 1.0f;
1710 break;
1711 case MEFFECT_LINE_SINE_WAVE:
1712 m_preprod_time = 0.25f;
1713 m_postprod_time = 0.0f;
1714 break;
1715 default:
1716 m_preprod_time = 1.5f;
1717 m_postprod_time = 1.0f;
1718 break;
1719 }
1720
1721 return true;
1722 }
1723
1724 void matcen::SetCreationTexture(int16_t texnum) { m_creation_texture = texnum; }
1725
1726 int16_t matcen::GetCreationTexture() { return m_creation_texture; }
1727
1728 int matcen::GetSound(char sound_type) {
1729 if (sound_type >= 0 && sound_type < MAX_MATCEN_SOUNDS) {
1730 return m_sounds[sound_type];
1731 }
1732
1733 return MATCEN_ERROR;
1734 }
1735
1736 bool matcen::SetSound(char sound_type, int sound_index) {
1737 if (sound_type >= 0 && sound_type < MAX_MATCEN_SOUNDS) {
1738 m_sounds[sound_type] = sound_index;
1739 }
1740 return false;
1741 }
1742
1743 void matcen::Reset() {
1744 int i;
1745
1746 // Resets the a matcen
1747 m_status &= ~(MSTAT_DONE_PROD | MSTAT_ACTIVE | MSTAT_ACTIVE_PAUSE | MSTAT_PROD_TILL_DONE | MSTAT_PROD_ONE_PAUSE |
1748 MSTAT_PROD_ONE_DISABLE);
1749
1750 m_num_prod = 0;
1751 m_last_prod_type_index = MATCEN_ERROR;
1752 m_last_prod_finish_time = 0.0;
1753
1754 m_last_active_check_result = false;
1755 m_next_active_check_time = 0.0;
1756
1757 m_prod_mode_time = 0.0f;
1758
1759 for (i = 0; i < MAX_PROD_TYPES; i++) {
1760 m_num_prod_type[i] = 0;
1761 }
1762
1763 m_last_prod_objref = OBJECT_HANDLE_NONE;
1764
1765 ComputeCreatePnt();
1766
1767 if (m_prod_mode == MMODE_NOTPROD) {
1768 m_cached_prod_index = -1;
1769 m_cached_prod_time = 0.0f;
1770
1771 ComputeNextProdInfo();
1772 }
1773 }
1774
1775 int matcen::GetMaxAliveChildren() { return m_max_alive_children; }
1776
1777 bool matcen::SetMaxAliveChildren(int max_alive) {
1778 // If the max_alive value is valid...
1779 if (max_alive >= -1 && max_alive != 0 && max_alive <= MAX_MATCEN_ALIVE_CHILDREN) {
1780 int *temp;
1781 int i;
1782
1783 // Allocate the alive children list
1784 if (max_alive > 0) {
1785 temp = (int *)mem_malloc(sizeof(int) * max_alive);
1786 } else {
1787 temp = NULL;
1788 }
1789
1790 // Determine the current number of alive (and tracked) children
1791 if (m_alive_list && temp) {
1792 m_num_alive = (max_alive < m_num_alive) ? max_alive : m_num_alive;
1793
1794 for (i = 0; i < m_num_alive; i++) {
1795 temp[i] = m_alive_list[i];
1796 }
1797 } else {
1798 m_num_alive = 0;
1799 }
1800
1801 // Cleanup the old list
1802 if (m_alive_list) {
1803 mem_free(m_alive_list);
1804 }
1805
1806 // Setup the new list pointer and max alive children value
1807 m_alive_list = temp;
1808 m_max_alive_children = max_alive;
1809
1810 return true;
1811 }
1812
1813 return false;
1814 }
1815
1816 float matcen::GetPreProdTime() { return m_preprod_time; }
1817
1818 bool matcen::SetPreProdTime(float time) {
1819 float upper_bound;
1820
1821 // MATCEN NOTE: Check if valid for effect here
1822 switch (m_creation_effect) {
1823 case MEFFECT_LINE_LIGHTNING:
1824 upper_bound = 2.0f;
1825 break;
1826 case MEFFECT_LINE_SINE_WAVE:
1827 upper_bound = 2.0f;
1828 break;
1829 default:
1830 upper_bound = 2.0f;
1831 break;
1832 }
1833
1834 if (time >= 0.0f && time <= upper_bound) {
1835 m_preprod_time = time;
1836 return true;
1837 }
1838
1839 return false;
1840 }
1841
1842 float matcen::GetPostProdTime() { return m_postprod_time; }
1843
1844 bool matcen::SetPostProdTime(float time) {
1845 float upper_bound;
1846
1847 // MATCEN NOTE: Check if valid for effect here
1848 switch (m_creation_effect) {
1849 case MEFFECT_LINE_LIGHTNING:
1850 upper_bound = 2.0f;
1851 break;
1852 case MEFFECT_LINE_SINE_WAVE:
1853 upper_bound = 2.0f;
1854 break;
1855 default:
1856 upper_bound = 2.0f;
1857 break;
1858 }
1859
1860 if (time >= 0.0f && time <= upper_bound) {
1861 m_postprod_time = time;
1862 return true;
1863 }
1864
1865 return false;
1866 }
1867
1868 int FindMatcenIndex(const char *name) {
1869 int i;
1870 char temp[MAX_MATCEN_NAME_LEN];
1871
1872 for (i = 0; i < Num_matcens; i++) {
1873 Matcen[i]->GetName(temp);
1874
1875 if (!(stricmp(temp, name))) {
1876 return i;
1877 }
1878 }
1879 return MATCEN_ERROR;
1880 }
1881
1882 int CreateMatcen(const char *name, bool *f_name_changed) {
1883 if (Num_matcens < MAX_MATCENS) {
1884 Matcen[Num_matcens] = new matcen;
1885 Matcen[Num_matcens++]->SetName(name);
1886
1887 *f_name_changed = false;
1888
1889 return Num_matcens - 1;
1890 }
1891
1892 return MATCEN_ERROR;
1893 }
1894
1895 void InitMatcens() {
1896 int i;
1897
1898 Num_matcens = 0;
1899
1900 for (i = 0; i < MAX_MATCENS; i++) {
1901 Matcen[i] = NULL;
1902 }
1903
1904 atexit(DestroyAllMatcens);
1905 }
1906
1907 #if defined(POSIX)
1908 void DestroyAllMatcens()
1909 #else
1910 void __cdecl DestroyAllMatcens()
1911 #endif
1912 {
1913 int i;
1914
1915 for (i = 0; i < Num_matcens; i++) {
1916 if (Matcen[i]) {
1917 delete Matcen[i];
1918 }
1919 }
1920
1921 Num_matcens = 0;
1922 }
1923
1924 #ifndef NEWEDITOR
1925
1926 void DoMatcensFrame() {
1927 int i;
1928
1929 for (i = 0; i < Num_matcens; i++) {
1930 if (Matcen[i]) {
1931 Matcen[i]->DoThinkFrame();
1932 }
1933 }
1934 }
1935
1936 #endif // NEWEDITOR
1937
1938 void InitMatcensForLevel() {
1939 int i;
1940
1941 for (i = 0; i < Num_matcens; i++) {
1942 if (Matcen[i]) {
1943 Matcen[i]->Reset();
1944 }
1945 }
1946 }
1947
1948 #ifndef NEWEDITOR
1949
1950 void DoMatcensRenderFrame() {
1951 int i;
1952
1953 for (i = 0; i < Num_matcens; i++) {
1954 if (Matcen[i]) {
1955 Matcen[i]->DoRenderFrame();
1956 }
1957 }
1958 }
1959
1960 #endif // NEWEDITOR
1961
1962 void DestroyMatcen(int id, bool f_resort = false) {
1963 #if (!defined(EDITOR) && !defined(NEWEDITOR))
1964 f_resort = false;
1965 #endif
1966
1967 if (MatcenValid(id)) {
1968 delete Matcen[id];
1969 Matcen[id] = NULL;
1970
1971 Num_matcens--;
1972
1973 if (f_resort) {
1974 while (id < Num_matcens) {
1975 Matcen[id] = Matcen[id + 1];
1976 id++;
1977 }
1978 }
1979 }
1980 }
1981
1982 bool MatcenValid(int id) { return (id >= 0 && id < Num_matcens && Matcen[id]); }