-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcajeput_user.cpp
2036 lines (1755 loc) · 72 KB
/
cajeput_user.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.
*/
#define DEBUG_CHANNEL 2147483647
#include "cajeput_core.h"
#include "cajeput_int.h"
#include "cajeput_anims.h"
#include "cajeput_grid_glue.h"
#include "caj_helpers.h"
#include <cassert>
#include <stdio.h>
const char *sl_throttle_names[] = { "resend","land","wind","cloud","task","texture","asset" };
const char *sl_wearable_names[] = {"body","skin","hair","eyes","shirt",
"pants","shoes","socks","jacket",
"gloves","undershirt","underpants",
"skirt", "alpha", "tattoo"};
// possible states of sys_folders requests:
#define SYS_FOLDERS_NOT_REQUESTED 0
#define SYS_FOLDERS_PENDING 1
#define SYS_FOLDERS_LOADED 2
#define SYS_FOLDERS_IN_CALLBACK 3
#define SYS_FOLDERS_BAD_STATE 4
void user_reset_timeout(struct user_ctx* ctx) {
ctx->last_activity = g_timer_elapsed(ctx->sgrp->timer, NULL);
}
void caj_uuid_to_name(struct simgroup_ctx *sgrp, uuid_t id,
void(*cb)(uuid_t uuid, const char* first,
const char* last, void *priv),
void *cb_priv) {
sgrp->gridh.uuid_to_name(sgrp, id, cb, cb_priv);
}
user_ctx *user_find_session(struct simulator_ctx *sim, const uuid_t agent_id,
const uuid_t session_id) {
for(user_ctx *ctx = sim->ctxts; ctx != NULL; ctx = ctx->next) {
if(uuid_compare(ctx->user_id, agent_id) == 0 &&
uuid_compare(ctx->session_id, session_id) == 0) {
return ctx;
}
}
return NULL;
}
user_ctx *user_find_ctx(struct simulator_ctx *sim, const uuid_t agent_id) {
for(user_ctx *ctx = sim->ctxts; ctx != NULL; ctx = ctx->next) {
if(uuid_compare(ctx->user_id, agent_id) == 0) {
return ctx;
}
}
return NULL;
}
void *user_get_grid_priv(struct user_ctx *user) {
return user->grid_priv;
}
void *user_get_priv(struct user_ctx *user) {
return user->user_priv;
}
void user_set_priv(struct user_ctx *user, void *priv) {
user->user_priv = priv;
}
struct simulator_ctx* user_get_sim(struct user_ctx *user) {
return user->sim;
}
struct simgroup_ctx* user_get_sgrp(struct user_ctx *user) {
return user->sgrp;
}
void user_get_uuid(struct user_ctx *user, uuid_t u) {
uuid_copy(u, user->user_id);
}
void user_get_session_id(struct user_ctx *user, uuid_t u) {
uuid_copy(u, user->session_id);
}
void user_get_secure_session_id(struct user_ctx *user, uuid_t u) {
uuid_copy(u, user->secure_session_id);
}
int user_check_session(struct user_ctx *user,
uuid_t agent, uuid_t session) {
return uuid_compare(user->user_id, agent) != 0 || uuid_compare(user->session_id, session) != 0;
}
uint32_t user_get_circuit_code(struct user_ctx *user) {
return user->circuit_code;
}
const char* user_get_first_name(struct user_ctx *user) {
return user->first_name;
}
const char* user_get_last_name(struct user_ctx *user) {
return user->last_name;
}
const char* user_get_name(struct user_ctx *user) {
return user->name;
}
const char* user_get_group_title(struct user_ctx *user) {
return user->group_title;
}
const char* user_get_group_name(struct user_ctx *user) {
return ""; // TODO
}
void user_get_active_group(struct user_ctx *user, uuid_t u) {
uuid_clear(u); // TODO
}
const caj_string* user_get_texture_entry(struct user_ctx *user) {
return &user->texture_entry;
}
const caj_string* user_get_visual_params(struct user_ctx *user) {
return &user->visual_params;
}
const struct animation_desc* user_get_default_anim(struct user_ctx *user) {
return &user->default_anim;
}
const wearable_desc* user_get_wearables(struct user_ctx* user) {
return user->wearables;
}
uint16_t* user_get_dirty_terrain_array(struct user_ctx *user) {
return user->dirty_terrain;
}
world_obj* user_get_avatar(struct user_ctx* user) {
if(user->av == NULL)
return NULL;
else return &user->av->ob;
}
void user_get_position(struct user_ctx* user, caj_vector3 *pos) {
if(user->av != NULL) {
*pos = user->av->ob.world_pos;
} else {
pos->x = 0.0f; pos->y = 0.0f; pos->z = 0.0f;
}
}
void user_get_initial_look_at(struct user_ctx* user, caj_vector3 *pos) {
*pos = user->start_look_at;
}
float user_get_draw_dist(struct user_ctx *user) {
return user->draw_dist;
}
void user_set_draw_dist(struct user_ctx *user, float far) {
user->draw_dist = far;
}
uint32_t user_get_flags(struct user_ctx *user) {
return user->flags;
}
void user_set_flag(struct user_ctx *user, uint32_t flag) {
user->flags |= flag;
}
void user_clear_flag(struct user_ctx *user, uint32_t flag) {
user->flags &= ~flag;
}
void user_add_self_pointer(struct user_ctx** pctx) {
(*pctx)->self_ptrs.insert(pctx);
}
void user_del_self_pointer(struct user_ctx** pctx) {
(*pctx)->self_ptrs.erase(pctx);
*pctx = NULL;
}
void user_set_texture_entry(struct user_ctx *user, struct caj_string* data) {
caj_string_free(&user->texture_entry);
user->texture_entry = *data;
data->data = NULL;
user->flags |= AGENT_FLAG_APPEARANCE_UPD; // FIXME - send full update instead?
}
void user_set_visual_params(struct user_ctx *user, struct caj_string* data) {
caj_string_free(&user->visual_params);
user->visual_params = *data;
data->data = NULL;
user->flags |= AGENT_FLAG_APPEARANCE_UPD;
}
void user_set_wearable_serial(struct user_ctx *ctx, uint32_t serial) {
ctx->wearable_serial = serial;
}
void user_set_wearable(struct user_ctx *ctx, int id,
const uuid_t item_id, const uuid_t asset_id) {
if(id >= SL_NUM_WEARABLES || id < 0) {
printf("ERROR: user_set_wearable bad id %i\n",id);
return;
}
uuid_copy(ctx->wearables[id].item_id, item_id);
uuid_copy(ctx->wearables[id].asset_id, asset_id);
}
struct wearable_item_query_st {
user_ctx *ctx;
int id;
wearable_item_query_st(user_ctx *ctx, int id) : ctx(ctx), id(id) {
user_add_self_pointer(&this->ctx);
}
~wearable_item_query_st() {
if(ctx != NULL) user_del_self_pointer(&ctx);
}
};
void user_incr_pending_wearables(struct user_ctx *ctx) {
ctx->pending_wearable_lookups++;
}
void user_decr_pending_wearables(struct user_ctx *ctx) {
ctx->pending_wearable_lookups--;
if(ctx->userh->wearables_changed != NULL &&
ctx->pending_wearable_lookups == 0)
ctx->userh->wearables_changed(ctx->user_priv);
}
static void wearable_item_cb(struct inventory_item* item, void* priv) {
wearable_item_query_st *st = (wearable_item_query_st*)priv;
if(st->ctx != NULL) {
if(item != NULL &&
uuid_compare(item->item_id, st->ctx->wearables[st->id].item_id) == 0) {
uuid_copy(st->ctx->wearables[st->id].asset_id, item->asset_id);
}
st->ctx->wearable_serial++;
user_decr_pending_wearables(st->ctx);
}
delete st;
}
void user_set_wearable_item_id(struct user_ctx *ctx, int id,
const uuid_t item_id) {
if(id >= SL_NUM_WEARABLES || id < 0) {
printf("ERROR: user_set_wearable_item_id bad id %i\n",id);
return;
}
if(uuid_is_null(item_id)) {
uuid_clear(ctx->wearables[id].item_id);
uuid_clear(ctx->wearables[id].asset_id);
user_incr_pending_wearables(ctx);
ctx->wearable_serial++;
user_decr_pending_wearables(ctx);
} else if(uuid_compare(item_id, ctx->wearables[id].item_id) != 0) {
uuid_copy(ctx->wearables[id].item_id, item_id);
uuid_clear(ctx->wearables[id].asset_id);
wearable_item_query_st *st = new wearable_item_query_st(ctx, id);
user_incr_pending_wearables(ctx);
user_fetch_inventory_item(ctx, item_id, ctx->user_id, wearable_item_cb,
st);
}
}
uint32_t user_get_wearable_serial(struct user_ctx *ctx) {
return ctx->wearable_serial;
}
void user_set_throttles(struct user_ctx *ctx, float rates[]) {
double time_now = g_timer_elapsed(ctx->sgrp->timer, NULL);
for(int i = 0; i < SL_NUM_THROTTLES; i++) {
ctx->throttles[i].time = time_now;
ctx->throttles[i].level = 0.0f;
ctx->throttles[i].rate = rates[i] / 8.0f;
}
}
void user_set_throttles_block(struct user_ctx* ctx, unsigned char* data,
int len) {
float throttles[SL_NUM_THROTTLES];
if(len < SL_NUM_THROTTLES*4) {
printf("Error: AgentThrottle with not enough data\n");
return;
}
printf("DEBUG: got new throttles:\n");
for(int i = 0; i < SL_NUM_THROTTLES; i++) {
throttles[i] = caj_bin_to_float_le(data + 4*i);
printf(" throttle %s: %f\n", sl_throttle_names[i], throttles[i]);
user_set_throttles(ctx, throttles);
}
}
void user_get_throttles_block(struct user_ctx* ctx, unsigned char* data,
int len) {
if(len < SL_NUM_THROTTLES*4) {
printf("Error: AgentThrottle with not enough data\n");
return;
} else {
len = SL_NUM_THROTTLES*4;
}
for(int i = 0; i < SL_NUM_THROTTLES; i++) {
caj_float_to_bin_le(data + i*4, ctx->throttles[i].rate * 8.0f);
}
}
void user_reset_throttles(struct user_ctx *ctx) {
double time_now = g_timer_elapsed(ctx->sgrp->timer, NULL);
for(int i = 0; i < SL_NUM_THROTTLES; i++) {
ctx->throttles[i].time = time_now;
ctx->throttles[i].level = 0.0f;
}
}
void user_update_throttles(struct user_ctx *ctx) {
double time_now = g_timer_elapsed(ctx->sgrp->timer, NULL);
for(int i = 0; i < SL_NUM_THROTTLES; i++) {
assert(time_now >= ctx->throttles[i].time); // need monotonic time
ctx->throttles[i].level += ctx->throttles[i].rate *
(time_now - ctx->throttles[i].time);
if(ctx->throttles[i].level > ctx->throttles[i].rate * 0.3f) {
// limit maximum reservoir level to 0.3 sec of data
ctx->throttles[i].level = ctx->throttles[i].rate * 0.3f;
}
ctx->throttles[i].time = time_now;
}
}
void user_throttle_expend(struct user_ctx *ctx, int id, float amount) {
ctx->throttles[id].level -= amount;
}
float user_throttle_level(struct user_ctx *ctx, int id) {
return ctx->throttles[id].level;
}
int user_owns_prim(struct user_ctx *ctx, struct primitive_obj *prim) {
return uuid_compare(prim->owner, ctx->user_id) == 0;
}
// FIXME - move this somewhere saner!
static void set_default_anim(struct user_ctx* ctx, uuid_t anim) {
if(uuid_compare(ctx->default_anim.anim, anim) != 0) {
uuid_copy(ctx->default_anim.anim, anim);
ctx->default_anim.sequence = ctx->anim_seq++; // FIXME
ctx->default_anim.caj_type = CAJ_ANIM_TYPE_DEFAULT;
uuid_copy(ctx->default_anim.obj, ctx->user_id);
ctx->flags |= AGENT_FLAG_ANIM_UPDATE;
}
}
// FIXME - move these to a header.
#define AGENT_CONTROL_AT_POS (1<<0)
#define AGENT_CONTROL_AT_NEG (1<<1)
#define AGENT_CONTROL_LEFT_POS (1<<2)
#define AGENT_CONTROL_LEFT_NEG (1<<3)
#define AGENT_CONTROL_UP_POS (1<<4)
#define AGENT_CONTROL_UP_NEG (1<<5)
#define AGENT_CONTROL_FLY (1<<13)
#define AGENT_CONTROL_STOP (1<<14) // ????
#define AGENT_CONTROL_STAND_UP (1<<16)
void user_set_control_flags(struct user_ctx *ctx, uint32_t control_flags,
const caj_quat *rot) {
if(ctx->av != NULL) {
if(!caj_quat_equal(&ctx->av->ob.rot, rot)) {
ctx->av->ob.rot = *rot;
}
if(control_flags & AGENT_CONTROL_STAND_UP) {
user_stand_up(ctx);
}
int is_flying = (control_flags & AGENT_CONTROL_FLY) != 0;
int is_running = (ctx->flags & AGENT_FLAG_ALWAYS_RUN) != 0;
caj_vector3 velocity;
velocity.x = 0.0f; velocity.y = 0.0f; velocity.z = 0.0f;
if(control_flags & AGENT_CONTROL_AT_POS)
velocity.x = is_flying ? 6.0 : (is_running ? 4.0 : 2.0);
if(control_flags & AGENT_CONTROL_AT_NEG)
velocity.x = is_flying ? -4.0 : -1.5;
if(control_flags & AGENT_CONTROL_LEFT_POS)
velocity.y = is_flying ? 4.0 : 1.5;
if(control_flags & AGENT_CONTROL_LEFT_NEG)
velocity.y = is_flying ? -4.0 : -1.5;
if(control_flags & AGENT_CONTROL_UP_POS)
velocity.z = 4.0;
if(control_flags & AGENT_CONTROL_UP_NEG)
velocity.z = -4.0;
caj_mult_vect3_quat(&velocity,&ctx->av->ob.rot,&velocity);
ctx->sim->physh.set_avatar_flying(ctx->sim,ctx->sim->phys_priv,&ctx->av->ob,is_flying);
ctx->sim->physh.set_target_velocity(ctx->sim,ctx->sim->phys_priv,&ctx->av->ob,velocity);
// FIXME - iffy
if(is_flying) {
// we don't have any auto-land code here. Since we send the right
// foot plane, the viewer takes care of it for us. I eventually managed
// to figure out why the OpenSim code works, though, and it's screwy.
if(control_flags & (AGENT_CONTROL_AT_POS|AGENT_CONTROL_AT_NEG|
AGENT_CONTROL_LEFT_POS|AGENT_CONTROL_LEFT_NEG)) {
set_default_anim(ctx, fly_anim);
} else if(control_flags & AGENT_CONTROL_UP_POS) {
set_default_anim(ctx, hover_up_anim);
} else if(control_flags & AGENT_CONTROL_UP_NEG) {
set_default_anim(ctx, hover_down_anim);
} else {
set_default_anim(ctx, hover_anim);
}
} else if(is_running && (control_flags & AGENT_CONTROL_AT_POS)) {
set_default_anim(ctx, run_anim);
} else {
if(control_flags & (AGENT_CONTROL_AT_POS|AGENT_CONTROL_AT_NEG|
AGENT_CONTROL_LEFT_POS|AGENT_CONTROL_LEFT_NEG)) {
set_default_anim(ctx, walk_anim);
} else {
set_default_anim(ctx, stand_anim);
}
}
}
}
int user_begin_sit(struct user_ctx *ctx, struct primitive_obj *seat,
struct caj_sit_info *info_out) {
if(ctx->av == NULL) return FALSE;
return world_avatar_begin_sit(ctx->sim, &ctx->av->ob, seat, info_out);
}
int user_complete_sit(struct user_ctx *ctx, struct caj_sit_info *info) {
if(ctx->av == NULL) return FALSE;
int success = world_avatar_complete_sit(ctx->sim, &ctx->av->ob, info);
if(success) ctx->flags |= AGENT_FLAG_AV_FULL_UPD;
return success;
}
void user_stand_up(struct user_ctx *ctx) {
if(ctx->av == NULL) return;
world_unsit_avatar_now(ctx->sim, &ctx->av->ob);
}
int32_t user_get_an_anim_seq(struct user_ctx *ctx) {
return ctx->anim_seq++;
}
// FIXME - optimise this
void user_add_animation(struct user_ctx *ctx, struct animation_desc* anim,
int replace) {
if(replace) {
// FIXME - is the replace functionality actually useful?
int found = 0;
for(std::vector<animation_desc>::iterator iter = ctx->anims.begin();
iter != ctx->anims.end(); /* nothing */) {
if(uuid_compare(iter->anim, anim->anim) == 0 ||
iter->caj_type == anim->caj_type) {
if(found) {
ctx->flags |= AGENT_FLAG_ANIM_UPDATE;
iter = ctx->anims.erase(iter);
continue;
} else if(uuid_compare(iter->anim, anim->anim) == 0 &&
iter->caj_type == anim->caj_type) {
found = 1; /* do nothing - FIXME update other stuff */
} else {
ctx->flags |= AGENT_FLAG_ANIM_UPDATE;
*iter = *anim; found = 1;
}
}
iter++;
}
} else {
for(std::vector<animation_desc>::iterator iter = ctx->anims.begin();
iter != ctx->anims.end(); iter++) {
if(uuid_compare(iter->anim, anim->anim) == 0) {
iter->caj_type = anim->caj_type;
return; // FIXME - update other stuff
}
}
ctx->anims.push_back(*anim);
ctx->flags |= AGENT_FLAG_ANIM_UPDATE;
}
}
void user_clear_animation_by_type(struct user_ctx *ctx, int caj_type) {
for(std::vector<animation_desc>::iterator iter = ctx->anims.begin();
iter != ctx->anims.end(); /* nothing */) {
if(iter->caj_type == caj_type) {
ctx->flags |= AGENT_FLAG_ANIM_UPDATE;
iter = ctx->anims.erase(iter);
} else {
iter++;
}
}
}
void user_clear_animation_by_id(struct user_ctx *ctx, uuid_t anim) {
for(std::vector<animation_desc>::iterator iter = ctx->anims.begin();
iter != ctx->anims.end(); /* nothing */) {
if(uuid_compare(iter->anim, anim) == 0) {
ctx->flags |= AGENT_FLAG_ANIM_UPDATE;
iter = ctx->anims.erase(iter);
} else {
iter++;
}
}
}
void user_av_chat_callback(struct simulator_ctx *sim, struct world_obj *obj,
const struct chat_message *msg,
struct obj_chat_listener *listen, void *user_data) {
struct user_ctx* ctx = (user_ctx*)user_data;
if(ctx->userh != NULL && ctx->userh->chat_callback != NULL)
ctx->userh->chat_callback(ctx->user_priv, msg);
}
void user_send_message(struct user_ctx *ctx, const char* msg) {
struct chat_message chat;
chat.channel = 0;
chat.source_type = CHAT_SOURCE_SYSTEM;
chat.chat_type = CHAT_TYPE_NORMAL;
uuid_clear(chat.source); // FIXME - set this to something?
uuid_clear(chat.owner);
chat.name = (char*)"Cajeput";
chat.msg = (char*)msg;
// slightly evil hack
user_av_chat_callback(ctx->sim, NULL, &chat, NULL, ctx);
}
void user_send_alert_message(struct user_ctx *ctx, const char* msg,
int is_modal) {
if(ctx->userh != NULL && ctx->userh->alert_message != NULL)
ctx->userh->alert_message(ctx->user_priv, msg, is_modal);
}
int user_request_god_powers(user_ctx *ctx) {
return 200; // TODO
}
void user_relinquish_god_powers(user_ctx *ctx) {
// TODO
}
void user_fetch_inventory_folder(user_ctx *user, const uuid_t folder_id,
const uuid_t owner_id,
void(*cb)(struct inventory_contents* inv,
void* priv),
void *cb_priv) {
simgroup_ctx *sgrp = user->sgrp;
if(uuid_compare(owner_id, user->user_id) == 0) {
printf("DEBUG: fetching from inventory\n");
sgrp->gridh.fetch_inventory_folder(sgrp,user,user->grid_priv,
folder_id,cb,cb_priv);
} else {
printf("DEBUG: fetching from library\n");
std::map<obj_uuid_t,inventory_contents*>::iterator iter =
sgrp->inv_lib.find(folder_id);
if(iter == sgrp->inv_lib.end()) {
printf("ERROR: folder not in library\n");
cb(NULL, cb_priv);
} else {
printf("DEBUG: got library folder, %i items\n", iter->second->num_items);
cb(iter->second, cb_priv);
}
}
}
void user_fetch_inventory_item(user_ctx *user, const uuid_t item_id,
const uuid_t owner_id,
void(*cb)(struct inventory_item* item,
void* priv),
void *cb_priv) {
if(uuid_compare(owner_id, user->user_id) == 0) {
user->sgrp->gridh.fetch_inventory_item(user->sgrp,user,user->grid_priv,
item_id,cb,cb_priv);
} else {
#if 0 // FIXME - handle library right!
std::map<obj_uuid_t,inventory_contents*>::iterator iter =
sim->inv_lib.find(folder_id);
if(iter == sim->inv_lib.end()) {
sim->gridh.fetch_inventory_folder(sim,user,user->grid_priv,
folder_id,cb,cb_priv);
} else {
cb(iter->second, cb_priv);
}
#endif
cb(NULL, cb_priv);
}
}
void user_add_inventory_item(user_ctx *ctx, struct inventory_item* item,
void(*cb)(void* priv, int success, uuid_t item_id),
void *cb_priv) {
uuid_generate(item->item_id);
uuid_copy(item->owner_id, ctx->user_id);
ctx->sgrp->gridh.add_inventory_item(ctx->sgrp, ctx, ctx->grid_priv,
item, cb, cb_priv);
}
struct update_inv_asset_st {
user_ctx *ctx;
void(*cb)(void *priv, int success, uuid_t new_asset_id);
void *cb_priv;
int asset_type;
uuid_t item_id, asset_id;
update_inv_asset_st(user_ctx *ctx, void(*cb)(void *priv, int success,
uuid_t new_asset_id),
void *cb_priv, int asset_type)
: ctx(ctx), cb(cb), cb_priv(cb_priv), asset_type(asset_type) {
user_add_self_pointer(&this->ctx);
}
~update_inv_asset_st() {
if(ctx) user_del_self_pointer(&ctx);
}
};
static void update_inv_asset_invmod_cb(void* priv, int success) {
update_inv_asset_st *st = (update_inv_asset_st*)priv;
if(st->ctx == NULL || !success) {
printf("DEBUG: user_update_inventory_asset: bailing after inv modify\n");
uuid_t u; uuid_clear(u);
st->cb(st->cb_priv, FALSE, u);
} else {
st->cb(st->cb_priv, TRUE, st->asset_id);
}
delete st;
}
static void update_inv_asset_invf_cb(struct inventory_item* item,
void* priv) {
update_inv_asset_st *st = (update_inv_asset_st*)priv;
if(st->ctx == NULL || item == NULL || item->asset_type != st->asset_type ||
st->ctx->sgrp->gridh.update_inventory_item == NULL) {
if(item != NULL && item->asset_type != st->asset_type) {
printf("DEBUG: user_update_inventory_asset: mismatched asset type\n");
printf("DEBUG: upload is %i, inventory has %i\n", (int)st->asset_type,
(int)item->asset_type);
}
printf("DEBUG: user_update_inventory_asset: bailing after inv load\n");
uuid_t u; uuid_clear(u);
st->cb(st->cb_priv, FALSE, u);
delete st; return;
}
uuid_copy(item->asset_id, st->asset_id);
st->ctx->sgrp->gridh.update_inventory_item(st->ctx->sgrp, st->ctx,
st->ctx->grid_priv,
item, update_inv_asset_invmod_cb, st);
}
static void update_inv_asset_cb(uuid_t asset_id, void* priv) {
update_inv_asset_st *st = (update_inv_asset_st*)priv;
if(st->ctx == NULL || uuid_is_null(asset_id)) {
uuid_t u; uuid_clear(u);
st->cb(st->cb_priv, FALSE, u);
delete st; return;
}
uuid_copy(st->asset_id, asset_id);
user_fetch_inventory_item(st->ctx, st->item_id, st->ctx->user_id,
update_inv_asset_invf_cb, st);
}
void user_update_inventory_asset(user_ctx *ctx, uuid_t item_id, int asset_type,
caj_string *data, void(*cb)(void *priv, int success,
uuid_t new_asset_id),
void *cb_priv) {
// FIXME - re-order this to check inventory first at some point
update_inv_asset_st *st = new update_inv_asset_st(ctx, cb, cb_priv, asset_type);
uuid_copy(st->item_id, item_id);
struct simple_asset asset;
asset.name = const_cast<char*>("");
asset.description = const_cast<char*>("");
asset.type = asset_type;
uuid_generate(asset.id);
asset.data = *data;
caj_put_asset(ctx->sgrp, &asset, update_inv_asset_cb, st);
}
void user_set_system_folders(struct user_ctx *ctx,
struct inventory_contents* inv) {
if(ctx->sys_folders != NULL)
caj_inv_free_contents_desc(ctx->sys_folders);
ctx->sys_folders = inv;
ctx->sys_folders_state = SYS_FOLDERS_IN_CALLBACK;
for(caj_callback<user_generic_cb>::cb_set::iterator iter =
ctx->sys_folders_cbs.callbacks.begin();
iter != ctx->sys_folders_cbs.callbacks.end(); iter++) {
iter->cb(ctx, iter->priv);
}
ctx->sys_folders_cbs.callbacks.clear();
ctx->sys_folders_state = SYS_FOLDERS_LOADED;
}
struct inventory_folder* user_find_system_folder(user_ctx *ctx,
int8_t asset_type) {
assert(ctx->sys_folders_state == SYS_FOLDERS_IN_CALLBACK);
if(ctx->sys_folders == NULL) return NULL;
for(unsigned int i = 0; i < ctx->sys_folders->num_subfolder; i++) {
if(ctx->sys_folders->subfolders[i].asset_type == asset_type)
return &ctx->sys_folders->subfolders[i];
}
return NULL;
}
// don't rely on the cb == NULL behaviour; it may change in future.
void user_fetch_system_folders(user_ctx *ctx, user_generic_cb cb,
void *cb_priv) {
if(ctx->sys_folders_state == SYS_FOLDERS_NOT_REQUESTED) {
ctx->sys_folders_state = SYS_FOLDERS_PENDING;
ctx->sgrp->gridh.fetch_system_folders(ctx->sgrp, ctx, ctx->grid_priv);
}
if(ctx->sys_folders_state == SYS_FOLDERS_LOADED) {
ctx->sys_folders_state = SYS_FOLDERS_IN_CALLBACK;
if(cb != NULL) cb(ctx, cb_priv);
ctx->sys_folders_state = SYS_FOLDERS_LOADED;
} else if(ctx->sys_folders_state == SYS_FOLDERS_IN_CALLBACK) {
// this is permitted, so I suppose we should handle it...
if(cb != NULL) cb(ctx, cb_priv);
} else {
assert(ctx->sys_folders_state == SYS_FOLDERS_PENDING);
if(cb != NULL) ctx->sys_folders_cbs.add_callback(cb, cb_priv);
}
}
static void free_system_folders(user_ctx *ctx) {
// this is a valid (if undocumented) use of user_set_system_folders
user_set_system_folders(ctx, NULL);
ctx->sys_folders_state = SYS_FOLDERS_BAD_STATE;
}
void user_send_script_dialog(user_ctx *ctx, primitive_obj* prim,
char *msg, int num_buttons, char** buttons,
int32_t channel) {
if(ctx->userh->script_dialog != NULL)
ctx->userh->script_dialog(ctx->user_priv, prim, msg, num_buttons,
buttons, channel);
}
static void sanitise_teleport_pos(simulator_ctx *sim, caj_vector3 *pos) {
if(!finite(pos->x)) pos->x = 128.0f;
else if(pos->x <= 0.0f) pos->x = 1.0f;
else if(pos->x >= WORLD_REGION_SIZE) pos->x = WORLD_REGION_SIZE - 1.0f;
if(!finite(pos->y)) pos->y = 128.0f;
else if(pos->y <= 0.0f) pos->y = 1.0f;
else if(pos->y >= WORLD_REGION_SIZE) pos->y = WORLD_REGION_SIZE - 1.0f;
if(!finite(pos->z)) pos->z = 1.0f;
else if(pos->z <= 0.0f) pos->z = 1.0f;
else if(pos->z >= WORLD_HEIGHT) pos->z = WORLD_HEIGHT - 1.0f;
float min_height = 1.0f + sim_get_terrain_height(sim, pos->x, pos->y);
if(pos->z < min_height) pos->z = min_height;
}
static teleport_desc* begin_teleport(struct user_ctx* ctx) {
if(ctx->tp_out != NULL) {
printf("!!! ERROR: can't teleport while teleporting!\n");
return NULL;
} else if(ctx->av == NULL) {
printf("!!! ERROR: can't teleport with no body!\n");
return NULL;
}
teleport_desc* desc = new teleport_desc();
desc->ctx = ctx;
user_add_self_pointer(&desc->ctx);
ctx->tp_out = desc;
return desc;
}
static void del_teleport_desc(teleport_desc* desc) {
if(desc->ctx != NULL) {
assert(desc->ctx->tp_out == desc);
desc->ctx->tp_out = NULL;
user_del_self_pointer(&desc->ctx);
}
delete desc;
}
void user_teleport_failed(struct teleport_desc* tp, const char* reason) {
if(tp->ctx != NULL && tp->ctx->userh->teleport_failed != NULL) {
tp->ctx->userh->teleport_failed(tp->ctx, reason);
}
del_teleport_desc(tp);
}
// In theory, we can send arbitrary strings, but that seems to be bugged.
void user_teleport_progress(struct teleport_desc* tp, const char* msg) {
if(tp->ctx != NULL && tp->ctx->userh->teleport_progress != NULL)
tp->ctx->userh->teleport_progress(tp->ctx, msg, tp->flags);
}
static void user_complete_teleport_int(struct teleport_desc* tp, int is_local) {
if(tp->ctx != NULL) {
printf("DEBUG: completing teleport\n");
tp->ctx->flags |= AGENT_FLAG_TELEPORT_COMPLETE;
// FIXME - need to check hook not NULL?
if(is_local)
tp->ctx->userh->teleport_local(tp->ctx, tp);
else
tp->ctx->userh->teleport_complete(tp->ctx, tp);
}
del_teleport_desc(tp);
}
void user_complete_teleport(struct teleport_desc* tp) {
user_complete_teleport_int(tp, FALSE);
}
// for after region handle is resolved...
static void do_real_teleport(struct teleport_desc* tp) {
if(tp->ctx == NULL) {
user_teleport_failed(tp,"cancelled");
} else if(tp->region_handle == tp->ctx->sim->region_handle) {
if(tp->ctx->av == NULL) {
// FIXME - not quite right?
user_teleport_failed(tp,"Your avatar disappeared!");
return;
}
// FIXME - this is horribly hacky!
sanitise_teleport_pos(tp->ctx->sim, &tp->pos);
struct caj_multi_upd pos_upd;
pos_upd.flags = CAJ_MULTI_UPD_POS;
pos_upd.pos = tp->pos;
world_multi_update_obj(tp->ctx->sim, &tp->ctx->av->ob, &pos_upd);
user_complete_teleport_int(tp, TRUE);
} else {
simgroup_ctx *sgrp = tp->ctx->sgrp;
sgrp->gridh.do_teleport(sgrp,tp->ctx->sim, tp);
}
}
void user_teleport_location(struct user_ctx* ctx, uint64_t region_handle,
const caj_vector3 *pos, const caj_vector3 *look_at,
int is_from_viewer) {
teleport_desc* desc = begin_teleport(ctx);
if(desc == NULL) return;
desc->region_handle = region_handle;
desc->pos = *pos;
desc->look_at = *look_at;
desc->flags = TELEPORT_TO_LOCATION;
if(!is_from_viewer && ctx->userh->teleport_begin != NULL) {
// FIXME - should this be unconditional? Dunno...
ctx->userh->teleport_begin(ctx, desc);
}
do_real_teleport(desc);
}
void tp_region_name_cb(void* cb_priv, struct map_block_info* block) {
teleport_desc* tp = (teleport_desc*)cb_priv;
if(tp->ctx == NULL) {
user_teleport_failed(tp,"cancelled");
} else if(block == NULL) {
user_teleport_failed(tp,"Destination region not found");
} else {
tp->region_handle = ((uint64_t)block->x <<40)|((uint64_t)block->y << 8);
do_real_teleport(tp);
}
}
void user_teleport_by_region_name(struct user_ctx* ctx, char *region_name,
const caj_vector3 *pos,
const caj_vector3 *look_at,
int is_from_viewer) {
teleport_desc* desc = begin_teleport(ctx);
if(desc == NULL) return;
desc->region_handle = 0;
desc->pos = *pos;
desc->look_at = *look_at;
desc->flags = TELEPORT_TO_LOCATION;
if(!is_from_viewer && ctx->userh->teleport_begin != NULL) {
ctx->userh->teleport_begin(ctx, desc);
}
caj_map_region_by_name(ctx->sgrp, region_name,
tp_region_name_cb, desc);
}
void user_teleport_landmark(struct user_ctx* ctx, uuid_t landmark) {
teleport_desc* desc = begin_teleport(ctx);
if(desc == NULL) return;
// FIXME - todo
if(uuid_is_null(landmark)) {
desc->flags = TELEPORT_TO_HOME;
user_teleport_failed(desc,"FIXME: teleport home not supported");
} else {
desc->flags = TELEPORT_TO_LOCATION;
user_teleport_failed(desc,"FIXME: teleport to landmark not supported");
}
}
// FIXME - HACK
void user_teleport_add_temp_child(struct user_ctx* ctx, uint64_t region,
uint32_t sim_ip, uint16_t sim_port,
const char* seed_cap) {
// FIXME - need to move this into general EnableSimulator handling once
// we have such a thing.
caj_llsd *body = llsd_new_map();
caj_llsd *sims = llsd_new_array();
caj_llsd *info = llsd_new_map();
llsd_map_append(info, "IP", llsd_new_binary(&sim_ip,4)); // big-endian?
llsd_map_append(info, "Port", llsd_new_int(sim_port));
llsd_map_append(info, "Handle", llsd_new_from_u64(region));
llsd_array_append(sims, info);
llsd_map_append(body,"SimulatorInfo",sims);
user_event_queue_send(ctx,"EnableSimulator",body);
}
static void debug_prepare_new_user(struct simulator_ctx *sim,
struct sim_new_user *uinfo) {
char user_id[40], session_id[40];
uuid_unparse(uinfo->user_id,user_id);
uuid_unparse(uinfo->session_id,session_id);
printf("DEBUG: %s expecting new user %s %s, user_id=%s, session_id=%s, "
"circuit_code=%lu (%s)\n", sim->shortname,
uinfo->first_name, uinfo->last_name,
user_id, session_id, (unsigned long)uinfo->circuit_code,
uinfo->is_child ? "child" : "main");
}
static float throttle_init[SL_NUM_THROTTLES] = {
64000.0, 64000.0, 64000.0, 64000.0, 64000.0,
64000.0, 64000.0,
};
struct user_ctx* sim_prepare_new_user(struct simulator_ctx *sim,
struct sim_new_user *uinfo) {
struct user_ctx* ctx;
ctx = new user_ctx(sim);
ctx->userh = NULL; ctx->user_priv = NULL;
ctx->draw_dist = 0.0f;
ctx->circuit_code = uinfo->circuit_code;
ctx->tp_out = NULL;
ctx->texture_entry.len = 0;
ctx->texture_entry.data = NULL;
ctx->visual_params.len = 0;
ctx->visual_params.data = NULL;
ctx->wearable_serial = 0;
memset(ctx->wearables, 0, sizeof(ctx->wearables));
ctx->pending_wearable_lookups = 0;
uuid_copy(ctx->default_anim.anim, stand_anim);
ctx->default_anim.sequence = 1;
uuid_clear(ctx->default_anim.obj); // FIXME - is this right?
ctx->default_anim.caj_type = CAJ_ANIM_TYPE_DEFAULT;
ctx->anim_seq = 2;
debug_prepare_new_user(sim, uinfo);
uuid_copy(ctx->user_id, uinfo->user_id);