-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcajeput_world.cpp
1894 lines (1631 loc) · 63.5 KB
/
cajeput_world.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2009-2010 Aidan Thornton, all rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AIDAN THORNTON ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AIDAN THORNTON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cajeput_core.h"
#include "cajeput_world.h"
#include "cajeput_int.h"
#include "cajeput_prim.h"
#include "caj_script.h"
#include "caj_helpers.h"
#include "cajeput_user_glue.h"
#include <cassert>
#include <stdio.h>
#define CAJ_LOGGER (sim->sgrp->log)
struct script_info {
private:
int m_refcnt;
public:
void *priv;
caj_string state;
simulator_ctx *sim; // FIXME - sim and prim may be NULL!
primitive_obj *prim;
inventory_item *inv;
script_info(simulator_ctx *sim_, primitive_obj *prim_, inventory_item *inv_)
: m_refcnt(1), priv(NULL), sim(sim_), prim(prim_), inv(inv_) {
state.len = 0; state.data = NULL;
}
~script_info() {
caj_string_free(&state);
}
void ref(void) { m_refcnt++; }
void unref(void) {
m_refcnt--;
if(m_refcnt == 0) delete this;
}
};
static void mark_deleted_obj_for_updates(simulator_ctx* sim, world_obj *obj);
static void world_update_global_pos_int(struct simulator_ctx *sim, struct world_obj *ob);
static void world_move_root_obj_int(struct simulator_ctx *sim, struct world_obj *ob,
const caj_vector3 &new_pos);
// --- START octree code ---
#define OCTREE_VERT_SCALE 64
#define OCTREE_HORIZ_SCALE 4
#define OCTREE_DEPTH 6
#define OCTREE_WIDTH (1<<OCTREE_DEPTH)
#if (OCTREE_WIDTH*OCTREE_HORIZ_SCALE) != WORLD_REGION_SIZE
#error World octree has bad horizontal size
#endif
#if (OCTREE_WIDTH*OCTREE_VERT_SCALE) != WORLD_HEIGHT
#error World octree has bad vertical size
#endif
#define OCTREE_MAGIC 0xc913e31cUL
#define OCTREE_LEAF_MAGIC 0x5b1ad072UL
#define OCTREE_CHECK_MAGIC(tree) assert(tree->magic == OCTREE_MAGIC);
#define OCTREE_LEAF_CHECK_MAGIC(leaf) assert(leaf->magic == OCTREE_LEAF_MAGIC);
// NB: not a C struct. In particular, I'm not sure the standard
// guarantees a pointer to the struct is the same as a pointer
// to its first member.
struct world_octree {
uint32_t magic;
struct world_octree* nodes[8];
struct world_octree* parent;
std::set<int32_t> chat_mask;
world_octree(world_octree* pparent) : magic(OCTREE_MAGIC), parent(pparent) {
for(int i = 0; i < 8; i++) nodes[i] = NULL;
//memset(&nodes,0,sizeof(nodes));
}
};
typedef std::multimap<int32_t,obj_chat_listener*> octree_chat_map;
typedef std::multimap<int32_t,obj_chat_listener*>::iterator octree_chat_map_iter;
struct world_ot_leaf {
uint32_t magic;
struct world_octree* parent;
std::set<world_obj*> objects;
octree_chat_map chat_map;
world_ot_leaf(world_octree* pparent) : magic(OCTREE_LEAF_MAGIC), parent(pparent) {
}
};
struct world_octree* world_octree_create() {
//struct world_octree* ot = new world_octree();
//memset(&ot->nodes,0,sizeof(ot->nodes));
return new world_octree(NULL);
}
#define OCTREE_IDX(x,y,z,level) ((((x)>>(level))&1)<<2|(((y)>>(level))&1)<<1|(((z)>>(level))&1)<<0)
static void octree_coord_fix(int &x, int &y, int &z) {
x /= OCTREE_HORIZ_SCALE;
y /= OCTREE_HORIZ_SCALE;
z /= OCTREE_VERT_SCALE;
if(x >= OCTREE_WIDTH) x = OCTREE_WIDTH - 1;
if(y >= OCTREE_WIDTH) y = OCTREE_WIDTH - 1;
if(z >= OCTREE_WIDTH) z = OCTREE_WIDTH - 1;
if(x < 0) x = 0;
if(y < 0) y = 0;
if(z < 0) z = 0;
}
struct world_ot_leaf* world_octree_find(struct world_octree* tree, int x, int y, int z, int add) {
octree_coord_fix(x,y,z);
for(int i = OCTREE_DEPTH - 1; i >= 0; i--) {
struct world_octree** tp = tree->nodes+OCTREE_IDX(x,y,z,i);
if(*tp == NULL) {
if(!add) {
return NULL;
} else if(i > 0) {
*tp = new world_octree(tree);
} else {
*tp = (world_octree*)new world_ot_leaf(tree);
}
}
tree = *tp;
}
return (world_ot_leaf*) tree;
}
static void real_octree_destroy(struct world_octree* tree, int depth) {
for(int i = 0; i < 8; i++) {
struct world_octree* tp = tree->nodes[i];
if(tp != NULL) {
if(depth > 0) {
real_octree_destroy(tp, depth-1);
} else {
delete (world_ot_leaf*)tp;
}
}
}
delete tree;
}
/* Note - for use in sim shutdown only. Please remove all objects first */
void world_octree_destroy(struct world_octree* tree) {
real_octree_destroy(tree, OCTREE_DEPTH - 1);
}
void world_octree_insert(struct world_octree* tree, struct world_obj* obj) {
struct world_ot_leaf* leaf = world_octree_find(tree, (int)obj->world_pos.x,
(int)obj->world_pos.y,
(int)obj->world_pos.z, true);
leaf->objects.insert(obj);
}
void octree_add_chat(struct world_ot_leaf* leaf, int32_t channel,
struct obj_chat_listener *listen) {
leaf->chat_map.insert(std::pair<int32_t,obj_chat_listener*>(channel,listen));
for(world_octree *tree = leaf->parent; tree != NULL; tree = tree->parent) {
std::set<int32_t>::iterator iter = tree->chat_mask.lower_bound(channel);
if(iter != tree->chat_mask.end() && *iter == channel)
break;
tree->chat_mask.insert(iter, channel);
}
}
void octree_del_chat(struct world_ot_leaf* leaf, int32_t channel,
struct obj_chat_listener *listen) {
std::pair<octree_chat_map_iter,octree_chat_map_iter> span =
leaf->chat_map.equal_range(channel);
int count = 0;
for(octree_chat_map_iter iter = span.first; iter != span.second;) {
octree_chat_map_iter next = iter; next++;
assert(next != iter);
if(iter->second == listen) {
leaf->chat_map.erase(iter);
} else count++;
iter = next;
}
if(count) return;
struct world_octree* tree = leaf->parent;
for(int i = 0; i < 8; i++) {
leaf = (world_ot_leaf*)tree->nodes[i];
if(leaf != NULL) {
if(leaf->chat_map.find(channel) != leaf->chat_map.end())
return;
}
}
tree->chat_mask.erase(channel);
tree = tree->parent;
while(tree != NULL) {
for(int i = 0; i < 8; i++) {
struct world_octree* subtree = tree->nodes[i];
if(subtree && subtree->chat_mask.find(channel) != subtree->chat_mask.end())
return;
}
tree->chat_mask.erase(channel);
tree = tree->parent;
}
// FIXME - need to cleanup parent nodes
}
void world_octree_move(struct world_octree* tree, struct world_obj* obj,
const caj_vector3 &new_pos) {
// FIXME - need to improve efficiency;
struct world_ot_leaf* old_leaf = world_octree_find(tree, (int)obj->world_pos.x,
(int)obj->world_pos.y,
(int)obj->world_pos.z, false);
struct world_ot_leaf* new_leaf = world_octree_find(tree, (int)new_pos.x,
(int)new_pos.y,
(int)new_pos.z, true);
if(old_leaf == new_leaf) return;
old_leaf->objects.erase(obj);
new_leaf->objects.insert(obj);
if(obj->chat != NULL) {
for(std::set<std::pair<int32_t, obj_chat_listener*> >::iterator iter =
obj->chat->channels.begin(); iter != obj->chat->channels.end();
iter++) {
octree_add_chat(new_leaf, iter->first, iter->second);
octree_del_chat(old_leaf, iter->first, iter->second);
}
}
}
// FIXME - this and the move function need to clean up unused nodes
void world_octree_delete(struct world_octree* tree, struct world_obj* obj) {
struct world_ot_leaf* leaf = world_octree_find(tree, (int)obj->world_pos.x,
(int)obj->world_pos.y,
(int)obj->world_pos.z, false);
leaf->objects.erase(obj);
if(obj->chat != NULL) {
for(std::set<std::pair<int32_t, obj_chat_listener*> >::iterator iter =
obj->chat->channels.begin(); iter != obj->chat->channels.end();
iter++) {
octree_del_chat(leaf, iter->first, iter->second);
}
}
}
static void real_octree_send_chat(struct simulator_ctx *sim, struct world_octree* tree,
struct chat_message* chat,
float range, int depth) {
OCTREE_CHECK_MAGIC(tree)
if(tree->chat_mask.find(chat->channel) == tree->chat_mask.end())
return;
for(int i = 0; i < 8; i++) {
struct world_octree* tp = tree->nodes[i];
if(tp != NULL) {
// FIXME - should filter out out-of-range octree nodes
if(depth > 0) {
real_octree_send_chat(sim, tp, chat, range, depth-1);
} else {
world_ot_leaf* leaf = (world_ot_leaf*)tp;
OCTREE_LEAF_CHECK_MAGIC(leaf);
std::pair<octree_chat_map_iter,octree_chat_map_iter> span =
leaf->chat_map.equal_range(chat->channel);
// int count = 0; // ??? what was this meant for?
for(octree_chat_map_iter iter = span.first; iter != span.second;iter++) {
obj_chat_listener* listen = iter->second;
if(caj_vect3_dist(&listen->obj->world_pos, &chat->pos) < range)
listen->callback(sim, listen->obj, chat, listen, listen->user_data);
}
}
}
}
}
void world_send_chat(struct simulator_ctx *sim, struct chat_message* chat) {
float range = 40.0f;
switch(chat->chat_type) {
case CHAT_TYPE_WHISPER:
range = 10.0f; break;
case CHAT_TYPE_NORMAL:
range = 20.0f; break;
case CHAT_TYPE_SHOUT:
range = 100.0f; break;
}
CAJ_DEBUG("DEBUG: Sending chat message from %s @ (%f, %f, %f) range %f: %s\n",
chat->name, chat->pos.x, chat->pos.y, chat->pos.z,
range, chat->msg);
real_octree_send_chat(sim,sim->world_tree,chat,range,OCTREE_DEPTH-1);
}
// ---- END of octree code ---
// FIXME - this actually isn't quite what we want.
static void object_compute_global_pos(struct world_obj *ob, caj_vector3 *pos_out) {
if(ob->parent == NULL) {
*pos_out = ob->local_pos;
} else if(ob->parent->type == OBJ_TYPE_PRIM) {
caj_vector3 offset;
caj_mult_vect3_quat(&offset, &ob->parent->rot, &ob->local_pos);
*pos_out = ob->parent->world_pos + offset;
} else {
// probably an attachment.
*pos_out = ob->parent->world_pos;
}
}
void world_chat_from_prim(struct simulator_ctx *sim, struct primitive_obj* prim,
int32_t chan, const char *msg, int chat_type) {
struct chat_message chat;
chat.channel = chan;
chat.msg = (char*)msg; // FIXME - correct type?
chat.chat_type = chat_type;
chat.pos = prim->ob.world_pos;
chat.name = prim->name;
uuid_copy(chat.source,prim->ob.id);
uuid_copy(chat.owner,prim->owner);
chat.source_type = CHAT_SOURCE_OBJECT;
if(chat_type == CHAT_TYPE_OWNER_SAY) {
user_ctx *ctx = user_find_ctx(sim, prim->owner);
if(ctx != NULL) {
if(ctx->userh != NULL && ctx->userh->chat_callback != NULL)
ctx->userh->chat_callback(ctx->user_priv, &chat);
} else {
CAJ_DEBUG("DEBUG: discarding llOwnerSay for absent user\n");
}
} else {
world_send_chat(sim, &chat);
}
}
void world_chat_from_user(struct user_ctx* ctx,
int32_t chan, const char *msg, int chat_type) {
struct chat_message chat;
if(ctx->av == NULL) return; // I have no mouth and I must scream...
if(chat_type == CHAT_TYPE_WHISPER || chat_type == CHAT_TYPE_NORMAL ||
chat_type == CHAT_TYPE_SHOUT) {
chat.channel = chan;
chat.pos = ctx->av->ob.world_pos;
chat.name = ctx->name;
uuid_copy(chat.source,ctx->user_id);
uuid_clear(chat.owner); // FIXME - ???
chat.source_type = CHAT_SOURCE_AVATAR;
chat.chat_type = chat_type;
chat.msg = (char*)msg;
world_send_chat(ctx->sim, &chat);
}
}
/* WARNING: do not call this until the object has been added to the octree.
Seriously, just don't. It's not a good idea */
void world_obj_add_listen(struct simulator_ctx *sim, struct world_obj *ob,
int32_t channel, struct obj_chat_listener* listen) {
if(ob->chat == NULL) {
ob->chat = new obj_chat_listeners();
ob->chat->obj = ob;
}
struct world_ot_leaf* leaf = world_octree_find(sim->world_tree,
(int)ob->world_pos.x,
(int)ob->world_pos.y,
(int)ob->world_pos.z, false);
assert(leaf != NULL);
listen->obj = ob;
ob->chat->channels.insert(std::pair<int, obj_chat_listener*>(channel,
listen));
octree_add_chat(leaf, channel, listen);
}
void world_obj_remove_listen(struct simulator_ctx *sim, struct world_obj *ob,
int32_t channel, struct obj_chat_listener* listen) {
assert(ob->chat != NULL);
struct world_ot_leaf* leaf = world_octree_find(sim->world_tree,
(int)ob->world_pos.x,
(int)ob->world_pos.y,
(int)ob->world_pos.z, false);
assert(leaf != NULL);
ob->chat->channels.erase(std::pair<int, obj_chat_listener*>(channel,
listen));
octree_del_chat(leaf, channel, listen);
}
static world_obj *prim_find_listen_obj(primitive_obj* prim) {
world_obj *cur = &prim->ob;
while(cur->parent != NULL && cur->type != OBJ_TYPE_AVATAR)
cur = cur->parent;
return cur;
}
void world_script_add_listen(struct script_chat_listener *listen) {
world_obj *root = prim_find_listen_obj(listen->prim);
world_obj_add_listen(listen->sim, root, listen->chan,
&listen->l);
}
void world_script_remove_listen(struct script_chat_listener *listen) {
world_obj *root = prim_find_listen_obj(listen->prim);
if(root->chat == NULL) return; // prim is probably being removed
world_obj_remove_listen(listen->sim, root, listen->chan,
&listen->l);
}
void world_add_attachment(struct simulator_ctx *sim, struct avatar_obj *av,
struct primitive_obj *prim, uint8_t attach_point) {
assert(attach_point < NUM_ATTACH_POINTS && attach_point != ATTACH_TO_LAST);
prim->attach_point = attach_point;
world_obj *ob = &prim->ob;
ob->parent = &av->ob;
sim->uuid_map.insert(std::pair<obj_uuid_t,world_obj*>(obj_uuid_t(ob->id),ob));
ob->local_id = (uint32_t)random();
object_compute_global_pos(ob, &ob->world_pos);
//FIXME - generate local ID properly.
sim->localid_map.insert(std::pair<uint32_t,world_obj*>(ob->local_id,ob));
for(int i = 0; i < prim->num_children; i++) {
world_insert_obj(sim, &prim->children[i]->ob);
}
// FIXME - save old attachment back to inventory
if(av->attachments[attach_point] != NULL)
world_delete_prim(sim, av->attachments[attach_point]);
av->attachments[attach_point] = prim;
world_mark_object_updated(sim, &av->ob, CAJ_OBJUPD_CHILDREN);
world_mark_object_updated(sim, ob, CAJ_OBJUPD_CREATED);
}
void world_insert_obj(struct simulator_ctx *sim, struct world_obj *ob) {
sim->uuid_map.insert(std::pair<obj_uuid_t,world_obj*>(obj_uuid_t(ob->id),ob));
ob->local_id = (uint32_t)random();
object_compute_global_pos(ob, &ob->world_pos);
//FIXME - generate local ID properly.
sim->localid_map.insert(std::pair<uint32_t,world_obj*>(ob->local_id,ob));
world_octree_insert(sim->world_tree, ob);
if(ob->type == OBJ_TYPE_PRIM) {
primitive_obj *prim = (primitive_obj*)ob;
for(int i = 0; i < prim->num_children; i++) {
world_insert_obj(sim, &prim->children[i]->ob);
}
for(unsigned i = 0; i < prim->inv.num_items; i++) {
inventory_item *inv = prim->inv.items[i];
if(inv->inv_type == INV_TYPE_LSL && inv->spriv != NULL) {
script_info *sinfo = (script_info*)inv->spriv;
if(sinfo->state.len > 0 && sim->scripth.restore_script != NULL)
sinfo->priv = sim->scripth.restore_script(sim, sim->script_priv,
prim, inv, &sinfo->state);
}
}
}
world_mark_object_updated(sim, ob, CAJ_OBJUPD_CREATED);
}
static void world_remove_obj(struct simulator_ctx *sim, struct world_obj *ob) {
sim->localid_map.erase(ob->local_id);
world_octree_delete(sim->world_tree, ob);
sim->physh.del_object(sim,sim->phys_priv,ob);
mark_deleted_obj_for_updates(sim, ob);
delete ob->chat; ob->chat = NULL;
}
struct world_obj* world_object_by_id(struct simulator_ctx *sim, const uuid_t id) {
std::map<obj_uuid_t,world_obj*>::iterator iter = sim->uuid_map.find(id);
if(iter == sim->uuid_map.end())
return NULL;
return iter->second;
}
struct world_obj* world_object_by_localid(struct simulator_ctx *sim, uint32_t id) {
std::map<uint32_t,world_obj*>::iterator iter = sim->localid_map.find(id);
if(iter == sim->localid_map.end())
return NULL;
return iter->second;
}
struct primitive_obj* world_get_root_prim(struct primitive_obj *prim) {
while(prim->ob.parent != NULL && prim->ob.parent->type == OBJ_TYPE_PRIM) {
prim = (primitive_obj*)prim->ob.parent;
}
return prim;
}
// NOTE: if you're adding new fields to prims and want them to be initialised
// properly, you *must* edit cajeput_dump.cpp as well as here, since it doesn't
// use world_begin_new_prim when revivifying loaded prims.
// You probably also need to update clone_prim below.
// Plus, you might want to update prim_from_os_xml (though it's not essential).
struct primitive_obj* world_begin_new_prim(struct simulator_ctx *sim) {
struct primitive_obj *prim = new primitive_obj();
memset(prim, 0, sizeof(struct primitive_obj));
uuid_generate(prim->ob.id);
prim->ob.type = OBJ_TYPE_PRIM;
prim->ob.scale.x = prim->ob.scale.y = prim->ob.scale.z = 1.0f;
prim->ob.rot.x = 0.0f; prim->ob.rot.y = 0.0f; prim->ob.rot.z = 0.0f;
prim->ob.rot.w = 1.0f; prim->crc_counter = 0;
prim->profile_curve = PROFILE_SHAPE_SQUARE | PROFILE_HOLLOW_DEFAULT;
prim->path_curve = PATH_CURVE_STRAIGHT;
prim->path_scale_x = 100; prim->path_scale_y = 100;
prim->name = strdup("Object");
prim->description = strdup("");
prim->perms.next = prim->perms.current = prim->perms.base = PERM_FULL_PERMS;
prim->perms.group = prim->perms.everyone = 0;
prim->flags = 0; prim->caj_flags = 0; prim->ob.phys = NULL;
prim->attach_point = 0;
prim->ob.parent = NULL; prim->children = NULL;
prim->num_children = 0;
prim->inv.num_items = prim->inv.alloc_items = 0;
prim->inv.items = NULL; prim->inv.serial = 0;
prim->inv.filename = NULL;
// tad hacky; the first byte is actually a count, not a terminating null
caj_string_set_bin(&prim->extra_params, (const unsigned char*)"", 1);
prim->hover_text = strdup("");
memset(prim->text_color, 0, sizeof(prim->text_color)); // technically redundant
prim->sit_name = strdup(""); prim->touch_name = strdup("");
prim->creation_date = time(NULL);
prim->sit_target.x = 0.0f; prim->sit_target.y = 0.0f;
prim->sit_target.z = 0.0f;
prim->sit_rot.x = 0.0f; prim->sit_rot.y = 0.0f; prim->sit_rot.z = 0.0f;
prim->sit_rot.w = 1.0f;
prim->avatar_sitting = NULL;
prim->num_avatars = 0;
prim->avatars = NULL;
return prim;
}
#define MAX_EXTRA_PARAMS_LEN 4096
// currently just a dumb wrapper, but I expect to smarten it up later
void prim_set_extra_params(struct primitive_obj *prim, const caj_string *params) {
caj_string_free(&prim->extra_params);
if(params->len < 1 || params->len > MAX_EXTRA_PARAMS_LEN)
caj_string_set_bin(&prim->extra_params, (const unsigned char*)"", 1);
else caj_string_copy(&prim->extra_params, params);
}
// internal helper function.
static void copy_rm_extra_param(caj_string &ep, caj_string &new_ep,
uint16_t param_type) {
assert(ep.len >= 1);
int count = ep.data[0], new_count = 0;
int offset = 1, new_offset = 1;
for(int i = 0; i < count; i++) {
if(ep.len - offset < 6) break;
uint16_t ptype = ep.data[offset] | ((uint16_t)ep.data[offset+1]<<8);
uint32_t plen = caj_bin_to_uint32_le(ep.data+2);
if(plen > (uint32_t)(ep.len - offset - 6)) break;
if(ptype != param_type) {
memcpy(new_ep.data+new_offset, ep.data+offset, plen+6);
new_count++;
}
offset += 6 + plen;
}
new_ep.data[0] = new_count; new_ep.len = new_offset;
}
void prim_delete_extra_param(struct primitive_obj *prim, uint16_t param_type) {
caj_string new_ep;
assert( prim->extra_params.len >= 1);
new_ep.len = 0;
new_ep.data = (unsigned char*)malloc(prim->extra_params.len);
copy_rm_extra_param( prim->extra_params, new_ep, param_type);
prim_set_extra_params(prim, &new_ep);
free(new_ep.data);
}
int prim_set_extra_param(struct primitive_obj *prim, uint16_t param_type,
const caj_string *param_data) {
if(param_data->len > MAX_EXTRA_PARAMS_LEN) return FALSE;
assert( prim->extra_params.len >= 1);
caj_string new_ep;
new_ep.len = 0;
new_ep.data = (unsigned char*)malloc(prim->extra_params.len+param_data->len+6);
copy_rm_extra_param(prim->extra_params, new_ep, param_type);
new_ep.data[new_ep.len] = param_type & 0xff;
new_ep.data[new_ep.len+1] = (param_type>>8) & 0xff;
caj_uint32_to_bin_le(new_ep.data+new_ep.len+2, param_data->len);
memcpy(new_ep.data+new_ep.len+6, param_data->data, param_data->len);
new_ep.len += 6 + param_data->len;
if(new_ep.len > MAX_EXTRA_PARAMS_LEN || new_ep.data[0] == 255) {
printf("DEBUG: prim_set_extra_param: no more space!\n");
free(new_ep.data); return FALSE;
} else {
new_ep.data[0]++;
prim_set_extra_params(prim, &new_ep);
free(new_ep.data); return TRUE;
}
}
inventory_item* world_prim_alloc_inv_item(void) {
inventory_item *inv = new inventory_item();
inv->asset_hack = NULL; inv->spriv = NULL;
return inv;
}
static void prim_free_inv_item(inventory_item *inv) {
free(inv->name); free(inv->description); free(inv->creator_id);
if(inv->asset_hack != NULL) {
free(inv->asset_hack->name); free(inv->asset_hack->description);
caj_string_free(&inv->asset_hack->data);
delete inv->asset_hack;
}
delete inv;
}
void world_free_prim(struct primitive_obj *prim) {
for(unsigned i = 0; i < prim->inv.num_items; i++) {
inventory_item *inv = prim->inv.items[i];
prim_free_inv_item(inv);
}
free(prim->name); free(prim->description); free(prim->inv.filename);
caj_string_free(&prim->tex_entry); caj_string_free(&prim->extra_params);
free(prim->hover_text); free(prim->sit_name); free(prim->touch_name);
free(prim->inv.items); free(prim->children); delete prim;
}
void world_delete_avatar(struct simulator_ctx *sim, struct avatar_obj *av) {
if(av->ob.parent != NULL)
world_unsit_avatar_now(sim, &av->ob);
world_remove_obj(sim, &av->ob);
for(int i = 0; i < NUM_ATTACH_POINTS; i++) {
if(av->attachments[i] != NULL)
world_delete_prim(sim, av->attachments[i]);
}
free(av);
}
static void linkset_unsit_all_avatars(struct simulator_ctx *sim,
struct primitive_obj* prim) {
// actually works on child prims too, not just linksets.
while(prim->num_avatars > 0)
world_unsit_avatar_now(sim, prim->avatars[0]);
if(prim->avatar_sitting != NULL)
world_unsit_avatar_now(sim, prim->avatar_sitting);
}
void world_delete_prim(struct simulator_ctx *sim, struct primitive_obj *prim) {
linkset_unsit_all_avatars(sim, prim);
world_remove_obj(sim, &prim->ob);
if(prim->children != NULL) {
// tad inefficient, this - actually O(num_children^2)
for(int i = prim->num_children - 1; i >= 0; i--) {
world_delete_prim(sim, prim->children[i]);
}
}
if(prim->ob.parent != NULL) {
assert(prim->ob.parent->type == OBJ_TYPE_PRIM ||
prim->ob.parent->type == OBJ_TYPE_AVATAR); // FIXME?
if(prim->ob.parent->type == OBJ_TYPE_PRIM) {
primitive_obj *parent = (primitive_obj*) prim->ob.parent;
for(int i = 0; i < parent->num_children; i++) {
if(parent->children[i] == prim) {
parent->num_children--;
for( ; i < parent->num_children; i++)
parent->children[i] = parent->children[i+1];
}
}
} else if(prim->ob.parent->type == OBJ_TYPE_AVATAR) {
avatar_obj *av = (avatar_obj*) prim->ob.parent;
assert(prim->attach_point != 0 && prim->attach_point < NUM_ATTACH_POINTS);
assert(av->attachments[prim->attach_point] == prim);
av->attachments[prim->attach_point] = NULL;
}
}
for(unsigned i = 0; i < prim->inv.num_items; i++) {
inventory_item *inv = prim->inv.items[i];
if(inv->spriv != NULL) {
script_info *sinfo = (script_info*)inv->spriv;
if(sinfo->priv != NULL)
sim->scripth.kill_script(sim, sim->script_priv, sinfo->priv);
sinfo->inv = NULL; sinfo->unref(); inv->spriv = NULL;
}
}
world_free_prim(prim);
}
static void prim_disable_listens(struct simulator_ctx *sim,
struct primitive_obj* prim) {
for(unsigned i = 0; i < prim->inv.num_items; i++) {
inventory_item *inv = prim->inv.items[i];
if(inv->spriv != NULL) {
script_info *sinfo = (script_info*)inv->spriv;
if(sinfo->priv != NULL && sim->scripth.disable_listens != NULL)
sim->scripth.disable_listens(sim, sim->script_priv, sinfo->priv);
}
}
for(unsigned i = 0; i < prim->num_children; i++) {
prim_disable_listens(sim, prim->children[i]);
}
}
static void prim_reenable_listens(struct simulator_ctx *sim,
struct primitive_obj* prim) {
for(unsigned i = 0; i < prim->inv.num_items; i++) {
inventory_item *inv = prim->inv.items[i];
if(inv->spriv != NULL) {
script_info *sinfo = (script_info*)inv->spriv;
if(sinfo->priv != NULL && sim->scripth.reenable_listens != NULL)
sim->scripth.reenable_listens(sim, sim->script_priv, sinfo->priv);
}
}
for(unsigned i = 0; i < prim->num_children; i++) {
prim_reenable_listens(sim, prim->children[i]);
}
}
void world_prim_link(struct simulator_ctx *sim, struct primitive_obj* main,
struct primitive_obj* child) {
if(main->ob.parent != NULL || child->ob.parent != NULL || child->num_children != 0) {
CAJ_WARN("FIXME - can't handle this prim linking case");
return;
}
if(main->num_children > 255) {
CAJ_WARN("ERROR: tried linking prim with too many children\n");
return;
}
// any avatars sitting on the child linkset must be unsat, since avatars
// must not have a child prim as their parent object.
linkset_unsit_all_avatars(sim, child);
main->children = (primitive_obj**)realloc(main->children,
sizeof(primitive_obj*)*(main->num_children+1));
main->children[main->num_children++] = child;
// need to move all listeners to the new root prim
prim_disable_listens(sim, child);
child->ob.parent = &main->ob;
prim_reenable_listens(sim, child);
world_mark_object_updated(sim, &child->ob, CAJ_OBJUPD_PARENT);
world_mark_object_updated(sim, &main->ob, CAJ_OBJUPD_CHILDREN);
// FIXME - this isn't quite right. Need to handle rotation
child->ob.local_pos.x -= main->ob.local_pos.x;
child->ob.local_pos.y -= main->ob.local_pos.y;
child->ob.local_pos.z -= main->ob.local_pos.z;
}
static bool calc_sit_by_target(struct primitive_obj *seat,
struct caj_sit_info *info_out, bool &has_sit_target) {
if(seat->sit_target.x != 0.0f || seat->sit_target.y != 0.0f ||
seat->sit_target.z != 0.0f) {
has_sit_target = true;
if(seat->avatar_sitting == NULL) {
uuid_copy(info_out->target, seat->ob.id);
info_out->offset = seat->sit_target;
info_out->rot = seat->sit_rot;
return TRUE;
}
}
return FALSE;
}
int world_avatar_begin_sit(struct simulator_ctx *sim, struct world_obj *av,
struct primitive_obj *seat, struct caj_sit_info *info_out) {
bool has_sit_target = false;
assert(av->type == OBJ_TYPE_AVATAR);
primitive_obj *root = world_get_root_prim(seat);
if(av->parent != NULL) return FALSE;
if(calc_sit_by_target(root, info_out, has_sit_target))
return TRUE;
for(int i = 0; i < root->num_children; i++) {
if(calc_sit_by_target(root->children[i], info_out, has_sit_target))
return TRUE;
}
// if any prim on the linkset has a sit target, we can't then go and sit on
// a prim without one
if(has_sit_target) return FALSE;
// Finally, fall back to the original chosen prim.
uuid_copy(info_out->target, seat->ob.id);
// note that the caller is expected to fill in info_out->offset appropriately!
// FIXME - calculate it more accurately ourselves, if possible.
info_out->rot.x = 0.0f; info_out->rot.y = 0.0f; info_out->rot.z = 0.0f;
info_out->rot.w = 1.0f;
return TRUE;
}
// NOTE: can't safely be used on its own until avatar update code is cleaned up.
// Use user_complete_sit instead!
int world_avatar_complete_sit(struct simulator_ctx *sim, struct world_obj *av,
const struct caj_sit_info *info) {
assert(av->type == OBJ_TYPE_AVATAR);
if(av->parent != NULL) return FALSE;
struct world_obj *obj = world_object_by_id(sim, info->target);
if(obj == NULL || obj->type != OBJ_TYPE_PRIM)
return FALSE;
primitive_obj *prim = (primitive_obj*)obj;
primitive_obj *root = world_get_root_prim(prim);
if(prim->avatar_sitting != NULL) return FALSE;
prim->avatar_sitting = av;
root->avatars = (world_obj**)realloc(root->avatars, sizeof(world_obj*) *
(root->num_avatars+1));
root->avatars[root->num_avatars++] = av;
av->parent = &root->ob;
((avatar_obj*)av)->sitting_on = prim;
// FIXME - the position calculation is actually more complex than this.
av->local_pos = info->offset + (prim->ob.world_pos - root->ob.world_pos);
av->rot = info->rot; // FIXME - rotation calculation here is wrong!
world_update_global_pos_int(sim, av);
world_mark_object_updated(sim, &root->ob, CAJ_OBJUPD_AVATARS);
world_mark_object_updated(sim, av, CAJ_OBJUPD_PARENT | CAJ_OBJUPD_POSROT);
world_mark_object_updated(sim, &prim->ob, CAJ_OBJUPD_AV_ON_SEAT);
return TRUE;
}
void world_unsit_avatar_now(struct simulator_ctx *sim, struct world_obj *av) {
struct avatar_obj *real_av = (avatar_obj*)av;
assert(av->type == OBJ_TYPE_AVATAR);
if(av->parent == NULL) {
assert(real_av->sitting_on == NULL);
return;
}
assert(real_av->sitting_on != NULL);
primitive_obj *seat = real_av->sitting_on;
primitive_obj *root = world_get_root_prim(seat);
assert(seat->avatar_sitting == av);
assert(av->parent == &root->ob);
caj_vector3 new_pos;
object_compute_global_pos(av, &new_pos);
new_pos.z += 0.5f;
//world_update_global_pos_int(sim, av);
seat->avatar_sitting = NULL;
av->parent = NULL;
//av->local_pos = av->world_pos;
world_move_root_obj_int(sim, av, new_pos);
int i;
for(i = 0; i < root->num_avatars; i++) {
if(root->avatars[i] == av) {
for(int j = i; j < root->num_avatars-1; j++)
root->avatars[j] = root->avatars[j+1];
break;
}
}
assert(i < root->num_avatars);
root->num_avatars--;
world_mark_object_updated(sim, av, CAJ_OBJUPD_PARENT); // FIXME
world_mark_object_updated(sim, &root->ob, CAJ_OBJUPD_AVATARS);
world_mark_object_updated(sim, &seat->ob, CAJ_OBJUPD_AV_ON_SEAT);
// FIXME - HACK
{
struct user_ctx *ctx = user_find_ctx(sim, av->id);
if(ctx != NULL) ctx->flags |= AGENT_FLAG_AV_FULL_UPD;
}
}
int world_unsit_avatar_via_script(struct simulator_ctx *sim,
struct primitive_obj *src_prim,
uuid_t avatar_id) {
world_obj *avatar = world_object_by_id(sim, avatar_id);
if(avatar == NULL || avatar->type != OBJ_TYPE_AVATAR ||
avatar->parent == NULL)
return FALSE;
src_prim = world_get_root_prim(src_prim);
// FIXME - should also allow unsit if avatar is over land owned
// by the prim owner.
if(&src_prim->ob != avatar->parent)
return FALSE;
world_unsit_avatar_now(sim, avatar);
return TRUE;
}
char* world_prim_upd_inv_filename(struct primitive_obj* prim) {
if(prim->inv.filename != NULL) return prim->inv.filename;
// knowing LL, they probably rely on the exact format of this somewhere.
char buf[40]; uuid_t u;
uuid_generate_random(u); uuid_unparse_lower(u, buf);
prim->inv.filename = (char*)malloc(51);
snprintf(prim->inv.filename, 51, "inventory_%s.tmp", buf);
return prim->inv.filename;
}
void world_prim_set_text(struct simulator_ctx *sim, struct primitive_obj* prim,
const char *text, uint8_t color[4]) {
int len = strlen(text); if(len > 254) len = 254;
free(prim->hover_text); prim->hover_text = (char*)malloc(len+1);
memcpy(prim->hover_text, text, len); prim->hover_text[len] = 0;
memcpy(prim->text_color, color, sizeof(prim->text_color));
world_mark_object_updated(sim, &prim->ob, CAJ_OBJUPD_TEXT);
}
void world_set_script_evmask(struct simulator_ctx *sim, struct primitive_obj* prim,
void *script_priv, int evmask) {
int prim_evmask = 0;
for(unsigned i = 0; i < prim->inv.num_items; i++) {
inventory_item *inv = prim->inv.items[i];
if(inv->inv_type == INV_TYPE_LSL && inv->asset_type == ASSET_LSL_TEXT
&& inv->spriv != NULL) {
script_info *sinfo = (script_info*)inv->spriv;
if(sinfo->priv != NULL)
prim_evmask |= sim->scripth.get_evmask(sim, sim->script_priv, sinfo->priv);
}
}
uint32_t newflags = prim->flags & ~(uint32_t)PRIM_FLAG_TOUCH;
if(prim_evmask & (CAJ_EVMASK_TOUCH|CAJ_EVMASK_TOUCH_CONT)) {
newflags |= PRIM_FLAG_TOUCH;
}
if(newflags != prim->flags) {
prim->flags = newflags;
world_mark_object_updated(sim, &prim->ob, CAJ_OBJUPD_FLAGS);
}
}
void user_prim_touch(struct user_ctx *ctx, struct primitive_obj* prim,
int touch_type, const struct caj_touch_info *info) {
struct simulator_ctx *sim = ctx->sim;
CAJ_DEBUG("DEBUG: in user_prim_touch, type %i\n", touch_type);
int handled = 0;
for(unsigned i = 0; i < prim->inv.num_items; i++) {
inventory_item *inv = prim->inv.items[i];
if(inv->asset_type == ASSET_LSL_TEXT && inv->spriv != NULL &&
((script_info*)inv->spriv)->priv != NULL) {
script_info* sinfo = (script_info*)inv->spriv;
int evmask = sim->scripth.get_evmask(sim, sim->script_priv,
sinfo->priv);
if(evmask & (CAJ_EVMASK_TOUCH_CONT | CAJ_EVMASK_TOUCH)) {
handled = 1; // any touch handler will do for this
// we leave finer-grained filtering to the script engine
CAJ_DEBUG("DEBUG: sending touch event %i to script\n", touch_type);
sim->scripth.touch_event(sim, sim->script_priv, sinfo->priv,
ctx, &ctx->av->ob, touch_type, info);
} else {