| | 1263 | /* |
|---|
| | 1264 | cv & res may be the same. |
|---|
| | 1265 | |
|---|
| | 1266 | Return zero if error thrown. |
|---|
| | 1267 | */ |
|---|
| | 1268 | static int _curveValue( UThread* ut, UCell* cv, UCell* res, double t ) |
|---|
| | 1269 | { |
|---|
| | 1270 | UBlock* blk; |
|---|
| | 1271 | UCell* it; |
|---|
| | 1272 | UCell* end; |
|---|
| | 1273 | UCell* v1; |
|---|
| | 1274 | int len; |
|---|
| | 1275 | double t1, d; |
|---|
| | 1276 | |
|---|
| | 1277 | blk = ur_block( cv ); |
|---|
| | 1278 | UR_ITER_BLOCK( it, end, blk, cv ); |
|---|
| | 1279 | len = end - it; |
|---|
| | 1280 | if( len & 1 ) |
|---|
| | 1281 | { |
|---|
| | 1282 | --len; |
|---|
| | 1283 | --end; |
|---|
| | 1284 | } |
|---|
| | 1285 | if( len ) |
|---|
| | 1286 | { |
|---|
| | 1287 | v1 = 0; |
|---|
| | 1288 | while( it != end ) |
|---|
| | 1289 | { |
|---|
| | 1290 | // Assume decimal! for speed. |
|---|
| | 1291 | if( t <= ur_decimal(it) ) |
|---|
| | 1292 | { |
|---|
| | 1293 | if( ! v1 ) |
|---|
| | 1294 | { |
|---|
| | 1295 | ur_copyCell( res, it[1] ); |
|---|
| | 1296 | return 1; |
|---|
| | 1297 | } |
|---|
| | 1298 | |
|---|
| | 1299 | t1 = ur_decimal(v1); |
|---|
| | 1300 | d = ur_decimal(it) - t1; |
|---|
| | 1301 | if( ! d ) |
|---|
| | 1302 | goto copy; |
|---|
| | 1303 | |
|---|
| | 1304 | if( _lerpCells( v1 + 1, it + 1, (t - t1) / d, res ) ) |
|---|
| | 1305 | return 1; |
|---|
| | 1306 | |
|---|
| | 1307 | ur_throwErr( UR_ERR_DATATYPE, |
|---|
| | 1308 | "lerp expected 2 decimal!/vec3!/coord! values" ); |
|---|
| | 1309 | return 0; |
|---|
| | 1310 | } |
|---|
| | 1311 | v1 = it; |
|---|
| | 1312 | it += 2; |
|---|
| | 1313 | } |
|---|
| | 1314 | copy: |
|---|
| | 1315 | ur_copyCell( res, v1[1] ); |
|---|
| | 1316 | } |
|---|
| | 1317 | else |
|---|
| | 1318 | { |
|---|
| | 1319 | ur_setNone( res ); |
|---|
| | 1320 | } |
|---|
| | 1321 | return 1; |
|---|
| | 1322 | } |
|---|
| | 1323 | |
|---|
| | 1324 | |
|---|
| 1284 | | blk = ur_block( cv ); |
|---|
| 1285 | | UR_ITER_BLOCK( it, end, blk, cv ); |
|---|
| 1286 | | len = end - it; |
|---|
| 1287 | | if( len & 1 ) |
|---|
| 1288 | | { |
|---|
| 1289 | | --len; |
|---|
| 1290 | | --end; |
|---|
| 1291 | | } |
|---|
| 1292 | | if( len ) |
|---|
| 1293 | | { |
|---|
| 1294 | | v1 = 0; |
|---|
| 1295 | | while( it != end ) |
|---|
| 1296 | | { |
|---|
| 1297 | | // Assume decimal! for speed. |
|---|
| 1298 | | if( t <= ur_decimal(it) ) |
|---|
| 1299 | | { |
|---|
| 1300 | | if( ! v1 ) |
|---|
| 1301 | | { |
|---|
| 1302 | | ur_copyCell( cv, it[1] ); |
|---|
| 1303 | | goto finish; |
|---|
| 1304 | | } |
|---|
| 1305 | | |
|---|
| 1306 | | t1 = ur_decimal(v1); |
|---|
| 1307 | | d = ur_decimal(it) - t1; |
|---|
| 1308 | | if( ! d ) |
|---|
| 1309 | | goto copy; |
|---|
| 1310 | | |
|---|
| 1311 | | if( _lerpCells( v1 + 1, it + 1, (t - t1) / d, cv ) ) |
|---|
| 1312 | | goto finish; |
|---|
| 1313 | | ur_throwErr( UR_ERR_DATATYPE, |
|---|
| 1314 | | "lerp expected 2 decimal!/vec3!/coord! values" ); |
|---|
| 1315 | | return; |
|---|
| 1316 | | } |
|---|
| 1317 | | v1 = it; |
|---|
| 1318 | | it += 2; |
|---|
| 1319 | | } |
|---|
| 1320 | | copy: |
|---|
| 1321 | | ur_copyCell( cv, v1[1] ); |
|---|
| 1322 | | } |
|---|
| 1323 | | else |
|---|
| 1324 | | { |
|---|
| 1325 | | ur_setNone( cv ); |
|---|
| 1326 | | } |
|---|
| 1327 | | |
|---|
| 1328 | | finish: |
|---|
| 1329 | | UR_S_DROP; |
|---|
| | 1341 | if( _curveValue( ut, cv, cv, t ) ) |
|---|
| | 1342 | { |
|---|
| | 1343 | UR_S_DROP; |
|---|
| | 1344 | } |
|---|
| | 1352 | } |
|---|
| | 1353 | |
|---|
| | 1354 | |
|---|
| | 1355 | enum ContextIndexAnimation |
|---|
| | 1356 | { |
|---|
| | 1357 | CI_ANIM_VALUE = 0, |
|---|
| | 1358 | CI_ANIM_CURVE, |
|---|
| | 1359 | CI_ANIM_PERIOD, |
|---|
| | 1360 | CI_ANIM_TIME, |
|---|
| | 1361 | CI_ANIM_BEHAVIOR, |
|---|
| | 1362 | CI_ANIM_CELLS |
|---|
| | 1363 | }; |
|---|
| | 1364 | |
|---|
| | 1365 | |
|---|
| | 1366 | /* |
|---|
| | 1367 | Return zero if error thrown. |
|---|
| | 1368 | */ |
|---|
| | 1369 | static int _animate( UThread* ut, UCell* acell, double dt ) |
|---|
| | 1370 | { |
|---|
| | 1371 | double period; |
|---|
| | 1372 | UCell* behav; |
|---|
| | 1373 | UCell* curve; |
|---|
| | 1374 | UCell* value; |
|---|
| | 1375 | UCell* timec; |
|---|
| | 1376 | UBlock* vblk = ur_blockPtr( acell->ctx.valBlk ); |
|---|
| | 1377 | UCell* vc = vblk->ptr.cells; |
|---|
| | 1378 | |
|---|
| | 1379 | if( vblk->used < CI_ANIM_CELLS ) |
|---|
| | 1380 | { |
|---|
| | 1381 | ur_throwErr( UR_ERR_SCRIPT, "Invalid animation context" ); |
|---|
| | 1382 | return 0; |
|---|
| | 1383 | } |
|---|
| | 1384 | |
|---|
| | 1385 | curve = vc + CI_ANIM_CURVE; |
|---|
| | 1386 | if( ! ur_is(curve, UT_BLOCK) ) |
|---|
| | 1387 | { |
|---|
| | 1388 | ur_throwErr( UR_ERR_DATATYPE, "animation curve must be a block!" ); |
|---|
| | 1389 | return 0; |
|---|
| | 1390 | } |
|---|
| | 1391 | |
|---|
| | 1392 | value = vc + CI_ANIM_VALUE; |
|---|
| | 1393 | if( ur_is(value, UT_BLOCK) ) |
|---|
| | 1394 | { |
|---|
| | 1395 | vblk = ur_block(value); |
|---|
| | 1396 | value = vblk->ptr.cells + value->series.it; |
|---|
| | 1397 | } |
|---|
| | 1398 | |
|---|
| | 1399 | behav = vc + CI_ANIM_BEHAVIOR; |
|---|
| | 1400 | if( ur_is(behav, UT_INT) ) |
|---|
| | 1401 | { |
|---|
| | 1402 | period = ur_decimal(vc + CI_ANIM_PERIOD); |
|---|
| | 1403 | |
|---|
| | 1404 | timec = vc + CI_ANIM_TIME; |
|---|
| | 1405 | dt += ur_decimal(timec); |
|---|
| | 1406 | if( dt > period ) |
|---|
| | 1407 | { |
|---|
| | 1408 | int repeat = ur_int(behav) - 1; |
|---|
| | 1409 | if( repeat < 1 ) |
|---|
| | 1410 | { |
|---|
| | 1411 | ur_setNone(behav); |
|---|
| | 1412 | } |
|---|
| | 1413 | else |
|---|
| | 1414 | { |
|---|
| | 1415 | dt -= period; |
|---|
| | 1416 | ur_int(behav) = repeat; |
|---|
| | 1417 | } |
|---|
| | 1418 | } |
|---|
| | 1419 | else |
|---|
| | 1420 | { |
|---|
| | 1421 | ur_decimal(timec) = dt; |
|---|
| | 1422 | } |
|---|
| | 1423 | |
|---|
| | 1424 | if( (period != 1.0) && (period > 0.0) ) |
|---|
| | 1425 | dt /= period; |
|---|
| | 1426 | |
|---|
| | 1427 | return _curveValue( ut, curve, value, dt ); |
|---|
| | 1428 | } |
|---|
| | 1429 | #if 0 |
|---|
| | 1430 | else if( ur_is(cell, UT_WORD) ) |
|---|
| | 1431 | { |
|---|
| | 1432 | switch( ur_atom(cell) ) |
|---|
| | 1433 | { |
|---|
| | 1434 | case UR_ATOM_LOOP: |
|---|
| | 1435 | case UR_ATOM_PING_PONG: |
|---|
| | 1436 | case UR_ATOM_PONG: |
|---|
| | 1437 | break; |
|---|
| | 1438 | } |
|---|
| | 1439 | } |
|---|
| | 1440 | #endif |
|---|
| | 1441 | return 1; |
|---|
| | 1442 | } |
|---|
| | 1443 | |
|---|
| | 1444 | |
|---|
| | 1445 | // (anims time -- ) |
|---|
| | 1446 | UR_CALL( uc_animate ) |
|---|
| | 1447 | { |
|---|
| | 1448 | UCell* prev; |
|---|
| | 1449 | double dt; |
|---|
| | 1450 | |
|---|
| | 1451 | if( ! ur_is(tos, UT_DECIMAL) ) |
|---|
| | 1452 | { |
|---|
| | 1453 | ur_throwErr( UR_ERR_DATATYPE, "animate expected decimal! time" ); |
|---|
| | 1454 | return; |
|---|
| | 1455 | } |
|---|
| | 1456 | dt = ur_decimal(tos); |
|---|
| | 1457 | |
|---|
| | 1458 | prev = ur_s_prev(tos); |
|---|
| | 1459 | if( ur_is(prev, UT_CONTEXT) ) |
|---|
| | 1460 | { |
|---|
| | 1461 | if( ! _animate( ut, prev, dt ) ) |
|---|
| | 1462 | return; |
|---|
| | 1463 | } |
|---|
| | 1464 | else if( ur_is(prev, UT_BLOCK) ) |
|---|
| | 1465 | { |
|---|
| | 1466 | UBlock* blk; |
|---|
| | 1467 | UCell* it; |
|---|
| | 1468 | UCell* end; |
|---|
| | 1469 | blk = ur_block(prev); |
|---|
| | 1470 | UR_ITER_BLOCK( it, end, blk, prev ); |
|---|
| | 1471 | while( it != end ) |
|---|
| | 1472 | { |
|---|
| | 1473 | if( ur_is(it, UT_CONTEXT) ) |
|---|
| | 1474 | { |
|---|
| | 1475 | if( ! _animate( ut, it, dt ) ) |
|---|
| | 1476 | return; |
|---|
| | 1477 | } |
|---|
| | 1478 | ++it; |
|---|
| | 1479 | } |
|---|
| | 1480 | } |
|---|
| | 1481 | else |
|---|
| | 1482 | { |
|---|
| | 1483 | ur_throwErr( UR_ERR_DATATYPE, "animate expected block!/context!" ); |
|---|
| | 1484 | return; |
|---|
| | 1485 | } |
|---|
| | 1486 | UR_S_DROPN(2); |
|---|