Function StartUnit

Line
1223
Location
common.ai
Constant?
no
Type
function
Arguments
integer ask_qty, integer unitid, integer town
Returns
boolean
1223
function StartUnit takes integer ask_qty, integer unitid, integer town returns boolean
1224
    local integer have_qty
1225
    local integer need_qty
1226
    local integer afford_gold
1227
    local integer afford_wood
1228
    local integer afford_qty
1229
    local integer gold_cost
1230
    local integer wood_cost
1231
1232
    //------------------------------------------------------------------------
1233
    // if we have all we're asking for then make nothing
1234
    //
1235
    if town == -1 then
1236
        set have_qty = TownCount(unitid)
1237
    else
1238
        set have_qty = TownCountTown(unitid,town)
1239
    endif
1240
1241
    if have_qty >= ask_qty then
1242
        return true
1243
    endif
1244
    set need_qty = ask_qty - have_qty
1245
1246
    //------------------------------------------------------------------------
1247
    // limit the qty we're requesting to the amount of resources available
1248
    //
1249
    set gold_cost = GetUnitGoldCost(unitid)
1250
    set wood_cost = GetUnitWoodCost(unitid)
1251
1252
    if gold_cost == 0 then
1253
        set afford_gold = need_qty
1254
    else
1255
        set afford_gold = total_gold / gold_cost
1256
    endif
1257
    if afford_gold < need_qty then
1258
        set afford_qty = afford_gold
1259
    else
1260
        set afford_qty = need_qty
1261
    endif
1262
1263
    if wood_cost == 0 then
1264
        set afford_wood = need_qty
1265
    else
1266
        set afford_wood = total_wood / wood_cost
1267
    endif
1268
    if afford_wood < afford_qty then
1269
        set afford_qty = afford_wood
1270
    endif
1271
1272
    // if we're waiting on gold/wood; pause build orders
1273
    if afford_qty < 1 then
1274
        return false
1275
    endif
1276
1277
    //------------------------------------------------------------------------
1278
    // whether we make right now what we're requesting or not, assume we will
1279
    // and deduct the cost of the units from our fake gold total right away
1280
    //
1281
    set total_gold = total_gold - gold_cost * need_qty
1282
    set total_wood = total_wood - wood_cost * need_qty
1283
1284
    if total_gold < 0 then
1285
        set total_gold = 0
1286
    endif
1287
    if total_wood < 0 then
1288
        set total_wood = 0
1289
    endif
1290
1291
    //------------------------------------------------------------------------
1292
    // give the AI a chance to make the units (it may not be able to right now
1293
    // but that doesn't stop us from trying other units after this as long
1294
    // as we have enough money to make this AND the needed, unbuilt ones)
1295
    //
1296
    return SetProduce(afford_qty,unitid,town)
1297
endfunction