Search in sources :

Example 11 with TIntList

use of gnu.trove.list.TIntList in project Terasology by MovingBlocks.

the class NetEntityRefTypeHandler method deserialize.

@Override
public EntityRef deserialize(PersistedData data, DeserializationContext context) {
    if (data.isArray()) {
        PersistedDataArray array = data.getAsArray();
        if (array.isNumberArray() && array.size() == 3) {
            TIntList items = data.getAsArray().getAsIntegerArray();
            Vector3i pos = new Vector3i(items.get(0), items.get(1), items.get(2));
            return blockEntityRegistry.getBlockEntityAt(pos);
        }
    }
    if (data.isNumber()) {
        return networkSystem.getEntity(data.getAsInteger());
    }
    return EntityRef.NULL;
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) Vector3i(org.terasology.math.geom.Vector3i) TIntList(gnu.trove.list.TIntList)

Example 12 with TIntList

use of gnu.trove.list.TIntList in project Terasology by MovingBlocks.

the class LocalChunkProviderTest method testCompleteUpdateSendsBlockActivatedEvents.

@Test
public void testCompleteUpdateSendsBlockActivatedEvents() throws Exception {
    final Chunk chunk = mockChunkAt(0, 0, 0);
    final TShortObjectHashMap<TIntList> blockPositionMappings = new TShortObjectHashMap<>();
    final short blockId = 42;
    final EntityRef blockEntity = mock(EntityRef.class);
    registerBlockWithIdAndEntity(blockId, blockEntity, blockManager);
    blockPositionMappings.put(blockId, withPositions(new Vector3i(1, 2, 3)));
    final ReadyChunkInfo readyChunkInfo = ReadyChunkInfo.createForRestoredChunk(chunk, blockPositionMappings, mock(ChunkStore.class), Collections.emptyList());
    when(chunkFinalizer.completeFinalization()).thenReturn(readyChunkInfo);
    chunkProvider.completeUpdate();
    final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
    verify(blockEntity, atLeastOnce()).send(eventArgumentCaptor.capture());
    final Event event = eventArgumentCaptor.getAllValues().get(1);
    assertThat(event, instanceOf(OnActivatedBlocks.class));
    assertThat(((OnActivatedBlocks) event).getBlockPositions(), hasItem(new Vector3i(1, 2, 3)));
}
Also used : OnActivatedBlocks(org.terasology.world.block.OnActivatedBlocks) TShortObjectHashMap(gnu.trove.map.hash.TShortObjectHashMap) Vector3i(org.terasology.math.geom.Vector3i) ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo) Event(org.terasology.entitySystem.event.Event) Chunk(org.terasology.world.chunks.Chunk) TIntList(gnu.trove.list.TIntList) EntityRef(org.terasology.entitySystem.entity.EntityRef) ChunkStore(org.terasology.persistence.ChunkStore) Test(org.junit.Test)

Example 13 with TIntList

use of gnu.trove.list.TIntList in project Terasology by MovingBlocks.

the class LocalChunkProviderTest method testCompleteUpdateSendsBlockAddedEvents.

@Test
public void testCompleteUpdateSendsBlockAddedEvents() throws Exception {
    final Chunk chunk = mockChunkAt(0, 0, 0);
    final short blockId = 42;
    final EntityRef blockEntity = mock(EntityRef.class);
    registerBlockWithIdAndEntity(blockId, blockEntity, blockManager);
    final TShortObjectHashMap<TIntList> blockPositionMappings = new TShortObjectHashMap<>();
    blockPositionMappings.put(blockId, withPositions(new Vector3i(1, 2, 3)));
    final ReadyChunkInfo readyChunkInfo = ReadyChunkInfo.createForRestoredChunk(chunk, blockPositionMappings, mock(ChunkStore.class), Collections.emptyList());
    when(chunkFinalizer.completeFinalization()).thenReturn(readyChunkInfo);
    chunkProvider.completeUpdate();
    final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
    verify(blockEntity, atLeastOnce()).send(eventArgumentCaptor.capture());
    final Event event = eventArgumentCaptor.getAllValues().get(0);
    assertThat(event, instanceOf(OnAddedBlocks.class));
    assertThat(((OnAddedBlocks) event).getBlockPositions(), hasItem(new Vector3i(1, 2, 3)));
}
Also used : TShortObjectHashMap(gnu.trove.map.hash.TShortObjectHashMap) Vector3i(org.terasology.math.geom.Vector3i) ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo) Event(org.terasology.entitySystem.event.Event) OnAddedBlocks(org.terasology.world.block.OnAddedBlocks) Chunk(org.terasology.world.chunks.Chunk) TIntList(gnu.trove.list.TIntList) EntityRef(org.terasology.entitySystem.entity.EntityRef) ChunkStore(org.terasology.persistence.ChunkStore) Test(org.junit.Test)

Example 14 with TIntList

use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.

the class SkipStop method apply.

@Override
public Collection<TripPattern> apply(TripPattern original) {
    if (!couldMatch(original))
        return Arrays.asList(original);
    // figure out which stops we skip
    TIntList skippedStops = new TIntArrayList();
    // retained stops
    // we use stop times to carry a little additional information, e.g. pickup/dropoff type.
    List<StopTime> stopTimes = Lists.newArrayList();
    {
        int i = 0;
        for (Stop stop : original.getStops()) {
            if (stopId.contains(stop.getId().getId()))
                skippedStops.add(i);
            else {
                // make a fake stop time
                StopTime stopTime = new StopTime();
                stopTime.setStop(stop);
                stopTime.setPickupType(original.stopPattern.pickups[i]);
                stopTime.setDropOffType(original.stopPattern.dropoffs[i]);
                stopTimes.add(stopTime);
            }
            i++;
        }
    }
    if (skippedStops.isEmpty()) {
        LOG.warn("No stops found to skip on matched trip pattern {}", original);
        return Arrays.asList(original);
    }
    if (original.getStops().size() - skippedStops.size() < 2) {
        // TODO best way to handle this case?
        LOG.warn("Trip with skipped stops would have less that two stops for TripPattern {}, not skipping stops", original);
        return Arrays.asList(original);
    }
    // make the new stop pattern
    StopPattern sp = new StopPattern(stopTimes);
    TripPattern modified = new TripPattern(original.route, sp);
    // Any trips that are not matched keep the original trip pattern, so put them here.
    TripPattern originalClone = new TripPattern(original.route, original.stopPattern);
    // keep track of what we have to return
    boolean anyTripsMatched = false;
    boolean allTripsMatched = true;
    for (TripTimes tt : original.scheduledTimetable.tripTimes) {
        if (!matches(tt.trip)) {
            // this trip should not be modified
            allTripsMatched = false;
            originalClone.scheduledTimetable.addTripTimes(tt);
        } else {
            // This trip should be modified
            anyTripsMatched = true;
            modified.scheduledTimetable.addTripTimes(omitStops(tt, skippedStops.toArray()));
        }
    }
    for (FrequencyEntry fe : original.scheduledTimetable.frequencyEntries) {
        if (!matches(fe.tripTimes.trip)) {
            allTripsMatched = false;
            originalClone.scheduledTimetable.addFrequencyEntry(fe);
        } else {
            anyTripsMatched = true;
            TripTimes newtt = omitStops(fe.tripTimes, skippedStops.toArray());
            FrequencyEntry newfe = new FrequencyEntry(fe.startTime, fe.endTime, fe.headway, fe.exactTimes, newtt);
            modified.scheduledTimetable.addFrequencyEntry(newfe);
        }
    }
    if (!anyTripsMatched)
        return Arrays.asList(original);
    List<TripPattern> ret = Lists.newArrayList();
    ret.add(modified);
    if (!allTripsMatched)
        ret.add(originalClone);
    return ret;
}
Also used : StopPattern(org.opentripplanner.model.StopPattern) Stop(org.onebusaway.gtfs.model.Stop) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) FrequencyEntry(org.opentripplanner.routing.trippattern.FrequencyEntry) TIntList(gnu.trove.list.TIntList) TIntArrayList(gnu.trove.list.array.TIntArrayList) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 15 with TIntList

use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.

the class GTFSPatternHopFactory method removeRepeatedStops.

/**
 * Filter out any series of stop times that refer to the same stop. This is very inefficient in
 * an array-backed list, but we are assuming that this is a rare occurrence. The alternative is
 * to copy every list of stop times during filtering.
 *
 * TODO: OBA GFTS makes the stoptime lists unmodifiable, so this will not work.
 * We need to copy any modified list.
 *
 * @return whether any repeated stops were filtered out.
 */
private TIntList removeRepeatedStops(List<StopTime> stopTimes) {
    boolean filtered = false;
    StopTime prev = null;
    Iterator<StopTime> it = stopTimes.iterator();
    TIntList stopSequencesRemoved = new TIntArrayList();
    while (it.hasNext()) {
        StopTime st = it.next();
        if (prev != null) {
            if (prev.getStop().equals(st.getStop())) {
                // This is particularly important at the last stop in a route (see issue #2220)
                if (prev.getArrivalTime() == StopTime.MISSING_VALUE)
                    prev.setArrivalTime(st.getArrivalTime());
                // prefer to replace with the departure time of this stop time, unless this stop time has no departure time
                if (st.getDepartureTime() != StopTime.MISSING_VALUE)
                    prev.setDepartureTime(st.getDepartureTime());
                it.remove();
                stopSequencesRemoved.add(st.getStopSequence());
            }
        }
        prev = st;
    }
    return stopSequencesRemoved;
}
Also used : TIntList(gnu.trove.list.TIntList) TIntArrayList(gnu.trove.list.array.TIntArrayList) StopTime(org.onebusaway.gtfs.model.StopTime)

Aggregations

TIntList (gnu.trove.list.TIntList)35 TIntArrayList (gnu.trove.list.array.TIntArrayList)21 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 List (java.util.List)5 TFloatList (gnu.trove.list.TFloatList)4 ItemStack (net.minecraft.item.ItemStack)4 BlockPos (net.minecraft.util.math.BlockPos)4 Vector2f (org.terasology.math.geom.Vector2f)4 Vector3i (org.terasology.math.geom.Vector3i)4 TIntIterator (gnu.trove.iterator.TIntIterator)3 TShortObjectHashMap (gnu.trove.map.hash.TShortObjectHashMap)3 InputStream (java.io.InputStream)3 Arrays (java.util.Arrays)3 Vector3f (org.terasology.math.geom.Vector3f)3 Lists (com.google.common.collect.Lists)2 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)2 IvMutableBlockPos (ivorius.ivtoolkit.blocks.IvMutableBlockPos)2 IntegerRange (ivorius.ivtoolkit.gui.IntegerRange)2 Consumer (java.util.function.Consumer)2