const int player = 1; const int threshold = 5; const int cObjBombardCannon = 36; int cannonsCount = 0; int printed = -1; bool cannonsCondition () { return (cannonsCount >= threshold); } mutable void checkForNewUnits(){} mutable void printNewUnits(int interval = 1000){} rule cannonsUpdateCount { static int playerId = -1; static int cannonArr = -1; static int uidArr = -1; static int lastDisplay = 0; printNewUnits(); return; if(printed >= 0 && printed < cannonsCount){ int now = xsGetWorldTime(); if(now - lastDisplay < 1000) return; lastDisplay = now; int duid = xsArrayGetInt(cannonArr, printed); int ct = xsGetTurn(); string uName = xsGetUnitName(duid); string civName = xsGetUnitCivName(duid); xsChatData(fstr("Tick{ct}: {printed}/{cannonsCount} {duid} {uName} ({civName})")); printed++; return; } if(printed > 0){ xsDisableSelf(); return; } printed = 0; if (playerId == -1) { playerId = xsGetWorldPlayerId(player); cannonArr = xsArrayCreateInt(0, -1); uidArr = xsArrayCreateInt(0, -1); } uidArr = xsGetPlayerUnitIds(playerId, -1, uidArr); //uidArr = xs for (i = 0; < xsArrayGetSize(uidArr)) { int uid = xsArrayGetInt(uidArr, i); bool isNew = true; //xsChatData(fstr("Cannons owned so far: {cannonsCount}/{threshold}")); for (j = 0; < cannonsCount) { if (xsArrayGetInt(cannonArr, j) == uid) { isNew = false; break; } } if (isNew) { cannonsCount++; xsArrayResizeInt(cannonArr, cannonsCount); xsArraySetInt(cannonArr, cannonsCount-1, uid); //xsChatData(fstr("Cannons owned so far: {cannonsCount}/{threshold}")); } } //xsDisableSelf(); } void printUnitInfo(int unitID = 0, string prefix = ""){ static int prUnits = 0; string uName = xsGetUnitName(unitID); string civName = xsGetUnitCivName(unitID); xsChatData(prefix + fstr("{prUnits} ID:{unitID} {uName} ({civName})")); prUnits++; } mutable int getNewUnitsIds(){ return (-1);} void printNewUnits(int interval = 1000){ static int pendingPrints = -1; static int currentPrint = 0; static int lastPrintTime = 0; if(pendingPrints == -1){ pendingPrints = xsArrayCreateInt(0, -1); } int news = getNewUnitsIds(); int newsCount = xsArrayGetSize(news); if(newsCount > 0){ //xsChatData("New units for printing!"); int pendingCount = xsArrayGetSize(pendingPrints); xsArrayResizeInt(pendingPrints, pendingCount + newsCount); for (i = 0; < xsArrayGetSize(news)) { int nu = xsArrayGetInt(news, i); xsChatData(fstr("Queued {nu}")); xsArraySetInt(pendingPrints, pendingCount+i, nu); } } int now = xsGetWorldTime(); if(now - lastPrintTime < interval) return; lastPrintTime = now; pendingCount = xsArrayGetSize(pendingPrints); //xsChatData(fstr("Hello? {currentPrint}/{pendingCount}")); if(currentPrint == pendingCount){ xsArrayResizeInt(pendingPrints, 0); currentPrint = 0; pendingCount = 0; }else if (currentPrint < pendingCount) { //xsChatData(fstr("printing: {currentPrint}/{pendingCount}")); int current = xsArrayGetInt(pendingPrints, currentPrint); currentPrint++; printUnitInfo(current, "New unit: "); } } int newUnits = -1; int newUnitsCount = 0; int removedUnits = -1; int removedUnitsCount = 0; int changeCheckTurn = -1; int getNewUnitsIds(){ checkForNewUnits(); return (newUnits); } int getAllUnitsIDs(int playerId = 1, int array = -1){ static int units = -1; for(i = 900; < 965) { units = xsGetPlayerUnitIds(playerId, i, units); int cc = xsArrayGetSize(units);//current count if (cc == 0) continue; int ts = xsArrayGetSize(array); xsArrayResizeInt(array, ts + cc); for(j = 0; < xsArrayGetSize(units)) { xsArraySetInt(array, ts + j, xsArrayGetInt(units, j)); } } return (array); } /** Direction: -1 = allow shirnk only, 0 = resize both ways, 1 = allow growth only **/ void resizeInt(int array = -1, int sizeNeeded = -1, int direction = 0, int defaultItem = 0){ if (sizeNeeded < 0) { xsChatData("Error: Negative size passed to `resizeInt()`"); return; } int initialSize = xsArrayGetSize(array); int newsCount = sizeNeeded - initialSize; if(newsCount == 0) return; if(newsCount < 0 && direction > 0) return; if(newsCount > 0 && direction < 0) return; xsChatData(fstr("Resizing all units to {sizeNeeded} from {initialSize}")); bool success = xsArrayResizeInt(array, sizeNeeded); // int newSize = xsArrayGetSize(array); // xsChatData(fstr("New size {newSize} success: {success}")); if (newsCount > 0) { for(i = 0; < newsCount) { xsArraySetInt(array, initialSize + i, defaultItem); } } } void growInt(int array = -1, int sizeNeeded = -1, int defaultItem = 0){ resizeInt(array, sizeNeeded, 1, defaultItem); } void shrinkInt(int array = -1, int sizeNeeded = -1, int defaultItem = 0){ resizeInt(array, sizeNeeded, -1, defaultItem); } void checkForNewUnits(){ static int playerId = -1; static int currentUnits = -1; static int allUnits = 0; static int lastCount = 0; int ct = xsGetTurn(); if(ct == changeCheckTurn) return; changeCheckTurn = ct; if (playerId == -1) { playerId = xsGetWorldPlayerId(player); currentUnits = xsArrayCreateInt(0, -1); allUnits = xsArrayCreateInt(0, -1); newUnits = xsArrayCreateInt(64, -1); removedUnits = xsArrayCreateInt(64, -1); xsChatData(fstr("PLayer: {playerId} Turn:{changeCheckTurn}")); } //currentUnits = xsGetPlayerUnitIds(playerId, 36, currentUnits); currentUnits = getAllUnitsIDs(playerId, currentUnits); //int cuc = xsArrayGetSize(currentUnits); // if(lastCount != cuc){ // xsChatData(fstr("Counted: {cuc}")); // lastCount = cuc; // } for (i = 0; < xsArrayGetSize(currentUnits)) { int u = xsArrayGetInt(currentUnits, i); if(u < 0){ xsChatData(fstr("invalid id: {u}")); continue; } int sizeNeeded = u + 1; growInt(allUnits, sizeNeeded, -1); // if (xsArrayGetSize(allUnits) < sizeNeeded){ // xsChatData(fstr("Resizing all units to {sizeNeeded}")); // xsArrayResizeInt(allUnits, sizeNeeded); // } int eu = xsArrayGetInt(allUnits, u); if (eu < 0){ //does not exist yet xsChatData(fstr("new unit! {u}")); xsArraySetInt(allUnits, u, changeCheckTurn); //growInt(newUnits, newUnitsCount+1, -1); if(xsArrayGetSize(newUnits) < newUnitsCount+1) xsArrayResizeInt(newUnits, xsArrayGetSize(newUnits)*2); xsArraySetInt(newUnits, newUnitsCount, u); newUnitsCount++; } else { //alive if(lastCount == 0) { xsChatData(fstr("[{u}]alive! {eu}")); lastCount++; } xsArraySetInt(allUnits, u, changeCheckTurn); } } for (j = 0; < xsArrayGetSize(allUnits)) { eu = xsArrayGetInt(allUnits, j); if (eu == changeCheckTurn) continue; //still alive if (eu < 0) continue; //already killed/not alive yet xsChatData(fstr("killed unit! {j} {eu}")); xsArraySetInt(allUnits, j, -2); //killed this turn if(xsArrayGetSize(removedUnits) < removedUnitsCount+1) xsArrayResizeInt(removedUnits, xsArrayGetSize(removedUnits)*2); xsArraySetInt(removedUnits, removedUnitsCount, j); removedUnitsCount++; } xsArrayResizeInt(newUnits, newUnitsCount); xsArrayResizeInt(removedUnits, removedUnitsCount); if(newUnitsCount > 0) xsChatData(fstr("Counted {newUnitsCount} new units")); if(removedUnitsCount > 0) xsChatData(fstr("Counted {removedUnitsCount} removed units")); newUnitsCount = 0; removedUnitsCount = 0; }